Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gas and nonce management for Ethereum #640

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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