2021-05-11 07:37:33 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
import
|
2021-09-07 14:00:01 +00:00
|
|
|
../common/[eth_types, state_accessors]
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
# TODO: Perhaps we can move this to eth-common
|
|
|
|
|
2021-05-06 15:20:54 +00:00
|
|
|
proc getBlockHeaders*(db: AbstractChainDB, req: BlocksRequest): seq[BlockHeader]
|
2021-05-11 15:32:47 +00:00
|
|
|
{.gcsafe, raises: [RlpError, Defect].} =
|
2019-02-05 15:40:29 +00:00
|
|
|
result = newSeqOfCap[BlockHeader](req.maxResults)
|
|
|
|
|
|
|
|
var foundBlock: BlockHeader
|
|
|
|
if db.getBlockHeader(req.startBlock, foundBlock):
|
|
|
|
result.add foundBlock
|
2019-07-09 15:06:20 +00:00
|
|
|
|
|
|
|
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
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
template fetcher*(fetcherName, fetchingFunc, InputType, ResultType: untyped) =
|
2019-03-13 01:37:04 +00:00
|
|
|
proc fetcherName*(db: AbstractChainDB,
|
2019-02-05 15:40:29 +00:00
|
|
|
lookups: openarray[InputType]): seq[ResultType] {.gcsafe.} =
|
|
|
|
for lookup in lookups:
|
|
|
|
let fetched = fetchingFunc(db, lookup)
|
|
|
|
if fetched.hasData:
|
|
|
|
# TODO: should there be an else clause here.
|
|
|
|
# Is the peer responsible of figuring out that
|
|
|
|
# some of the requested items were not found?
|
|
|
|
result.add deref(fetched)
|
|
|
|
|
|
|
|
fetcher getContractCodes, getContractCode, ContractCodeRequest, Blob
|
|
|
|
fetcher getBlockBodies, getBlockBody, KeccakHash, BlockBody
|
|
|
|
fetcher getStorageNodes, getStorageNode, KeccakHash, Blob
|
|
|
|
fetcher getReceipts, getReceipt, KeccakHash, Receipt
|
|
|
|
fetcher getProofs, getProof, ProofRequest, Blob
|
|
|
|
fetcher getHeaderProofs, getHeaderProof, ProofRequest, Blob
|
|
|
|
|
2019-03-13 01:37:04 +00:00
|
|
|
proc getHelperTrieProofs*(db: AbstractChainDB,
|
2019-02-05 15:40:29 +00:00
|
|
|
reqs: openarray[HelperTrieProofRequest],
|
|
|
|
outNodes: var seq[Blob], outAuxData: var seq[Blob]) =
|
|
|
|
discard
|