diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index 6fbde9f..fc4edf4 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -180,6 +180,12 @@ when BlockNumber is int64: template toBlockNumber*(n: SomeInteger): BlockNumber = int64(n) + template toBlockNumber*(n: UInt256): BlockNumber = + n.toInt + + template toInt*(n: BlockNumber): int = + int(n) + else: template vmWordToBlockNumber*(word: VMWord): BlockNumber = word @@ -190,6 +196,12 @@ else: template toBlockNumber*(n: SomeInteger): BlockNumber = u256(n) + template toBlockNumber*(n: UInt256): BlockNumber = + n + + template u256*(n: BlockNumber): UInt256 = + n + proc toBlockNonce*(n: uint64): BlockNonce = bigEndian64(addr result[0], unsafeAddr n) @@ -303,7 +315,7 @@ proc read*(rlp: var Rlp, T: typedesc[HashOrNum]): T = if rlp.blobLen == 32: result = HashOrNum(isHash: true, hash: rlp.read(Hash256)) else: - result = HashOrNum(isHash: false, number: rlp.read(UInt256)) + result = HashOrNum(isHash: false, number: rlp.read(BlockNumber)) proc append*(rlpWriter: var RlpWriter, t: Time) {.inline.} = rlpWriter.append(t.toUnix()) diff --git a/eth/p2p/blockchain_sync.nim b/eth/p2p/blockchain_sync.nim index 361b827..9ae4dae 100644 --- a/eth/p2p/blockchain_sync.nim +++ b/eth/p2p/blockchain_sync.nim @@ -40,7 +40,7 @@ proc hash*(p: Peer): Hash {.inline.} = hash(cast[pointer](p)) proc endIndex(b: WantedBlocks): BlockNumber = result = b.startIndex - result += (b.numBlocks - 1).u256 + result += (b.numBlocks - 1).toBlockNumber proc availableWorkItem(ctx: SyncContext): int = var maxPendingBlock = ctx.finalizedBlock diff --git a/eth/p2p/blockchain_utils.nim b/eth/p2p/blockchain_utils.nim index 23f5c89..024d609 100644 --- a/eth/p2p/blockchain_utils.nim +++ b/eth/p2p/blockchain_utils.nim @@ -10,16 +10,15 @@ proc getBlockHeaders*(db: AbstractChainDB, var foundBlock: BlockHeader if db.getBlockHeader(req.startBlock, foundBlock): result.add foundBlock - # Quick sanity check for lower bounds, code should be safe though without. - if not req.reverse or (foundBlock.blockNumber >= (req.skip + 1).toBlockNumber): - while uint64(result.len) < req.maxResults: - if not req.reverse: - if not db.getSuccessorHeader(foundBlock, foundBlock, req.skip): - break - else: - if not db.getAncestorHeader(foundBlock, foundBlock, req.skip): - break - result.add foundBlock + + while uint64(result.len) < req.maxResults: + if not req.reverse: + if not db.getSuccessorHeader(foundBlock, foundBlock, req.skip): + break + else: + if not db.getAncestorHeader(foundBlock, foundBlock, req.skip): + break + result.add foundBlock template fetcher*(fetcherName, fetchingFunc, InputType, ResultType: untyped) =