Skip to content

Commit

Permalink
Merge pull request #158 from EYBlockchain/fabio/aquasec-update
Browse files Browse the repository at this point in the history
Aquasec update merge
  • Loading branch information
SwatiEY committed Sep 11, 2024
2 parents d3adadb + 5a911ab commit 3cac884
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 175 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/release-multiple-contracts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Release multiple-contracts

on:
push:
branches:
- multiple-contracts

jobs:
release:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Login to Github Packages
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# - uses: codfish/semantic-release-action@master
# id: semantic
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

# - name: Build docker merkle-tree image
# run: docker build ./merkle-tree

# - run: echo ${{ steps.semantic.outputs.release-version }}

- name: Build and push UI docker image
uses: docker/build-push-action@v2
with:
context: ./merkle-tree
build-args: |
GPR_TOKEN=${{ secrets.GPR_TOKEN }}
tags: |
ghcr.io/eyblockchain/timber-multicontract:latest
# username: ${{ github.actor }}
# password: ${{ secrets.GITHUB_TOKEN }}
push: true
6 changes: 5 additions & 1 deletion merkle-tree/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ module.exports = {
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,
autoReconnectInterval: process.env.BLOCKCHAIN_RECONNECT_INTERVAL || 1000,
options: {
defaultAccount: '0x0',
defaultBlock: '0', // e.g. the genesis block our blockchain
Expand All @@ -165,5 +165,9 @@ module.exports = {
// transactionSigner: new CustomTransactionSigner()
},
},

isLoggerEnabled: true,
LOG_LEVEL: process.env.LOG_LEVEL || 'info',

REUSE_FILTERS: process.env.REUSE_FILTERS || false,
};
50 changes: 32 additions & 18 deletions merkle-tree/src/contract-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ async function getContractInstanceFromRemote(db, contractName) {
`contractAddress ${contractAddress} not yet added to the merkle-tree's metadata db. Adding it now...`,
);
await metadataService.insertContractAddress({ contractAddress });
} else if (contractAddress !== contractInstance._address) { // eslint-disable-line no-underscore-dangle, prettier/prettier
} else if (contractAddress !== contractInstance._address) {
// eslint-disable-line no-underscore-dangle, prettier/prettier
// here, we've noticed that the stored mongodb contractAddress !== the address retrieved from the external microservice.
throw new Error(
`Unexpected mismatch between the stored mongodb contractAddress (${contractAddress}), and the address retrieved from the external microservice (${contractInstance._address}).`, // eslint-disable-line no-underscore-dangle, prettier/prettier
Expand Down Expand Up @@ -105,37 +106,50 @@ async function getContractInstanceFromContractsFolder(db, contractName, contract
return contractInstance;
}

async function getContractInstanceFromBuildFolder(db, contractName, contractAddress) {
async function getContractInstanceFromBuildFolder(db, contractName, contractAddress, contractId) {
logger.debug(
`\nsrc/contract-controller getContractInstanceFromBuildFolder(db, contractName=${contractName})`,
);

const metadataService = new MetadataService(db);
if (!contractId) {
const metadataService = new MetadataService(db);

// if no address specified in the API call, then let's try to get one from the contract's json interface in the build folder"
if (contractAddress === undefined)
contractAddress = await utilsWeb3.getContractAddress(contractName); // eslint-disable-line no-param-reassign
// if no address specified in the API call, then let's try to get one from the contract's json interface in the build folder"
if (contractAddress === undefined)
contractAddress = await utilsWeb3.getContractAddress(contractName); // eslint-disable-line no-param-reassign

if (contractAddress === undefined)
throw new Error(
`No deployed contract address found in the contract interface json for ${contractName}`,
);
if (contractAddress === undefined)
throw new Error(
`No deployed contract address found in the contract interface json for ${contractName}`,
);

// retrieve the contract from the 'build' folder, and create a web3 contract instance:
const contractInstance = await utilsWeb3.getContractInstance(contractName, contractAddress);
// retrieve the contract from the 'build' folder, and create a web3 contract instance:
const contractInstance = await utilsWeb3.getContractInstance(contractName, contractAddress);

// Then add its address to the db:
logger.info(`Adding contractAddress ${contractAddress} to the merkle-tree's metadata db...`);
await metadataService.insertContractAddress({ contractAddress });
// Then add its address to the db:
logger.info(`Adding contractAddress ${contractAddress} to the merkle-tree's metadata db...`);
await metadataService.insertContractAddress({ contractAddress });

return contractInstance;
return contractInstance;
} else {
const metadataService = new MetadataService(db);
logger.info(`Adding contractAddress ${contractAddress} to the merkle-tree's metadata db...`);
await metadataService.insertContractAddress({ contractAddress });

const contractInstance = await utilsWeb3.getContractInstance(
contractName,
contractAddress,
contractId,
);
return contractInstance;
}
}

/**
Gets a web3 contract instance a contract, and checks its consistency with the merkle tree's existing metadata db.
@param {object} db - an instance of mongoose.createConnection (a 'Connection' instance in mongoose terminoligy). This contains permissions to access the merkle tree's databases.
*/
async function instantiateContract(db, contractName, contractAddress) {
async function instantiateContract(db, contractName, contractAddress, contractId) {
logger.debug(
`src/contract-controller instantiateContract(db, contractName=${contractName}, contractAddress=${contractAddress})`,
);
Expand Down Expand Up @@ -165,14 +179,14 @@ async function instantiateContract(db, contractName, contractAddress) {
contractAddress,
);
break;

default:
// 'default' - get the (truffle-compiled) contract interface json from the /app/build folder. Infer the contract's address from this json file. A web3 contract instance is created from these components and assigned to contractInstance:
logger.info(`Getting contract from contractOrigin '/app/build/'...`);
contractInstance = await getContractInstanceFromBuildFolder(
db,
contractName,
contractAddress,
contractId,
);
}
return contractInstance;
Expand Down
14 changes: 8 additions & 6 deletions merkle-tree/src/db/common/adminDbConnection.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import mongoose from 'mongoose';
import config from 'config';

mongoose.set('useNewUrlParser', true);
mongoose.set('useUnifiedTopology', true);
const { host, port, databaseName, dbUrl } = config.get('mongo');
const dbConnections = {};

let url;
if (dbUrl) url = dbUrl;
else url = `mongodb://${host}:${port}`;
if (dbUrl)
url = dbUrl;
else
url = `mongodb://${host}:${port}/${databaseName}`;

dbConnections.admin = mongoose.createConnection(`${url}/${databaseName}`, {
useNewUrlParser: true,
useCreateIndex: true,
});
dbConnections.admin = mongoose.createConnection(`${url}`);

const adminDbConnection = dbConnections.admin;

Expand Down
2 changes: 1 addition & 1 deletion merkle-tree/src/db/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

export const COLLECTIONS = {
NODE: 'node',
METADATA: 'metadata',
METADATA: 'metadata'
};
2 changes: 1 addition & 1 deletion merkle-tree/src/db/models/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as nodeSchema } from './node.model';
export { default as metadataSchema } from './metadata.model';
export { default as metadataSchema } from './metadata.model';
45 changes: 19 additions & 26 deletions merkle-tree/src/db/mongodb/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,33 @@ Class created from within src/middleware/assign-db-connection
@param {string} contractName - contractName of the contract which relates to this db
*/
export default class DB {
constructor(connection, username, contractName, treeId) {
constructor(connection, username, contractName, treeId, contractId) {
this.connection = connection;
this.username = username;
this.contractId = contractId;
if (!username) return;
this.createModelsForUser(contractName, treeId);
this.createModelsForUser(contractName, treeId, contractId);
}

/**
A model is a class with which we construct documents
*/
createModelsForUser(contractName, treeId) {
if (treeId === undefined || treeId === '') {
this.Models = {
node: this.connection.model(
`${this.username}_${contractName}_${COLLECTIONS.NODE}`,
nodeSchema,
),
metadata: this.connection.model(
`${this.username}_${contractName}_${COLLECTIONS.METADATA}`,
metadataSchema,
),
};
} else {
this.Models = {
node: this.connection.model(
`${this.username}_${contractName}_${treeId}_${COLLECTIONS.NODE}`,
nodeSchema,
),
metadata: this.connection.model(
`${this.username}_${contractName}_${treeId}_${COLLECTIONS.METADATA}`,
metadataSchema,
),
};
}
createModelsForUser(contractName, treeId, contractId) {
logger.debug(
`Creating Models. contractName: ${contractName}, treeId: ${treeId}, contractId: ${contractId}`,
);
const contractIdStr = contractId ? `_${contractId}` : ``;
const treeIdStr = treeId ? `_${treeId}` : ``;
this.Models = {
node: this.connection.model(
`${this.username}_${contractName}${treeIdStr}${contractIdStr}_${COLLECTIONS.NODE}`,
nodeSchema,
),
metadata: this.connection.model(
`${this.username}_${contractName}${treeIdStr}${contractIdStr}_${COLLECTIONS.METADATA}`,
metadataSchema,
),
};
}

/**
Expand Down
1 change: 0 additions & 1 deletion merkle-tree/src/db/service/leaf.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export default class LeafService {
const docs = await this.db.getDoc(COLLECTIONS.NODE, {
value,
});

return docs;
}

Expand Down
Loading

0 comments on commit 3cac884

Please sign in to comment.