Prague types conversion

This commit is contained in:
jangko 2024-09-11 14:02:50 +07:00
parent fd3475ea3e
commit 8e8258e460
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 130 additions and 4 deletions

View File

@ -35,6 +35,13 @@ func txRoot(list: openArray[Web3Tx]): common.Hash256
{.noSideEffect.}: {.noSideEffect.}:
calcTxRoot(ethTxs(list)) calcTxRoot(ethTxs(list))
func requestsRoot(p: ExecutionPayload): Opt[common.Hash256]
{.gcsafe, raises:[].} =
{.noSideEffect.}:
let reqs = ethRequests(p)
if reqs.isNone: Opt.none(common.Hash256)
else: Opt.some(calcRequestsRoot reqs.get)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public functions # Public functions
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -59,7 +66,10 @@ func executionPayload*(blk: EthBlock): ExecutionPayload =
transactions : w3Txs blk.txs, transactions : w3Txs blk.txs,
withdrawals : w3Withdrawals blk.withdrawals, withdrawals : w3Withdrawals blk.withdrawals,
blobGasUsed : w3Qty blk.header.blobGasUsed, blobGasUsed : w3Qty blk.header.blobGasUsed,
excessBlobGas: w3Qty blk.header.excessBlobGas excessBlobGas: w3Qty blk.header.excessBlobGas,
depositRequests: w3DepositRequests blk.requests,
withdrawalRequests: w3WithdrawalRequests blk.requests,
consolidationRequests: w3ConsolidationRequests blk.requests,
) )
func executionPayloadV1V2*(blk: EthBlock): ExecutionPayloadV1OrV2 = func executionPayloadV1V2*(blk: EthBlock): ExecutionPayloadV1OrV2 =
@ -104,7 +114,8 @@ func blockHeader*(p: ExecutionPayload,
withdrawalsRoot: wdRoot p.withdrawals, withdrawalsRoot: wdRoot p.withdrawals,
blobGasUsed : u64(p.blobGasUsed), blobGasUsed : u64(p.blobGasUsed),
excessBlobGas : u64(p.excessBlobGas), excessBlobGas : u64(p.excessBlobGas),
parentBeaconBlockRoot: beaconRoot parentBeaconBlockRoot: beaconRoot,
requestsRoot : requestsRoot(p),
) )
func blockBody*(p: ExecutionPayload): func blockBody*(p: ExecutionPayload):
@ -113,6 +124,7 @@ func blockBody*(p: ExecutionPayload):
uncles : @[], uncles : @[],
transactions: ethTxs p.transactions, transactions: ethTxs p.transactions,
withdrawals : ethWithdrawals p.withdrawals, withdrawals : ethWithdrawals p.withdrawals,
requests : ethRequests(p),
) )
func ethBlock*(p: ExecutionPayload, func ethBlock*(p: ExecutionPayload,
@ -122,5 +134,6 @@ func ethBlock*(p: ExecutionPayload,
header : blockHeader(p, beaconRoot), header : blockHeader(p, beaconRoot),
uncles : @[], uncles : @[],
transactions: ethTxs p.transactions, transactions: ethTxs p.transactions,
withdrawals: ethWithdrawals p.withdrawals, withdrawals : ethWithdrawals p.withdrawals,
requests : ethRequests(p),
) )

View File

@ -12,6 +12,7 @@ import
web3/primitives as web3types, web3/primitives as web3types,
web3/eth_api_types, web3/eth_api_types,
web3/engine_api_types, web3/engine_api_types,
web3/execution_types,
eth/common/eth_types_rlp, eth/common/eth_types_rlp,
stew/byteutils, stew/byteutils,
../utils/utils ../utils/utils
@ -293,3 +294,112 @@ proc w3AccessList*(list: openArray[AccessPair]): seq[AccessTuple] =
result = newSeqOfCap[AccessTuple](list.len) result = newSeqOfCap[AccessTuple](list.len)
for x in list: for x in list:
result.add w3AccessTuple(x) result.add w3AccessTuple(x)
func w3DepositRequest*(x: DepositRequest): DepositRequestV1 =
DepositRequestV1(
pubkey: FixedBytes[48](x.pubkey),
withdrawalCredentials: FixedBytes[32](x.withdrawalCredentials),
amount: w3Qty x.amount,
signature: FixedBytes[96](x.signature),
index: w3Qty x.index,
)
func w3DepositRequests*(reqs: Opt[seq[Request]]): Opt[seq[DepositRequestV1]] =
if reqs.isNone:
return Opt.none(seq[DepositRequestV1])
var res: seq[DepositRequestV1]
for req in reqs.get:
if req.requestType == DepositRequestType:
res.add w3DepositRequest req.deposit
ok(res)
func w3WithdrawalRequest*(x: WithdrawalRequest): WithdrawalRequestV1 =
WithdrawalRequestV1(
sourceAddress: w3Addr x.sourceAddress,
validatorPubkey: FixedBytes[48](x.validatorPubkey),
amount: w3Qty x.amount,
)
func w3WithdrawalRequests*(reqs: Opt[seq[Request]]): Opt[seq[WithdrawalRequestV1]] =
if reqs.isNone:
return Opt.none(seq[WithdrawalRequestV1])
var res: seq[WithdrawalRequestV1]
for req in reqs.get:
if req.requestType == WithdrawalRequestType:
res.add w3WithdrawalRequest req.withdrawal
ok(res)
func w3ConsolidationRequest*(x: ConsolidationRequest): ConsolidationRequestV1 =
ConsolidationRequestV1(
sourceAddress: w3Addr x.sourceAddress,
sourcePubkey: FixedBytes[48](x.sourcePubkey),
targetPubkey: FixedBytes[48](x.targetPubkey),
)
func w3ConsolidationRequests*(reqs: Opt[seq[Request]]): Opt[seq[ConsolidationRequestV1]] =
if reqs.isNone:
return Opt.none(seq[ConsolidationRequestV1])
var res: seq[ConsolidationRequestV1]
for req in reqs.get:
if req.requestType == ConsolidationRequestType:
res.add w3ConsolidationRequest req.consolidation
ok(res)
func ethRequest*(x: DepositRequestV1): Request =
Request(
requestType: DepositRequestType,
deposit: DepositRequest(
pubkey: x.pubkey.bytes,
withdrawalCredentials: x.withdrawalCredentials.bytes,
amount: uint64 x.amount,
signature: x.signature.bytes,
index: uint64 x.index,
)
)
func ethRequest*(x: WithdrawalRequestV1): Request =
Request(
requestType: WithdrawalRequestType,
withdrawal: WithDrawalRequest(
sourceAddress: ethAddr x.sourceAddress,
validatorPubkey: x.validatorPubkey.bytes,
amount: uint64 x.amount,
)
)
func ethRequest*(x: ConsolidationRequestV1): Request =
Request(
requestType: ConsolidationRequestType,
consolidation: ConsolidationRequest(
sourceAddress: ethAddr x.sourceAddress,
sourcePubkey: x.sourcePubkey.bytes,
targetPubkey: x.targetPubkey.bytes,
)
)
func ethRequests*(p: ExecutionPayload): Opt[seq[Request]] =
if p.depositRequests.isNone and
p.withdrawalRequests.isNone and
p.consolidationRequests.isNone:
return Opt.none(seq[Request])
var res: seq[Request]
if p.depositRequests.isSome:
for x in p.depositRequests.get:
res.add ethRequest(x)
if p.withdrawalRequests.isSome:
for x in p.withdrawalRequests.get:
res.add ethRequest(x)
if p.consolidationRequests.isSome:
for x in p.consolidationRequests.get:
res.add ethRequest(x)
ok(res)

View File

@ -36,6 +36,9 @@ template calcWithdrawalsRoot*(withdrawals: openArray[Withdrawal]): Hash256 =
template calcReceiptsRoot*(receipts: openArray[Receipt]): Hash256 = template calcReceiptsRoot*(receipts: openArray[Receipt]): Hash256 =
calcRootHash(receipts) calcRootHash(receipts)
template calcRequestsRoot*(requests: openArray[Request]): Hash256 =
calcRootHash(requests)
func sumHash*(hashes: varargs[Hash256]): Hash256 = func sumHash*(hashes: varargs[Hash256]): Hash256 =
var ctx: sha256 var ctx: sha256
ctx.init() ctx.init()

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 11eafac0f086510f3d2fbd54573b4a1521bfbbf5 Subproject commit 59715353dbe73c49fbcde58e720387efd70d5684