Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

Distributed ledger concept in Blockchain / Solidity

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a distributed ledger in blockchain technology?
easy
A. To store data only on one central computer
B. To share data across many computers to keep it safe and clear
C. To allow only one person to control the data
D. To make data unreadable for everyone

Solution

  1. Step 1: Understand the concept of distributed ledger

    A distributed ledger shares data across many computers instead of one central place.
  2. Step 2: Identify the main goal

    This sharing keeps data safe and clear for everyone involved.
  3. Final Answer:

    To share data across many computers to keep it safe and clear -> Option B
  4. Quick Check:

    Distributed ledger = shared safe data [OK]
Hint: Distributed means data is shared, not stored in one place [OK]
Common Mistakes:
  • Thinking data is stored on a single computer
  • Believing one person controls the ledger
  • Confusing encryption with data sharing
2. Which of the following is the correct way to describe a block in a distributed ledger?
easy
A. A software program that controls the network
B. A single piece of data stored only on one computer
C. A secret key used to encrypt data
D. A group of transactions linked together and shared across the network

Solution

  1. Step 1: Define what a block is in blockchain

    A block is a group of transactions bundled together.
  2. Step 2: Understand its role in the network

    Blocks are linked and shared across the network to keep data consistent.
  3. Final Answer:

    A group of transactions linked together and shared across the network -> Option D
  4. Quick Check:

    Block = grouped shared transactions [OK]
Hint: Blocks group transactions and share them across the network [OK]
Common Mistakes:
  • Thinking a block is just one piece of data
  • Confusing blocks with encryption keys
  • Believing blocks are software programs
3. Consider a simple blockchain network where each block contains a hash of the previous block. If the first block's hash is 'abc123', and the second block's hash is calculated as hash('abc123' + 'data2'), what happens if someone tries to change 'data2' in the second block?
medium
A. The first block's hash changes automatically
B. Nothing changes because hashes are not linked
C. The hash of the second block changes, breaking the chain
D. The network ignores the change and continues

Solution

  1. Step 1: Understand hash linking in blocks

    Each block's hash depends on the previous block's hash and its own data.
  2. Step 2: Effect of changing data in second block

    Changing 'data2' changes the second block's hash, which breaks the chain's integrity.
  3. Final Answer:

    The hash of the second block changes, breaking the chain -> Option C
  4. Quick Check:

    Changing data breaks hash chain [OK]
Hint: Changing block data changes its hash, breaking the chain [OK]
Common Mistakes:
  • Thinking hashes are independent
  • Believing first block changes automatically
  • Assuming network ignores hash mismatches
4. A developer wrote this pseudocode for adding a new block to a distributed ledger:
function addBlock(data) {
  previousHash = getLastBlockHash()
  newHash = hash(data)
  block = { data: data, previousHash: previousHash, hash: newHash }
  ledger.append(block)
}
What is the main error in this code?
medium
A. The new hash should include the previous hash, not just data
B. The previous hash should be ignored when adding a block
C. The block should not store the previous hash
D. The ledger should be cleared before appending

Solution

  1. Step 1: Review how block hashes are calculated

    A block's hash must include the previous block's hash and its own data to link blocks securely.
  2. Step 2: Identify the mistake in hash calculation

    The code only hashes the data, missing the previous hash, breaking the chain security.
  3. Final Answer:

    The new hash should include the previous hash, not just data -> Option A
  4. Quick Check:

    Hash must include previous hash + data [OK]
Hint: Hash must combine previous hash and data to link blocks [OK]
Common Mistakes:
  • Hashing only data without previous hash
  • Ignoring previous hash in block structure
  • Clearing ledger unnecessarily
5. You want to design a distributed ledger system where multiple participants can add transactions, but only valid transactions are accepted after consensus. Which approach best ensures data integrity and trust in this system?
hard
A. Use a consensus algorithm that requires agreement from most participants before adding a block
B. Allow any participant to add blocks without checks to speed up the process
C. Store the ledger on a single trusted server controlled by one participant
D. Encrypt all transactions so no one can see the data

Solution

  1. Step 1: Understand consensus in distributed ledgers

    Consensus algorithms require most participants to agree on the data before adding it, ensuring trust.
  2. Step 2: Evaluate options for data integrity and trust

    Allowing unchecked additions or central control reduces trust; encryption alone doesn't ensure consensus.
  3. Final Answer:

    Use a consensus algorithm that requires agreement from most participants before adding a block -> Option A
  4. Quick Check:

    Consensus ensures trust and data integrity [OK]
Hint: Consensus means most agree before adding data [OK]
Common Mistakes:
  • Skipping consensus to speed up adding blocks
  • Centralizing ledger storage
  • Relying only on encryption without consensus
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard