BRValidator

class beaconrunner.validatorlib.BRValidator(validator_index: beaconrunner.specs.ValidatorIndex)[source]

Abstract superclass from which validator behaviours inherit. Defines and maintains environment accessor functions (is the validator an attester? proposer?) Performs caching to avoid recomputing expensive operations.

In general, you are not expected to use any of the methods or attributes defined here, _except_ for validator.data, which exposes current simulation environment properties, up-to-date with respect to the validator (e.g., proposer and attester duties).

Subclasses of BRValidator must define at least two methods:

  • attest(self, known_items) -> Optional[Attestation]

  • propose(self, known_items) -> Optional[Attestation]

check_backlog(known_items: Dict[str, Sequence[remerkleable.complex.Container]]) → None[source]

Called whenever a new event happens on the network that might make a validator update their internals. We loop over known blocks and attestations to check whether we should record any that we might have discarded before, or just received.

data: ValidatorData

Current validator data. Maintained by the BRValidator methods.

forward_by(seconds: remerkleable.basic.uint64, frequency: remerkleable.basic.uint64 = 1) → None[source]

A utility method to forward the clock by a given number of seconds. Useful for exposition!

Parameters
  • self (BRValidator) – Validator

  • seconds (uint64) – Number of seconds to fast-forward by

  • frequency (uint64) – Simulation update rate

Returns

None

get_hashable_store() → beaconrunner.validatorlib.HashableSpecStore[source]

Returns a hash of the current store state.

Parameters

self (BRValidator) – Validator

Returns

A hashable representation of the current self.store

Return type

HashableSpecStore

get_head() → beaconrunner.specs.Root[source]

Our cached reimplementation of specs-defined get_head.

Parameters

self (BRValidator) – Validator

Returns

Current head according to the validator self.store

Return type

Root

head_store: Dict[beaconrunner.specs.Root, beaconrunner.specs.Root] = {}

Static cache for expensive operations. head_store stores a map from store hash to head root.

history: List[ValidatorMove, VALIDATOR_REGISTRY_LIMIT]

History of ValidatorMove by the validator.

load_state(state: beaconrunner.specs.BeaconState) → None[source]
log_attestation(item: beaconrunner.specs.Attestation) → None[source]

Recording ‘attestation proposal’ move by the validator in its history.

log_block(item: beaconrunner.specs.SignedBeaconBlock) → None[source]

Recording ‘block proposal’ move by the validator in its history.

privkey: int

Validator private key.

process_to_slot(current_head_root: beaconrunner.specs.Root, slot: beaconrunner.specs.Slot) → beaconrunner.specs.BeaconState[source]

Our cached process_slots operation.

Parameters
  • self (BRValidator) – Validator

  • current_head_root (Root) – Process to slot from this state root

  • slot (Slot) – Slot to process to

Returns

Post-state after transition to slot

Return type

BeaconState

pubkey: int

Validator public key.

record_attestation(item: beaconrunner.specs.Attestation) → bool[source]

When a validator receives an attestation from the network, they call record_attestation to see whether they should record it.

record_block(item: beaconrunner.specs.SignedBeaconBlock) → bool[source]

When a validator receives a block from the network, they call record_block to see whether they should record it.

state_store: Dict[Tuple[beaconrunner.specs.Root, beaconrunner.specs.Slot], beaconrunner.specs.BeaconState] = {}

Static cache for expensive operations. state_store stores a map from (current_state_hash, to_slot) calling process_slots(current_state, to_slot).

store: Store

Store objects are defined in the specs.

update_attest_move() → None[source]

When was the last attestation by the validator? Updates self.data.last_slot_attested.

Parameters

self (BRValidator) – Validator

Returns

None

update_attester(current_state: beaconrunner.specs.BeaconState, epoch: beaconrunner.specs.Epoch) → None[source]

This is a fairly expensive operation, so we try not to call it when we don’t have to. Update attester duties for the epoch. This can be queried no earlier than two epochs before (e.g., learn about epoch e + 2 duties at epoch t).

Parameters
  • self (BRValidator) – Validator

  • current_state (BeaconState) – The state from which proposer duties are computed

  • epoch (Epoch) – Either current_epoch or current_epoch + 1

Returns

None

update_data() → None[source]

The head may change if we recorded a new block/new attestation in the store. Attester/proposer responsibilities may change if head changes and canonical chain changes to further back from start current epoch.

---x------
    \           x is fork point
     -----

In the following
  attester = attester responsibilities for current epoch
  proposer = proposer responsibilities for current epoch

- If x after current epoch change
  (---|--x , | = start current epoch),
  proposer and attester don't change
- If x between start of previous epoch and
  start of current epoch
  (--||--x---|-- , || = start previous epoch)
  proposer changes but not attester
- If x before start of previous epoch
  (--x--||-----|----) both proposer and attester change
Parameters

self (BRValidator) – Validator

Returns

None

update_propose_move() → None[source]

When was the last block proposal by the validator? Updates self.data.last_slot_proposed.

Parameters

self (BRValidator) – Validator

Returns

None

update_proposer(current_state: beaconrunner.specs.BeaconState) → None[source]

This is a fairly expensive operation, so we try not to call it when we don’t have to. Update proposer duties for the current epoch. We need to check for each slot of the epoch whether the validator is a proposer or not.

Parameters
  • self (BRValidator) – Validator

  • current_state (BeaconState) – The state from which proposer duties are computed

Returns

None

update_time(frequency: remerkleable.basic.uint64 = 1) → None[source]

Moving validators’ clocks by one step. To keep it simple, we assume frequency is a power of ten.

Parameters
  • self (BRValidator) – Validator

  • frequency (uint64) – Simulation update rate

Returns

None

validator_index: ValidatorIndex

Validator index in the simulation.