Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified database frontend #1661

Merged
merged 8 commits into from
Jul 31, 2023
Next Next commit
Remove 32bit os support from custom_network unit test
also:
* Fix compilation annoyance #1648
* Fix unit test on Kiln (changed `merge` logic?)
  • Loading branch information
mjfh committed Jul 26, 2023
commit e4049e18bcf42afbf0148baa31349bf05676d74e
102 changes: 36 additions & 66 deletions tests/test_custom_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
##

import
std/[distros, os],
std/os,
chronicles,
results,
unittest2,
../nimbus/core/chain, # must be early (compilation annoyance)
../nimbus/config,
../nimbus/db/select_backend,
../nimbus/core/chain,
../nimbus/common/common,
./replay/[undump_blocks, pp],
chronicles,
stew/results,
unittest2
./replay/[undump_blocks, pp]

type
ReplaySession = object
Expand Down Expand Up @@ -85,31 +85,7 @@ const
termTotalDff: 20_000_000_000_000.u256,
mergeFork: 1000,
ttdReachedAt: 55127,
failBlockAt: 9999999)

when not defined(linux):
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 ChainDBRef 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
failBlockAt: 1000) # Kludge, some change at the `merge` logic?

# Block chains shared between test suites
var
Expand Down Expand Up @@ -170,9 +146,8 @@ proc setErrorLevel =
# ------------------------------------------------------------------------------

proc ddbCleanUp(dir: string) =
if not disablePersistentDB:
ddbDir = dir
dir.flushDbDir
ddbDir = dir
dir.flushDbDir

proc ddbCleanUp =
ddbDir.ddbCleanUp
Expand All @@ -195,14 +170,15 @@ proc importBlocks(c: ChainRef; h: seq[BlockHeader]; b: seq[BlockBody];
bRng = if 1 < h.len: &"s [#{first}..#{last}]={h.len}" else: &" #{first}"
blurb = &"persistBlocks([#{first}..#"

noisy.say "***", &"block{bRng} #txs={nTxs} #uncles={nUnc}"

catchException("persistBlocks()", trace = true):
if c.persistBlocks(h, b).isOk:
noisy.say "***", &"block{bRng} #txs={nTxs} #uncles={nUnc}"
if not tddOk and c.com.ttdReached:
noisy.say "***", &"block{bRng} => tddReached"
return true

noisy.say "***", &"block{bRng} #txs={nTxs} #uncles={nUnc} -- failed"

# ------------------------------------------------------------------------------
# Test Runner
# ------------------------------------------------------------------------------
Expand All @@ -216,8 +192,7 @@ proc genesisLoadRunner(noisy = true;
gFileInfo = sSpcs.genesisFile.splitFile.name.split(".")[0]
gFilePath = sSpcs.genesisFile.findFilePath.value

tmpDir = if disablePersistentDB: "*notused*"
else: gFilePath.splitFile.dir / "tmp"
tmpDir = gFilePath.splitFile.dir / "tmp"

persistPruneInfo = if persistPruneTrie: "pruning enabled"
else: "no pruning"
Expand All @@ -240,24 +215,21 @@ proc genesisLoadRunner(noisy = true;
check mcom.toHardFork(sSpcs.mergeFork.toBlockNumber.blockNumberToForkDeterminationInfo) == MergeFork

test &"Construct persistent ChainDBRef on {tmpDir}, {persistPruneInfo}":
if disablePersistentDB:
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.ddbCleanUp

# Constructor ...
dcom = CommonRef.new(
tmpDir.newChainDB.trieDB,
networkId = params.config.chainId.NetworkId,
pruneTrie = persistPruneTrie,
params = params)

check dcom.ttd.get == sSpcs.termTotalDff
check dcom.toHardFork(sSpcs.mergeFork.toBlockNumber.blockNumberToForkDeterminationInfo) == MergeFork
# 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.ddbCleanUp

# Constructor ...
dcom = CommonRef.new(
tmpDir.newChainDB.trieDB,
networkId = params.config.chainId.NetworkId,
pruneTrie = persistPruneTrie,
params = params)

check dcom.ttd.get == sSpcs.termTotalDff
check dcom.toHardFork(sSpcs.mergeFork.toBlockNumber.blockNumberToForkDeterminationInfo) == MergeFork

test "Initialise in-memory Genesis":
mcom.initializeEmptyDb
Expand All @@ -270,18 +242,15 @@ proc genesisLoadRunner(noisy = true;
check storedhHeaderPP == onTheFlyHeaderPP

test "Initialise persistent Genesis":
if disablePersistentDB:
skip()
else:
dcom.initializeEmptyDb
dcom.initializeEmptyDb

# Must be the same as the in-memory DB value
check dcom.db.getBlockHash(0.u256) == mcom.db.getBlockHash(0.u256)
# Must be the same as the in-memory DB value
check dcom.db.getBlockHash(0.u256) == mcom.db.getBlockHash(0.u256)

let
storedhHeaderPP = dcom.db.getBlockHeader(0.u256).pp
onTheFlyHeaderPP = dcom.genesisHeader.pp
check storedhHeaderPP == onTheFlyHeaderPP
let
storedhHeaderPP = dcom.db.getBlockHeader(0.u256).pp
onTheFlyHeaderPP = dcom.genesisHeader.pp
check storedhHeaderPP == onTheFlyHeaderPP


proc testnetChainRunner(noisy = true;
Expand Down Expand Up @@ -386,6 +355,7 @@ when isMainModule:
# typically on the `nimbus-eth1-blobs` module.
noisy.testnetChainRunner(
stopAfterBlock = 999999999)

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------