convert hash_tree_roots_prefix(...) to an iterator

This commit is contained in:
Dustin Brody 2019-11-25 14:13:12 +01:00
parent c3d2634b97
commit b82328b148
2 changed files with 9 additions and 8 deletions

View File

@ -229,11 +229,12 @@ func initialize_beacon_state_from_eth1*(
# Process deposits # Process deposits
let let
leaves = deposits.mapIt(it.data) leaves = deposits.mapIt(it.data)
prefix_roots = var i = 0
hash_tree_roots_prefix(leaves, 2'i64^DEPOSIT_CONTRACT_TREE_DEPTH) for prefix_root in hash_tree_roots_prefix(
for i, deposit in deposits: leaves, 2'i64^DEPOSIT_CONTRACT_TREE_DEPTH):
state.eth1_data.deposit_root = prefix_roots[i] state.eth1_data.deposit_root = prefix_root
discard process_deposit(state, deposit, flags) discard process_deposit(state, deposits[i], flags)
i += 1
# Process activations # Process activations
for validator_index in 0 ..< state.validators.len: for validator_index in 0 ..< state.validators.len:

View File

@ -573,8 +573,8 @@ func hash_tree_root*(x: auto): Eth2Digest =
trs "HASH TREE ROOT FOR ", name(type x), " = ", "0x", $result trs "HASH TREE ROOT FOR ", name(type x), " = ", "0x", $result
func hash_tree_roots_prefix*[T](lst: openarray[T], limit: auto): iterator hash_tree_roots_prefix*[T](lst: openarray[T], limit: auto):
seq[Eth2Digest] = Eth2Digest =
# This is a particular type's instantiation of a general fold, reduce, # This is a particular type's instantiation of a general fold, reduce,
# accumulation, prefix sums, etc family of operations. As long as that # accumulation, prefix sums, etc family of operations. As long as that
# Eth1 deposit case is the only notable example -- the usual uses of a # Eth1 deposit case is the only notable example -- the usual uses of a
@ -583,7 +583,7 @@ func hash_tree_roots_prefix*[T](lst: openarray[T], limit: auto):
var merkelizer = SszChunksMerkelizer(limit: uint64(limit)) var merkelizer = SszChunksMerkelizer(limit: uint64(limit))
for i, elem in lst: for i, elem in lst:
merkelizer.addChunk(hash_tree_root(elem).data) merkelizer.addChunk(hash_tree_root(elem).data)
result.add mixInLength(merkelizer.getFinalHash(), i + 1) yield mixInLength(merkelizer.getFinalHash(), i + 1)
func lastFieldName(RecordType: type): string {.compileTime.} = func lastFieldName(RecordType: type): string {.compileTime.} =
enumAllSerializedFields(RecordType): enumAllSerializedFields(RecordType):