hyperledger-caliper / caliper

A blockchain benchmark framework to measure performance of multiple blockchain solutions https://wiki.hyperledger.org/display/caliper
https://hyperledger-caliper.github.io/caliper/
Apache License 2.0
655 stars 404 forks source link

Allow the Ethereum connector to use an already deployed contract #1497

Open mkrielza opened 1 year ago

mkrielza commented 1 year ago

Please share the technical limitation of Caliper that you encountered.

Running benchmarks against an already deployed contract was not possible without changing the Ethereum connector.

Please detail your feature idea that could alleviate the limitation.

Allow for the case where no smart contracts need to be deployed by adding a contract address to the network configuration file.

Please share some details about your use case if possible, and how the new feature would make Caliper a better performance benchmarking framework.

It would allow the Ethereum connector to use an already deployed contract when running a benchmark.

Please share any suggestions about the new feature's code/configuration API (using formatted YAML segments or pseudo-code).

diff --git a/packages/caliper-ethereum/lib/ethereum-connector.js b/packages/caliper-ethereum/lib/ethereum-connector.js
--- a/packages/caliper-ethereum/lib/ethereum-connector.js   (revision 943ab2a22872639f39ccb36f8baf94b2863e21c6)
+++ b/packages/caliper-ethereum/lib/ethereum-connector.js   (date 1684132327195)
@@ -114,24 +114,30 @@
             }
             this.ethereumConfig.contracts[key].abi = contractData.abi;
-            promises.push(new Promise(async function(resolve, reject) {
-                let contractInstance;
-                try {
-                    if (privacy) {
-                        contractInstance = await self.deployPrivateContract(contractData, privacy);
-                        logger.info(`Deployed private contract ${contractData.name} at ${contractInstance.options.address}`);
-                    } else {
-                        contractInstance = await self.deployContract(contractData);
-                        logger.info(`Deployed contract ${contractData.name} at ${contractInstance.options.address}`);
-                    }
-                } catch (err) {
-                    reject(err);
-                }
-                self.ethereumConfig.contracts[key].address = contractInstance.options.address;
-                self.ethereumConfig.contracts[key].gas = contractGas;
-                self.ethereumConfig.contracts[key].estimateGas = estimateGas;
-                resolve(contractInstance);
-            }));
+            if (contract.address) {
+                self.ethereumConfig.contracts[key].address = contract.address;
+                self.ethereumConfig.contracts[key].gas = contractGas;
+                self.ethereumConfig.contracts[key].estimateGas = estimateGas;
+            } else {
+                promises.push(new Promise(async function (resolve, reject) {
+                    let contractInstance;
+                    try {
+                        if (privacy) {
+                            contractInstance = await self.deployPrivateContract(contractData, privacy);
+                            logger.info(`Deployed private contract ${contractData.name} at ${contractInstance.options.address}`);
+                        } else {
+                            contractInstance = await self.deployContract(contractData);
+                            logger.info(`Deployed contract ${contractData.name} at ${contractInstance.options.address}`);
+                        }
+                    } catch (err) {
+                        reject(err);
+                    }
+                    self.ethereumConfig.contracts[key].address = contractInstance.options.address;
+                    self.ethereumConfig.contracts[key].gas = contractGas;
+                    self.ethereumConfig.contracts[key].estimateGas = estimateGas;
+                    resolve(contractInstance);
+                }));
+            }
         }
         return Promise.all(promises);
     }
davidkel commented 5 months ago

The solution here is to run caliper but ensure it skips the install phase, you can do this by either skipping the phase or only running a specific phase (Caliper has 5 phases which admittedly are poorly documented). To skip the install phase and thus work with an already deployed contract use the --caliper-flow-skip-install option when launching caliper.

davidkel commented 5 months ago

see #1589 for more details on skipping install