Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SSZ, implement incremental hash #115

Merged
merged 98 commits into from
Apr 17, 2019
Merged

Conversation

Nashatyrev
Copy link
Contributor

@Nashatyrev Nashatyrev commented Apr 6, 2019

Implement incremental tree hashing support to improve hash_tree_root(BeaconChain) performance

The central logic is in the SSZIncrementalHasher class. (incremental update of basic list is not implemented yet. See #120)
The general algorithm is pretty straightforward:

  • initially calculate full merkle tries for the BeaconChain structure and all of its non-basic children
  • keep these tries in-memory
  • track updates for the BeaconChain structure and all of its non-basic children by recording indexes of changed/added/removed children (dirty children)
  • on the next hash request - update only trie chunks corresponding to dirty children
  • recalculate only needed subset of the trie nodes
  • clear dirty children index set

Classes which want to be incrementally hashed (e.g. BeaconChain and ReadList) need to implement ObservableComposite interface and notify UpdateListener on its child changes. These classes are also in charge of properly doing their copies so e.g. if a BeaconChain instance is copied any changes made on the original instance and its fork are further processed isolated
For ReadList an ObservableComposite wrapper around standard implementation was created: ObservableListImpl

What else can be done here

  • add missing incremental updates of basic lists/vectors (UPDATE: done here Implement incremental packed list (basic type list/vector) hashing #122)
  • add ability to configure memory/performance balance. I.e. merkle trie can be kept in memory/on disk only till some depth causing less memory consumption but requiring additional hashes recalculations
  • lazy supporting structures copying to avoid unnecessary copy overhead when no forked copies are changing
  • try similar 'incremental ssz serialization' for state storage optimization

…ation class fields. Get rid of serialization workaround with ArrayList instead of ReadList in the BeaconState
…l-ssz

# Conflicts:
#	consensus/src/main/java/org/ethereum/beacon/consensus/hasher/SSZObjectHasher.java
#	consensus/src/main/java/org/ethereum/beacon/consensus/verifier/operation/DepositVerifier.java
#	core/src/main/java/org/ethereum/beacon/core/BeaconState.java
#	core/src/main/java/org/ethereum/beacon/core/state/BeaconStateImpl.java
#	core/src/main/java/org/ethereum/beacon/core/state/ImmutableBeaconStateImpl.java
#	core/src/test/java/org/ethereum/beacon/core/SSZSerializableAnnotationTest.java
#	pow/validator/src/main/java/org/ethereum/beacon/pow/validator/ValidatorRegistrationServiceImpl.java
#	ssz/src/main/java/org/ethereum/beacon/ssz/SSZCodecHasher.java
#	ssz/src/main/java/org/ethereum/beacon/ssz/SSZHashSerializer.java
#	ssz/src/main/java/org/ethereum/beacon/ssz/SSZSchemeBuilder.java
#	ssz/src/main/java/org/ethereum/beacon/ssz/access/basic/BytesCodec.java
#	test/src/test/java/org/ethereum/beacon/test/runner/SszRunner.java
@Nashatyrev Nashatyrev marked this pull request as ready for review April 15, 2019 17:47
@@ -74,12 +75,20 @@
String type() default "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is type still relevant? If yes, its description and shortcut values should be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it still relevant, since you may have java BigInteger field but want it represent uin256 SSZ type
Though some types can be removed for now, since SSZ now defines only uintN and bool

* parameter when set to <b>true</b> marks that it's such kind of field
* Indicates vector type (list with fixed length) and specifies this vector size
*/
int vectorSize() default 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about just size? It would give an opportunity of using this parameter as a size specifier not only for vectors but for other fixed sized types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups, I think it's not quite correct naming. Should rather be vectorLength. Will fix...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants