Skip to content

Commit

Permalink
[FAB-3223] clean java chaincode get/set state API
Browse files Browse the repository at this point in the history
ChaincodeStub:
 - byte[] getState(String key)
 - void putState(String key, byte[] value)
 - void delState(String key)
 - String getStringState(String key)
 - void putStringState(String key, String value)

Updated example java chaincodes used in tests.

Change-Id: If8d2418fccb3786d2e10f241cd2c08c9f8cc053f
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Apr 26, 2017
1 parent de3d2d1 commit 908af1a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.hyperledger.fabric.shim;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
Expand All @@ -23,8 +25,6 @@
import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;

import com.google.protobuf.ByteString;

public interface ChaincodeStub {

/**
Expand Down Expand Up @@ -63,31 +63,30 @@ public interface ChaincodeStub {
Response invokeChaincode(String chaincodeName, List<byte[]> args, String channel);

/**
* Get the state of the provided key from the ledger, and returns is as a
* string
* Returns the byte array value specified by the key, from the ledger.
*
* @param key
* the key of the desired state
* @return the String value of the requested state
* name of the value
* @return value
* the value read from the ledger
*/
String getState(String key);
byte[] getState(String key);

/**
* Puts the given state into a ledger, automatically wrapping it in a
* ByteString
* Writes the specified value and key into the ledger
*
* @param key
* reference key
* name of the value
* @param value
* value to be put
* the value to write to the ledger
*/
void putState(String key, String value);
void putState(String key, byte[] value);

/**
* Deletes the state of the given key from the ledger
* Removes the specified key from the ledger
*
* @param key
* key of the state to be deleted
* name of the value to be deleted
*/
void delState(String key);

Expand Down Expand Up @@ -169,16 +168,29 @@ default Response invokeChaincodeWithStringArgs(final String chaincodeName, final
}

/**
* Returns the byte array value specified by the key and decoded as a UTF-8
* encoded string, from the ledger.
*
* @param key
* @return
* name of the value
* @return value
* the value read from the ledger
*/
ByteString getRawState(String key);
default String getStringState(String key) {
return new String(getState(key), UTF_8);
}

/**
* Writes the specified value and key into the ledger
*
* @param key
* name of the value
* @param value
* the value to write to the ledger
*/
void putRawState(String key, ByteString value);
default void putStringState(String key, String value) {
putState(key, value.getBytes(UTF_8));
}

/**
* Returns the CHAINCODE type event that will be posted to interested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ public String getTxId() {
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getState(java.lang.String)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getState(String)
*/
@Override
public String getState(String key) {
return handler.handleGetState(key, txid).toStringUtf8();
public byte[] getState(String key) {
return handler.handleGetState(key, txid).toByteArray();
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#putState(java.lang.String, java.lang.String)
* @see org.hyperledger.fabric.shim.ChaincodeStub#putRawState(java.lang.String, com.google.protobuf.ByteString)
*/
@Override
public void putState(String key, String value) {
handler.handlePutState(key, ByteString.copyFromUtf8(value), txid);
public void putState(String key, byte[] value) {
handler.handlePutState(key, ByteString.copyFrom(value), txid);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -143,22 +143,4 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
return handler.handleInvokeChaincode(compositeName, args, this.txid);
}

// ------RAW CALLS------

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getRawState(java.lang.String)
*/
@Override
public ByteString getRawState(String key) {
return handler.handleGetState(key, txid);
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#putRawState(java.lang.String, com.google.protobuf.ByteString)
*/
@Override
public void putRawState(String key, ByteString value) {
handler.handlePutState(key, value, txid);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Response invoke(ChaincodeStub stub) {
return transfer(stub, args);
case "put":
for (int i = 0; i < args.length; i += 2)
stub.putState(args[i], args[i + 1]);
stub.putStringState(args[i], args[i + 1]);
return newSuccessResponse();
case "del":
for (String arg : args)
Expand Down Expand Up @@ -91,8 +91,8 @@ private Response transfer(ChaincodeStub stub, String[] args) {
final String amount = args[2];

// get state of the from/to keys
final String fromKeyState = stub.getState(fromKey);
final String toKeyState = stub.getState(toKey);
final String fromKeyState = stub.getStringState(fromKey);
final String toKeyState = stub.getStringState(toKey);

// parse states as integers
int fromAccountBalance = Integer.parseInt(fromKeyState);
Expand All @@ -111,8 +111,8 @@ private Response transfer(ChaincodeStub stub, String[] args) {
int newFromAccountBalance = fromAccountBalance - transferAmount;
int newToAccountBalance = toAccountBalance + transferAmount;
log.info(String.format("New holding values will be: %s = %d, %s = %d", fromKey, newFromAccountBalance, toKey, newToAccountBalance));
stub.putState(fromKey, Integer.toString(newFromAccountBalance));
stub.putState(toKey, Integer.toString(newToAccountBalance));
stub.putStringState(fromKey, Integer.toString(newFromAccountBalance));
stub.putStringState(toKey, Integer.toString(newToAccountBalance));
log.info("Transfer complete.");

return newSuccessResponse(format("Successfully transferred %d assets from %s to %s.", transferAmount, fromKey, toKey));
Expand All @@ -126,8 +126,8 @@ private Response init(ChaincodeStub stub, String[] args) {
final String account1Balance = args[1];
final String account2Balance = args[3];

stub.putState(accountKey1, new Integer(account1Balance).toString());
stub.putState(accountKey2, new Integer(account2Balance).toString());
stub.putStringState(accountKey1, new Integer(account1Balance).toString());
stub.putStringState(accountKey2, new Integer(account2Balance).toString());

return newSuccessResponse();
}
Expand All @@ -139,7 +139,7 @@ public Response query(ChaincodeStub stub, String function, String[] args) {

return newSuccessResponse(Json.createObjectBuilder()
.add("Name", accountKey)
.add("Amount", Integer.parseInt(stub.getState(accountKey)))
.add("Amount", Integer.parseInt(stub.getStringState(accountKey)))
.build().toString().getBytes(UTF_8)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private Response init(ChaincodeStub stub, String[] args) {
final String account1Balance = args[1];
final String account2Balance = args[3];

stub.putState(accountKey1, new Integer(account1Balance).toString());
stub.putState(accountKey2, new Integer(account2Balance).toString());
stub.putStringState(accountKey1, new Integer(account1Balance).toString());
stub.putStringState(accountKey2, new Integer(account2Balance).toString());

return newSuccessResponse();
}
Expand All @@ -105,8 +105,8 @@ private Response invoke(ChaincodeStub stub, String[] args) {
final String amount = args[2];

// get state of the from/to keys
final String fromKeyState = stub.getState(fromKey);
final String toKeyState = stub.getState(toKey);
final String fromKeyState = stub.getStringState(fromKey);
final String toKeyState = stub.getStringState(toKey);

// parse states as integers
int fromAccountBalance = Integer.parseInt(fromKeyState);
Expand All @@ -126,8 +126,8 @@ private Response invoke(ChaincodeStub stub, String[] args) {
int newToAccountBalance = toAccountBalance + transferAmount;
log.info(format("New holding values will be: %s = %d, %s = %d", fromKey, newFromAccountBalance, toKey,
newToAccountBalance));
stub.putState(fromKey, Integer.toString(newFromAccountBalance));
stub.putState(toKey, Integer.toString(newToAccountBalance));
stub.putStringState(fromKey, Integer.toString(newFromAccountBalance));
stub.putStringState(toKey, Integer.toString(newToAccountBalance));
log.info("Transfer complete.");

return newSuccessResponse(format("Successfully transferred %d assets from %s to %s.", transferAmount, fromKey, toKey));
Expand All @@ -151,7 +151,7 @@ private Response query(ChaincodeStub stub, String[] args) {

return newSuccessResponse(Json.createObjectBuilder()
.add("Name", accountKey)
.add("Amount", Integer.parseInt(stub.getState(accountKey)))
.add("Amount", Integer.parseInt(stub.getStringState(accountKey)))
.build().toString().getBytes(UTF_8));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Response init(ChaincodeStub stub) {

final String key = args.get(1);
final int value = Integer.parseInt(args.get(2));
stub.putState(key, String.valueOf(value));
stub.putStringState(key, String.valueOf(value));

} catch (NumberFormatException e) {
return newBadRequestResponse("Expecting integer value for sum");
Expand Down Expand Up @@ -87,7 +87,7 @@ private Response doQuery(ChaincodeStub stub, String[] args) {
case 2:
case 3: {
final String key = args[0];
final int value = Integer.parseInt(stub.getState(key));
final int value = Integer.parseInt(stub.getStringState(key));
return newSuccessResponse(
Json.createObjectBuilder()
.add("Name", key)
Expand Down Expand Up @@ -145,7 +145,7 @@ private Response doInvoke(ChaincodeStub stub, String[] args) {
}

// update the ledger to indicate a successful invoke
stub.putState(key, String.valueOf(value));
stub.putStringState(key, String.valueOf(value));

// return the called chaincode's response
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Response init(ChaincodeStub stub) {

final String key = args.get(1);
final int value = Integer.parseInt(args.get(2));
stub.putState(key, String.valueOf(value));
stub.putStringState(key, String.valueOf(value));

} catch (NumberFormatException e) {
return newBadRequestResponse("Expecting integer value for sum");
Expand Down Expand Up @@ -124,7 +124,7 @@ private Response doInvoke(ChaincodeStub stub, String[] args) {
final int sum = a + b;

// write new sum to the ledger
stub.putState(key, String.valueOf(sum));
stub.putStringState(key, String.valueOf(sum));

// return sum as string in payload
return newSuccessResponse(String.valueOf(sum).getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class EventSender extends ChaincodeBase {

@Override
public Response init(ChaincodeStub stub) {
stub.putState(EVENT_COUNT, Integer.toString(0));
stub.putStringState(EVENT_COUNT, Integer.toString(0));
return newSuccessResponse();
}

Expand Down Expand Up @@ -65,10 +65,10 @@ public Response invoke(ChaincodeStub stub) {
private Response doInvoke(ChaincodeStub stub, List<String> args) {

// get number of events sent
final int eventNumber = Integer.parseInt(stub.getState(EVENT_COUNT));
final int eventNumber = Integer.parseInt(stub.getStringState(EVENT_COUNT));

// increment number of events sent
stub.putState(EVENT_COUNT, Integer.toString(eventNumber + 1));
stub.putStringState(EVENT_COUNT, Integer.toString(eventNumber + 1));

// create event payload
final String payload = args.stream().collect(joining(",", "Event " + String.valueOf(eventNumber), ""));
Expand All @@ -80,7 +80,7 @@ private Response doInvoke(ChaincodeStub stub, List<String> args) {
}

private Response doQuery(ChaincodeStub stub) {
return newSuccessResponse(String.format("{\"NoEvents\":%d}", Integer.parseInt(stub.getState(EVENT_COUNT))));
return newSuccessResponse(String.format("{\"NoEvents\":%d}", Integer.parseInt(stub.getStringState(EVENT_COUNT))));
}

@Override
Expand Down

0 comments on commit 908af1a

Please sign in to comment.