Skip to content

Add check for unique chaincode names #596

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

Open
wants to merge 8 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
1,594 changes: 1,594 additions & 0 deletions e2e/__snapshots__/extendConfig.test.ts.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`samples/fablo-config-hlf2-2orgs-2chaincodes-private-data-duplicate.yaml should create proper files from samples/fablo-config-hlf2-2orgs-2chaincodes-private-data-duplicate.yaml 1`] = `[]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import performTests from "./performTests";

const config = "samples/fablo-config-hlf2-2orgs-2chaincodes-private-data-duplicate.yaml";

describe(config, () => {
performTests(config);
});
25 changes: 25 additions & 0 deletions e2e/fabloCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ describe("validate", () => {
expect(commandResult.output).toContain(" instance.orgs[0].organization.mspName : does not match pattern");
expect(commands.getFiles()).toEqual([]);
});

it("should throw an error for duplicate chaincode names across different channels", () => {
// Given
commands.fabloExec("init");
const fabloConfig = `${commands.relativeRoot}/samples/fablo-config-hlf2-2orgs-2chaincodes-private-data-duplicate.yaml`;

// When
const commandResult = commands.fabloExec(`validate ${fabloConfig}`);

// Then
// expect(commandResult).toEqual(TestCommands.success());
expect(commandResult.output).toContain("Chaincode name 'duplicateChaincode' is not unique");
});

it("should validate when all chaincode names are unique across channels", () => {
// Given
const fabloConfig = `${commands.relativeRoot}/samples/fablo-config-hlf2-2orgs-2chaincodes-raft.yaml`;

// When
const commandResult = commands.fabloExec(`validate ${fabloConfig}`);

// Then
expect(commandResult.output).toContain("Validation errors count: 0");
expect(commandResult.output).not.toContain("is not unique");
});
});

describe("extend config", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
$schema: "https://github.com/hyperledger-labs/fablo/releases/download/2.2.0/schema.json"
global:
fabricVersion: "2.5.12"
tls: false

orgs:
- organization:
name: Orderer
domain: orderer.example.com
orderers:
- groupName: group1
prefix: orderer
type: solo
instances: 1
- organization:
name: Org1
domain: org1.example.com
peer:
instances: 2
- organization:
name: Org2
domain: org2.example.com
peer:
instances: 1

channels:
- name: my-channel1
orgs:
- name: Org1
peers: [peer0]
- name: Org2
peers: [peer0]
- name: my-channel2
orgs:
- name: Org1
peers: [peer0]
- name: Org2
peers: [peer0]

chaincodes:
- name: duplicateChaincode
version: "1.0"
lang: node
channel: my-channel1
directory: "./chaincodes/chaincode-kv-node"
endorsement: "OR('Org1MSP.member', 'Org2MSP.member')"
- name: duplicateChaincode
version: "1.0"
lang: node
channel: my-channel2
directory: "./chaincodes/chaincode-kv-node"
endorsement: "OR('Org1MSP.member', 'Org2MSP.member')"
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ chaincodes:
- name: both-orgs-collection
orgNames:
- Org1
- Org2
- Org2
20 changes: 20 additions & 0 deletions src/extend-config/extendChaincodesConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,31 @@ const createPrivateCollectionConfig = (
};
};

const checkUniqueChaincodeNames = (allChannelsConfig: any): void => {
const chaincodeNames = new Set<string>();

const allChaincodes: ChaincodeJson[] = [];

Object.values(allChannelsConfig.channels || {}).forEach((channel: any) => {
if (channel.chaincodes) {
allChaincodes.push(...channel.chaincodes);
}
});

allChaincodes.forEach((chaincode) => {
if (chaincodeNames.has(chaincode.name)) {
throw new Error(`Chaincode name '${chaincode.name}' is not unique across channels.`);
}
chaincodeNames.add(chaincode.name);
});
};

const extendChaincodesConfig = (
chaincodes: ChaincodeJson[],
transformedChannels: ChannelConfig[],
network: Global,
): ChaincodeConfig[] => {
checkUniqueChaincodeNames(chaincodes);
return chaincodes.map((chaincode) => {
const channel = transformedChannels.find((c) => c.name === chaincode.channel);
if (!channel) throw new Error(`No matching channel with name '${chaincode.channel}'`);
Expand Down
2 changes: 2 additions & 0 deletions src/extend-config/extendConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { extendOrgsConfig } from "./extendOrgsConfig";
import extendGlobal from "./extendGlobal";
import extendChannelsConfig from "./extendChannelsConfig";
import extendChaincodesConfig from "./extendChaincodesConfig";
import checkUniqueChaincodeNames from "./extendChaincodesConfig";
import extendHooksConfig from "./extendHooksConfig";
import { distinctOrdererHeads, mergeOrdererGroups } from "./mergeOrdererGroups";

Expand All @@ -22,6 +23,7 @@ const extendConfig = (json: FabloConfigJson): FabloConfigExtended => {
const orderedHeadsDistinct = distinctOrdererHeads(ordererGroups);

const channels = extendChannelsConfig(channelsJson, orgs, ordererGroups);
checkUniqueChaincodeNames(chaincodesJson, channels, global);
const chaincodes = extendChaincodesConfig(chaincodesJson, channels, global);
const hooks = extendHooksConfig(hooksJson);

Expand Down
Loading