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

feat(consensus)!: add balanced binary merkle tree #5189

Merged

Conversation

Cifko
Copy link
Contributor

@Cifko Cifko commented Feb 17, 2023

Description

Implement balanced binary merkle tree and use it instead of mmr for vns. It has no effect on the base layer functionality apart for being a breaking change (it changes the vn merkle root). In the base layer only the generation is used. The proofs are used only in tari-dan (signing votes).
The advantages of MMR over binary tree is the time complexity when adding an element. But that's not our case. We regenerate the tree everytime. So it's much simpler to generate balanced binary tree. Which is the best option for this. The proof time/size for binary tree is always log n. For MMR the worst case scenario is 2*log n (when there is peak of every possible height).
The merged proofs are part of another PR.

Some comparison between balanced binary merkle tree vs merkle mountain range.
The data set I used was number of VNs of 1000 to 1M (step by 1000), committee size from 10 to 200 (step by 10) (In reality the committee can be bigger, this is representing the number of proofs).
For generating the proofs the random subset of VNs were selected (same for BMT and MMR).

  • Tree generation takes 62.03% of the time
    tree_generation
  • Generating proofs takes 42.37% of the time
    Generating merged proofs takes 85.96% of the time
    In the graph below the green represents only the merging time. But in reality we need to generate the proof anyway and then merge them (figure above takes this into account)
    proof_generation
  • Verifying proofs takes 68.98% of the time
    Verifying merged proofs takes 54.77% of the time
    verifying_proofs
  • Proof size is 76.12% of the size
    Merged proof size is 9.47% of the size
    proof_size

How Has This Been Tested?

There are couple cargo tests.

This is a breaking change : The Merkle root for VNs is different. It's equal in the genesis block, but once there is a single vn registration it starts to differ.

SWvheerden
SWvheerden previously approved these changes Feb 20, 2023
Copy link
Collaborator

@SWvheerden SWvheerden left a comment

Choose a reason for hiding this comment

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

utACK
Pending Green CI

jorgeantonio21
jorgeantonio21 previously approved these changes Feb 20, 2023
Copy link
Contributor

@jorgeantonio21 jorgeantonio21 left a comment

Choose a reason for hiding this comment

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

Very nice ! Left a few minor comments

base_layer/mmr/src/balanced_binary_merkle_proof.rs Outdated Show resolved Hide resolved
base_layer/mmr/src/balanced_binary_merkle_tree.rs Outdated Show resolved Hide resolved
@Cifko Cifko dismissed stale reviews from jorgeantonio21 and SWvheerden via d69971a February 20, 2023 10:17
@Cifko Cifko force-pushed the balanced-binary-merkle-tree branch from a3d9495 to fb95396 Compare February 21, 2023 12:31
@stringhandler stringhandler changed the title feat: add balanced binary merkle tree feat(consensus)!: add balanced binary merkle tree Feb 22, 2023
@Cifko Cifko force-pushed the balanced-binary-merkle-tree branch from fb95396 to 1550b39 Compare February 23, 2023 08:55
Comment on lines +622 to +626
let vn_bmt = ValidatorNodeBMT::create(vec![Blake256::new()
.chain(public_key.as_bytes())
.chain(shard_key.as_slice())
.finalize()
.to_vec()]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unless there's a compelling reason not to, this should use a domain-separated hasher.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just a test. The balanced tree is using domain-separated hasher. But as I'm looking at the code for the hash of VNs we are not using the domain-separated hasher there. But that's not part of this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Whoops, made this comment in the wrong spot. Sorry for the confusion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Opened an issue for this, so another PR can address it.

Copy link
Collaborator

@stringhandler stringhandler left a comment

Choose a reason for hiding this comment

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

looking good

@stringhandler stringhandler merged commit 8d34e8a into tari-project:development Feb 28, 2023
stringhandler pushed a commit that referenced this pull request Mar 3, 2023
Description
---
Removes dead validator node Merkle mountain range (MMR) code left over from [another PR](#5189) and renames a few things for clarity.

Motivation and Context
---
An earlier PR moves validator node data from an MMR to a balanced binary Merkle tree, but leaves some old MMR code around. This PR removes that code and does some minor renaming for clarity.

How Has This Been Tested?
---
Existing tests pass. It should be separately confirmed that none of the removed or renamed functionality needs to be externally available.
stringhandler pushed a commit that referenced this pull request Mar 8, 2023
### ⚠ BREAKING CHANGES

* **peer_db:** more accurate peer stats per address (5142)
* use consensus hashing API for validator node MMR (5207)
* **consensus:** add balanced binary merkle tree (5189)

### Features

* add favourite flag to contact ([5217](#5217)) ([0371b60](0371b60))
* add indexer config ([5210](#5210)) ([cf95601](cf95601))
* add merge proof for balanced binary merkle tree ([5193](#5193)) ([8962909](8962909))
* **consensus:** add balanced binary merkle tree ([5189](#5189)) ([8d34e8a](8d34e8a))
* log to base dir ([#5197](#5197)) ([5147b5c](5147b5c))
* **peer_db:** more accurate peer stats per address ([5142](#5142)) ([fdad1c6](fdad1c6))


### Bug Fixes

* add grpc commitment signature proto type ([5200](#5200)) ([d523f1e](d523f1e))
* peer seeds for esme/igor ([5202](#5202)) ([1bc226c](1bc226c))
* remove panics from merged BBMT verification ([5221](#5221)) ([a4c5fce](a4c5fce))
* source coverage ci failure ([5209](#5209)) ([80294a1](80294a1))
* use consensus hashing API for validator node MMR ([5207](#5207)) ([de28115](de28115))
* wallet reuse existing tor address ([5092](#5092)) ([576f44e](576f44e))
* **wallet:** avoids empty addresses in node identity ([5224](#5224)) ([1a66312](1a66312))
@Cifko Cifko deleted the balanced-binary-merkle-tree branch April 18, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants