Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Error with program argument #27

Open
Andromelus opened this issue Jun 18, 2019 · 1 comment
Open

Error with program argument #27

Andromelus opened this issue Jun 18, 2019 · 1 comment

Comments

@Andromelus
Copy link

Andromelus commented Jun 18, 2019

Error

When running node test.js , obtains the following error: gist

Main message:

ERROR [lib/handler.js] uncaughtException: The "peer.address" program argument must be set to a legitimate value of :

Configuration

Yarn

yarn --version
1.16.0

package.json

{
  "devDependencies": {
    "@theledger/fabric-mock-stub": "2.0.0"
  }
}

test.js

const { idManager } = require('../public-channel/identity-manager/index');
const { ChaincodeMockStub, Transform } =  require("@theledger/fabric-mock-stub");

const identityManager = new idManager();


describe('Init chaincode', () => {

    it("Should init idManager without issues", async () => {
        const mockStub = new ChaincodeMockStub("MyMockStub", identityManager);

        const response = await mockStub.mockInit("tx1", []);

        expect(response.status).to.eql(200)
        // Your test code
    });

chaincode

const shim = require('fabric-shim');
const errorCodes = {
    unknownFunction: "ERR1",
    incorrectNumberOfArgs: "ERR2",
    noRight: "ERR3",
    companyDiffers: "ERR4",
    invokeChaincodeFailed: "ERR5",
    queryStateNotFound: "ERR6",
    accessListIncorrect: "ERR7",
    chaincodeInitAlreadyDone: "ERR8",
    keyDoesNotExist: "ERR9",
    identityAlreadyExists: "ERR10"
}
var Chaincode = class {

    /**
    * Function invoked during chaincode instantiation
    * @constructor
    * @param {stub} stub - The chaincode API interface
    * @return {Buffer} returns data as buffer
    */
    Init(stub) {
        return shim.success();
    }

    /**
    * This function is the entry point of the chaincode. EVERY function invokation will start here
    * @function Invoke
    * @param {stub} stub - The chaincode API interface
    * @return {Buffer} returns data as buffer
    */
    async Invoke(stub) {
        let ret = stub.getFunctionAndParameters();
        console.info(ret);

        let method = this[ret.fcn];
        if (!method) {
            console.error('no function of name:' + ret.fcn + ' found');
            throw new Error(errorCodes.incorrectNumberOfArgs);
        }
        try {
            let payload = await method(stub, ret.params);
            return shim.success(payload);
        } catch (err) {
            console.info(err);
            return shim.error(err);
        }
    }

    /**
    * Creates an identity in the identity-manager world state
    * @function createIdentity
    * @param {stub} stub - The chaincode API interface
    * @param {Array} args - The ID of the identity to create | The ID of the identity of the creator | The first name of the identity holder | The last name of the identity holder | The company if the identity holder | The status of the identity to create | The role of the identity to create | The email of the identity to create    
    */
    async createIdentity(stub, args) {
        console.info('============= START : Create Identity ===========');
        if (args.length != 8) {

            throw new Error(errorCodes.incorrectNumberOfArgs);
        }

        try {

            //Check if identity exists. If it does, cannot create it
            var identityToCreateAsBytes = await stub.getState(args[0]);
            if (identityToCreateAsBytes.length) {
                console.log("Identity already exist");
                throw new Error(errorCodes.identityAlreadyExists);
            }

            var identityCreatorAsBytes = await stub.getState(args[1]);
            var identityCreatorAsJson = JSON.parse(identityCreatorAsBytes);

            // Only chain.administrator can create another chain.administrator
            if (args[6] === "chain.administrator" && identityCreatorAsJson.role !== "chain.administrator") {
                console.error("Only chain administrator can create a chain administrator identity");
                throw new Error(errorCodes.noRight);
            }

            // checks the role of the identity creator
            if (identityCreatorAsJson.role !== "org.administrator" && identityCreatorAsJson.role !== "chain.administrator") {
                console.error("Identity creator does not have rights to create this new identity");
                throw new Error(errorCodes.noRight);
            }
            // checks the company of the identity creator and created
            if (identityCreatorAsJson.company !== args[4]) {
                console.error("The company of the creator differs from the new identity company")
                throw new Error(errorCodes.companyDiffers);
            }

            var identity = {
                "providerId": args[1],
                "firstName": args[2],
                "lastName": args[3],
                "company": args[4],
                "status": args[5],
                "role": args[6],
                "email": args[7]
            }

            var eventData = {
                "identityId": args[0],
                "providerId": args[1],
                "company": args[4]
            };
            stub.setEvent("createIdentity", Buffer.from(JSON.stringify(eventData)));
            await stub.putState(args[0], Buffer.from(JSON.stringify(identity)));
            console.info('============= END : Create Identity ===========');
            identity['id'] = args[0];
            return Buffer.from(JSON.stringify(identity));
        } catch (error) {
            throw new Error(error);
        }


    }



    //many other functions
shim.start(new Chaincode());
@sneljo1
Copy link
Contributor

sneljo1 commented Aug 1, 2019

shim.start may not be called in a node context, only when running from a peer. When writing your tests, please make sure shim.start and your chaincode are in a seperate file.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants