Fix #3650 (participation format in BeaconState result is out of spec) (#3776)

* Fix #3650 (participation format in BeaconState result is out of spec)
* Make EpochParticipationFlags a distinct type
This commit is contained in:
zah 2022-06-20 08:38:56 +03:00 committed by GitHub
parent 9dcbc44fc8
commit c24c737866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 75 additions and 23 deletions

View File

@ -110,12 +110,12 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 2/2 Fail: 0/2 Skip: 0/2
## DeleteKeys requests [Preset: mainnet]
```diff
- Deleting not existing key [Preset: mainnet] Fail
- Invalid Authorization Header [Preset: mainnet] Fail
- Invalid Authorization Token [Preset: mainnet] Fail
- Missing Authorization header [Preset: mainnet] Fail
+ Deleting not existing key [Preset: mainnet] OK
+ Invalid Authorization Header [Preset: mainnet] OK
+ Invalid Authorization Token [Preset: mainnet] OK
+ Missing Authorization header [Preset: mainnet] OK
```
OK: 0/4 Fail: 4/4 Skip: 0/4
OK: 4/4 Fail: 0/4 Skip: 0/4
## DeleteRemoteKeys requests [Preset: mainnet]
```diff
+ Deleting existing local key and remote key [Preset: mainnet] OK
@ -224,12 +224,12 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 4/4 Fail: 0/4 Skip: 0/4
## ImportKeystores requests [Preset: mainnet]
```diff
- ImportKeystores/ListKeystores/DeleteKeystores [Preset: mainnet] Fail
+ ImportKeystores/ListKeystores/DeleteKeystores [Preset: mainnet] OK
+ Invalid Authorization Header [Preset: mainnet] OK
+ Invalid Authorization Token [Preset: mainnet] OK
- Missing Authorization header [Preset: mainnet] Fail
+ Missing Authorization header [Preset: mainnet] OK
```
OK: 2/4 Fail: 2/4 Skip: 0/4
OK: 4/4 Fail: 0/4 Skip: 0/4
## ImportRemoteKeys/ListRemoteKeys/DeleteRemoteKeys [Preset: mainnet]
```diff
+ Importing list of remote keys [Preset: mainnet] OK
@ -573,4 +573,4 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 9/9 Fail: 0/9 Skip: 0/9
---TOTAL---
OK: 312/323 Fail: 6/323 Skip: 5/323
OK: 318/323 Fail: 0/323 Skip: 5/323

View File

@ -670,7 +670,7 @@ func get_proposer_reward*(state: ForkyBeaconState,
# these are all valid; TODO statically verify or do it type-safely
result += get_base_reward(
state, index, base_reward_per_increment) * weight.uint64
epoch_participation.clearCache()
epoch_participation.asHashList.clearCache()
let proposer_reward_denominator =
(WEIGHT_DENOMINATOR.uint64 - PROPOSER_WEIGHT.uint64) *
@ -810,11 +810,11 @@ func translate_participation(
func upgrade_to_altair*(cfg: RuntimeConfig, pre: phase0.BeaconState):
ref altair.BeaconState =
var
empty_participation = EpochParticipationFlags()
empty_participation: EpochParticipationFlags
inactivity_scores = HashList[uint64, Limit VALIDATOR_REGISTRY_LIMIT]()
doAssert empty_participation.data.setLen(pre.validators.len)
empty_participation.resetCache()
empty_participation.asHashList.resetCache()
doAssert inactivity_scores.data.setLen(pre.validators.len)
inactivity_scores.resetCache()

View File

@ -79,7 +79,7 @@ type
ParticipationFlags* = uint8
EpochParticipationFlags* =
HashList[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]
distinct HashList[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/beacon-chain.md#syncaggregate
SyncAggregate* = object
@ -580,6 +580,27 @@ template `[]`*(arr: array[SYNC_COMMITTEE_SIZE, auto] | seq;
makeLimitedU8(SyncSubcommitteeIndex, SYNC_COMMITTEE_SUBNET_COUNT)
makeLimitedU16(IndexInSyncCommittee, SYNC_COMMITTEE_SIZE)
template asHashList*(epochFlags: EpochParticipationFlags): untyped =
HashList[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] epochFlags
template item*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex): ParticipationFlags =
asHashList(epochFlags).item(idx)
template `[]`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex|uint64): ParticipationFlags =
asHashList(epochFlags)[idx]
template `[]=`*(epochFlags: EpochParticipationFlags, idx: ValidatorIndex, flags: ParticipationFlags) =
asHashList(epochFlags)[idx] = flags
template add*(epochFlags: var EpochParticipationFlags, flags: ParticipationFlags): bool =
asHashList(epochFlags).add flags
template len*(epochFlags: EpochParticipationFlags): int =
asHashList(epochFlags).len
template data*(epochFlags: EpochParticipationFlags): untyped =
asHashList(epochFlags).data
func shortLog*(v: SomeBeaconBlock): auto =
(
slot: shortLog(v.slot),

View File

@ -130,6 +130,30 @@ type
{.push raises: [Defect].}
proc writeValue*(writer: var JsonWriter[RestJson],
epochFlags: EpochParticipationFlags)
{.raises: [IOError, Defect].} =
for e in writer.stepwiseArrayCreation(epochFlags.asHashList):
writer.writeValue $e
proc readValue*(reader: var JsonReader[RestJson],
epochFlags: var EpochParticipationFlags)
{.raises: [SerializationError, IOError, Defect].} =
# Please note that this function won't compute the cached hash tree roots
# immediately. They will be computed on the first HTR attempt.
for e in reader.readArray(string):
let parsed = try:
parseBiggestUint(e)
except ValueError as err:
reader.raiseUnexpectedValue("A string-encoded 8-bit usigned integer value expected")
if parsed > uint8.high:
reader.raiseUnexpectedValue("The usigned integer value should fit in 8 bits")
if not epochFlags.data.add(uint8(parsed)):
reader.raiseUnexpectedValue("The participation flags list size exceeds limit")
proc prepareJsonResponse*(t: typedesc[RestApiResponse], d: auto): seq[byte] =
let res =
block:
@ -1261,8 +1285,8 @@ proc readValue*(reader: var JsonReader[RestJson],
reader.raiseUnexpectedValue("Incorrect altair beacon state format")
toValue(bellatrixData)
proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedHashedBeaconState) {.
raises: [IOError, Defect].} =
proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedHashedBeaconState)
{.raises: [IOError, Defect].} =
writer.beginRecord()
case value.kind
of BeaconStateFork.Phase0:

View File

@ -13,7 +13,10 @@ import
ssz_serialization/codec,
./datatypes/base
export codec, base, typetraits
from ./datatypes/altair import
ParticipationFlags, EpochParticipationFlags, asHashList
export codec, base, typetraits, EpochParticipationFlags
# Coding and decoding of SSZ to spec-specific types
@ -22,6 +25,7 @@ template toSszType*(v: BlsCurveType): auto = toRaw(v)
template toSszType*(v: ForkDigest|GraffitiBytes): auto = distinctBase(v)
template toSszType*(v: Version): auto = distinctBase(v)
template toSszType*(v: JustificationBits): auto = distinctBase(v)
template toSszType*(epochFlags: EpochParticipationFlags): auto = asHashList epochFlags
func fromSszBytes*(T: type GraffitiBytes, data: openArray[byte]): T {.raisesssz.} =
if data.len != sizeof(result):
@ -51,3 +55,6 @@ func fromSszBytes*(T: type JustificationBits, bytes: openArray[byte]): T {.raise
if bytes.len != sizeof(result):
raiseIncorrectSize T
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
func fromSszBytes*(T: type EpochParticipationFlags, bytes: openArray[byte]): T {.raisesssz.} =
readSszValue(bytes, HashList[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT] result)

View File

@ -989,7 +989,7 @@ func process_participation_flag_updates*(state: var (altair.BeaconState | bellat
# grows. New elements are automatically initialized to 0, as required.
doAssert state.current_epoch_participation.data.setLen(state.validators.len)
state.current_epoch_participation.resetCache()
state.current_epoch_participation.asHashList.resetCache()
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/beacon-chain.md#sync-committee-updates
func process_sync_committee_updates*(

View File

@ -189,9 +189,9 @@ func applyDiff*(
assign(state.slashings.mitem(epochIndex), stateDiff.slashing)
assign(
state.previous_epoch_participation, stateDiff.previous_epoch_participation)
state.previous_epoch_participation.data, stateDiff.previous_epoch_participation)
assign(
state.current_epoch_participation, stateDiff.current_epoch_participation)
state.current_epoch_participation.data, stateDiff.current_epoch_participation)
state.justification_bits = stateDiff.justification_bits
assign(

@ -1 +1 @@
Subproject commit fc03a0c4e172471294ee5f58a880fa1c8495c00b
Subproject commit 5d0d86451038c9519544e5722089ae83f0af7ab8

@ -1 +1 @@
Subproject commit 5034fef8d048d8d9e5e0228864dacf627d35b96f
Subproject commit dbe0f1ae510d74a23f3693325635277bd0f0c1b9

@ -1 +1 @@
Subproject commit 9631fbd1c81c8b25ff8740df440ca7ba87fa6131
Subproject commit 1d33fa3ced6bc274ed43d99345ceb9cd6bb4dd24

@ -1 +1 @@
Subproject commit 90369dd67b4a41109e26716829f6f3f077eddf38
Subproject commit 875b562aa381ed4cb88e9c7ab06bfb9dfbe1e3db