Ledger

As we discovered in Chapter 1Exploring Blockchain and BaaS, accounting ledgers have transaction history recorded as credit and debit transactions, while a statement shows the current balance. All the credit and debit entries lead to the current balance. Now, let's build the analogy, where the current balance is the current state of the ledger, and credit and debit entries are the transaction log entries. Current state and transaction logs, together, form a ledger; a blockchain ledger. To interact (also known as transact) on a distributed ledger, chaincode needs to be invoked:

  • Current state: Also termed world state or just state, this is a key-value pair that shows the current value of the ledger states, and it changes frequently as states are created, appended, or erased.
  • Transaction log: This is the set of changes that leads to the current world state. Transactions that are ordered and sequenced in blocks are appended to the blockchain. Sets of transaction lead to the current value of the world state. These transactions, once ordered and sequenced, cannot be modified and, hence, the block and blockchain, also known as the ledger, become immutable.
The ledger is distributed; hence, it is on all the peers. We have seen in transaction flows that a consensus is required before a transaction is committed to the ledger. Hence, the ledger is kept in sync with participating peers.

Each peer node has a copy of the ledger. This copy contains the transaction log and world state results, stored as key-value pairs in the database. DLT in HLF has two aspects; a world state, and a transaction log. A ledger state comprises a key-value pair. The world state is an aggregated state of the distributed ledger at a given point in time. The world state allows consumers to get the current state of the ledger. The equation for the ledger is as follows:

distributed ledger = world state + transaction log

The following diagram shows a ledger consisting of the world state and transaction log. The transaction log comprises various blocks, with each block containing one or more transactions:

Ledger state and the transaction log

This example shows the PiggyBank of a boy named Noah. The world state and the transaction log are empty at the start. This is represented by the Genesis Block (B0). Block B1 includes Transaction T1, where Daddy puts 10k i.e. Credit to PiggyBank. This is like a debit to daddy's account. The first transaction is represented by block 1 (B1). Block 1 represents the ledger state, with Key as PiggyBank and a Value of 10000. Also, it has ledger state with Key as DaddyPutsToBank and a Value of 10000. Both the ledger states are at Version 0. Version 0 indicates that it is a starting version number and that they have not been updated since they were created.

Applications invoke the chaincode, which actually accesses the ledger states via APIs to perform operations such as get and put on states using the state key.

Noah's mommy and Noah take money from the PiggyBank. Block B2 includes Transaction T2 and T3. Here, T2 represents the transaction where Mommy takes 2k i.e. Credit to Mommy's account, and T3 represents a transaction where Noah takes 2k i.e. Credit to Noah's account. PiggyBank is debited by 4k. Similarly, Block B3 includes Transaction T4, where Mommy puts 1k i.e. Debit to Mommy's account, which is 1k credit to PiggyBank. Likewise, Block B4 includes Transaction T5, where Noah takes 3k i.e. Credit to Noah's account. At this state, PiggyBank shows the latest current state of PiggyBank, which is 4k. Hence, the current state represents the current world state of PiggyBank. Even if the world state gets corrupted and information pertaining to the current state is destroyed, replaying the transactions in order will help to create the current world state.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset