2018-11-26 13:33:06 +00:00
|
|
|
import
|
2018-11-29 01:08:34 +00:00
|
|
|
deques,
|
|
|
|
spec/[datatypes, crypto]
|
2018-11-26 13:33:06 +00:00
|
|
|
|
|
|
|
type
|
2018-12-11 17:55:45 +00:00
|
|
|
AttestationCandidate* = object
|
2018-11-26 13:33:06 +00:00
|
|
|
validator*: int
|
2018-12-03 21:41:24 +00:00
|
|
|
data*: AttestationData
|
2018-11-29 01:08:34 +00:00
|
|
|
signature*: ValidatorSig
|
2018-11-26 13:33:06 +00:00
|
|
|
|
|
|
|
AttestationPool* = object
|
2018-12-11 17:55:45 +00:00
|
|
|
attestations: Deque[seq[AttestationCandidate]]
|
2018-11-26 13:33:06 +00:00
|
|
|
startingSlot: int
|
|
|
|
|
|
|
|
proc init*(T: type AttestationPool, startingSlot: int): T =
|
2018-12-11 17:55:45 +00:00
|
|
|
result.attestationsPerSlot = initDeque[seq[AttestationCandidate]]()
|
2018-11-26 13:33:06 +00:00
|
|
|
result.startingSlot = startingSlot
|
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
proc setLen*[T](d: var Deque[T], len: int) =
|
|
|
|
# TODO: The upstream `Deque` type should gain a proper resize API
|
|
|
|
let delta = len - d.len
|
|
|
|
if delta > 0:
|
|
|
|
for i in 0 ..< delta:
|
|
|
|
var defaultVal: T
|
|
|
|
d.addLast(defaultVal)
|
|
|
|
else:
|
|
|
|
d.shrink(fromLast = delta)
|
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
proc add*(pool: var AttestationPool,
|
2018-12-11 17:55:45 +00:00
|
|
|
attestation: AttestationCandidate,
|
2018-11-26 13:33:06 +00:00
|
|
|
beaconState: BeaconState) =
|
|
|
|
# The caller of this function is responsible for ensuring that
|
|
|
|
# the attestations will be given in a strictly slot increasing order:
|
2018-11-29 01:08:34 +00:00
|
|
|
doAssert attestation.data.slot.int >= pool.startingSlot
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
let slotIdxInPool = attestation.data.slot.int - pool.startingSlot
|
2018-11-26 13:33:06 +00:00
|
|
|
if slotIdxInPool >= pool.attestations.len:
|
|
|
|
pool.attestations.setLen(slotIdxInPool + 1)
|
2018-11-29 01:08:34 +00:00
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
pool.attestations[slotIdxInPool].add attestation
|
|
|
|
|
|
|
|
iterator each*(pool: AttestationPool,
|
2018-12-11 17:55:45 +00:00
|
|
|
firstSlot, lastSlot: int): AttestationCandidate =
|
2018-11-26 13:33:06 +00:00
|
|
|
## Both indices are treated inclusively
|
|
|
|
## TODO: this should return a lent value
|
|
|
|
doAssert firstSlot <= lastSlot
|
|
|
|
for idx in countup(max(0, firstSlot - pool.startingSlot),
|
2018-11-29 01:08:34 +00:00
|
|
|
min(pool.attestations.len - 1, lastSlot - pool.startingSlot)):
|
2018-11-26 13:33:06 +00:00
|
|
|
for attestation in pool.attestations[idx]:
|
|
|
|
yield attestation
|
|
|
|
|
|
|
|
proc discardHistoryToSlot*(pool: var AttestationPool, slot: int) =
|
|
|
|
## The index is treated inclusively
|
2018-12-10 12:06:45 +00:00
|
|
|
if slot < pool.startingSlot:
|
|
|
|
return
|
|
|
|
let slotIdx = int(slot - pool.startingSlot)
|
2018-11-29 01:08:34 +00:00
|
|
|
pool.attestations.shrink(fromFirst = slotIdx + 1)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-12-15 16:32:36 +00:00
|
|
|
func getAttestationCandidate*(attestation: Attestation): AttestationCandidate =
|
|
|
|
# TODO: not complete AttestationCandidate object
|
|
|
|
result.data = attestation.data
|
|
|
|
result.signature = attestation.aggregate_signature
|
|
|
|
|
|
|
|
func getLatestAttestation*(pool: AttestationPool, validator: ValidatorRecord) =
|
2018-11-26 13:33:06 +00:00
|
|
|
discard
|
|
|
|
|
2018-12-15 16:32:36 +00:00
|
|
|
func getLatestAttestationTarget*() =
|
2018-11-26 13:33:06 +00:00
|
|
|
discard
|
|
|
|
|
2018-12-15 16:32:36 +00:00
|
|
|
func forkChoice*(pool: AttestationPool, oldHead, newBlock: BeaconBlock): bool =
|
2018-11-26 13:33:06 +00:00
|
|
|
# This will return true if the new block is accepted over the old head block
|
|
|
|
discard
|
|
|
|
|