diff --git a/beacon_chain/beacon_node_types.nim b/beacon_chain/beacon_node_types.nim index 47f7c8a48..9553021ee 100644 --- a/beacon_chain/beacon_node_types.nim +++ b/beacon_chain/beacon_node_types.nim @@ -209,6 +209,8 @@ type Table[tuple[a: int, b: Eth2Digest], seq[ValidatorIndex]] active_validator_indices_cache*: Table[Epoch, seq[ValidatorIndex]] + start_shard_cache*: Table[Epoch, Shard] + committee_count_cache*: Table[Epoch, uint64] BlockSlot* = object ## Unique identifier for a particular fork in the block chain - normally, diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index f0a8462e1..b9a2eec98 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -151,11 +151,18 @@ func get_crosslink_committee*(state: BeaconState, epoch: Epoch, shard: Shard, stateCache.active_validator_indices_cache[epoch] = get_active_validator_indices(state, epoch) + if epoch notin stateCache.start_shard_cache: + stateCache.start_shard_cache[epoch] = get_start_shard(state, epoch) + + if epoch notin stateCache.committee_count_cache: + stateCache.committee_count_cache[epoch] = get_committee_count(state, epoch) + compute_committee( stateCache.active_validator_indices_cache[epoch], get_seed(state, epoch), - (shard + SHARD_COUNT - get_start_shard(state, epoch)) mod SHARD_COUNT, - get_committee_count(state, epoch), + (shard + SHARD_COUNT - stateCache.start_shard_cache[epoch]) mod + SHARD_COUNT, + stateCache.committee_count_cache[epoch], stateCache ) @@ -165,6 +172,8 @@ func get_empty_per_epoch_cache*(): StateCache = initTable[tuple[a: int, b: Eth2Digest], seq[ValidatorIndex]]() result.active_validator_indices_cache = initTable[Epoch, seq[ValidatorIndex]]() + result.start_shard_cache = initTable[Epoch, Shard]() + result.committee_count_cache = initTable[Epoch, uint64]() # https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#get_beacon_proposer_index func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):