-
Notifications
You must be signed in to change notification settings - Fork 107
/
helpers.nim
399 lines (336 loc) · 11.3 KB
/
helpers.nim
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Nimbus
# Copyright (c) 2022 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http:https://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http:https://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
import
std/[json, strutils, tables],
stew/[byteutils, results],
stint,
eth/[common, rlp, keys],
../../nimbus/transaction,
../../nimbus/common/chain_config,
../common/helpers,
./types,
./txpriv
export
helpers
proc parseHexOrInt[T](x: string): T =
when T is UInt256:
if x.startsWith("0x"):
UInt256.fromHex(x)
else:
parse(x, UInt256, 10)
else:
if x.startsWith("0x"):
fromHex[T](x)
else:
parseInt(x).T
template fromJson(T: type EthAddress, n: JsonNode, field: string): EthAddress =
hexToByteArray(n[field].getStr(), sizeof(T))
template fromJson(T: type Blob, n: JsonNode, field: string): Blob =
hexToSeqByte(n[field].getStr())
proc fromJson(T: type uint64, n: JsonNode, field: string): uint64 =
if n[field].kind == JInt:
n[field].getInt().uint64
else:
parseHexOrInt[AccountNonce](n[field].getStr())
template fromJson(T: type UInt256, n: JsonNode, field: string): UInt256 =
parseHexOrInt[UInt256](n[field].getStr())
template fromJson(T: type GasInt, n: JsonNode, field: string): GasInt =
parseHexOrInt[GasInt](n[field].getStr())
template fromJson(T: type ChainId, n: JsonNode, field: string): ChainId =
parseHexOrInt[uint64](n[field].getStr()).ChainId
proc fromJson(T: type Hash256, n: JsonNode, field: string): Hash256 =
var num = n[field].getStr()
num.removePrefix("0x")
if num.len < 64:
num = repeat('0', 64 - num.len) & num
Hash256(data: hexToByteArray(num, 32))
template fromJson(T: type EthTime, n: JsonNode, field: string): EthTime =
fromUnix(parseHexOrInt[int64](n[field].getStr()))
proc fromJson(T: type AccessList, n: JsonNode, field: string): AccessList =
let z = n[field]
if z.kind == JNull:
return
for x in z:
var ap = AccessPair(
address: EthAddress.fromJson(x, "address")
)
let sks = x["storageKeys"]
for sk in sks:
ap.storageKeys.add hexToByteArray(sk.getStr(), 32)
result.add ap
proc fromJson(T: type Ommer, n: JsonNode): Ommer =
Ommer(
delta: fromJson(uint64, n, "delta"),
address: fromJson(EthAddress, n, "address")
)
template `gas=`(tx: var Transaction, x: GasInt) =
tx.gasLimit = x
template `input=`(tx: var Transaction, x: Blob) =
tx.payload = x
template `v=`(tx: var Transaction, x: int64) =
tx.V = x
template `r=`(tx: var Transaction, x: UInt256) =
tx.R = x
template `s=`(tx: var Transaction, x: UInt256) =
tx.S = x
template `maxPriorityFeePerGas=`(tx: var Transaction, x: GasInt) =
tx.maxPriorityFee = x
template `maxFeePerGas=`(tx: var Transaction, x: GasInt) =
tx.maxFee = x
template required(o: untyped, T: type, oField: untyped) =
const fName = astToStr(oField)
if not n.hasKey(fName):
raise newError(ErrorJson, "missing required field '" & fName & "' in transaction")
o.oField = T.fromJson(n, fName)
template omitZero(o: untyped, T: type, oField: untyped) =
const fName = astToStr(oField)
if n.hasKey(fName):
o.oField = T.fromJson(n, fName)
template optional(o: untyped, T: type, oField: untyped) =
const fName = astToStr(oField)
if n.hasKey(fName) and n[fName].kind != JNull:
o.oField = some(T.fromJson(n, fName))
proc parseAlloc*(ctx: var TransContext, n: JsonNode) =
for accAddr, acc in n:
let address = hexToByteArray[20](accAddr)
var ga = GenesisAccount()
if acc.hasKey("code"):
ga.code = Blob.fromJson(acc, "code")
if acc.hasKey("nonce"):
ga.nonce = AccountNonce.fromJson(acc, "nonce")
if acc.hasKey("balance"):
ga.balance = UInt256.fromJson(acc, "balance")
else:
raise newError(ErrorJson, "GenesisAlloc: balance required")
if acc.hasKey("storage"):
let storage = acc["storage"]
for k, v in storage:
ga.storage[UInt256.fromHex(k)] = UInt256.fromHex(v.getStr())
ctx.alloc[address] = ga
proc parseEnv*(ctx: var TransContext, n: JsonNode) =
required(ctx.env, EthAddress, currentCoinbase)
required(ctx.env, GasInt, currentGasLimit)
required(ctx.env, BlockNumber, currentNumber)
required(ctx.env, EthTime, currentTimestamp)
optional(ctx.env, DifficultyInt, currentDifficulty)
optional(ctx.env, Hash256, currentRandom)
optional(ctx.env, DifficultyInt, parentDifficulty)
omitZero(ctx.env, EthTime, parentTimestamp)
optional(ctx.env, UInt256, currentBaseFee)
omitZero(ctx.env, Hash256, parentUncleHash)
optional(ctx.env, UInt256, parentBaseFee)
optional(ctx.env, GasInt, parentGasUsed)
optional(ctx.env, GasInt, parentGasLimit)
if n.hasKey("blockHashes"):
let w = n["blockHashes"]
for k, v in w:
ctx.env.blockHashes[fromHex[uint64](k)] = Hash256.fromHex(v.getStr())
if n.hasKey("ommers"):
let w = n["ommers"]
for v in w:
ctx.env.ommers.add Ommer.fromJson(v)
proc parseTx(n: JsonNode, chainId: ChainID): Transaction =
var tx: Transaction
if not n.hasKey("type"):
tx.txType = TxLegacy
else:
tx.txType = int64.fromJson(n, "type").TxType
required(tx, AccountNonce, nonce)
required(tx, GasInt, gas)
required(tx, UInt256, value)
required(tx, Blob, input)
required(tx, int64, v)
required(tx, UInt256, r)
required(tx, UInt256, s)
if n.hasKey("to"):
tx.to = some(EthAddress.fromJson(n, "to"))
case tx.txType
of TxLegacy:
required(tx, GasInt, gasPrice)
of TxEip2930:
required(tx, GasInt, gasPrice)
required(tx, ChainId, chainId)
omitZero(tx, AccessList, accessList)
of TxEip1559:
required(tx, ChainId, chainId)
required(tx, GasInt, maxPriorityFeePerGas)
required(tx, GasInt, maxFeePerGas)
omitZero(tx, AccessList, accessList)
var eip155 = true
if n.hasKey("protected"):
eip155 = n["protected"].bval
if n.hasKey("secretKey"):
let data = Blob.fromJson(n, "secretKey")
let secretKey = PrivateKey.fromRaw(data).tryGet
signTransaction(tx, secretKey, chainId, eip155)
else:
tx
proc parseTxLegacy(item: var Rlp): Result[Transaction, string] =
try:
var tx: Transaction
item.readTxLegacy(tx)
return ok(tx)
except RlpError as x:
return err(x.msg)
proc parseTxTyped(item: var Rlp): Result[Transaction, string] =
try:
var tx: Transaction
var rr = rlpFromBytes(item.read(Blob))
rr.readTxTyped(tx)
return ok(tx)
except RlpError as x:
return err(x.msg)
proc parseTxJson(ctx: TransContext, i: int, chainId: ChainId): Result[Transaction, string] =
try:
let n = ctx.txs.n[i]
return ok(parseTx(n, chainId))
except Exception as x:
return err(x.msg)
proc parseTxs*(ctx: TransContext, chainId: ChainId): seq[Result[Transaction, string]] =
if ctx.txs.txsType == TxsJson:
let len = ctx.txs.n.len
result = newSeqOfCap[Result[Transaction, string]](len)
for i in 0 ..< len:
result.add ctx.parseTxJson(i, chainId)
return
if ctx.txs.txsType == TxsRlp:
result = newSeqOfCap[Result[Transaction, string]](ctx.txs.r.listLen)
var rlp = ctx.txs.r
for item in rlp:
if item.isList:
result.add parseTxLegacy(item)
else:
result.add parseTxTyped(item)
return
proc txList*(ctx: TransContext, chainId: ChainId): seq[Transaction] =
let list = ctx.parseTxs(chainId)
for txRes in list:
if txRes.isOk:
result.add txRes.get
proc parseTxs*(ctx: var TransContext, txs: JsonNode) =
if txs.kind == JNull:
return
if txs.kind != JArray:
raise newError(ErrorJson,
"Transaction list should be a JSON array, got=" & $txs.kind)
ctx.txs = TxsList(
txsType: TxsJson,
n: txs)
proc parseTxsRlp*(ctx: var TransContext, hexData: string) =
let bytes = hexToSeqByte(hexData)
ctx.txs = TxsList(
txsType: TxsRlp,
r: rlpFromBytes(bytes)
)
if ctx.txs.r.isList.not:
raise newError(ErrorRlp, "RLP Transaction list should be a list")
proc parseInputFromStdin*(ctx: var TransContext) =
let data = stdin.readAll()
let n = json.parseJson(data)
if n.hasKey("alloc"): ctx.parseAlloc(n["alloc"])
if n.hasKey("env"): ctx.parseEnv(n["env"])
if n.hasKey("txs"): ctx.parseTxs(n["txs"])
if n.hasKey("txsRlp"): ctx.parseTxsRlp(n["txsRlp"].getStr())
template stripLeadingZeros(value: string): string =
var cidx = 0
# ignore the last character so we retain '0' on zero value
while cidx < value.len - 1 and value[cidx] == '0':
cidx.inc
value[cidx .. ^1]
proc `@@`*[K, V](x: Table[K, V]): JsonNode
proc `@@`*[T](x: seq[T]): JsonNode
proc to0xHex(x: UInt256): string =
"0x" & x.toHex
proc `@@`(x: uint64 | int64 | int): JsonNode =
let hex = x.toHex.stripLeadingZeros
%("0x" & hex.toLowerAscii)
proc `@@`(x: UInt256): JsonNode =
%("0x" & x.toHex)
proc `@@`(x: Hash256): JsonNode =
%("0x" & x.data.toHex)
proc `@@`*(x: Blob): JsonNode =
%("0x" & x.toHex)
proc `@@`(x: bool): JsonNode =
%(if x: "0x1" else: "0x0")
proc `@@`(x: EthAddress): JsonNode =
%("0x" & x.toHex)
proc `@@`(x: Topic): JsonNode =
%("0x" & x.toHex)
proc toJson(x: Table[UInt256, UInt256]): JsonNode =
# special case, we need to convert UInt256 into full 32 bytes
# and not shorter
result = newJObject()
for k, v in x:
result["0x" & k.dumpHex] = %("0x" & v.dumpHex)
proc `@@`(acc: GenesisAccount): JsonNode =
result = newJObject()
if acc.code.len > 0:
result["code"] = @@(acc.code)
result["balance"] = @@(acc.balance)
if acc.nonce > 0:
result["nonce"] = @@(acc.nonce)
if acc.storage.len > 0:
result["storage"] = toJson(acc.storage)
proc `@@`[K, V](x: Table[K, V]): JsonNode =
result = newJObject()
for k, v in x:
result[k.to0xHex] = @@(v)
proc `@@`(x: BloomFilter): JsonNode =
%("0x" & toHex[256](x))
proc `@@`(x: Log): JsonNode =
result = %{
"address": @@(x.address),
"topics" : @@(x.topics),
"data" : @@(x.data)
}
proc `@@`(x: TxReceipt): JsonNode =
result = %{
"root" : if x.root == Hash256(): %("0x") else: @@(x.root),
"status" : @@(x.status),
"cumulativeGasUsed": @@(x.cumulativeGasUsed),
"logsBloom" : @@(x.logsBloom),
"logs" : if x.logs.len == 0: newJNull() else: @@(x.logs),
"transactionHash" : @@(x.transactionHash),
"contractAddress" : @@(x.contractAddress),
"gasUsed" : @@(x.gasUsed),
"blockHash" : @@(x.blockHash),
"transactionIndex" : @@(x.transactionIndex)
}
if x.txType > TxLegacy:
result["type"] = %("0x" & toHex(x.txType.int, 1))
proc `@@`(x: RejectedTx): JsonNode =
result = %{
"index": %(x.index),
"error": %(x.error)
}
proc `@@`[T](x: seq[T]): JsonNode =
result = newJArray()
for c in x:
result.add @@(c)
proc `@@`[T](x: Option[T]): JsonNode =
if x.isNone:
newJNull()
else:
@@(x.get())
proc `@@`*(x: ExecutionResult): JsonNode =
result = %{
"stateRoot" : @@(x.stateRoot),
"txRoot" : @@(x.txRoot),
"receiptsRoot": @@(x.receiptsRoot),
"logsHash" : @@(x.logsHash),
"logsBloom" : @@(x.bloom),
"receipts" : @@(x.receipts),
"currentDifficulty": @@(x.currentDifficulty),
"gasUsed" : @@(x.gasUsed)
}
if x.rejected.len > 0:
result["rejected"] = @@(x.rejected)
if x.currentBaseFee.isSome:
result["currentBaseFee"] = @@(x.currentBaseFee)