Skip to content

Commit

Permalink
[FAB-15433] Add argument metadata
Browse files Browse the repository at this point in the history
Change-Id: Ie9db72d2ec58e2ee5d1d766bda7ae819b25271b3
Signed-off-by: James Taylor <jamest@uk.ibm.com>
  • Loading branch information
jt-nti committed May 16, 2019
1 parent 4852d22 commit 08dd598
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/test/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ gulp.task('query_functions', async (done) => {

const metadata = JSON.parse(stdout);

const expectedMetadata = '{"$schema":"https://fabric-shim.github.io/master/contract-schema.json","contracts":{"UpdateValues":{"name":"UpdateValues","contractInstance":{"name":"UpdateValues","logBuffer":{"output":[]},"default":true},"transactions":[{"name":"setup","tags":["submitTx"]},{"name":"setNewAssetValue","tags":["submitTx"]},{"name":"doubleAssetValue","tags":["submitTx"]}],"info":{"title":"","version":""}},"RemoveValues":{"name":"RemoveValues","contractInstance":{"name":"RemoveValues"},"transactions":[{"name":"quarterAssetValue","tags":["submitTx"]},{"name":"getAssetValue","tags":["submitTx"]}],"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 expectedMetadata = '{"$schema":"https://fabric-shim.github.io/master/contract-schema.json","contracts":{"UpdateValues":{"name":"UpdateValues","contractInstance":{"name":"UpdateValues","logBuffer":{"output":[]},"default":true},"transactions":[{"name":"setup","tags":["submitTx"]},{"name":"setNewAssetValue","tags":["submitTx"],"parameters":[{"name":"arg0","description":"Argument 0","schema":{"type":"string"}}]},{"name":"doubleAssetValue","tags":["submitTx"]}],"info":{"title":"","version":""}},"RemoveValues":{"name":"RemoveValues","contractInstance":{"name":"RemoveValues"},"transactions":[{"name":"quarterAssetValue","tags":["submitTx"]},{"name":"getAssetValue","tags":["submitTx"]}],"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
14 changes: 14 additions & 0 deletions fabric-shim/lib/contract-spi/chaincodefromcontract.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ class ChaincodeFromContract {
transaction.tags = ['submitTx'];
}

// add 'argN' parameters, skipping the first (stub) parameter
if (propValue.length > 1) {
transaction.parameters = [];
for (let param = 1; param < propValue.length; param++) {
transaction.parameters.push({
name: `arg${param - 1}`,
description: `Argument ${param - 1}`,
schema: {
type: 'string'
}
});
}
}

transactions.push(transaction);
}
}
Expand Down
90 changes: 89 additions & 1 deletion fabric-shim/test/unit/contract-spi/chaincodefromcontract.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,22 @@ describe('chaincodefromcontract', () => {
sinon.assert.calledWith(getMetadataStub, 'fabric:transactions', ci);
transactions.should.deep.equal([{
name: 'alpha',
parameters: [
{
description: 'Argument 0',
name: 'arg0',
schema: {
type: 'string'
}
},
{
description: 'Argument 1',
name: 'arg1',
schema: {
type: 'string'
}
}
],
tags: ['submitTx']
}]);

Expand All @@ -1025,6 +1041,22 @@ describe('chaincodefromcontract', () => {
sinon.assert.calledWith(getMetadataStub, 'fabric:transactions', ci);
transactions.should.deep.equal([{
name: 'alpha',
parameters: [
{
description: 'Argument 0',
name: 'arg0',
schema: {
type: 'string'
}
},
{
description: 'Argument 1',
name: 'arg1',
schema: {
type: 'string'
}
}
],
tags: ['submitTx']
}]);

Expand All @@ -1045,7 +1077,23 @@ describe('chaincodefromcontract', () => {
sinon.assert.calledOnce(getMetadataStub);
sinon.assert.calledWith(getMetadataStub, 'fabric:transactions', ci);
transactions.should.deep.equal([{
name: 'alpha'
name: 'alpha',
parameters: [
{
description: 'Argument 0',
name: 'arg0',
schema: {
type: 'string'
}
},
{
description: 'Argument 1',
name: 'arg1',
schema: {
type: 'string'
}
}
],
}]);
});

Expand All @@ -1062,6 +1110,46 @@ describe('chaincodefromcontract', () => {
some: 'transaction'
}]);
});

it ('should handle transactions with no arguments', () => {
const getMetadataStub = sandbox.stub(Reflect, 'getMetadata').returns(null);

const ci = cc.contractImplementations.alpha.contractInstance;
Object.getPrototypeOf(ci).conga = (api) => {};

const transactions = ChaincodeFromContract.prototype._processContractTransactions(ci, []);

sinon.assert.calledOnce(getMetadataStub);
sinon.assert.calledWith(getMetadataStub, 'fabric:transactions', ci);
transactions.should.deep.equal([
{
name: 'alpha',
parameters: [
{
description: 'Argument 0',
name: 'arg0',
schema: {
type: 'string'
}
},
{
description: 'Argument 1',
name: 'arg1',
schema: {
type: 'string'
}
}
],
tags: ['submitTx']
},
{
name: 'conga',
tags: ['submitTx']
}
]);

delete Object.getPrototypeOf(ci).conga;
});
});

describe ('#_augmentMetadataFromCode', () => {
Expand Down

0 comments on commit 08dd598

Please sign in to comment.