2018-12-25 12:10:04 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 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
|
2020-04-15 11:09:49 +00:00
|
|
|
unittest2, json, os, tables, strutils,
|
2019-07-07 10:12:01 +00:00
|
|
|
eth/[common, rlp], stew/byteutils, eth/trie/db,
|
2019-11-13 14:49:39 +00:00
|
|
|
./test_helpers, ../nimbus/db/db_chain,
|
2021-09-07 15:00:21 +00:00
|
|
|
../nimbus/p2p/chain, ../nimbus/config
|
2018-12-25 12:10:04 +00:00
|
|
|
|
|
|
|
# use tracerTestGen.nim to generate additional test data
|
|
|
|
proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus) =
|
|
|
|
var
|
|
|
|
blockNumber = UInt256.fromHex(node["blockNumber"].getStr())
|
|
|
|
memoryDB = newMemoryDB()
|
|
|
|
chainDB = newBaseChainDB(memoryDB, false)
|
|
|
|
state = node["state"]
|
|
|
|
|
|
|
|
for k, v in state:
|
|
|
|
let key = hexToSeqByte(k)
|
|
|
|
let value = hexToSeqByte(v.getStr())
|
|
|
|
memoryDB.put(key, value)
|
|
|
|
|
|
|
|
let
|
|
|
|
parentNumber = blockNumber - 1
|
|
|
|
parent = chainDB.getBlockHeader(parentNumber)
|
|
|
|
header = chainDB.getBlockHeader(blockNumber)
|
|
|
|
headerHash = header.blockHash
|
|
|
|
blockBody = chainDB.getBlockBody(headerHash)
|
|
|
|
chain = newChain(chainDB)
|
|
|
|
headers = @[header]
|
|
|
|
bodies = @[blockBody]
|
|
|
|
|
2022-07-04 05:35:56 +00:00
|
|
|
# it's ok if setHead fails here because of missing ancestors
|
|
|
|
discard chainDB.setHead(parent, true)
|
2018-12-25 12:10:04 +00:00
|
|
|
let validationResult = chain.persistBlocks(headers, bodies)
|
|
|
|
check validationResult == ValidationResult.OK
|
2019-05-30 16:42:55 +00:00
|
|
|
|
|
|
|
proc persistBlockJsonMain*() =
|
|
|
|
suite "persist block json tests":
|
2019-11-20 17:07:17 +00:00
|
|
|
jsonTest("PersistBlockTests", testFixture)
|
2021-01-19 09:02:24 +00:00
|
|
|
#var testStatusIMPL: TestStatus
|
|
|
|
#let n = json.parseFile("tests" / "fixtures" / "PersistBlockTests" / "block420301.json")
|
|
|
|
#testFixture(n, testStatusIMPL)
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
persistBlockJsonMain()
|