use types from eth/common/eth_types (#6614)

* bump nim-eth

* use types from `eth/common/eth_types`
This commit is contained in:
Miran 2024-10-14 16:55:56 +02:00 committed by GitHub
parent c01cf6601a
commit 585deb8f90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 36 deletions

View File

@ -1287,7 +1287,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
doAssert sizeof(uint64) == sizeof(data.gasUsed) doAssert sizeof(uint64) == sizeof(data.gasUsed)
if data.nonce.isNone: if data.nonce.isNone:
return nil return nil
let blockHeader = ExecutionBlockHeader( let blockHeader = eth_types.Header(
parentHash: data.parentHash.asEth2Digest.to(Hash32), parentHash: data.parentHash.asEth2Digest.to(Hash32),
ommersHash: data.sha3Uncles.asEth2Digest.to(Hash32), ommersHash: data.sha3Uncles.asEth2Digest.to(Hash32),
coinbase: distinctBase(data.miner).to(EthAddress), coinbase: distinctBase(data.miner).to(EthAddress),
@ -1308,7 +1308,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
if data.withdrawalsRoot.isSome: if data.withdrawalsRoot.isSome:
Opt.some(data.withdrawalsRoot.get.asEth2Digest.to(Hash32)) Opt.some(data.withdrawalsRoot.get.asEth2Digest.to(Hash32))
else: else:
Opt.none(ExecutionHash256), Opt.none(Hash32),
blobGasUsed: blobGasUsed:
if data.blobGasUsed.isSome: if data.blobGasUsed.isSome:
Opt.some distinctBase(data.blobGasUsed.get) Opt.some distinctBase(data.blobGasUsed.get)
@ -1323,12 +1323,12 @@ proc ETHExecutionBlockHeaderCreateFromJson(
if data.parentBeaconBlockRoot.isSome: if data.parentBeaconBlockRoot.isSome:
Opt.some data.parentBeaconBlockRoot.get.asEth2Digest.to(Hash32) Opt.some data.parentBeaconBlockRoot.get.asEth2Digest.to(Hash32)
else: else:
Opt.none(ExecutionHash256), Opt.none(Hash32),
requestsRoot: requestsRoot:
if data.requestsRoot.isSome: if data.requestsRoot.isSome:
Opt.some(data.requestsRoot.get.asEth2Digest.to(Hash32)) Opt.some(data.requestsRoot.get.asEth2Digest.to(Hash32))
else: else:
Opt.none(ExecutionHash256)) Opt.none(Hash32))
if rlpHash(blockHeader) != executionHash[]: if rlpHash(blockHeader) != executionHash[]:
return nil return nil
@ -1345,7 +1345,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct withdrawal # Construct withdrawal
let let
wd = ExecutionWithdrawal( wd = eth_types.EthWithdrawal(
index: distinctBase(data.index), index: distinctBase(data.index),
validatorIndex: distinctBase(data.validatorIndex), validatorIndex: distinctBase(data.validatorIndex),
address: distinctBase(data.address).to(EthAddress), address: distinctBase(data.address).to(EthAddress),
@ -1379,7 +1379,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct deposit request # Construct deposit request
let let
req = ExecutionDepositRequest( req = eth_types.EthDepositRequest(
pubkey: distinctBase(data.pubkey).to(Bytes48), pubkey: distinctBase(data.pubkey).to(Bytes48),
withdrawalCredentials: distinctBase(data.withdrawalCredentials).to(Bytes32), withdrawalCredentials: distinctBase(data.withdrawalCredentials).to(Bytes32),
amount: distinctBase(data.amount), amount: distinctBase(data.amount),
@ -1411,7 +1411,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct withdrawal request # Construct withdrawal request
let let
req = ExecutionWithdrawalRequest( req = eth_types.EthWithdrawalRequest(
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress), sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
validatorPubkey: distinctBase(data.validatorPubkey).to(Bytes48), validatorPubkey: distinctBase(data.validatorPubkey).to(Bytes48),
amount: distinctBase(data.amount)) amount: distinctBase(data.amount))
@ -1439,7 +1439,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct consolidation request # Construct consolidation request
let let
req = ExecutionConsolidationRequest( req = eth_types.EthConsolidationRequest(
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress), sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
sourcePubkey: distinctBase(data.sourcePubkey).to(Bytes48), sourcePubkey: distinctBase(data.sourcePubkey).to(Bytes48),
targetPubkey: distinctBase(data.targetPubkey).to(Bytes48)) targetPubkey: distinctBase(data.targetPubkey).to(Bytes48))
@ -1755,7 +1755,7 @@ proc ETHTransactionsCreateFromJson(
if distinctBase(authorization.yParity) > 1: if distinctBase(authorization.yParity) > 1:
return nil return nil
let let
tx = ExecutionTransaction( tx = eth_types.EthTransaction(
txType: txType, txType: txType,
chainId: data.chainId.get(0.Quantity).ChainId, chainId: data.chainId.get(0.Quantity).ChainId,
nonce: distinctBase(data.nonce), nonce: distinctBase(data.nonce),
@ -2560,15 +2560,15 @@ proc ETHReceiptsCreateFromJson(
if distinctBase(data.cumulativeGasUsed) > int64.high.uint64: if distinctBase(data.cumulativeGasUsed) > int64.high.uint64:
return nil return nil
let let
rec = ExecutionReceipt( rec = eth_types.EthReceipt(
receiptType: txType, receiptType: txType,
isHash: data.root.isSome, isHash: data.root.isSome,
status: distinctBase(data.status.get(1.Quantity)) != 0'u64, status: distinctBase(data.status.get(1.Quantity)) != 0'u64,
hash: hash:
if data.root.isSome: if data.root.isSome:
ExecutionHash256(distinctBase(data.root.get)) Hash32(distinctBase(data.root.get))
else: else:
default(ExecutionHash256), default(Hash32),
cumulativeGasUsed: distinctBase(data.cumulativeGasUsed).GasInt, cumulativeGasUsed: distinctBase(data.cumulativeGasUsed).GasInt,
logsBloom: distinctBase(data.logsBloom).to(Bloom), logsBloom: distinctBase(data.logsBloom).to(Bloom),
logs: data.logs.mapIt(Log( logs: data.logs.mapIt(Log(

View File

@ -35,15 +35,6 @@ func toGwei*(eth: Ether): Gwei =
distinctBase(eth) * ETH_TO_GWEI distinctBase(eth) * ETH_TO_GWEI
type type
ExecutionHash256* = eth_types.Hash32
ExecutionTransaction* = eth_types.Transaction
ExecutionReceipt* = eth_types.Receipt
ExecutionWithdrawal* = eth_types.Withdrawal
ExecutionDepositRequest* = eth_types.DepositRequest
ExecutionWithdrawalRequest* = eth_types.WithdrawalRequest
ExecutionConsolidationRequest* = eth_types.ConsolidationRequest
ExecutionBlockHeader* = eth_types.Header
FinalityCheckpoints* = object FinalityCheckpoints* = object
justified*: Checkpoint justified*: Checkpoint
finalized*: Checkpoint finalized*: Checkpoint
@ -444,18 +435,18 @@ template append*(w: var RlpWriter, v: bellatrix.Transaction) =
w.appendRawBytes(distinctBase v) w.appendRawBytes(distinctBase v)
template append*(w: var RlpWriter, withdrawal: capella.Withdrawal) = template append*(w: var RlpWriter, withdrawal: capella.Withdrawal) =
w.appendRecordType(ExecutionWithdrawal( w.appendRecordType(EthWithdrawal(
index: withdrawal.index, index: withdrawal.index,
validatorIndex: withdrawal.validator_index, validatorIndex: withdrawal.validator_index,
address: EthAddress withdrawal.address.data, address: EthAddress withdrawal.address.data,
amount: distinctBase(withdrawal.amount))) amount: distinctBase(withdrawal.amount)))
proc computeTransactionsTrieRoot( proc computeTransactionsTrieRoot(
payload: ForkyExecutionPayload): ExecutionHash256 = payload: ForkyExecutionPayload): EthHash32 =
orderedTrieRoot(payload.transactions.asSeq) orderedTrieRoot(payload.transactions.asSeq)
func append*(w: var RlpWriter, request: electra.DepositRequest) = func append*(w: var RlpWriter, request: electra.DepositRequest) =
w.append ExecutionDepositRequest( w.append EthDepositRequest(
pubkey: Bytes48 request.pubkey.blob, pubkey: Bytes48 request.pubkey.blob,
withdrawalCredentials: Bytes32 request.withdrawal_credentials.data, withdrawalCredentials: Bytes32 request.withdrawal_credentials.data,
amount: distinctBase(request.amount), amount: distinctBase(request.amount),
@ -463,20 +454,20 @@ func append*(w: var RlpWriter, request: electra.DepositRequest) =
index: request.index) index: request.index)
func append*(w: var RlpWriter, request: electra.WithdrawalRequest) = func append*(w: var RlpWriter, request: electra.WithdrawalRequest) =
w.append ExecutionWithdrawalRequest( w.append EthWithdrawalRequest(
sourceAddress: Address request.source_address.data, sourceAddress: Address request.source_address.data,
validatorPubkey: Bytes48 request.validator_pubkey.blob, validatorPubkey: Bytes48 request.validator_pubkey.blob,
amount: distinctBase(request.amount)) amount: distinctBase(request.amount))
func append*(w: var RlpWriter, request: electra.ConsolidationRequest) = func append*(w: var RlpWriter, request: electra.ConsolidationRequest) =
w.append ExecutionConsolidationRequest( w.append EthConsolidationRequest(
sourceAddress: Address request.source_address.data, sourceAddress: Address request.source_address.data,
sourcePubkey: Bytes48 request.source_pubkey.blob, sourcePubkey: Bytes48 request.source_pubkey.blob,
targetPubkey: Bytes48 request.target_pubkey.blob) targetPubkey: Bytes48 request.target_pubkey.blob)
# https://eips.ethereum.org/EIPS/eip-7685 # https://eips.ethereum.org/EIPS/eip-7685
proc computeRequestsTrieRoot( proc computeRequestsTrieRoot(
requests: electra.ExecutionRequests): ExecutionHash256 = requests: electra.ExecutionRequests): EthHash32 =
let n = let n =
requests.deposits.len + requests.deposits.len +
requests.withdrawals.len + requests.withdrawals.len +
@ -494,7 +485,7 @@ proc computeRequestsTrieRoot(
b.rootHash() b.rootHash()
proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader = proc blockToBlockHeader*(blck: ForkyBeaconBlock): EthHeader =
template payload: auto = blck.body.execution_payload template payload: auto = blck.body.execution_payload
static: # `GasInt` is signed. We only use it for hashing. static: # `GasInt` is signed. We only use it for hashing.
@ -507,7 +498,7 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
when typeof(payload).kind >= ConsensusFork.Capella: when typeof(payload).kind >= ConsensusFork.Capella:
Opt.some orderedTrieRoot(payload.withdrawals.asSeq) Opt.some orderedTrieRoot(payload.withdrawals.asSeq)
else: else:
Opt.none(ExecutionHash256) Opt.none(EthHash32)
blobGasUsed = blobGasUsed =
when typeof(payload).kind >= ConsensusFork.Deneb: when typeof(payload).kind >= ConsensusFork.Deneb:
Opt.some payload.blob_gas_used Opt.some payload.blob_gas_used
@ -520,16 +511,16 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
Opt.none(uint64) Opt.none(uint64)
parentBeaconBlockRoot = parentBeaconBlockRoot =
when typeof(payload).kind >= ConsensusFork.Deneb: when typeof(payload).kind >= ConsensusFork.Deneb:
Opt.some ExecutionHash256(blck.parent_root.data) Opt.some EthHash32(blck.parent_root.data)
else: else:
Opt.none(ExecutionHash256) Opt.none(EthHash32)
requestsRoot = requestsRoot =
when typeof(payload).kind >= ConsensusFork.Electra: when typeof(payload).kind >= ConsensusFork.Electra:
Opt.some blck.body.execution_requests.computeRequestsTrieRoot() Opt.some blck.body.execution_requests.computeRequestsTrieRoot()
else: else:
Opt.none(ExecutionHash256) Opt.none(EthHash32)
ExecutionBlockHeader( EthHeader(
parentHash : payload.parent_hash.to(Hash32), parentHash : payload.parent_hash.to(Hash32),
ommersHash : EMPTY_UNCLE_HASH, ommersHash : EMPTY_UNCLE_HASH,
coinbase : EthAddress payload.fee_recipient.data, coinbase : EthAddress payload.fee_recipient.data,

View File

@ -13,9 +13,9 @@ import
"."/[helpers, state_transition_block] "."/[helpers, state_transition_block]
func readExecutionTransaction( func readExecutionTransaction(
txBytes: bellatrix.Transaction): Result[ExecutionTransaction, string] = txBytes: bellatrix.Transaction): Result[EthTransaction, string] =
try: try:
ok rlp.decode(distinctBase(txBytes), ExecutionTransaction) ok rlp.decode(distinctBase(txBytes), EthTransaction)
except RlpError as exc: except RlpError as exc:
err("Invalid transaction: " & exc.msg) err("Invalid transaction: " & exc.msg)

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 00c91a1dcaf488046bbc9b9fcbd430934312930f Subproject commit bb76daa6d93d217f9ad61e547f097e2eb2f1afbb