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
|
||||
|
||||
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!"
|
||||
doAssert parent.root != child.root, "self-references not allowed"
|
||||
|
||||
|
|
|
@ -108,7 +108,7 @@ proc updateValidatorKeys*(dag: ChainDAGRef, validators: openArray[Validator]) =
|
|||
proc updateFinalizedBlocks*(dag: ChainDAGRef) =
|
||||
template update(s: 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])
|
||||
else:
|
||||
let dagIndex = int(s - dag.tail.slot)
|
||||
|
@ -258,7 +258,7 @@ func getBlockIdAtSlot*(dag: ChainDAGRef, slot: Slot): BlockSlotId =
|
|||
|
||||
var pos = 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)
|
||||
pos -= 1
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ import
|
|||
chronos, json, metrics, chronicles/timings, stint/endians2,
|
||||
web3, web3/ethtypes as web3Types, web3/ethhexstrings, web3/engine_api,
|
||||
eth/common/eth_types,
|
||||
eth/async_utils, stew/[objects, byteutils, shims/hashes],
|
||||
eth/async_utils, stew/[byteutils, shims/hashes],
|
||||
# Local modules:
|
||||
../spec/[eth2_merkleization, forks, helpers],
|
||||
../spec/datatypes/[base, phase0, bellatrix],
|
||||
|
@ -574,7 +574,7 @@ when hasDepositRootChecks:
|
|||
try:
|
||||
let fetchedRoot = asEth2Digest(
|
||||
awaitOrRaiseOnTimeout(depositRoot, contractCallTimeout))
|
||||
if blk.voteData.deposit_root == default(Eth2Digest):
|
||||
if blk.voteData.deposit_root.isZero:
|
||||
blk.voteData.deposit_root = fetchedRoot
|
||||
result = Fetched
|
||||
elif blk.voteData.deposit_root == fetchedRoot:
|
||||
|
|
|
@ -159,7 +159,7 @@ func process_attestation*(
|
|||
block_root: Eth2Digest,
|
||||
target_epoch: Epoch
|
||||
) =
|
||||
if block_root == Eth2Digest():
|
||||
if block_root.isZero:
|
||||
return
|
||||
|
||||
## Add an attestation to the fork choice context
|
||||
|
@ -206,7 +206,7 @@ proc on_attestation*(
|
|||
): FcResult[void] =
|
||||
? self.update_time(dag, wallTime)
|
||||
|
||||
if beacon_block_root == Eth2Digest():
|
||||
if beacon_block_root.isZero:
|
||||
return ok()
|
||||
|
||||
if attestation_slot < self.checkpoints.time.slotOrZero:
|
||||
|
@ -472,7 +472,7 @@ func compute_deltas(
|
|||
for val_index, vote in votes.mpairs():
|
||||
# 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)
|
||||
if vote.current_root == default(Eth2Digest) and vote.next_root == default(Eth2Digest):
|
||||
if vote.current_root.isZero and vote.next_root.isZero:
|
||||
continue
|
||||
|
||||
# If the validator was not included in `old_balances` (i.e. did not exist)
|
||||
|
|
|
@ -12,7 +12,7 @@ import
|
|||
std/[options, tables, typetraits],
|
||||
# Status libraries
|
||||
chronicles,
|
||||
stew/[objects, results],
|
||||
stew/results,
|
||||
# Internal
|
||||
../spec/datatypes/base,
|
||||
# Fork choice
|
||||
|
@ -169,7 +169,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
|||
|
||||
# Iterate backwards through all the indices in `self.nodes`
|
||||
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
||||
if node.root.isZeroMemory:
|
||||
if node.root.isZero:
|
||||
continue
|
||||
|
||||
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,
|
||||
# decrease the delta by the previous score amount.
|
||||
if useProposerBoost and
|
||||
(not self.previousProposerBoostRoot.isZeroMemory) and
|
||||
(not self.previousProposerBoostRoot.isZero) and
|
||||
self.previousProposerBoostRoot == node.root:
|
||||
if nodeDelta < 0 and
|
||||
nodeDelta - low(Delta) < self.previousProposerBoostScore:
|
||||
|
@ -190,8 +190,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
|||
# 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
|
||||
if useProposerBoost and
|
||||
(not proposer_boost_root.isZeroMemory) and
|
||||
if useProposerBoost and (not proposer_boost_root.isZero) and
|
||||
proposer_boost_root == node.root:
|
||||
proposerBoostScore = calculateProposerBoost(newBalances)
|
||||
if nodeDelta >= 0 and
|
||||
|
@ -251,7 +250,7 @@ func applyScoreChanges*(self: var ProtoArray,
|
|||
self.previousProposerBoostScore = proposerBoostScore
|
||||
|
||||
for nodePhysicalIdx in countdown(self.nodes.len - 1, 0):
|
||||
if node.root.isZeroMemory:
|
||||
if node.root.isZero:
|
||||
continue
|
||||
|
||||
if node.parent.isSome():
|
||||
|
|
|
@ -338,7 +338,9 @@ proc validateBeaconBlock*(
|
|||
# to the finalized checkpoint (else it wouldn't be in the DAG)
|
||||
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)
|
||||
|
||||
return errReject("BeaconBlock: Finalized checkpoint not an ancestor")
|
||||
|
|
|
@ -27,7 +27,7 @@ import
|
|||
# Status libraries
|
||||
chronicles,
|
||||
nimcrypto/[sha2, hash],
|
||||
stew/[endians2, byteutils],
|
||||
stew/[byteutils, endians2, objects],
|
||||
json_serialization,
|
||||
blscurve
|
||||
|
||||
|
@ -111,6 +111,9 @@ func `==`*(a, b: Eth2Digest): bool =
|
|||
# Eth2Digest is unnecessary - the type should never hold a secret!
|
||||
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].} =
|
||||
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
|
||||
# separate function for those
|
||||
doAssert signedBlock.message.state_root != Eth2Digest(),
|
||||
doAssert not signedBlock.message.state_root.isZero,
|
||||
"see makeBeaconBlock for block production"
|
||||
state.root = signedBlock.message.state_root
|
||||
|
||||
|
|
|
@ -137,8 +137,8 @@ proc checkStatusMsg(state: BeaconSyncNetworkState, status: StatusMsg):
|
|||
if status.finalizedEpoch <= dag.finalizedHead.slot.epoch:
|
||||
let blockId = dag.getBlockIdAtSlot(status.finalizedEpoch.start_slot())
|
||||
if status.finalizedRoot != blockId.bid.root and
|
||||
blockId.bid.root != Eth2Digest() and
|
||||
status.finalizedRoot != Eth2Digest():
|
||||
(not blockId.bid.root.isZero) and
|
||||
(not status.finalizedRoot.isZero):
|
||||
return err("peer following different finality")
|
||||
|
||||
ok()
|
||||
|
|
|
@ -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).
|
||||
|
@ -42,7 +42,7 @@ suite "Specific field types":
|
|||
t = default(type t)
|
||||
readSszBytes(encoded, t, false)
|
||||
check:
|
||||
t.root == Eth2Digest()
|
||||
t.root.isZero
|
||||
|
||||
testit(phase0.SignedBeaconBlock)
|
||||
testit(phase0.TrustedSignedBeaconBlock)
|
||||
|
|
Loading…
Reference in New Issue