PurposeTo implement a Blockchain protocol by creating a cryptographically secure data structure in Python. What is BlockchainBefore we look at the implementation, lets take a quick look at what is a Blockchain. As per Wikipedia: "A blockchain is a growing list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data (generally represented as a merkle tree root hash). By design, a blockchain is resistant to modification of the data. It is "an open, distributed ledger that can record transactions between two parties efficiently and in a verifiable and permanent way." We will implement the Blockchain data structure by providing ability to group current transactions, create blocks and then link that block with the current blockchain by applying a proof-of-work (PoW) algorithm. Required Libraries and Initializing SchemaWe will need Python's hashlib and json libraries.
Next, we need to define the schema for Transactions and Blocks.
Imports and Setting schema for Transaction & Block
Create a Genesis blockWe need to initialize the Blockchain by first creating an empty block, called the genesis block. Since, this is the first block we manually specify value for Previous Hash and a Proof Hash and use that to create this block. This is then added to our blockchain. Below code performs this functionality as seen from the output logs. Genesis Block
Now let's add some transactionsTo create a transaction, we need 3 required values - From, To, and Amount, as seen in the schema above. Below functions helps us in creating these transactions and have them added to the list of pending (aka mineable transactions). Create transactions
Create Block and MineNow, its time to do the real stuff - Proof-of-Work (PoW) implementation by using SHA-256 cryptographic hash algorithms. In order to perform PoW, we need two inputs:
We use input 1) - and dump it into JSON format (as show below in hash_block function) in an ordered way to maintain sanity over hashes. This dump is then passed onto hashlib's sha256 algorithm (also shown below). Now, we have LastProof and Hash of LastBlock. Let's start the guess work (aka Proof-of-work) by incrementing a variable, called guess, by 1 and then computing its hash concatenated with already computed LastProof and LastBlock's hash. As soon as we get a hash that starts with "0000" (or we can change it depending on difficulty level we want to set), that "guess" becomes our proof for this block. Functions to create block and perform PoW
Source-code
|
AuthorI am a passionate, driven polyglot programmer and architect with a knack of solving complex problems in quick and efficient way. Along with programming, software development, financial products, and management expertise, I also bring skills in statistical modeling, empowering me to work on challenging projects that require combination of software development and quantitative analysis. Categories
All
|