Last active
October 11, 2017 06:47
-
-
Save laurentsenta/13b2836f2f8b599f5a543344cc3eed21 to your computer and use it in GitHub Desktop.
SingularGarden - How to implement a Blockchain - Init
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
# https://github.com/singulargarden/ouroboros/blob/v0.1/ouroboros/blockchain/__init__.py | |
ZERO_HASH = '0' * 96 | |
def make_block(previous_hash, payload): | |
"""Produce a new block""" | |
previous_hash_bytes = encode(previous_hash) | |
# Generate a fingerprint of the new state: | |
# hash the fingerprint of the previous state and the payload leading to the new state. | |
hash_ = sha3(previous_hash_bytes, payload) | |
# Serialize the block content for storage | |
bytes_ = previous_hash_bytes + b'\n' + payload | |
return Block(hash=hash_, previous_hash=previous_hash, | |
payload=payload, bytes=bytes_) | |
def init(path, genesis_payload=None): | |
""" | |
Init a new blockchain folder. | |
Use "now" for the initial, Genesis, payload if not provided. | |
""" | |
# Prepare the initial payload | |
if genesis_payload is None: | |
genesis_payload = bytes(str(time.time()), encoding="ascii") | |
# Create the initial block and the blockchain metadata | |
genesis_block = make_block(ZERO_HASH, genesis_payload) | |
descr = make_descr(genesis_block.hash, genesis_block.hash) | |
# Store the block and its description | |
update_storage(path, descr, genesis_block) | |
return genesis_block.hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment