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

ethereum: prevent unnecessary additional requests #780

Merged
Merged
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
16 changes: 16 additions & 0 deletions packages/caliper-ethereum/lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ class Ethereum extends BlockchainInterface {
*/
async getContext(name, args) {
let context = {
chainId: 1,
clientIndex: this.clientIndex,
gasPrice: 0,
contracts: {},
nonces: {},
web3: this.web3
};

context.gasPrice = this.ethereumConfig.gasPrice !== undefined
? this.ethereumConfig.gasPrice
: await this.web3.eth.getGasPrice();

context.chainId = this.ethereumConfig.chainId !== undefined
? this.ethereumConfig.chainId
: await this.web3.eth.getChainId();

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),
Expand Down Expand Up @@ -211,7 +221,13 @@ class Ethereum extends BlockchainInterface {
let nonce = context.nonces[context.fromAddress];
context.nonces[context.fromAddress] = nonce + 1;
params.nonce = nonce;
// leaving these values unset causes web3 to fetch gasPrice and
// chainId on the fly. This can cause transactions to be
// reordered, which in turn causes nonce failures
params.gasPrice = context.gasPrice;
params.chainId = context.chainId;
}

if (methodCall.args) {
if (contractInfo.gas && contractInfo.gas[methodCall.verb]) {
params.gas = contractInfo.gas[methodCall.verb];
Expand Down