Bitcoin Forum
November 08, 2024, 08:40:08 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Trying a little experiment using a programming language.  (Read 199 times)
finaleshot2016 (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1009


Degen in the Space


View Profile WWW
August 11, 2019, 09:47:46 AM
Merited by friends1980 (2), LoyceV (1), Quickseller (1), mk4 (1), hugeblack (1), DdmrDdmr (1), BQ (1)
 #1

So I just want to share this little achievement of mine, I know it's kinda easy for some to make this but for me as a beginner, it's very satisfying. So I made a simple blockchain using a Programming language which is Python. Some of you already know what Python is, it's a programming language that is similar to c+, java and many more. You can use it to manipulate modules especially advanced devices to work based on your instructions.

I made a twist here, I'm actually doing this experiment by using my Raspberry Pi. --


It's kinda basic though but for me, it's another learning that I've achieved today.

So these are the codes that I've used on my blockchain.
Code:
import datetime
import hashlib

class Block:
    blockNo = 0
    data = None
    next = None
    hash = None
    nonce = 0
    previous_hash = 0x0
    timestamp = datetime.datetime.now()

    def __init__(self, data):
        self.data = data

    def hash(self):
        h = hashlib.sha256()
        h.update(
        str(self.nonce).encode('utf-8') +
        str(self.data).encode('utf-8') +
        str(self.previous_hash).encode('utf-8') +
        str(self.timestamp).encode('utf-8') +
        str(self.blockNo).encode('utf-8')
        )
        return h.hexdigest()

    def __str__(self):
        return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------"

class Blockchain:

    diff = 20
    maxNonce = 2**32
    target = 2 ** (256-diff)

    block = Block("Genesis")
    dummy = head = block

    def add(self, block):

        block.previous_hash = self.block.hash()
        block.blockNo = self.block.blockNo + 1

        self.block.next = block
        self.block = self.block.next

    def mine(self, block):
        for n in range(self.maxNonce):
            if int(block.hash(), 16) <= self.target:
                self.add(block)
                print(block)
                break
            else:
                block.nonce += 1

blockchain = Blockchain()

for n in range(10):
    blockchain.mine(Block("Block " + str(n+1)))

while blockchain.head != None:
    print(blockchain.head)
    blockchain.head = blockchain.head.next
credits to howCodeORG of github.

The code is already existing but the twist here is I used my RPi.  Cheesy
I also know that some of you aren't familiar with this kind of software so here it goes.


Once I put all of the codes in the terminal of Rpi, it's now ready to run.
This is the output of blockchain.py.


It generates its own Block Hash and Hashes. You can also try it on your pc/laptop, RPi is also applicable since it's the device that I've used to show this.
--

Thanks for reading!

████████▄▄▄▄▄▄▀▀▀▀▀▀▄
███▄▀▀▀▀▀███████████
███▐▌████████████▀█▀▐▌
███▐▌███▄█▀█████████████████▄▄▄▄
▄▀█████▐█████████▄▄▄▐█▌▄█▌██▀▀
██████▐███▐██▌▄█▀▀▀▐█████▀███▄
▐█
██▐▌██▐████▌█▌█▌███▐█▌█▄▄▄▄██
▐██
▐▌██▐█▌▐█▀█▌▀█▄▄█▐███▀▀▀▀▀▀
████████▐█▌█▌▀▀▀██▀▀████▄▌████▄
███▄███▌▐████▄██▌█▌██▐████▌█▌▄█▀
██▐█▄▄▄▄██████████▌██▐████▌█▌▐██
███▀███▀▀████▌█████▄▄▐█▄▄█▌██▀▀
████████████▀███▌▀▀▀▀██▀▀

 ......NO FEES ON BITCOIN WITHDRAWALS...... 

▄▄███████▄▄
▄███████████████▄
▄███████████████████▄
▄█████████████████████▄
▄███████████████████████▄
█████████████████████████
████████████████████████
█████████████████████████
▀██████████████████████▀
▀█████████████████████▀
▀███████████████████▀
▀███████████████▀
▀▀███████▀▀

▀███████████▀
[
[
RELOAD
BONUS
 

RAKEBACK
BONUS
]
]
[
[
FREE
COINS
 

VIP
REWARDS
]
]
 
........► Play Now .... 
BQ
Member
**
Offline Offline

Activity: 616
Merit: 53

CoinMetro - the future of exchanges


View Profile
August 11, 2019, 10:55:18 PM
 #2

Cool and interesting! Good job  Smiley
How is it stored?

5 EUR free crypto on signup! XCM exchange token ⚜⚜⚜  CoinMetro - Copy Trading - Margin - 0% maker fee  ⚜⚜⚜ - generate €500 volume in July for a chance to win €100!
pooya87
Legendary
*
Offline Offline

Activity: 3626
Merit: 11020


Crypto Swap Exchange


View Profile
August 12, 2019, 03:59:01 AM
 #3

i am not familiar with python but your code looks broken to me. it is accepting the first hash without proper comparison with the target you provide which is why all of your nonces are zero.
two more things.
you are using utf-8, which may be wrong since usually the data you fetch is in hex
also if this is for Bitcoin, then the hash function is double SHA256 meaning SHA256(SHA256(header)). if it is a test then use a faster hash algorithm such as MD4. if you do use another hash function you also must change your target based on the hash digest size (128 bit in case of MD4)

PS. keep it up.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
finaleshot2016 (OP)
Legendary
*
Offline Offline

Activity: 1778
Merit: 1009


Degen in the Space


View Profile WWW
August 12, 2019, 05:30:29 AM
 #4

i am not familiar with python but your code looks broken to me. it is accepting the first hash without proper comparison with the target you provide which is why all of your nonces are zero.
two more things.
you are using utf-8, which may be wrong since usually the data you fetch is in hex
also if this is for Bitcoin, then the hash function is double SHA256 meaning SHA256(SHA256(header)). if it is a test then use a faster hash algorithm such as MD4. if you do use another hash function you also must change your target based on the hash digest size (128 bit in case of MD4)

PS. keep it up.

I think the diff=0 that's why nonces are zero too. I'll try to fix it.

--
changed the diff=20.

████████▄▄▄▄▄▄▀▀▀▀▀▀▄
███▄▀▀▀▀▀███████████
███▐▌████████████▀█▀▐▌
███▐▌███▄█▀█████████████████▄▄▄▄
▄▀█████▐█████████▄▄▄▐█▌▄█▌██▀▀
██████▐███▐██▌▄█▀▀▀▐█████▀███▄
▐█
██▐▌██▐████▌█▌█▌███▐█▌█▄▄▄▄██
▐██
▐▌██▐█▌▐█▀█▌▀█▄▄█▐███▀▀▀▀▀▀
████████▐█▌█▌▀▀▀██▀▀████▄▌████▄
███▄███▌▐████▄██▌█▌██▐████▌█▌▄█▀
██▐█▄▄▄▄██████████▌██▐████▌█▌▐██
███▀███▀▀████▌█████▄▄▐█▄▄█▌██▀▀
████████████▀███▌▀▀▀▀██▀▀

 ......NO FEES ON BITCOIN WITHDRAWALS...... 

▄▄███████▄▄
▄███████████████▄
▄███████████████████▄
▄█████████████████████▄
▄███████████████████████▄
█████████████████████████
████████████████████████
█████████████████████████
▀██████████████████████▀
▀█████████████████████▀
▀███████████████████▀
▀███████████████▀
▀▀███████▀▀

▀███████████▀
[
[
RELOAD
BONUS
 

RAKEBACK
BONUS
]
]
[
[
FREE
COINS
 

VIP
REWARDS
]
]
 
........► Play Now .... 
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!