Skip to content

Commit

Permalink
fix: now supports mutiple contracts and treeIds
Browse files Browse the repository at this point in the history
  • Loading branch information
Westlad committed Nov 4, 2020
1 parent 805a1f0 commit 5d3c88e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
rules: {
'no-console': 'off',
'no-plusplus': 'off',
'no-restricted-syntax': 'off',
'import/no-extraneous-dependencies': [
'error',
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Interact with the merkle-tree microservice through its API. A postman collection

Send a `post` request to `http://localhost:9000/start` to start the merkle-tree's event filters for `NewLeaf` and `NewLeaves` events. Any 'new leaf' events will be picked up by the filters, and cause the new leaf data to be inserted into the mongodb.

If you only have a single 'shield' contract from which leaf events are emitted, and you are not using multiple Merkle trees, you can ask Timber to start automatically by setting the environment variable `AUTOSTART` to the name of the contract that is emitting leaf events. This is useful when you are deploying another Timber instance to an already-running ZKP solution.
You can ask Timber to start automatically by setting the environment variable `AUTOSTART` to anything that is truthy. This is useful when you are deploying another Timber instance to an already-running ZKP solution. In this case, Timber will take contract (name, address, treeId) from its config file. Depending on your configuration, not all of these may be needed. Often Timber can infer the contract address from build artefacts.

##### Update the merkle-tree database:

Expand Down
33 changes: 19 additions & 14 deletions merkle-tree/src/auto-start.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import axios from 'axios';
import config from 'config';
import logger from './logger';
/**
function to automatically start the Timber instance. This is useful if you are
starting it up in an already-established blockchain environment.
Currently it only supports a single shield contract.
*/
const autoStart = async contractName => {
const data = { contractName };
if (process.env.CONTRACT_ADDRESS) data.contractAddress = process.env.CONTRACT_ADDRESS;
try {
logger.debug(
`Calling /start for Timber, with contractName '${contractName}' and url localhost`,
);
const response = await axios.post('http://localhost/start', data, {
timeout: 3600000,
});
logger.debug('Timber Response:', response.data.data);
return response.data.data;
} catch (error) {
throw new Error(error);
const autoStart = async () => {
const contractNames = Object.keys(config.contracts);
for (const contractName of contractNames) {
const data = {
contractName,
contractAddress: config.contracts[contractName].address,
treeId: config.contracts[contractName].treeId,
};
try {
logger.debug(
`Calling /start for Timber, with contractName '${contractName}' and url localhost`,
);
axios.post('http://localhost/start', data, {
timeout: 3600000,
});
} catch (error) {
throw new Error(error);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion merkle-tree/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ app.use(logError);

const server = app.listen(80, '0.0.0.0', () => {
logger.info('merkle-tree RESTful API server started on ::: 80');
if (process.env.AUTOSTART) autostart(process.env.AUTOSTART);
if (process.env.AUTOSTART) autostart();
});
server.timeout = 0;

0 comments on commit 5d3c88e

Please sign in to comment.