From b59f9f5e1a15841f1222c5f57b16e81e2f801117 Mon Sep 17 00:00:00 2001 From: zah Date: Wed, 5 Apr 2023 21:52:42 +0300 Subject: [PATCH] Remove some doAsserts that are easy to trigger from user actions (#4791) --- .../validators/slashing_protection_v2.nim | 75 ++++++++++++------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/beacon_chain/validators/slashing_protection_v2.nim b/beacon_chain/validators/slashing_protection_v2.nim index 473c01e8d..0a78dee83 100644 --- a/beacon_chain/validators/slashing_protection_v2.nim +++ b/beacon_chain/validators/slashing_protection_v2.nim @@ -300,7 +300,8 @@ proc setupDB(db: SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest) = ); """).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 let selectStmt = db.backend.prepareStmt( "SELECT * FROM metadata;", @@ -316,13 +317,14 @@ proc checkDB(db: SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest) = selectStmt.dispose() - doAssert status.isOk() - doAssert version == db.typeof().version(), - "Incorrect database version: " & $version & "\n" & - "but expected: " & $db.typeof().version() - doAssert root == genesis_validators_root, - "Invalid database genesis validator root: " & root.data.toHex() & "\n" & - "but expected: " & genesis_validators_root.data.toHex() + if status.isErr: + return err "Unable to read DB metadata" + if version != db.typeof().version(): + return err "Incorrect database version: " & $version & " " & + "but expected: " & $db.typeof().version() + if root != genesis_validators_root: + return err "Invalid database genesis validator root: " & root.data.toHex() & " " & + "but expected: " & genesis_validators_root.data.toHex() proc setupCachedQueries(db: SlashingProtectionDB_v2) = ## Create prepared queries for reuse @@ -655,22 +657,32 @@ proc getMetadataTable_DbV2*(db: SlashingProtectionDB_v2): Option[Eth2Digest] = else: return none(Eth2Digest) -proc initCompatV1*(T: type SlashingProtectionDB_v2, - genesis_validators_root: Eth2Digest, - basePath: string, - dbname: string - ): tuple[db: SlashingProtectionDB_v2, requiresMigration: bool] = +proc initCompatV1*( + T: type SlashingProtectionDB_v2, + genesis_validators_root: Eth2Digest, + databasePath: string, + databaseName: string + ): tuple[db: SlashingProtectionDB_v2, requiresMigration: bool] = ## Initialize a new slashing protection database ## 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( - basePath, dbname, - ).get()) + result.db = T(backend: backend) 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 elif alreadyExists: result.db.setupDB(genesis_validators_root) @@ -684,25 +696,36 @@ proc initCompatV1*(T: type SlashingProtectionDB_v2, debug "Loaded slashing protection (v2)", genesis_validators_root = shortLog(genesis_validators_root), - requiresMigration = result.requiresMigration, - basePath, dbname + requiresMigration = result.requiresMigration # Resource Management # ------------------------------------------------------------- proc init*(T: type SlashingProtectionDB_v2, genesis_validators_root: Eth2Digest, - basePath: string, - dbname: string): T = + databasePath: string, + databaseName: string): T = ## Initialize a new slashing protection database ## or load an existing one with matching genesis root ## `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: - 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: result.setupDB(genesis_validators_root)