reduce stack space usage in process_final_updates(...) (#925)

* reduce stack space usage in process_final_updates(...) to avoid fuzzed segfault in https://github.com/status-im/nim-beacon-chain/issues/921

* document motivation behind manually constructing hash_tree_root of a HistoricalBatch
This commit is contained in:
tersec 2020-04-23 11:39:38 +00:00 committed by GitHub
parent 7e94482409
commit 7cd4b0bfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions

View File

@ -410,11 +410,12 @@ func process_final_updates*(state: var BeaconState) {.nbench.}=
# Set historical root accumulator
if next_epoch mod (SLOTS_PER_HISTORICAL_ROOT div SLOTS_PER_EPOCH).uint64 == 0:
let historical_batch = HistoricalBatch(
block_roots: state.block_roots,
state_roots: state.state_roots,
)
state.historical_roots.add (hash_tree_root(historical_batch))
# Equivalent to hash_tree_root(foo: HistoricalBatch), but without using
# significant additional stack or heap.
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#historicalbatch
# In response to https://github.com/status-im/nim-beacon-chain/issues/921
state.historical_roots.add hash_tree_root(
[hash_tree_root(state.block_roots), hash_tree_root(state.state_roots)])
# Rotate current/previous epoch attestations
state.previous_epoch_attestations = state.current_epoch_attestations