fabric-samples icon indicating copy to clipboard operation
fabric-samples copied to clipboard

currentState variable is not defined in State object neither determined in it subclass CP

Open alfahami opened this issue 4 years ago • 6 comments

Hello, I've got this error while deserializing the byte[] cp to the cp object.

org.json.JSONException: JSONObject["currentState"] not found while deserializing cp using java

And I found out that the object to deserialize (CP-->byte[]) doesn't have the variable currentState determined.

To reproduce : Follow the cp tutorial using java untill the response of the issue tx.

alfahami avatar Oct 28 '21 22:10 alfahami

Can you point to the file and code line in question?

denyeart avatar Nov 10 '21 06:11 denyeart

Sure. In the application-java papernet, the CommercialPaper.java has a field "currentState" on the deserialize function which has not been defined in the contract-java so not in the byte object to be serialized. I tried to declaring a int currentState in the State Object and define it in CommercialPaper both in contract-java and papernetwork but I couldn't still get the current state of the object. I ended up getting rid of current state variable and used another approch by getting the state of the object using the state variable defined in CP while deserializing. Here is the function in commercial-paper/organization/magnetocorp/application-java/src/org/papernet

`/** * Deserialize a state data to commercial paper * * @param {Buffer} data to form back into the object */ public static CommercialPaper deserialize(byte[] data) { JSONObject json = new JSONObject(new String(data, UTF_8));

    String issuer = json.getString("issuer");
    String paperNumber = json.getString("paperNumber");
    String issueDateTime = json.getString("issueDateTime");
    String maturityDateTime = json.getString("maturityDateTime");
    String owner = json.getString("owner");
    int faceValue = json.getInt("faceValue");
    int currentState = json.getInt("currentState");
    String state = STATES[currentState-1];
    return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state);
}

`

alfahami avatar Nov 14 '21 16:11 alfahami

Why can't I install the chain code after I change it~ I need help ...

Tangziyi1 avatar Nov 25 '21 07:11 Tangziyi1

Make sure you rebuild your smart contracts and have the same code on both organisation. After rebuilding with gradle, the installation, definition and committing should work fine.

alfahami avatar Dec 30 '21 23:12 alfahami

The issue was in state variable. I changed it to String type. Below code change is working. /** * Deserialize a state data to commercial paper * * @param {Buffer} data to form back into the object */ public static CommercialPaper deserialize(byte[] data) { JSONObject json = new JSONObject(new String(data, UTF_8));

    String issuer = json.getString("issuer");
    String paperNumber = json.getString("paperNumber");
    String issueDateTime = json.getString("issueDateTime");
    String maturityDateTime = json.getString("maturityDateTime");
    String owner = json.getString("owner");
    int faceValue = json.getInt("faceValue");
   //below is the change
    String state = json.getString("state");

    return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state);
}

kirant2001 avatar Jan 04 '22 09:01 kirant2001

Had figured it out, was just lazy updating here.

alfahami avatar Jan 10 '22 13:01 alfahami

The current code in the release-2.2 branch looks to match the working code above so I will close this issue.

bestbeforetoday avatar Dec 03 '24 17:12 bestbeforetoday