What is variable packing in blockchain smart contracts?
Variable packing is the technique of grouping multiple smaller variables into a single storage slot to reduce gas costs by minimizing storage usage.
Click to reveal answer
beginner
Why does packing variables save gas in Ethereum smart contracts?
Because Ethereum charges gas based on storage slots used, packing multiple small variables into one 32-byte slot reduces the number of slots, lowering gas consumption.
Click to reveal answer
intermediate
Which data types are best suited for packing together?
Smaller data types like uint8, bool, or bytes1 can be packed together efficiently since they take less than 32 bytes each and fit into one storage slot.
Click to reveal answer
intermediate
What happens if variables are declared out of order for packing?
If variables are not ordered from smallest to largest, the compiler may not pack them efficiently, causing more storage slots to be used and increasing gas costs.
Click to reveal answer
beginner
Show an example of packing two uint128 variables in Solidity.
```solidity
contract Packed {
uint128 a;
uint128 b;
}
```
Here, both uint128 variables fit into one 32-byte storage slot, saving gas.
Click to reveal answer
Why is packing variables important in Ethereum smart contracts?
ATo avoid compiler errors
BTo increase contract execution speed
CTo reduce gas costs by minimizing storage slots used
DTo make code easier to read
✗ Incorrect
Packing variables reduces the number of storage slots used, which lowers gas costs.
Which of these variable types can be packed together efficiently?
Auint256 and uint128
Buint8 and bool
Cstring and bytes
Daddress and uint256
✗ Incorrect
Small types like uint8 and bool fit together in one storage slot, saving gas.
What is the size of one storage slot in Ethereum?
A64 bytes
B16 bytes
C8 bytes
D32 bytes
✗ Incorrect
Each storage slot in Ethereum is 32 bytes.
What is a common mistake that reduces packing efficiency?
ADeclaring variables in random order
BUsing only uint8 variables
CUsing constants
DUsing public variables
✗ Incorrect
Declaring variables out of size order can prevent efficient packing.
How many uint128 variables can fit in one storage slot?
ATwo
BOne
CFour
DEight
✗ Incorrect
Two uint128 variables (16 bytes each) fit into one 32-byte storage slot.
Explain how packing variables can reduce gas costs in Ethereum smart contracts.
Think about how Ethereum charges for storage and how combining variables helps.
You got /3 concepts.
Describe best practices for ordering variables to maximize packing efficiency.
Consider how variable size affects packing in storage slots.
You got /3 concepts.
Practice
(1/5)
1.
What is the main benefit of packing variables in blockchain smart contracts?
easy
A. It makes the code run faster on the CPU
B. It reduces gas costs by using fewer storage slots
C. It increases the contract's storage size
D. It improves the contract's security automatically
Solution
Step 1: Understand storage slots in blockchain
Each storage slot costs gas. Packing variables reduces the number of slots used.
Step 2: Effect of packing variables
By grouping smaller variables into one slot, gas usage decreases because fewer slots are needed.
Final Answer:
It reduces gas costs by using fewer storage slots -> Option B
Quick Check:
Packing = Less gas [OK]
Hint: Packing saves gas by reducing storage slots used [OK]
Common Mistakes:
Thinking packing speeds up CPU execution
Believing packing increases storage size
Assuming packing improves security automatically
2.
Which of the following Solidity variable declarations correctly packs two uint8 variables into one storage slot?
contract Example {
uint8 a;
uint8 b;
}
easy
A. Both a and b are declared consecutively as uint8
B. a is uint8 and b is uint256
C. a is uint256 and b is uint8
D. Both a and b are declared as uint256
Solution
Step 1: Check variable types for packing
Variables of the same small type declared consecutively can be packed into one slot.
Step 2: Analyze given declarations
Both a and b are uint8, so they fit together in one 256-bit slot.
Final Answer:
Both a and b are declared consecutively as uint8 -> Option A
Quick Check:
Same small types together = packed [OK]
Hint: Use same small types consecutively to pack [OK]
Common Mistakes:
Mixing large and small types breaks packing
Declaring variables far apart prevents packing
Assuming different types pack automatically
3.
Consider this Solidity contract snippet:
contract Test {
uint128 a = 1;
uint64 b = 2;
uint64 c = 3;
}
How many storage slots does this contract use for these variables?
medium
A. 1 storage slot
B. 3 storage slots
C. 2 storage slots
D. 4 storage slots
Solution
Step 1: Calculate sizes of variables
a is 128 bits, b and c are 64 bits each.
Step 2: Pack variables into 256-bit slots
a (128 bits) starts the slot, b and c (64+64=128 bits) fit exactly in the remaining space of the same slot.
Final Answer:
1 storage slot -> Option A
Quick Check:
128 + 64 + 64 = 256 bits = 1 slot [OK]
Hint: Sum consecutive vars' bits: 128+64+64=256 -> 1 slot [OK]
Common Mistakes:
Counting each variable as one slot
Ignoring packing into remaining space after uint128
Assuming different sizes cannot pack together
4.
Identify the error in this Solidity contract that tries to pack variables for gas savings: