
- #How to encode a message in block parity code how to#
- #How to encode a message in block parity code code#
Next, we proceed to the algorithm that needs to be solved by the miner. Your create_blockchain() function should be similar to the one shown below: Previous hash: Lastly, the previous hash key takes a value of previous_hash from the function which is equivalent to the hash of the previous block.īy adding these key-value pairs, we then append this block to the chain and return the block itself. Note that this variable refers to the proof of work. Proof: This key will receive a proof value that will be passed to the function when called. Timestamp: The timestamp key will take a value of the current Date and Time the block was created or mined. We will use this variable to access each block in the chain. It is represented by the chain variable in the _init_ method with an added value of one(1). Index: An index key will store the blockchain’s length. The dictionary will take the following key-value pairs: Within the create_blockchain function, we include a block variable of type dictionary that will be used to define each block in a blockchain. The only difference here is that we will pass in three parameters which are self, proof, and previous_hash.Īll parameters are without a default value. Next, we define a create_blockchain method that extends the Genesis block. create_blockchain(proof = 1, previous_hash = '0') In a blockchain, there is always a first block called the Genesis block, and this block does not have any previous_hash.
#How to encode a message in block parity code code#
This aspect of the code shows the importance of having a background on how blockchain works. The create_blockchain() method will take two default arguments which are proof with a value of one(1), and the previous_hash with a value of zero(0). The create_blockchain() method will allow us to create our Genesis block on instantiation of the class. The _init_ method will consist of a variable called chain to store a list of all the blocks in the blockchain. To start building our blockchain, we create a Blockchain class. Jsonify from the Flask library will be used to return messages to Postman. The hahshlib will be used to hash a block, JSON will be used to encode the block before we hash it. We use the DateTime library to attach a timestamp to each block that is created or mined.

In the blockchain.py file, we import the following packages as they are required in building our blockchain: We begin by launching our IDE and installing Flask in our virtual environment.Īfter setting up our environment, we create a new file for our code, and name it blockchain.py. Let’s proceed to build our first blockchain.

The GET request allows us to retrieve the actual state of our blockchain or to mine a block. We also require Postman to make requests to interact with our blockchain.

We make use of Flask to create a web application containing blockchain technology.
#How to encode a message in block parity code how to#
This article provides a step-by-step guide on how to create a simple blockchain using Python
