2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-03-10 22:16:42 +00:00
|
|
|
import
|
2023-08-04 11:10:09 +00:00
|
|
|
std/options,
|
2023-03-10 22:16:42 +00:00
|
|
|
chronos,
|
|
|
|
stint,
|
|
|
|
eth/common,
|
2023-08-04 11:10:09 +00:00
|
|
|
../../db/core_db
|
2023-03-10 22:16:42 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
AsyncDataSource* = ref object of RootObj
|
2023-08-04 11:10:09 +00:00
|
|
|
ifNecessaryGetSlots*: proc(db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, slots: seq[UInt256], newStateRootForSanityChecking: Hash256): Future[void] {.gcsafe.}
|
|
|
|
ifNecessaryGetCode*: proc(db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, newStateRootForSanityChecking: Hash256): Future[void] {.gcsafe.}
|
|
|
|
ifNecessaryGetAccount*: proc(db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, newStateRootForSanityChecking: Hash256): Future[void] {.gcsafe.}
|
|
|
|
ifNecessaryGetBlockHeaderByNumber*: proc(coreDb: CoreDbRef, blockNumber: BlockNumber): Future[void] {.gcsafe.}
|
2023-04-24 20:59:38 +00:00
|
|
|
# FIXME-Adam: Later.
|
|
|
|
#fetchNodes*: proc(stateRoot: Hash256, paths: seq[seq[seq[byte]]], nodeHashes: seq[Hash256]): Future[seq[seq[byte]]] {.gcsafe.}
|
2023-03-10 22:16:42 +00:00
|
|
|
fetchBlockHeaderWithHash*: proc(h: Hash256): Future[BlockHeader] {.gcsafe.}
|
|
|
|
fetchBlockHeaderWithNumber*: proc(n: BlockNumber): Future[BlockHeader] {.gcsafe.}
|
|
|
|
fetchBlockHeaderAndBodyWithHash*: proc(h: Hash256): Future[(BlockHeader, BlockBody)] {.gcsafe.}
|
|
|
|
fetchBlockHeaderAndBodyWithNumber*: proc(n: BlockNumber): Future[(BlockHeader, BlockBody)] {.gcsafe.}
|
|
|
|
|
|
|
|
# FIXME-Adam: maybe rename this?
|
|
|
|
AsyncOperationFactory* = ref object of RootObj
|
|
|
|
maybeDataSource*: Option[AsyncDataSource]
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME-Adam: Can I make a singleton?
|
|
|
|
proc asyncFactoryWithNoDataSource*(): AsyncOperationFactory =
|
|
|
|
AsyncOperationFactory(maybeDataSource: none[AsyncDataSource]())
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME-Adam: Ugly but straightforward; can this be cleaned up using some combination of:
|
|
|
|
# - an ifSome/map operation on Options
|
|
|
|
# - some kind of "what are we fetching" tuple, so that this is just one thing
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc ifNecessaryGetSlots*(asyncFactory: AsyncOperationFactory, db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, slots: seq[UInt256], newStateRootForSanityChecking: Hash256): Future[void] {.async.} =
|
2023-03-10 22:16:42 +00:00
|
|
|
if asyncFactory.maybeDataSource.isSome:
|
|
|
|
await asyncFactory.maybeDataSource.get.ifNecessaryGetSlots(db, blockNumber, stateRoot, address, slots, newStateRootForSanityChecking)
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc ifNecessaryGetCode*(asyncFactory: AsyncOperationFactory, db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, newStateRootForSanityChecking: Hash256): Future[void] {.async.} =
|
2023-03-10 22:16:42 +00:00
|
|
|
if asyncFactory.maybeDataSource.isSome:
|
|
|
|
await asyncFactory.maybeDataSource.get.ifNecessaryGetCode(db, blockNumber, stateRoot, address, newStateRootForSanityChecking)
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc ifNecessaryGetAccount*(asyncFactory: AsyncOperationFactory, db: CoreDbRef, blockNumber: BlockNumber, stateRoot: Hash256, address: EthAddress, newStateRootForSanityChecking: Hash256): Future[void] {.async.} =
|
2023-03-10 22:16:42 +00:00
|
|
|
if asyncFactory.maybeDataSource.isSome:
|
|
|
|
await asyncFactory.maybeDataSource.get.ifNecessaryGetAccount(db, blockNumber, stateRoot, address, newStateRootForSanityChecking)
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc ifNecessaryGetBlockHeaderByNumber*(asyncFactory: AsyncOperationFactory, coreDb: CoreDbRef, blockNumber: BlockNumber): Future[void] {.async.} =
|
2023-03-10 22:16:42 +00:00
|
|
|
if asyncFactory.maybeDataSource.isSome:
|
2023-08-04 11:10:09 +00:00
|
|
|
await asyncFactory.maybeDataSource.get.ifNecessaryGetBlockHeaderByNumber(coreDb, blockNumber)
|