Skip to content

Commit

Permalink
Fix vmflags for witness generation 1934. (#1961)
Browse files Browse the repository at this point in the history
* Fix issue causing vmflags to be reset during call to processBlocks and enable witness generation in test_blockchain_json test.

* Fix copyright notice on updated files.
  • Loading branch information
web3-developer committed Jan 9, 2024
1 parent 84e7c68 commit e60e580
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 13 additions & 5 deletions nimbus/evm/state.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http:https://www.apache.org/licenses/LICENSE-2.0)
Expand Down Expand Up @@ -27,7 +27,8 @@ proc init(
blockCtx: BlockContext;
com: CommonRef;
tracer: TracerRef,
asyncFactory: AsyncOperationFactory = AsyncOperationFactory(maybeDataSource: none[AsyncDataSource]()))
asyncFactory: AsyncOperationFactory = AsyncOperationFactory(maybeDataSource: none[AsyncDataSource]()),
flags: set[VMFlag] = self.flags)
{.gcsafe.} =
## Initialisation helper
self.parent = parent
Expand All @@ -37,6 +38,7 @@ proc init(
self.tracer = tracer
self.stateDB = ac
self.asyncFactory = asyncFactory
self.flags = flags

func blockCtx(com: CommonRef, header: BlockHeader):
BlockContext {.gcsafe, raises: [CatchableError].} =
Expand Down Expand Up @@ -102,13 +104,15 @@ proc reinit*(self: BaseVMState; ## Object descriptor
db = com.db
ac = if self.stateDB.rootHash == parent.stateRoot: self.stateDB
else: com.ledgerType.init(db, parent.stateRoot, com.pruneTrie)
flags = self.flags
self[].reset
self.init(
ac = ac,
parent = parent,
blockCtx = blockCtx,
com = com,
tracer = tracer)
tracer = tracer,
flags = flags)
return true
# else: false

Expand Down Expand Up @@ -283,15 +287,19 @@ proc generateWitness*(vmState: BaseVMState): bool =
GenerateWitness in vmState.flags

proc `generateWitness=`*(vmState: BaseVMState, status: bool) =
if status: vmState.flags.incl GenerateWitness
else: vmState.flags.excl GenerateWitness
if status: vmState.flags.incl GenerateWitness
else: vmState.flags.excl GenerateWitness

proc buildWitness*(vmState: BaseVMState): seq[byte]
{.raises: [CatchableError].} =
let rootHash = vmState.stateDB.rootHash
let mkeys = vmState.stateDB.makeMultiKeys()
let flags = if vmState.fork >= FkSpurious: {wfEIP170} else: {}

# A valid block having no transactions should return an empty witness
if mkeys.keys.len() == 0:
return @[]

# build witness from tree
var wb = initWitnessBuilder(vmState.com.db, rootHash, flags)
wb.buildWitness(mkeys)
Expand Down
12 changes: 9 additions & 3 deletions tests/test_blockchain_json.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http:https://www.apache.org/licenses/LICENSE-2.0)
Expand Down Expand Up @@ -176,6 +176,12 @@ proc parseTestCtx(fixture: JsonNode, testStatusIMPL: var TestStatus): TestCtx =
proc blockWitness(vmState: BaseVMState, chainDB: CoreDbRef) =
let rootHash = vmState.stateDB.rootHash
let witness = vmState.buildWitness()

if witness.len() == 0:
if vmState.stateDB.makeMultiKeys().keys.len() != 0:
raise newException(ValidationError, "Invalid trie generated from block witness")
return

let fork = vmState.fork
let flags = if fork >= FKSpurious: {wfEIP170} else: {}

Expand Down Expand Up @@ -221,6 +227,7 @@ proc importBlock(ctx: var TestCtx, com: CommonRef,
com,
tracerInst,
)
ctx.vmState.generateWitness = true # Enable saving witness data

let
chain = newChain(com, extraValidation = true, ctx.vmState)
Expand All @@ -229,8 +236,7 @@ proc importBlock(ctx: var TestCtx, com: CommonRef,
if res == ValidationResult.Error:
raise newException(ValidationError, "persistBlocks validation")
else:
if ctx.vmState.generateWitness():
blockWitness(ctx.vmState, com.db)
blockWitness(chain.vmState, com.db)

proc applyFixtureBlockToChain(ctx: var TestCtx, tb: var TestBlock,
com: CommonRef, checkSeal: bool) =
Expand Down

0 comments on commit e60e580

Please sign in to comment.