From 151d116f02985810faf0ba7c0e28fad083e5d9fe Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 25 Nov 2020 01:51:17 +0200 Subject: [PATCH] Fix off-by-one error in Eth1 pruning --- beacon_chain/eth1_monitor.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/beacon_chain/eth1_monitor.nim b/beacon_chain/eth1_monitor.nim index 2d6fdaef3..e5f74cc4d 100644 --- a/beacon_chain/eth1_monitor.nim +++ b/beacon_chain/eth1_monitor.nim @@ -177,9 +177,6 @@ template asBlockHash(x: Eth2Digest): BlockHash = func shortLog*(b: Eth1Block): string = &"{b.number}:{shortLog b.voteData.block_hash}(deposits = {b.voteData.deposit_count})" -template findBlock*(eth1Chain: Eth1Chain, hash: BlockHash): Eth1Block = - eth1Chain.blocksByHash.getOrDefault(hash, nil) - template findBlock*(eth1Chain: Eth1Chain, eth1Data: Eth1Data): Eth1Block = getOrDefault(eth1Chain.blocksByHash, asBlockHash(eth1Data.block_hash), nil) @@ -343,6 +340,8 @@ proc onBlockHeaders*(p: Web3DataProviderRef, p.blockHeadersSubscription = await p.web3.subscribeForBlockHeaders( blockHeaderHandler, errorHandler) +{.push raises: [Defect].} + func getDepositsRoot(m: DepositsMerkleizer): Eth2Digest = mixInLength(m.getFinalHash, int m.totalChunks) @@ -371,13 +370,13 @@ func eth1DataFromMerkleizer(eth1Block: Eth2Digest, deposit_count: merkleizer.getChunkCount, deposit_root: merkleizer.getDepositsRoot) -proc pruneOldBlocks(m: Eth1Monitor, depositIndex: uint64) {.raises: [Defect].} = +proc pruneOldBlocks(m: Eth1Monitor, depositIndex: uint64) = let initialChunks = m.eth2FinalizedDepositsMerkleizer.getChunkCount var lastBlock: Eth1Block while m.eth1Chain.blocks.len > 0: let blk = m.eth1Chain.blocks.peekFirst - if blk.voteData.deposit_count > depositIndex: + if blk.voteData.deposit_count >= depositIndex: break else: for deposit in blk.deposits: @@ -443,6 +442,7 @@ proc getBlockProposalData*(m: Eth1Monitor, let finalizedEth1Block = m.eth1Chain.findBlock(finalizedEth1Data) let hasLatestDeposits = if finalizedEth1Block != nil: if finalizedEth1Block.voteData.deposit_root == finalizedEth1Data.deposit_root: + finalizedEth1Block.voteDataVerified = true true else: error "Corrupted deposits history detected", @@ -524,6 +524,8 @@ proc getBlockProposalData*(m: Eth1Monitor, else: result.hasMissingDeposits = true +{.pop.} + proc new(T: type Web3DataProvider, depositContractAddress: Eth1Address, web3Url: string): Future[Result[Web3DataProviderRef, string]] {.async.} =