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

caliper-ethereum : Add new tests for connectorFactory and ethereum-connector #1560

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/caliper-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": ".",
Expand Down
51 changes: 51 additions & 0 deletions packages/caliper-ethereum/test/connectorFactory.js
Original file line number Diff line number Diff line change
@@ -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.
*/

Sweetdevil144 marked this conversation as resolved.
Show resolved Hide resolved
'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);
});
});
73 changes: 73 additions & 0 deletions packages/caliper-ethereum/test/ethereum-connector.js
Original file line number Diff line number Diff line change
@@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be part of the connector factory. As stated before we don't want to have tests that directly test the constructor of the EthereumConnector as it isn't really a public constructor. Please move this test to the ConnectorFactory tests.

'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 () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be part of the connector factory. As stated before we don't want to have tests that directly test the constructor of the EthereumConnector as it isn't really a public constructor. Please move this test to the ConnectorFactory tests.

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.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is actually a bug. The error message should not provide links like this as it means it needs to be updated with version changes. So we should fix the code and the test. Again this is an example where we should not assume that the code is correct and the test just passes because it is wrong as well. This is a good bug you have found though and simple to fix. We need to change the code to just say to refer to the ethereum configuration in the caliper documentation

);
});
});
});
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
22 changes: 22 additions & 0 deletions packages/caliper-ethereum/test/sample-configs/networkconfig.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
21 changes: 21 additions & 0 deletions packages/caliper-ethereum/test/sample-configs/noUrlConfig.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
73 changes: 73 additions & 0 deletions packages/caliper-ethereum/test/src/simple/simple.json
Original file line number Diff line number Diff line change
@@ -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
}