sync committee topic names; borrow -> template/distinctbase (#2781)

This commit is contained in:
tersec 2021-08-15 14:50:31 +00:00 committed by GitHub
parent df48409969
commit 89d9aa9240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 14 deletions

View File

@ -9,7 +9,7 @@
import
# Std lib
std/[typetraits, sequtils, os, algorithm, math, sets, strutils],
std/[typetraits, sequtils, os, algorithm, math, sets],
std/options as stdOptions,
# Status libs

View File

@ -430,6 +430,8 @@ type
SomeSomeSignedBeaconBlock* = SomeSignedBeaconBlock | phase0.SomeSignedBeaconBlock
SyncCommitteeIndex* = distinct uint8
# TODO when https://github.com/nim-lang/Nim/issues/14440 lands in Status's Nim,
# switch proc {.noSideEffect.} to func.
when false:
@ -440,6 +442,8 @@ when false:
chronicles.formatIt BeaconBlock: it.shortLog
template asUInt8*(x: SyncCommitteeIndex): uint8 = uint8(x)
Json.useCustomSerialization(BeaconState.justification_bits):
read:
let s = reader.readValue(string)

View File

@ -678,10 +678,23 @@ func `[]`*[T](a: seq[T], b: ValidatorIndex): auto =
a[b.int]
# `ValidatorIndex` Nim integration
proc `==`*(x, y: ValidatorIndex) : bool {.borrow, noSideEffect.}
proc `<`*(x, y: ValidatorIndex) : bool {.borrow, noSideEffect.}
proc hash*(x: ValidatorIndex): Hash {.borrow, noSideEffect.}
func `$`*(x: ValidatorIndex): auto = $(distinctBase(x))
template `==`*(x, y: ValidatorIndex) : bool =
distinctBase(x) == distinctBase(y)
template `<`*(x, y: ValidatorIndex): bool =
distinctBase(x) < distinctBase(y)
template hash*(x: ValidatorIndex): Hash =
hash distinctBase(x)
template `$`*(x: ValidatorIndex): auto =
$ distinctBase(x)
template `==`*(x: uint64, y: ValidatorIndex): bool =
x == uint64(y)
template `==`*(x: ValidatorIndex, y: uint64): bool =
uint64(x) == y
# TODO Nim 1.4, but not Nim 1.2, defines a function by this name, which works
# only on openArray[int]. They do the same thing, so either's fine, when both
@ -692,13 +705,23 @@ func toIntSet*[T](x: openArray[T]): IntSet =
for item in items(x):
result.incl(item.int)
proc `==`*(x, y: CommitteeIndex) : bool {.borrow, noSideEffect.}
proc `<`*(x, y: CommitteeIndex) : bool {.borrow, noSideEffect.}
proc hash*(x: CommitteeIndex): Hash {.borrow, noSideEffect.}
func `$`*(x: CommitteeIndex): auto = $(distinctBase(x))
template `==`*(x, y: CommitteeIndex): bool =
distinctBase(x) == distinctBase(y)
proc `==`*(x, y: SubnetId) : bool {.borrow, noSideEffect.}
proc `$`*(x: SubnetId): string {.borrow, noSideEffect.}
template `<`*(x, y: CommitteeIndex): bool =
distinctBase(x) < distinctBase(y)
template hash*(x: CommitteeIndex): Hash =
hash distinctBase(x)
template `$`*(x: CommitteeIndex): auto =
$ distinctBase(x)
template `==`*(x, y: SubnetId): bool =
distinctBase(x) == distinctBase(y)
template `$`*(x: SubnetId): string =
$ distinctBase(x)
func `as`*(d: DepositData, T: type DepositMessage): T =
T(pubkey: d.pubkey,
@ -753,7 +776,7 @@ template newClone*[T](x: ref T not nil): ref T =
newClone(x[])
template lenu64*(x: untyped): untyped =
x.len.uint64
uint64(len(x))
func `$`*(v: ForkDigest | Version): string =
toHex(array[4, byte](v))

View File

@ -79,6 +79,17 @@ func getAttestationTopic*(forkDigest: ForkDigest,
## For subscribing and unsubscribing to/from a subnet.
eth2Prefix(forkDigest) & "beacon_attestation_" & $(subnetId) & "/ssz"
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/p2p-interface.md#topics-and-messages
func getSyncCommitteeTopic*(forkDigest: ForkDigest,
committeeIdx: SyncCommitteeIndex): string =
## For subscribing and unsubscribing to/from a subnet.
eth2Prefix(forkDigest) & "sync_committee_" & $(committeeIdx.asUInt8) & "/ssz"
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/p2p-interface.md#topics-and-messages
func getSyncCommitteeContributionAndProofTopic*(forkDigest: ForkDigest): string =
## For subscribing and unsubscribing to/from a subnet.
eth2Prefix(forkDigest) & "sync_committee_contribution_and_proof" & "/ssz"
func getENRForkID*(cfg: RuntimeConfig,
epoch: Epoch,
genesis_validators_root: Eth2Digest): ENRForkID =

View File

@ -514,7 +514,7 @@ proc cmdValidatorPerf(conf: DbConf, cfg: RuntimeConfig) =
perf.first_slot_head_attester_when_first_slot_not_empty += 1
if s.is_previous_epoch_attester.isSome():
perf.delays.mGetOrPut(
perf.delays.mgetOrPut(
s.is_previous_epoch_attester.get().delay, 0'u64) += 1
else:

View File

@ -4,7 +4,7 @@ import
unittest2,
./testutil,
../beacon_chain/spec/network,
../beacon_chain/spec/datatypes/base,
../beacon_chain/spec/datatypes/[base, altair],
../beacon_chain/validators/attestation_aggregation
suite "Honest validator":
@ -17,6 +17,7 @@ suite "Honest validator":
getProposerSlashingsTopic(forkDigest) == "/eth2/00000000/proposer_slashing/ssz"
getAttesterSlashingsTopic(forkDigest) == "/eth2/00000000/attester_slashing/ssz"
getAggregateAndProofsTopic(forkDigest) == "/eth2/00000000/beacon_aggregate_and_proof/ssz"
getSyncCommitteeContributionAndProofTopic(forkDigest) == "/eth2/00000000/sync_committee_contribution_and_proof/ssz"
test "Mainnet attestation topics":
check:
@ -60,6 +61,12 @@ suite "Honest validator":
"/eth2/00000000/beacon_attestation_62/ssz"
getAttestationTopic(forkDigest, SubnetId(63)) ==
"/eth2/00000000/beacon_attestation_63/ssz"
getSyncCommitteeTopic(forkDigest, SyncCommitteeIndex(0)) ==
"/eth2/00000000/sync_committee_0/ssz"
getSyncCommitteeTopic(forkDigest, SyncCommitteeIndex(1)) ==
"/eth2/00000000/sync_committee_1/ssz"
getSyncCommitteeTopic(forkDigest, SyncCommitteeIndex(3)) ==
"/eth2/00000000/sync_committee_3/ssz"
test "is_aggregator":
check: