-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTube.sol
69 lines (56 loc) · 1.7 KB
/
YouTube.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
/*
Contract deployed on Ropsten
Vulnerability
Storage layout
Setup
Code
Demo
Preventative technique
*/
contract Vault {
// slot 0
uint public count = 123; // 32 bytes (2**8) * 32
// slot 1
address public owner = msg.sender; // 20 bytes (2**8) * 20
bool public isTrue = true; // 1 byte
uint16 public u16 = 31; // 2 bytes (2**8) * 2
// slot 2
bytes32 private password;
// constants do not use storage
uint public constant someConst = 123;
// slot 3, 4, 5 (one for each array element)
bytes32[3] public data;
struct User {
uint id;
bytes32 password;
}
// slot 6 - length of array
// starting from slot keccak256(6) - array elements
// slot where array element is stored - keccak256(slot) + (index * elementSize)
// where slot = 6 and elementSize = 2 (1 (uint) + 1 (bytes32))
User[] private users;
// slot 7 - empty
// entries are stored at keccak256(key, slot)
// where slot = 7, key = map key
mapping(uint => User) private idToUser;
constructor(bytes32 _password) public {
password = _password;
}
function addUser(bytes32 _paasword) public {
User memory user = User({id: users.length, password: _paasword});
users.push(user);
idToUser[user.id] = user;
}
function getArrayLocation(
uint slot,
uint index,
uint elementSize
) public pure returns (uint) {
return uint(keccak256(abi.encodePacked(slot))) + (index * elementSize);
}
function getMapLocation(uint slot, uint key) public pure returns (uint) {
return uint(keccak256(abi.encodePacked(key, slot)));
}
}