Reputation: 71
I am trying to benchmark a smart contract using Hyperledger Caliper. I use the below code and it works absolutely fine. However, if the contract is already deployed I am unable to interact with it using this code. I am unable to find any resources regarding that and not sure how to modify the below code.
Here is a summary of the problem:
Framework: Hyperledger Caliper
Blockchain: Ethereum
Issue: Unable to do benchmarking....in the scenario where the transaction interacts with an already deployed contract....
Network configuration*
{
"caliper": {
"blockchain": "ethereum"
},
"ethereum": {
"url": "ws://127.0.0.1:8545",
"contractDeployerAddress": "0x618.......d9",
"contractDeployerAddressPassword": "",
"contractDeployerAddressPrivateKey": "0xe8....d936",
"fromAddress": "0x6188....64d9",
"fromAddressPassword": "",
"transactionConfirmationBlocks": 12,
"contracts": {
"Storage": {
"path": "Path/to/contract/Storage.json",
"gas": {
"query": 100000,
"transfer": 70000
}
}
}
}
}
Benchmark configuration file
caliper:
blockchain: ethereum
ethereum:
url: ws://127.0.0.1:7545
from: "0x618......C64d9"
accounts:
- privateKey: "0xe8.....d936"
balance: "1000000000000000000000" # Initial balance in Wei
test:
name: basic-contract-benchmark
description: A test benchmark
workers:
type: local
number: 1
rounds:
- label: callFunction
description: Call function benchmark
txNumber: 1
rateControl:
type: "fixed-rate"
opts:
tps: 1
workload:
module: path/to/sample2.js
readOnly: true
Work load module file sample2.js*
const { WorkloadModuleBase, ConfigUtil } = require('@hyperledger/caliper-core');
const SupportedConnectors = ['ethereum'];
class MyWorkload extends WorkloadModuleBase {
constructor() {
super();
}
/**
* Initialize the workload module with the given parameters.
* @param {number} workerIndex The 0-based index of the worker instantiating the workload module.
* @param {number} totalWorkers The total number of workers participating in the round.
* @param {number} roundIndex The 0-based index of the currently executing round.
* @param {Object} roundArguments The user-provided arguments for the round from the benchmark configuration file.
* @param {ConnectorBase} sutAdapter The adapter of the underlying SUT.
* @param {Object} sutContext The custom context object provided by the SUT adapter.
* @async
*/
async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);
this.connectorType = this.sutAdapter.getType();
}
/**
* Assemble a Ethereum-specific request from the business parameters.
* @param {string} operation The name of the operation to invoke.
* @param {object} args The object containing the arguments.
* @return {object} The Ethereum-specific request.
* @private
*/
_createEthereumConnectorRequest(operation, args) {
return {
contract: "Storage",
verb: operation,
arg: Object.keys(args).map(k=>args[k]),
readOnly: false
};
}
createConnectorRequest(operation, args) {
switch (this.connectorType) {
case 'ethereum':
return this._createEthereumConnectorRequest(operation, args);
default:
// this shouldn't happen
throw new Error(`Connector type ${this.connectorType} is not supported by the benchmark`);
}
}
async getNextNonce(address) {
//console.log(await this.sutAdapter.web3.eth.getTransactionCount(address));
return await this.sutAdapter.web3.eth.getTransactionCount(address);
}
async submitTransaction() {
try {
let connectorRequest = [{
contract: 'Storage',
verb: 'store',
args: [25],
readOnly: false
}]
console.log('Connector Request:', connectorRequest);
// Invoke smart contract
const tx_status = await this.sutAdapter.sendRequests(connectorRequest);
console.log(tx_status)
// Log successful invocation
console.log('Smart contract invoked successfully');
} catch (error) {
console.error('Error invoking smart contract:', error);
// Handle the error appropriately (e.g., throw, log, etc.)
}
}
}
function createWorkloadModule() {
return new MyWorkload();
}
module.exports.createWorkloadModule = createWorkloadModule;
Upvotes: 0
Views: 66