2021-02-09 15:23:06 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-02-09 15:23:06 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
import
|
|
|
|
# stdlib
|
2021-05-19 06:38:13 +00:00
|
|
|
std/[os, algorithm, sequtils],
|
2021-02-09 15:23:06 +00:00
|
|
|
# Status
|
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
2024-01-16 22:37:14 +00:00
|
|
|
results,
|
|
|
|
stew/byteutils,
|
2021-05-19 06:38:13 +00:00
|
|
|
chronicles, chronicles/timings,
|
2021-02-09 15:23:06 +00:00
|
|
|
# Internal
|
2021-06-23 14:43:18 +00:00
|
|
|
../spec/datatypes/base,
|
2021-02-09 15:23:06 +00:00
|
|
|
./slashing_protection_common,
|
|
|
|
./slashing_protection_v2
|
|
|
|
|
2021-09-27 09:24:58 +00:00
|
|
|
export slashing_protection_common, kvstore, kvstore_sqlite3
|
2021-05-04 13:17:28 +00:00
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
# Generic sandwich
|
|
|
|
export chronicles
|
|
|
|
|
|
|
|
# The high-level slashing protection DB
|
|
|
|
# -------------------------------------
|
|
|
|
# This file abstracts differences and
|
|
|
|
# migration between slashing protection implementations
|
|
|
|
# and DB schemas
|
|
|
|
#
|
|
|
|
# This is done by instantiating
|
|
|
|
# multiple slashing DB versions using the same handle.
|
|
|
|
#
|
|
|
|
# We assume that in case of backward compatible changes
|
|
|
|
# The new version will use different tables.
|
|
|
|
#
|
|
|
|
# During transition period, we allow using multiple
|
|
|
|
# slashing protection implementations to validate
|
|
|
|
# the behavior of the new implementation.
|
|
|
|
#
|
|
|
|
# Note: this will increase disk IO.
|
|
|
|
|
|
|
|
type
|
|
|
|
SlashProtDBMode* = enum
|
2021-05-19 06:38:13 +00:00
|
|
|
kCompleteArchive # Complete Format V2 backend (saves all attestations)
|
|
|
|
kLowWatermark # Low-Watermark Format V2 backend (prunes attestations)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
SlashingProtectionDB* = ref object
|
|
|
|
## Database storing the blocks attested
|
|
|
|
## by validators attached to a beacon node
|
|
|
|
## or validator client.
|
2021-05-04 13:17:28 +00:00
|
|
|
db_v2*: SlashingProtectionDB_v2
|
2021-02-09 15:23:06 +00:00
|
|
|
modes: set[SlashProtDBMode]
|
|
|
|
|
|
|
|
# DB Multiversioning
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
|
|
func version*(_: type SlashingProtectionDB): static int =
|
|
|
|
# The highest DB version supported
|
|
|
|
2
|
|
|
|
|
|
|
|
# Resource Management
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type SlashingProtectionDB,
|
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
basePath, dbname: string,
|
2021-05-17 16:37:26 +00:00
|
|
|
modes: set[SlashProtDBMode]
|
2021-05-19 06:38:13 +00:00
|
|
|
): T =
|
2021-02-09 15:23:06 +00:00
|
|
|
## Initialize or load a slashing protection DB
|
|
|
|
## This is for Beacon Node usage
|
|
|
|
## Handles DB version migration
|
|
|
|
|
|
|
|
doAssert modes.card >= 1, "No slashing protection mode chosen. Choose a v1, a v2 or v1 and v2 slashing DB mode."
|
|
|
|
doAssert not(
|
2021-05-19 06:38:13 +00:00
|
|
|
kCompleteArchive in modes and
|
|
|
|
kLowWatermark in modes), "Mode(s): " & $modes & ". Choose only one of V2 DB modes."
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
new result
|
|
|
|
result.modes = modes
|
|
|
|
|
2021-02-19 15:18:17 +00:00
|
|
|
let (db, requiresMigration) = SlashingProtectionDB_v2.initCompatV1(
|
2021-02-09 15:23:06 +00:00
|
|
|
genesis_validators_root,
|
|
|
|
basePath, dbname
|
|
|
|
)
|
2021-02-19 15:18:17 +00:00
|
|
|
result.db_v2 = db
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
if requiresMigration:
|
2022-01-20 16:14:06 +00:00
|
|
|
fatal "The slashing database predates Altair hardfork from October 2021." &
|
2022-04-08 16:22:49 +00:00
|
|
|
" You can migrate to the new DB format using Nimbus 1.6.0" &
|
2022-01-20 16:14:06 +00:00
|
|
|
" for a few minutes at https://github.com/status-im/nimbus-eth2/releases/tag/v1.6.0" &
|
|
|
|
" until the messages \"Migrating local validators slashing DB from v1 to v2\"" &
|
|
|
|
" and \"Slashing DB migration successful.\""
|
|
|
|
|
|
|
|
quit 1
|
2021-05-17 16:37:26 +00:00
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
proc init*(
|
|
|
|
T: type SlashingProtectionDB,
|
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
basePath, dbname: string
|
|
|
|
): T =
|
|
|
|
## Initialize or load a slashing protection DB
|
|
|
|
## With defaults
|
|
|
|
## - v2 DB only, low watermark (regular pruning)
|
|
|
|
##
|
|
|
|
## Does not handle migration
|
|
|
|
init(
|
|
|
|
T, genesis_validators_root, basePath, dbname,
|
2021-05-19 06:38:13 +00:00
|
|
|
modes = {kLowWatermark}
|
2021-02-09 15:23:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc loadUnchecked*(
|
|
|
|
T: type SlashingProtectionDB,
|
|
|
|
basePath, dbname: string, readOnly: bool
|
2023-08-25 09:29:07 +00:00
|
|
|
): SlashingProtectionDB {.raises:[IOError].}=
|
2021-02-09 15:23:06 +00:00
|
|
|
## Load a slashing protection DB
|
|
|
|
## Note: This is for CLI tool usage
|
|
|
|
## this doesn't check the genesis validator root
|
|
|
|
##
|
|
|
|
## Does not handle migration
|
2021-05-19 06:38:13 +00:00
|
|
|
new result
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-19 06:38:13 +00:00
|
|
|
result.modes = {}
|
|
|
|
try:
|
|
|
|
result.db_v2 = SlashingProtectionDB_v2.loadUnchecked(
|
|
|
|
basePath, dbname, readOnly
|
|
|
|
)
|
|
|
|
result.modes.incl(kCompleteArchive)
|
|
|
|
except CatchableError as err:
|
|
|
|
error "Failed to load the Slashing protection database", err = err.msg
|
|
|
|
quit 1
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
proc close*(db: SlashingProtectionDB) =
|
|
|
|
## Close a slashing protection database
|
|
|
|
db.db_v2.close()
|
|
|
|
# v1 and v2 are ref objects and use the same DB handle
|
|
|
|
# so closing one closes both
|
|
|
|
|
|
|
|
# DB Queries
|
|
|
|
# --------------------------------------------
|
|
|
|
|
|
|
|
proc checkSlashableBlockProposal*(
|
|
|
|
db: SlashingProtectionDB,
|
2021-05-04 13:17:28 +00:00
|
|
|
index: ValidatorIndex,
|
2021-02-09 15:23:06 +00:00
|
|
|
validator: ValidatorPubKey,
|
|
|
|
slot: Slot
|
|
|
|
): Result[void, BadProposal] =
|
|
|
|
## Returns an error if the specified validator
|
|
|
|
## already proposed a block for the specified slot.
|
|
|
|
## This would lead to slashing.
|
|
|
|
## The error contains the blockroot that was already proposed
|
|
|
|
##
|
|
|
|
## Returns success otherwise
|
2023-11-19 13:08:07 +00:00
|
|
|
checkSlashableBlockProposal(db.db_v2, Opt.some(index), validator, slot)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
proc checkSlashableAttestation*(
|
|
|
|
db: SlashingProtectionDB,
|
2021-05-04 13:17:28 +00:00
|
|
|
index: ValidatorIndex,
|
2021-02-09 15:23:06 +00:00
|
|
|
validator: ValidatorPubKey,
|
|
|
|
source: Epoch,
|
|
|
|
target: Epoch
|
|
|
|
): Result[void, BadVote] =
|
|
|
|
## Returns an error if the specified validator
|
|
|
|
## already voted for the specified slot
|
|
|
|
## or would vote in a contradiction to previous votes
|
|
|
|
## (surrounding vote or surrounded vote).
|
|
|
|
##
|
|
|
|
## Returns success otherwise
|
2023-11-19 13:08:07 +00:00
|
|
|
checkSlashableAttestation(db.db_v2, Opt.some(index), validator, source, target)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
# DB Updates - only v2 supported here
|
2021-02-09 15:23:06 +00:00
|
|
|
# --------------------------------------------
|
|
|
|
|
|
|
|
proc registerBlock*(
|
|
|
|
db: SlashingProtectionDB,
|
2021-05-04 13:17:28 +00:00
|
|
|
index: ValidatorIndex,
|
2021-02-09 15:23:06 +00:00
|
|
|
validator: ValidatorPubKey,
|
2021-05-04 13:17:28 +00:00
|
|
|
slot: Slot, block_signing_root: Eth2Digest): Result[void, BadProposal] =
|
|
|
|
## Add a block to the slashing protection DB - the registration will
|
|
|
|
## fail if it would violate a slashing protection rule.
|
2021-02-09 15:23:06 +00:00
|
|
|
##
|
|
|
|
## block_signing_root is the output of
|
|
|
|
## compute_signing_root(block, domain)
|
2023-11-19 13:08:07 +00:00
|
|
|
registerBlock(db.db_v2, Opt.some(index), validator, slot, block_signing_root)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
proc registerAttestation*(
|
|
|
|
db: SlashingProtectionDB,
|
2021-05-04 13:17:28 +00:00
|
|
|
index: ValidatorIndex,
|
2021-02-09 15:23:06 +00:00
|
|
|
validator: ValidatorPubKey,
|
|
|
|
source, target: Epoch,
|
2021-05-04 13:17:28 +00:00
|
|
|
attestation_signing_root: Eth2Digest): Result[void, BadVote] =
|
|
|
|
## Add an attestation to the slashing protection DB - the registration will
|
|
|
|
## fail if it would violate a slashing protection rule.
|
2021-02-09 15:23:06 +00:00
|
|
|
##
|
|
|
|
## attestation_signing_root is the output of
|
|
|
|
## compute_signing_root(attestation, domain)
|
2023-11-19 13:08:07 +00:00
|
|
|
registerAttestation(db.db_v2, Opt.some(index), validator,
|
2021-05-04 13:17:28 +00:00
|
|
|
source, target, attestation_signing_root)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2023-11-19 13:08:07 +00:00
|
|
|
template withContext*(db: SlashingProtectionDB, body: untyped): untyped =
|
|
|
|
## Perform multiple slashing database operations within a single database
|
|
|
|
## context
|
|
|
|
db.db_v2.withContext:
|
|
|
|
template registerAttestationInContext(
|
|
|
|
index: ValidatorIndex,
|
|
|
|
validator: ValidatorPubKey,
|
|
|
|
source, target: Epoch,
|
|
|
|
attestation_signing_root: Eth2Digest): Result[void, BadVote] =
|
|
|
|
registerAttestationInContextV2(Opt.some(index), validator, source, target, attestation_signing_root)
|
|
|
|
block:
|
|
|
|
body
|
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
# DB maintenance
|
|
|
|
# --------------------------------------------
|
|
|
|
# private for now
|
|
|
|
|
|
|
|
proc pruneBlocks*(
|
|
|
|
db: SlashingProtectionDB,
|
2022-04-08 16:22:49 +00:00
|
|
|
validator: ValidatorPubKey,
|
2021-02-09 15:23:06 +00:00
|
|
|
newMinSlot: Slot) =
|
|
|
|
## Prune all blocks from a validator before the specified newMinSlot
|
|
|
|
## This is intended for interchange import to ensure
|
|
|
|
## that in case of a gap, we don't allow signing in that gap.
|
|
|
|
##
|
|
|
|
## Note: DB v1 does not support pruning
|
|
|
|
|
|
|
|
# {.error: "This is a backend specific proc".}
|
|
|
|
fatal "This is a backend specific proc"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
proc pruneAttestations*(
|
|
|
|
db: SlashingProtectionDB,
|
2022-04-08 16:22:49 +00:00
|
|
|
validator: ValidatorPubKey,
|
2021-03-10 15:53:42 +00:00
|
|
|
newMinSourceEpoch: int64,
|
|
|
|
newMinTargetEpoch: int64) =
|
2021-02-09 15:23:06 +00:00
|
|
|
## Prune all blocks from a validator before the specified newMinSlot
|
|
|
|
## This is intended for interchange import to ensure
|
|
|
|
## that in case of a gap, we don't allow signing in that gap.
|
|
|
|
##
|
|
|
|
## Note: DB v1 does not support pruning
|
|
|
|
|
|
|
|
# {.error: "This is a backend specific proc".}
|
|
|
|
fatal "This is a backend specific proc"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
proc pruneAfterFinalization*(
|
|
|
|
db: SlashingProtectionDB,
|
|
|
|
finalizedEpoch: Epoch
|
|
|
|
) =
|
2021-05-10 14:32:28 +00:00
|
|
|
## Prune blocks and attestations after a specified `finalizedEpoch`
|
|
|
|
## The block with the highest slot
|
|
|
|
## and the attestation(s) with the highest source and target epochs
|
|
|
|
## are never pruned.
|
|
|
|
##
|
|
|
|
## This ensures that even if pruning is called with an incorrect epoch
|
|
|
|
## slashing protection can fallback to the minimal / high-watermark protection mode.
|
|
|
|
##
|
2021-05-19 06:38:13 +00:00
|
|
|
## Pruning is only done if pruning is enabled (DB in kLowWatermark mode)
|
2021-05-10 14:32:28 +00:00
|
|
|
## Pruning is only triggered on v2 database.
|
|
|
|
|
2021-05-19 06:38:13 +00:00
|
|
|
if kLowWatermark in db.modes:
|
|
|
|
debug.logTime "Pruning slashing DB":
|
|
|
|
db.db_v2.pruneAfterFinalization(finalizedEpoch)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2022-01-20 16:14:06 +00:00
|
|
|
# Interchange
|
|
|
|
# --------------------------------------------
|
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
# The high-level import/export functions are
|
|
|
|
# - importSlashingInterchange
|
|
|
|
# - exportSlashingInterchange
|
|
|
|
# in slashing_protection_types.nim
|
|
|
|
#
|
|
|
|
# That builds on a DB backend inclSPDIR and toSPDIR
|
|
|
|
# SPDIR being a common Intermediate Representation
|
|
|
|
|
2022-01-20 16:14:06 +00:00
|
|
|
proc registerSyntheticAttestation*(db: SlashingProtectionDB,
|
|
|
|
validator: ValidatorPubKey,
|
|
|
|
source, target: Epoch) =
|
|
|
|
db.db_v2.registerSyntheticAttestation(validator, source, target)
|
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
proc inclSPDIR*(db: SlashingProtectionDB, spdir: SPDIR): SlashingImportStatus
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [SerializationError, IOError].} =
|
2021-05-04 13:17:28 +00:00
|
|
|
db.db_v2.inclSPDIR(spdir)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
proc toSPDIR*(db: SlashingProtectionDB): SPDIR
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [IOError].} =
|
2021-05-04 13:17:28 +00:00
|
|
|
db.db_v2.toSPDIR()
|
2021-05-19 06:38:13 +00:00
|
|
|
|
|
|
|
proc exportSlashingInterchange*(
|
|
|
|
db: SlashingProtectionDB,
|
|
|
|
path: string,
|
|
|
|
validatorsWhiteList: seq[PubKey0x] = @[],
|
2023-08-25 09:29:07 +00:00
|
|
|
prettify = true) {.raises: [IOError].} =
|
2021-05-19 06:38:13 +00:00
|
|
|
## Export a database to the Slashing Protection Database Interchange Format
|
|
|
|
# We could modify toSPDIR to do the filtering directly
|
|
|
|
# but this is not a performance sensitive operation.
|
|
|
|
# so it's better to keep it simple.
|
|
|
|
var spdir = db.toSPDIR()
|
|
|
|
|
|
|
|
if validatorsWhiteList.len > 0:
|
|
|
|
# O(a log b) with b the number of validators to keep
|
|
|
|
# and a the total number of validators in DB
|
|
|
|
let validators = validatorsWhiteList.sorted()
|
|
|
|
spdir.data.keepItIf(validators.binarySearch(it.pubkey) != -1)
|
|
|
|
|
|
|
|
if spdir.data.len != validatorsWhiteList.len:
|
|
|
|
let exportedKeys = spdir.data.mapIt(it.pubkey).sorted()
|
|
|
|
for v in validators:
|
|
|
|
if exportedKeys.binarySearch(v) == -1:
|
|
|
|
warn "Specified validator key not found in the slashing database",
|
|
|
|
key = v.PubKeyBytes.toHex
|
|
|
|
|
|
|
|
Json.saveFile(path, spdir, prettify)
|
|
|
|
echo "Exported slashing protection DB to '", path, "'"
|