Skip to content

Commit

Permalink
[FAB-13217] default name of contract
Browse files Browse the repository at this point in the history
Set the default contract name to the first one in the list of exported
contracts.

Change-Id: I2d9b57e57e3e8e014ef63816d978649e80897daa
Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
  • Loading branch information
mbwhite committed Dec 10, 2018
1 parent 0b58a8e commit 5ac251c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
5 changes: 3 additions & 2 deletions build/test/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ gulp.task('invoke_functions', async (done) => {
getTLSArgs(),
CHANNEL_NAME,
'mysmartcontract',
'{"Args":["UpdateValues:setNewAssetValue","\'42\'"]}').split(' '); //eslint-disable-line
'{"Args":["setNewAssetValue","\'42\'"]}').split(' '); //eslint-disable-line
// use the short form with the default being the first contract in the export list

const {error, stdout, stderr} = await execFile(script, args, options);
if (error) {
Expand Down Expand Up @@ -118,7 +119,7 @@ gulp.task('query_functions', async (done) => {
const metadata = JSON.parse(stdout);

const expectedMetadata =
'{"contracts":{"UpdateValues":{"name":"UpdateValues","contractInstance":{"name":"UpdateValues","logBuffer":{"output":[]}},"transactions":[{"name":"unknownTransaction"},{"name":"beforeTransaction"},{"name":"createContext"},{"name":"setup"},{"name":"setNewAssetValue"},{"name":"doubleAssetValue"}],"info":{"title":"","version":""}},"RemoveValues":{"name":"RemoveValues","contractInstance":{"name":"RemoveValues"},"transactions":[{"name":"quarterAssetValue"},{"name":"getAssetValue"}],"info":{"title":"","version":""}},"org.hyperledger.fabric":{"name":"org.hyperledger.fabric","contractInstance":{"name":"org.hyperledger.fabric"},"transactions":[{"name":"GetMetadata"}],"info":{"title":"","version":""}}},"info":{"version":"1.0.0","title":"chaincode"},"components":{"schemas":{}}}';
'{"contracts":{"UpdateValues":{"name":"UpdateValues","contractInstance":{"name":"UpdateValues","logBuffer":{"output":[]},"default":true},"transactions":[{"name":"unknownTransaction"},{"name":"beforeTransaction"},{"name":"createContext"},{"name":"setup"},{"name":"setNewAssetValue"},{"name":"doubleAssetValue"}],"info":{"title":"","version":""}},"RemoveValues":{"name":"RemoveValues","contractInstance":{"name":"RemoveValues"},"transactions":[{"name":"quarterAssetValue"},{"name":"getAssetValue"}],"info":{"title":"","version":""}},"org.hyperledger.fabric":{"name":"org.hyperledger.fabric","contractInstance":{"name":"org.hyperledger.fabric"},"transactions":[{"name":"GetMetadata"}],"info":{"title":"","version":""}}},"info":{"version":"1.0.0","title":"chaincode"},"components":{"schemas":{}}}';

const schema = fs.readFileSync(path.join(__dirname, '../../fabric-contract-api/schema/contract-schema.json'));

Expand Down
11 changes: 11 additions & 0 deletions fabric-shim/lib/contract-spi/chaincodefromcontract.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ChaincodeFromContract {
this.serializers = serializers;
logger.info(serializers);



// always add in the 'meta' class that has general abilities
const SystemContract = require('./systemcontract');
contractClasses.push(SystemContract);
Expand Down Expand Up @@ -144,6 +146,7 @@ class ChaincodeFromContract {
_resolveContractImplementations(contractClasses) {
const Contract = require('fabric-contract-api').Contract;
const implementations = {};

for (const contractClass of contractClasses) {

const contract = new(contractClass);
Expand All @@ -152,6 +155,10 @@ class ChaincodeFromContract {
}

const name = contract.getName();
if (!this.defaultContractName) {
this.defaultContractName = name;
contract.default = true;
}
const transactions = this._processContractTransactions(contract);
const info = this._processContractInfo(contract);

Expand Down Expand Up @@ -364,6 +371,10 @@ class ChaincodeFromContract {
result.contractName = m[1];
result.function = m[2];

if (!result.contractName || result.contractName.trim() === '') {
result.contractName = this.defaultContractName;
}

return result;
}

Expand Down
20 changes: 16 additions & 4 deletions fabric-shim/test/unit/contract-spi/chaincodefromcontract.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ describe('chaincodefromcontract', () => {
sandbox.stub(ChaincodeFromContract.prototype, '_augmentMetadataFromCode').returns({});
sandbox.stub(ChaincodeFromContract.prototype, '_compileSchemas');
mockery.registerMock('SCAlpha', SCAlpha);
new ChaincodeFromContract([SCAlpha], defaultSerialization);
const cc = new ChaincodeFromContract([SCAlpha], defaultSerialization);
sinon.assert.calledOnce(_checkSuppliedStub);
cc.defaultContractName.should.deep.equal('alpha');
});
it('should handle a single class being passed that is not valid', () => {

Expand All @@ -322,6 +323,15 @@ describe('chaincodefromcontract', () => {
}).should.throw(/invalid contract instance/);

});
it('should handle a two classes being passed as a contract', () => {
const _checkSuppliedStub = sandbox.stub(ChaincodeFromContract.prototype, '_checkAgainstSuppliedMetadata');
sandbox.stub(ChaincodeFromContract.prototype, '_augmentMetadataFromCode').returns({});
sandbox.stub(ChaincodeFromContract.prototype, '_compileSchemas');
mockery.registerMock('SCAlpha', SCAlpha);
const cc = new ChaincodeFromContract([SCBeta, SCAlpha], defaultSerialization);
sinon.assert.calledOnce(_checkSuppliedStub);
cc.defaultContractName.should.deep.equal('beta');
});
});


Expand Down Expand Up @@ -422,6 +432,7 @@ describe('chaincodefromcontract', () => {
});
sandbox.stub(ChaincodeFromContract.prototype, '_dataMarshall').returns(MockDataMarhsall);
cc = new ChaincodeFromContract([SCBeta], defaultSerialization);
cc.defaultContractName = 'default';
});

it('should handle the usual case of ns:fn', () => {
Expand All @@ -430,18 +441,19 @@ describe('chaincodefromcontract', () => {
});

it('should handle the case of no contractName explicit', () => {

const result = cc._splitFunctionName(':function');
result.should.deep.equal({contractName: '', function: 'function'});
result.should.deep.equal({contractName: 'default', function: 'function'});
});

it('should handle the case of no contractName implict', () => {
const result = cc._splitFunctionName('function');
result.should.deep.equal({contractName: '', function: 'function'});
result.should.deep.equal({contractName: 'default', function: 'function'});
});

it('should handle the case of no input', () => {
const result = cc._splitFunctionName('');
result.should.deep.equal({contractName: '', function: ''});
result.should.deep.equal({contractName: 'default', function: ''});
});

it('should handle the case of multiple :', () => {
Expand Down
5 changes: 3 additions & 2 deletions test/fv/annotations/src/test_contract/expected-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"contracts": {
"TestContract": {
"contractInstance": {
"name": "TestContract"
"name": "TestContract",
"default":true
},
"info": {
"title": "",
Expand Down Expand Up @@ -100,4 +101,4 @@
"title": "ts_chaincode",
"version": "1.0.0"
}
}
}

0 comments on commit 5ac251c

Please sign in to comment.