From acf72a5d18801f35db10fb8f46348601c99a8366 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Thu, 28 Nov 2019 12:35:42 -0800 Subject: [PATCH] fix(quorum/api): contract deploy uses gas estimate Instead of hardcoding guesses regarding what the CI environment will produce as a gas price for the contract deployment be pro-active, ask for a quote and then offer to spend the quote + a million gas to be on the safe side. This should guarantee that there will be enough gas unless there's a bug in the underlying network (which has happened in the past, quorum had a gas bug where it was asking for more than the maximum possible gas). Also downgraded quorum to v2.2.5 because that is considered stabl while 2.3.0 is a beta release and therefore it is possible that has issues with the gas estimates like previous versions did. Changed the gas upper and lower bounds for blocks through the newly introduced CLI flags of Quorum eth: https://github.com/jpmorganchase/quorum/issues/700#issue-438849481 https://github.com/jpmorganchase/quorum/blob/v2.2.5/cmd/utils/flags.go#L333 https://github.com/jpmorganchase/quorum/blob/v2.2.5/cmd/utils/flags.go#L343 Signed-off-by: Peter Somogyvari --- .../quorum/api/scripts/deploy-contracts.js | 3 +++ .../quorum/api/utils/contracts.js | 24 +++++++++++-------- .../quorum/api/wrappers/actor.wrapper.js | 17 ++++++++----- .../quorum/platform/docker-compose.yml | 6 ++++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/packages/core/examples/simple-asset-transfer/quorum/api/scripts/deploy-contracts.js b/packages/core/examples/simple-asset-transfer/quorum/api/scripts/deploy-contracts.js index a12bc6a0c19..b7531427b07 100755 --- a/packages/core/examples/simple-asset-transfer/quorum/api/scripts/deploy-contracts.js +++ b/packages/core/examples/simple-asset-transfer/quorum/api/scripts/deploy-contracts.js @@ -38,6 +38,9 @@ module.exports = (async () => { } tryCount += 1; } + if (notSuccessful) { + throw new Error(`Failed to deploy contracts after ${maxTries} attempts. See logs above for details.`); + } if (config.env !== 'test') { /* Forcibly exit the process as we suspect that web3.js may leave dangling open network connections that prevent normal exit. */ diff --git a/packages/core/examples/simple-asset-transfer/quorum/api/utils/contracts.js b/packages/core/examples/simple-asset-transfer/quorum/api/utils/contracts.js index c14a97a2c19..ed2a9ea32fd 100644 --- a/packages/core/examples/simple-asset-transfer/quorum/api/utils/contracts.js +++ b/packages/core/examples/simple-asset-transfer/quorum/api/utils/contracts.js @@ -51,16 +51,20 @@ const Contracts = { }, async newPublicContract(contractObject, ...contractArguments) { - const newContract = await contractObject - .deploy({ - arguments: contractArguments, - }) - .send({ - from: web3Ws.eth.defaultAccount, - gas: 10000000, - }) - .on('transactionHash', txHash => logger.log('info', `newPublicContract: txHash:${txHash}`)); - logger.log('debug', `newPublicContract: ${newContract.options.address}`); + const deployTask = contractObject.deploy({ arguments: contractArguments }); + const gasEstimate = await deployTask.estimateGas({ + from: web3Ws.eth.defaultAccount, + gas: 10000000, + }); + const gas = gasEstimate * 3; // offer triple the gas estimate to be sure + const taskPayload = { + from: web3Ws.eth.defaultAccount, + gas, + }; + logger.info(`newPublicContract: gasEstimate=%s taskPayload=%s`, gasEstimate, JSON.stringify(taskPayload)); + const newContract = await deployTask.send(taskPayload); + + logger.log('debug', `newPublicContract: deployment DONE. address=${newContract.options.address}`); return newContract; }, diff --git a/packages/core/examples/simple-asset-transfer/quorum/api/wrappers/actor.wrapper.js b/packages/core/examples/simple-asset-transfer/quorum/api/wrappers/actor.wrapper.js index 9ae8604b76d..f484f4d89ef 100644 --- a/packages/core/examples/simple-asset-transfer/quorum/api/wrappers/actor.wrapper.js +++ b/packages/core/examples/simple-asset-transfer/quorum/api/wrappers/actor.wrapper.js @@ -22,12 +22,17 @@ async function registerActor({ name, type, constKey, ethAddress, host, port }) { host, port ); - const receipt = await rootContract.methods - .registerActor(name, constKey, ethAddress, ActorTypes[type], host, port) - .send({ - from: web3.eth.defaultAccount, - gas: 3000000, - }); + const actorRegistrationTask = rootContract.methods + .registerActor(name, constKey, ethAddress, ActorTypes[type], host, port); + + const gasEstimate = await actorRegistrationTask.estimateGas(); + const gas = gasEstimate * 3; // offer triple the gas estimate to be sure + const taskPayload = { + from: web3.eth.defaultAccount, + gas, + }; + logger.info(`registerActor gasEstimate=%s taskPayload=%s`, gasEstimate, JSON.stringify(taskPayload)); + const receipt = await actorRegistrationTask.send(taskPayload); logger.log('debug', 'registerActor receipt %j', receipt); return receipt; } diff --git a/packages/core/examples/simple-asset-transfer/quorum/platform/docker-compose.yml b/packages/core/examples/simple-asset-transfer/quorum/platform/docker-compose.yml index 9708e29f841..5b923299897 100644 --- a/packages/core/examples/simple-asset-transfer/quorum/platform/docker-compose.yml +++ b/packages/core/examples/simple-asset-transfer/quorum/platform/docker-compose.yml @@ -10,7 +10,7 @@ version: "3.6" x-quorum-def: &quorum-def restart: "on-failure" - image: "${QUORUM_DOCKER_IMAGE:-quorumengineering/quorum:2.3.0}" + image: "${QUORUM_DOCKER_IMAGE:-quorumengineering/quorum:2.2.5}" expose: - "21000" - "50400" @@ -71,6 +71,10 @@ x-quorum-def: --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,${QUORUM_CONSENSUS:-istanbul} \ --port 21000 \ --unlock 0 \ + --mine \ + --miner.threads 1 \ + --miner.gaslimit 60000000 \ + --miner.gastarget 10 \ --password /examples/passwords.txt \ ${QUORUM_GETH_ARGS:-} $${GETH_ARGS_${QUORUM_CONSENSUS:-istanbul}} x-tx-manager-def: