2019-02-28 21:21:29 +00:00
|
|
|
import
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
bitops, chronicles, options, tables,
|
2019-02-28 21:21:29 +00:00
|
|
|
ssz, beacon_chain_db, state_transition, extras,
|
2019-10-22 11:57:34 +00:00
|
|
|
beacon_node_types, metrics,
|
2019-03-13 22:59:20 +00:00
|
|
|
spec/[crypto, datatypes, digest, helpers]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-10-22 11:57:34 +00:00
|
|
|
declareCounter beacon_reorgs_total, "Total occurrences of reorganizations of the chain" # On fork choice
|
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: topics = "blkpool"
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
proc updateStateData*(
|
|
|
|
pool: BlockPool, state: var StateData, bs: BlockSlot) {.gcsafe.}
|
|
|
|
proc add*(
|
|
|
|
pool: var BlockPool, blockRoot: Eth2Digest,
|
|
|
|
signedBlock: SignedBeaconBlock): BlockRef {.gcsafe.}
|
|
|
|
|
|
|
|
template withState*(
|
|
|
|
pool: BlockPool, cache: var StateData, blockSlot: BlockSlot, body: untyped): untyped =
|
|
|
|
## Helper template that updates state to a particular BlockSlot - usage of
|
|
|
|
## cache is unsafe outside of block.
|
|
|
|
## TODO async transformations will lead to a race where cache gets updated
|
|
|
|
## while waiting for future to complete - catch this here somehow?
|
|
|
|
|
|
|
|
updateStateData(pool, cache, blockSlot)
|
|
|
|
|
|
|
|
template hashedState(): HashedBeaconState {.inject, used.} = cache.data
|
|
|
|
template state(): BeaconState {.inject, used.} = cache.data.data
|
|
|
|
template blck(): BlockRef {.inject, used.} = cache.blck
|
|
|
|
template root(): Eth2Digest {.inject, used.} = cache.data.root
|
|
|
|
|
|
|
|
body
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func parent*(bs: BlockSlot): BlockSlot =
|
2019-12-23 15:34:09 +00:00
|
|
|
## Return a blockslot representing the previous slot, using the parent block
|
|
|
|
## if the current slot had a block
|
|
|
|
if bs.slot == Slot(0):
|
|
|
|
BlockSlot(blck: nil, slot: Slot(0))
|
|
|
|
else:
|
|
|
|
BlockSlot(
|
|
|
|
blck: if bs.slot > bs.blck.slot: bs.blck else: bs.blck.parent,
|
|
|
|
slot: bs.slot - 1
|
|
|
|
)
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func link(parent, child: BlockRef) =
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert (not (parent.root == Eth2Digest() or child.root == Eth2Digest())),
|
|
|
|
"blocks missing root!"
|
|
|
|
doAssert parent.root != child.root, "self-references not allowed"
|
|
|
|
|
|
|
|
child.parent = parent
|
|
|
|
parent.children.add(child)
|
|
|
|
|
2019-12-13 13:54:26 +00:00
|
|
|
func isAncestorOf*(a, b: BlockRef): bool =
|
|
|
|
var b = b
|
|
|
|
var depth = 0
|
|
|
|
const maxDepth = (100'i64 * 365 * 24 * 60 * 60 div SECONDS_PER_SLOT.int)
|
|
|
|
while true:
|
|
|
|
if a == b: return true
|
|
|
|
|
|
|
|
# for now, use an assert for block chain length since a chain this long
|
|
|
|
# indicates a circular reference here..
|
|
|
|
doAssert depth < maxDepth
|
|
|
|
depth += 1
|
|
|
|
|
|
|
|
if a.slot >= b.slot or b.parent.isNil:
|
|
|
|
return false
|
|
|
|
|
|
|
|
doAssert b.slot > b.parent.slot
|
|
|
|
b = b.parent
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
func getAncestorAt*(blck: BlockRef, slot: Slot): BlockRef =
|
|
|
|
## Return the most recent block as of the time at `slot` that not more recent
|
|
|
|
## than `blck` itself
|
|
|
|
|
|
|
|
var blck = blck
|
|
|
|
|
|
|
|
var depth = 0
|
|
|
|
const maxDepth = (100'i64 * 365 * 24 * 60 * 60 div SECONDS_PER_SLOT.int)
|
|
|
|
|
|
|
|
while true:
|
|
|
|
if blck.slot <= slot:
|
|
|
|
return blck
|
|
|
|
|
|
|
|
if blck.parent.isNil:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
doAssert depth < maxDepth
|
|
|
|
depth += 1
|
|
|
|
|
|
|
|
blck = blck.parent
|
|
|
|
|
|
|
|
func get_ancestor*(blck: BlockRef, slot: Slot): BlockRef =
|
2020-01-14 17:46:58 +00:00
|
|
|
## https://github.com/ethereum/eth2.0-specs/blob/v0.10.0/specs/phase0/fork-choice.md#get_ancestor
|
2019-12-23 15:34:09 +00:00
|
|
|
## Return ancestor at slot, or nil if queried block is older
|
2019-12-19 13:02:28 +00:00
|
|
|
var blck = blck
|
|
|
|
|
|
|
|
var depth = 0
|
|
|
|
const maxDepth = (100'i64 * 365 * 24 * 60 * 60 div SECONDS_PER_SLOT.int)
|
|
|
|
|
|
|
|
while true:
|
|
|
|
if blck.slot == slot:
|
|
|
|
return blck
|
|
|
|
|
|
|
|
if blck.slot < slot:
|
|
|
|
return nil
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
if blck.parent.isNil:
|
2019-12-19 13:02:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
doAssert depth < maxDepth
|
|
|
|
depth += 1
|
|
|
|
|
|
|
|
blck = blck.parent
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
func atSlot*(blck: BlockRef, slot: Slot): BlockSlot =
|
|
|
|
## Return a BlockSlot at a given slot, with the block set to the closest block
|
|
|
|
## available. If slot comes from before the block, a suitable block ancestor
|
|
|
|
## will be used, else blck is returned as if all slots after it were empty.
|
|
|
|
## This helper is useful when imagining what the chain looked like at a
|
|
|
|
## particular moment in time, or when imagining what it will look like in the
|
|
|
|
## near future if nothing happens (such as when looking ahead for the next
|
|
|
|
## block proposal)
|
|
|
|
BlockSlot(blck: blck.getAncestorAt(slot), slot: slot)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func init*(T: type BlockRef, root: Eth2Digest, slot: Slot): BlockRef =
|
2019-03-13 22:59:20 +00:00
|
|
|
BlockRef(
|
|
|
|
root: root,
|
|
|
|
slot: slot
|
|
|
|
)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func init*(T: type BlockRef, root: Eth2Digest, blck: BeaconBlock): BlockRef =
|
2019-03-13 22:59:20 +00:00
|
|
|
BlockRef.init(root, blck.slot)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func findAncestorBySlot*(blck: BlockRef, slot: Slot): BlockSlot =
|
2019-03-14 13:33:56 +00:00
|
|
|
## Find the first ancestor that has a slot number less than or equal to `slot`
|
2019-11-22 15:47:08 +00:00
|
|
|
doAssert(not blck.isNil)
|
2019-05-01 09:19:29 +00:00
|
|
|
var ret = blck
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
while ret.parent != nil and ret.slot > slot:
|
|
|
|
ret = ret.parent
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
BlockSlot(blck: ret, slot: slot)
|
2019-03-22 10:57:19 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc init*(T: type BlockPool, db: BeaconChainDB): BlockPool =
|
|
|
|
# TODO we require that the db contains both a head and a tail block -
|
|
|
|
# asserting here doesn't seem like the right way to go about it however..
|
|
|
|
|
|
|
|
let
|
2019-05-01 09:19:29 +00:00
|
|
|
tailBlockRoot = db.getTailBlock()
|
|
|
|
headBlockRoot = db.getHeadBlock()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
doAssert tailBlockRoot.isSome(), "Missing tail block, database corrupt?"
|
|
|
|
doAssert headBlockRoot.isSome(), "Missing head block, database corrupt?"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
let
|
2019-05-01 09:19:29 +00:00
|
|
|
tailRoot = tailBlockRoot.get()
|
2019-03-13 22:59:20 +00:00
|
|
|
tailBlock = db.getBlock(tailRoot).get()
|
2019-12-16 18:08:50 +00:00
|
|
|
tailRef = BlockRef.init(tailRoot, tailBlock.message)
|
2019-05-01 09:19:29 +00:00
|
|
|
headRoot = headBlockRoot.get()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
var
|
|
|
|
blocks = {tailRef.root: tailRef}.toTable()
|
|
|
|
latestStateRoot = Option[Eth2Digest]()
|
|
|
|
headRef: BlockRef
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
if headRoot != tailRoot:
|
|
|
|
var curRef: BlockRef
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
for root, blck in db.getAncestors(headRoot):
|
2019-02-28 21:21:29 +00:00
|
|
|
if root == tailRef.root:
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert(not curRef.isNil)
|
2019-02-28 21:21:29 +00:00
|
|
|
link(tailRef, curRef)
|
|
|
|
curRef = curRef.parent
|
|
|
|
break
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
let newRef = BlockRef.init(root, blck.message)
|
2019-02-28 21:21:29 +00:00
|
|
|
if curRef == nil:
|
2019-03-13 22:59:20 +00:00
|
|
|
curRef = newRef
|
|
|
|
headRef = newRef
|
2019-02-28 21:21:29 +00:00
|
|
|
else:
|
2019-03-13 22:59:20 +00:00
|
|
|
link(newRef, curRef)
|
2019-02-28 21:21:29 +00:00
|
|
|
curRef = curRef.parent
|
|
|
|
blocks[curRef.root] = curRef
|
2019-12-03 11:07:43 +00:00
|
|
|
trace "Populating block pool", key = curRef.root, val = curRef
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
if latestStateRoot.isNone() and db.containsState(blck.message.state_root):
|
|
|
|
latestStateRoot = some(blck.message.state_root)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert curRef == tailRef,
|
|
|
|
"head block does not lead to tail, database corrupt?"
|
2019-03-13 22:59:20 +00:00
|
|
|
else:
|
|
|
|
headRef = tailRef
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
var blocksBySlot = initTable[Slot, seq[BlockRef]]()
|
2019-03-11 15:38:36 +00:00
|
|
|
for _, b in tables.pairs(blocks):
|
2019-12-16 18:08:50 +00:00
|
|
|
let slot = db.getBlock(b.root).get().message.slot
|
2019-11-22 14:14:13 +00:00
|
|
|
blocksBySlot.mgetOrPut(slot, @[]).add(b)
|
2019-03-11 15:38:36 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
# TODO can't do straight init because in mainnet config, there are too
|
|
|
|
# many live beaconstates on the stack...
|
|
|
|
var tmpState = new Option[BeaconState]
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
let
|
|
|
|
# The head state is necessary to find out what we considered to be the
|
|
|
|
# finalized epoch last time we saved something.
|
|
|
|
headStateRoot =
|
|
|
|
if latestStateRoot.isSome():
|
|
|
|
latestStateRoot.get()
|
|
|
|
else:
|
2019-12-16 18:08:50 +00:00
|
|
|
db.getBlock(tailRef.root).get().message.state_root
|
2019-03-13 22:59:20 +00:00
|
|
|
|
|
|
|
# TODO right now, because we save a state at every epoch, this *should*
|
|
|
|
# be the latest justified state or newer, meaning it's enough for
|
|
|
|
# establishing what we consider to be the finalized head. This logic
|
|
|
|
# will need revisiting however
|
2019-12-19 13:02:28 +00:00
|
|
|
tmpState[] = db.getState(headStateRoot)
|
|
|
|
let
|
2019-03-13 22:59:20 +00:00
|
|
|
finalizedHead =
|
More 0.8.0 updates (#311)
* replace BeaconState.finalized_{epoch,root} with BeaconState.finalized_checkpoint; rename get_delayed_activation_exit_epoch(...) to compute_activation_exit_epoch(...) and mark as 0.8.0; update get_churn_limit(...)/get_validator_churn_limit(...) to 0.8.0; update process_registry_updates(...) to 0.8.0
* update process_crosslinks(...) to 0.8.0; mark compute_start_slot_of_epoch(...) and get_committee_count(...) as 0.8.0
* mark Fork, is_slashable_validator(...), and get_beacon_proposer_index(...) as 0.8.0
* rename LATEST_SLASHED_EXIT_LENGTH to EPOCHS_PER_SLASHINGS_VECTOR; update process_slashings(...) to 0.8.0; remove pointless type conversion warning in get_previous_epoch(...)
* convert remaining references to finalized_epoch to finalized_checkpoint.epoch
* update slash_validator(...) to 0.8.0; mark inital value, Gwei, and time constants as 0.8.0; mark hash(...) and processBlockHeader(...) as 0.8.0
* rename WHISTLEBLOWING_REWARD_QUOTIENT to WHISTLEBLOWER_REWARD_QUOTIENT; rename LATEST_ACTIVE_INDEX_ROOTS_LENGTH to EPOCHS_PER_HISTORICAL_VECTOR (randao will also get merged into this); remove get_active_index_root(...); mark time parameter, signature domain types, and max operations per block constants as 0.8.0; update rewards and penalties constants to 0.8.0
* update is_valid_indexed_attestation(...) to 0.8.0; mark process_slot(...) as 0.8.0
* replace BeaconState.{current,previous}_justified_{epoch,root} with BeaconState.{current,previous}_justified_checkpoint
2019-07-05 08:30:05 +00:00
|
|
|
headRef.findAncestorBySlot(
|
2019-12-19 13:02:28 +00:00
|
|
|
tmpState[].get().finalized_checkpoint.epoch.compute_start_slot_at_epoch())
|
More 0.8.0 updates (#311)
* replace BeaconState.finalized_{epoch,root} with BeaconState.finalized_checkpoint; rename get_delayed_activation_exit_epoch(...) to compute_activation_exit_epoch(...) and mark as 0.8.0; update get_churn_limit(...)/get_validator_churn_limit(...) to 0.8.0; update process_registry_updates(...) to 0.8.0
* update process_crosslinks(...) to 0.8.0; mark compute_start_slot_of_epoch(...) and get_committee_count(...) as 0.8.0
* mark Fork, is_slashable_validator(...), and get_beacon_proposer_index(...) as 0.8.0
* rename LATEST_SLASHED_EXIT_LENGTH to EPOCHS_PER_SLASHINGS_VECTOR; update process_slashings(...) to 0.8.0; remove pointless type conversion warning in get_previous_epoch(...)
* convert remaining references to finalized_epoch to finalized_checkpoint.epoch
* update slash_validator(...) to 0.8.0; mark inital value, Gwei, and time constants as 0.8.0; mark hash(...) and processBlockHeader(...) as 0.8.0
* rename WHISTLEBLOWING_REWARD_QUOTIENT to WHISTLEBLOWER_REWARD_QUOTIENT; rename LATEST_ACTIVE_INDEX_ROOTS_LENGTH to EPOCHS_PER_HISTORICAL_VECTOR (randao will also get merged into this); remove get_active_index_root(...); mark time parameter, signature domain types, and max operations per block constants as 0.8.0; update rewards and penalties constants to 0.8.0
* update is_valid_indexed_attestation(...) to 0.8.0; mark process_slot(...) as 0.8.0
* replace BeaconState.{current,previous}_justified_{epoch,root} with BeaconState.{current,previous}_justified_checkpoint
2019-07-05 08:30:05 +00:00
|
|
|
justifiedSlot =
|
2019-12-19 13:02:28 +00:00
|
|
|
tmpState[].get().current_justified_checkpoint.epoch.compute_start_slot_at_epoch()
|
2019-05-01 09:19:29 +00:00
|
|
|
justifiedHead = headRef.findAncestorBySlot(justifiedSlot)
|
|
|
|
head = Head(blck: headRef, justified: justifiedHead)
|
2019-12-19 13:02:28 +00:00
|
|
|
justifiedBlock = db.getBlock(justifiedHead.blck.root).get()
|
|
|
|
justifiedStateRoot = justifiedBlock.message.state_root
|
2019-03-13 22:59:20 +00:00
|
|
|
|
|
|
|
doAssert justifiedHead.slot >= finalizedHead.slot,
|
|
|
|
"justified head comes before finalized head - database corrupt?"
|
|
|
|
|
2019-11-15 14:09:25 +00:00
|
|
|
debug "Block pool initialized",
|
|
|
|
head = head.blck, finalizedHead, tail = tailRef,
|
|
|
|
totalBlocks = blocks.len, totalKnownSlots = blocksBySlot.len
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
let res = BlockPool(
|
2019-12-16 18:08:50 +00:00
|
|
|
pending: initTable[Eth2Digest, SignedBeaconBlock](),
|
2019-04-26 16:38:56 +00:00
|
|
|
missing: initTable[Eth2Digest, MissingBlock](),
|
2019-02-28 21:21:29 +00:00
|
|
|
blocks: blocks,
|
2019-03-11 15:38:36 +00:00
|
|
|
blocksBySlot: blocksBySlot,
|
2019-03-13 22:59:20 +00:00
|
|
|
tail: tailRef,
|
2019-05-01 09:19:29 +00:00
|
|
|
head: head,
|
2019-03-13 22:59:20 +00:00
|
|
|
finalizedHead: finalizedHead,
|
2019-05-01 09:19:29 +00:00
|
|
|
db: db,
|
2019-12-19 13:02:28 +00:00
|
|
|
heads: @[head],
|
2019-02-28 21:21:29 +00:00
|
|
|
)
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
res.headState = StateData(
|
|
|
|
data: HashedBeaconState(data: tmpState[].get(), root: headStateRoot),
|
|
|
|
blck: headRef)
|
|
|
|
res.tmpState = res.headState
|
|
|
|
|
|
|
|
tmpState[] = db.getState(justifiedStateRoot)
|
|
|
|
res.justifiedState = StateData(
|
|
|
|
data: HashedBeaconState(data: tmpState[].get(), root: justifiedStateRoot),
|
|
|
|
blck: justifiedHead.blck)
|
|
|
|
|
|
|
|
|
|
|
|
res
|
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
proc addSlotMapping(pool: BlockPool, br: BlockRef) =
|
2019-03-11 15:38:36 +00:00
|
|
|
proc addIfMissing(s: var seq[BlockRef], v: BlockRef) =
|
|
|
|
if v notin s:
|
|
|
|
s.add(v)
|
2019-11-22 14:14:13 +00:00
|
|
|
pool.blocksBySlot.mgetOrPut(br.slot, @[]).addIfMissing(br)
|
|
|
|
|
|
|
|
proc delSlotMapping(pool: BlockPool, br: BlockRef) =
|
|
|
|
var blks = pool.blocksBySlot.getOrDefault(br.slot)
|
|
|
|
if blks.len != 0:
|
|
|
|
let i = blks.find(br)
|
|
|
|
if i >= 0: blks.del(i)
|
|
|
|
if blks.len == 0:
|
|
|
|
pool.blocksBySlot.del(br.slot)
|
|
|
|
else:
|
|
|
|
pool.blocksBySlot[br.slot] = blks
|
2019-03-11 15:38:36 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
proc addResolvedBlock(
|
|
|
|
pool: var BlockPool, state: var StateData, blockRoot: Eth2Digest,
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock: SignedBeaconBlock, parent: BlockRef): BlockRef =
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "block_resolution"
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
let blockRef = BlockRef.init(blockRoot, signedBlock.message)
|
2019-05-01 09:19:29 +00:00
|
|
|
link(parent, blockRef)
|
|
|
|
|
|
|
|
pool.blocks[blockRoot] = blockRef
|
2019-12-03 11:07:43 +00:00
|
|
|
trace "Populating block pool", key = blockRoot, val = blockRef
|
2019-05-01 09:19:29 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
pool.addSlotMapping(blockRef)
|
2019-05-01 09:19:29 +00:00
|
|
|
|
|
|
|
# Resolved blocks should be stored in database
|
2019-12-16 18:08:50 +00:00
|
|
|
pool.db.putBlock(blockRoot, signedBlock)
|
2019-05-01 09:19:29 +00:00
|
|
|
|
|
|
|
# TODO this is a bit ugly - we update state.data outside of this function then
|
|
|
|
# set the rest here - need a blockRef to update it. Clean this up -
|
|
|
|
# hopefully it won't be necessary by the time hash caching and the rest
|
|
|
|
# is done..
|
2019-05-04 14:10:45 +00:00
|
|
|
doAssert state.data.data.slot == blockRef.slot
|
2019-05-01 09:19:29 +00:00
|
|
|
state.blck = blockRef
|
|
|
|
|
|
|
|
# This block *might* have caused a justification - make sure we stow away
|
|
|
|
# that information:
|
More 0.8.0 updates (#311)
* replace BeaconState.finalized_{epoch,root} with BeaconState.finalized_checkpoint; rename get_delayed_activation_exit_epoch(...) to compute_activation_exit_epoch(...) and mark as 0.8.0; update get_churn_limit(...)/get_validator_churn_limit(...) to 0.8.0; update process_registry_updates(...) to 0.8.0
* update process_crosslinks(...) to 0.8.0; mark compute_start_slot_of_epoch(...) and get_committee_count(...) as 0.8.0
* mark Fork, is_slashable_validator(...), and get_beacon_proposer_index(...) as 0.8.0
* rename LATEST_SLASHED_EXIT_LENGTH to EPOCHS_PER_SLASHINGS_VECTOR; update process_slashings(...) to 0.8.0; remove pointless type conversion warning in get_previous_epoch(...)
* convert remaining references to finalized_epoch to finalized_checkpoint.epoch
* update slash_validator(...) to 0.8.0; mark inital value, Gwei, and time constants as 0.8.0; mark hash(...) and processBlockHeader(...) as 0.8.0
* rename WHISTLEBLOWING_REWARD_QUOTIENT to WHISTLEBLOWER_REWARD_QUOTIENT; rename LATEST_ACTIVE_INDEX_ROOTS_LENGTH to EPOCHS_PER_HISTORICAL_VECTOR (randao will also get merged into this); remove get_active_index_root(...); mark time parameter, signature domain types, and max operations per block constants as 0.8.0; update rewards and penalties constants to 0.8.0
* update is_valid_indexed_attestation(...) to 0.8.0; mark process_slot(...) as 0.8.0
* replace BeaconState.{current,previous}_justified_{epoch,root} with BeaconState.{current,previous}_justified_checkpoint
2019-07-05 08:30:05 +00:00
|
|
|
let justifiedSlot =
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
state.data.data.current_justified_checkpoint.epoch.compute_start_slot_at_epoch()
|
2019-05-01 09:19:29 +00:00
|
|
|
|
|
|
|
var foundHead: Option[Head]
|
|
|
|
for head in pool.heads.mitems():
|
2019-12-13 13:54:26 +00:00
|
|
|
if head.blck.isAncestorOf(blockRef):
|
2019-05-01 09:19:29 +00:00
|
|
|
if head.justified.slot != justifiedSlot:
|
|
|
|
head.justified = blockRef.findAncestorBySlot(justifiedSlot)
|
|
|
|
|
2019-12-13 13:54:26 +00:00
|
|
|
head.blck = blockRef
|
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
foundHead = some(head)
|
|
|
|
break
|
|
|
|
|
|
|
|
if foundHead.isNone():
|
|
|
|
foundHead = some(Head(
|
|
|
|
blck: blockRef,
|
|
|
|
justified: blockRef.findAncestorBySlot(justifiedSlot)))
|
|
|
|
pool.heads.add(foundHead.get())
|
|
|
|
|
|
|
|
info "Block resolved",
|
2019-12-16 18:08:50 +00:00
|
|
|
blck = shortLog(signedBlock.message),
|
2019-05-01 09:19:29 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
justifiedRoot = shortLog(foundHead.get().justified.blck.root),
|
2019-09-12 01:45:04 +00:00
|
|
|
justifiedSlot = shortLog(foundHead.get().justified.slot),
|
|
|
|
cat = "filtering"
|
2019-05-01 09:19:29 +00:00
|
|
|
|
|
|
|
# Now that we have the new block, we should see if any of the previously
|
|
|
|
# unresolved blocks magically become resolved
|
|
|
|
# TODO there are more efficient ways of doing this that don't risk
|
|
|
|
# running out of stack etc
|
2019-11-27 15:19:41 +00:00
|
|
|
# TODO This code is convoluted because when there are more than ~1.5k
|
|
|
|
# blocks being synced, there's a stack overflow as `add` gets called
|
|
|
|
# for the whole chain of blocks. Instead we use this ugly field in `pool`
|
|
|
|
# which could be avoided by refactoring the code
|
|
|
|
if not pool.inAdd:
|
|
|
|
pool.inAdd = true
|
|
|
|
defer: pool.inAdd = false
|
|
|
|
var keepGoing = true
|
|
|
|
while keepGoing:
|
|
|
|
let retries = pool.pending
|
|
|
|
for k, v in retries:
|
2019-12-19 13:02:28 +00:00
|
|
|
discard pool.add(k, v)
|
2019-11-27 15:19:41 +00:00
|
|
|
# Keep going for as long as the pending pool is shrinking
|
|
|
|
# TODO inefficient! so what?
|
|
|
|
keepGoing = pool.pending.len < retries.len
|
2019-05-01 09:19:29 +00:00
|
|
|
blockRef
|
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
proc add*(
|
2019-12-19 13:02:28 +00:00
|
|
|
pool: var BlockPool, blockRoot: Eth2Digest,
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock: SignedBeaconBlock): BlockRef {.gcsafe.} =
|
2019-03-22 15:49:37 +00:00
|
|
|
## return the block, if resolved...
|
2019-03-08 16:40:17 +00:00
|
|
|
## the state parameter may be updated to include the given block, if
|
|
|
|
## everything checks out
|
|
|
|
# TODO reevaluate passing the state in like this
|
2019-12-16 18:08:50 +00:00
|
|
|
let blck = signedBlock.message
|
|
|
|
doAssert blockRoot == hash_tree_root(blck)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "block_addition"
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# Already seen this block??
|
|
|
|
if blockRoot in pool.blocks:
|
|
|
|
debug "Block already exists",
|
2019-03-14 13:33:56 +00:00
|
|
|
blck = shortLog(blck),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "filtering"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
return pool.blocks[blockRoot]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-04-26 16:38:56 +00:00
|
|
|
pool.missing.del(blockRoot)
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
# If the block we get is older than what we finalized already, we drop it.
|
|
|
|
# One way this can happen is that we start resolving a block and finalization
|
|
|
|
# happens in the meantime - the block we requested will then be stale
|
|
|
|
# by the time it gets here.
|
|
|
|
if blck.slot <= pool.finalizedHead.slot:
|
2019-02-28 21:21:29 +00:00
|
|
|
debug "Old block, dropping",
|
2019-03-14 13:33:56 +00:00
|
|
|
blck = shortLog(blck),
|
2019-08-15 16:01:55 +00:00
|
|
|
tailSlot = shortLog(pool.tail.slot),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "filtering"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
return
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-06-14 13:50:47 +00:00
|
|
|
let parent = pool.blocks.getOrDefault(blck.parent_root)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
if parent != nil:
|
2019-12-19 13:02:28 +00:00
|
|
|
if parent.slot >= blck.slot:
|
|
|
|
# TODO Malicious block? inform peer pool?
|
|
|
|
notice "Invalid block slot",
|
|
|
|
blck = shortLog(blck),
|
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
parentRoot = shortLog(parent.root),
|
|
|
|
parentSlot = shortLog(parent.slot)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
# The block might have been in either of these - we don't want any more
|
|
|
|
# work done on its behalf
|
|
|
|
pool.pending.del(blockRoot)
|
|
|
|
|
|
|
|
# The block is resolved, now it's time to validate it to ensure that the
|
|
|
|
# blocks we add to the database are clean for the given state
|
2019-04-26 16:38:56 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
# TODO if the block is from the future, we should not be resolving it (yet),
|
|
|
|
# but maybe we should use it as a hint that our clock is wrong?
|
2019-12-19 13:02:28 +00:00
|
|
|
updateStateData(pool, pool.tmpState, BlockSlot(blck: parent, slot: blck.slot - 1))
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
if not state_transition(pool.tmpState.data, blck, {}):
|
2019-03-08 16:40:17 +00:00
|
|
|
# TODO find a better way to log all this block data
|
|
|
|
notice "Invalid block",
|
2019-03-14 13:33:56 +00:00
|
|
|
blck = shortLog(blck),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "filtering"
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
return
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
# Careful, pool.tmpState is now partially inconsistent and will be updated
|
|
|
|
# inside addResolvedBlock
|
|
|
|
return pool.addResolvedBlock(pool.tmpState, blockRoot, signedBlock, parent)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
# TODO already checked hash though? main reason to keep this is because
|
|
|
|
# the pending pool calls this function back later in a loop, so as long
|
|
|
|
# as pool.add(...) requires a SignedBeaconBlock, easier to keep them in
|
|
|
|
# pending too.
|
|
|
|
pool.pending[blockRoot] = signedBlock
|
2019-04-26 16:38:56 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO possibly, it makes sense to check the database - that would allow sync
|
|
|
|
# to simply fill up the database with random blocks the other clients
|
|
|
|
# think are useful - but, it would also risk filling the database with
|
|
|
|
# junk that's not part of the block graph
|
|
|
|
|
2019-06-14 13:50:47 +00:00
|
|
|
if blck.parent_root in pool.missing or
|
|
|
|
blck.parent_root in pool.pending:
|
2019-03-22 15:49:37 +00:00
|
|
|
return
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-04-26 16:38:56 +00:00
|
|
|
# This is an unresolved block - put its parent on the missing list for now...
|
2019-03-09 04:23:14 +00:00
|
|
|
# TODO if we receive spam blocks, one heurestic to implement might be to wait
|
|
|
|
# for a couple of attestations to appear before fetching parents - this
|
|
|
|
# would help prevent using up network resources for spam - this serves
|
|
|
|
# two purposes: one is that attestations are likely to appear for the
|
|
|
|
# block only if it's valid / not spam - the other is that malicious
|
|
|
|
# validators that are not proposers can sign invalid blocks and send
|
|
|
|
# them out without penalty - but signing invalid attestations carries
|
|
|
|
# a risk of being slashed, making attestations a more valuable spam
|
|
|
|
# filter.
|
2019-03-27 20:17:01 +00:00
|
|
|
# TODO when we receive the block, we don't know how many others we're missing
|
|
|
|
# from that branch, so right now, we'll just do a blind guess
|
2019-04-26 16:38:56 +00:00
|
|
|
debug "Unresolved block (parent missing)",
|
2019-03-14 13:33:56 +00:00
|
|
|
blck = shortLog(blck),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "filtering"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-27 20:17:01 +00:00
|
|
|
let parentSlot = blck.slot - 1
|
|
|
|
|
2019-06-14 13:50:47 +00:00
|
|
|
pool.missing[blck.parent_root] = MissingBlock(
|
2019-03-27 20:17:01 +00:00
|
|
|
slots:
|
|
|
|
# The block is at least two slots ahead - try to grab whole history
|
2019-05-01 09:19:29 +00:00
|
|
|
if parentSlot > pool.head.blck.slot:
|
|
|
|
parentSlot - pool.head.blck.slot
|
2019-03-27 20:17:01 +00:00
|
|
|
else:
|
|
|
|
# It's a sibling block from a branch that we're missing - fetch one
|
|
|
|
# epoch at a time
|
|
|
|
max(1.uint64, SLOTS_PER_EPOCH.uint64 -
|
|
|
|
(parentSlot.uint64 mod SLOTS_PER_EPOCH.uint64))
|
|
|
|
)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func getRef*(pool: BlockPool, root: Eth2Digest): BlockRef =
|
2019-06-10 11:13:53 +00:00
|
|
|
## Retrieve a resolved block reference, if available
|
2019-11-25 23:39:33 +00:00
|
|
|
pool.blocks.getOrDefault(root, nil)
|
2019-06-10 11:13:53 +00:00
|
|
|
|
2019-11-25 23:39:33 +00:00
|
|
|
proc getBlockRange*(pool: BlockPool, headBlock: Eth2Digest,
|
2019-09-10 05:50:37 +00:00
|
|
|
startSlot: Slot, skipStep: Natural,
|
|
|
|
output: var openarray[BlockRef]): Natural =
|
|
|
|
## This function populates an `output` buffer of blocks
|
|
|
|
## with a range starting from `startSlot` and skipping
|
|
|
|
## every `skipTest` number of blocks.
|
|
|
|
##
|
|
|
|
## Please note that the function may not necessarily
|
|
|
|
## populate the entire buffer. The values will be written
|
|
|
|
## in a way such that the last block is placed at the end
|
|
|
|
## of the buffer while the first indices of the buffer
|
|
|
|
## may remain unwritten.
|
|
|
|
##
|
|
|
|
## The result value of the function will be the index of
|
|
|
|
## the first block in the resulting buffer. If no values
|
|
|
|
## were written to the buffer, the result will be equal to
|
|
|
|
## `buffer.len`. In other words, you can use the function
|
|
|
|
## like this:
|
|
|
|
##
|
|
|
|
## var buffer: array[N, BlockRef]
|
|
|
|
## let startPos = pool.getBlockRange(headBlock, startSlot, skipStep, buffer)
|
|
|
|
## for i in startPos ..< buffer.len:
|
|
|
|
## echo buffer[i].slot
|
|
|
|
##
|
|
|
|
result = output.len
|
|
|
|
|
2019-11-26 17:02:27 +00:00
|
|
|
trace "getBlockRange entered", headBlock, startSlot, skipStep
|
|
|
|
|
2019-09-10 05:50:37 +00:00
|
|
|
var b = pool.getRef(headBlock)
|
2019-11-14 13:19:19 +00:00
|
|
|
if b == nil:
|
2019-11-15 14:09:25 +00:00
|
|
|
trace "head block not found", headBlock
|
2019-11-14 13:19:19 +00:00
|
|
|
return
|
|
|
|
|
2019-11-26 17:02:27 +00:00
|
|
|
trace "head block found", headBlock = b
|
|
|
|
|
2019-11-14 13:19:19 +00:00
|
|
|
if b.slot < startSlot:
|
|
|
|
trace "head block is older than startSlot", headBlockSlot = b.slot, startSlot
|
2019-09-10 05:50:37 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
template skip(n: int) =
|
2019-11-27 11:45:34 +00:00
|
|
|
let targetSlot = b.slot - Slot(n)
|
|
|
|
while b.slot > targetSlot:
|
2019-11-14 13:19:19 +00:00
|
|
|
if b.parent == nil:
|
2019-11-25 23:39:33 +00:00
|
|
|
trace "stopping at parentless block", slot = b.slot, root = b.root
|
2019-11-14 13:19:19 +00:00
|
|
|
return
|
2019-11-27 09:20:58 +00:00
|
|
|
trace "skipping block", nextBlock = b.parent
|
2019-09-10 05:50:37 +00:00
|
|
|
b = b.parent
|
|
|
|
|
|
|
|
# We must compute the last block that is eligible for inclusion
|
|
|
|
# in the results. This will be a block with a slot number that's
|
|
|
|
# aligned to the stride of the requested block range, so we first
|
|
|
|
# compute the steps needed to get to an aligned position:
|
|
|
|
var blocksToSkip = b.slot.int mod skipStep
|
|
|
|
let alignedHeadSlot = b.slot.int - blocksToSkip
|
|
|
|
|
|
|
|
# Then we see if this aligned position is within our wanted
|
|
|
|
# range. If it's outside it, we must skip more blocks:
|
2019-11-12 22:53:19 +00:00
|
|
|
let lastWantedSlot = startSlot.int + (output.len - 1) * skipStep
|
2019-09-10 05:50:37 +00:00
|
|
|
if alignedHeadSlot > lastWantedSlot:
|
|
|
|
blocksToSkip += (alignedHeadSlot - lastWantedSlot)
|
|
|
|
|
|
|
|
# Finally, we skip the computed number of blocks
|
2019-11-27 09:20:58 +00:00
|
|
|
trace "aligning head", blocksToSkip
|
2019-09-10 05:50:37 +00:00
|
|
|
skip blocksToSkip
|
|
|
|
|
|
|
|
# From here, we can just write out the requested block range:
|
2019-11-27 11:45:34 +00:00
|
|
|
while b != nil and b.slot >= startSlot and result > 0:
|
2019-09-10 05:50:37 +00:00
|
|
|
dec result
|
|
|
|
output[result] = b
|
2019-11-14 13:19:19 +00:00
|
|
|
trace "getBlockRange result", position = result, blockSlot = b.slot
|
2019-09-10 05:50:37 +00:00
|
|
|
skip skipStep
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc get*(pool: BlockPool, blck: BlockRef): BlockData =
|
|
|
|
## Retrieve the associated block body of a block reference
|
|
|
|
doAssert (not blck.isNil), "Trying to get nil BlockRef"
|
|
|
|
|
|
|
|
let data = pool.db.getBlock(blck.root)
|
|
|
|
doAssert data.isSome, "BlockRef without backing data, database corrupt?"
|
|
|
|
|
|
|
|
BlockData(data: data.get(), refs: blck)
|
|
|
|
|
|
|
|
proc get*(pool: BlockPool, root: Eth2Digest): Option[BlockData] =
|
|
|
|
## Retrieve a resolved block reference and its associated body, if available
|
2019-06-10 11:13:53 +00:00
|
|
|
let refs = pool.getRef(root)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
if not refs.isNil:
|
|
|
|
some(pool.get(refs))
|
|
|
|
else:
|
|
|
|
none(BlockData)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func getOrResolve*(pool: var BlockPool, root: Eth2Digest): BlockRef =
|
2019-02-28 21:21:29 +00:00
|
|
|
## Fetch a block ref, or nil if not found (will be added to list of
|
|
|
|
## blocks-to-resolve)
|
2019-06-10 11:13:53 +00:00
|
|
|
result = pool.getRef(root)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
if result.isNil:
|
2019-04-26 16:38:56 +00:00
|
|
|
pool.missing[root] = MissingBlock(slots: 1)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
iterator blockRootsForSlot*(pool: BlockPool, slot: Slot): Eth2Digest =
|
|
|
|
for br in pool.blocksBySlot.getOrDefault(slot, @[]):
|
2019-03-11 15:38:36 +00:00
|
|
|
yield br.root
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func checkMissing*(pool: var BlockPool): seq[FetchRecord] =
|
2019-02-28 21:21:29 +00:00
|
|
|
## Return a list of blocks that we should try to resolve from other client -
|
|
|
|
## to be called periodically but not too often (once per slot?)
|
|
|
|
var done: seq[Eth2Digest]
|
|
|
|
|
2019-04-26 16:38:56 +00:00
|
|
|
for k, v in pool.missing.mpairs():
|
2019-02-28 21:21:29 +00:00
|
|
|
if v.tries > 8:
|
|
|
|
done.add(k)
|
|
|
|
else:
|
|
|
|
inc v.tries
|
|
|
|
|
|
|
|
for k in done:
|
2019-03-08 16:40:17 +00:00
|
|
|
# TODO Need to potentially remove from pool.pending - this is currently a
|
|
|
|
# memory leak here!
|
2019-04-26 16:38:56 +00:00
|
|
|
pool.missing.del(k)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
# simple (simplistic?) exponential backoff for retries..
|
2019-04-26 16:38:56 +00:00
|
|
|
for k, v in pool.missing.pairs():
|
2019-02-28 21:21:29 +00:00
|
|
|
if v.tries.popcount() == 1:
|
2019-03-27 20:17:01 +00:00
|
|
|
result.add(FetchRecord(root: k, historySlots: v.slots))
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
proc skipAndUpdateState(
|
2019-05-04 14:10:45 +00:00
|
|
|
state: var HashedBeaconState, blck: BeaconBlock, flags: UpdateFlags,
|
|
|
|
afterUpdate: proc (state: HashedBeaconState)): bool =
|
2019-07-15 21:10:40 +00:00
|
|
|
|
|
|
|
process_slots(state, blck.slot - 1)
|
|
|
|
afterUpdate(state)
|
|
|
|
|
|
|
|
let ok = state_transition(state, blck, flags)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
afterUpdate(state)
|
|
|
|
|
|
|
|
ok
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-04 14:10:45 +00:00
|
|
|
proc maybePutState(pool: BlockPool, state: HashedBeaconState, blck: BlockRef) =
|
2019-03-08 16:40:17 +00:00
|
|
|
# TODO we save state at every epoch start but never remove them - we also
|
|
|
|
# potentially save multiple states per slot if reorgs happen, meaning
|
|
|
|
# we could easily see a state explosion
|
2019-08-14 08:56:32 +00:00
|
|
|
# TODO this is out of sync with epoch def now, I think -- (slot + 1) mod foo.
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "save_state_at_epoch_start"
|
|
|
|
|
2019-05-04 14:10:45 +00:00
|
|
|
if state.data.slot mod SLOTS_PER_EPOCH == 0:
|
|
|
|
if not pool.db.containsState(state.root):
|
2019-03-22 15:49:37 +00:00
|
|
|
info "Storing state",
|
2019-08-15 16:01:55 +00:00
|
|
|
stateSlot = shortLog(state.data.slot),
|
2019-09-12 01:45:04 +00:00
|
|
|
stateRoot = shortLog(state.root),
|
|
|
|
cat = "caching"
|
2019-05-04 14:10:45 +00:00
|
|
|
pool.db.putState(state.root, state.data)
|
2019-03-28 06:10:48 +00:00
|
|
|
# TODO this should be atomic with the above write..
|
2019-05-04 14:10:45 +00:00
|
|
|
pool.db.putStateRoot(blck.root, state.data.slot, state.root)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
proc rewindState(pool: BlockPool, state: var StateData, bs: BlockSlot):
|
|
|
|
seq[BlockData] =
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "replay_state"
|
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
var ancestors = @[pool.get(bs.blck)]
|
|
|
|
# Common case: the last block applied is the parent of the block to apply:
|
|
|
|
if not bs.blck.parent.isNil and state.blck.root == bs.blck.parent.root and
|
2019-05-04 14:10:45 +00:00
|
|
|
state.data.data.slot < bs.slot:
|
2019-03-28 06:10:48 +00:00
|
|
|
return ancestors
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
# It appears that the parent root of the proposed new block is different from
|
|
|
|
# what we expected. We will have to rewind the state to a point along the
|
|
|
|
# chain of ancestors of the new block. We will do this by loading each
|
|
|
|
# successive parent block and checking if we can find the corresponding state
|
|
|
|
# in the database.
|
2019-03-28 06:10:48 +00:00
|
|
|
var
|
|
|
|
stateRoot = pool.db.getStateRoot(bs.blck.root, bs.slot)
|
|
|
|
curBs = bs
|
|
|
|
while stateRoot.isNone():
|
|
|
|
let parBs = curBs.parent()
|
|
|
|
if parBs.blck.isNil:
|
|
|
|
break # Bug probably!
|
|
|
|
|
|
|
|
if parBs.blck != curBs.blck:
|
|
|
|
ancestors.add(pool.get(parBs.blck))
|
|
|
|
|
|
|
|
if (let tmp = pool.db.getStateRoot(parBs.blck.root, parBs.slot); tmp.isSome()):
|
|
|
|
if pool.db.containsState(tmp.get):
|
|
|
|
stateRoot = tmp
|
|
|
|
break
|
|
|
|
|
|
|
|
curBs = parBs
|
|
|
|
|
|
|
|
if stateRoot.isNone():
|
|
|
|
# TODO this should only happen if the database is corrupt - we walked the
|
|
|
|
# list of parent blocks and couldn't find a corresponding state in the
|
|
|
|
# database, which should never happen (at least we should have the
|
|
|
|
# tail state in there!)
|
|
|
|
error "Couldn't find ancestor state root!",
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(bs.blck.root),
|
|
|
|
cat = "crash"
|
2019-03-28 06:10:48 +00:00
|
|
|
doAssert false, "Oh noes, we passed big bang!"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
let
|
2019-12-19 13:02:28 +00:00
|
|
|
ancestor = ancestors.pop()
|
2019-03-28 06:10:48 +00:00
|
|
|
ancestorState = pool.db.getState(stateRoot.get())
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
if ancestorState.isNone():
|
|
|
|
# TODO this should only happen if the database is corrupt - we walked the
|
|
|
|
# list of parent blocks and couldn't find a corresponding state in the
|
|
|
|
# database, which should never happen (at least we should have the
|
|
|
|
# tail state in there!)
|
|
|
|
error "Couldn't find ancestor state or block parent missing!",
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(bs.blck.root),
|
|
|
|
cat = "crash"
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert false, "Oh noes, we passed big bang!"
|
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
trace "Replaying state transitions",
|
2019-08-15 16:01:55 +00:00
|
|
|
stateSlot = shortLog(state.data.data.slot),
|
2019-12-17 09:52:04 +00:00
|
|
|
ancestorStateRoot = shortLog(ancestor.data.message.state_root),
|
2019-08-15 16:01:55 +00:00
|
|
|
ancestorStateSlot = shortLog(ancestorState.get().slot),
|
|
|
|
slot = shortLog(bs.slot),
|
2019-03-28 06:10:48 +00:00
|
|
|
blockRoot = shortLog(bs.blck.root),
|
2019-09-12 01:45:04 +00:00
|
|
|
ancestors = ancestors.len,
|
|
|
|
cat = "replay_state"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-04 14:10:45 +00:00
|
|
|
state.data.data = ancestorState.get()
|
|
|
|
state.data.root = stateRoot.get()
|
|
|
|
state.blck = ancestor.refs
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
ancestors
|
|
|
|
|
2019-04-29 08:14:22 +00:00
|
|
|
proc updateStateData*(pool: BlockPool, state: var StateData, bs: BlockSlot) =
|
2019-03-28 06:10:48 +00:00
|
|
|
## Rewind or advance state such that it matches the given block and slot -
|
|
|
|
## this may include replaying from an earlier snapshot if blck is on a
|
|
|
|
## different branch or has advanced to a higher slot number than slot
|
|
|
|
## If slot is higher than blck.slot, replay will fill in with empty/non-block
|
|
|
|
## slots, else it is ignored
|
|
|
|
|
|
|
|
# We need to check the slot because the state might have moved forwards
|
|
|
|
# without blocks
|
2019-05-04 14:10:45 +00:00
|
|
|
if state.blck.root == bs.blck.root and state.data.data.slot <= bs.slot:
|
|
|
|
if state.data.data.slot != bs.slot:
|
2019-04-29 08:14:22 +00:00
|
|
|
# Might be that we're moving to the same block but later slot
|
2019-07-15 21:10:40 +00:00
|
|
|
process_slots(state.data, bs.slot)
|
|
|
|
pool.maybePutState(state.data, bs.blck)
|
2019-04-29 08:14:22 +00:00
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
return # State already at the right spot
|
|
|
|
|
|
|
|
let ancestors = rewindState(pool, state, bs)
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# If we come this far, we found the state root. The last block on the stack
|
|
|
|
# is the one that produced this particular state, so we can pop it
|
|
|
|
# TODO it might be possible to use the latest block hashes from the state to
|
|
|
|
# do this more efficiently.. whatever!
|
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
# Time to replay all the blocks between then and now. We skip one because
|
2019-02-28 21:21:29 +00:00
|
|
|
# it's the one that we found the state with, and it has already been
|
|
|
|
# applied
|
2019-12-19 13:02:28 +00:00
|
|
|
for i in countdown(ancestors.len - 1, 0):
|
2019-03-28 06:10:48 +00:00
|
|
|
let ok =
|
2019-12-16 18:08:50 +00:00
|
|
|
skipAndUpdateState(state.data, ancestors[i].data.message, {skipValidation}) do(
|
2019-05-04 14:10:45 +00:00
|
|
|
state: HashedBeaconState):
|
2019-03-28 06:10:48 +00:00
|
|
|
pool.maybePutState(state, ancestors[i].refs)
|
|
|
|
doAssert ok, "Blocks in database should never fail to apply.."
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-08-14 08:56:32 +00:00
|
|
|
# TODO check if this triggers rest of state transition, or should
|
2019-07-15 21:10:40 +00:00
|
|
|
process_slots(state.data, bs.slot)
|
|
|
|
pool.maybePutState(state.data, bs.blck)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
state.blck = bs.blck
|
2019-03-14 13:33:56 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc loadTailState*(pool: BlockPool): StateData =
|
|
|
|
## Load the state associated with the current tail in the pool
|
2019-12-16 18:08:50 +00:00
|
|
|
let stateRoot = pool.db.getBlock(pool.tail.root).get().message.state_root
|
2019-02-28 21:21:29 +00:00
|
|
|
StateData(
|
2019-05-04 14:10:45 +00:00
|
|
|
data: HashedBeaconState(
|
|
|
|
data: pool.db.getState(stateRoot).get(),
|
|
|
|
root: stateRoot),
|
2019-03-13 22:59:20 +00:00
|
|
|
blck: pool.tail
|
2019-02-28 21:21:29 +00:00
|
|
|
)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
proc delBlockAndState(pool: BlockPool, blockRoot: Eth2Digest) =
|
|
|
|
if (let blk = pool.db.getBlock(blockRoot); blk.isSome):
|
2019-12-16 18:08:50 +00:00
|
|
|
pool.db.delState(blk.get.message.stateRoot)
|
2019-11-22 14:14:13 +00:00
|
|
|
pool.db.delBlock(blockRoot)
|
|
|
|
|
|
|
|
proc delFinalizedStateIfNeeded(pool: BlockPool, b: BlockRef) =
|
|
|
|
# Delete finalized state for block `b` from the database, that doesn't need
|
|
|
|
# to be kept for replaying.
|
|
|
|
# TODO: Currently the protocol doesn't provide a way to request states,
|
|
|
|
# so we don't need any of the finalized states, and thus remove all of them
|
|
|
|
# (except the most recent)
|
|
|
|
if (let blk = pool.db.getBlock(b.root); blk.isSome):
|
2019-12-16 18:08:50 +00:00
|
|
|
pool.db.delState(blk.get.message.stateRoot)
|
2019-11-22 14:14:13 +00:00
|
|
|
|
|
|
|
proc setTailBlock(pool: BlockPool, newTail: BlockRef) =
|
|
|
|
## Advance tail block, pruning all the states and blocks with older slots
|
|
|
|
let oldTail = pool.tail
|
|
|
|
let fromSlot = oldTail.slot.uint64
|
|
|
|
let toSlot = newTail.slot.uint64 - 1
|
|
|
|
assert(toSlot > fromSlot)
|
|
|
|
for s in fromSlot .. toSlot:
|
|
|
|
for b in pool.blocksBySlot.getOrDefault(s.Slot, @[]):
|
|
|
|
pool.delBlockAndState(b.root)
|
|
|
|
b.children = @[]
|
|
|
|
b.parent = nil
|
|
|
|
pool.blocks.del(b.root)
|
|
|
|
pool.pending.del(b.root)
|
|
|
|
pool.missing.del(b.root)
|
|
|
|
|
|
|
|
pool.blocksBySlot.del(s.Slot)
|
|
|
|
|
|
|
|
pool.db.putTailBlock(newTail.root)
|
|
|
|
pool.tail = newTail
|
|
|
|
pool.addSlotMapping(newTail)
|
|
|
|
info "Tail block updated",
|
|
|
|
slot = newTail.slot,
|
|
|
|
root = shortLog(newTail.root)
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
proc updateHead*(pool: BlockPool, newHead: BlockRef) =
|
2019-03-13 22:59:20 +00:00
|
|
|
## Update what we consider to be the current head, as given by the fork
|
|
|
|
## choice.
|
|
|
|
## The choice of head affects the choice of finalization point - the order
|
|
|
|
## of operations naturally becomes important here - after updating the head,
|
|
|
|
## blocks that were once considered potential candidates for a tree will
|
|
|
|
## now fall from grace, or no longer be considered resolved.
|
2019-12-19 13:02:28 +00:00
|
|
|
doAssert newHead.parent != nil or newHead.slot == 0
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "fork_choice"
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
if pool.head.blck == newHead:
|
2019-09-12 01:45:04 +00:00
|
|
|
info "No head block update",
|
2019-12-19 13:02:28 +00:00
|
|
|
headBlockRoot = shortLog(newHead.root),
|
|
|
|
headBlockSlot = shortLog(newHead.slot),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "fork_choice"
|
2019-03-22 11:33:10 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
return
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
let
|
|
|
|
lastHead = pool.head
|
2019-12-19 13:02:28 +00:00
|
|
|
pool.db.putHeadBlock(newHead.root)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
|
|
|
# Start off by making sure we have the right state
|
2019-12-19 13:02:28 +00:00
|
|
|
updateStateData(
|
|
|
|
pool, pool.headState, BlockSlot(blck: newHead, slot: newHead.slot))
|
|
|
|
|
|
|
|
let
|
|
|
|
justifiedSlot = pool.headState.data.data
|
|
|
|
.current_justified_checkpoint
|
|
|
|
.epoch
|
|
|
|
.compute_start_slot_at_epoch()
|
|
|
|
justifiedBS = newHead.findAncestorBySlot(justifiedSlot)
|
|
|
|
|
|
|
|
pool.head = Head(blck: newHead, justified: justifiedBS)
|
|
|
|
updateStateData(pool, pool.justifiedState, justifiedBS)
|
|
|
|
|
|
|
|
# TODO isAncestorOf may be expensive - too expensive?
|
|
|
|
if not lastHead.blck.isAncestorOf(newHead):
|
2019-11-25 17:50:35 +00:00
|
|
|
info "Updated head block (new parent)",
|
2019-05-01 09:19:29 +00:00
|
|
|
lastHeadRoot = shortLog(lastHead.blck.root),
|
2019-12-19 13:02:28 +00:00
|
|
|
parentRoot = shortLog(newHead.parent.root),
|
|
|
|
stateRoot = shortLog(pool.headState.data.root),
|
|
|
|
headBlockRoot = shortLog(pool.headState.blck.root),
|
|
|
|
stateSlot = shortLog(pool.headState.data.data.slot),
|
|
|
|
justifiedEpoch = shortLog(pool.headState.data.data.current_justified_checkpoint.epoch),
|
|
|
|
finalizedEpoch = shortLog(pool.headState.data.data.finalized_checkpoint.epoch),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "fork_choice"
|
2019-10-22 11:57:34 +00:00
|
|
|
|
|
|
|
# A reasonable criterion for "reorganizations of the chain"
|
|
|
|
beacon_reorgs_total.inc()
|
2019-03-22 15:49:37 +00:00
|
|
|
else:
|
2019-11-25 17:50:35 +00:00
|
|
|
info "Updated head block",
|
2019-12-19 13:02:28 +00:00
|
|
|
stateRoot = shortLog(pool.headState.data.root),
|
|
|
|
headBlockRoot = shortLog(pool.headState.blck.root),
|
|
|
|
stateSlot = shortLog(pool.headState.data.data.slot),
|
|
|
|
justifiedEpoch = shortLog(pool.headState.data.data.current_justified_checkpoint.epoch),
|
|
|
|
finalizedEpoch = shortLog(pool.headState.data.data.finalized_checkpoint.epoch),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "fork_choice"
|
2019-03-13 22:59:20 +00:00
|
|
|
|
|
|
|
let
|
2019-12-19 13:02:28 +00:00
|
|
|
finalizedEpochStartSlot =
|
|
|
|
pool.headState.data.data.finalized_checkpoint.epoch.
|
|
|
|
compute_start_slot_at_epoch()
|
2019-03-13 22:59:20 +00:00
|
|
|
# TODO there might not be a block at the epoch boundary - what then?
|
2019-12-19 13:02:28 +00:00
|
|
|
finalizedHead = newHead.findAncestorBySlot(finalizedEpochStartSlot)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
doAssert (not finalizedHead.blck.isNil),
|
2019-03-13 22:59:20 +00:00
|
|
|
"Block graph should always lead to a finalized block"
|
|
|
|
|
|
|
|
if finalizedHead != pool.finalizedHead:
|
|
|
|
info "Finalized block",
|
2019-05-01 09:19:29 +00:00
|
|
|
finalizedBlockRoot = shortLog(finalizedHead.blck.root),
|
2019-08-15 16:01:55 +00:00
|
|
|
finalizedBlockSlot = shortLog(finalizedHead.slot),
|
2019-12-19 13:02:28 +00:00
|
|
|
headBlockRoot = shortLog(newHead.root),
|
|
|
|
headBlockSlot = shortLog(newHead.slot),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "fork_choice"
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-11-15 14:09:25 +00:00
|
|
|
pool.finalizedHead = finalizedHead
|
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
var cur = finalizedHead.blck
|
|
|
|
while cur != pool.finalizedHead.blck:
|
|
|
|
# Finalization means that we choose a single chain as the canonical one -
|
|
|
|
# it also means we're no longer interested in any branches from that chain
|
|
|
|
# up to the finalization point
|
|
|
|
|
|
|
|
# TODO technically, if we remove from children the gc should free the block
|
|
|
|
# because it should become orphaned, via mark&sweep if nothing else,
|
|
|
|
# though this needs verification
|
|
|
|
# TODO what about attestations? we need to drop those too, though they
|
|
|
|
# *should* be pretty harmless
|
|
|
|
# TODO remove from database as well.. here, or using some GC-like setup
|
|
|
|
# that periodically cleans it up?
|
|
|
|
for child in cur.parent.children:
|
|
|
|
if child != cur:
|
|
|
|
pool.blocks.del(child.root)
|
2019-11-22 14:14:13 +00:00
|
|
|
pool.delBlockAndState(child.root)
|
|
|
|
pool.delSlotMapping(child)
|
|
|
|
else:
|
|
|
|
pool.delFinalizedStateIfNeeded(child)
|
2019-05-01 09:19:29 +00:00
|
|
|
cur.parent.children = @[cur]
|
|
|
|
cur = cur.parent
|
|
|
|
|
|
|
|
let hlen = pool.heads.len
|
|
|
|
for i in 0..<hlen:
|
|
|
|
let n = hlen - i - 1
|
2019-12-13 13:54:26 +00:00
|
|
|
if pool.heads[n].blck.slot < pool.finalizedHead.blck.slot:
|
|
|
|
# By definition, the current head should be newer than the finalized
|
|
|
|
# head, so we'll never delete it here
|
2019-05-01 09:19:29 +00:00
|
|
|
pool.heads.del(n)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
# Calculate new tail block and set it
|
|
|
|
# New tail should be WEAK_SUBJECTIVITY_PERIOD * 2 older than finalizedHead
|
|
|
|
const tailSlotInterval = WEAK_SUBJECTVITY_PERIOD * 2
|
|
|
|
if finalizedEpochStartSlot - GENESIS_SLOT > tailSlotInterval:
|
|
|
|
let tailSlot = finalizedEpochStartSlot - tailSlotInterval
|
|
|
|
let newTail = finalizedHead.blck.findAncestorBySlot(tailSlot)
|
|
|
|
pool.setTailBlock(newTail.blck)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func latestJustifiedBlock*(pool: BlockPool): BlockSlot =
|
2019-03-13 22:59:20 +00:00
|
|
|
## Return the most recent block that is justified and at least as recent
|
|
|
|
## as the latest finalized block
|
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
doAssert pool.heads.len > 0,
|
|
|
|
"We should have at least the genesis block in heaads"
|
|
|
|
doAssert (not pool.head.blck.isNil()),
|
|
|
|
"Genesis block will be head, if nothing else"
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2019-05-01 09:19:29 +00:00
|
|
|
# Prefer stability: use justified block from current head to break ties!
|
2019-09-01 15:02:49 +00:00
|
|
|
result = pool.head.justified
|
2019-05-01 09:19:29 +00:00
|
|
|
for head in pool.heads[1 ..< ^0]:
|
2019-09-01 15:02:49 +00:00
|
|
|
if head.justified.slot > result.slot:
|
|
|
|
result = head.justified
|
2019-03-28 06:10:48 +00:00
|
|
|
|
|
|
|
proc preInit*(
|
2019-12-16 18:08:50 +00:00
|
|
|
T: type BlockPool, db: BeaconChainDB, state: BeaconState,
|
|
|
|
signedBlock: SignedBeaconBlock) =
|
2019-03-28 06:10:48 +00:00
|
|
|
# write a genesis state, the way the BlockPool expects it to be stored in
|
|
|
|
# database
|
|
|
|
# TODO probably should just init a blockpool with the freshly written
|
|
|
|
# state - but there's more refactoring needed to make it nice - doing
|
|
|
|
# a minimal patch for now..
|
|
|
|
let
|
2019-12-16 18:08:50 +00:00
|
|
|
blockRoot = hash_tree_root(signedBlock.message)
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
notice "New database from snapshot",
|
2019-03-28 11:22:11 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
2019-12-16 18:08:50 +00:00
|
|
|
stateRoot = shortLog(signedBlock.message.state_root),
|
2019-03-28 11:22:11 +00:00
|
|
|
fork = state.fork,
|
2019-09-12 01:45:04 +00:00
|
|
|
validators = state.validators.len(),
|
|
|
|
cat = "initialization"
|
2019-03-28 06:10:48 +00:00
|
|
|
|
|
|
|
db.putState(state)
|
2019-12-16 18:08:50 +00:00
|
|
|
db.putBlock(signedBlock)
|
2019-03-28 06:10:48 +00:00
|
|
|
db.putTailBlock(blockRoot)
|
|
|
|
db.putHeadBlock(blockRoot)
|
2019-12-16 18:08:50 +00:00
|
|
|
db.putStateRoot(
|
|
|
|
blockRoot, signedBlock.message.slot, signedBlock.message.state_root)
|