diff --git a/packages/caliper-ethereum/package.json b/packages/caliper-ethereum/package.json index d2f378ebf..2053d32ba 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/connectorFactory.js b/packages/caliper-ethereum/test/connectorFactory.js new file mode 100644 index 000000000..201b8feb2 --- /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 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 tempConfigFilePath; + + beforeEach(() => { + tempConfigFilePath = path.resolve( + __dirname, + './sample-configs/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 () { + 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 () { + const workerIndex = -1; + const connector = await ConnectorFactory(workerIndex); + expect(connector).to.be.an.instanceof(EthereumConnector); + expect(connector.workerIndex).to.equal(workerIndex); + }); +}); diff --git a/packages/caliper-ethereum/test/ethereum-connector.js b/packages/caliper-ethereum/test/ethereum-connector.js new file mode 100644 index 000000000..c9d1ebf0e --- /dev/null +++ b/packages/caliper-ethereum/test/ethereum-connector.js @@ -0,0 +1,73 @@ +/* +* 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 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 () { + let ConfigFilePath; + + beforeEach(() => { + ConfigFilePath = path.resolve( + __dirname, + './sample-configs/networkconfig.json' + ); + ConfigUtil.set(ConfigUtil.keys.NetworkConfig, ConfigFilePath); + }); + + 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'; + 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('When constructed with an invalid url path', function () { + it('should throw an error', 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('When constructed with absent url path', function () { + it('should throw an error', 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 new file mode 100644 index 000000000..ac0ef7d5b --- /dev/null +++ b/packages/caliper-ethereum/test/sample-configs/invalidUrlConfig.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": "http://localhost:8545", + "contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "contractDeployerAddressPassword": "password", + "fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2", + "fromAddressPassword": "password", + "transactionConfirmationBlocks": 2, + "contracts": { + "simple": { + "path": "src/simple/simple.json" + } + } + } +} \ No newline at end of file diff --git a/packages/caliper-ethereum/test/sample-configs/networkconfig.json b/packages/caliper-ethereum/test/sample-configs/networkconfig.json new file mode 100644 index 000000000..8b2c1cb0c --- /dev/null +++ b/packages/caliper-ethereum/test/sample-configs/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": "test/src/simple/simple.json" + } + } + } +} 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 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 +}