forked from Fantom-foundation/Substate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
substate_json.go
347 lines (270 loc) · 9.44 KB
/
substate_json.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package substate
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
)
// SubstateAccountJSON is modification of core.GenesisAccount
type SubstateAccountJSON struct {
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
Balance *math.HexOrDecimal256 `json:"balance" gencodec:"required"`
Nonce math.HexOrDecimal64 `json:"nonce,omitempty"`
}
func NewSubstateAccountJSON(sa *SubstateAccount) *SubstateAccountJSON {
return &SubstateAccountJSON{
Nonce: math.HexOrDecimal64(sa.Nonce),
Balance: (*math.HexOrDecimal256)(sa.Balance),
Storage: sa.Storage,
Code: sa.Code,
}
}
func (sa *SubstateAccount) SetJSON(saJSON *SubstateAccountJSON) {
sa.Nonce = uint64(saJSON.Nonce)
sa.Balance = (*big.Int)(saJSON.Balance)
sa.Storage = make(map[common.Hash]common.Hash)
if saJSON.Storage != nil {
sa.Storage = saJSON.Storage
}
sa.Code = saJSON.Code
}
func (sa SubstateAccount) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateAccountJSON(&sa))
}
func (sa *SubstateAccount) UnmarshalJSON(b []byte) error {
var err error
var saJSON SubstateAccountJSON
err = json.Unmarshal(b, &saJSON)
if err != nil {
return err
}
sa.SetJSON(&saJSON)
return nil
}
type SubstateAllocJSON map[common.Address]*SubstateAccountJSON
func NewSubstateAllocJSON(alloc SubstateAlloc) SubstateAllocJSON {
allocJSON := make(SubstateAllocJSON)
for addr, account := range alloc {
allocJSON[addr] = NewSubstateAccountJSON(account)
}
return allocJSON
}
func (alloc *SubstateAlloc) SetJSON(allocJSON SubstateAllocJSON) {
*alloc = make(SubstateAlloc)
for addr, saJSON := range allocJSON {
var sa SubstateAccount
sa.Nonce = uint64(saJSON.Nonce)
sa.Balance = (*big.Int)(saJSON.Balance)
sa.Storage = make(map[common.Hash]common.Hash)
if saJSON.Storage != nil {
sa.Storage = saJSON.Storage
}
sa.Code = saJSON.Code
(*alloc)[addr] = &sa
}
}
func (alloc SubstateAlloc) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateAllocJSON(alloc))
}
func (alloc *SubstateAlloc) UnmarshalJSON(b []byte) error {
var err error
var allocJSON SubstateAllocJSON
err = json.Unmarshal(b, &allocJSON)
if err != nil {
return err
}
alloc.SetJSON(allocJSON)
return nil
}
// SubstateEnvJSON is modification of t8ntool.stEnv
type SubstateEnvJSON struct {
Coinbase common.Address `json:"coinbase" gencodec:"required"`
Difficulty *math.HexOrDecimal256 `json:"difficulty" gencodec:"required"`
GasLimit math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"`
Number math.HexOrDecimal64 `json:"number" gencodec:"required"`
Timestamp math.HexOrDecimal64 `json:"timestamp" gencodec:"required"`
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
BaseFee *math.HexOrDecimal256 `json:"baseFee,omitempty"`
}
func NewSubstateEnvJSON(env *SubstateEnv) *SubstateEnvJSON {
var envJSON SubstateEnvJSON
envJSON.Coinbase = env.Coinbase
envJSON.Difficulty = (*math.HexOrDecimal256)(env.Difficulty)
envJSON.GasLimit = math.HexOrDecimal64(env.GasLimit)
envJSON.Number = math.HexOrDecimal64(env.Number)
envJSON.Timestamp = math.HexOrDecimal64(env.Timestamp)
envJSON.BlockHashes = make(map[math.HexOrDecimal64]common.Hash)
if env.BlockHashes != nil {
for num64, bhash := range env.BlockHashes {
envJSON.BlockHashes[math.HexOrDecimal64(num64)] = bhash
}
}
envJSON.BaseFee = (*math.HexOrDecimal256)(env.BaseFee)
return &envJSON
}
func (env *SubstateEnv) SetJSON(envJSON *SubstateEnvJSON) {
env.Coinbase = envJSON.Coinbase
env.Difficulty = (*big.Int)(envJSON.Difficulty)
env.GasLimit = uint64(envJSON.GasLimit)
env.Number = uint64(envJSON.Number)
env.Timestamp = uint64(envJSON.Timestamp)
env.BlockHashes = make(map[uint64]common.Hash)
if envJSON.BlockHashes != nil {
for num64, bhash := range envJSON.BlockHashes {
env.BlockHashes[uint64(num64)] = bhash
}
}
env.BaseFee = (*big.Int)(envJSON.BaseFee)
if env.BaseFee.Cmp(big.NewInt(0)) == 0 {
env.BaseFee = nil
}
}
func (env SubstateEnv) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateEnvJSON(&env))
}
func (env *SubstateEnv) UnmarshalJSON(b []byte) error {
var err error
var envJSON SubstateEnvJSON
err = json.Unmarshal(b, &envJSON)
if err != nil {
return err
}
env.SetJSON(&envJSON)
return nil
}
// SubstateMessageJSON is modification of types.msgdata
type SubstateMessageJSON struct {
Nonce math.HexOrDecimal64 `json:"nonce" gencodec:"required"`
CheckNonce bool `json:"checkNonce" gencodec:"required"`
GasPrice *math.HexOrDecimal256 `json:"gasPrice" gencodec:"required"`
Gas math.HexOrDecimal64 `json:"gas" gencodec:"required"`
From common.Address `json:"from"`
To *common.Address `json:"to"` // nil means contract creation
Value *math.HexOrDecimal256 `json:"value" gencodec:"required"`
Data hexutil.Bytes `json:"input" gencodec:"required"`
AccessList types.AccessList `json:"accessList,omitempty"`
GasFeeCap *math.HexOrDecimal256 `json:"gasFeeCap,omitempty"`
GasTipCap *math.HexOrDecimal256 `json:"gasTipCap,omitempty"`
}
func NewSubstateMessageJSON(msg *SubstateMessage) *SubstateMessageJSON {
var msgJSON SubstateMessageJSON
msgJSON.Nonce = math.HexOrDecimal64(msg.Nonce)
msgJSON.CheckNonce = msg.CheckNonce
msgJSON.GasPrice = (*math.HexOrDecimal256)(msg.GasPrice)
msgJSON.Gas = math.HexOrDecimal64(msg.Gas)
msgJSON.From = msg.From
msgJSON.To = msg.To
msgJSON.Value = (*math.HexOrDecimal256)(msg.Value)
msgJSON.Data = hexutil.Bytes(msg.Data)
msgJSON.AccessList = msg.AccessList
msgJSON.GasFeeCap = (*math.HexOrDecimal256)(msg.GasFeeCap)
msgJSON.GasTipCap = (*math.HexOrDecimal256)(msg.GasTipCap)
return &msgJSON
}
func (msg *SubstateMessage) SetJSON(msgJSON *SubstateMessageJSON) {
msg.Nonce = uint64(msgJSON.Nonce)
msg.CheckNonce = msgJSON.CheckNonce
msg.GasPrice = (*big.Int)(msgJSON.GasPrice)
msg.Gas = uint64(msgJSON.Gas)
msg.From = msgJSON.From
msg.To = msgJSON.To
msg.Value = (*big.Int)(msgJSON.Value)
msg.Data = []byte(msgJSON.Data)
msg.AccessList = msgJSON.AccessList
msg.GasFeeCap = (*big.Int)(msgJSON.GasFeeCap)
if msg.GasFeeCap.Cmp(big.NewInt(0)) == 0 {
msg.GasFeeCap = msg.GasPrice
}
msg.GasTipCap = (*big.Int)(msgJSON.GasTipCap)
if msg.GasTipCap.Cmp(big.NewInt(0)) == 0 {
msg.GasTipCap = msg.GasPrice
}
}
func (msg SubstateMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateMessageJSON(&msg))
}
func (msg *SubstateMessage) UnmarshalJSON(b []byte) error {
var err error
var msgJSON SubstateMessageJSON
err = json.Unmarshal(b, &msgJSON)
if err != nil {
return err
}
msg.SetJSON(&msgJSON)
return nil
}
type SubstateResultJSON struct {
Status math.HexOrDecimal64 `json:"status"`
Bloom types.Bloom `json:"logsBloom"`
Logs []*types.Log `json:"logs"`
ContractAddress common.Address `json:"contractAddress"`
GasUsed math.HexOrDecimal64 `json:"gasUsed" gencodec:"required"`
}
func NewSubstateResultJSON(result *SubstateResult) *SubstateResultJSON {
var resultJSON SubstateResultJSON
resultJSON.Status = math.HexOrDecimal64(result.Status)
resultJSON.Bloom = result.Bloom
resultJSON.Logs = result.Logs
resultJSON.ContractAddress = result.ContractAddress
resultJSON.GasUsed = math.HexOrDecimal64(result.GasUsed)
return &resultJSON
}
func (result *SubstateResult) SetJSON(resultJSON *SubstateResultJSON) {
result.Status = uint64(resultJSON.Status)
result.Bloom = resultJSON.Bloom
result.Logs = resultJSON.Logs
result.ContractAddress = resultJSON.ContractAddress
result.GasUsed = uint64(resultJSON.GasUsed)
}
func (result SubstateResult) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateResultJSON(&result))
}
func (result *SubstateResult) UnmarshalJSON(b []byte) error {
var err error
var resultJSON SubstateResultJSON
err = json.Unmarshal(b, &resultJSON)
if err != nil {
return err
}
result.SetJSON(&resultJSON)
return nil
}
type SubstateJSON struct {
InputAlloc SubstateAllocJSON `json:"inputAlloc"`
OutputAlloc SubstateAllocJSON `json:"outputAlloc"`
Env *SubstateEnvJSON `json:"env"`
Message *SubstateMessageJSON `json:"message"`
Result *SubstateResultJSON `json:"result"`
}
func NewSubstateJSON(substate *Substate) *SubstateJSON {
var substateJSON SubstateJSON
substateJSON.InputAlloc = NewSubstateAllocJSON(substate.InputAlloc)
substateJSON.OutputAlloc = NewSubstateAllocJSON(substate.OutputAlloc)
substateJSON.Env = NewSubstateEnvJSON(substate.Env)
substateJSON.Message = NewSubstateMessageJSON(substate.Message)
substateJSON.Result = NewSubstateResultJSON(substate.Result)
return &substateJSON
}
func (substate *Substate) SetJSON(substateJSON *SubstateJSON) {
substate.InputAlloc.SetJSON(substateJSON.InputAlloc)
substate.OutputAlloc.SetJSON(substateJSON.OutputAlloc)
substate.Env.SetJSON(substateJSON.Env)
substate.Message.SetJSON(substateJSON.Message)
substate.Result.SetJSON(substateJSON.Result)
}
func (substate Substate) MarshalJSON() ([]byte, error) {
return json.Marshal(NewSubstateJSON(&substate))
}
func (substate *Substate) UnmarshalJSON(b []byte) error {
var err error
var substateJSON SubstateJSON
err = json.Unmarshal(b, &substateJSON)
if err != nil {
return err
}
substate.SetJSON(&substateJSON)
return nil
}