generic putCorruptState; {Merge=>Bellatrix}BeaconStateNoImmutableValidators (#3427)

This commit is contained in:
tersec 2022-02-21 11:55:56 +00:00 committed by GitHub
parent 9d392711fa
commit 7de3f00f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 29 additions and 34 deletions

View File

@ -634,8 +634,8 @@ template toBeaconStateNoImmutableValidators(state: altair.BeaconState):
isomorphicCast[AltairBeaconStateNoImmutableValidators](state)
template toBeaconStateNoImmutableValidators(state: bellatrix.BeaconState):
MergeBeaconStateNoImmutableValidators =
isomorphicCast[MergeBeaconStateNoImmutableValidators](state)
BellatrixBeaconStateNoImmutableValidators =
isomorphicCast[BellatrixBeaconStateNoImmutableValidators](state)
proc putState*(db: BeaconChainDB, key: Eth2Digest, value: ForkyBeaconState) =
db.updateImmutableValidators(value.validators.asSeq())
@ -648,14 +648,9 @@ proc putState*(db: BeaconChainDB, state: ForkyHashedBeaconState) =
db.putState(state.root, state.data)
# For testing rollback
proc putCorruptPhase0State*(db: BeaconChainDB, key: Eth2Digest) =
db.statesNoVal[BeaconStateFork.Phase0].putSnappySSZ(key.data, Validator())
proc putCorruptAltairState*(db: BeaconChainDB, key: Eth2Digest) =
db.statesNoVal[BeaconStateFork.Altair].putSnappySSZ(key.data, Validator())
proc putCorruptMergeState*(db: BeaconChainDB, key: Eth2Digest) =
db.statesNoVal[BeaconStateFork.Bellatrix].putSnappySSZ(key.data, Validator())
proc putCorruptState*(
db: BeaconChainDB, fork: static BeaconStateFork, key: Eth2Digest) =
db.statesNoVal[fork].putSnappySSZ(key.data, Validator())
func stateRootKey(root: Eth2Digest, slot: Slot): array[40, byte] =
var ret: array[40, byte]

View File

@ -126,7 +126,7 @@ type
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/bellatrix/beacon-chain.md#beaconstate
# Memory-representation-equivalent to a Merge BeaconState for in-place SSZ
# reading and writing
MergeBeaconStateNoImmutableValidators* = object
BellatrixBeaconStateNoImmutableValidators* = object
# Versioning
genesis_time*: uint64
genesis_validators_root*: Eth2Digest

View File

@ -153,7 +153,7 @@ func computeAggregateSig(votes: seq[TrustedSyncCommitteeMsg],
if initialized:
contribution.signature = aggregateSig.finish.toValidatorSig
return initialized
initialized
func produceContribution*(
pool: SyncCommitteeMsgPool,

View File

@ -62,7 +62,7 @@ func `[]`(nodes: ProtoNodes, idx: Index): Option[ProtoNode] =
let i = idx - nodes.offset
if i >= nodes.buf.len:
return none(ProtoNode)
return some(nodes.buf[i])
some(nodes.buf[i])
func len*(nodes: ProtoNodes): int =
nodes.buf.len
@ -535,19 +535,19 @@ when isMainModule:
echo "Sanity checks on fork choice tiebreaks"
block:
let a = Eth2Digest.fromHex("0x0000000000000001000000000000000000000000000000000000000000000000")
let b = Eth2Digest.fromHex("0x0000000000000000000000000000000000000000000000000000000000000000") # sha256(1)
const a = Eth2Digest.fromHex("0x0000000000000001000000000000000000000000000000000000000000000000")
const b = Eth2Digest.fromHex("0x0000000000000000000000000000000000000000000000000000000000000000") # sha256(1)
doAssert tiebreak(a, b)
block:
let a = Eth2Digest.fromHex("0x0000000000000002000000000000000000000000000000000000000000000000")
let b = Eth2Digest.fromHex("0x0000000000000001000000000000000000000000000000000000000000000000") # sha256(1)
const a = Eth2Digest.fromHex("0x0000000000000002000000000000000000000000000000000000000000000000")
const b = Eth2Digest.fromHex("0x0000000000000001000000000000000000000000000000000000000000000000") # sha256(1)
doAssert tiebreak(a, b)
block:
let a = Eth2Digest.fromHex("0xD86E8112F3C4C4442126F8E9F44F16867DA487F29052BF91B810457DB34209A4") # sha256(2)
let b = Eth2Digest.fromHex("0x7C9FA136D4413FA6173637E883B6998D32E1D675F88CDDFF9DCBCF331820F4B8") # sha256(1)
const a = Eth2Digest.fromHex("0xD86E8112F3C4C4442126F8E9F44F16867DA487F29052BF91B810457DB34209A4") # sha256(2)
const b = Eth2Digest.fromHex("0x7C9FA136D4413FA6173637E883B6998D32E1D675F88CDDFF9DCBCF331820F4B8") # sha256(1)
doAssert tiebreak(a, b)

View File

@ -1,5 +1,5 @@
# beacon_chain
# Copyright (c) 2018-2021 Status Research & Development GmbH
# Copyright (c) 2018-2022 Status Research & Development GmbH
# 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).
@ -191,13 +191,13 @@ proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata {.raises:
fatal "The selected network is not compatible with the current build",
reason = metadata.incompatibilityDesc
quit 1
return metadata
metadata
proc getRuntimeConfig*(
eth2Network: Option[string]): RuntimeConfig {.raises: [Defect, IOError].} =
if eth2Network.isSome:
return getMetadataForNetwork(eth2Network.get).cfg
return defaultRuntimeConfig
defaultRuntimeConfig
proc extractGenesisValidatorRootFromSnapshot*(
snapshot: string): Eth2Digest {.raises: [Defect, IOError, SszError].} =

View File

@ -564,7 +564,7 @@ proc init*(T: type BeaconNode,
node.updateValidatorMetrics()
return node
node
func verifyFinalization(node: BeaconNode, slot: Slot) =
# Epoch must be >= 4 to check finalization

View File

@ -264,7 +264,7 @@ func findValidatorIndex*(state: ForkyBeaconState, pubkey: ValidatorPubKey): int
for i in 0 ..< state.validators.len:
if state.validators.asSeq[i].pubkey == pubkey:
return i
return -1
-1
proc process_deposit*(cfg: RuntimeConfig,
state: var ForkyBeaconState,

View File

@ -96,7 +96,7 @@ proc getAggregatedFilesLastEpoch*(dir: string): Epoch =
fn[epochInfoFileNameDigitsCount + 1 .. 2 * epochInfoFileNameDigitsCount])
if fileLastEpoch > largestEpochInFileName:
largestEpochInFileName = fileLastEpoch
return largestEpochInFileName.Epoch
largestEpochInFileName.Epoch
func epochAsString*(epoch: Epoch): string =
let strEpoch = $epoch
@ -194,7 +194,7 @@ func getFinalizedCheckpoint(state: ForkyBeaconState,
old_current_justified_checkpoint.epoch + 1 == current_epoch:
return old_current_justified_checkpoint
return state.finalized_checkpoint
state.finalized_checkpoint
func getFinalizedCheckpoint(state: phase0.BeaconState, balances: TotalBalances):
Checkpoint =

View File

@ -196,7 +196,7 @@ proc stepOnBlock(
)
doAssert status.isOk()
return blockAdded
blockAdded
proc stepOnAttestation(
dag: ChainDagRef,

View File

@ -35,7 +35,7 @@ proc getAltairStateRef(db: BeaconChainDB, root: Eth2Digest):
if db.getState(root, res[], noRollback):
return res
proc getMergeStateRef(db: BeaconChainDB, root: Eth2Digest):
proc getBellatrixStateRef(db: BeaconChainDB, root: Eth2Digest):
bellatrix.NilableBeaconStateRef =
# load beaconstate the way the block pool does it - into an existing instance
let res = (bellatrix.BeaconStateRef)()
@ -76,7 +76,7 @@ proc getTestStates(stateFork: BeaconStateFork): auto =
testStates
# Each of phase 0/altair/merge states gets used twice, so make them global to
# Each of phase 0/altair/bellatrix states gets used twice, so scope them to
# module
let
testStatesPhase0 = getTestStates(BeaconStateFork.Phase0)
@ -253,12 +253,12 @@ suite "Beacon chain DB" & preset():
check:
db.containsState(root)
hash_tree_root(db.getMergeStateRef(root)[]) == root
hash_tree_root(db.getBellatrixStateRef(root)[]) == root
db.delState(root)
check:
not db.containsState(root)
db.getMergeStateRef(root).isNil
db.getBellatrixStateRef(root).isNil
db.close()
@ -333,7 +333,7 @@ suite "Beacon chain DB" & preset():
slot: 10.Slot)))
root = Eth2Digest()
db.putCorruptPhase0State(root)
db.putCorruptState(BeaconStateFork.Phase0, root)
let restoreAddr = addr dag.headState
@ -356,7 +356,7 @@ suite "Beacon chain DB" & preset():
slot: 10.Slot)))
root = Eth2Digest()
db.putCorruptAltairState(root)
db.putCorruptState(BeaconStateFork.Altair, root)
let restoreAddr = addr dag.headState
@ -382,7 +382,7 @@ suite "Beacon chain DB" & preset():
slot: 10.Slot)))
root = Eth2Digest()
db.putCorruptMergeState(root)
db.putCorruptState(BeaconStateFork.Bellatrix, root)
let restoreAddr = addr dag.headState