Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Why data location affects cost in Blockchain / Solidity - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why data location affects cost
User wants to store data
↓
Choose data location
↓
Check storage cost per location
↓
Calculate total cost
↓
Decide to store or not
↓
Store data
↓
Cancel storage
Data location affects cost because different blockchain storage options charge differently based on where data is stored.
Execution Sample
Blockchain / Solidity
storage_costs = {"on_chain": 10, "off_chain": 2}
data_size = 5
location = "on_chain"
total_cost = storage_costs[location] * data_size
print(total_cost)
Calculates total cost to store data based on chosen location and data size.
Execution Table
StepVariableValueActionOutput
1storage_costs{"on_chain": 10, "off_chain": 2}Define cost per unit data
2data_size5Define data size to store
3location"on_chain"Choose data location
4total_coststorage_costs["on_chain"] * 5Calculate total cost50
5print(total_cost)Output total cost50
💡 Calculation done and cost output printed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
storage_costsundefined{"on_chain": 10, "off_chain": 2}{"on_chain": 10, "off_chain": 2}{"on_chain": 10, "off_chain": 2}{"on_chain": 10, "off_chain": 2}{"on_chain": 10, "off_chain": 2}
data_sizeundefinedundefined5555
locationundefinedundefinedundefined"on_chain""on_chain""on_chain"
total_costundefinedundefinedundefinedundefined5050
Key Moments - 2 Insights
Why does choosing 'on_chain' cost more than 'off_chain'?
Because 'on_chain' storage cost per unit is 10, higher than 'off_chain' which is 2, as shown in execution_table step 4 calculation.
What happens if data_size changes?
Total cost changes proportionally since total_cost = cost_per_unit * data_size, as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the total_cost value?
A10
B50
C5
D2
💡 Hint
Check the 'Value' column for total_cost at step 4 in execution_table.
At which step is the data location chosen?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for the step where 'location' variable is assigned in execution_table.
If data_size was 10 instead of 5, what would total_cost be at step 4?
A100
B50
C20
D10
💡 Hint
Multiply storage_costs['on_chain'] (10) by new data_size (10) as in step 4 calculation.
Concept Snapshot
Data location affects cost because different storage options charge differently.
Cost = cost_per_unit * data_size.
On-chain storage is usually more expensive than off-chain.
Choosing location impacts total cost directly.
Always calculate cost before storing data.
Full Transcript
This visual execution shows how data location affects cost in blockchain storage. First, storage costs per location are defined. Then, data size and location are chosen. The total cost is calculated by multiplying cost per unit by data size. Finally, the cost is printed. On-chain storage costs more per unit than off-chain, so total cost depends on location and data size.

Practice

(1/5)
1. Why does storing data directly on the blockchain usually cost more than storing it off-chain?
easy
A. Because off-chain data is always public and on-chain data is private
B. Because on-chain data uses shared network resources and requires consensus from all nodes
C. Because on-chain data is stored only temporarily
D. Because off-chain data is encrypted and on-chain data is not

Solution

  1. Step 1: Understand blockchain data storage

    On-chain data is stored by all nodes in the network, requiring consensus and replication.
  2. Step 2: Recognize resource usage impact

    This replication uses shared network resources, making on-chain storage more expensive than off-chain.
  3. Final Answer:

    Because on-chain data uses shared network resources and requires consensus from all nodes -> Option B
  4. Quick Check:

    On-chain storage costs more due to network resource use [OK]
Hint: On-chain means shared network storage, so it costs more [OK]
Common Mistakes:
  • Thinking off-chain data is always public
  • Assuming on-chain data is temporary
  • Confusing encryption with cost
2. Which of the following is the correct way to describe storing data off-chain in a blockchain app?
easy
A. Storing data on the blockchain ledger directly
B. Storing data only in smart contract variables
C. Storing data in external databases or storage systems linked to the blockchain
D. Storing data by broadcasting it to all network nodes without consensus

Solution

  1. Step 1: Define off-chain storage

    Off-chain means data is stored outside the blockchain, often in external databases or storage systems.
  2. 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.
  3. Final Answer:

    Storing data in external databases or storage systems linked to the blockchain -> Option C
  4. Quick Check:

    Off-chain = external storage linked to blockchain [OK]
Hint: Off-chain means outside blockchain, usually external databases [OK]
Common Mistakes:
  • Confusing on-chain ledger with off-chain storage
  • Thinking smart contract variables are off-chain
  • Believing broadcasting without consensus is off-chain storage
3. Consider a blockchain app that stores user profiles. If storing 1 KB on-chain costs 0.01 ETH and storing off-chain costs 0.001 ETH, what is the cost difference for storing 100 KB on-chain vs off-chain?
medium
A. 0.9 ETH more for on-chain storage
B. 0.01 ETH more for on-chain storage
C. 1 ETH more for on-chain storage
D. No cost difference

Solution

  1. Step 1: Calculate on-chain cost for 100 KB

    100 KB * 0.01 ETH/KB = 1 ETH total on-chain cost.
  2. Step 2: Calculate off-chain cost for 100 KB

    100 KB * 0.001 ETH/KB = 0.1 ETH total off-chain cost.
  3. Step 3: Find cost difference

    1 ETH - 0.1 ETH = 0.9 ETH more for on-chain.
  4. Final Answer:

    0.9 ETH more for on-chain storage -> Option A
  5. Quick Check:

    On-chain cost > off-chain cost by 0.9 ETH [OK]
Hint: Multiply size by cost per KB, compare totals [OK]
Common Mistakes:
  • Multiplying incorrectly or mixing units
  • Confusing which cost is higher
  • Ignoring multiplication by 100 KB
4. A developer wrote this code snippet to store user data:
storeData(userData) {
  blockchain.save(userData);
  offchain.save(userData);
}
Why might this cause unnecessary cost?
medium
A. Because offchain.save() should be called before blockchain.save()
B. Because blockchain.save() is asynchronous and offchain.save() is synchronous
C. Because userData is not encrypted before saving
D. Because storing data both on-chain and off-chain duplicates storage cost unnecessarily

Solution

  1. Step 1: Analyze storage calls

    The code stores the same data both on-chain and off-chain, duplicating storage.
  2. Step 2: Understand cost impact

    On-chain storage is costly, so duplicating data on both increases unnecessary cost.
  3. Final Answer:

    Because storing data both on-chain and off-chain duplicates storage cost unnecessarily -> Option D
  4. Quick Check:

    Duplicate storage increases cost [OK]
Hint: Avoid storing same data twice on-chain and off-chain [OK]
Common Mistakes:
  • Thinking async vs sync affects cost here
  • Assuming encryption affects storage cost directly
  • Believing call order changes cost
5. You want to design a blockchain app that stores transaction logs. To minimize cost, which approach is best?
hard
A. Store only transaction hashes on-chain and full logs off-chain
B. Store all transaction logs fully on-chain for transparency
C. Store all logs off-chain and never record anything on-chain
D. Store logs on-chain but compress them with lossy compression

Solution

  1. Step 1: Consider cost and transparency

    Storing full logs on-chain is costly; storing nothing on-chain loses trust.
  2. Step 2: Use hashes on-chain for verification

    Storing hashes on-chain keeps proof of logs without full data cost.
  3. Step 3: Keep full logs off-chain for detail

    Full logs off-chain save cost but remain accessible and verifiable.
  4. Final Answer:

    Store only transaction hashes on-chain and full logs off-chain -> Option A
  5. Quick Check:

    Hashes on-chain + full off-chain = cost-effective and verifiable [OK]
Hint: Keep proofs on-chain, full data off-chain to save cost [OK]
Common Mistakes:
  • Storing all data on-chain ignoring cost
  • Storing nothing on-chain losing trust
  • Using lossy compression risking data integrity
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