Skip to content

Commit

Permalink
Update gas and nonce management for Ethereum
Browse files Browse the repository at this point in the history
* Estimating gas is now an explicit opt in, and secondary to explicit
  gas values.
* Nonces are only added if caliper signs the transactions
* Update explicit gas in simple transfer queries

Signed-off-by: Danno Ferrin <[email protected]>
  • Loading branch information
shemnon committed Nov 6, 2019
1 parent b6cf1eb commit b0bfcf3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
13 changes: 8 additions & 5 deletions packages/caliper-ethereum/lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ class Ethereum extends BlockchainInterface {
for (const key of Object.keys(this.ethereumConfig.contracts)) {
let contractData = require(CaliperUtils.resolvePath(this.ethereumConfig.contracts[key].path, this.workspaceRoot)); // TODO remove path property
let contractGas = this.ethereumConfig.contracts[key].gas;
let estimateGas = this.ethereumConfig.contracts[key].estimateGas;
this.ethereumConfig.contracts[key].abi = contractData.abi;
promises.push(new Promise(async function(resolve, reject) {
let contractInstance = await self.deployContract(contractData);
logger.info('Deployed contract ' + contractData.name + ' at ' + contractInstance.options.address);
self.ethereumConfig.contracts[key].address = contractInstance.options.address;
self.ethereumConfig.contracts[key].gas = contractGas;
self.ethereumConfig.contracts[key].estimateGas = estimateGas;
resolve(contractInstance);
}));
}
Expand All @@ -99,11 +101,12 @@ class Ethereum extends BlockchainInterface {
for (const key of Object.keys(args.contracts)) {
context.contracts[key] = {
contract: new this.web3.eth.Contract(args.contracts[key].abi, args.contracts[key].address),
gas: args.contracts[key].gas
gas: args.contracts[key].gas,
estimateGas: args.contracts[key].estimateGas
};
}
context.nonces[this.ethereumConfig.fromAddress] = await this.web3.eth.getTransactionCount(this.ethereumConfig.fromAddress);
if (this.ethereumConfig.fromAddressPrivateKey) {
context.nonces[this.ethereumConfig.fromAddress] = await this.web3.eth.getTransactionCount(this.ethereumConfig.fromAddress);
this.web3.eth.accounts.wallet.add(this.ethereumConfig.fromAddressPrivateKey);
} else if (this.ethereumConfig.fromAddressPassword) {
await context.web3.eth.personal.unlockAccount(this.ethereumConfig.fromAddress, this.ethereumConfig.fromAddressPassword, 1000);
Expand Down Expand Up @@ -186,22 +189,22 @@ class Ethereum extends BlockchainInterface {
let methodType = 'send';
if (methodCall.isView) {
methodType = 'call';
} else {
} else if (context.nonces && context.nonces[context.fromAddress]) {
let nonce = context.nonces[context.fromAddress];
context.nonces[context.fromAddress] = nonce + 1;
params.nonce = nonce;
}
if (methodCall.args) {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else {
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb](...methodCall.args).estimateGas();
}
receipt = await contractInfo.contract.methods[methodCall.verb](...methodCall.args)[methodType](params);
} else {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else {
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb].estimateGas(params);
}
receipt = await contractInfo.contract.methods[methodCall.verb]()[methodType](params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"gas": {
"open": 45000,
"query": 100000,
"transfer": 36000
"transfer": 70000
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"caliper": {
"blockchain": "ethereum",
"command" : {
"start": "docker-compose -f config/docker-compose.yml up -d && sleep 10",
"start": "docker-compose -f config/docker-compose.yml up -d && sleep 15",
"end" : "docker-compose -f config/docker-compose.yml down"
}
},
Expand All @@ -16,10 +16,9 @@
"contracts": {
"simple": {
"path": "src/simple/simple.json",
"estimateGas": true,
"gas": {
"open": 45000,
"query": 100000,
"transfer": 36000
"transfer": 70000
}
}
}
Expand Down

0 comments on commit b0bfcf3

Please sign in to comment.