2022-01-26 11:56:53 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2019 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.
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
## This unit test was roughly inspired by repeated failings of running nimbus
|
|
|
|
## similar to
|
|
|
|
## ::
|
|
|
|
## nimbus \
|
|
|
|
## --data-dir:./kintsugi/tmp \
|
|
|
|
## --custom-network:kintsugi-network.json \
|
|
|
|
## --bootstrap-file:kintsugi-bootnodes.txt \
|
|
|
|
## --prune-mode:full ...
|
|
|
|
##
|
|
|
|
## from `issue 932` <https://github.com/status-im/nimbus-eth1/issues/932>`_.
|
|
|
|
|
2022-01-26 11:56:53 +00:00
|
|
|
import
|
2022-02-11 16:28:39 +00:00
|
|
|
std/[distros, os, strformat, strutils, sequtils],
|
2022-01-26 11:56:53 +00:00
|
|
|
../nimbus/[chain_config, config, genesis],
|
|
|
|
../nimbus/db/[db_chain, select_backend],
|
2022-02-11 16:28:39 +00:00
|
|
|
./replay/pp,
|
2022-01-26 11:56:53 +00:00
|
|
|
eth/[common, p2p, trie/db],
|
2022-02-11 16:28:39 +00:00
|
|
|
nimcrypto/hash,
|
2022-01-26 11:56:53 +00:00
|
|
|
unittest2
|
|
|
|
|
|
|
|
const
|
2022-02-11 16:28:39 +00:00
|
|
|
baseDir = [".", "tests", ".." / "tests", $DirSep] # path containg repo
|
|
|
|
repoDir = ["customgenesis", "."] # alternative repo paths
|
|
|
|
jFile = "kintsugi.json"
|
|
|
|
|
2022-01-26 11:56:53 +00:00
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
when not defined(linux):
|
2022-02-01 12:04:20 +00:00
|
|
|
const isUbuntu32bit = false
|
|
|
|
else:
|
|
|
|
# The `detectOs(Ubuntu)` directive is not Windows compatible, causes an
|
|
|
|
# error when running the system command `lsb_release -d` in the background.
|
|
|
|
let isUbuntu32bit = detectOs(Ubuntu) and int.sizeof == 4
|
|
|
|
|
|
|
|
let
|
|
|
|
# There is a problem with the Github/CI which results in spurious crashes
|
|
|
|
# when leaving the `runner()` if the persistent BaseChainDB initialisation
|
|
|
|
# was present. The Github/CI set up for Linux/i386 is
|
|
|
|
#
|
|
|
|
# Ubuntu 10.04.06 LTS
|
|
|
|
# with repo kernel 5.4.0-1065-azure (see 'uname -a')
|
|
|
|
#
|
|
|
|
# base OS architecture is amd64
|
|
|
|
# with i386 foreign architecture
|
|
|
|
#
|
|
|
|
# nimbus binary is an
|
|
|
|
# ELF 32-bit LSB shared object,
|
|
|
|
# Intel 80386, version 1 (SYSV), dynamically linked,
|
|
|
|
#
|
|
|
|
disablePersistentDB = isUbuntu32bit
|
|
|
|
|
2022-01-26 11:56:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc findFilePath(file: string): string =
|
|
|
|
result = "?unknown?" / file
|
|
|
|
for dir in baseDir:
|
|
|
|
for repo in repoDir:
|
|
|
|
let path = dir / repo / file
|
|
|
|
if path.fileExists:
|
|
|
|
return path
|
|
|
|
|
|
|
|
proc flushDbDir(s: string) =
|
|
|
|
let dataDir = s / "nimbus"
|
|
|
|
if (dataDir / "data").dirExists:
|
|
|
|
# Typically under Windows: there might be stale file locks.
|
|
|
|
try: dataDir.removeDir except: discard
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
proc say*(noisy = false; pfx = "***"; args: varargs[string, `$`]) =
|
|
|
|
if noisy:
|
|
|
|
if args.len == 0:
|
|
|
|
echo "*** ", pfx
|
|
|
|
elif 0 < pfx.len and pfx[^1] != ' ':
|
|
|
|
echo pfx, " ", args.toSeq.join
|
|
|
|
else:
|
|
|
|
echo pfx, args.toSeq.join
|
2022-01-26 11:56:53 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Test Runner
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc runner(noisy = true; file = jFile) =
|
|
|
|
let
|
|
|
|
fileInfo = file.splitFile.name.split(".")[0]
|
|
|
|
filePath = file.findFilePath
|
|
|
|
|
2022-02-01 12:04:20 +00:00
|
|
|
tmpDir = if disablePersistentDB: "*notused*"
|
|
|
|
else: filePath.splitFile.dir / "tmp"
|
|
|
|
|
|
|
|
defer:
|
|
|
|
if not disablePersistentDB: tmpDir.flushDbDir
|
2022-01-26 11:56:53 +00:00
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
suite "Kintsugi custom network test scenario":
|
2022-01-26 11:56:53 +00:00
|
|
|
var
|
|
|
|
params: NetworkParams
|
2022-02-01 12:04:20 +00:00
|
|
|
mdb, ddb: BaseChainDB
|
2022-01-26 11:56:53 +00:00
|
|
|
|
|
|
|
test &"Load params from {fileInfo}":
|
2022-02-11 16:28:39 +00:00
|
|
|
noisy.say "***", "custom-file=", filePath
|
2022-01-26 11:56:53 +00:00
|
|
|
check filePath.loadNetworkParams(params)
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
test "Construct in-memory BaseChainDB":
|
2022-01-26 11:56:53 +00:00
|
|
|
mdb = newBaseChainDB(
|
|
|
|
newMemoryDb(),
|
|
|
|
id = params.config.chainID.NetworkId,
|
|
|
|
params = params)
|
|
|
|
|
|
|
|
test &"Construct persistent BaseChainDB on {tmpDir}":
|
2022-02-01 12:04:20 +00:00
|
|
|
if disablePersistentDB:
|
2022-01-26 11:56:53 +00:00
|
|
|
skip()
|
|
|
|
else:
|
|
|
|
# Before allocating the database, the data directory needs to be
|
|
|
|
# cleared. There might be left overs from a previous crash or
|
|
|
|
# because there were file locks under Windows which prevented a
|
|
|
|
# previous clean up.
|
|
|
|
tmpDir.flushDbDir
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
# Constructor ...
|
2022-01-26 11:56:53 +00:00
|
|
|
ddb = newBaseChainDB(
|
|
|
|
tmpDir.newChainDb.trieDB,
|
|
|
|
id = params.config.chainID.NetworkId,
|
2022-02-11 16:28:39 +00:00
|
|
|
pruneTrie = true,
|
2022-01-26 11:56:53 +00:00
|
|
|
params = params)
|
|
|
|
|
|
|
|
test "Initialise in-memory Genesis":
|
|
|
|
mdb.initializeEmptyDb
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
# Verify variant of `toBlockHeader()`. The function `pp()` is used
|
|
|
|
# (rather than blockHash()) for readable error report (if any).
|
|
|
|
let
|
|
|
|
storedhHeaderPP = mdb.getBlockHeader(0.u256).pp
|
|
|
|
onTheFlyHeaderPP = mdb.toGenesisHeader.pp
|
|
|
|
check storedhHeaderPP == onTheFlyHeaderPP
|
2022-02-01 12:04:20 +00:00
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
test "Initialise persistent Genesis":
|
2022-02-01 12:04:20 +00:00
|
|
|
if disablePersistentDB:
|
|
|
|
skip()
|
|
|
|
else:
|
|
|
|
ddb.initializeEmptyDb
|
2022-01-26 11:56:53 +00:00
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
# Must be the same as the in-memory DB value
|
|
|
|
check ddb.getBlockHash(0.u256) == mdb.getBlockHash(0.u256)
|
|
|
|
|
|
|
|
let
|
|
|
|
storedhHeaderPP = ddb.getBlockHeader(0.u256).pp
|
|
|
|
onTheFlyHeaderPP = ddb.toGenesisHeader.pp
|
|
|
|
check storedhHeaderPP == onTheFlyHeaderPP
|
|
|
|
|
2022-01-26 11:56:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Main function(s)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-02-11 16:28:39 +00:00
|
|
|
proc customNetworkMain*(noisy = defined(debug)) =
|
2022-01-26 11:56:53 +00:00
|
|
|
noisy.runner
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
var noisy = defined(debug)
|
2022-02-11 16:28:39 +00:00
|
|
|
noisy = true
|
2022-01-26 11:56:53 +00:00
|
|
|
noisy.runner
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|