nimbus-eth1/nimbus/conf_utils.nim
Jordan Hrycaj a49a812879
Jordan/fix some failing nohive tests (#727)
* continue importing rlp blocks

why:
  a chain of blocks to be imported might have legit blocks
  after rejected blocks

details:
  import loop only stops if the import list is exhausted or if there
  was a decoding error. this adds another four to the count of successful
  no-hive tests.

* verify DAO marked extra data field in block header

why:
  was ignored, scores another two no-hive tests

* verify minimum required difficulty in header validator

why:
  two more nohive tests to succeed

details:
  * subsumed extended header tests under validateKinship() and renamed it
    more appropriately validateHeaderAndKinship()
  * enhanced readability of p2p/chain.nim
  * cleaned up test_blockchain_json.nim

* verify positive gasUsed unless no transactions

why:
  solves another to nohive tests

details:
  straightened test_blockchain_json chech so there is no unconditional
  rejection anymore (based on the input test  scenario)
2021-06-24 16:29:21 +01:00

62 lines
1.7 KiB
Nim

# Nimbus
# Copyright (c) 2021 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
std/[terminal, os],
chronicles, eth/trie/db, eth/[common, rlp], stew/[io2, byteutils],
./config, ./genesis, ./p2p/chain,
./db/[db_chain, select_backend, storage_types]
type
# trick the rlp decoder
# so we can separate the body and header
EthHeader = object
header: BlockHeader
proc importRlpBlock*(importFile: string; chainDB: BasechainDB): bool =
let res = io2.readAllBytes(importFile)
if res.isErr:
error "failed to import",
fileName = importFile
return false
var
# the encoded rlp can contains one or more blocks
rlp = rlpFromBytes(res.get)
chain = newChain(chainDB, extraValidation = true)
errorCount = 0
let
head = chainDB.getCanonicalHead()
while rlp.hasData:
try:
let
header = rlp.read(EthHeader).header
body = rlp.readRecordType(BlockBody, false)
if header.blockNumber > head.blockNumber:
if chain.persistBlocks([header], [body]) == ValidationResult.Error:
# register one more error and continue
errorCount.inc
except RlpError as e:
# terminate if there was a decoding error
error "rlp error",
fileName = importFile,
msg = e.msg,
exception = e.name
return false
except CatchableError as e:
# otherwise continue
error "import error",
fileName = importFile,
msg = e.msg,
exception = e.name
errorCount.inc
return errorCount == 0