Skip to content

Commit

Permalink
fix: startup (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
iAmMichaelConnor authored Mar 3, 2020
1 parent 4126939 commit d582dc9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docker-compose.mongodb.push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
merkle-tree:
build:
context: ./merkle-tree
dockerfile: Dockerfile
dockerfile: dev.Dockerfile
restart: on-failure
depends_on:
- ganache
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.remote.pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
merkle-tree:
build:
context: ./merkle-tree
dockerfile: Dockerfile
dockerfile: dev.Dockerfile
restart: on-failure
depends_on:
- ganache
Expand Down
8 changes: 4 additions & 4 deletions merkle-tree/src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const getSolcVersion = contractName => {
}
});

console.log('source:', source);
// console.log('source:', source);

if (Object.keys(source).length === 0 && source.constructor === Object)
throw new Error(`Contract ${contractName} not found in ${contractsPath}.`);

Expand All @@ -53,7 +54,7 @@ const buildSources = () => {
}
});

console.log('SOURCES:', sources);
// console.log('SOURCES:', sources);

return sources;
};
Expand All @@ -75,7 +76,7 @@ const createSolcInput = sources => {

const compile = (solcInstance, input) => {
const compiledOutput = solcInstance.compile(JSON.stringify(input));
console.log('COMPILED OUTPUT:', compiledOutput);
console.log('COMPILED OUTPUT:', JSON.parse(compiledOutput).errors || 'no errors :)');

const compiledContracts = JSON.parse(compiledOutput).contracts;

Expand All @@ -98,7 +99,6 @@ const loadRemoteVersionAsync = async solcVersionRelease => {
solc.loadRemoteVersion(solcVersionRelease, (err, solcInstance) => {
if (err) {
reject(err);
// throw new Error(`Error loading solc instance from solc version ${solcVersionRelease}`, err);
} else resolve(solcInstance);
});
});
Expand Down
4 changes: 3 additions & 1 deletion merkle-tree/src/db/service/metadata.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ export default class MetadataService {
async getLatestRecalculation() {
console.log('\nsrc/db/service/metadata.service getLatestRecalculation()');

const doc = await this.db.getDoc(
let doc = await this.db.getDoc(
COLLECTIONS.METADATA,
{ _id: 1 }, // 'match all' (within our one document)
['latestRecalculation', '-_id'], // return only the 'latestRecalculation' key (and exclude the _id key)
);

doc = doc || {};

return doc;
}

Expand Down
7 changes: 5 additions & 2 deletions merkle-tree/src/merkle-tree-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ async function update(db) {
let { latestLeaf } = await updateLatestLeaf(db);

// get the latest recalculation metadata (to know how up-to-date the nodes of our tree actually are):
let { latestRecalculation } = (await metadataService.getLatestRecalculation()) || {};
let { latestRecalculation } = await metadataService.getLatestRecalculation();
latestRecalculation = latestRecalculation === undefined ? {} : latestRecalculation;

const latestRecalculationLeafIndex =
latestRecalculation.leafIndex === undefined ? 0 : Number(latestRecalculation.leafIndex);
Expand All @@ -227,7 +228,9 @@ async function update(db) {
);
console.log(`\n${numberOfHashes} hashes are required to update the tree...\n\n\n\n\n`);

const { frontier } = latestRecalculation || [];
let { frontier } = latestRecalculation;
frontier = frontier === undefined ? [] : frontier;

const leaves = await leafService.getLeavesByLeafIndexRange(fromLeafIndex, toLeafIndex);
const leafValues = leaves.map(leaf => leaf.value);
const currentLeafCount = fromLeafIndex;
Expand Down
12 changes: 9 additions & 3 deletions merkle-tree/src/routes/merkle-tree.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import filterController from '../filter-controller';
import merkleTreeController from '../merkle-tree-controller';

const alreadyStarted = {}; // initialises as false
const alreadyStarting = {}; // initialises as false

/**
* Updates the entire tree based on the latest-stored leaves.
Expand All @@ -24,12 +25,15 @@ async function startEventFilter(req, res, next) {
const { contractAddress } = req.body; // contractAddress is an optional parameter. Address can instead be inferred by Timber in many cases.

try {
console.log(`already started?`);
console.log(alreadyStarted);

if (alreadyStarted[contractName]) {
res.data = { message: `filter already started for ${contractName}` };
} else if (alreadyStarting[contractName]) {
res.data = {
message: `filter is already in the process of being started for ${contractName}`,
};
} else {
alreadyStarting[contractName] = true;
console.log(`starting filter for ${contractName}`);
// get a web3 contractInstance we can work with:
const contractInstance = await contractController.instantiateContract(
db,
Expand All @@ -41,11 +45,13 @@ async function startEventFilter(req, res, next) {
const started = await filterController.start(db, contractName, contractInstance);

alreadyStarted[contractName] = started; // true/false
alreadyStarting[contractName] = false;

res.data = { message: 'filter started' };
}
next();
} catch (err) {
alreadyStarting[contractName] = false;
next(err);
}
}
Expand Down
7 changes: 0 additions & 7 deletions merkle-tree/src/routes/metadata.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ async function updateLatestLeaf(req, res, next) {

/**
* Update the latestRecalculation metadata in the tree's db.
* req.body {
* latestLeaf: {
* blockNumber: 12345678,
* leafIndex: 87654321,
* root: '0xabc1234',
* }
* }
* @param {*} req
* @param {*} res
*/
Expand Down
7 changes: 1 addition & 6 deletions merkle-tree/src/utils-web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,12 @@ async function subscribeToEvent(
) {
console.log(`\nSubscribing to event...`);
console.log(`contractName`, contractName);
// console.log(`contractInstance:`, contractInstance);
console.log(`deployedAddress`, deployedAddress);
console.log(`eventName`, eventName);
console.log(`fromBlock`, fromBlock);
// console.log(`responder`, responder);
// console.log(`responseFunction`, responseFunction);
// console.log(`responseFunctionArgs`, responseFunctionArgs);

if (!contractInstance) {
console.log(
`Contract instance not provided. Generating a contractInstance from the contractName and deployedAddress...`,
`Contract instance not provided. Generating a contractInstance from the contractName ${contractName} and deployedAddress ${deployedAddress}...`,
);
contractInstance = getContractInstance(contractName, deployedAddress); // eslint-disable-line no-param-reassign
} else {
Expand Down

0 comments on commit d582dc9

Please sign in to comment.