0
0
Blockchain / Solidityprogramming~5 mins

Distributed ledger concept in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

A distributed ledger is a way to store data across many computers so everyone has the same record. It helps keep information safe and clear without needing one single boss.

When you want many people to share and agree on the same data without trusting one person.
When you need a secure way to track transactions or changes that everyone can see.
When you want to avoid cheating or mistakes by having many copies of the data.
When you want to build systems like digital money, voting, or supply tracking that need trust.
When you want to make sure data cannot be changed secretly after it is recorded.
Syntax
Blockchain / Solidity
DistributedLedger {
  nodes: List<Node>
  ledger: List<Block>

  addTransaction(transaction) {
    // validate and add transaction to a new block
  }

  reachConsensus() {
    // nodes agree on the current ledger state
  }

  addBlock(block) {
    // add block to ledger if valid
  }
}

This is a simple structure showing main parts: nodes (computers), ledger (records), and methods to add data and agree.

Each node keeps a copy of the ledger and works together to keep it updated and secure.

Examples
Two nodes sharing the same ledger data to keep records consistent.
Blockchain / Solidity
Node1: ledger = [Block1, Block2]
Node2: ledger = [Block1, Block2]

// Both nodes have the same data
Adding a transaction to the ledger to record a transfer.
Blockchain / Solidity
addTransaction({from: 'Alice', to: 'Bob', amount: 10})
// Transaction added to a new block
Nodes communicate to make sure they all have the same ledger.
Blockchain / Solidity
reachConsensus()
// Nodes check and agree on the ledger state
Sample Program

This simple program shows how a distributed ledger stores blocks of transactions. Each block is added and then printed out.

Blockchain / Solidity
class DistributedLedger:
    def __init__(self):
        self.ledger = []

    def add_block(self, block):
        self.ledger.append(block)

    def show_ledger(self):
        for i, block in enumerate(self.ledger, 1):
            print(f"Block {i}: {block}")

# Create ledger
ledger = DistributedLedger()

# Add blocks
ledger.add_block({'from': 'Alice', 'to': 'Bob', 'amount': 50})
ledger.add_block({'from': 'Bob', 'to': 'Charlie', 'amount': 25})

# Show ledger
ledger.show_ledger()
OutputSuccess
Important Notes

Distributed ledgers work by having many copies of the same data on different computers.

Consensus means all computers agree on the data to avoid mistakes or cheating.

Blocks group transactions together and link to keep history safe and unchangeable.

Summary

A distributed ledger shares data across many computers to keep it safe and clear.

It is useful when many people need to trust the same information without a single boss.

Blocks and consensus help keep the data correct and secure.