Why data location affects cost in Blockchain / Solidity - Performance Analysis
In blockchain, where data is stored can change how much work the system does.
We want to see how data location affects the cost of running code.
Analyze the time complexity of the following code snippet.
contract Example {
uint[] storageData;
uint[] memoryData;
function updateStorage() public {
for (uint i = 0; i < storageData.length; i++) {
storageData[i] = storageData[i] + 1;
}
}
function updateMemory() public {
uint[] memory temp = memoryData;
for (uint i = 0; i < temp.length; i++) {
temp[i] = temp[i] + 1;
}
}
}
This code updates numbers stored in two places: storage (permanent) and memory (temporary).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array to update its value.
- How many times: Once for each element in the array (n times).
As the array gets bigger, the loop runs more times, so the work grows with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to update grows in a straight line as the data size grows.
[X] Wrong: "Updating data in memory costs the same as updating data in storage."
[OK] Correct: Storage updates are more expensive because they change permanent data on the blockchain, while memory updates are temporary and cheaper.
Understanding how data location affects cost helps you write efficient blockchain code and shows you know how the system works behind the scenes.
"What if we copied storage data to memory before updating, then wrote back once? How would the time complexity change?"
Practice
Solution
Step 1: Understand blockchain data storage
On-chain data is stored by all nodes in the network, requiring consensus and replication.Step 2: Recognize resource usage impact
This replication uses shared network resources, making on-chain storage more expensive than off-chain.Final Answer:
Because on-chain data uses shared network resources and requires consensus from all nodes -> Option BQuick Check:
On-chain storage costs more due to network resource use [OK]
- Thinking off-chain data is always public
- Assuming on-chain data is temporary
- Confusing encryption with cost
Solution
Step 1: Define off-chain storage
Off-chain means data is stored outside the blockchain, often in external databases or storage systems.Step 2: Match options to definition
Storing data in external databases or storage systems linked to the blockchain correctly describes off-chain storage as external linked storage, unlike on-chain ledger or smart contract variables.Final Answer:
Storing data in external databases or storage systems linked to the blockchain -> Option CQuick Check:
Off-chain = external storage linked to blockchain [OK]
- Confusing on-chain ledger with off-chain storage
- Thinking smart contract variables are off-chain
- Believing broadcasting without consensus is off-chain storage
Solution
Step 1: Calculate on-chain cost for 100 KB
100 KB * 0.01 ETH/KB = 1 ETH total on-chain cost.Step 2: Calculate off-chain cost for 100 KB
100 KB * 0.001 ETH/KB = 0.1 ETH total off-chain cost.Step 3: Find cost difference
1 ETH - 0.1 ETH = 0.9 ETH more for on-chain.Final Answer:
0.9 ETH more for on-chain storage -> Option AQuick Check:
On-chain cost > off-chain cost by 0.9 ETH [OK]
- Multiplying incorrectly or mixing units
- Confusing which cost is higher
- Ignoring multiplication by 100 KB
storeData(userData) {
blockchain.save(userData);
offchain.save(userData);
}
Why might this cause unnecessary cost?Solution
Step 1: Analyze storage calls
The code stores the same data both on-chain and off-chain, duplicating storage.Step 2: Understand cost impact
On-chain storage is costly, so duplicating data on both increases unnecessary cost.Final Answer:
Because storing data both on-chain and off-chain duplicates storage cost unnecessarily -> Option DQuick Check:
Duplicate storage increases cost [OK]
- Thinking async vs sync affects cost here
- Assuming encryption affects storage cost directly
- Believing call order changes cost
Solution
Step 1: Consider cost and transparency
Storing full logs on-chain is costly; storing nothing on-chain loses trust.Step 2: Use hashes on-chain for verification
Storing hashes on-chain keeps proof of logs without full data cost.Step 3: Keep full logs off-chain for detail
Full logs off-chain save cost but remain accessible and verifiable.Final Answer:
Store only transaction hashes on-chain and full logs off-chain -> Option AQuick Check:
Hashes on-chain + full off-chain = cost-effective and verifiable [OK]
- Storing all data on-chain ignoring cost
- Storing nothing on-chain losing trust
- Using lossy compression risking data integrity
Ready to prove it?
Pass the test and this pattern moves unknown
