diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index 022b012ea..ba532fe2b 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -180,6 +180,13 @@ proc setAsCanonicalChainHead(self: BaseChainDB; headerHash: Hash256): seq[BlockH return newCanonicalHeaders +proc setHead*(self: BaseChainDB, header: BlockHeader, writeHeader = false) = + var headerHash = rlpHash(header) + if writeHeader: + self.db.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header)) + self.addBlockNumberToHashLookup(header) + self.db.put(canonicalHeadHashKey().toOpenArray, rlp.encode(headerHash)) + proc headerExists*(self: BaseChainDB; blockHash: Hash256): bool = ## Returns True if the header with the given block hash is in our DB. self.db.contains(genericHashKey(blockHash).toOpenArray) diff --git a/nimbus/rpc/debug.nim b/nimbus/rpc/debug.nim index 48d004dd2..13d443e7b 100644 --- a/nimbus/rpc/debug.nim +++ b/nimbus/rpc/debug.nim @@ -113,3 +113,11 @@ proc setupDebugRpc*(chainDB: BaseChainDB, rpcsrv: RpcServer) = flags = traceOptionsToFlags(options) traceBlock(chainDB, header, body, flags) + + rpcsrv.rpc("debug_setHead") do(quantityTag: string): + ## Sets the current head of the local chain by block number. + ## Note, this is a destructive action and may severely damage your chain. + ## Use with extreme caution. + let + header = chainDB.headerFromTag(quantityTag) + chainDB.setHead(header) diff --git a/tests/persistBlockTestGen.nim b/tests/persistBlockTestGen.nim index f516b0a68..fd2075cf7 100644 --- a/tests/persistBlockTestGen.nim +++ b/tests/persistBlockTestGen.nim @@ -4,12 +4,6 @@ import ../nimbus/[tracer, vm_types, config], ../nimbus/p2p/chain -proc putCanonicalHead(chainDB: BaseChainDB, header: BlockHeader) = - var headerHash = rlpHash(header) - chainDB.db.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header)) - chainDB.addBlockNumberToHashLookup(header) - chainDB.db.put(canonicalHeadHashKey().toOpenArray, rlp.encode(headerHash)) - proc dumpTest(chainDB: BaseChainDB, blockNumber: int) = let blockNumber = blockNumber.u256 @@ -30,7 +24,7 @@ proc dumpTest(chainDB: BaseChainDB, blockNumber: int) = headers = @[header] bodies = @[blockBody] - captureChainDB.putCanonicalHead(parent) + captureChainDB.setHead(parent, true) discard chain.persistBlocks(headers, bodies) var metaData = %{ diff --git a/tests/test_persistblock_json.nim b/tests/test_persistblock_json.nim index 4aa8be7cd..31938050d 100644 --- a/tests/test_persistblock_json.nim +++ b/tests/test_persistblock_json.nim @@ -16,12 +16,6 @@ proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) suite "persist block json tests": jsonTest("PersistBlockTests", testFixture) -proc putCanonicalHead(chainDB: BaseChainDB, header: BlockHeader) = - var headerHash = rlpHash(header) - chainDB.db.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header)) - chainDB.addBlockNumberToHashLookup(header) - chainDB.db.put(canonicalHeadHashKey().toOpenArray, rlp.encode(headerHash)) - # use tracerTestGen.nim to generate additional test data proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) = var @@ -45,6 +39,6 @@ proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) = headers = @[header] bodies = @[blockBody] - chainDB.putCanonicalHead(parent) + chainDB.setHead(parent, true) let validationResult = chain.persistBlocks(headers, bodies) check validationResult == ValidationResult.OK