2021-02-09 15:23:06 +00:00
|
|
|
# Nimbus
|
2021-03-19 08:26:02 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2021-02-09 15:23:06 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2021-04-28 16:41:02 +00:00
|
|
|
std/[os],
|
2021-02-09 15:23:06 +00:00
|
|
|
# Status lib
|
2021-05-04 13:17:28 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
2021-02-09 15:23:06 +00:00
|
|
|
stew/results,
|
|
|
|
nimcrypto/utils,
|
|
|
|
serialization,
|
|
|
|
json_serialization,
|
|
|
|
# Internal
|
2021-03-02 10:27:45 +00:00
|
|
|
../../beacon_chain/validators/[
|
2021-02-09 15:23:06 +00:00
|
|
|
slashing_protection,
|
2021-03-19 08:26:02 +00:00
|
|
|
slashing_protection_v1
|
2021-02-09 15:23:06 +00:00
|
|
|
],
|
2021-06-23 14:43:18 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base,
|
|
|
|
../../beacon_chain/spec/[digest, crypto, presets],
|
2021-02-09 15:23:06 +00:00
|
|
|
# Test utilies
|
|
|
|
../testutil
|
|
|
|
|
|
|
|
func hexToDigest(hex: string): Eth2Digest =
|
|
|
|
result = Eth2Digest.fromHex(hex)
|
|
|
|
|
|
|
|
proc sqlite3db_delete(basepath, dbname: string) =
|
|
|
|
removeFile(basepath / dbname&".sqlite3-shm")
|
|
|
|
removeFile(basepath / dbname&".sqlite3-wal")
|
|
|
|
removeFile(basepath / dbname&".sqlite3")
|
|
|
|
|
|
|
|
const TestDir = ""
|
|
|
|
const TestDbName = "t_slashprot_migration"
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Slashing Protection DB - v1 and v2 migration" & preset():
|
2021-02-09 15:23:06 +00:00
|
|
|
# https://eips.ethereum.org/EIPS/eip-3076
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
test "Minimal format migration" & preset():
|
2021-02-09 15:23:06 +00:00
|
|
|
let genesis_validators_root = hexToDigest"0x04700007fabc8282644aed6d1c7c9e21d38a03a0c4ba193f3afe428824b3a673"
|
|
|
|
block: # export from a v1 DB
|
|
|
|
let db = SlashingProtectionDB_v1.init(
|
|
|
|
genesis_validators_root,
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
|
|
|
|
let pubkey = ValidatorPubKey
|
|
|
|
.fromHex"0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed"
|
|
|
|
.get()
|
2021-05-04 13:17:28 +00:00
|
|
|
check:
|
|
|
|
db.registerBlock(
|
|
|
|
pubkey,
|
|
|
|
Slot 81952,
|
|
|
|
Eth2Digest()
|
|
|
|
).isOk()
|
|
|
|
|
|
|
|
db.registerAttestation(
|
|
|
|
pubkey,
|
|
|
|
source = Epoch 2290,
|
|
|
|
target = Epoch 3007,
|
|
|
|
Eth2Digest()
|
|
|
|
).isOk()
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
let spdir = db.toSPDIR_lowWatermark()
|
|
|
|
Json.saveFile(
|
|
|
|
currentSourcePath.parentDir/"t_migration_slashing_protection_v1.json",
|
|
|
|
spdir,
|
|
|
|
pretty = true
|
|
|
|
)
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
block: # Reopen as the new version
|
|
|
|
let db = SlashingProtectionDB.init(
|
|
|
|
genesis_validators_root,
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that v2 as been initialized (private field :/)
|
|
|
|
# doAssert: db.db_v2.getMetadataTable_DbV2().get() == genesis_validators_root
|
|
|
|
|
|
|
|
db.exportSlashingInterchange(
|
|
|
|
currentSourcePath.parentDir/"t_migration_slashing_protection_migrated.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
doAssert sameFileContent(
|
|
|
|
currentSourcePath.parentDir/"t_migration_slashing_protection_v1.json",
|
|
|
|
currentSourcePath.parentDir/"t_migration_slashing_protection_migrated.json"
|
|
|
|
)
|