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

Add finalize like method #30

Open
drbh opened this issue Apr 12, 2020 · 0 comments
Open

Add finalize like method #30

drbh opened this issue Apr 12, 2020 · 0 comments

Comments

@drbh
Copy link

drbh commented Apr 12, 2020

This is an awesome library! Great work.

In my use case - I'd like to produce a single root regardless of the input size (similar to the Bitcoin implementation)

Looking at the JS library, it seems like there was a PR open to add this exact functionality https://github.com/mafintosh/merkle-tree-stream/pull/4/files

I've forked the repo but am having trouble translating the code. Would you be able to either help me implement this feature or implement it yourself?

The logic doesn't seem too complicated - I am just get hung up on creating the correct struct to append on the root vector.

Here is the start of my attempt

while self.roots.len() > 1 {
  let leaf = {
    let left = &self.roots[self.roots.len() - 2];
    let right = &self.roots[self.roots.len() - 1];

    if left.parent() != right.parent() {
      if (!merge) {
        break;
      }

      let partial_n = match &leaf.data() {
        NodeKind::Parent => PartialNode {
          index: flat::sibling(right.index()),
          parent: right.parent(),
          length: right.len(),
          data: NodeKind::Leaf(data.to_vec()),
        },
        NodeKind::Leaf(data) => PartialNode {
          index: flat::sibling(right.index()),
          parent: right.parent(),
          length: right.len(),
          data: NodeKind::Leaf(data.to_vec()),
        },
      };

      H::Node::from(NodeParts {
        node: partial_n,
        hash: hash, // Problem cloning hash??
      });
    }

    let hash = self.handler.parent(left, right);
    let partial = PartialNode {
      index: left.parent(),
      parent: flat::parent(left.parent()) as u64,
      length: left.len() + right.len(),
      data: NodeKind::Parent,
    };

    H::Node::from(NodeParts {
      node: partial,
      hash,
    })
  };

Thanks again!

@drbh drbh changed the title Add finalize like methof Add finalize like method Apr 14, 2020
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

No branches or pull requests

1 participant