Distributed ledger concept in Blockchain / Solidity - Time & Space Complexity
When working with distributed ledgers, it is important to understand how the time to process transactions grows as more participants join the network.
We want to know how the system's work changes when the number of nodes or transactions increases.
Analyze the time complexity of the following code snippet.
function validateLedger(transactions, nodes) {
for (let node of nodes) {
for (let tx of transactions) {
if (!node.verify(tx)) {
return false;
}
}
}
return true;
}
This code checks every transaction on every node to confirm the ledger is valid across the network.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over nodes and transactions.
- How many times: For each node, it checks every transaction once.
As the number of nodes or transactions grows, the total checks increase by multiplying both.
| Input Size (nodes x transactions) | Approx. Operations |
|---|---|
| 10 nodes, 10 transactions | 100 checks |
| 100 nodes, 100 transactions | 10,000 checks |
| 1000 nodes, 1000 transactions | 1,000,000 checks |
Pattern observation: The work grows quickly as both inputs increase, multiplying together.
Time Complexity: O(n * m)
This means the time to validate grows in proportion to the number of nodes times the number of transactions.
[X] Wrong: "The time only depends on the number of transactions, not nodes."
[OK] Correct: Each node must check all transactions, so more nodes mean more total work.
Understanding how work scales in distributed ledgers shows you can think about real systems where many computers share tasks.
"What if each node only verified a random subset of transactions? How would the time complexity change?"
Practice
distributed ledger in blockchain technology?Solution
Step 1: Understand the concept of distributed ledger
A distributed ledger shares data across many computers instead of one central place.Step 2: Identify the main goal
This sharing keeps data safe and clear for everyone involved.Final Answer:
To share data across many computers to keep it safe and clear -> Option BQuick Check:
Distributed ledger = shared safe data [OK]
- Thinking data is stored on a single computer
- Believing one person controls the ledger
- Confusing encryption with data sharing
Solution
Step 1: Define what a block is in blockchain
A block is a group of transactions bundled together.Step 2: Understand its role in the network
Blocks are linked and shared across the network to keep data consistent.Final Answer:
A group of transactions linked together and shared across the network -> Option DQuick Check:
Block = grouped shared transactions [OK]
- Thinking a block is just one piece of data
- Confusing blocks with encryption keys
- Believing blocks are software programs
Solution
Step 1: Understand hash linking in blocks
Each block's hash depends on the previous block's hash and its own data.Step 2: Effect of changing data in second block
Changing 'data2' changes the second block's hash, which breaks the chain's integrity.Final Answer:
The hash of the second block changes, breaking the chain -> Option CQuick Check:
Changing data breaks hash chain [OK]
- Thinking hashes are independent
- Believing first block changes automatically
- Assuming network ignores hash mismatches
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?Solution
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.Step 2: Identify the mistake in hash calculation
The code only hashes the data, missing the previous hash, breaking the chain security.Final Answer:
The new hash should include the previous hash, not just data -> Option AQuick Check:
Hash must include previous hash + data [OK]
- Hashing only data without previous hash
- Ignoring previous hash in block structure
- Clearing ledger unnecessarily
Solution
Step 1: Understand consensus in distributed ledgers
Consensus algorithms require most participants to agree on the data before adding it, ensuring trust.Step 2: Evaluate options for data integrity and trust
Allowing unchecked additions or central control reduces trust; encryption alone doesn't ensure consensus.Final Answer:
Use a consensus algorithm that requires agreement from most participants before adding a block -> Option AQuick Check:
Consensus ensures trust and data integrity [OK]
- Skipping consensus to speed up adding blocks
- Centralizing ledger storage
- Relying only on encryption without consensus
Ready to prove it?
Pass the test and this pattern moves unknown
