-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathstorage.go
215 lines (186 loc) · 5.71 KB
/
storage.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package oasys
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)
const (
storageSlotSize = 64
hexPrefix = "0x"
)
// Represents Solidity storage
type storage map[string]interface{}
// Returns the contract storage slot map.
// see: https://docs.soliditylang.org/en/v0.8.11/internals/layout_in_storage.html
func (s storage) build(cfg *params.ChainConfig) (map[common.Hash]common.Hash, error) {
storage := make(map[common.Hash]common.Hash)
for slot, val := range s {
if err := setStorage(cfg, storage, common.HexToHash(slot), val); err != nil {
return nil, err
}
}
return storage, nil
}
// Non-primitive values in Solidity(such as array,mapping,struct)
type dynamicSlotValue interface {
apply(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, rootSlot common.Hash) error
}
// Different storage values for each genesis.
type genesismap map[common.Hash]interface{}
// Add values to storage.
func (g genesismap) apply(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, rootSlot common.Hash) error {
if val := g.value(); val != nil {
return setStorage(cfg, storage, rootSlot, val)
}
return nil
}
// Return the mapped value.
func (g genesismap) value() interface{} {
if val, ok := g[GenesisHash]; ok {
return val
}
if val, ok := g[defaultGenesisHash]; ok {
return val
}
return nil
}
// `array` type storage.
// If `length` is explicitly specified, it will be written to the root slot.
// Otherwise, the length of values will be written.
type array struct {
length int64
values map[int64]interface{}
}
// Add array values to storage.
func (a *array) apply(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, rootSlot common.Hash) error {
length := a.length
if length == 0 {
length = int64(len(a.values))
}
storage[rootSlot] = common.BigToHash(big.NewInt(length))
startSlot := new(big.Int).SetBytes(crypto.Keccak256(rootSlot.Bytes()))
for index, val := range a.values {
offset := index
// When the value is a struct, the starting slot will be
// `index + number of slots used by struct`
if size := structSize(cfg, val); size > 0 {
offset *= size
}
slot := new(big.Int).Add(startSlot, big.NewInt(offset))
if err := setStorage(cfg, storage, common.BigToHash(slot), val); err != nil {
return err
}
}
return nil
}
// `mapping` type storage.
type mapping struct {
keyFn func(key string) common.Hash
values map[string]interface{}
}
var (
addressKeyFn = func(key string) common.Hash { return common.HexToHash(key) }
)
// Add mapping values to storage.
func (m *mapping) apply(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, rootSlot common.Hash) error {
for mkey, mval := range m.values {
k := bytes.Join([][]byte{m.keyFn(mkey).Bytes(), rootSlot[:]}, nil)
slot := common.BytesToHash(crypto.Keccak256(k))
if err := setStorage(cfg, storage, slot, mval); err != nil {
return err
}
}
return nil
}
// `struct` type value.
// Assumes that each contained value is exactly one slot in length (32 bytes).
type structvalue []interface{}
// Add members to storage.
func (s structvalue) apply(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, rootSlot common.Hash) error {
for pos, member := range s {
memberSlot := new(big.Int).Add(rootSlot.Big(), big.NewInt(int64(pos)))
if err := setStorage(cfg, storage, common.BigToHash(memberSlot), member); err != nil {
return err
}
}
return nil
}
func setStorage(cfg *params.ChainConfig, storage map[common.Hash]common.Hash, slot common.Hash, val interface{}) error {
switch t := val.(type) {
case common.Hash:
storage[slot] = t
case common.Address:
storage[slot] = common.BytesToHash(t.Bytes())
case *big.Int:
storage[slot] = common.BigToHash(t)
case string:
isHex := strings.HasPrefix(t, hexPrefix)
if isHex {
t = strings.TrimPrefix(t, hexPrefix)
} else {
t = hex.EncodeToString([]byte(t))
}
if isHex && len(t) <= storageSlotSize {
storage[slot] = common.HexToHash(t)
} else if len(t) < storageSlotSize {
ends := strconv.FormatInt(int64(len(t)), 16)
storage[slot] = common.HexToHash(rightZeroPad(t, 62) + leftZeroPad(ends, 2))
} else {
storage[slot] = common.BigToHash(big.NewInt(int64(len(t) + 1)))
chunkStartPos := crypto.Keccak256Hash(slot.Bytes()).Big()
for i, chunk := range toChunks(t, storageSlotSize) {
chunkSlot := common.BigToHash(new(big.Int).Add(chunkStartPos, big.NewInt(int64(i))))
storage[chunkSlot] = common.HexToHash(chunk)
}
}
case dynamicSlotValue:
return t.apply(cfg, storage, slot)
case func(cfg *params.ChainConfig) interface{}:
return setStorage(cfg, storage, slot, t(cfg))
default:
return fmt.Errorf("unsupported type: %s, slot: %s", t, slot.String())
}
return nil
}
func toChunks(s string, l int) []string {
slen := len(s)
chunks := make([]string, 0)
for i := 0; i < slen; i += l {
end := i + l
if end > slen {
end = slen
}
slice := s[i:end]
chunks = append(chunks, rightZeroPad(slice, l))
}
return chunks
}
func rightZeroPad(s string, l int) string {
return s + strings.Repeat("0", l-len(s))
}
func leftZeroPad(s string, l int) string {
return strings.Repeat("0", l-len(s)) + s
}
func structSize(cfg *params.ChainConfig, val interface{}) int64 {
// Resolve the value until reaching either primitive or Solidity's data structure.
var extract func(val interface{}) interface{}
extract = func(val interface{}) interface{} {
switch t := val.(type) {
case genesismap:
return extract(t.value())
case func(cfg *params.ChainConfig) interface{}:
return extract(t(cfg))
}
return val
}
if t, ok := extract(val).(structvalue); ok {
return int64(len(t))
}
return 0
}