Remove some doAsserts that are easy to trigger from user actions (#4791)

This commit is contained in:
zah 2023-04-05 21:52:42 +03:00 committed by GitHub
parent 1459189e46
commit b59f9f5e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 26 deletions

View File

@ -300,7 +300,8 @@ proc setupDB(db: SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest) =
); );
""").expect("DB should be working and \"attestations\" should not exist") """).expect("DB should be working and \"attestations\" should not exist")
proc checkDB(db: SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest) = proc checkDB(db: SlashingProtectionDB_v2,
genesis_validators_root: Eth2Digest): Result[void, string] =
## Check the metadata of the DB ## Check the metadata of the DB
let selectStmt = db.backend.prepareStmt( let selectStmt = db.backend.prepareStmt(
"SELECT * FROM metadata;", "SELECT * FROM metadata;",
@ -316,13 +317,14 @@ proc checkDB(db: SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest) =
selectStmt.dispose() selectStmt.dispose()
doAssert status.isOk() if status.isErr:
doAssert version == db.typeof().version(), return err "Unable to read DB metadata"
"Incorrect database version: " & $version & "\n" & if version != db.typeof().version():
"but expected: " & $db.typeof().version() return err "Incorrect database version: " & $version & " " &
doAssert root == genesis_validators_root, "but expected: " & $db.typeof().version()
"Invalid database genesis validator root: " & root.data.toHex() & "\n" & if root != genesis_validators_root:
"but expected: " & genesis_validators_root.data.toHex() return err "Invalid database genesis validator root: " & root.data.toHex() & " " &
"but expected: " & genesis_validators_root.data.toHex()
proc setupCachedQueries(db: SlashingProtectionDB_v2) = proc setupCachedQueries(db: SlashingProtectionDB_v2) =
## Create prepared queries for reuse ## Create prepared queries for reuse
@ -655,22 +657,32 @@ proc getMetadataTable_DbV2*(db: SlashingProtectionDB_v2): Option[Eth2Digest] =
else: else:
return none(Eth2Digest) return none(Eth2Digest)
proc initCompatV1*(T: type SlashingProtectionDB_v2, proc initCompatV1*(
genesis_validators_root: Eth2Digest, T: type SlashingProtectionDB_v2,
basePath: string, genesis_validators_root: Eth2Digest,
dbname: string databasePath: string,
): tuple[db: SlashingProtectionDB_v2, requiresMigration: bool] = databaseName: string
): tuple[db: SlashingProtectionDB_v2, requiresMigration: bool] =
## Initialize a new slashing protection database ## Initialize a new slashing protection database
## or load an existing one with matching genesis root ## or load an existing one with matching genesis root
## `dbname` MUST not be ending with .sqlite3 ## `databaseName` MUST not be ending with .sqlite3
logScope:
databasePath
databaseName
let alreadyExists = fileExists(basePath/dbname&".sqlite3") let
alreadyExists = fileExists(databasePath / databaseName & ".sqlite3")
backend = SqStoreRef.init(databasePath, databaseName).valueOr:
fatal "Failed to open slashing protection database"
quit 1
result.db = T(backend: SqStoreRef.init( result.db = T(backend: backend)
basePath, dbname,
).get())
if alreadyExists and result.db.getMetadataTable_DbV2().isSome(): if alreadyExists and result.db.getMetadataTable_DbV2().isSome():
result.db.checkDB(genesis_validators_root) let status = result.db.checkDB(genesis_validators_root)
if status.isErr:
fatal "Incompatible slashing protection database",
reason = status.error
quit 1
result.requiresMigration = false result.requiresMigration = false
elif alreadyExists: elif alreadyExists:
result.db.setupDB(genesis_validators_root) result.db.setupDB(genesis_validators_root)
@ -684,25 +696,36 @@ proc initCompatV1*(T: type SlashingProtectionDB_v2,
debug "Loaded slashing protection (v2)", debug "Loaded slashing protection (v2)",
genesis_validators_root = shortLog(genesis_validators_root), genesis_validators_root = shortLog(genesis_validators_root),
requiresMigration = result.requiresMigration, requiresMigration = result.requiresMigration
basePath, dbname
# Resource Management # Resource Management
# ------------------------------------------------------------- # -------------------------------------------------------------
proc init*(T: type SlashingProtectionDB_v2, proc init*(T: type SlashingProtectionDB_v2,
genesis_validators_root: Eth2Digest, genesis_validators_root: Eth2Digest,
basePath: string, databasePath: string,
dbname: string): T = databaseName: string): T =
## Initialize a new slashing protection database ## Initialize a new slashing protection database
## or load an existing one with matching genesis root ## or load an existing one with matching genesis root
## `dbname` MUST not be ending with .sqlite3 ## `dbname` MUST not be ending with .sqlite3
logScope:
databasePath
databaseName
let alreadyExists = fileExists(basePath/dbname&".sqlite3") let
alreadyExists = fileExists(databasePath / databaseName & ".sqlite3")
backend = SqStoreRef.init(databasePath, databaseName,
keyspaces = []).valueOr:
fatal "Failed to open slashing protection database"
quit 1
result = T(backend: SqStoreRef.init(basePath, dbname, keyspaces = []).get()) result = T(backend: backend)
if alreadyExists: if alreadyExists:
result.checkDB(genesis_validators_root) let status = result.checkDB(genesis_validators_root)
if status.isErr:
fatal "Slashing protection database check error",
reason = status.error
quit 1
else: else:
result.setupDB(genesis_validators_root) result.setupDB(genesis_validators_root)