Skip to content

Commit

Permalink
Merge pull request #37 from EYBlockchain/westlad/wait-for-contract
Browse files Browse the repository at this point in the history
fix: autostart waits for shield contract deployment
  • Loading branch information
Westlad authored Nov 12, 2020
2 parents c0fe2bb + bd111d8 commit 0fb3bc5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Send a `post` request to `http://localhost:9000/start` to start the merkle-tree'

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.

If `AUTOSTART` is used, it will attempt to obtain a contract address and will try to do this `AUTOSTART_RETRIES` (default 20) times, every three seconds, before giving up. This is useful if the contracts are not deployed before Timber is started.

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

Send a `patch` request to `http://localhost:9000/update`. Given the leaves now stored in the mongodb, this `update` command will calculate all of the intermediate nodes from the leaves to the root, and store all of these nodes in the mongodb.
Expand Down
37 changes: 0 additions & 37 deletions merkle-tree/setup-mongo-acl-for-new-users.js

This file was deleted.

22 changes: 22 additions & 0 deletions merkle-tree/src/auto-start.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import config from 'config';
import logger from './logger';
import uw from './utils-web3';
/**
function to automatically start the Timber instance. This is useful if you are
starting it up in an already-established blockchain environment.
Expand All @@ -14,6 +15,27 @@ const autoStart = async () => {
contractAddress: config.contracts[contractName].address,
treeId: config.contracts[contractName].treeId,
};
// if we haven't been given a contractAddress, it's best to make sure that
// Timber has the ability to infer one. Otherwise Timber will fall over. If
// the build artefacts don't exist yet then we need to wait.
// Stop eslint complaining because we DO want delays in this loop
let retries = process.env.AUTOSTART_RETRIES || 20;
while (!data.contractAddress) {
try {
// eslint-disable-next-line no-await-in-loop
data.contractAddress = await uw.getContractAddress(contractName);
// we want to catch if the above function throws OR returns undefined
if (data.contractAddress === undefined) throw new Error('undefined contract address');
logger.info(`Contract address for contract ${contractName} is ${data.contractAddress}`);
} catch (err) {
if (retries === 0) throw new Error(err);
retries--;
logger.warn('Unable to find a contract address. Retrying in 3 seconds.');
// eslint-disable-next-line no-await-in-loop
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
// Now that we are fairly sure calling start will work, we can go ahead.
try {
logger.debug(
`Calling /start for Timber, with contractName '${contractName}' and url localhost`,
Expand Down
5 changes: 3 additions & 2 deletions merkle-tree/src/utils-web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// First we need to connect to a websocket provider.
// Important Note: Subscribe method only works with a websocket provider!

import fs from 'fs';
import Web3 from './web3';
import logger from './logger';

Expand Down Expand Up @@ -67,8 +68,8 @@ let events = {};
function getContractInterface(contractName) {
logger.debug(`./src/utils-web3 getContractInterface(${contractName})`);

const path = `../build/contracts/${contractName}.json`;
const contractInterface = require(path); // eslint-disable-line global-require, import/no-dynamic-require
const path = `./build/contracts/${contractName}.json`;
const contractInterface = JSON.parse(fs.readFileSync(path));
logger.silly(`contractInterface: ${JSON.stringify(contractInterface, null, 2)}`);
return contractInterface;
}
Expand Down
4 changes: 2 additions & 2 deletions merkle-tree/src/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default {
config.web3.options,
);

provider.on('error', console.error);
provider.on('error', err => logger.error(err));
provider.on('connect', () => logger.info('Blockchain Connected ...'));
provider.on('end', console.error);
provider.on('end', () => logger.error('Blockchain Disconnected'));

this.web3 = new Web3(provider);

Expand Down

0 comments on commit 0fb3bc5

Please sign in to comment.