Skip to content

Commit

Permalink
feat: testnet support (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-shameem authored Mar 10, 2021
1 parent e64e240 commit dcbe66b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
5 changes: 3 additions & 2 deletions merkle-tree/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,18 @@ 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,

// web3:
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
Expand Down
7 changes: 5 additions & 2 deletions merkle-tree/src/db/common/adminDbConnection.js
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
15 changes: 12 additions & 3 deletions merkle-tree/src/filter-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}`,
);
Expand All @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions merkle-tree/src/utils-web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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})`);
Expand Down Expand Up @@ -266,4 +288,6 @@ export default {
getContractBytecode,
subscribeToEvent,
unsubscribe,
getDeployedContractTransactionHash,
getTransactionReceipt,
};
7 changes: 5 additions & 2 deletions merkle-tree/src/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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,
);
Expand Down

0 comments on commit dcbe66b

Please sign in to comment.