use isZeroMemory for Eth2Digest comparisons (#3386)
* use isZeroMemory for Eth2Digest comparisons * use Eth2Digest.isZero abstraction
This commit is contained in:
parent
1a0bcf0b02
commit
873a8ec1e6
|
@ -120,7 +120,7 @@ func isAncestorOf*(a, b: BlockRef): bool =
|
||||||
isAncestor
|
isAncestor
|
||||||
|
|
||||||
func link*(parent, child: BlockRef) =
|
func link*(parent, child: BlockRef) =
|
||||||
doAssert (not (parent.root == Eth2Digest() or child.root == Eth2Digest())),
|
doAssert (not (parent.root.isZero or child.root.isZero)),
|
||||||
"blocks missing root!"
|
"blocks missing root!"
|
||||||
doAssert parent.root != child.root, "self-references not allowed"
|
doAssert parent.root != child.root, "self-references not allowed"
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,7 @@ proc updateValidatorKeys*(dag: ChainDAGRef, validators: openArray[Validator]) =
|
||||||
proc updateFinalizedBlocks*(dag: ChainDAGRef) =
|
proc updateFinalizedBlocks*(dag: ChainDAGRef) =
|
||||||
template update(s: Slot) =
|
template update(s: Slot) =
|
||||||
if s < dag.tail.slot:
|
if s < dag.tail.slot:
|
||||||
if dag.backfillBlocks[s.int] != Eth2Digest():
|
if not dag.backfillBlocks[s.int].isZero:
|
||||||
dag.db.finalizedBlocks.insert(s, dag.backfillBlocks[s.int])
|
dag.db.finalizedBlocks.insert(s, dag.backfillBlocks[s.int])
|
||||||
else:
|
else:
|
||||||
let dagIndex = int(s - dag.tail.slot)
|
let dagIndex = int(s - dag.tail.slot)
|
||||||
|
@ -258,7 +258,7 @@ func getBlockIdAtSlot*(dag: ChainDAGRef, slot: Slot): BlockSlotId =
|
||||||
|
|
||||||
var pos = slot.int
|
var pos = slot.int
|
||||||
while pos >= dag.backfill.slot.int:
|
while pos >= dag.backfill.slot.int:
|
||||||
if dag.backfillBlocks[pos] != Eth2Digest():
|
if not dag.backfillBlocks[pos].isZero:
|
||||||
return BlockId(root: dag.backfillBlocks[pos], slot: Slot(pos)).atSlot(slot)
|
return BlockId(root: dag.backfillBlocks[pos], slot: Slot(pos)).atSlot(slot)
|
||||||
pos -= 1
|
pos -= 1
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ import
|
||||||
chronos, json, metrics, chronicles/timings, stint/endians2,
|
chronos, json, metrics, chronicles/timings, stint/endians2,
|
||||||
web3, web3/ethtypes as web3Types, web3/ethhexstrings, web3/engine_api,
|
web3, web3/ethtypes as web3Types, web3/ethhexstrings, web3/engine_api,
|
||||||
eth/common/eth_types,
|
eth/common/eth_types,
|
||||||
eth/async_utils, stew/[objects, byteutils, shims/hashes],
|
eth/async_utils, stew/[byteutils, shims/hashes],
|
||||||
# Local modules:
|
# Local modules:
|
||||||
../spec/[eth2_merkleization, forks, helpers],
|
../spec/[eth2_merkleization, forks, helpers],
|
||||||
../spec/datatypes/[base, phase0, bellatrix],
|
../spec/datatypes/[base, phase0, bellatrix],
|
||||||
|
@ -574,7 +574,7 @@ when hasDepositRootChecks:
|
||||||
try:
|
try:
|
||||||
let fetchedRoot = asEth2Digest(
|
let fetchedRoot = asEth2Digest(
|
||||||
awaitOrRaiseOnTimeout(depositRoot, contractCallTimeout))
|
awaitOrRaiseOnTimeout(depositRoot, contractCallTimeout))
|
||||||
if blk.voteData.deposit_root == default(Eth2Digest):
|
if blk.voteData.deposit_root.isZero:
|
||||||
blk.voteData.deposit_root = fetchedRoot
|
blk.voteData.deposit_root = fetchedRoot
|
||||||
result = Fetched
|
result = Fetched
|
||||||
elif blk.voteData.deposit_root == fetchedRoot:
|
elif blk.voteData.deposit_root == fetchedRoot:
|
||||||
|
|
|
@ -159,7 +159,7 @@ func process_attestation*(
|
||||||
block_root: Eth2Digest,
|
block_root: Eth2Digest,
|
||||||
target_epoch: Epoch
|
target_epoch: Epoch
|
||||||
) =
|
) =
|
||||||
if block_root == Eth2Digest():
|
if block_root.isZero:
|
||||||
return
|
return
|
||||||
|
|
||||||
## Add an attestation to the fork choice context
|
## Add an attestation to the fork choice context
|
||||||
|
@ -206,7 +206,7 @@ proc on_attestation*(
|
||||||
): FcResult[void] =
|
): FcResult[void] =
|
||||||
? self.update_time(dag, wallTime)
|
? self.update_time(dag, wallTime)
|
||||||
|
|
||||||
if beacon_block_root == Eth2Digest():
|
if beacon_block_root.isZero:
|
||||||
return ok()
|
return ok()
|
||||||
|
|
||||||
if attestation_slot < self.checkpoints.time.slotOrZero:
|
if attestation_slot < self.checkpoints.time.slotOrZero:
|
||||||
|
@ -472,7 +472,7 @@ func compute_deltas(
|
||||||
for val_index, vote in votes.mpairs():
|
for val_index, vote in votes.mpairs():
|
||||||
# No need to create a score change if the validator has never voted
|
# No need to create a score change if the validator has never voted
|
||||||
# or if votes are for the zero hash (alias to the genesis block)
|
# or if votes are for the zero hash (alias to the genesis block)
|
||||||
if vote.current_root == default(Eth2Digest) and vote.next_root == default(Eth2Digest):
|
if vote.current_root.isZero and vote.next_root.isZero:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# If the validator was not included in `old_balances` (i.e. did not exist)
|
# If the validator was not included in `old_balances` (i.e. did not exist)
|
||||||
|
|
|
@ -12,7 +12,7 @@ import
|
||||||
std/[options, tables, typetraits],
|
std/[options, tables, typetraits],
|
||||||
# Status libraries
|
# Status libraries
|
||||||
chronicles,
|
chronicles,
|
||||||
stew/[objects, results],
|
stew/results,
|
||||||
# Internal
|
# Internal
|
||||||
../spec/datatypes/base,
|
../spec/datatypes/base,
|
||||||
# Fork choice
|
# Fork choice
|
||||||
|
@ -169,7 +169,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
||||||
|
|
||||||
# Iterate backwards through all the indices in `self.nodes`
|
# Iterate backwards through all the indices in `self.nodes`
|
||||||
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
||||||
if node.root.isZeroMemory:
|
if node.root.isZero:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
var nodeDelta = deltas[nodePhysicalIdx]
|
var nodeDelta = deltas[nodePhysicalIdx]
|
||||||
|
@ -177,7 +177,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
||||||
# If we find the node for which the proposer boost was previously applied,
|
# If we find the node for which the proposer boost was previously applied,
|
||||||
# decrease the delta by the previous score amount.
|
# decrease the delta by the previous score amount.
|
||||||
if useProposerBoost and
|
if useProposerBoost and
|
||||||
(not self.previousProposerBoostRoot.isZeroMemory) and
|
(not self.previousProposerBoostRoot.isZero) and
|
||||||
self.previousProposerBoostRoot == node.root:
|
self.previousProposerBoostRoot == node.root:
|
||||||
if nodeDelta < 0 and
|
if nodeDelta < 0 and
|
||||||
nodeDelta - low(Delta) < self.previousProposerBoostScore:
|
nodeDelta - low(Delta) < self.previousProposerBoostScore:
|
||||||
|
@ -190,8 +190,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
||||||
# the delta by the new score amount.
|
# the delta by the new score amount.
|
||||||
#
|
#
|
||||||
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/fork-choice.md#get_latest_attesting_balance
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/fork-choice.md#get_latest_attesting_balance
|
||||||
if useProposerBoost and
|
if useProposerBoost and (not proposer_boost_root.isZero) and
|
||||||
(not proposer_boost_root.isZeroMemory) and
|
|
||||||
proposer_boost_root == node.root:
|
proposer_boost_root == node.root:
|
||||||
proposerBoostScore = calculateProposerBoost(newBalances)
|
proposerBoostScore = calculateProposerBoost(newBalances)
|
||||||
if nodeDelta >= 0 and
|
if nodeDelta >= 0 and
|
||||||
|
@ -251,7 +250,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
||||||
self.previousProposerBoostScore = proposerBoostScore
|
self.previousProposerBoostScore = proposerBoostScore
|
||||||
|
|
||||||
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
||||||
if node.root.isZeroMemory:
|
if node.root.isZero:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if node.parent.isSome():
|
if node.parent.isSome():
|
||||||
|
|
|
@ -338,7 +338,9 @@ proc validateBeaconBlock*(
|
||||||
# to the finalized checkpoint (else it wouldn't be in the DAG)
|
# to the finalized checkpoint (else it wouldn't be in the DAG)
|
||||||
return errIgnore("BeaconBlock: Can't find ancestor")
|
return errIgnore("BeaconBlock: Can't find ancestor")
|
||||||
|
|
||||||
if not (finalized_checkpoint.root in [ancestor.root, Eth2Digest()]):
|
if not (
|
||||||
|
finalized_checkpoint.root == ancestor.root or
|
||||||
|
finalized_checkpoint.root.isZero):
|
||||||
quarantine[].addUnviable(signed_beacon_block.root)
|
quarantine[].addUnviable(signed_beacon_block.root)
|
||||||
|
|
||||||
return errReject("BeaconBlock: Finalized checkpoint not an ancestor")
|
return errReject("BeaconBlock: Finalized checkpoint not an ancestor")
|
||||||
|
|
|
@ -27,7 +27,7 @@ import
|
||||||
# Status libraries
|
# Status libraries
|
||||||
chronicles,
|
chronicles,
|
||||||
nimcrypto/[sha2, hash],
|
nimcrypto/[sha2, hash],
|
||||||
stew/[endians2, byteutils],
|
stew/[byteutils, endians2, objects],
|
||||||
json_serialization,
|
json_serialization,
|
||||||
blscurve
|
blscurve
|
||||||
|
|
||||||
|
@ -111,6 +111,9 @@ func `==`*(a, b: Eth2Digest): bool =
|
||||||
# Eth2Digest is unnecessary - the type should never hold a secret!
|
# Eth2Digest is unnecessary - the type should never hold a secret!
|
||||||
equalMem(unsafeAddr a.data[0], unsafeAddr b.data[0], sizeof(a.data))
|
equalMem(unsafeAddr a.data[0], unsafeAddr b.data[0], sizeof(a.data))
|
||||||
|
|
||||||
|
func isZero*(x: Eth2Digest): bool =
|
||||||
|
x.isZeroMemory
|
||||||
|
|
||||||
proc writeValue*(w: var JsonWriter, a: Eth2Digest) {.raises: [Defect, IOError, SerializationError].} =
|
proc writeValue*(w: var JsonWriter, a: Eth2Digest) {.raises: [Defect, IOError, SerializationError].} =
|
||||||
w.writeValue $a
|
w.writeValue $a
|
||||||
|
|
||||||
|
|
|
@ -228,7 +228,7 @@ proc state_transition_block_aux(
|
||||||
|
|
||||||
# only blocks currently being produced have an empty state root - we use a
|
# only blocks currently being produced have an empty state root - we use a
|
||||||
# separate function for those
|
# separate function for those
|
||||||
doAssert signedBlock.message.state_root != Eth2Digest(),
|
doAssert not signedBlock.message.state_root.isZero,
|
||||||
"see makeBeaconBlock for block production"
|
"see makeBeaconBlock for block production"
|
||||||
state.root = signedBlock.message.state_root
|
state.root = signedBlock.message.state_root
|
||||||
|
|
||||||
|
|
|
@ -137,8 +137,8 @@ proc checkStatusMsg(state: BeaconSyncNetworkState, status: StatusMsg):
|
||||||
if status.finalizedEpoch <= dag.finalizedHead.slot.epoch:
|
if status.finalizedEpoch <= dag.finalizedHead.slot.epoch:
|
||||||
let blockId = dag.getBlockIdAtSlot(status.finalizedEpoch.start_slot())
|
let blockId = dag.getBlockIdAtSlot(status.finalizedEpoch.start_slot())
|
||||||
if status.finalizedRoot != blockId.bid.root and
|
if status.finalizedRoot != blockId.bid.root and
|
||||||
blockId.bid.root != Eth2Digest() and
|
(not blockId.bid.root.isZero) and
|
||||||
status.finalizedRoot != Eth2Digest():
|
(not status.finalizedRoot.isZero):
|
||||||
return err("peer following different finality")
|
return err("peer following different finality")
|
||||||
|
|
||||||
ok()
|
ok()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# beacon_chain
|
# beacon_chain
|
||||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
||||||
# Licensed and distributed under either of
|
# Licensed and distributed under either of
|
||||||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
# * 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).
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
|
@ -42,7 +42,7 @@ suite "Specific field types":
|
||||||
t = default(type t)
|
t = default(type t)
|
||||||
readSszBytes(encoded, t, false)
|
readSszBytes(encoded, t, false)
|
||||||
check:
|
check:
|
||||||
t.root == Eth2Digest()
|
t.root.isZero
|
||||||
|
|
||||||
testit(phase0.SignedBeaconBlock)
|
testit(phase0.SignedBeaconBlock)
|
||||||
testit(phase0.TrustedSignedBeaconBlock)
|
testit(phase0.TrustedSignedBeaconBlock)
|
||||||
|
|
Loading…
Reference in New Issue