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
|
|
|
|
std/[typetraits, strutils, algorithm],
|
|
|
|
# Status
|
2024-01-16 22:37:14 +00:00
|
|
|
stew/byteutils,
|
|
|
|
results,
|
2021-02-09 15:23:06 +00:00
|
|
|
serialization,
|
2023-05-31 15:51:00 +00:00
|
|
|
json_serialization, json_serialization/std/options,
|
2021-02-09 15:23:06 +00:00
|
|
|
chronicles,
|
|
|
|
# Internal
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/datatypes/base
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2023-05-31 15:51:00 +00:00
|
|
|
export options, serialization, json_serialization # Generic sandwich https://github.com/nim-lang/Nim/issues/11225
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
# Slashing Protection Interop
|
|
|
|
# --------------------------------------------
|
|
|
|
# We use the SPDIR type as an intermediate representation
|
|
|
|
# between database versions and to generate
|
|
|
|
# the serialized interchanged format.
|
|
|
|
#
|
|
|
|
# References: https://eips.ethereum.org/EIPS/eip-3076
|
|
|
|
#
|
|
|
|
# SPDIR: Nimbus-specific, Slashing Protection Database Intermediate Representation
|
2023-05-31 15:51:00 +00:00
|
|
|
# SPDIF: Cross-client, JSON, Slashing Protection Database Interchange Format
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SPDIR* = object
|
|
|
|
## Slashing Protection Database Interchange Format
|
|
|
|
metadata*: SPDIR_Meta
|
|
|
|
data*: seq[SPDIR_Validator]
|
|
|
|
|
|
|
|
Eth2Digest0x* = distinct Eth2Digest
|
|
|
|
## The spec mandates "0x" prefix on serialization
|
|
|
|
## So we need to set custom read/write
|
|
|
|
|
|
|
|
PubKeyBytes* = array[RawPubKeySize, byte]
|
|
|
|
## This is the serialized byte representation
|
|
|
|
## of a Validator Public Key.
|
|
|
|
## Portable between Miracl/BLST
|
|
|
|
## and limits serialization/deserialization call
|
|
|
|
|
|
|
|
PubKey0x* = distinct PubKeyBytes
|
|
|
|
## The spec mandates "0x" prefix on serialization
|
|
|
|
## So we need to set custom read/write
|
|
|
|
## We also assume that pubkeys in the database
|
|
|
|
## are valid points on the BLS12-381 G1 curve
|
|
|
|
## (so we skip fromRaw/serialization checks)
|
|
|
|
|
|
|
|
SlotString* = distinct Slot
|
|
|
|
## The spec mandates string serialization for wide compatibility (javascript)
|
|
|
|
EpochString* = distinct Epoch
|
|
|
|
## The spec mandates string serialization for wide compatibility (javascript)
|
|
|
|
|
|
|
|
SPDIR_Meta* = object
|
|
|
|
interchange_format_version*: string
|
|
|
|
genesis_validators_root*: Eth2Digest0x
|
|
|
|
|
|
|
|
SPDIR_Validator* = object
|
|
|
|
pubkey*: PubKey0x
|
|
|
|
signed_blocks*: seq[SPDIR_SignedBlock]
|
|
|
|
signed_attestations*: seq[SPDIR_SignedAttestation]
|
|
|
|
|
|
|
|
SPDIR_SignedBlock* = object
|
|
|
|
slot*: SlotString
|
2023-05-31 15:51:00 +00:00
|
|
|
signing_root*: Option[Eth2Digest0x] # compute_signing_root(block, domain)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
SPDIR_SignedAttestation* = object
|
|
|
|
source_epoch*: EpochString
|
|
|
|
target_epoch*: EpochString
|
2023-05-31 15:51:00 +00:00
|
|
|
signing_root*: Option[Eth2Digest0x] # compute_signing_root(attestation, domain)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
# Slashing Protection types
|
|
|
|
# --------------------------------------------
|
|
|
|
|
|
|
|
SlashingImportStatus* = enum
|
|
|
|
siSuccess
|
|
|
|
siFailure
|
|
|
|
siPartial
|
|
|
|
|
|
|
|
BadVoteKind* = enum
|
|
|
|
## Attestation bad vote kind
|
|
|
|
# h: height (i.e. epoch for attestation, slot for blocks)
|
|
|
|
# t: target
|
|
|
|
# s: source
|
|
|
|
# 1: existing attestations
|
|
|
|
# 2: candidate attestation
|
|
|
|
|
|
|
|
# Spec slashing condition
|
2021-05-04 13:17:28 +00:00
|
|
|
DoubleVote # h(t1) == h(t2)
|
|
|
|
SurroundVote # h(s1) < h(s2) < h(t2) < h(t1) or h(s2) < h(s1) < h(t1) < h(t2)
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
# Non-spec, should never happen in a well functioning client
|
|
|
|
TargetPrecedesSource # h(t1) < h(s1) - current epoch precedes last justified epoch
|
|
|
|
|
|
|
|
# EIP-3067 (https://eips.ethereum.org/EIPS/eip-3076)
|
|
|
|
MinSourceViolation # h(s2) < h(s1) - EIP3067 condition 4 (strict inequality)
|
|
|
|
MinTargetViolation # h(t2) <= h(t1) - EIP3067 condition 5
|
2021-05-04 13:17:28 +00:00
|
|
|
DatabaseError # Cannot read/write the slashing protection db
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
BadVote* {.pure.} = object
|
2021-02-09 15:23:06 +00:00
|
|
|
case kind*: BadVoteKind
|
|
|
|
of DoubleVote:
|
|
|
|
existingAttestation*: Eth2Digest
|
2021-05-04 13:17:28 +00:00
|
|
|
of SurroundVote:
|
2021-02-09 15:23:06 +00:00
|
|
|
existingAttestationRoot*: Eth2Digest # Many roots might be in conflict
|
|
|
|
sourceExisting*, targetExisting*: Epoch
|
|
|
|
sourceSlashable*, targetSlashable*: Epoch
|
|
|
|
of TargetPrecedesSource:
|
|
|
|
discard
|
|
|
|
of MinSourceViolation:
|
|
|
|
minSource*: Epoch
|
|
|
|
candidateSource*: Epoch
|
|
|
|
of MinTargetViolation:
|
|
|
|
minTarget*: Epoch
|
|
|
|
candidateTarget*: Epoch
|
2021-05-04 13:17:28 +00:00
|
|
|
of BadVoteKind.DatabaseError:
|
|
|
|
message*: string
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
BadProposalKind* {.pure.} = enum
|
2021-02-09 15:23:06 +00:00
|
|
|
# Spec slashing condition
|
|
|
|
DoubleProposal # h(t1) == h(t2)
|
|
|
|
# EIP-3067 (https://eips.ethereum.org/EIPS/eip-3076)
|
|
|
|
MinSlotViolation # h(t2) <= h(t1)
|
2021-05-04 13:17:28 +00:00
|
|
|
DatabaseError # Cannot read/write the slashing protection db
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
BadProposal* = object
|
|
|
|
case kind*: BadProposalKind
|
|
|
|
of DoubleProposal:
|
|
|
|
existingBlock*: Eth2Digest
|
|
|
|
of MinSlotViolation:
|
|
|
|
minSlot*: Slot
|
|
|
|
candidateSlot*: Slot
|
2021-05-04 13:17:28 +00:00
|
|
|
of BadProposalKind.DatabaseError:
|
|
|
|
message*: string
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2023-10-04 08:26:52 +00:00
|
|
|
{.push warning[ProveField]:off.}
|
2021-02-09 15:23:06 +00:00
|
|
|
func `==`*(a, b: BadVote): bool =
|
|
|
|
## Comparison operator.
|
|
|
|
## Used implictily by Result when comparing the
|
|
|
|
## result of multiple DB versions
|
|
|
|
if a.kind != b.kind:
|
|
|
|
false
|
2021-05-04 13:17:28 +00:00
|
|
|
else:
|
|
|
|
case a.kind
|
|
|
|
of DoubleVote:
|
|
|
|
a.existingAttestation == b.existingAttestation
|
|
|
|
of SurroundVote:
|
|
|
|
(a.existingAttestationRoot == b.existingAttestationRoot) and
|
|
|
|
(a.sourceExisting == b.sourceExisting) and
|
|
|
|
(a.targetExisting == b.targetExisting) and
|
|
|
|
(a.sourceSlashable == b.sourceSlashable) and
|
|
|
|
(a.targetSlashable == b.targetSlashable)
|
|
|
|
of TargetPrecedesSource:
|
|
|
|
true
|
|
|
|
of MinSourceViolation:
|
|
|
|
(a.minSource == b.minSource) and
|
|
|
|
(a.candidateSource == b.candidateSource)
|
|
|
|
of MinTargetViolation:
|
|
|
|
(a.minTarget == b.minTarget) and
|
|
|
|
(a.candidateTarget == b.candidateTarget)
|
|
|
|
of BadVoteKind.DatabaseError:
|
|
|
|
true
|
2023-10-04 08:26:52 +00:00
|
|
|
{.pop.}
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2021-05-19 06:38:13 +00:00
|
|
|
template `==`*(a, b: PubKey0x): bool =
|
|
|
|
PubKeyBytes(a) == PubKeyBytes(b)
|
|
|
|
|
|
|
|
template `<`*(a, b: PubKey0x): bool =
|
|
|
|
PubKeyBytes(a) < PubKeyBytes(b)
|
|
|
|
|
|
|
|
template cmp*(a, b: PubKey0x): bool =
|
|
|
|
cmp(PubKeyBytes(a), PubKeyBytes(b))
|
|
|
|
|
2023-10-04 08:26:52 +00:00
|
|
|
{.push warning[ProveField]:off.}
|
2021-02-09 15:23:06 +00:00
|
|
|
func `==`*(a, b: BadProposal): bool =
|
|
|
|
## Comparison operator.
|
|
|
|
## Used implictily by Result when comparing the
|
|
|
|
## result of multiple DB versions
|
|
|
|
##
|
|
|
|
## Except that V1 doesn't support low-watermark...
|
|
|
|
if a.kind != b.kind:
|
|
|
|
false
|
|
|
|
elif a.kind == DoubleProposal:
|
|
|
|
a.existingBlock == b.existingBlock
|
|
|
|
elif a.kind == MinSlotViolation:
|
|
|
|
a.minSlot == b.minSlot and
|
|
|
|
a.candidateSlot == b.candidateSlot
|
|
|
|
else: # Unreachable
|
|
|
|
false
|
2023-10-04 08:26:52 +00:00
|
|
|
{.pop.}
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
# Serialization
|
|
|
|
# --------------------------------------------
|
|
|
|
|
2023-08-19 15:11:56 +00:00
|
|
|
proc writeValue*(
|
|
|
|
writer: var JsonWriter, value: PubKey0x) {.inline, raises: [IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
writer.writeValue("0x" & value.PubKeyBytes.toHex())
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var PubKey0x)
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [SerializationError, IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
try:
|
2021-05-19 06:38:13 +00:00
|
|
|
value = PubKey0x hexToByteArray(reader.readValue(string), RawPubKeySize)
|
2021-02-09 15:23:06 +00:00
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader, "Hex string expected")
|
|
|
|
|
2023-08-19 15:11:56 +00:00
|
|
|
proc writeValue*(
|
|
|
|
w: var JsonWriter, a: Eth2Digest0x) {.inline, raises: [IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
w.writeValue "0x" & a.Eth2Digest.data.toHex()
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader, a: var Eth2Digest0x)
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [SerializationError, IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
try:
|
|
|
|
a = Eth2Digest0x fromHex(Eth2Digest, r.readValue(string))
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(r, "Hex string expected")
|
|
|
|
|
2023-08-19 15:11:56 +00:00
|
|
|
proc writeValue*(
|
|
|
|
w: var JsonWriter, a: SlotString or EpochString
|
|
|
|
) {.inline, raises: [IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
w.writeValue $distinctBase(a)
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader, a: var (SlotString or EpochString))
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [SerializationError, IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
try:
|
2022-04-08 16:22:49 +00:00
|
|
|
a = (typeof a)(r.readValue(string).parseBiggestUInt())
|
2021-02-09 15:23:06 +00:00
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(r, "Integer in a string expected")
|
|
|
|
|
|
|
|
proc importSlashingInterchange*(
|
2021-05-04 13:17:28 +00:00
|
|
|
db: auto,
|
2023-08-25 09:29:07 +00:00
|
|
|
path: string): SlashingImportStatus {.raises: [IOError, SerializationError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
## Import a Slashing Protection Database Interchange Format
|
|
|
|
## into a Nimbus DB.
|
|
|
|
## This adds data to already existing data.
|
|
|
|
let spdir = Json.loadFile(path, SPDIR)
|
|
|
|
return db.inclSPDIR(spdir)
|
|
|
|
|
|
|
|
# Logging
|
|
|
|
# --------------------------------------------
|
|
|
|
|
2023-05-31 15:51:00 +00:00
|
|
|
func shortLog*(v: Option[Eth2Digest0x]): auto =
|
|
|
|
(
|
|
|
|
if v.isSome:
|
|
|
|
v.get.Eth2Digest.shortLog
|
|
|
|
else:
|
|
|
|
"none"
|
|
|
|
)
|
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
func shortLog*(v: SPDIR_SignedBlock): auto =
|
|
|
|
(
|
|
|
|
slot: shortLog(v.slot.Slot),
|
2023-05-31 15:51:00 +00:00
|
|
|
signing_root: shortLog(v.signing_root)
|
2021-02-09 15:23:06 +00:00
|
|
|
)
|
|
|
|
func shortLog*(v: SPDIR_SignedAttestation): auto =
|
|
|
|
(
|
|
|
|
source_epoch: shortLog(v.source_epoch.Epoch),
|
|
|
|
target_epoch: shortLog(v.target_epoch.Epoch),
|
2023-05-31 15:51:00 +00:00
|
|
|
signing_root: shortLog(v.signing_root)
|
2021-02-09 15:23:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
chronicles.formatIt SlotString: it.Slot.shortLog
|
|
|
|
chronicles.formatIt EpochString: it.Slot.shortLog
|
|
|
|
chronicles.formatIt Eth2Digest0x: it.Eth2Digest.shortLog
|
|
|
|
chronicles.formatIt SPDIR_SignedBlock: it.shortLog
|
|
|
|
chronicles.formatIt SPDIR_SignedAttestation: it.shortLog
|
|
|
|
|
|
|
|
# Interchange import
|
|
|
|
# --------------------------------------------
|
|
|
|
|
|
|
|
proc importInterchangeV5Impl*(
|
2021-05-04 13:17:28 +00:00
|
|
|
db: auto,
|
2021-02-09 15:23:06 +00:00
|
|
|
spdir: var SPDIR
|
|
|
|
): SlashingImportStatus
|
2023-08-25 09:29:07 +00:00
|
|
|
{.raises: [SerializationError, IOError].} =
|
2021-02-09 15:23:06 +00:00
|
|
|
## Common implementation of interchange import
|
|
|
|
## according to https://eips.ethereum.org/EIPS/eip-3076
|
|
|
|
## spdir needs to be `var` as it will be sorted in-place
|
|
|
|
|
|
|
|
result = siSuccess
|
|
|
|
|
|
|
|
for v in 0 ..< spdir.data.len:
|
|
|
|
let parsedKey = block:
|
|
|
|
let key = ValidatorPubKey.fromRaw(spdir.data[v].pubkey.PubKeyBytes)
|
|
|
|
if key.isErr:
|
|
|
|
# The bytes does not describe a valid encoding (length error)
|
|
|
|
error "Invalid public key.",
|
|
|
|
pubkey = "0x" & spdir.data[v].pubkey.PubKeyBytes.toHex()
|
|
|
|
|
|
|
|
result = siPartial
|
|
|
|
continue
|
2021-06-01 11:13:40 +00:00
|
|
|
if key.get().load().isNone():
|
2021-02-09 15:23:06 +00:00
|
|
|
# The bytes don't deserialize to a valid BLS G1 elliptic curve point.
|
|
|
|
# Deserialization is costly but done only once per validator.
|
|
|
|
# and SlashingDB import is a very rare event.
|
|
|
|
error "Invalid public key.",
|
|
|
|
pubkey = "0x" & spdir.data[v].pubkey.PubKeyBytes.toHex()
|
|
|
|
|
|
|
|
result = siPartial
|
|
|
|
continue
|
|
|
|
key.get()
|
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
# TODO: with minification sorting is unnecessary, cleanup
|
2021-02-09 15:23:06 +00:00
|
|
|
# Sort by ascending minimum slot so that we don't trigger MinSlotViolation
|
|
|
|
spdir.data[v].signed_blocks.sort do (a, b: SPDIR_SignedBlock) -> int:
|
|
|
|
result = cmp(a.slot.int, b.slot.int)
|
|
|
|
|
|
|
|
spdir.data[v].signed_attestations.sort do (a, b: SPDIR_SignedAttestation) -> int:
|
|
|
|
result = cmp(a.source_epoch.int, b.source_epoch.int)
|
|
|
|
if result == 0: # Same epoch
|
|
|
|
result = cmp(a.target_epoch.int, b.target_epoch.int)
|
|
|
|
|
|
|
|
const ZeroDigest = Eth2Digest()
|
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
let (dbSlot, dbSource, dbTarget) = db.retrieveLatestValidatorData(parsedKey)
|
|
|
|
|
2021-02-09 15:23:06 +00:00
|
|
|
# Blocks
|
|
|
|
# ---------------------------------------------------
|
|
|
|
# After import we need to prune the DB from everything
|
|
|
|
# besides the last imported block slot.
|
|
|
|
# This ensures that even if 2 slashing DB are imported in the wrong order
|
|
|
|
# (the last before the earliest) the minSlotViolation check stays consistent.
|
|
|
|
var maxValidSlotSeen = -1
|
2022-03-04 14:43:34 +00:00
|
|
|
if dbSlot.isSome():
|
|
|
|
maxValidSlotSeen = int dbSlot.get()
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
if spdir.data[v].signed_blocks.len >= 1:
|
2023-05-31 15:51:00 +00:00
|
|
|
# Minification, to limit SQLite IO we only import the last block after sorting
|
2022-03-04 14:43:34 +00:00
|
|
|
template B: untyped = spdir.data[v].signed_blocks[^1]
|
2023-05-31 15:51:00 +00:00
|
|
|
let
|
|
|
|
signing_root =
|
|
|
|
if B.signing_root.isSome:
|
|
|
|
B.signing_root.get.Eth2Digest
|
|
|
|
else:
|
|
|
|
# https://eips.ethereum.org/EIPS/eip-3076#advice-for-complete-databases
|
|
|
|
# "If your database records the signing roots of messages in
|
|
|
|
# addition to their slot/epochs, you should ensure that imported
|
|
|
|
# messages without signing roots are assigned a suitable dummy
|
|
|
|
# signing root internally. We suggest using a special "null" value
|
|
|
|
# which is distinct from all other signing roots, although a value
|
|
|
|
# like 0x0 may be used instead (as it is extremely unlikely to
|
|
|
|
# collide with any real signing root)."
|
|
|
|
ZeroDigest
|
|
|
|
status = db.registerBlock(parsedKey, B.slot.Slot, signing_root)
|
2021-02-09 15:23:06 +00:00
|
|
|
if status.isErr():
|
|
|
|
# We might be importing a duplicate which EIP-3076 allows
|
|
|
|
# there is no reason during normal operation to integrate
|
|
|
|
# a duplicate so checkSlashableBlockProposal would have rejected it.
|
|
|
|
# We special-case that for imports.
|
|
|
|
# Note: rule 2 mentions repeat signing in the MinSlotViolation case
|
|
|
|
# having 2 blocks with the same signing root and different slots
|
|
|
|
# would break the blockchain so we only check for exact slot.
|
|
|
|
if status.error.kind == DoubleProposal and
|
2023-05-31 15:51:00 +00:00
|
|
|
signing_root != ZeroDigest and
|
|
|
|
status.error.existingBlock == signing_root:
|
2021-02-09 15:23:06 +00:00
|
|
|
warn "Block already exists in the DB",
|
|
|
|
pubkey = spdir.data[v].pubkey.PubKeyBytes.toHex(),
|
|
|
|
candidateBlock = B
|
|
|
|
else:
|
|
|
|
error "Slashable block. Skipping its import.",
|
|
|
|
pubkey = spdir.data[v].pubkey.PubKeyBytes.toHex(),
|
|
|
|
candidateBlock = B,
|
|
|
|
conflict = status.error()
|
|
|
|
result = siPartial
|
|
|
|
|
|
|
|
if B.slot.int > maxValidSlotSeen:
|
2022-03-04 14:43:34 +00:00
|
|
|
maxValidSlotSeen = int B.slot
|
2021-02-09 15:23:06 +00:00
|
|
|
|
|
|
|
# Now prune everything that predates
|
2022-03-04 14:43:34 +00:00
|
|
|
# this DB or interchange file max slot
|
|
|
|
# Even if the block is not imported, pruning will keep the latest one.
|
2021-02-09 15:23:06 +00:00
|
|
|
db.pruneBlocks(parsedKey, Slot maxValidSlotSeen)
|
|
|
|
|
|
|
|
# Attestations
|
|
|
|
# ---------------------------------------------------
|
|
|
|
# After import we need to prune the DB from everything
|
|
|
|
# besides the last imported attestation source and target epochs.
|
|
|
|
# This ensures that even if 2 slashing DB are imported in the wrong order
|
|
|
|
# (the last before the earliest) the minEpochViolation check stays consistent.
|
|
|
|
var maxValidSourceEpochSeen = -1
|
|
|
|
var maxValidTargetEpochSeen = -1
|
2022-04-08 16:22:49 +00:00
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
if dbSource.isSome():
|
|
|
|
maxValidSourceEpochSeen = int dbSource.get()
|
|
|
|
if dbTarget.isSome():
|
|
|
|
maxValidTargetEpochSeen = int dbTarget.get()
|
2021-02-09 15:23:06 +00:00
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
# We do a first pass over the data to find the max source/target seen
|
2021-02-09 15:23:06 +00:00
|
|
|
for a in 0 ..< spdir.data[v].signed_attestations.len:
|
|
|
|
template A: untyped = spdir.data[v].signed_attestations[a]
|
|
|
|
|
|
|
|
if A.source_epoch.int > maxValidSourceEpochSeen:
|
|
|
|
maxValidSourceEpochSeen = A.source_epoch.int
|
|
|
|
if A.target_epoch.int > maxValidTargetEpochSeen:
|
|
|
|
maxValidTargetEpochSeen = A.target_epoch.int
|
|
|
|
|
2021-03-10 15:53:42 +00:00
|
|
|
if maxValidSourceEpochSeen < 0 or maxValidTargetEpochSeen < 0:
|
|
|
|
doAssert maxValidSourceEpochSeen == -1 and maxValidTargetEpochSeen == -1
|
2022-01-20 16:14:06 +00:00
|
|
|
notice "No attestation found in slashing interchange file for validator",
|
|
|
|
pubkey = spdir.data[v].pubkey.PubKeyBytes.toHex()
|
|
|
|
continue
|
2022-03-04 14:43:34 +00:00
|
|
|
|
|
|
|
# See formal proof https://github.com/michaelsproul/slashing-proofs
|
|
|
|
# of synthetic attestation
|
2022-04-08 16:22:49 +00:00
|
|
|
if not(maxValidSourceEpochSeen < maxValidTargetEpochSeen) and
|
2022-03-04 14:43:34 +00:00
|
|
|
not(maxValidSourceEpochSeen == 0 and maxValidTargetEpochSeen == 0):
|
|
|
|
# Special-case genesis (Slashing prot is deactivated anyway)
|
|
|
|
warn "Invalid attestation(s), source epochs should be less than target epochs, skipping import",
|
|
|
|
pubkey = spdir.data[v].pubkey.PubKeyBytes.toHex(),
|
|
|
|
maxValidSourceEpochSeen = maxValidSourceEpochSeen,
|
|
|
|
maxValidTargetEpochSeen = maxValidTargetEpochSeen
|
|
|
|
result = siPartial
|
|
|
|
continue
|
2022-04-08 16:22:49 +00:00
|
|
|
|
2022-03-04 14:43:34 +00:00
|
|
|
db.registerSyntheticAttestation(
|
|
|
|
parsedKey,
|
|
|
|
Epoch maxValidSourceEpochSeen,
|
|
|
|
Epoch maxValidTargetEpochSeen
|
|
|
|
)
|
|
|
|
|
2021-03-10 15:53:42 +00:00
|
|
|
db.pruneAttestations(parsedKey, maxValidSourceEpochSeen, maxValidTargetEpochSeen)
|