Author’s note: Hacker News comments here.
I started 2022 with a mini batch at the Recurse Center. I did a full batch in Fall 2020, and hanging out with people working on their side projects full time seems like a fun way to kick off the year.
My plan was to port my toy blockchain from Python to Rust (repo here, write-up here). The initial project had multiple iterations, starting with proof-of-work and later Merkle trees and elliptic curve cryptography - a suitable candidate for a 1-week rewrite.
As with a lot of RC projects (for me anyway), things rarely play out as planned. I had moments of confusion with the Rust compiler, moved past that to contemplate working on speedups, but ended up compiling to WebAssembly to enable mining in the browser.
What I thought made for a nice ending was being able to connect the mini batch project with what I had previously worked on at RC.
For our purposes, a blockchain is a distributed database where records are divided into 'blocks' and the blocks form a 'chain'.
Each block is made up of a header (where block attributes live) and a body (where records live).
The hash of the header is used to identify each block (commonly referred to as the block hash) and is included in the header of the next block (hence the chain).
The Python function to initialize the header is relatively straightforward - a simple conversion from ints to bytes followed by a concatenation. Note that all the attributes are pre-determined except for the nonce.
VERSION: int = 0
def init_header(previous_hash: bytes, timestamp: int, nonce: int) -> bytes:
"""Initialize header."""
return (
VERSION.to_bytes(1, byteorder="big")
+ previous_hash
+ timestamp.to_bytes(4, byteorder="big")
+ nonce.to_bytes(32, byteorder="big")
)