From bde20436e86fc9f72398c9b2b46414497d9a1cd6 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 26 Nov 2019 15:32:45 +0100 Subject: [PATCH] ~50% speedup by fast-pathing Eth2Digest merkle hashing --- beacon_chain/spec/beaconstate.nim | 4 ++-- beacon_chain/ssz.nim | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 724ea3528..19b3c2e92 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -258,8 +258,8 @@ func is_valid_genesis_state*(state: BeaconState): bool = return false return true -# TODO candidate for spec? -# https://github.com/ethereum/eth2.0-specs/blob/0.5.1/specs/core/0_beacon-chain.md#on-genesis +# TODO this is now a non-spec helper function, and it's not really accurate +# so only usable/used in research/ and tests/ func get_initial_beacon_block*(state: BeaconState): BeaconBlock = BeaconBlock( slot: GENESIS_SLOT, diff --git a/beacon_chain/ssz.nim b/beacon_chain/ssz.nim index bd577cf4d..707980ae5 100644 --- a/beacon_chain/ssz.nim +++ b/beacon_chain/ssz.nim @@ -139,7 +139,7 @@ func writeFixedSized(c: var WriteCursor, x: auto) = c.append x else: for elem in x: - trs "WRITING FIXED SIZE ARRAY ELEMENENT" + trs "WRITING FIXED SIZE ARRAY ELEMENT" c.writeFixedSized toSszType(elem) elif x is tuple|object: enumInstanceSerializedFields(x, fieldName, field): @@ -508,7 +508,12 @@ func bitlistHashTreeRoot(merkelizer: SszChunksMerkelizer, x: BitSeq): Eth2Digest mixInLength contentsHash, x.len func hashTreeRootImpl[T](x: T): Eth2Digest = - when (T is BasicType) or (when T is array: ElemType(T) is BasicType else: false): + when (when T is array: ElemType(T) is byte and + sizeof(T) == sizeof(Eth2Digest) else: false): + # TODO is this sizeof comparison guranteed? it's whole structure vs field + trs "ETH2DIGEST; IDENTITY MAPPING" + Eth2Digest(data: x) + elif (T is BasicType) or (when T is array: ElemType(T) is BasicType else: false): trs "FIXED TYPE; USE CHUNK STREAM" merkelizeSerializedChunks x elif T is string or (when T is (seq|openarray): ElemType(T) is BasicType else: false):