Created
April 29, 2018 01:20
-
-
Save mega-byte2600/ff9a11d006ef36caafa2ccc622dccd9a to your computer and use it in GitHub Desktop.
<50 lines of py for sCoin blockchain by GeraldNash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import hashlib as hasher | |
class Block: | |
def __init__(self, index, timestamp, data, previous_hash): | |
self.index = index | |
self.timestamp = timestamp | |
self.data = data | |
self.previous_hash = previous_hash | |
self.hash = self.hash_block() | |
def __str__(self): | |
return 'Block #{}'.format(self.index) | |
def hash_block(self): | |
sha = hasher.sha256() | |
seq = (str(x) for x in ( | |
self.index, self.timestamp, self.data, self.previous_hash)) | |
sha.update(''.join(seq).encode('utf-8')) | |
return sha.hexdigest() | |
def make_genesis_block(): | |
"""Make the first block in a block-chain.""" | |
block = Block(index=0, | |
timestamp=datetime.now(), | |
data="Genesis Block", | |
previous_hash="0") | |
return block | |
def next_block(last_block, data=''): | |
"""Return next block in a block chain.""" | |
idx = last_block.index + 1 | |
block = Block(index=idx, | |
timestamp=datetime.now(), | |
data='{}{}'.format(data, idx), | |
previous_hash=last_block.hash) | |
return block | |
def test_code(): | |
"""Test creating chain of 77 blocks.""" | |
blockchain = [make_genesis_block()] | |
prev_block = blockchain[0] | |
for _ in range(0, 77): | |
block = next_block(prev_block, data='some data here') | |
blockchain.append(block) | |
prev_block = block | |
print('{} added to blockchain'.format(block)) | |
print('Hash: {}\n'.format(block.hash)) | |
# run the test code | |
test_code() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
*changed it to make 77 vs 20 blockchains---
Block #77 added to blockchain
Hash: 97a5d0d15bd6eaa7910c77c932e8caeb487368dac1857f6431107678d255ab04