From dcbe66b3b5095f7692ac5bf8d42071b1b3c3f7be Mon Sep 17 00:00:00 2001 From: mohammed-shameem Date: Wed, 10 Mar 2021 10:41:06 +0000 Subject: [PATCH] feat: testnet support (#46) --- merkle-tree/config/default.js | 5 ++-- .../src/db/common/adminDbConnection.js | 7 ++++-- merkle-tree/src/filter-controller.js | 15 +++++++++--- merkle-tree/src/utils-web3.js | 24 +++++++++++++++++++ merkle-tree/src/web3.js | 7 ++++-- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/merkle-tree/config/default.js b/merkle-tree/config/default.js index eef3ef1..1bf71d3 100644 --- a/merkle-tree/config/default.js +++ b/merkle-tree/config/default.js @@ -142,9 +142,10 @@ module.exports = { mongo: { host: 'mongo-merkle-tree', port: '27017', - databaseName: 'merkle_tree', + databaseName: process.env.DB_NAME || 'merkle_tree', admin: 'admin', adminPassword: 'admin', + dbUrl: process.env.DB_URL || 'mongodb://mongo-merkle-tree:27017', }, isLoggerEnabled: true, @@ -152,7 +153,7 @@ module.exports = { web3: { host: process.env.BLOCKCHAIN_HOST, port: process.env.BLOCKCHAIN_PORT, - + rpcUrl: process.env.RPC_URL, options: { defaultAccount: '0x0', defaultBlock: '0', // e.g. the genesis block our blockchain diff --git a/merkle-tree/src/db/common/adminDbConnection.js b/merkle-tree/src/db/common/adminDbConnection.js index e633716..1caf4e2 100644 --- a/merkle-tree/src/db/common/adminDbConnection.js +++ b/merkle-tree/src/db/common/adminDbConnection.js @@ -1,10 +1,13 @@ import mongoose from 'mongoose'; import config from 'config'; -const { host, port, databaseName } = config.get('mongo'); +const { host, port, databaseName, dbUrl } = config.get('mongo'); const dbConnections = {}; +let url; +if (dbUrl) url = dbUrl; +else url = `${host}:${port}`; -dbConnections.admin = mongoose.createConnection(`mongodb://${host}:${port}/${databaseName}`, { +dbConnections.admin = mongoose.createConnection(`${url}/${databaseName}`, { useNewUrlParser: true, useCreateIndex: true, }); diff --git a/merkle-tree/src/filter-controller.js b/merkle-tree/src/filter-controller.js index 9c2cb55..a1f9e43 100644 --- a/merkle-tree/src/filter-controller.js +++ b/merkle-tree/src/filter-controller.js @@ -208,7 +208,7 @@ async function filterBlock(db, contractName, contractInstance, fromBlock, treeId Check which block was the last to be filtered. @return {number} the next blockNumber which should be filtered. */ -async function getFromBlock(db) { +async function getFromBlock(db, contractName) { const metadataService = new MetadataService(db); const metadata = await metadataService.getLatestLeaf(); @@ -230,7 +230,16 @@ async function getFromBlock(db) { ); if (blockNumber === undefined) { - blockNumber = config.FILTER_GENESIS_BLOCK_NUMBER; + let receipt; + let transactionHash = await utilsWeb3.getDeployedContractTransactionHash(contractName); + logger.info(` ${contractName} deployed transactionHash: ${transactionHash}`); + + if (transactionHash) { + receipt = await utilsWeb3.getTransactionReceipt(transactionHash); + logger.info(`receipt: ${receipt}`); + } + + blockNumber = receipt ? receipt.blockNumber : config.FILTER_GENESIS_BLOCK_NUMBER; logger.warn( `No filtering history found in mongodb, so starting filter from the contract's deployment block ${blockNumber}`, ); @@ -251,7 +260,7 @@ async function start(db, contractName, contractInstance, treeId) { try { logger.info('Starting filter...'); // check the fiddly case of having to re-filter any old blocks due to lost information (e.g. due to a system crash). - const fromBlock = await getFromBlock(db); // the blockNumber we get is the next WHOLE block to start filtering. + const fromBlock = await getFromBlock(db, contractName); // the blockNumber we get is the next WHOLE block to start filtering. // Now we filter indefinitely: await filterBlock(db, contractName, contractInstance, fromBlock, treeId); diff --git a/merkle-tree/src/utils-web3.js b/merkle-tree/src/utils-web3.js index dd0708d..54472c9 100644 --- a/merkle-tree/src/utils-web3.js +++ b/merkle-tree/src/utils-web3.js @@ -44,6 +44,11 @@ async function getBlockTransactionCount(blockHashOrBlockNumber) { return blockTxCount; } +async function getTransactionReceipt(transactionHash) { + const receipt = await web3.eth.getTransactionReceipt(transactionHash); + return receipt; +} + /** Returns a block matching the block number or block hash. @param {String|Number} hashStringOrNumber A block number or hash. Or the string "genesis", "latest" or "pending" as in the default block parameter. @@ -91,6 +96,23 @@ async function getContractAddress(contractName) { return deployedAddress; } +async function getDeployedContractTransactionHash(contractName) { + logger.debug(`./src/utils-web3 getDeployedContractTransactionHash(${contractName})`); + let transactionHash; + const contractInterface = getContractInterface(contractName); + + const networkId = await web3.eth.net.getId(); + logger.silly(`networkId: ${networkId}`); + + if (contractInterface && contractInterface.networks && contractInterface.networks[networkId]) { + transactionHash = contractInterface.networks[networkId].transactionHash; + } + + logger.silly(`deployed transactionHash: ${transactionHash}`); + + return transactionHash; +} + // returns a web3 contract instance (as opposed to a truffle-contract instance) async function getContractInstance(contractName, deployedAddress) { logger.debug(`./src/utils-web3 getContractInstance(${contractName}, ${deployedAddress})`); @@ -266,4 +288,6 @@ export default { getContractBytecode, subscribeToEvent, unsubscribe, + getDeployedContractTransactionHash, + getTransactionReceipt, }; diff --git a/merkle-tree/src/web3.js b/merkle-tree/src/web3.js index 365b250..38e2e51 100644 --- a/merkle-tree/src/web3.js +++ b/merkle-tree/src/web3.js @@ -12,7 +12,10 @@ export default { connection() { return this.web3; }, - + buildUrl() { + if (config.web3.rpcUrl) return config.web3.rpcUrl; + else return `${config.web3.host}:${config.web3.port}`; + }, /** * Connects to web3 and then sets proper handlers for events */ @@ -21,7 +24,7 @@ export default { logger.info('Blockchain Connecting ...'); const provider = new Web3.providers.WebsocketProvider( - `${config.web3.host}:${config.web3.port}`, + this.buildUrl(), null, config.web3.options, );