Skip to content

Commit 498f90e

Browse files
authored
Feat: Add check for unique chaincode names (#596)
1 parent 6f4d26d commit 498f90e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

e2e/fabloCommands.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,47 @@ describe("extend config", () => {
182182
expect(commandResult).toEqual(TestCommands.failure());
183183
expect(commandResult.output).toContain("commands-tests/fablo-config.json does not exist\n");
184184
});
185+
186+
it("should throw an error for duplicate chaincode names across different channels", () => {
187+
// Given
188+
commands.fabloExec("init");
189+
const configPath = `${commands.workdir}/fablo-config.json`;
190+
// const config = JSON.parse(fs.readFileSync(configPath, "utf8")) as FabloConfigJson;
191+
const config = JSON.parse(commands.getFileContent("fablo-config.json")) as FabloConfigJson;
192+
193+
config.channels.push({
194+
name: "my-channel2",
195+
orgs: [{ name: "Org1", peers: ["peer0"] }],
196+
});
197+
198+
config.chaincodes.push(
199+
{
200+
name: "chaincode1",
201+
version: "1.0",
202+
channel: "my-channel",
203+
lang: "node",
204+
directory: "./samples/chaincodes/chaincode-kv-node",
205+
privateData: [],
206+
},
207+
{
208+
name: "chaincode1",
209+
version: "1.0",
210+
channel: "my-channel2",
211+
lang: "node",
212+
directory: "./samples/chaincodes/chaincode-kv-node",
213+
privateData: [],
214+
},
215+
);
216+
217+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
218+
219+
// When
220+
const commandResult = commands.fabloExec("validate", true);
221+
222+
// Then
223+
expect(commandResult.output).toContain("Chaincode name 'chaincode1' is not unique");
224+
expect(commandResult.output).toContain("Validation errors count: 1");
225+
});
185226
});
186227

187228
describe("version", () => {

samples/fablo-config-hlf2-2orgs-2chaincodes-private-data.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ chaincodes:
5555
- name: both-orgs-collection
5656
orgNames:
5757
- Org1
58-
- Org2
58+
- Org2

src/extend-config/extendChaincodesConfig.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,31 @@ const createPrivateCollectionConfig = (
3737
};
3838
};
3939

40+
const checkUniqueChaincodeNames = (allChannelsConfig: any): void => {
41+
const chaincodeNames = new Set<string>();
42+
43+
const allChaincodes: ChaincodeJson[] = [];
44+
45+
Object.values(allChannelsConfig.channels || {}).forEach((channel: any) => {
46+
if (channel.chaincodes) {
47+
allChaincodes.push(...channel.chaincodes);
48+
}
49+
});
50+
51+
allChaincodes.forEach((chaincode) => {
52+
if (chaincodeNames.has(chaincode.name)) {
53+
throw new Error(`Chaincode name '${chaincode.name}' is not unique across channels.`);
54+
}
55+
chaincodeNames.add(chaincode.name);
56+
});
57+
};
58+
4059
const extendChaincodesConfig = (
4160
chaincodes: ChaincodeJson[],
4261
transformedChannels: ChannelConfig[],
4362
network: Global,
4463
): ChaincodeConfig[] => {
64+
checkUniqueChaincodeNames(chaincodes);
4565
return chaincodes.map((chaincode, index) => {
4666
const channel = transformedChannels.find((c) => c.name === chaincode.channel);
4767
if (!channel) throw new Error(`No matching channel with name '${chaincode.channel}'`);

src/extend-config/extendConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { extendOrgsConfig } from "./extendOrgsConfig";
44
import extendGlobal from "./extendGlobal";
55
import extendChannelsConfig from "./extendChannelsConfig";
66
import extendChaincodesConfig from "./extendChaincodesConfig";
7+
import checkUniqueChaincodeNames from "./extendChaincodesConfig";
78
import extendHooksConfig from "./extendHooksConfig";
89
import { distinctOrdererHeads, mergeOrdererGroups } from "./mergeOrdererGroups";
910

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

2425
const channels = extendChannelsConfig(channelsJson, orgs, ordererGroups);
26+
checkUniqueChaincodeNames(chaincodesJson, channels, global);
2527
const chaincodes = extendChaincodesConfig(chaincodesJson, channels, global);
2628
const hooks = extendHooksConfig(hooksJson);
2729

0 commit comments

Comments
 (0)