EYBlockchain / timber

Construct a Merkle Tree database from Ethereum logs.
Other
67 stars 19 forks source link

Multiple trees per contract #16

Closed MirandaWood closed 4 years ago

MirandaWood commented 4 years ago

treeids

Adding functionality to allow multiple trees for one smart contract (for our ZVM/Midnight work). Timber will still work for contracts with one tree (e.g. Nightfall). For example: MerkleTreeControllerSHAZVM in merkle-tree/config/default.js has two trees, named a and b with their own events to listen for and heights:

// contracts to filter:
contracts: {
  // contract name:
  MerkleTreeControllerSHAZVM: {
    treeId: {
      a: {
        treeHeight: 16,
        events: {
          // filter for the following event names:
          NewLeafA: {
            // filter for these event parameters:
            parameters: ['leafIndex', 'leafValue'],
          },
          NewLeavesA: {
            // filter for these event parameters:
            parameters: ['minLeafIndex', 'leafValues'],
          },
        },
      },
      b: {
        treeHeight: 10,
        events: {
          // filter for the following event names:
          NewLeafB: {
            // filter for these event parameters:
            parameters: ['leafIndex', 'leafValue'],
          },
          NewLeavesB: {
            // filter for these event parameters:
            parameters: ['minLeafIndex', 'leafValues'],
          },
        },
      },
    },
  },
},

These work alongside the usual MerkleTreeController contracts and event listeners.

User

The user must specify treeid in the request body for a contract defined in the config as having more than one tree (otherwise Timber does not know which collection to point to). Everything else works as before.

Changes

Behind the scenes, the specific treeHeight is checked to match that of the config whenever a new db connection starts. The collection's name is set to contractName_treeid rather than just contract. Also, the specific event names are bound to the treeid when the filter is started. As long as the single leaf event contains the term 'Leaf' and the multiple one contains 'Leaves' they can have any unique name.

A new test (MerkleTreeControllerZVM.test.js) and instructions have been added (which adds different leaves to two trees on the same contract), plus some new postman tests. For now, this test only works with SHA hashing (chosen for cheaper gas costs).

iAmMichaelConnor commented 4 years ago

MerkleTreeControllerSHAZVM

Pedantic one: please could we remove any references to specific projects (such as the ZVM)? Timber is generic, so perhaps something like 'MultipleMerkleTreesController' (or some similar, pithier name)?

As long as the single leaf event contains the term 'Leaf' and the multiple one contains 'Leaves' they can have any unique name

Is this strict naming requirement enforced somewhere in the code? I'd have thought the event names could be 'Cheese' and 'Cake' and Timber would happily listen for them and create a DB accordingly. I could be wrong.

For now, this test only works with SHA hashing (chosen for cheaper gas costs).

Is it just that we don't have a test for MiMC? Or does this new 'multiple trees' functionality only work for SHA256?

I've only read the description, so I'll take a look at the code and updated README now :)

MirandaWood commented 4 years ago

MerkleTreeControllerSHAZVM

Pedantic one: please could we remove any references to specific projects (such as the ZVM)? Timber is generic, so perhaps something like 'MultipleMerkleTreesController' (or some similar, pithier name)?

Yes - I'll get on doing this now, they were just placeholders!

As long as the single leaf event contains the term 'Leaf' and the multiple one contains 'Leaves' they can have any unique name

Is this strict naming requirement enforced somewhere in the code? I'd have thought the event names could be 'Cheese' and 'Cake' and Timber would happily listen for them and create a DB accordingly. I could be wrong.

Originally the functions were hardcoded to look for 'NewLeaf' and 'NewLeaves', so I made them a bit more general. However we could have the first listed event to be single leaves and the second multiple leaves?

For now, this test only works with SHA hashing (chosen for cheaper gas costs).

Is it just that we don't have a test for MiMC? Or does this new 'multiple trees' functionality only work for SHA256?

Yes, it's just the test. It wouldn't be too much effort to make another contract like 'MultipleMerkleTreesMiMC' if that would be useful?

iAmMichaelConnor commented 4 years ago

Yes, it's just the test. It wouldn't be too much effort to make another contract like 'MultipleMerkleTreesMiMC' if that would be useful?

No worries - a single test is fine, thanks for clarifying :)

github-actions[bot] commented 4 years ago

:tada: This PR is included in version 3.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: