From b581ee83397cf6ebc0492b132f64e7c4107bdf1a Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 1 May 2024 13:37:39 +0530 Subject: [PATCH 01/13] Add tests for caliper-ethereum package Signed-off-by: Abhinav Pandey --- .../caliper-ethereum/test/connectorFactory.js | 51 +++++++++++ .../test/ethereum-connector.js | 88 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 packages/caliper-ethereum/test/connectorFactory.js create mode 100644 packages/caliper-ethereum/test/ethereum-connector.js diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js new file mode 100644 index 000000000..0f44f35d3 --- /dev/null +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -0,0 +1,51 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const chai = require('chai'); +const sinon = require('sinon'); +const expect = chai.expect; +const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; +const path = require('path'); + +const { ConnectorFactory } = require('../lib/connectorFactory'); +const EthereumConnector = require('../lib/ethereum-connector'); + +describe('ConnectorFactory', function() { + let ethereumConnectorStub; + let tempConfigFilePath; + + beforeEach(() => { + ethereumConnectorStub = sinon.stub(EthereumConnector, 'constructor').returns({}); + tempConfigFilePath = path.resolve(__dirname, '../../caliper-tests-integration/ethereum_tests/networkconfig.json'); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); + }); + + afterEach(() => { + ethereumConnectorStub.restore(); + }); + + it('should create an instance of EthereumConnector with correct parameters', async function() { + const workerIndex = 0; + const connector = await ConnectorFactory(workerIndex); + expect(connector).to.be.an.instanceof(EthereumConnector); + }); + + it('should handle -1 for the manager process', async function() { + const workerIndex = -1; + const connector = await ConnectorFactory(workerIndex); + expect(connector).to.be.an.instanceof(EthereumConnector); + }); +}); diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js new file mode 100644 index 000000000..d63c2fe28 --- /dev/null +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -0,0 +1,88 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +const chai = require('chai'); +const sinon = require('sinon'); +const expect = chai.expect; +const rewire = require('rewire'); +const networkConfig = require('../../caliper-tests-integration/ethereum_tests/networkconfig.json'); + +const EthereumConnector = rewire('../lib/ethereum-connector'); +const { ConfigUtil, CaliperUtils } = require('@hyperledger/caliper-core'); + +describe('EthereumConnector', function() { + let sandbox = sinon.createSandbox(); + let mockWeb3, mockEEAClient, configStub; + + beforeEach(() => { + mockWeb3 = { + eth: { + Contract: sinon.stub(), + accounts: { + wallet: { + add: sinon.stub() + }, + create: sinon.stub() + }, + personal: { + unlockAccount: sinon.stub().resolves(true) + } + }, + utils: {} + }; + mockEEAClient = function() { + return mockWeb3; + }; + + EthereumConnector.__set__('Web3', function() { + return mockWeb3; + }); + EthereumConnector.__set__('EEAClient', mockEEAClient); + + // Stub configurations + configStub = sandbox.stub(ConfigUtil, 'get'); + configStub.withArgs(ConfigUtil.keys.NetworkConfig).returns(networkConfig); + sandbox.stub(CaliperUtils, 'resolvePath').returnsArg(0); + sandbox.stub(JSON, 'parse').returns({ + ethereum: { + url: 'ws://localhost:8546', + contractDeployerAddress: '0x1234567890', + transactionConfirmationBlocks: 2, + contracts: { + simple: { + path : 'caliper-tests-integration/ethereum_tests/src/simple/simple.sol', // TODO : change this to the correct path + gas: 3000000 + } + } + } + }); + sandbox.stub(require, 'main').returns({ + ethereum: { + url: 'ws://localhost:8546' + } + }); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it('should correctly initialize the EthereumConnector', async () => { + const connector = new EthereumConnector(0, 'ethereum'); + expect(connector).to.be.instanceOf(EthereumConnector); + expect(mockWeb3.eth.Contract.called).to.be.false; + }); +}); From d40236f1b032315796dcd4da161082e02e4e54da Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 1 May 2024 16:09:35 +0530 Subject: [PATCH 02/13] Update test files for ethereum-connector.js Signed-off-by: Abhinav Pandeey Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/package.json | 6 +- .../test/ethereum-connector.js | 98 ++++++++----------- 2 files changed, 44 insertions(+), 60 deletions(-) diff --git a/packages/caliper-ethereum/package.json b/packages/caliper-ethereum/package.json index 5e9f71c5c..2fd49fbda 100644 --- a/packages/caliper-ethereum/package.json +++ b/packages/caliper-ethereum/package.json @@ -13,7 +13,7 @@ "scripts": { "pretest": "npm run licchk", "licchk": "license-check-and-add", - "test": "npm run lint", + "test": "npm run lint && npm run nyc", "lint": "npx eslint .", "nyc": "nyc --reporter=text --reporter=clover mocha --recursive -t 10000" }, @@ -28,9 +28,9 @@ }, "devDependencies": { "eslint": "^5.16.0", + "license-check-and-add": "2.3.6", "mocha": "3.4.2", - "nyc": "11.1.0", - "license-check-and-add": "2.3.6" + "nyc": "11.1.0" }, "license-check-and-add-config": { "folder": ".", diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index d63c2fe28..d84e3bce4 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -14,75 +14,59 @@ 'use strict'; -const chai = require('chai'); +const path = require('path'); +const expect = require('chai').expect; const sinon = require('sinon'); -const expect = chai.expect; -const rewire = require('rewire'); -const networkConfig = require('../../caliper-tests-integration/ethereum_tests/networkconfig.json'); - -const EthereumConnector = rewire('../lib/ethereum-connector'); -const { ConfigUtil, CaliperUtils } = require('@hyperledger/caliper-core'); +const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; +const EthereumConnector = require('../lib/ethereum-connector'); describe('EthereumConnector', function() { - let sandbox = sinon.createSandbox(); - let mockWeb3, mockEEAClient, configStub; + let ethereumConnectorStub; + let tempConfigFilePath; beforeEach(() => { - mockWeb3 = { - eth: { - Contract: sinon.stub(), - accounts: { - wallet: { - add: sinon.stub() - }, - create: sinon.stub() - }, - personal: { - unlockAccount: sinon.stub().resolves(true) - } - }, - utils: {} - }; - mockEEAClient = function() { - return mockWeb3; - }; + ethereumConnectorStub = sinon.stub(EthereumConnector, 'constructor').returns({}); + tempConfigFilePath = path.resolve(__dirname, '../../caliper-tests-integration/ethereum_tests/networkconfig.json'); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); + }); - EthereumConnector.__set__('Web3', function() { - return mockWeb3; - }); - EthereumConnector.__set__('EEAClient', mockEEAClient); + afterEach(() => { + ethereumConnectorStub.restore(); + }); - // Stub configurations - configStub = sandbox.stub(ConfigUtil, 'get'); - configStub.withArgs(ConfigUtil.keys.NetworkConfig).returns(networkConfig); - sandbox.stub(CaliperUtils, 'resolvePath').returnsArg(0); - sandbox.stub(JSON, 'parse').returns({ - ethereum: { - url: 'ws://localhost:8546', - contractDeployerAddress: '0x1234567890', - transactionConfirmationBlocks: 2, - contracts: { - simple: { - path : 'caliper-tests-integration/ethereum_tests/src/simple/simple.sol', // TODO : change this to the correct path - gas: 3000000 - } - } - } + describe('constructor', () => { + it('should create a new EthereumConnector instance', () => { + const ethereumConnector = new EthereumConnector(0, 'ethereum'); + expect(ethereumConnector).to.be.instanceOf(EthereumConnector); }); - sandbox.stub(require, 'main').returns({ - ethereum: { - url: 'ws://localhost:8546' + }); + + describe('installSmartContract', () => { + it('should throw an error when the specified contract path does not exist', async () => { + const ethereumConnector = new EthereumConnector(0, 'ethereum'); + const contractDetails = { + path: './nonexistent/contract.sol' + }; + try { + await ethereumConnector.installSmartContract(contractDetails); + } catch (err) { + expect(err.message).to.contain('Cannot find module'); } }); }); - afterEach(() => { - sandbox.restore(); - }); + describe('init', () => { + it('should throw an error when the specified contract path does not exist', async () => { + const ethereumConnector = new EthereumConnector(0, 'ethereum'); + const contractDetails = { + path: './nonexistent/contract.sol' + }; - it('should correctly initialize the EthereumConnector', async () => { - const connector = new EthereumConnector(0, 'ethereum'); - expect(connector).to.be.instanceOf(EthereumConnector); - expect(mockWeb3.eth.Contract.called).to.be.false; + try { + await ethereumConnector.init(contractDetails); + } catch (err) { + expect(err.message).to.contain('connection not open'); + } + }); }); }); From 9e010f6ac7f0bd532b0905173591ee75704ef5fb Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 1 May 2024 16:27:01 +0530 Subject: [PATCH 03/13] Update test cases for ethereum-connector.js Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/test/ethereum-connector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index d84e3bce4..6ec9535c9 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -45,7 +45,7 @@ describe('EthereumConnector', function() { it('should throw an error when the specified contract path does not exist', async () => { const ethereumConnector = new EthereumConnector(0, 'ethereum'); const contractDetails = { - path: './nonexistent/contract.sol' + path: 'src/simple/nonexistent.sol' }; try { await ethereumConnector.installSmartContract(contractDetails); @@ -59,7 +59,7 @@ describe('EthereumConnector', function() { it('should throw an error when the specified contract path does not exist', async () => { const ethereumConnector = new EthereumConnector(0, 'ethereum'); const contractDetails = { - path: './nonexistent/contract.sol' + path: 'src/simple/nonexistent.sol' }; try { From 4d3ff42c3464a0c21f56339bccc49d37ea9a2519 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 5 May 2024 13:14:31 +0530 Subject: [PATCH 04/13] Update connectorFactory test utils Signed-off-by: Abhinav Pandey --- .../caliper-ethereum/test/connectorFactory.js | 20 ++++++++--------- .../test/utils/networkconfig.json | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 packages/caliper-ethereum/test/utils/networkconfig.json diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js index 0f44f35d3..f94a4923f 100644 --- a/packages/caliper-ethereum/test/connectorFactory.js +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -15,7 +15,7 @@ 'use strict'; const chai = require('chai'); -const sinon = require('sinon'); +// const sinon = require('sinon'); const expect = chai.expect; const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; const path = require('path'); @@ -24,23 +24,21 @@ const { ConnectorFactory } = require('../lib/connectorFactory'); const EthereumConnector = require('../lib/ethereum-connector'); describe('ConnectorFactory', function() { - let ethereumConnectorStub; let tempConfigFilePath; beforeEach(() => { - ethereumConnectorStub = sinon.stub(EthereumConnector, 'constructor').returns({}); - tempConfigFilePath = path.resolve(__dirname, '../../caliper-tests-integration/ethereum_tests/networkconfig.json'); + tempConfigFilePath = path.resolve(__dirname, './utils/networkconfig.json'); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); - afterEach(() => { - ethereumConnectorStub.restore(); - }); - it('should create an instance of EthereumConnector with correct parameters', async function() { - const workerIndex = 0; - const connector = await ConnectorFactory(workerIndex); - expect(connector).to.be.an.instanceof(EthereumConnector); + const workerIndices = [0, -1, 1, 2]; + workerIndices.forEach((workerIndex) => { + it(`should create an instance of EthereumConnector with workerIndex ${workerIndex}`, async function () { + const connector = await ConnectorFactory(workerIndex); + expect(connector).to.be.an.instanceof(EthereumConnector); + expect(connector.workerIndex).to.equal(workerIndex); + }); }); it('should handle -1 for the manager process', async function() { diff --git a/packages/caliper-ethereum/test/utils/networkconfig.json b/packages/caliper-ethereum/test/utils/networkconfig.json new file mode 100644 index 000000000..2ae91ac54 --- /dev/null +++ b/packages/caliper-ethereum/test/utils/networkconfig.json @@ -0,0 +1,22 @@ +{ + "caliper": { + "blockchain": "ethereum", + "command" : { + "start": "docker-compose -f config/docker-compose.yml up -d && sleep 30s", + "end" : "docker-compose -f config/docker-compose.yml down" + } + }, + "ethereum": { + "url": "ws://localhost:8546", + "contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "contractDeployerAddressPassword": "password", + "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "fromAddressPassword": "password", + "transactionConfirmationBlocks": 2, + "contracts": { + "simple": { + "path": "src/simple/simple.json" + } + } + } +} From 7912cf4778da8b8bfba5ad27691aaed4a0e8ec69 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 9 May 2024 23:29:27 +0530 Subject: [PATCH 05/13] chore: Update connectorFactory test utils Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/test/connectorFactory.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js index f94a4923f..b2e6134b3 100644 --- a/packages/caliper-ethereum/test/connectorFactory.js +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -15,7 +15,6 @@ 'use strict'; const chai = require('chai'); -// const sinon = require('sinon'); const expect = chai.expect; const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; const path = require('path'); @@ -32,7 +31,7 @@ describe('ConnectorFactory', function() { }); - const workerIndices = [0, -1, 1, 2]; + const workerIndices = [0, 1, 2]; workerIndices.forEach((workerIndex) => { it(`should create an instance of EthereumConnector with workerIndex ${workerIndex}`, async function () { const connector = await ConnectorFactory(workerIndex); @@ -46,4 +45,11 @@ describe('ConnectorFactory', function() { const connector = await ConnectorFactory(workerIndex); expect(connector).to.be.an.instanceof(EthereumConnector); }); + + it('should throw an error for an incorrect network configuration', function() { + const invalidConfigPath = './test/utils/invalidNetworkConfig.json'; + const invalidConfig = JSON.parse(invalidConfigPath); + + expect(() => new EthereumConnector(invalidConfig)).to.throw(); + }); }); From 7ae2f499e320a4db605703bb4ba48e72c6068f1f Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 9 May 2024 23:43:31 +0530 Subject: [PATCH 06/13] Test Invalid network connections for connectorFactory.js Signed-off-by: Abhinav Pandey --- .../caliper-ethereum/test/connectorFactory.js | 29 ++++++++++++++----- .../test/utils/invalidconfig.json | 22 ++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 packages/caliper-ethereum/test/utils/invalidconfig.json diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js index b2e6134b3..844364642 100644 --- a/packages/caliper-ethereum/test/connectorFactory.js +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -22,15 +22,17 @@ const path = require('path'); const { ConnectorFactory } = require('../lib/connectorFactory'); const EthereumConnector = require('../lib/ethereum-connector'); -describe('ConnectorFactory', function() { +describe('ConnectorFactory', function () { let tempConfigFilePath; beforeEach(() => { - tempConfigFilePath = path.resolve(__dirname, './utils/networkconfig.json'); + tempConfigFilePath = path.resolve( + __dirname, + './utils/networkconfig.json' + ); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); - const workerIndices = [0, 1, 2]; workerIndices.forEach((workerIndex) => { it(`should create an instance of EthereumConnector with workerIndex ${workerIndex}`, async function () { @@ -40,16 +42,27 @@ describe('ConnectorFactory', function() { }); }); - it('should handle -1 for the manager process', async function() { + it('should handle -1 for the manager process', async function () { const workerIndex = -1; const connector = await ConnectorFactory(workerIndex); expect(connector).to.be.an.instanceof(EthereumConnector); + expect(connector.workerIndex).to.equal(workerIndex); }); +}); - it('should throw an error for an incorrect network configuration', function() { - const invalidConfigPath = './test/utils/invalidNetworkConfig.json'; - const invalidConfig = JSON.parse(invalidConfigPath); - +describe('ConnectorFactory', function () { + beforeEach(() => { + const invalidConfig = path.resolve( + __dirname, + './utils/invalidconfig.json' + ); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); + }); + it('should throw an error for an incorrect network configuration', function () { + const invalidConfig = path.resolve( + __dirname, + './utils/invalidconfig.json' + ); expect(() => new EthereumConnector(invalidConfig)).to.throw(); }); }); diff --git a/packages/caliper-ethereum/test/utils/invalidconfig.json b/packages/caliper-ethereum/test/utils/invalidconfig.json new file mode 100644 index 000000000..362d7b176 --- /dev/null +++ b/packages/caliper-ethereum/test/utils/invalidconfig.json @@ -0,0 +1,22 @@ +{ + "caliper": { + "blockchain": "ethereum", + "command" : { + "start": "docker-compose -f config/docker-compose.yml up -d && sleep 30s", + "end" : "docker-compose -f config/docker-compose.yml down" + } + }, + "ethereum": { + "url": 123, + "contractDeployerAddress": "", + "contractDeployerAddressPassword": "password", + "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "fromAddressPassword": "password", + "transactionConfirmationBlocks": "two", + "contracts": { + "simple": { + "path": "src/simple/simple.json" + } + } + } +} \ No newline at end of file From 52f90b3f18ebdb751277feea67f32145e8f6bbc5 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 9 May 2024 23:49:45 +0530 Subject: [PATCH 07/13] Update EthereumConnector.js suites Signed-off-by: Abhinav Pandey --- .../caliper-ethereum/test/connectorFactory.js | 17 ----------------- .../caliper-ethereum/test/ethereum-connector.js | 17 +++++++++++++++++ ...invalidconfig.json => invalidUrlConfig.json} | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) rename packages/caliper-ethereum/test/utils/{invalidconfig.json => invalidUrlConfig.json} (82%) diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js index 844364642..51d30e2dc 100644 --- a/packages/caliper-ethereum/test/connectorFactory.js +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -49,20 +49,3 @@ describe('ConnectorFactory', function () { expect(connector.workerIndex).to.equal(workerIndex); }); }); - -describe('ConnectorFactory', function () { - beforeEach(() => { - const invalidConfig = path.resolve( - __dirname, - './utils/invalidconfig.json' - ); - ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); - }); - it('should throw an error for an incorrect network configuration', function () { - const invalidConfig = path.resolve( - __dirname, - './utils/invalidconfig.json' - ); - expect(() => new EthereumConnector(invalidConfig)).to.throw(); - }); -}); diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index 6ec9535c9..85991e28a 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -70,3 +70,20 @@ describe('EthereumConnector', function() { }); }); }); + +describe('EthereumConnector', function () { + beforeEach(() => { + const invalidConfig = path.resolve( + __dirname, + './utils/invalidconfig.json' + ); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); + }); + it('should throw an error for an incorrect url path', function () { + const invalidConfig = path.resolve( + __dirname, + './utils/invalidUrlConfig.json' + ); + expect(() => new EthereumConnector(invalidConfig)).to.throw(); + }); +}); diff --git a/packages/caliper-ethereum/test/utils/invalidconfig.json b/packages/caliper-ethereum/test/utils/invalidUrlConfig.json similarity index 82% rename from packages/caliper-ethereum/test/utils/invalidconfig.json rename to packages/caliper-ethereum/test/utils/invalidUrlConfig.json index 362d7b176..d46a17d3a 100644 --- a/packages/caliper-ethereum/test/utils/invalidconfig.json +++ b/packages/caliper-ethereum/test/utils/invalidUrlConfig.json @@ -8,11 +8,11 @@ }, "ethereum": { "url": 123, - "contractDeployerAddress": "", + "contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", "contractDeployerAddressPassword": "password", "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", "fromAddressPassword": "password", - "transactionConfirmationBlocks": "two", + "transactionConfirmationBlocks": 2, "contracts": { "simple": { "path": "src/simple/simple.json" From 30193944e4a0e68b8fe901a29fcd006747869a48 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 12 May 2024 00:20:46 +0530 Subject: [PATCH 08/13] Update ethereumConenctor.js Signed-off-by: Abhinav Pandey --- .../test/ethereum-connector.js | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index 85991e28a..ec768b879 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -16,46 +16,38 @@ const path = require('path'); const expect = require('chai').expect; -const sinon = require('sinon'); const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; const EthereumConnector = require('../lib/ethereum-connector'); describe('EthereumConnector', function() { - let ethereumConnectorStub; let tempConfigFilePath; beforeEach(() => { - ethereumConnectorStub = sinon.stub(EthereumConnector, 'constructor').returns({}); - tempConfigFilePath = path.resolve(__dirname, '../../caliper-tests-integration/ethereum_tests/networkconfig.json'); + tempConfigFilePath = path.resolve(__dirname, './utils/networkconfig.json'); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); - afterEach(() => { - ethereumConnectorStub.restore(); - }); - - describe('constructor', () => { - it('should create a new EthereumConnector instance', () => { - const ethereumConnector = new EthereumConnector(0, 'ethereum'); - expect(ethereumConnector).to.be.instanceOf(EthereumConnector); - }); - }); - describe('installSmartContract', () => { + describe('EthereumConnector.installSmartContract', () => { it('should throw an error when the specified contract path does not exist', async () => { - const ethereumConnector = new EthereumConnector(0, 'ethereum'); - const contractDetails = { - path: 'src/simple/nonexistent.sol' + const invalidConfig = { + contracts: { + nonexistent: { + path: 'src/simple/simple.sol' + } + } }; + const ethereumConnector = new EthereumConnector(invalidConfig); + try { - await ethereumConnector.installSmartContract(contractDetails); + await ethereumConnector.installSmartContract(); } catch (err) { expect(err.message).to.contain('Cannot find module'); } }); }); - describe('init', () => { + describe('EthereumConnector.init', () => { it('should throw an error when the specified contract path does not exist', async () => { const ethereumConnector = new EthereumConnector(0, 'ethereum'); const contractDetails = { @@ -71,7 +63,7 @@ describe('EthereumConnector', function() { }); }); -describe('EthereumConnector', function () { +describe('EthereumConnector.checkConfig()', function () { beforeEach(() => { const invalidConfig = path.resolve( __dirname, From 63c45ff26c36eb8ed64efdb7a098bd07bc7092e5 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sat, 18 May 2024 20:47:44 +0530 Subject: [PATCH 09/13] Update sample-configs Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/test/connectorFactory.js | 2 +- packages/caliper-ethereum/test/ethereum-connector.js | 6 +++--- .../test/{utils => sample-configs}/invalidUrlConfig.json | 0 .../test/{utils => sample-configs}/networkconfig.json | 0 4 files changed, 4 insertions(+), 4 deletions(-) rename packages/caliper-ethereum/test/{utils => sample-configs}/invalidUrlConfig.json (100%) rename packages/caliper-ethereum/test/{utils => sample-configs}/networkconfig.json (100%) diff --git a/packages/caliper-ethereum/test/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js index 51d30e2dc..201b8feb2 100644 --- a/packages/caliper-ethereum/test/connectorFactory.js +++ b/packages/caliper-ethereum/test/connectorFactory.js @@ -28,7 +28,7 @@ describe('ConnectorFactory', function () { beforeEach(() => { tempConfigFilePath = path.resolve( __dirname, - './utils/networkconfig.json' + './sample-configs/networkconfig.json' ); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index ec768b879..3d651d031 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -23,7 +23,7 @@ describe('EthereumConnector', function() { let tempConfigFilePath; beforeEach(() => { - tempConfigFilePath = path.resolve(__dirname, './utils/networkconfig.json'); + tempConfigFilePath = path.resolve(__dirname, './sample-configs/networkconfig.json'); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); @@ -67,14 +67,14 @@ describe('EthereumConnector.checkConfig()', function () { beforeEach(() => { const invalidConfig = path.resolve( __dirname, - './utils/invalidconfig.json' + './sample-configs/invalidconfig.json' ); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); }); it('should throw an error for an incorrect url path', function () { const invalidConfig = path.resolve( __dirname, - './utils/invalidUrlConfig.json' + './sample-configs/invalidUrlConfig.json' ); expect(() => new EthereumConnector(invalidConfig)).to.throw(); }); diff --git a/packages/caliper-ethereum/test/utils/invalidUrlConfig.json b/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json similarity index 100% rename from packages/caliper-ethereum/test/utils/invalidUrlConfig.json rename to packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json diff --git a/packages/caliper-ethereum/test/utils/networkconfig.json b/packages/caliper-ethereum/test/sample-configs/networkconfig.json similarity index 100% rename from packages/caliper-ethereum/test/utils/networkconfig.json rename to packages/caliper-ethereum/test/sample-configs/networkconfig.json From 7929e143822acd9d5bb77121bd8438ece6757298 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 20 May 2024 12:25:21 +0530 Subject: [PATCH 10/13] Update checkConfig() tests Temporarily removed tests for installSmartContracts for debugging purposes Signed-off-by: Abhinav Pandey --- .../test/ethereum-connector.js | 86 +++++++++---------- .../test/sample-configs/invalidUrlConfig.json | 2 +- .../test/sample-configs/noUrlConfig.json | 21 +++++ 3 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 packages/caliper-ethereum/test/sample-configs/noUrlConfig.json diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index 3d651d031..a45b3066d 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -15,67 +15,61 @@ 'use strict'; const path = require('path'); +const sinon = require('sinon'); const expect = require('chai').expect; const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; const EthereumConnector = require('../lib/ethereum-connector'); - -describe('EthereumConnector', function() { +describe('EthereumConnector', function () { let tempConfigFilePath; beforeEach(() => { - tempConfigFilePath = path.resolve(__dirname, './sample-configs/networkconfig.json'); + tempConfigFilePath = path.resolve( + __dirname, + './sample-configs/networkconfig.json' + ); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); - describe('EthereumConnector.installSmartContract', () => { - it('should throw an error when the specified contract path does not exist', async () => { - const invalidConfig = { - contracts: { - nonexistent: { - path: 'src/simple/simple.sol' - } - } - }; - const ethereumConnector = new EthereumConnector(invalidConfig); - - try { - await ethereumConnector.installSmartContract(); - } catch (err) { - expect(err.message).to.contain('Cannot find module'); - } + it('should deploy all contracts successfully when no privacy settings are used', async () => { + const workerIndex = 0; + const bcType = 'ethereum'; + const ethereumConnector = new EthereumConnector(workerIndex, bcType); + const deployContractStub = sinon.stub(ethereumConnector, 'deployContract').resolves({ + options: { address: '0x123' }, + }); + await ethereumConnector.installSmartContract(); + sinon.assert.called(deployContractStub); + deployContractStub.restore(); }); }); - describe('EthereumConnector.init', () => { - it('should throw an error when the specified contract path does not exist', async () => { - const ethereumConnector = new EthereumConnector(0, 'ethereum'); - const contractDetails = { - path: 'src/simple/nonexistent.sol' - }; + describe('EthereumConnector.init', () => {}); - try { - await ethereumConnector.init(contractDetails); - } catch (err) { - expect(err.message).to.contain('connection not open'); - } + describe('EthereumConnector.checkConfig()', function () { + it('should throw an error for an incorrect url path', function () { + const invalidConfig = path.resolve( + __dirname, + './sample-configs/invalidUrlConfig.json' + ); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); + expect(() => new EthereumConnector(invalidConfig)).to.throw( + 'Ethereum benchmarks must not use http(s) RPC connections, as there is no way to guarantee the ' + + 'order of submitted transactions when using other transports. For more information, please see ' + + 'https://github.com/hyperledger/caliper/issues/776#issuecomment-624771622' + ); }); - }); -}); -describe('EthereumConnector.checkConfig()', function () { - beforeEach(() => { - const invalidConfig = path.resolve( - __dirname, - './sample-configs/invalidconfig.json' - ); - ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); - }); - it('should throw an error for an incorrect url path', function () { - const invalidConfig = path.resolve( - __dirname, - './sample-configs/invalidUrlConfig.json' - ); - expect(() => new EthereumConnector(invalidConfig)).to.throw(); + it('should throw an error for absent url path', function () { + const invalidConfig = path.resolve( + __dirname, + './sample-configs/noUrlConfig.json' + ); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); + expect(() => new EthereumConnector(invalidConfig)).to.throw( + 'No URL given to access the Ethereum SUT. Please check your network configuration. ' + + 'Please see https://hyperledger.github.io/caliper/v0.3/ethereum-config/ for more info.' + ); + }); }); }); diff --git a/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json b/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json index d46a17d3a..ac0ef7d5b 100644 --- a/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json +++ b/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.json @@ -7,7 +7,7 @@ } }, "ethereum": { - "url": 123, + "url": "http://localhost:8545", "contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", "contractDeployerAddressPassword": "password", "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", diff --git a/packages/caliper-ethereum/test/sample-configs/noUrlConfig.json b/packages/caliper-ethereum/test/sample-configs/noUrlConfig.json new file mode 100644 index 000000000..03f957c47 --- /dev/null +++ b/packages/caliper-ethereum/test/sample-configs/noUrlConfig.json @@ -0,0 +1,21 @@ +{ + "caliper": { + "blockchain": "ethereum", + "command" : { + "start": "docker-compose -f config/docker-compose.yml up -d && sleep 30s", + "end" : "docker-compose -f config/docker-compose.yml down" + } + }, + "ethereum": { + "contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "contractDeployerAddressPassword": "password", + "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "fromAddressPassword": "password", + "transactionConfirmationBlocks": 2, + "contracts": { + "simple": { + "path": "src/simple/simple.json" + } + } + } +} \ No newline at end of file From 40a06d40a21b478136134ae5a25017c3419ffd78 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Tue, 21 May 2024 21:44:01 +0530 Subject: [PATCH 11/13] Update 'describe' messages and remove empty test suites Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/test/ethereum-connector.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index a45b3066d..af4afc5ea 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -30,7 +30,7 @@ describe('EthereumConnector', function () { ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); }); - describe('EthereumConnector.installSmartContract', () => { + describe('While installing a Smart Contract, it', () => { it('should deploy all contracts successfully when no privacy settings are used', async () => { const workerIndex = 0; const bcType = 'ethereum'; @@ -44,10 +44,9 @@ describe('EthereumConnector', function () { }); }); - describe('EthereumConnector.init', () => {}); - describe('EthereumConnector.checkConfig()', function () { - it('should throw an error for an incorrect url path', function () { + describe('When constructed with an invalid url path', function () { + it('should throw an error', function () { const invalidConfig = path.resolve( __dirname, './sample-configs/invalidUrlConfig.json' @@ -59,8 +58,10 @@ describe('EthereumConnector', function () { 'https://github.com/hyperledger/caliper/issues/776#issuecomment-624771622' ); }); + }); - it('should throw an error for absent url path', function () { + describe('When constructed with absent url path', function () { + it('should throw an error', function () { const invalidConfig = path.resolve( __dirname, './sample-configs/noUrlConfig.json' From 3d27fae4278cd590e9adb8dd9e6348dd6514bb63 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Tue, 21 May 2024 22:06:04 +0530 Subject: [PATCH 12/13] Facing Package Issues while Running test suites Signed-off-by: Abhinav Pandey --- .../test/ethereum-connector.js | 6 +- .../test/sample-configs/networkconfig.json | 2 +- .../test/src/simple/simple.json | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 packages/caliper-ethereum/test/src/simple/simple.json diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index af4afc5ea..2e28f6d3f 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -20,14 +20,14 @@ const expect = require('chai').expect; const ConfigUtil = require('@hyperledger/caliper-core').ConfigUtil; const EthereumConnector = require('../lib/ethereum-connector'); describe('EthereumConnector', function () { - let tempConfigFilePath; + let ConfigFilePath; beforeEach(() => { - tempConfigFilePath = path.resolve( + ConfigFilePath = path.resolve( __dirname, './sample-configs/networkconfig.json' ); - ConfigUtil.set(ConfigUtil.keys.NetworkConfig, tempConfigFilePath); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, ConfigFilePath); }); describe('While installing a Smart Contract, it', () => { diff --git a/packages/caliper-ethereum/test/sample-configs/networkconfig.json b/packages/caliper-ethereum/test/sample-configs/networkconfig.json index 2ae91ac54..8b2c1cb0c 100644 --- a/packages/caliper-ethereum/test/sample-configs/networkconfig.json +++ b/packages/caliper-ethereum/test/sample-configs/networkconfig.json @@ -15,7 +15,7 @@ "transactionConfirmationBlocks": 2, "contracts": { "simple": { - "path": "src/simple/simple.json" + "path": "test/src/simple/simple.json" } } } diff --git a/packages/caliper-ethereum/test/src/simple/simple.json b/packages/caliper-ethereum/test/src/simple/simple.json new file mode 100644 index 000000000..ec2d0b807 --- /dev/null +++ b/packages/caliper-ethereum/test/src/simple/simple.json @@ -0,0 +1,73 @@ +{ + "name": "simple", + "abi": [ + { + "constant": false, + "inputs": [ + { + "internalType": "string", + "name": "acc_id", + "type": "string" + }, + { + "internalType": "int256", + "name": "amount", + "type": "int256" + } + ], + "name": "open", + "outputs": [], + "payable": true, + "stateMutability": "payable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "internalType": "string", + "name": "acc_id", + "type": "string" + } + ], + "name": "query", + "outputs": [ + { + "internalType": "int256", + "name": "amount", + "type": "int256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "internalType": "string", + "name": "acc_from", + "type": "string" + }, + { + "internalType": "string", + "name": "acc_to", + "type": "string" + }, + { + "internalType": "int256", + "name": "amount", + "type": "int256" + } + ], + "name": "transfer", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + } + ], + "bytecode": "0x608060405234801561001057600080fd5b506105c8806100206000396000f3fe6080604052600436106100345760003560e01c80631de45b10146100395780637c261929146101a2578063906412931461027e575b600080fd5b34801561004557600080fd5b506101a06004803603606081101561005c57600080fd5b810190808035906020019064010000000081111561007957600080fd5b82018360208201111561008b57600080fd5b803590602001918460018302840111640100000000831117156100ad57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561011057600080fd5b82018360208201111561012257600080fd5b8035906020019184600183028401116401000000008311171561014457600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929080359060200190929190505050610343565b005b3480156101ae57600080fd5b50610268600480360360208110156101c557600080fd5b81019080803590602001906401000000008111156101e257600080fd5b8201836020820111156101f457600080fd5b8035906020019184600183028401116401000000008311171561021657600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610436565b6040518082815260200191505060405180910390f35b6103416004803603604081101561029457600080fd5b81019080803590602001906401000000008111156102b157600080fd5b8201836020820111156102c357600080fd5b803590602001918460018302840111640100000000831117156102e557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803590602001909291905050506104a8565b005b806000846040518082805190602001908083835b6020831061037a5780518252602082019150602081019050602083039250610357565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008282540392505081905550806000836040518082805190602001908083835b602083106103f157805182526020820191506020810190506020830392506103ce565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008282540192505081905550505050565b600080826040518082805190602001908083835b6020831061046d578051825260208201915060208101905060208303925061044a565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020549050919050565b803414610500576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260218152602001806105736021913960400191505060405180910390fd5b806000836040518082805190602001908083835b602083106105375780518252602082019150602081019050602083039250610514565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902081905550505056fe4e6f20636f6d6d69746d656e74206d616465206279207468652063616c6c65722ea265627a7a72315820f76b07509e537883b5471090aeca0a231503064fc2dc6bfbd81169217bd96c2464736f6c63430005110032", + "gas": 4700000 +} From b2f4ae1711f7f037406493e7a49c67b801a43515 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Tue, 21 May 2024 22:42:07 +0530 Subject: [PATCH 13/13] Updated test suites for caliper-ethereun package Signed-off-by: Abhinav Pandey --- packages/caliper-ethereum/test/ethereum-connector.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js index 2e28f6d3f..c9d1ebf0e 100644 --- a/packages/caliper-ethereum/test/ethereum-connector.js +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -62,10 +62,7 @@ describe('EthereumConnector', function () { describe('When constructed with absent url path', function () { it('should throw an error', function () { - const invalidConfig = path.resolve( - __dirname, - './sample-configs/noUrlConfig.json' - ); + const invalidConfig = path.resolve(__dirname,'./sample-configs/noUrlConfig.json'); ConfigUtil.set(ConfigUtil.keys.NetworkConfig, invalidConfig); expect(() => new EthereumConnector(invalidConfig)).to.throw( 'No URL given to access the Ethereum SUT. Please check your network configuration. ' +