Challenge - 5 Problems
Master of Variable Packing
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Solidity variable packing in storage
Consider the following Solidity contract snippet. What will be the value of
slot0 after deployment?contract Test {
uint128 a = 1;
uint128 b = 2;
uint256 slot0;
function readSlot0() public view returns (uint256) {
assembly {
slot0 := sload(0)
}
}
}Blockchain / Solidity
contract Test {
uint128 a = 1;
uint128 b = 2;
uint256 slot0;
function readSlot0() public view returns (uint256) {
assembly {
slot0 := sload(0)
}
}
}Attempts:
2 left
💡 Hint
Remember that two uint128 variables fit into one 256-bit storage slot, with the first declared variable occupying the lower bits.
✗ Incorrect
In Solidity, uint128 variables are packed into a single 256-bit slot if declared consecutively. The first variable 'a' occupies the lower 128 bits, and 'b' occupies the higher 128 bits. So the slot value is (b << 128) + a = (2 << 128) + 1, which in hex is 0x...0200000001.
🧠 Conceptual
intermediate1:30remaining
Understanding variable packing order in Solidity
In Solidity, which of the following statements about variable packing in storage is correct?
Attempts:
2 left
💡 Hint
Think about how Solidity stores smaller variables in a 256-bit slot.
✗ Incorrect
Solidity packs variables in the order they are declared, starting from the lowest bits of the storage slot. This means the first declared variable occupies the least significant bits, and subsequent variables fill higher bits if they fit.
❓ Predict Output
advanced2:30remaining
Output of packed struct storage in Solidity
Given the following Solidity contract, what is the value returned by
readSlot0() after deployment?contract Test {
struct Data {
uint64 x;
uint64 y;
uint128 z;
}
Data public data = Data(1, 2, 3);
function readSlot0() public view returns (uint256) {
assembly {
let val := sload(0)
mstore(0x0, val)
return(0x0, 32)
}
}
}Blockchain / Solidity
contract Test {
struct Data {
uint64 x;
uint64 y;
uint128 z;
}
Data public data = Data(1, 2, 3);
function readSlot0() public view returns (uint256) {
assembly {
let val := sload(0)
mstore(0x0, val)
return(0x0, 32)
}
}
}Attempts:
2 left
💡 Hint
Remember the order and size of struct members and how they pack into a 256-bit slot.
✗ Incorrect
The struct members are packed in order: x (uint64) in lowest 64 bits, y (uint64) next 64 bits, z (uint128) in highest 128 bits. So the slot value is (z << 128) + (y << 64) + x = (3 << 128) + (2 << 64) + 1.
🔧 Debug
advanced2:00remaining
Identify the error in variable packing usage
A developer tries to pack variables in Solidity as follows:
What is the main issue with this variable packing approach?
contract Test {
uint8 a = 1;
uint256 b = 2;
uint8 c = 3;
}What is the main issue with this variable packing approach?
Attempts:
2 left
💡 Hint
Think about how Solidity packs variables and the effect of a large variable between smaller ones.
✗ Incorrect
Solidity packs variables only if they are declared consecutively and fit in the same slot. Here, 'a' is uint8, then 'b' is uint256 which occupies a full slot, so 'c' starts a new slot. 'a' and 'c' are separated by 'b' and cannot be packed together.
🚀 Application
expert3:00remaining
Calculate total storage slots used by packed variables
Consider this Solidity contract:
How many 256-bit storage slots will these variables occupy in total?
contract Test {
uint128 a;
uint64 b;
uint64 c;
uint256 d;
uint8 e;
uint8 f;
uint16 g;
}How many 256-bit storage slots will these variables occupy in total?
Attempts:
2 left
💡 Hint
Group variables by their declaration order and sizes to see how they fit into 256-bit slots.
✗ Incorrect
Slot 0: a (128 bits) + b (64 bits) + c (64 bits) = 256 bits total.
Slot 1: d (256 bits) alone.
Slot 2: e (8 bits) + f (8 bits) + g (16 bits) = 32 bits total, fits in one slot.
Total slots = 3.
Slot 1: d (256 bits) alone.
Slot 2: e (8 bits) + f (8 bits) + g (16 bits) = 32 bits total, fits in one slot.
Total slots = 3.