Integrate fork choice helpers into skeleton

This commit is contained in:
mratsim 2019-01-08 15:20:17 +01:00 committed by zah
parent e3d8bc9695
commit 8a68bbb26c
7 changed files with 26 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import import
deques, options, deques, options,
milagro_crypto, milagro_crypto,
spec/[datatypes, crypto, helpers], extras ./spec/[datatypes, crypto, digest, helpers], extras
type type
AttestationCandidate* = object AttestationCandidate* = object
@ -74,6 +74,9 @@ proc add*(pool: var AttestationPool,
pool.attestations.setLen(slotIdxInPool + 1) pool.attestations.setLen(slotIdxInPool + 1)
let shard = attestation.data.shard let shard = attestation.data.shard
if attestation.data.slot.int > pool.highestSlot:
pool.highestSlot = attestation.data.slot.int
pool.highestSlotAttestationShard = shard.int
if pool.attestations[slotIdxInPool][shard].isSome: if pool.attestations[slotIdxInPool][shard].isSome:
combine(pool.attestations[slotIdxInPool][shard].get, attestation, {}) combine(pool.attestations[slotIdxInPool][shard].get, attestation, {})
@ -116,11 +119,27 @@ func getAttestationCandidate*(attestation: Attestation): AttestationCandidate =
result.data = attestation.data result.data = attestation.data
result.signature = attestation.aggregate_signature result.signature = attestation.aggregate_signature
func getLatestAttestation*(pool: AttestationPool, validator: ValidatorRecord) = func getLatestAttestation*(pool: AttestationPool): AttestationData =
discard ## Search for the attestation with the highest slot number
## If multiple attestation have the same slot number, keep the first one.
#
# AttestationPool contains the attestations observed and verified
# by the current client. (It might miss some).
#
# Difference with the spec
# - Contrary to the spec we don't use "validator" as an input.
# Specs assume that there is a global "Store" that keeps track of all attestations
# observed by each invidual client.
# AttestationPool only tracks attestations observed by the current client.
func getLatestAttestationTarget*() = var idx = pool.attestations.len - 1
discard # Within a shard we can receive several attestations for the highest slot.
while pool.attestations[idx][pool.highestSlotAttestationShard].get.data.slot.int == pool.highestSlot:
idx -= 1
result = pool.attestations[idx][pool.highestSlotAttestationShard].get.data
func getLatestAttestationTarget*(pool: AttestationPool): Eth2Digest =
pool.getLatestAttestation.beacon_block_root
func forkChoice*(pool: AttestationPool, oldHead, newBlock: BeaconBlock): bool = func forkChoice*(pool: AttestationPool, oldHead, newBlock: BeaconBlock): bool =
# This will return true if the new block is accepted over the old head block # This will return true if the new block is accepted over the old head block

View File

@ -25,8 +25,8 @@
import import
tables, hashes, tables, hashes,
milagro_crypto, milagro_crypto,
./datatypes, ./helpers, ./digest, ../../beacon_chain/spec/[datatypes, helpers, digest],
../ssz ../../beacon_chain/ssz
type type
AttesterIdx = int AttesterIdx = int