Let's start by answering to the question "what’s a blockchain ?". It’s simply a distributed database over a P2P network.
The idea of a distributed and decentralized ledger system is probably one of the best things that have ever happened since the invention of the world wide web. Over the years it has found many applications and one of them is “currency“
In this post we are going to explore the main data structures found in almost any cryptocurrency based on the blockchain technology.
A blockchain in general is a hash pointer based data structure composed of :
It’s the main data structure. It does include:
- index: It’s the position of the block on the blockchain. For instance the first block for any blockchain has 0 as index
hash: It’s the result of a hash function to the components of the block. For example, the hash function used in Bitcoin is a variant of the SHA2 with 256 bits digest which is SHA256, In Ethereum SHA3 is used, …
previousHash: It’s the hash that links or chains a block to its predecessor. Reason behind the denomination “Blockchain”
timestamp : the UTC unix timestamp of when the block was created.
nonce: a 32/64 bits integer used in mining process.
numTx: the number of transactions in the block.
transactions: an array of all the transactions that are found in that block.
For transactions data, some implementations use Merkle trees for space optimization.
A transaction represents a transfer of ownership. And what is more interesting with cryptocurrencies is you can create some of the most interesting transactions you have ever dreamed of.
For example a transaction that needs all your kids to agree cryptographically for it to be unlocked. You don’t need a lawyer there.
It does include:
1.hash: It’s the a message digest of the data included in a transaction like its type, the transaction outputs and transaction inputs data.
2.type: It’s the type of the transaction. There are many types but the most important are :
(a) COIN BASE Transaction : cryptocurrency token supply mechanism.
(b) FEE Transaction: the fee that a transaction's creator tips to the
(c) REGULAR Transaction: A regular transfer of token ownership.
3.An array of transaction inputs and transaction outputs that I will talk about shortly.
Transaction input data is where you declare where the token you plan to transfer comes from.
What happens here is you point to the blockchain aware valid unspent transaction outputs, and you provide a digital signature that proves the ownership of those tokens.
It does include:
- txid :The id of the blockchain aware transaction where the token you try to unlock comes from.
- vout : Its index on that transaction’s outputs array.
- amount : the amount of token you are trying to unlock.
- unlockingScript : this is a list of script operation codes + data like digital signature and public key where you provide a script to unlock the tokens.
The transaction output represents where the tokens are being spent.
It does include:
- amount: the amount of tokens to be spent or transferred.
- locking script: its the script that sets the rules on the address that will be receiving the tokens. it does contain most of time some script operation codes and an address.
Congratulations ! We have got to the end of our exploration of the data structures found in a Blockchain system.
As a reminder stay tuned, I am planning to publish another article on the CPU Block Mining algorithm.
Thanks for reading.
Front image courtesy https://www.digimarc.com