From 4d66914f5ac4b96fee277424cf8b08feea6b7abd Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Mon, 12 Oct 2020 23:49:05 +0300 Subject: [PATCH] Fix the test suite --- beacon_chain/beacon_chain_db.nim | 15 +++++++++------ tests/test_beacon_chain_db.nim | 10 +++++----- tests/testutil.nim | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/beacon_chain/beacon_chain_db.nim b/beacon_chain/beacon_chain_db.nim index aa4336354..0de9f1f6c 100644 --- a/beacon_chain/beacon_chain_db.nim +++ b/beacon_chain/beacon_chain_db.nim @@ -167,18 +167,21 @@ proc insert*[K, V](m: DbMap[K, V], key: K, value: V) = proc contains*[K, V](m: DbMap[K, V], key: K): bool = contains(m.db, SSZ.encode key).expect("working database") -proc init*(T: type BeaconChainDB, dir: string): BeaconChainDB = +proc init*(T: type BeaconChainDB, dir: string, inMemory = false): BeaconChainDB = let s = createPath(dir, 0o750) doAssert s.isOk # TODO Handle this in a better way let sqliteStore = SqStoreRef.init(dir, "nbc", Keyspaces).expect( "working database") - T(backend: kvStore sqliteStore, - deposits: createSeq(sqliteStore, dir, "deposits", DepositData), - validators: createSeq(sqliteStore, dir, "validators", ImmutableValidatorData), - validatorsByKey: createMap(sqliteStore, int validatorIndexFromPubKey, - ValidatorPubKey, ValidatorIndex)) + if inMemory: + T(backend: kvStore MemStoreRef.init()) + else: + T(backend: kvStore sqliteStore, + deposits: createSeq(sqliteStore, dir, "deposits", DepositData), + validators: createSeq(sqliteStore, dir, "validators", ImmutableValidatorData), + validatorsByKey: createMap(sqliteStore, int validatorIndexFromPubKey, + ValidatorPubKey, ValidatorIndex)) proc snappyEncode(inp: openArray[byte]): seq[byte] = try: diff --git a/tests/test_beacon_chain_db.nim b/tests/test_beacon_chain_db.nim index 15655197f..11d4214f9 100644 --- a/tests/test_beacon_chain_db.nim +++ b/tests/test_beacon_chain_db.nim @@ -38,14 +38,14 @@ func withDigest(blck: TrustedBeaconBlock): TrustedSignedBeaconBlock = suiteReport "Beacon chain DB" & preset(): wrappedTimedTest "empty database" & preset(): var - db = init(BeaconChainDB, kvStore MemStoreRef.init()) + db = init(BeaconChainDB, "", inMemory = true) check: db.getStateRef(Eth2Digest()).isNil db.getBlock(Eth2Digest()).isNone wrappedTimedTest "sanity check blocks" & preset(): var - db = init(BeaconChainDB, kvStore MemStoreRef.init()) + db = init(BeaconChainDB, "", inMemory = true) let signedBlock = withDigest(TrustedBeaconBlock()) @@ -70,7 +70,7 @@ suiteReport "Beacon chain DB" & preset(): wrappedTimedTest "sanity check states" & preset(): var - db = init(BeaconChainDB, kvStore MemStoreRef.init()) + db = init(BeaconChainDB, "", inMemory = true) let state = BeaconStateRef() @@ -86,7 +86,7 @@ suiteReport "Beacon chain DB" & preset(): wrappedTimedTest "find ancestors" & preset(): var - db = init(BeaconChainDB, kvStore MemStoreRef.init()) + db = init(BeaconChainDB, "", inMemory = true) let a0 = withDigest( @@ -120,7 +120,7 @@ suiteReport "Beacon chain DB" & preset(): # serialization where an all-zero default-initialized bls signature could # not be deserialized because the deserialization was too strict. var - db = init(BeaconChainDB, kvStore MemStoreRef.init()) + db = init(BeaconChainDB, "", inMemory = true) let state = initialize_beacon_state_from_eth1( diff --git a/tests/testutil.nim b/tests/testutil.nim index 8fa704892..4b7c21e25 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -6,12 +6,12 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - algorithm, strformat, stats, times, tables, std/monotimes, stew/endians2, + os, algorithm, strformat, stats, times, tables, std/monotimes, stew/endians2, testutils/markdown_reports, chronicles, ../beacon_chain/[beacon_chain_db, extras, ssz], ../beacon_chain/spec/[digest, beaconstate, datatypes, presets], ../beacon_chain/block_pools/chain_dag, - eth/db/kvstore, + eth/db/[kvstore, kvstore_sqlite3], testblockutil type @@ -98,7 +98,7 @@ template timedTest*(name, body) = testTimes.add (f, name) proc makeTestDB*(tailState: BeaconState, tailBlock: SignedBeaconBlock): BeaconChainDB = - result = init(BeaconChainDB, kvStore MemStoreRef.init()) + result = init(BeaconChainDB, "", inMemory = true) ChainDAGRef.preInit(result, tailState, tailState, tailBlock) proc makeTestDB*(validators: Natural): BeaconChainDB =