There are two good reasons to follow. If you know some code, but don’t know anything about blockchain, it will help you better understand what blockchain is or, more specifically, the structure of blocks in Bitcoin. The second reason to follow is if you know a few things about blockchain but are just beginning your coding journey, this will be a fun little project to build on.
Without further ado, let’s go!
First, we will need to use a crypto library so that we can perform one of the most common functions of Bitcoin and blockchain technology: hashing. The crypto library that we will be using is the very popular crypto-js library. Indeed, it is overkill to load the whole library when I can only use the SHA256 component, but let’s keep it simple and short. So from a terminal window (command prompt), type npm install crypto-js
Our first line will require loading this library object into a variable. We will use the name CryptoJS.
With this we have a “Block” class which will allow us to easily create countless Block instances in our blockchain. We therefore create a constructor to allow us to easily and dynamically create our Blocks. Each block will require the definition of a certain number of values. These are the index (usually referred to as the block height), the timestamp, the merkle root of transactions, and the previous block hash. These are common attributes for a block, but there are others in Bitcoin, the Nuncio being a notable (used in mining).
The index will be a number that we assign to each block, consecutively. You will see that many block explorers call this “block height” when they refer to a particular block. They refer to the value of the index.
The timestamp is self-explanatory, so we switch to txHash. It will be a hypothetical value that we plug in here. Our nano-sized blockchain will actually have no transactions in the blocks, so we’re just going to put in some arbitrary data to represent that. (We didn’t want to leave it out because it is important for the structure of a block). In Bitcoin, block headers contain the “Merkle root,” which is a hash of all transactions in that block. So we’ll at least provide the hash to represent it later in the program.
The “previousHash” parameter is probably the real key here, as this is the link that creates the “chain” to the blockchain. Each block contains a hash of the previous block. So every time you create a new block, we want to create pass in the hash of the last block as an argument. It can execute the hash function inline in the constructor, so just pass the block.
With our blocks properly defined, we can create a simple function that we can call to generate the next block.
As mentioned earlier, this function will put arbitrary data to represent transactions and return a new block. Since we are passing the last block as an argument, we have all the variables needed to call a new instance of Block.
Great! Now we just have to initialize the blockchain board and create our very first block: The Genesis Block.
Creating the data manually in the genesis block will allow us to start our chain as it will provide the first set of values that will be hashed in the next block. Remember that each block requires the hash of the previous block. Satoshi did the same in Bitcoin, and we’ll do the same here. The Genesis block is unique for this reason, and in Bitcoin it does not have a usable coin.
With our genesis block created, we can start a simple loop, which will generate as many blocks sequentially as we want. In this example, we will do 100.
For the sake of readability, we’ve put a bit of formatting in there. There you go, a pocket-sized blockchain that creates a sequence of blocks with a bare bone structure in less than 30 lines of code!
Of course there is a lot more if you want real utility… We would need to create functionality for mining, transactions, networking and much more. But if you can understand this basic structure, you are already way ahead of understanding blocks.
The fundamental nature of Bitcoin is that each block builds on top of the previous block. And all proofs of work that go into the last block are hashed in the current block. The continuation of the proof-of-work cycle sees Bitcoin constantly strengthening its security. But it requires mining …
So if you want to build a bit on that, you can try adding a Nonce (with a random number) to the constructor, and before displaying each block, the program has to guess what the Nonce is repeatedly. and don’t give up until he guesses correctly. . This would provide you with a basic imitation of the proof of work feature and how miners solve the Nuncio.
Special thanks to Jonathan Hicks for his contribution.
New to Bitcoin? Discover CoinGeek Bitcoin for beginners section, the ultimate resource guide to learning more about Bitcoin – as originally envisioned by Satoshi Nakamoto – and blockchain.