diff --git a/beacon_chain/el/el_manager.nim b/beacon_chain/el/el_manager.nim index 9068b03c6..ed57face0 100644 --- a/beacon_chain/el/el_manager.nim +++ b/beacon_chain/el/el_manager.nim @@ -356,9 +356,6 @@ func isSynced*(m: ELManager): bool = template eth1ChainBlocks*(m: ELManager): Deque[Eth1Block] = m.eth1Chain.blocks -template toGaugeValue(x: Quantity): int64 = - toGaugeValue(distinctBase x) - # TODO: Add cfg validation # MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH # doAssert SECONDS_PER_ETH1_BLOCK * cfg.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY, @@ -1514,10 +1511,10 @@ func earliestBlockOfInterest( (2 * m.cfg.ETH1_FOLLOW_DISTANCE) + votedBlocksSafetyMargin - if latestEth1BlockNumber > blocksOfInterestRange: + if latestEth1BlockNumber > blocksOfInterestRange.Eth1BlockNumber: latestEth1BlockNumber - blocksOfInterestRange else: - 0 + 0.Eth1BlockNumber proc syncBlockRange(m: ELManager, connection: ELConnection, @@ -1744,13 +1741,13 @@ proc syncEth1Chain(m: ELManager, connection: ELConnection) {.async.} = except CatchableError as err: warn "Failed to obtain the latest block from the EL", err = err.msg raise err - latestBlockNumber = Eth1BlockNumber(latestBlock.number) + latestBlockNumber = latestBlock.number m.syncTargetBlock = some( - if Eth1BlockNumber(latestBlock.number) > m.cfg.ETH1_FOLLOW_DISTANCE: - Eth1BlockNumber(latestBlock.number) - m.cfg.ETH1_FOLLOW_DISTANCE + if latestBlock.number > m.cfg.ETH1_FOLLOW_DISTANCE.Eth1BlockNumber: + latestBlock.number - m.cfg.ETH1_FOLLOW_DISTANCE else: - Eth1BlockNumber(0)) + 0.Eth1BlockNumber) if m.syncTargetBlock.get <= eth1SyncedTo: # The chain reorged to a lower height. # It's relatively safe to ignore that. @@ -1766,7 +1763,7 @@ proc syncEth1Chain(m: ELManager, connection: ELConnection) {.async.} = depositContract, eth1SyncedTo + 1, m.syncTargetBlock.get, - m.earliestBlockOfInterest(Eth1BlockNumber latestBlock.number)) + m.earliestBlockOfInterest(latestBlock.number)) eth1SyncedTo = m.syncTargetBlock.get eth1_synced_head.set eth1SyncedTo.toGaugeValue @@ -1858,4 +1855,4 @@ proc testWeb3Provider*(web3Url: Uri, ns = web3.contractSender(DepositContract, depositContractAddress) discard request "Deposit root": - ns.get_deposit_root.call(blockNumber = latestBlock.number.uint64) + ns.get_deposit_root.call(blockNumber = latestBlock.number) diff --git a/beacon_chain/el/eth1_chain.nim b/beacon_chain/el/eth1_chain.nim index 4238030e1..231ff3bc1 100644 --- a/beacon_chain/el/eth1_chain.nim +++ b/beacon_chain/el/eth1_chain.nim @@ -13,7 +13,7 @@ import ../beacon_chain_db, ../spec/[deposit_snapshots, digest, eth2_merkleization, forks, network], ../spec/datatypes/base, - web3/[primitives, eth_api_types], + web3/[conversions, eth_api_types], ./merkle_minimal export beacon_chain_db, deques, digest, base, forks @@ -30,8 +30,11 @@ declarePublicGauge eth1_finalized_deposits, declareGauge eth1_chain_len, "The length of the in-memory chain of Eth1 blocks" +template toGaugeValue*(x: Quantity | BlockNumber): int64 = + toGaugeValue(distinctBase x) + type - Eth1BlockNumber* = uint64 + Eth1BlockNumber* = BlockNumber Eth1BlockTimestamp* = uint64 Eth1BlockObj* = object @@ -169,7 +172,7 @@ proc pruneOldBlocks(chain: var Eth1Chain, depositIndex: uint64) = chain.db.putDepositContractSnapshot DepositContractSnapshot( eth1Block: lastBlock.hash, depositContractState: chain.finalizedDepositsMerkleizer.toDepositContractState, - blockHeight: lastBlock.number) + blockHeight: distinctBase(lastBlock.number)) eth1_finalized_head.set lastBlock.number.toGaugeValue eth1_finalized_deposits.set lastBlock.depositCount.toGaugeValue diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index dbfc6419e..93c6c59f8 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -29,8 +29,8 @@ export json_serialization, net, sets, rest_types, slashing_protection_common, jsonSerializationResults, rest_keymanager_types -from web3/primitives import BlockHash -export primitives.BlockHash +from web3/primitives import BlockHash, BlockNumber +export primitives.BlockHash, primitives.BlockNumber func decodeMediaType*( contentType: Opt[ContentTypeData]): Result[MediaType, string] = @@ -959,6 +959,16 @@ proc readValue*(reader: var JsonReader[RestJson], value: var uint8) {. else: reader.raiseUnexpectedValue($res.error() & ": " & svalue) +## BlockNumber +proc writeValue*( + w: var JsonWriter[RestJson], value: BlockNumber) {.raises: [IOError].} = + w.writeValue(distinctBase(value)) + +proc readValue*( + reader: var JsonReader[RestJson], + value: var BlockNumber) {.raises: [IOError, SerializationError].} = + reader.readValue(distinctBase(value)) + ## RestNumeric proc writeValue*(w: var JsonWriter[RestJson], value: RestNumeric) {.raises: [IOError].} = diff --git a/research/fakeee.nim b/research/fakeee.nim index 159774ee1..55f6a2842 100644 --- a/research/fakeee.nim +++ b/research/fakeee.nim @@ -81,7 +81,7 @@ proc setupEngineAPI*(server: RpcServer) = info "eth_getBlockByNumber", quantityTag, fullTransactions return if quantityTag == "latest": - JrpcConv.encode(BlockObject(number: 1000.Quantity)).JsonString + JrpcConv.encode(BlockObject(number: 1000.BlockNumber)).JsonString else: "{}".JsonString @@ -89,7 +89,7 @@ proc setupEngineAPI*(server: RpcServer) = data: string, fullTransactions: bool) -> BlockObject: info "eth_getBlockByHash", data = toHex(data), fullTransactions - return BlockObject(number: 1000.Quantity) + return BlockObject(number: 1000.BlockNumber) server.rpc("eth_chainId") do() -> Quantity: info "eth_chainId" diff --git a/vendor/nim-web3 b/vendor/nim-web3 index 747001250..80c7aa6de 160000 --- a/vendor/nim-web3 +++ b/vendor/nim-web3 @@ -1 +1 @@ -Subproject commit 747001250a27278e0f0f1706fe1dec88ad3ede8d +Subproject commit 80c7aa6de2a26c57fa1f06ad47f3ac6058e6545b