From e87e0ef17912f1d0f626865f8cb17b2bb1fbf808 Mon Sep 17 00:00:00 2001 From: tersec Date: Tue, 20 Oct 2020 08:54:11 +0000 Subject: [PATCH] update to 1.0.0-rc.0 message ID; unexport funcs; enable test in mainnet; remove unused subscribe-with-nondummy-handler function; fix gossip received metric (#1884) --- beacon_chain/eth2_network.nim | 34 ++++++++++--------------------- beacon_chain/spec/beaconstate.nim | 18 ++++++++-------- tests/test_state_transition.nim | 6 ++---- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index c03420c25..beb9a1221 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -23,7 +23,7 @@ import # Beacon node modules version, conf, eth2_discovery, libp2p_json_serialization, conf, ssz/ssz_serialization, - peer_pool, spec/[datatypes, network], ./time, + peer_pool, spec/[datatypes, digest, helpers, network], ./time, keystore_management when defined(nbc_gossipsub_11): @@ -1362,8 +1362,15 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext, return KeyPair(seckey: privKey, pubkey: privkey.getKey().tryGet()) func gossipId(data: openArray[byte]): string = - # https://github.com/ethereum/eth2.0-specs/blob/v0.12.3/specs/phase0/p2p-interface.md#topics-and-messages - base64.encode(Base64Url, sha256.digest(data).data) + # https://github.com/ethereum/eth2.0-specs/blob/v1.0.0-rc.0/specs/phase0/p2p-interface.md#topics-and-messages + # We don't use non-Snappy-compressed messages, so don't define + # MESSAGE_DOMAIN_INVALID_SNAPPY. + const MESSAGE_DOMAIN_VALID_SNAPPY = 0x01000000'u64 + var messageDigest = withEth2Hash: + h.update uint_to_bytes4(MESSAGE_DOMAIN_VALID_SNAPPY) + h.update data + + string.fromBytes(messageDigest.data.toOpenArray(0, 19)) func msgIdProvider(m: messages.Message): string = gossipId(m.data) @@ -1413,26 +1420,6 @@ proc announcedENR*(node: Eth2Node): enr.Record = proc shortForm*(id: KeyPair): string = $PeerID.init(id.pubkey) -proc subscribe*[MsgType](node: Eth2Node, - topic: string, - msgHandler: proc(msg: MsgType) {.gcsafe.} ) {.async.} = - proc execMsgHandler(topic: string, data: seq[byte]) {.async.} = - inc nbc_gossip_messages_received - trace "Incoming pubsub message received", - len = data.len, topic, msgId = gossipId(data) - try: - let decompressed = snappy.decode(data, GOSSIP_MAX_SIZE) - if decompressed.len > 0: - msgHandler SSZ.decode(decompressed, MsgType) - else: - # TODO penalize peer? - debug "Failed to decompress gossip payload" - except CatchableError as err: - debug "Gossip msg handler error", - msg = err.msg, len = data.len, topic, msgId = gossipId(data) - - await node.pubsub.subscribe(topic & "_snappy", execMsgHandler) - proc subscribe*(node: Eth2Node, topic: string) {.async.} = proc dummyMsgHandler(topic: string, data: seq[byte]) {.async.} = discard @@ -1446,6 +1433,7 @@ proc addValidator*[MsgType](node: Eth2Node, # Validate messages as soon as subscribed proc execValidator( topic: string, message: GossipMsg): Future[bool] {.async.} = + inc nbc_gossip_messages_received trace "Validating incoming gossip message", len = message.data.len, topic, msgId = gossipId(message.data) try: diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 75779c20d..f10f4f9c5 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -122,7 +122,7 @@ proc process_deposit*(preset: RuntimePreset, ok() # https://github.com/ethereum/eth2.0-specs/blob/v1.0.0-rc.0/specs/phase0/beacon-chain.md#compute_activation_exit_epoch -func compute_activation_exit_epoch*(epoch: Epoch): Epoch = +func compute_activation_exit_epoch(epoch: Epoch): Epoch = ## Return the epoch during which validator activations and exits initiated in ## ``epoch`` take effect. epoch + 1 + MAX_SEED_LOOKAHEAD @@ -294,7 +294,7 @@ proc initialize_hashed_beacon_state_from_eth1*( preset, eth1_block_hash, eth1_timestamp, deposits, flags) HashedBeaconState(data: genesisState[], root: hash_tree_root(genesisState[])) -func is_valid_genesis_state*(preset: RuntimePreset, +func is_valid_genesis_state(preset: RuntimePreset, state: BeaconState, active_validator_indices: seq[ValidatorIndex]): bool = if state.genesis_time < preset.MIN_GENESIS_TIME: @@ -304,7 +304,7 @@ func is_valid_genesis_state*(preset: RuntimePreset, return false true -func emptyBeaconBlockBody*(): BeaconBlockBody = +func emptyBeaconBlockBody(): BeaconBlockBody = # TODO: This shouldn't be necessary if OpaqueBlob is the default BeaconBlockBody(randao_reveal: ValidatorSig(kind: OpaqueBlob)) @@ -490,7 +490,7 @@ func get_attesting_indices*(state: BeaconState, get_beacon_committee(state, data.slot, data.index.CommitteeIndex, cache)) # https://github.com/ethereum/eth2.0-specs/blob/v1.0.0-rc.0/specs/phase0/beacon-chain.md#get_indexed_attestation -func get_indexed_attestation*(state: BeaconState, attestation: Attestation, +func get_indexed_attestation(state: BeaconState, attestation: Attestation, cache: var StateCache): IndexedAttestation = ## Return the indexed attestation corresponding to ``attestation``. let @@ -506,7 +506,7 @@ func get_indexed_attestation*(state: BeaconState, attestation: Attestation, signature: attestation.signature ) -func get_indexed_attestation*(state: BeaconState, attestation: TrustedAttestation, +func get_indexed_attestation(state: BeaconState, attestation: TrustedAttestation, cache: var StateCache): TrustedIndexedAttestation = ## Return the indexed attestation corresponding to ``attestation``. let @@ -533,7 +533,7 @@ func check_attestation_slot_target*(data: AttestationData): Result[void, cstring ok() -func check_attestation_target_epoch*( +func check_attestation_target_epoch( data: AttestationData, current_epoch: Epoch): Result[void, cstring] = if not (data.target.epoch == get_previous_epoch(current_epoch) or data.target.epoch == current_epoch): @@ -541,8 +541,8 @@ func check_attestation_target_epoch*( ok() -func check_attestation_inclusion*(data: AttestationData, - current_slot: Slot): Result[void, cstring] = +func check_attestation_inclusion(data: AttestationData, + current_slot: Slot): Result[void, cstring] = # Check for overflow static: doAssert SLOTS_PER_EPOCH >= MIN_ATTESTATION_INCLUSION_DELAY @@ -557,7 +557,7 @@ func check_attestation_inclusion*(data: AttestationData, ok() -func check_attestation_index*( +func check_attestation_index( data: AttestationData, committees_per_slot: uint64): Result[void, cstring] = if not (data.index < committees_per_slot): return err("Data index exceeds committee count") diff --git a/tests/test_state_transition.nim b/tests/test_state_transition.nim index 431803f4f..3c85e58ce 100644 --- a/tests/test_state_transition.nim +++ b/tests/test_state_transition.nim @@ -104,10 +104,8 @@ suiteReport "Block processing" & preset(): # enable exact 1-check again and keep finalization. state.data.current_epoch_attestations.len >= 1 - when const_preset=="minimal": - # Can take several minutes with mainnet settings - check: - process_slots(state[], Slot(191), cache) + check: + process_slots(state[], Slot(191), cache) # Would need to process more epochs for the attestation to be removed from # the state! (per above bug)