Skip to content
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
25 changes: 25 additions & 0 deletions e2e/fabloCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ describe("extend config", () => {
expect(commandResult).toEqual(TestCommands.failure());
expect(commandResult.output).toContain("commands-tests/fablo-config.json does not exist\n");
});

it("should throw an error for duplicate chaincode names across different channels", () => {
// Given
commands.fabloExec("init node");
const configPath = `${commands.workdir}/fablo-config.json`;
const config = JSON.parse(fs.readFileSync(configPath, "utf8")) as FabloConfigJson;

config.channels.push({
name: "my-channel2",
orgs: [{ name: "Org1", peers: ["peer0"] }],
});

const existingChaincode = JSON.parse(JSON.stringify(config.chaincodes[0]));
existingChaincode.channel = "my-channel2";
config.chaincodes.push(existingChaincode);

fs.writeFileSync(configPath, JSON.stringify(config, null, 2));

// When
const commandResult = commands.fabloExec("validate", true);

// Then
expect(commandResult.output).toContain("Chaincode name 'chaincode1' is not unique");
expect(commandResult.output).toContain("Validation errors count: 1");
});
});

describe("version", () => {
Expand Down
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, index) => {
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