Skip to content

Commit

Permalink
Merge pull request #627 from shemnon/ethereum-gas
Browse files Browse the repository at this point in the history
Hard coded ethereum gas values
  • Loading branch information
aklenik authored Nov 4, 2019
2 parents 7072eb1 + 7d07c06 commit 0bdce67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
38 changes: 28 additions & 10 deletions packages/caliper-ethereum/lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ class Ethereum extends BlockchainInterface {
logger.info('Creating contracts...');
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;
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;
resolve(contractInstance);
}));
}
Expand All @@ -82,17 +84,24 @@ class Ethereum extends BlockchainInterface {
* Return the Ethereum context associated with the given callback module name.
* @param {string} name The name of the callback module as defined in the configuration files.
* @param {object} args Unused.
* @param {integer} clientIdx the client index
* @return {object} The assembled Ethereum context.
* @async
*/
async getContext(name, args) {
let context = {fromAddress: this.ethereumConfig.fromAddress};
context.web3 = this.web3;
context.contracts = {};
async getContext(name, args, clientIdx) {
let context = {
clientIdx: clientIdx,
contracts: {},
fromAddress: this.ethereumConfig.fromAddress,
nonces: {},
web3: this.web3,
};
for (const key of Object.keys(args.contracts)) {
context.contracts[key] = new this.web3.eth.Contract(args.contracts[key].abi, args.contracts[key].address);
context.contracts[key] = {
contract: new this.web3.eth.Contract(args.contracts[key].abi, args.contracts[key].address),
gas: args.contracts[key].gas
};
}
context.nonces = {};
context.nonces[this.ethereumConfig.fromAddress] = await this.web3.eth.getTransactionCount(this.ethereumConfig.fromAddress);
if (this.ethereumConfig.fromAddressPrivateKey) {
this.web3.eth.accounts.wallet.add(this.ethereumConfig.fromAddressPrivateKey);
Expand Down Expand Up @@ -170,6 +179,7 @@ class Ethereum extends BlockchainInterface {
async sendTransaction(context, contractID, contractVer, methodCall, timeout) {
let status = new TxStatus();
let params = {from: context.fromAddress};
let contractInfo = context.contracts[contractID];
try {
context.engine.submitCallback(1);
let receipt = null;
Expand All @@ -182,11 +192,19 @@ class Ethereum extends BlockchainInterface {
params.nonce = nonce;
}
if (methodCall.args) {
params.gas = 1000 + await context.contracts[contractID].methods[methodCall.verb](...methodCall.args).estimateGas();
receipt = await context.contracts[contractID].methods[methodCall.verb](...methodCall.args)[methodType](params);
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb](...methodCall.args).estimateGas();
}
receipt = await contractInfo.contract.methods[methodCall.verb](...methodCall.args)[methodType](params);
} else {
params.gas = 1000 + await context.contracts[contractID].methods[methodCall.verb].estimateGas(params);
receipt = await context.contracts[contractID].methods[methodCall.verb]()[methodType](params);
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
} else {
params.gas = 1000 + await contractInfo.contract.methods[methodCall.verb].estimateGas(params);
}
receipt = await contractInfo.contract.methods[methodCall.verb]()[methodType](params);
}
status.SetID(receipt.transactionHash);
status.SetResult(receipt);
Expand Down
15 changes: 10 additions & 5 deletions packages/caliper-samples/network/besu/1node-clique/ethereum.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"caliper": {
"blockchain": "ethereum",
"command" : {
"command": {
"start": "docker-compose -f network/besu/1node-clique/docker-compose.yml up -d && sleep 10",
"end" : "docker-compose -f network/besu/1node-clique/docker-compose.yml down"
}
"end": "docker-compose -f network/besu/1node-clique/docker-compose.yml down"
}
},
"ethereum": {
"url": "http://localhost:8545",
Expand All @@ -15,8 +15,13 @@
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
"path": "src/contract/ethereum/simple/simple.json"
"path": "src/contract/ethereum/simple/simple.json",
"gas": {
"open": 45000,
"query": 100000,
"transfer": 36000
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
"path": "src/simple/simple.json"
"path": "src/simple/simple.json",
"gas": {
"open": 45000,
"query": 100000,
"transfer": 36000
}
}
}
}
Expand Down

0 comments on commit 0bdce67

Please sign in to comment.