Skip to content

Commit

Permalink
use types from eth/common/eth_types
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Oct 10, 2024
1 parent a85cc01 commit 658ff29
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
24 changes: 12 additions & 12 deletions beacon_chain/libnimbus_lc/libnimbus_lc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
doAssert sizeof(uint64) == sizeof(data.gasUsed)
if data.nonce.isNone:
return nil
let blockHeader = ExecutionBlockHeader(
let blockHeader = eth_types.Header(
parentHash: data.parentHash.asEth2Digest.to(Hash32),
ommersHash: data.sha3Uncles.asEth2Digest.to(Hash32),
coinbase: distinctBase(data.miner).to(EthAddress),
Expand All @@ -1308,7 +1308,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
if data.withdrawalsRoot.isSome:
Opt.some(data.withdrawalsRoot.get.asEth2Digest.to(Hash32))
else:
Opt.none(ExecutionHash256),
Opt.none(Hash32),
blobGasUsed:
if data.blobGasUsed.isSome:
Opt.some distinctBase(data.blobGasUsed.get)
Expand All @@ -1323,12 +1323,12 @@ proc ETHExecutionBlockHeaderCreateFromJson(
if data.parentBeaconBlockRoot.isSome:
Opt.some data.parentBeaconBlockRoot.get.asEth2Digest.to(Hash32)
else:
Opt.none(ExecutionHash256),
Opt.none(Hash32),
requestsRoot:
if data.requestsRoot.isSome:
Opt.some(data.requestsRoot.get.asEth2Digest.to(Hash32))
else:
Opt.none(ExecutionHash256))
Opt.none(Hash32))
if rlpHash(blockHeader) != executionHash[]:
return nil

Expand All @@ -1345,7 +1345,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(

# Construct withdrawal
let
wd = ExecutionWithdrawal(
wd = eth_types.EthWithdrawal(
index: distinctBase(data.index),
validatorIndex: distinctBase(data.validatorIndex),
address: distinctBase(data.address).to(EthAddress),
Expand Down Expand Up @@ -1379,7 +1379,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(

# Construct deposit request
let
req = ExecutionDepositRequest(
req = eth_types.EthDepositRequest(
pubkey: distinctBase(data.pubkey).to(Bytes48),
withdrawalCredentials: distinctBase(data.withdrawalCredentials).to(Bytes32),
amount: distinctBase(data.amount),
Expand Down Expand Up @@ -1411,7 +1411,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(

# Construct withdrawal request
let
req = ExecutionWithdrawalRequest(
req = eth_types.EthWithdrawalRequest(
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
validatorPubkey: distinctBase(data.validatorPubkey).to(Bytes48),
amount: distinctBase(data.amount))
Expand Down Expand Up @@ -1439,7 +1439,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(

# Construct consolidation request
let
req = ExecutionConsolidationRequest(
req = eth_types.EthConsolidationRequest(
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
sourcePubkey: distinctBase(data.sourcePubkey).to(Bytes48),
targetPubkey: distinctBase(data.targetPubkey).to(Bytes48))
Expand Down Expand Up @@ -1755,7 +1755,7 @@ proc ETHTransactionsCreateFromJson(
if distinctBase(authorization.yParity) > 1:
return nil
let
tx = ExecutionTransaction(
tx = eth_types.EthTransaction(
txType: txType,
chainId: data.chainId.get(0.Quantity).ChainId,
nonce: distinctBase(data.nonce),
Expand Down Expand Up @@ -2560,15 +2560,15 @@ proc ETHReceiptsCreateFromJson(
if distinctBase(data.cumulativeGasUsed) > int64.high.uint64:
return nil
let
rec = ExecutionReceipt(
rec = eth_types.EthReceipt(
receiptType: txType,
isHash: data.root.isSome,
status: distinctBase(data.status.get(1.Quantity)) != 0'u64,
hash:
if data.root.isSome:
ExecutionHash256(distinctBase(data.root.get))
Hash32(distinctBase(data.root.get))
else:
default(ExecutionHash256),
default(Hash32),
cumulativeGasUsed: distinctBase(data.cumulativeGasUsed).GasInt,
logsBloom: distinctBase(data.logsBloom).to(Bloom),
logs: data.logs.mapIt(Log(
Expand Down
33 changes: 12 additions & 21 deletions beacon_chain/spec/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ func toGwei*(eth: Ether): Gwei =
distinctBase(eth) * ETH_TO_GWEI

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
justified*: Checkpoint
finalized*: Checkpoint
Expand Down Expand Up @@ -444,39 +435,39 @@ template append*(w: var RlpWriter, v: bellatrix.Transaction) =
w.appendRawBytes(distinctBase v)

template append*(w: var RlpWriter, withdrawal: capella.Withdrawal) =
w.appendRecordType(ExecutionWithdrawal(
w.appendRecordType(EthWithdrawal(
index: withdrawal.index,
validatorIndex: withdrawal.validator_index,
address: EthAddress withdrawal.address.data,
amount: distinctBase(withdrawal.amount)))

proc computeTransactionsTrieRoot(
payload: ForkyExecutionPayload): ExecutionHash256 =
payload: ForkyExecutionPayload): EthHash32 =
orderedTrieRoot(payload.transactions.asSeq)

func append*(w: var RlpWriter, request: electra.DepositRequest) =
w.append ExecutionDepositRequest(
w.append EthDepositRequest(
pubkey: Bytes48 request.pubkey.blob,
withdrawalCredentials: Bytes32 request.withdrawal_credentials.data,
amount: distinctBase(request.amount),
signature: Bytes96 request.signature.blob,
index: request.index)

func append*(w: var RlpWriter, request: electra.WithdrawalRequest) =
w.append ExecutionWithdrawalRequest(
w.append EthWithdrawalRequest(
sourceAddress: Address request.source_address.data,
validatorPubkey: Bytes48 request.validator_pubkey.blob,
amount: distinctBase(request.amount))

func append*(w: var RlpWriter, request: electra.ConsolidationRequest) =
w.append ExecutionConsolidationRequest(
w.append EthConsolidationRequest(
sourceAddress: Address request.source_address.data,
sourcePubkey: Bytes48 request.source_pubkey.blob,
targetPubkey: Bytes48 request.target_pubkey.blob)

# https://eips.ethereum.org/EIPS/eip-7685
proc computeRequestsTrieRoot(
requests: electra.ExecutionRequests): ExecutionHash256 =
requests: electra.ExecutionRequests): EthHash32 =
let n =
requests.deposits.len +
requests.withdrawals.len +
Expand All @@ -494,7 +485,7 @@ proc computeRequestsTrieRoot(

b.rootHash()

proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
proc blockToBlockHeader*(blck: ForkyBeaconBlock): EthHeader =
template payload: auto = blck.body.execution_payload

static: # `GasInt` is signed. We only use it for hashing.
Expand All @@ -507,7 +498,7 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
when typeof(payload).kind >= ConsensusFork.Capella:
Opt.some orderedTrieRoot(payload.withdrawals.asSeq)
else:
Opt.none(ExecutionHash256)
Opt.none(EthHash32)
blobGasUsed =
when typeof(payload).kind >= ConsensusFork.Deneb:
Opt.some payload.blob_gas_used
Expand All @@ -520,16 +511,16 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
Opt.none(uint64)
parentBeaconBlockRoot =
when typeof(payload).kind >= ConsensusFork.Deneb:
Opt.some ExecutionHash256(blck.parent_root.data)
Opt.some EthHash32(blck.parent_root.data)
else:
Opt.none(ExecutionHash256)
Opt.none(EthHash32)
requestsRoot =
when typeof(payload).kind >= ConsensusFork.Electra:
Opt.some blck.body.execution_requests.computeRequestsTrieRoot()
else:
Opt.none(ExecutionHash256)
Opt.none(EthHash32)

ExecutionBlockHeader(
EthHeader(
parentHash : payload.parent_hash.to(Hash32),
ommersHash : EMPTY_UNCLE_HASH,
coinbase : EthAddress payload.fee_recipient.data,
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/spec/helpers_el.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import
"."/[helpers, state_transition_block]

func readExecutionTransaction(
txBytes: bellatrix.Transaction): Result[ExecutionTransaction, string] =
txBytes: bellatrix.Transaction): Result[EthTransaction, string] =
try:
ok rlp.decode(distinctBase(txBytes), ExecutionTransaction)
ok rlp.decode(distinctBase(txBytes), EthTransaction)
except RlpError as exc:
err("Invalid transaction: " & exc.msg)

Expand Down

0 comments on commit 658ff29

Please sign in to comment.