parent
f1d7d2d62b
commit
293ce28e4d
|
@ -1,45 +0,0 @@
|
||||||
# Nimbus
|
|
||||||
# Copyright (c) 2020-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.
|
|
||||||
|
|
||||||
import eth/[rlp, common], core_db
|
|
||||||
|
|
||||||
const
|
|
||||||
headerPrefix = 'h'.byte # headerPrefix + num (uint64 big endian) + hash -> header
|
|
||||||
headerHashSuffix = 'n'.byte # headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
|
|
||||||
blockBodyPrefix = 'b'.byte # blockBodyPrefix + num (uint64 big endian) + hash -> block body
|
|
||||||
|
|
||||||
proc headerHash*(db: CoreDbRef, number: uint64): Hash256 =
|
|
||||||
var key: array[10, byte]
|
|
||||||
key[0] = headerPrefix
|
|
||||||
key[1..8] = toBytesBE(number)[0..^1]
|
|
||||||
key[^1] = headerHashSuffix
|
|
||||||
let res = db.kvt.get(key)
|
|
||||||
doAssert(res.len == 32)
|
|
||||||
result.data[0..31] = res[0..31]
|
|
||||||
|
|
||||||
proc blockHeader*(db: CoreDbRef, hash: Hash256, number: uint64): BlockHeader =
|
|
||||||
var key: array[41, byte]
|
|
||||||
key[0] = headerPrefix
|
|
||||||
key[1..8] = toBytesBE(number)[0..^1]
|
|
||||||
key[9..40] = hash.data[0..^1]
|
|
||||||
let res = db.kvt.get(key)
|
|
||||||
result = rlp.decode(res, BlockHeader)
|
|
||||||
|
|
||||||
proc blockHeader*(db: CoreDbRef, number: uint64): BlockHeader =
|
|
||||||
let hash = db.headerHash(number)
|
|
||||||
db.blockHeader(hash, number)
|
|
||||||
|
|
||||||
proc blockBody*(db: CoreDbRef, hash: Hash256, number: uint64): BlockBody =
|
|
||||||
var key: array[41, byte]
|
|
||||||
key[0] = blockBodyPrefix
|
|
||||||
key[1..8] = toBytesBE(number)[0..^1]
|
|
||||||
key[9..40] = hash.data[0..^1]
|
|
||||||
let res = db.kvt.get(key)
|
|
||||||
result = rlp.decode(res, BlockBody)
|
|
|
@ -55,16 +55,10 @@ proc ifNodesExistGetAccount*(trie: AccountsTrie, address: EthAddress): Option[Ac
|
||||||
ifNodesExistGetAccountBytes(trie, address).map(accountFromBytes)
|
ifNodesExistGetAccountBytes(trie, address).map(accountFromBytes)
|
||||||
|
|
||||||
proc maybeGetCode*(db: CoreDbRef, codeHash: Hash256): Option[seq[byte]] =
|
proc maybeGetCode*(db: CoreDbRef, codeHash: Hash256): Option[seq[byte]] =
|
||||||
when defined(geth):
|
if db.isLegacy:
|
||||||
if db.isLegacy:
|
db.newKvt.backend.toLegacy.maybeGet(contractHashKey(codeHash).toOpenArray)
|
||||||
db.newKvt.backend.toLegacy.maybeGet(codeHash.data)
|
|
||||||
else:
|
|
||||||
db.kvt.get(codeHash.data)
|
|
||||||
else:
|
else:
|
||||||
if db.isLegacy:
|
some(db.kvt.get(contractHashKey(codeHash).toOpenArray))
|
||||||
db.newKvt.backend.toLegacy.maybeGet(contractHashKey(codeHash).toOpenArray)
|
|
||||||
else:
|
|
||||||
some(db.kvt.get(contractHashKey(codeHash).toOpenArray))
|
|
||||||
|
|
||||||
proc maybeGetCode*(trie: AccountsTrie, address: EthAddress): Option[seq[byte]] =
|
proc maybeGetCode*(trie: AccountsTrie, address: EthAddress): Option[seq[byte]] =
|
||||||
let maybeAcc = trie.ifNodesExistGetAccount(address)
|
let maybeAcc = trie.ifNodesExistGetAccount(address)
|
||||||
|
|
|
@ -303,10 +303,7 @@ proc persistMode(acc: RefAccount): PersistMode =
|
||||||
|
|
||||||
proc persistCode(acc: RefAccount, db: CoreDbRef) =
|
proc persistCode(acc: RefAccount, db: CoreDbRef) =
|
||||||
if acc.code.len != 0:
|
if acc.code.len != 0:
|
||||||
when defined(geth):
|
db.kvt.put(contractHashKey(acc.account.codeHash).toOpenArray, acc.code)
|
||||||
db.kvt.put(acc.account.codeHash.data, acc.code)
|
|
||||||
else:
|
|
||||||
db.kvt.put(contractHashKey(acc.account.codeHash).toOpenArray, acc.code)
|
|
||||||
|
|
||||||
proc persistStorage(acc: RefAccount, db: CoreDbRef, clearCache: bool) =
|
proc persistStorage(acc: RefAccount, db: CoreDbRef, clearCache: bool) =
|
||||||
if acc.overlayStorage.len == 0:
|
if acc.overlayStorage.len == 0:
|
||||||
|
@ -385,10 +382,7 @@ proc getCode*(ac: AccountsCache, address: EthAddress): seq[byte] =
|
||||||
if CodeLoaded in acc.flags or CodeChanged in acc.flags:
|
if CodeLoaded in acc.flags or CodeChanged in acc.flags:
|
||||||
result = acc.code
|
result = acc.code
|
||||||
else:
|
else:
|
||||||
when defined(geth):
|
let data = ac.kvt.get(contractHashKey(acc.account.codeHash).toOpenArray)
|
||||||
let data = ac.kvt.get(acc.account.codeHash.data)
|
|
||||||
else:
|
|
||||||
let data = ac.kvt.get(contractHashKey(acc.account.codeHash).toOpenArray)
|
|
||||||
|
|
||||||
acc.code = data
|
acc.code = data
|
||||||
acc.flags.incl CodeLoaded
|
acc.flags.incl CodeLoaded
|
||||||
|
|
|
@ -322,12 +322,8 @@ proc persistMode(acc: AccountRef): PersistMode =
|
||||||
|
|
||||||
proc persistCode(acc: AccountRef, ac: AccountsLedgerRef) =
|
proc persistCode(acc: AccountRef, ac: AccountsLedgerRef) =
|
||||||
if acc.code.len != 0:
|
if acc.code.len != 0:
|
||||||
when defined(geth):
|
let rc = ac.kvt.put(
|
||||||
let rc = ac.kvt.put(
|
contractHashKey(acc.statement.codeHash).toOpenArray, acc.code)
|
||||||
acc.statement.codeHash.data, acc.code)
|
|
||||||
else:
|
|
||||||
let rc = ac.kvt.put(
|
|
||||||
contractHashKey(acc.statement.codeHash).toOpenArray, acc.code)
|
|
||||||
if rc.isErr:
|
if rc.isErr:
|
||||||
warn logTxt "persistCode()",
|
warn logTxt "persistCode()",
|
||||||
codeHash=acc.statement.codeHash, error=($$rc.error)
|
codeHash=acc.statement.codeHash, error=($$rc.error)
|
||||||
|
@ -410,11 +406,7 @@ proc getCode*(ac: AccountsLedgerRef, address: EthAddress): seq[byte] =
|
||||||
if CodeLoaded in acc.flags or CodeChanged in acc.flags:
|
if CodeLoaded in acc.flags or CodeChanged in acc.flags:
|
||||||
result = acc.code
|
result = acc.code
|
||||||
else:
|
else:
|
||||||
let rc = block:
|
let rc = ac.kvt.get(contractHashKey(acc.statement.codeHash).toOpenArray)
|
||||||
when defined(geth):
|
|
||||||
ac.kvt.get(acc.statement.codeHash.data)
|
|
||||||
else:
|
|
||||||
ac.kvt.get(contractHashKey(acc.statement.codeHash).toOpenArray)
|
|
||||||
if rc.isErr:
|
if rc.isErr:
|
||||||
warn logTxt "getCode()", codeHash=acc.statement.codeHash, error=($$rc.error)
|
warn logTxt "getCode()", codeHash=acc.statement.codeHash, error=($$rc.error)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -235,35 +235,29 @@ proc statelessInit*(
|
||||||
asyncFactory = asyncFactory)
|
asyncFactory = asyncFactory)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
method coinbase*(vmState: BaseVMState): EthAddress {.base, gcsafe.} =
|
proc coinbase*(vmState: BaseVMState): EthAddress =
|
||||||
vmState.blockCtx.coinbase
|
vmState.blockCtx.coinbase
|
||||||
|
|
||||||
method blockNumber*(vmState: BaseVMState): BlockNumber {.base, gcsafe.} =
|
proc blockNumber*(vmState: BaseVMState): BlockNumber =
|
||||||
# it should return current block number
|
# it should return current block number
|
||||||
# and not head.blockNumber
|
# and not head.blockNumber
|
||||||
vmState.parent.blockNumber + 1
|
vmState.parent.blockNumber + 1
|
||||||
|
|
||||||
method difficultyOrPrevRandao*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
proc difficultyOrPrevRandao*(vmState: BaseVMState): UInt256 =
|
||||||
if vmState.com.consensus == ConsensusType.POS:
|
if vmState.com.consensus == ConsensusType.POS:
|
||||||
# EIP-4399/EIP-3675
|
# EIP-4399/EIP-3675
|
||||||
UInt256.fromBytesBE(vmState.blockCtx.prevRandao.data)
|
UInt256.fromBytesBE(vmState.blockCtx.prevRandao.data)
|
||||||
else:
|
else:
|
||||||
vmState.blockCtx.difficulty
|
vmState.blockCtx.difficulty
|
||||||
|
|
||||||
method baseFee*(vmState: BaseVMState): UInt256 {.base, gcsafe.} =
|
proc baseFee*(vmState: BaseVMState): UInt256 =
|
||||||
vmState.blockCtx.fee.get(0.u256)
|
vmState.blockCtx.fee.get(0.u256)
|
||||||
|
|
||||||
when defined(geth):
|
|
||||||
import db/geth_db
|
|
||||||
|
|
||||||
method getAncestorHash*(
|
method getAncestorHash*(
|
||||||
vmState: BaseVMState, blockNumber: BlockNumber):
|
vmState: BaseVMState, blockNumber: BlockNumber):
|
||||||
Hash256 {.base, gcsafe, raises: [CatchableError].} =
|
Hash256 {.base, gcsafe, raises: [CatchableError].} =
|
||||||
let db = vmState.com.db
|
let db = vmState.com.db
|
||||||
when defined(geth):
|
db.getBlockHash(blockNumber)
|
||||||
result = db.headerHash(blockNumber.truncate(uint64))
|
|
||||||
else:
|
|
||||||
result = db.getBlockHash(blockNumber)
|
|
||||||
|
|
||||||
proc readOnlyStateDB*(vmState: BaseVMState): ReadOnlyStateDB {.inline.} =
|
proc readOnlyStateDB*(vmState: BaseVMState): ReadOnlyStateDB {.inline.} =
|
||||||
ReadOnlyStateDB(vmState.stateDB)
|
ReadOnlyStateDB(vmState.stateDB)
|
||||||
|
|
|
@ -20,15 +20,8 @@ import
|
||||||
results,
|
results,
|
||||||
./beacon/web3_eth_conv
|
./beacon/web3_eth_conv
|
||||||
|
|
||||||
when defined(geth):
|
proc getParentHeader(self: CoreDbRef, header: BlockHeader): BlockHeader =
|
||||||
import db/geth_db
|
self.getBlockHeader(header.parentHash)
|
||||||
|
|
||||||
proc getParentHeader(db: CoreDbRef, header: BlockHeader): BlockHeader =
|
|
||||||
db.blockHeader(header.blockNumber.truncate(uint64) - 1)
|
|
||||||
|
|
||||||
else:
|
|
||||||
proc getParentHeader(self: CoreDbRef, header: BlockHeader): BlockHeader =
|
|
||||||
self.getBlockHeader(header.parentHash)
|
|
||||||
|
|
||||||
type
|
type
|
||||||
SaveCtxEnv = object
|
SaveCtxEnv = object
|
||||||
|
|
Loading…
Reference in New Issue