2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
import
|
2022-08-04 05:06:31 +00:00
|
|
|
std/[times, json, strutils],
|
2022-04-11 10:00:39 +00:00
|
|
|
stew/byteutils,
|
2023-03-09 23:40:55 +00:00
|
|
|
eth/[common, common/eth_types, rlp], chronos,
|
2023-07-09 02:16:22 +00:00
|
|
|
json_rpc/[rpcclient, errors, jsonmarshal],
|
2023-10-23 13:59:57 +00:00
|
|
|
../../../nimbus/beacon/web3_eth_conv,
|
|
|
|
./types
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
import
|
|
|
|
web3/eth_api_types,
|
|
|
|
web3/engine_api_types,
|
|
|
|
web3/execution_types,
|
|
|
|
web3/engine_api,
|
|
|
|
web3/eth_api
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
export
|
|
|
|
execution_types,
|
|
|
|
rpcclient
|
2023-09-06 09:18:26 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
type
|
|
|
|
Hash256 = eth_types.Hash256
|
|
|
|
VersionedHash = engine_api_types.VersionedHash
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2022-08-04 05:06:31 +00:00
|
|
|
template wrapTry(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except ValueError as e:
|
|
|
|
return err(e.msg)
|
|
|
|
except JsonRpcError as ex:
|
|
|
|
return err(ex.msg)
|
|
|
|
|
|
|
|
template wrapTrySimpleRes(body: untyped) =
|
|
|
|
wrapTry:
|
|
|
|
let res = waitFor body
|
|
|
|
return ok(res)
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc forkchoiceUpdatedV1*(client: RpcClient,
|
|
|
|
update: ForkchoiceStateV1,
|
2024-06-14 07:31:08 +00:00
|
|
|
payloadAttributes = Opt.none(PayloadAttributesV1)):
|
2022-04-11 10:00:39 +00:00
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_forkchoiceUpdatedV1(update, payloadAttributes)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc forkchoiceUpdatedV2*(client: RpcClient,
|
|
|
|
update: ForkchoiceStateV1,
|
2024-06-14 07:31:08 +00:00
|
|
|
payloadAttributes = Opt.none(PayloadAttributes)):
|
2023-08-21 02:08:54 +00:00
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_forkchoiceUpdatedV2(update, payloadAttributes)
|
|
|
|
|
|
|
|
proc forkchoiceUpdatedV3*(client: RpcClient,
|
|
|
|
update: ForkchoiceStateV1,
|
2024-06-14 07:31:08 +00:00
|
|
|
payloadAttributes = Opt.none(PayloadAttributes)):
|
2023-08-21 02:08:54 +00:00
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_forkchoiceUpdatedV3(update, payloadAttributes)
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc getPayloadV1*(client: RpcClient, payloadId: PayloadID): Result[ExecutionPayloadV1, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_getPayloadV1(payloadId)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc getPayloadV2*(client: RpcClient, payloadId: PayloadID): Result[GetPayloadV2Response, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_getPayloadV2(payloadId)
|
|
|
|
|
|
|
|
proc getPayloadV3*(client: RpcClient, payloadId: PayloadID): Result[GetPayloadV3Response, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_getPayloadV3(payloadId)
|
|
|
|
|
2024-03-28 07:16:40 +00:00
|
|
|
proc getPayloadV4*(client: RpcClient, payloadId: PayloadID): Result[GetPayloadV4Response, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_getPayloadV4(payloadId)
|
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
proc getPayload*(client: RpcClient,
|
|
|
|
payloadId: PayloadID,
|
|
|
|
version: Version): Result[GetPayloadResponse, string] =
|
2024-03-28 07:16:40 +00:00
|
|
|
if version == Version.V4:
|
|
|
|
let x = client.getPayloadV4(payloadId).valueOr:
|
|
|
|
return err(error)
|
|
|
|
ok(GetPayloadResponse(
|
|
|
|
executionPayload: executionPayload(x.executionPayload),
|
2024-06-14 07:31:08 +00:00
|
|
|
blockValue: Opt.some(x.blockValue),
|
|
|
|
blobsBundle: Opt.some(x.blobsBundle),
|
|
|
|
shouldOverrideBuilder: Opt.some(x.shouldOverrideBuilder),
|
2024-03-28 07:16:40 +00:00
|
|
|
))
|
|
|
|
elif version == Version.V3:
|
2023-09-30 12:20:29 +00:00
|
|
|
let x = client.getPayloadV3(payloadId).valueOr:
|
|
|
|
return err(error)
|
|
|
|
ok(GetPayloadResponse(
|
|
|
|
executionPayload: executionPayload(x.executionPayload),
|
2024-06-14 07:31:08 +00:00
|
|
|
blockValue: Opt.some(x.blockValue),
|
|
|
|
blobsBundle: Opt.some(x.blobsBundle),
|
|
|
|
shouldOverrideBuilder: Opt.some(x.shouldOverrideBuilder),
|
2023-09-30 12:20:29 +00:00
|
|
|
))
|
|
|
|
elif version == Version.V2:
|
|
|
|
let x = client.getPayloadV2(payloadId).valueOr:
|
|
|
|
return err(error)
|
|
|
|
ok(GetPayloadResponse(
|
|
|
|
executionPayload: executionPayload(x.executionPayload),
|
2024-06-14 07:31:08 +00:00
|
|
|
blockValue: Opt.some(x.blockValue)
|
2023-09-30 12:20:29 +00:00
|
|
|
))
|
|
|
|
else:
|
|
|
|
let x = client.getPayloadV1(payloadId).valueOr:
|
|
|
|
return err(error)
|
|
|
|
ok(GetPayloadResponse(
|
|
|
|
executionPayload: executionPayload(x),
|
|
|
|
))
|
|
|
|
|
|
|
|
proc forkchoiceUpdated*(client: RpcClient,
|
|
|
|
update: ForkchoiceStateV1,
|
|
|
|
attr: PayloadAttributes):
|
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
|
|
|
case attr.version
|
2024-06-14 07:31:08 +00:00
|
|
|
of Version.V1: return client.forkchoiceUpdatedV1(update, Opt.some attr.V1)
|
|
|
|
of Version.V2: return client.forkchoiceUpdatedV2(update, Opt.some attr)
|
|
|
|
of Version.V3: return client.forkchoiceUpdatedV3(update, Opt.some attr)
|
2024-03-28 07:16:40 +00:00
|
|
|
of Version.V4: discard
|
2023-09-30 12:20:29 +00:00
|
|
|
|
|
|
|
proc forkchoiceUpdated*(client: RpcClient,
|
|
|
|
version: Version,
|
|
|
|
update: ForkchoiceStateV1,
|
2024-06-14 07:31:08 +00:00
|
|
|
attr = Opt.none(PayloadAttributes)):
|
2023-09-30 12:20:29 +00:00
|
|
|
Result[ForkchoiceUpdatedResponse, string] =
|
|
|
|
case version
|
2024-03-28 07:16:40 +00:00
|
|
|
of Version.V1: return client.forkchoiceUpdatedV1(update, attr.V1)
|
|
|
|
of Version.V2: return client.forkchoiceUpdatedV2(update, attr)
|
|
|
|
of Version.V3: return client.forkchoiceUpdatedV3(update, attr)
|
|
|
|
of Version.V4: discard
|
2023-09-30 12:20:29 +00:00
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc newPayloadV1*(client: RpcClient,
|
|
|
|
payload: ExecutionPayloadV1):
|
|
|
|
Result[PayloadStatusV1, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV1(payload)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
proc newPayloadV2*(client: RpcClient,
|
|
|
|
payload: ExecutionPayloadV2):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV2(payload)
|
|
|
|
|
2023-07-09 02:16:22 +00:00
|
|
|
proc newPayloadV2*(client: RpcClient,
|
|
|
|
payload: ExecutionPayloadV1OrV2):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV2(payload)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc newPayloadV3*(client: RpcClient,
|
|
|
|
payload: ExecutionPayloadV3,
|
|
|
|
versionedHashes: seq[VersionedHash],
|
|
|
|
parentBeaconBlockRoot: FixedBytes[32]
|
|
|
|
):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV3(payload, versionedHashes, parentBeaconBlockRoot)
|
|
|
|
|
2024-03-28 07:16:40 +00:00
|
|
|
proc newPayloadV4*(client: RpcClient,
|
|
|
|
payload: ExecutionPayloadV4,
|
|
|
|
versionedHashes: seq[VersionedHash],
|
|
|
|
parentBeaconBlockRoot: FixedBytes[32]
|
|
|
|
):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV4(payload, versionedHashes, parentBeaconBlockRoot)
|
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
proc newPayloadV1*(client: RpcClient,
|
|
|
|
payload: ExecutionPayload):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV1(payload)
|
|
|
|
|
|
|
|
proc newPayloadV2*(client: RpcClient,
|
|
|
|
payload: ExecutionPayload):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV2(payload)
|
|
|
|
|
|
|
|
proc newPayloadV3*(client: RpcClient,
|
|
|
|
payload: ExecutionPayload,
|
2024-06-14 07:31:08 +00:00
|
|
|
versionedHashes: Opt[seq[VersionedHash]],
|
|
|
|
parentBeaconBlockRoot: Opt[FixedBytes[32]]
|
2023-10-23 13:59:57 +00:00
|
|
|
):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV3(payload, versionedHashes, parentBeaconBlockRoot)
|
|
|
|
|
2024-03-28 07:16:40 +00:00
|
|
|
proc newPayloadV4*(client: RpcClient,
|
|
|
|
payload: ExecutionPayload,
|
2024-06-14 07:31:08 +00:00
|
|
|
versionedHashes: Opt[seq[VersionedHash]],
|
|
|
|
parentBeaconBlockRoot: Opt[FixedBytes[32]]
|
2024-03-28 07:16:40 +00:00
|
|
|
):
|
|
|
|
Result[PayloadStatusV1, string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_newPayloadV4(payload, versionedHashes, parentBeaconBlockRoot)
|
|
|
|
|
2023-09-30 14:31:57 +00:00
|
|
|
proc collectBlobHashes(list: openArray[Web3Tx]): seq[Web3Hash] =
|
|
|
|
for w3tx in list:
|
2024-02-21 09:14:20 +00:00
|
|
|
let tx = ethTx(w3tx)
|
2023-09-30 14:31:57 +00:00
|
|
|
for h in tx.versionedHashes:
|
|
|
|
result.add w3Hash(h)
|
|
|
|
|
2023-09-30 12:20:29 +00:00
|
|
|
proc newPayload*(client: RpcClient,
|
|
|
|
payload: ExecutionPayload,
|
2024-06-14 07:31:08 +00:00
|
|
|
beaconRoot = Opt.none(common.Hash256)): Result[PayloadStatusV1, string] =
|
2023-09-30 14:31:57 +00:00
|
|
|
case payload.version
|
|
|
|
of Version.V1: return client.newPayloadV1(payload.V1)
|
|
|
|
of Version.V2: return client.newPayloadV2(payload.V2)
|
|
|
|
of Version.V3:
|
2023-10-05 03:04:12 +00:00
|
|
|
if beaconRoot.isNone:
|
|
|
|
# fallback
|
|
|
|
return client.newPayloadV2(payload.V2)
|
2023-09-30 14:31:57 +00:00
|
|
|
let versionedHashes = collectBlobHashes(payload.transactions)
|
|
|
|
return client.newPayloadV3(payload.V3,
|
|
|
|
versionedHashes,
|
|
|
|
w3Hash beaconRoot.get)
|
2024-03-28 07:16:40 +00:00
|
|
|
of Version.V4:
|
|
|
|
let versionedHashes = collectBlobHashes(payload.transactions)
|
|
|
|
return client.newPayloadV4(payload.V4,
|
|
|
|
versionedHashes,
|
|
|
|
w3Hash beaconRoot.get)
|
2023-09-30 12:20:29 +00:00
|
|
|
|
2024-05-15 16:22:03 +00:00
|
|
|
proc newPayload*(client: RpcClient,
|
|
|
|
version: Version,
|
|
|
|
payload: ExecutionPayload,
|
2024-06-14 07:31:08 +00:00
|
|
|
beaconRoot = Opt.none(common.Hash256)): Result[PayloadStatusV1, string] =
|
2024-05-15 16:22:03 +00:00
|
|
|
case version
|
|
|
|
of Version.V1: return client.newPayloadV1(payload)
|
|
|
|
of Version.V2: return client.newPayloadV2(payload)
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
of Version.V3:
|
2024-05-15 16:22:03 +00:00
|
|
|
let versionedHashes = collectBlobHashes(payload.transactions)
|
|
|
|
return client.newPayloadV3(payload,
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some(versionedHashes),
|
2024-05-15 16:22:03 +00:00
|
|
|
w3Hash beaconRoot)
|
|
|
|
of Version.V4:
|
|
|
|
let versionedHashes = collectBlobHashes(payload.transactions)
|
|
|
|
return client.newPayloadV4(payload,
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some(versionedHashes),
|
2024-05-15 16:22:03 +00:00
|
|
|
w3Hash beaconRoot)
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
|
2023-10-23 13:59:57 +00:00
|
|
|
proc newPayload*(client: RpcClient,
|
|
|
|
version: Version,
|
|
|
|
payload: ExecutableData): Result[PayloadStatusV1, string] =
|
|
|
|
case version
|
|
|
|
of Version.V1: return client.newPayloadV1(payload.basePayload)
|
|
|
|
of Version.V2: return client.newPayloadV2(payload.basePayload)
|
|
|
|
of Version.V3:
|
|
|
|
return client.newPayloadV3(payload.basePayload,
|
|
|
|
w3Hashes payload.versionedHashes,
|
|
|
|
w3Hash payload.beaconRoot)
|
2024-03-28 07:16:40 +00:00
|
|
|
of Version.V4:
|
|
|
|
return client.newPayloadV4(payload.basePayload,
|
|
|
|
w3Hashes payload.versionedHashes,
|
|
|
|
w3Hash payload.beaconRoot)
|
2023-10-23 13:59:57 +00:00
|
|
|
|
2023-08-08 03:44:29 +00:00
|
|
|
proc exchangeCapabilities*(client: RpcClient,
|
|
|
|
methods: seq[string]):
|
|
|
|
Result[seq[string], string] =
|
|
|
|
wrapTrySimpleRes:
|
|
|
|
client.engine_exchangeCapabilities(methods)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc toBlockNonce(n: Opt[FixedBytes[8]]): common.BlockNonce =
|
2022-04-11 10:00:39 +00:00
|
|
|
if n.isNone:
|
|
|
|
return default(BlockNonce)
|
2023-12-08 09:35:50 +00:00
|
|
|
n.get.bytes
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc maybeU64(n: Opt[Quantity]): Opt[uint64] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if n.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(uint64)
|
|
|
|
Opt.some(n.get.uint64)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc maybeU64(n: Opt[Web3BlockNumber]): Opt[uint64] =
|
2024-03-21 01:05:22 +00:00
|
|
|
if n.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(uint64)
|
|
|
|
Opt.some(n.get.uint64)
|
2024-03-21 01:05:22 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc maybeBool(n: Opt[Quantity]): Opt[bool] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if n.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(bool)
|
|
|
|
Opt.some(n.get.bool)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc maybeChainId(n: Opt[Quantity]): Opt[ChainId] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if n.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(ChainId)
|
|
|
|
Opt.some(n.get.ChainId)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc maybeInt(n: Opt[Quantity]): Opt[int] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if n.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(int)
|
|
|
|
Opt.some(n.get.int)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2024-03-11 17:20:29 +00:00
|
|
|
proc toBlockHeader*(bc: BlockObject): common.BlockHeader =
|
2022-04-11 10:00:39 +00:00
|
|
|
common.BlockHeader(
|
2024-06-14 07:31:08 +00:00
|
|
|
number : common.BlockNumber bc.number,
|
2023-12-08 09:35:50 +00:00
|
|
|
parentHash : ethHash bc.parentHash,
|
2023-08-21 02:08:54 +00:00
|
|
|
nonce : toBlockNonce(bc.nonce),
|
2023-12-08 09:35:50 +00:00
|
|
|
ommersHash : ethHash bc.sha3Uncles,
|
2024-06-14 07:31:08 +00:00
|
|
|
logsBloom : BloomFilter bc.logsBloom,
|
2023-12-08 09:35:50 +00:00
|
|
|
txRoot : ethHash bc.transactionsRoot,
|
|
|
|
stateRoot : ethHash bc.stateRoot,
|
2024-06-14 07:31:08 +00:00
|
|
|
receiptsRoot : ethHash bc.receiptsRoot,
|
2023-12-08 09:35:50 +00:00
|
|
|
coinbase : ethAddr bc.miner,
|
|
|
|
difficulty : bc.difficulty,
|
|
|
|
extraData : bc.extraData.bytes,
|
2024-06-14 07:31:08 +00:00
|
|
|
mixHash : ethHash bc.mixHash,
|
2023-12-08 09:35:50 +00:00
|
|
|
gasLimit : bc.gasLimit.GasInt,
|
|
|
|
gasUsed : bc.gasUsed.GasInt,
|
|
|
|
timestamp : EthTime bc.timestamp,
|
2024-06-14 07:31:08 +00:00
|
|
|
baseFeePerGas : bc.baseFeePerGas,
|
2023-12-08 09:35:50 +00:00
|
|
|
withdrawalsRoot: ethHash bc.withdrawalsRoot,
|
2023-08-21 02:08:54 +00:00
|
|
|
blobGasUsed : maybeU64(bc.blobGasUsed),
|
|
|
|
excessBlobGas : maybeU64(bc.excessBlobGas),
|
2023-12-08 09:35:50 +00:00
|
|
|
parentBeaconBlockRoot: ethHash bc.parentBeaconBlockRoot,
|
2022-04-11 10:00:39 +00:00
|
|
|
)
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
func vHashes(x: Opt[seq[Web3Hash]]): seq[common.Hash256] =
|
2023-12-08 09:35:50 +00:00
|
|
|
if x.isNone: return
|
|
|
|
else: ethHashes(x.get)
|
|
|
|
|
|
|
|
proc toTransaction(tx: TransactionObject): Transaction =
|
|
|
|
common.Transaction(
|
|
|
|
txType : tx.`type`.get(0.Web3Quantity).TxType,
|
|
|
|
chainId : tx.chainId.get(0.Web3Quantity).ChainId,
|
|
|
|
nonce : tx.nonce.AccountNonce,
|
|
|
|
gasPrice : tx.gasPrice.GasInt,
|
2024-06-14 07:31:08 +00:00
|
|
|
maxPriorityFeePerGas: tx.maxPriorityFeePerGas.get(0.Web3Quantity).GasInt,
|
|
|
|
maxFeePerGas : tx.maxFeePerGas.get(0.Web3Quantity).GasInt,
|
2023-12-08 09:35:50 +00:00
|
|
|
gasLimit : tx.gas.GasInt,
|
|
|
|
to : ethAddr tx.to,
|
|
|
|
value : tx.value,
|
|
|
|
payload : tx.input,
|
2024-03-21 11:24:32 +00:00
|
|
|
accessList : ethAccessList(tx.accessList),
|
2023-12-08 09:35:50 +00:00
|
|
|
maxFeePerBlobGas: tx.maxFeePerBlobGas.get(0.u256),
|
|
|
|
versionedHashes : vHashes(tx.blobVersionedHashes),
|
2024-06-14 07:31:08 +00:00
|
|
|
V : tx.v.uint64,
|
2023-12-08 09:35:50 +00:00
|
|
|
R : tx.r,
|
|
|
|
S : tx.s,
|
|
|
|
)
|
|
|
|
|
2024-03-11 17:20:29 +00:00
|
|
|
proc toTransactions*(txs: openArray[TxOrHash]): seq[Transaction] =
|
2022-04-11 10:00:39 +00:00
|
|
|
for x in txs:
|
2023-12-08 09:35:50 +00:00
|
|
|
doAssert x.kind == tohTx
|
|
|
|
result.add toTransaction(x.tx)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
proc toWithdrawal(wd: WithdrawalObject): Withdrawal =
|
2023-08-21 02:08:54 +00:00
|
|
|
Withdrawal(
|
2023-12-08 09:35:50 +00:00
|
|
|
index: wd.index.uint64,
|
|
|
|
validatorIndex: wd.validatorIndex.uint64,
|
|
|
|
address: ethAddr wd.address,
|
|
|
|
amount: wd.amount.uint64,
|
2023-08-21 02:08:54 +00:00
|
|
|
)
|
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
proc toWithdrawals(list: seq[WithdrawalObject]): seq[Withdrawal] =
|
2023-08-21 02:08:54 +00:00
|
|
|
result = newSeqOfCap[Withdrawal](list.len)
|
|
|
|
for wd in list:
|
|
|
|
result.add toWithdrawal(wd)
|
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc toWithdrawals*(list: Opt[seq[WithdrawalObject]]): Opt[seq[Withdrawal]] =
|
2023-08-21 02:08:54 +00:00
|
|
|
if list.isNone:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(seq[Withdrawal])
|
|
|
|
Opt.some(toWithdrawals(list.get))
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RPCReceipt* = object
|
|
|
|
txHash*: Hash256
|
|
|
|
txIndex*: int
|
|
|
|
blockHash*: Hash256
|
|
|
|
blockNumber*: uint64
|
|
|
|
sender*: EthAddress
|
2024-06-14 07:31:08 +00:00
|
|
|
to*: Opt[EthAddress]
|
2023-08-21 02:08:54 +00:00
|
|
|
cumulativeGasUsed*: GasInt
|
|
|
|
gasUsed*: GasInt
|
2024-06-14 07:31:08 +00:00
|
|
|
contractAddress*: Opt[EthAddress]
|
2023-12-08 09:35:50 +00:00
|
|
|
logs*: seq[LogObject]
|
2023-08-21 02:08:54 +00:00
|
|
|
logsBloom*: FixedBytes[256]
|
|
|
|
recType*: ReceiptType
|
2024-06-14 07:31:08 +00:00
|
|
|
stateRoot*: Opt[Hash256]
|
|
|
|
status*: Opt[bool]
|
2023-08-21 02:08:54 +00:00
|
|
|
effectiveGasPrice*: GasInt
|
2024-06-14 07:31:08 +00:00
|
|
|
blobGasUsed*: Opt[uint64]
|
|
|
|
blobGasPrice*: Opt[UInt256]
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
RPCTx* = object
|
|
|
|
txType*: TxType
|
2024-06-14 07:31:08 +00:00
|
|
|
blockHash*: Opt[Hash256] # none if pending
|
|
|
|
blockNumber*: Opt[uint64]
|
2023-08-21 02:08:54 +00:00
|
|
|
sender*: EthAddress
|
|
|
|
gasLimit*: GasInt
|
|
|
|
gasPrice*: GasInt
|
|
|
|
maxFeePerGas*: GasInt
|
|
|
|
maxPriorityFeePerGas*: GasInt
|
|
|
|
hash*: Hash256
|
|
|
|
payload*: seq[byte]
|
|
|
|
nonce*: AccountNonce
|
2024-06-14 07:31:08 +00:00
|
|
|
to*: Opt[EthAddress]
|
|
|
|
txIndex*: Opt[int]
|
2023-08-21 02:08:54 +00:00
|
|
|
value*: UInt256
|
2024-06-14 07:31:08 +00:00
|
|
|
v*: uint64
|
2023-08-21 02:08:54 +00:00
|
|
|
r*: UInt256
|
|
|
|
s*: UInt256
|
2024-06-14 07:31:08 +00:00
|
|
|
chainId*: Opt[ChainId]
|
|
|
|
accessList*: Opt[seq[AccessTuple]]
|
|
|
|
maxFeePerBlobGas*: Opt[UInt256]
|
|
|
|
versionedHashes*: Opt[VersionedHashes]
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
proc toRPCReceipt(rec: ReceiptObject): RPCReceipt =
|
2023-08-21 02:08:54 +00:00
|
|
|
RPCReceipt(
|
2023-12-08 09:35:50 +00:00
|
|
|
txHash: ethHash rec.transactionHash,
|
|
|
|
txIndex: rec.transactionIndex.int,
|
|
|
|
blockHash: ethHash rec.blockHash,
|
|
|
|
blockNumber: rec.blockNumber.uint64,
|
|
|
|
sender: ethAddr rec.`from`,
|
|
|
|
to: ethAddr rec.to,
|
|
|
|
cumulativeGasUsed: rec.cumulativeGasUsed.GasInt,
|
|
|
|
gasUsed: rec.gasUsed.GasInt,
|
|
|
|
contractAddress: ethAddr rec.contractAddress,
|
2023-08-21 02:08:54 +00:00
|
|
|
logs: rec.logs,
|
|
|
|
logsBloom: rec.logsBloom,
|
2023-12-08 09:35:50 +00:00
|
|
|
recType: rec.`type`.get(0.Web3Quantity).ReceiptType,
|
|
|
|
stateRoot: ethHash rec.root,
|
2023-08-21 02:08:54 +00:00
|
|
|
status: maybeBool(rec.status),
|
2023-12-08 09:35:50 +00:00
|
|
|
effectiveGasPrice: rec.effectiveGasPrice.GasInt,
|
2023-11-02 04:38:07 +00:00
|
|
|
blobGasUsed: maybeU64(rec.blobGasUsed),
|
2023-12-08 09:35:50 +00:00
|
|
|
blobGasPrice: rec.blobGasPrice,
|
2023-08-21 02:08:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc toRPCTx(tx: eth_api.TransactionObject): RPCTx =
|
|
|
|
RPCTx(
|
2023-12-08 09:35:50 +00:00
|
|
|
txType: tx.`type`.get(0.Web3Quantity).TxType,
|
|
|
|
blockHash: ethHash tx.blockHash,
|
2023-08-21 02:08:54 +00:00
|
|
|
blockNumber: maybeU64 tx.blockNumber,
|
2023-12-08 09:35:50 +00:00
|
|
|
sender: ethAddr tx.`from`,
|
|
|
|
gasLimit: tx.gas.GasInt,
|
|
|
|
gasPrice: tx.gasPrice.GasInt,
|
|
|
|
maxFeePerGas: tx.maxFeePerGas.get(0.Web3Quantity).GasInt,
|
|
|
|
maxPriorityFeePerGas: tx.maxPriorityFeePerGas.get(0.Web3Quantity).GasInt,
|
|
|
|
hash: ethHash tx.hash,
|
2023-08-21 02:08:54 +00:00
|
|
|
payload: tx.input,
|
2023-12-08 09:35:50 +00:00
|
|
|
nonce: tx.nonce.AccountNonce,
|
|
|
|
to: ethAddr tx.to,
|
2023-08-21 02:08:54 +00:00
|
|
|
txIndex: maybeInt(tx.transactionIndex),
|
2023-12-08 09:35:50 +00:00
|
|
|
value: tx.value,
|
2024-06-14 07:31:08 +00:00
|
|
|
v: tx.v.uint64,
|
2023-12-08 09:35:50 +00:00
|
|
|
r: tx.r,
|
|
|
|
s: tx.s,
|
2023-08-21 02:08:54 +00:00
|
|
|
chainId: maybeChainId(tx.chainId),
|
|
|
|
accessList: tx.accessList,
|
2023-12-08 09:35:50 +00:00
|
|
|
maxFeePerBlobGas: tx.maxFeePerBlobGas,
|
|
|
|
versionedHashes: ethHashes tx.blobVersionedHashes,
|
2023-08-21 02:08:54 +00:00
|
|
|
)
|
|
|
|
|
2022-04-11 10:00:39 +00:00
|
|
|
proc waitForTTD*(client: RpcClient,
|
|
|
|
ttd: DifficultyInt): Future[(common.BlockHeader, bool)] {.async.} =
|
|
|
|
let period = chronos.seconds(5)
|
|
|
|
var loop = 0
|
|
|
|
var emptyHeader: common.BlockHeader
|
|
|
|
while loop < 5:
|
2023-12-08 09:35:50 +00:00
|
|
|
let bc = await client.eth_getBlockByNumber("latest", false)
|
|
|
|
if bc.isNil:
|
2022-04-11 10:00:39 +00:00
|
|
|
return (emptyHeader, false)
|
2023-12-08 09:35:50 +00:00
|
|
|
if bc.totalDifficulty >= ttd:
|
2022-04-11 10:00:39 +00:00
|
|
|
return (toBlockHeader(bc), true)
|
|
|
|
|
|
|
|
await sleepAsync(period)
|
|
|
|
inc loop
|
|
|
|
|
|
|
|
return (emptyHeader, false)
|
|
|
|
|
|
|
|
proc blockNumber*(client: RpcClient): Result[uint64, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2022-04-11 10:00:39 +00:00
|
|
|
let res = waitFor client.eth_blockNumber()
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.uint64)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc headerByNumber*(client: RpcClient, number: uint64): Result[common.BlockHeader, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getBlockByNumber(blockId(number), false)
|
|
|
|
if res.isNil:
|
2022-04-11 10:00:39 +00:00
|
|
|
return err("failed to get blockHeader: " & $number)
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toBlockHeader)
|
|
|
|
|
|
|
|
#proc blockByNumber*(client: RpcClient, number: uint64, output: var common.EthBlock): Result[void, string] =
|
|
|
|
# wrapTry:
|
|
|
|
# let res = waitFor client.eth_getBlockByNumber(blockId(number), true)
|
|
|
|
# if res.isNil:
|
|
|
|
# return err("failed to get block: " & $number)
|
|
|
|
# output.header = toBlockHeader(res)
|
|
|
|
# output.txs = toTransactions(res.transactions)
|
|
|
|
# output.withdrawals = toWithdrawals(res.withdrawals)
|
|
|
|
# return ok()
|
2022-05-29 04:23:03 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc headerByHash*(client: RpcClient, hash: Hash256): Result[common.BlockHeader, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getBlockByHash(w3Hash hash, false)
|
|
|
|
if res.isNil:
|
2022-06-13 09:42:01 +00:00
|
|
|
return err("failed to get block: " & hash.data.toHex)
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toBlockHeader)
|
2022-06-13 09:42:01 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc latestHeader*(client: RpcClient): Result[common.BlockHeader, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getBlockByNumber(blockId("latest"), false)
|
|
|
|
if res.isNil:
|
2022-04-11 10:00:39 +00:00
|
|
|
return err("failed to get latest blockHeader")
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toBlockHeader)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-11-01 11:09:49 +00:00
|
|
|
proc latestBlock*(client: RpcClient): Result[common.EthBlock, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getBlockByNumber(blockId("latest"), true)
|
|
|
|
if res.isNil:
|
2022-04-11 10:00:39 +00:00
|
|
|
return err("failed to get latest blockHeader")
|
2023-11-01 11:09:49 +00:00
|
|
|
let output = EthBlock(
|
2023-12-08 09:35:50 +00:00
|
|
|
header: toBlockHeader(res),
|
Consolidate block type for block processing (#2325)
This PR consolidates the split header-body sequences into a single EthBlock
sequence and cleans up the fallout from that which significantly reduces
block processing overhead during import thanks to less garbage collection
and fewer copies of things all around.
Notably, since the number of headers must always match the number of bodies,
we also get rid of a pointless degree of freedom that in the future could
introduce unnecessary bugs.
* only read header and body from era file
* avoid several unnecessary copies along the block processing way
* simplify signatures, cleaning up unused arguemnts and returns
* use `stew/assign2` in a few strategic places where the generated
nim assignent is slow and add a few `move` to work around poor
analysis in nim 1.6 (will need to be revisited for 2.0)
```
stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv
bps_x bps_y tps_x tps_y bpsd tpsd timed
block_number
(498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92%
(713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21%
(928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28%
(1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50%
(1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80%
(1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91%
(1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91%
(2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61%
(2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80%
blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s
bpsd (mean): 19.55%
tpsd (mean): 19.55%
Time (total): -29m13s, -15.14%
```
2024-06-09 14:32:20 +00:00
|
|
|
transactions: toTransactions(res.transactions),
|
2023-12-08 09:35:50 +00:00
|
|
|
withdrawals: toWithdrawals(res.withdrawals),
|
2023-11-01 11:09:49 +00:00
|
|
|
)
|
|
|
|
return ok(output)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
proc namedHeader*(client: RpcClient, name: string): Result[common.BlockHeader, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2022-06-27 04:15:23 +00:00
|
|
|
let res = waitFor client.eth_getBlockByNumber(name, false)
|
2023-12-08 09:35:50 +00:00
|
|
|
if res.isNil:
|
2022-06-27 04:15:23 +00:00
|
|
|
return err("failed to get named blockHeader")
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toBlockHeader)
|
2022-06-27 13:18:54 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc sendTransaction*(
|
|
|
|
client: RpcClient, tx: common.PooledTransaction): Result[void, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2022-04-11 10:00:39 +00:00
|
|
|
let encodedTx = rlp.encode(tx)
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_sendRawTransaction(encodedTx)
|
2022-04-11 10:00:39 +00:00
|
|
|
let txHash = rlpHash(tx)
|
2023-12-08 09:35:50 +00:00
|
|
|
let getHash = ethHash res
|
2022-04-11 10:00:39 +00:00
|
|
|
if txHash != getHash:
|
|
|
|
return err("sendTransaction: tx hash mismatch")
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc balanceAt*(client: RpcClient, address: EthAddress): Result[UInt256, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getBalance(w3Addr(address), blockId("latest"))
|
|
|
|
return ok(res)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
proc balanceAt*(client: RpcClient, address: EthAddress, number: common.BlockNumber): Result[UInt256, string] =
|
2023-08-21 02:08:54 +00:00
|
|
|
wrapTry:
|
2024-06-14 07:31:08 +00:00
|
|
|
let res = waitFor client.eth_getBalance(w3Addr(address), blockId(number))
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-07-09 02:16:22 +00:00
|
|
|
proc nonceAt*(client: RpcClient, address: EthAddress): Result[AccountNonce, string] =
|
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getTransactionCount(w3Addr(address), blockId("latest"))
|
|
|
|
return ok(res.AccountNonce)
|
2023-07-09 02:16:22 +00:00
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
proc txReceipt*(client: RpcClient, txHash: Hash256): Result[RPCReceipt, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getTransactionReceipt(w3Hash txHash)
|
|
|
|
if res.isNil:
|
2022-04-11 10:00:39 +00:00
|
|
|
return err("failed to get receipt: " & txHash.data.toHex)
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toRPCReceipt)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
proc txByHash*(client: RpcClient, txHash: Hash256): Result[RPCTx, string] =
|
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getTransactionByHash(w3Hash txHash)
|
|
|
|
if res.isNil:
|
2023-08-21 02:08:54 +00:00
|
|
|
return err("failed to get transaction: " & txHash.data.toHex)
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res.toRPCTx)
|
2022-06-27 13:18:54 +00:00
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
proc storageAt*(client: RpcClient, address: EthAddress, slot: UInt256): Result[FixedBytes[32], string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = waitFor client.eth_getStorageAt(w3Addr(address), slot, blockId("latest"))
|
|
|
|
return ok(res)
|
2022-04-11 10:00:39 +00:00
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
proc storageAt*(client: RpcClient, address: EthAddress, slot: UInt256, number: common.BlockNumber): Result[FixedBytes[32], string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2024-06-14 07:31:08 +00:00
|
|
|
let res = waitFor client.eth_getStorageAt(w3Addr(address), slot, blockId(number))
|
2023-12-08 09:35:50 +00:00
|
|
|
return ok(res)
|
2022-05-29 04:23:03 +00:00
|
|
|
|
|
|
|
proc verifyPoWProgress*(client: RpcClient, lastBlockHash: Hash256): Future[Result[void, string]] {.async.} =
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = await client.eth_getBlockByHash(w3Hash lastBlockHash, false)
|
|
|
|
if res.isNil:
|
2022-05-29 04:23:03 +00:00
|
|
|
return err("cannot get block by hash " & lastBlockHash.data.toHex)
|
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
let header = res
|
2024-03-21 01:05:22 +00:00
|
|
|
let number = header.number.u256
|
2022-05-29 04:23:03 +00:00
|
|
|
|
|
|
|
let period = chronos.seconds(3)
|
|
|
|
var loop = 0
|
|
|
|
while loop < 5:
|
2023-12-08 09:35:50 +00:00
|
|
|
let res = await client.eth_getBlockByNumber(blockId("latest"), false)
|
|
|
|
if res.isNil:
|
2022-05-29 04:23:03 +00:00
|
|
|
return err("cannot get latest block")
|
|
|
|
|
|
|
|
# Chain has progressed, check that the next block is also PoW
|
|
|
|
# Difficulty must NOT be zero
|
2023-12-08 09:35:50 +00:00
|
|
|
let bc = res
|
|
|
|
let diff = bc.difficulty
|
|
|
|
if diff.isZero:
|
2022-05-29 04:23:03 +00:00
|
|
|
return err("Expected PoW chain to progress in PoW mode, but following block difficulty: " & $diff)
|
|
|
|
|
2024-03-21 01:05:22 +00:00
|
|
|
if bc.number.u256 > number:
|
2022-05-29 04:23:03 +00:00
|
|
|
return ok()
|
|
|
|
|
|
|
|
await sleepAsync(period)
|
|
|
|
inc loop
|
|
|
|
|
|
|
|
return err("verify PoW Progress timeout")
|
2022-08-04 05:06:31 +00:00
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
type
|
|
|
|
TraceOpts = object
|
|
|
|
disableStorage: bool
|
|
|
|
disableMemory: bool
|
|
|
|
disableState: bool
|
|
|
|
disableStateDiff: bool
|
|
|
|
|
|
|
|
TraceOpts.useDefaultSerializationIn JrpcConv
|
|
|
|
|
|
|
|
createRpcSigsFromNim(RpcClient):
|
|
|
|
proc debug_traceTransaction(hash: TxHash, opts: TraceOpts): JsonNode
|
2022-08-04 05:06:31 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc debugPrevRandaoTransaction*(
|
|
|
|
client: RpcClient,
|
|
|
|
tx: PooledTransaction,
|
|
|
|
expectedPrevRandao: Hash256): Result[void, string] =
|
2022-08-04 05:06:31 +00:00
|
|
|
wrapTry:
|
2023-12-08 09:35:50 +00:00
|
|
|
let hash = w3Hash tx.rlpHash
|
2022-08-04 05:06:31 +00:00
|
|
|
# we only interested in stack, disable all other elems
|
2024-01-13 01:41:57 +00:00
|
|
|
let opts = TraceOpts(
|
|
|
|
disableStorage: true,
|
|
|
|
disableMemory: true,
|
|
|
|
disableState: true,
|
|
|
|
disableStateDiff: true
|
|
|
|
)
|
|
|
|
|
|
|
|
let res = waitFor client.debug_traceTransaction(hash, opts)
|
2022-08-04 05:06:31 +00:00
|
|
|
let structLogs = res["structLogs"]
|
|
|
|
|
|
|
|
var prevRandaoFound = false
|
|
|
|
for i, x in structLogs.elems:
|
|
|
|
let op = x["op"].getStr
|
|
|
|
if op != "DIFFICULTY": continue
|
|
|
|
|
|
|
|
if i+1 >= structLogs.len:
|
|
|
|
return err("No information after PREVRANDAO operation")
|
|
|
|
|
|
|
|
prevRandaoFound = true
|
|
|
|
let stack = structLogs[i+1]["stack"]
|
|
|
|
if stack.len < 1:
|
|
|
|
return err("Invalid stack after PREVRANDAO operation")
|
|
|
|
|
|
|
|
let stackHash = Hash256(data: hextoByteArray[32](stack[0].getStr))
|
|
|
|
if stackHash != expectedPrevRandao:
|
|
|
|
return err("Invalid stack after PREVRANDAO operation $1 != $2" % [stackHash.data.toHex, expectedPrevRandao.data.toHex])
|
|
|
|
|
|
|
|
if not prevRandaoFound:
|
|
|
|
return err("PREVRANDAO opcode not found")
|
|
|
|
|
|
|
|
return ok()
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
template expectBalanceEqual*(res: Result[UInt256, string], account: EthAddress,
|
|
|
|
expectedBalance: UInt256): auto =
|
|
|
|
if res.isErr:
|
|
|
|
return err(res.error)
|
|
|
|
if res.get != expectedBalance:
|
|
|
|
return err("invalid wd balance at $1, expect $2, get $3" % [
|
|
|
|
account.toHex, $expectedBalance, $res.get])
|
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
template expectStorageEqual*(res: Result[FixedBytes[32], string], account: EthAddress,
|
|
|
|
expectedValue: FixedBytes[32]): auto =
|
2023-08-21 02:08:54 +00:00
|
|
|
if res.isErr:
|
|
|
|
return err(res.error)
|
|
|
|
if res.get != expectedValue:
|
|
|
|
return err("invalid wd storage at $1 is $2, expect $3" % [
|
|
|
|
account.toHex, $res.get, $expectedValue])
|