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

Issue doing benchmarking with Hyperledger Caliper.....where transactions interacting with already deployed Contract #266

Open
MohanElapolu opened this issue May 21, 2024 · 0 comments

Comments

@MohanElapolu
Copy link

MohanElapolu commented May 21, 2024

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
                }
            }
        }
    }
}

Storage.json file

{
    "name": "Storage",
    "abi": [{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function","constant":true,"signature":"0x2e64cec1"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function","signature":"0x6057361d"}],
    "bytecode": "608060405234801561001057600080fd5b5061012f806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80632e64cec11460375780636057361d146051575b600080fd5b603d6069565b6040516048919060c2565b60405180910390f35b6067600480360381019060639190608f565b6072565b005b60008054905090565b8060008190555050565b60008135905060898160e5565b92915050565b60006020828403121560a057600080fd5b600060ac84828501607c565b91505092915050565b60bc8160db565b82525050565b600060208201905060d5600083018460b5565b92915050565b6000819050919050565b60ec8160db565b811460f657600080fd5b5056fea2646970667358221220a919f081cdd0953c3eb15dd85fdd23cb57fcbc15f2675dfe59cc99fa69e9423864736f6c63430008000033",
    "gas": 1000000
}

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant