CPSC 5910�Blockchain Security�Winter 2022��Session 09
Dr. Christian Seifert
Outline for Today
Page 2
Storage Slots
Page 3
Storage Slot – Value Types
contract StorageLayout {
uint256 public id = 543;
}
Storage slot 0: 543
w3.eth.get_storage_at("0xc145718228438a045d76d11248fb779e4d23f942", 0)
-> 0x000000000000000000000000000000000000000000000000000000000000021f
Would this work if the id would be private?
https://coinsbench.com/solidity-layout-and-access-of-storage-variables-simply-explained-1ce964d7c738
Page 4
Storage Slot Packing
pragma solidity ^0.8.16;
contract StorageLayout {
uint64 public value1 = 1;
uint64 public value2 = 2;
uint64 public value3 = 3;
uint64 public value4 = 4;
uint256 public value5 = 5;
}
web3.eth.getStorageAt("0x9168fBa74ADA0EB1DA81b8E9AeB88b083b42eBB4", 0)
-> 0x0000000000000004000000000000000300000000000000020000000000000001
web3.eth.getStorageAt("0x9168fBa74ADA0EB1DA81b8E9AeB88b083b42eBB4", 1)
-> 0x0000000000000000000000000000000000000000000000000000000000000005
Order matters! What if we swap value 5 with value 3?
Page 5
Storage Slot – Arrays
contract StorageLayout {
uint256[] public values = [1,2,3,4,5,6,7,8];
}
keccak256 of the storage slot (e.g. 0)
-> 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563
2. Add the element index to access the specific element
e.g. index 1
-> 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564
web3.eth.getStorageAt("0x9168fBa74ADA0EB1DA81b8E9AeB88b083b42eBB4", “0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564”)
-> 0x0000000000000000000000000000000000000000000000000000000000000002
Page 6
Storage Slot – Mappings
contract StorageLayout {
mapping(address => uint) public balances;
constructor() {
balances[0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405] = 678;
balances[0x66B0b1d2930059407DcC30F1A2305435fc37315E] = 501;
balances[0x9Edc86C3BAe6B04Ce2ea8EFF3abAC0C3160ba243] = 321;
}
}
e.g. 0000000000000000000000006827b8f6cc60497d9bf5210d602c0ecafdf7c405
0000000000000000000000000000000000000000000000000000000000000000
-> 86dfc0930cb222883cc0138873d68c1c9864fc2fe59d208c17f3484f489bef04
2. web3.eth.getStorageAt("0x59580A79b12Df8A1763ED2e43C35ef700d20580E", "0x86dfc0930cb222883cc0138873d68c1c9864fc2fe59d208c17f3484f489bef04")
-> 0x00000000000000000000000000000000000000000000000000000000000002a6 (678)
Page 7
Storage slot collisions
Page 8
Proxy Pattern
Contracts are immutable, which makes upgrades and bug fixes impossible. Industry has adopted proxy pattern to create upgradable smart contracts.
Page 9
Proxy Contract
setImpl(addr)
functionA()
functionB()
Impl 1
functionA()
functionB()
Impl 1.1
functionA()
functionB()
Remix
Intro to Remix
Page 10
Brownie
Page 11
Tenderly
Page 12