2020-09-16 11:30:03 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2020-09-18 11:53:09 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-09-16 11:30:03 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2021-04-28 16:41:02 +00:00
|
|
|
std/[os],
|
2020-09-16 11:30:03 +00:00
|
|
|
# Status lib
|
2021-05-04 13:17:28 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
2020-09-16 11:30:03 +00:00
|
|
|
stew/results,
|
|
|
|
nimcrypto/utils,
|
|
|
|
# Internal
|
2021-05-04 13:17:28 +00:00
|
|
|
../../beacon_chain/validators/[slashing_protection, slashing_protection_v2],
|
2020-09-16 11:30:03 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, digest, crypto, presets],
|
|
|
|
# Test utilies
|
|
|
|
../testutil
|
|
|
|
|
|
|
|
func fakeRoot(index: SomeInteger): Eth2Digest =
|
|
|
|
## Create fake roots
|
|
|
|
## Those are just the value serialized in big-endian
|
|
|
|
## We prevent zero hash special case via a power of 2 prefix
|
|
|
|
result.data[0 ..< 8] = (1'u64 shl 32 + index.uint64).toBytesBE()
|
|
|
|
|
|
|
|
func fakeValidator(index: SomeInteger): ValidatorPubKey =
|
|
|
|
## Create fake validator public key
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
result = ValidatorPubKey()
|
2020-09-16 11:30:03 +00:00
|
|
|
result.blob[0 ..< 8] = (1'u64 shl 48 + index.uint64).toBytesBE()
|
|
|
|
|
|
|
|
func hexToDigest(hex: string): Eth2Digest =
|
|
|
|
result = Eth2Digest.fromHex(hex)
|
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
proc sqlite3db_delete(basepath, dbname: string) =
|
|
|
|
removeFile(basepath / dbname&".sqlite3-shm")
|
|
|
|
removeFile(basepath / dbname&".sqlite3-wal")
|
|
|
|
removeFile(basepath / dbname&".sqlite3")
|
|
|
|
|
|
|
|
const TestDir = ""
|
|
|
|
const TestDbName = "test_slashprot"
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Slashing Protection DB - Interchange" & preset():
|
2020-09-16 11:30:03 +00:00
|
|
|
# https://hackmd.io/@sproul/Bk0Y0qdGD#Format-1-Complete
|
2021-02-09 15:23:06 +00:00
|
|
|
# https://eips.ethereum.org/EIPS/eip-3076
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Smoke test - Complete format" & preset():
|
2020-09-16 11:30:03 +00:00
|
|
|
let genesis_validators_root = hexToDigest"0x04700007fabc8282644aed6d1c7c9e21d38a03a0c4ba193f3afe428824b3a673"
|
|
|
|
block: # export
|
2021-02-09 15:23:06 +00:00
|
|
|
let db = SlashingProtectionDB.init(
|
|
|
|
genesis_validators_root,
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
defer:
|
|
|
|
db.close()
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
2020-09-16 11:30:03 +00:00
|
|
|
|
|
|
|
let pubkey = ValidatorPubKey
|
|
|
|
.fromHex"0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed"
|
|
|
|
.get()
|
2021-05-04 13:17:28 +00:00
|
|
|
check:
|
|
|
|
db.db_v2.registerBlock(
|
|
|
|
pubkey,
|
|
|
|
Slot 81952,
|
|
|
|
hexToDigest"0x4ff6f743a43f3b4f95350831aeaf0a122a1a392922c45d804280284a69eb850b"
|
|
|
|
).isOk()
|
|
|
|
# db.registerBlock(
|
|
|
|
# pubkey,
|
|
|
|
# Slot 81951,
|
|
|
|
# fakeRoot(65535)
|
|
|
|
# )
|
2020-09-16 11:30:03 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
db.db_v2.registerAttestation(
|
|
|
|
pubkey,
|
|
|
|
source = Epoch 2290,
|
|
|
|
target = Epoch 3007,
|
|
|
|
hexToDigest"0x587d6a4f59a58fe24f406e0502413e77fe1babddee641fda30034ed37ecc884d"
|
|
|
|
).isOk()
|
|
|
|
db.db_v2.registerAttestation(
|
|
|
|
pubkey,
|
|
|
|
source = Epoch 2290,
|
|
|
|
target = Epoch 3008,
|
|
|
|
fakeRoot(65535)
|
|
|
|
).isOk()
|
2020-09-16 11:30:03 +00:00
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
db.exportSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection.json")
|
2020-09-16 11:30:03 +00:00
|
|
|
|
|
|
|
block: # import - zero root db
|
2021-02-09 15:23:06 +00:00
|
|
|
let db2 = SlashingProtectionDB.init(
|
|
|
|
Eth2Digest(),
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
defer:
|
|
|
|
db2.close()
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
|
|
|
|
|
|
|
doAssert siSuccess == db2.importSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection.json")
|
|
|
|
db2.exportSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection_roundtrip1.json")
|
2020-09-16 11:30:03 +00:00
|
|
|
|
|
|
|
block: # import - same root db
|
2021-02-09 15:23:06 +00:00
|
|
|
let db3 = SlashingProtectionDB.init(
|
|
|
|
genesis_validators_root,
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
defer:
|
|
|
|
db3.close()
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
|
|
|
|
|
|
|
doAssert siSuccess == db3.importSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection.json")
|
|
|
|
db3.exportSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection_roundtrip2.json")
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Smoke test - Complete format - Invalid database is refused" & preset():
|
2020-09-16 11:30:03 +00:00
|
|
|
block: # import - invalid root db
|
|
|
|
let invalid_genvalroot = hexToDigest"0x1234"
|
2021-02-09 15:23:06 +00:00
|
|
|
let db4 = SlashingProtectionDB.init(
|
|
|
|
invalid_genvalroot,
|
|
|
|
TestDir,
|
|
|
|
TestDbName
|
|
|
|
)
|
|
|
|
defer:
|
|
|
|
db4.close()
|
|
|
|
sqlite3db_delete(TestDir, TestDbName)
|
|
|
|
|
|
|
|
doAssert siFailure == db4.importSlashingInterchange(currentSourcePath.parentDir/"test_complete_export_slashing_protection.json")
|