initially implement specified attestation aggregation functions and data structures

This commit is contained in:
Dustin Brody 2019-12-06 13:05:00 +01:00 committed by tersec
parent eeadafa46b
commit 294db6aaca
3 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,28 @@
import
sequtils,
./spec/[datatypes, crypto, digest, helpers, validator],
./ssz
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/validator/0_beacon-chain-validator.md#aggregation-selection
func get_slot_signature(state: BeaconState, slot: Slot, privkey: ValidatorPrivKey):
ValidatorSig =
# TODO privkey is int in spec, but bls_sign wants a ValidatorPrivKey
let domain =
get_domain(state, DOMAIN_BEACON_ATTESTER, compute_epoch_at_slot(slot))
bls_sign(privkey, hash_tree_root(slot).data, domain)
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/validator/0_beacon-chain-validator.md#aggregation-selection
func is_aggregator(state: BeaconState, slot: Slot, index: uint64,
slot_signature: ValidatorSig): bool =
# TODO index is a CommitteeIndex, aka uint64
var cache = get_empty_per_epoch_cache()
let
committee = get_beacon_committee(state, slot, index, cache)
modulo = max(1, len(committee) div TARGET_AGGREGATORS_PER_COMMITTEE).uint64
bytes_to_int(eth2hash(slot_signature.getBytes).data[0..7]) mod modulo == 0
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/validator/0_beacon-chain-validator.md#aggregate-signature-1
func get_aggregate_signature(attestations: seq[Attestation]): ValidatorSig =
let signatures = mapIt(attestations, it.signature)
bls_aggregate_signatures(signatures)

View File

@ -128,6 +128,9 @@ func init(T: type VerKey): VerKey =
func init(T: type SigKey): SigKey =
result.point.inf()
func init(T: type Signature): Signature =
result.point.inf()
func combine*[T](values: openarray[BlsValue[T]]): BlsValue[T] =
result = BlsValue[T](kind: Real, blsValue: T.init())
@ -142,6 +145,10 @@ func combine*[T](x: var BlsValue[T], other: BlsValue[T]) =
func bls_aggregate_pubkeys*(keys: openArray[ValidatorPubKey]): ValidatorPubKey =
keys.combine()
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/bls_signature.md#bls_aggregate_signatures
func bls_aggregate_signatures*(keys: openArray[ValidatorSig]): ValidatorSig =
keys.combine()
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/bls_signature.md#bls_verify
func bls_verify*(
pubkey: ValidatorPubKey, msg: openArray[byte], sig: ValidatorSig,
@ -291,4 +298,3 @@ proc toGaugeValue*(hash: Eth2Digest): int64 =
template fromSszBytes*(T: type BlsValue, bytes: openarray[byte]): auto =
fromBytes(T, bytes)

View File

@ -320,6 +320,11 @@ type
deposit_count*: uint64
block_hash*: Eth2Digest
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.2/specs/validator/0_beacon-chain-validator.md#aggregateandproof
AggregateAndProof* = object
index: uint64
selection_proof: ValidatorSig
# TODO to be replaced with some magic hash caching
HashedBeaconState* = object
data*: BeaconState