Last active
May 20, 2018 14:31
-
-
Save vitiko/caea36a4a82f61a43c5ddb8bb6866cee to your computer and use it in GitHub Desktop.
Chaincode example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response { | |
fn, args := stub.GetFunctionAndParameters() | |
var result string | |
var err error | |
if fn == "set" { | |
result, err = set(stub, args) | |
} else { // assume 'get' even if fn is nil | |
result, err = get(stub, args) | |
} | |
if err != nil { | |
return shim.Error(err.Error()) | |
} | |
// Return the result as success payload | |
return shim.Success([]byte(result)) | |
} | |
// Set stores the asset (both key and value) on the ledger. | |
func set(stub shim.ChaincodeStubInterface, args []string) (string, error) { | |
if len(args) != 2 { | |
return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value") | |
} | |
err := stub.PutState(args[0], []byte(args[1])) | |
if err != nil { | |
return "", fmt.Errorf("Failed to set asset: %s", args[0]) | |
} | |
return args[1], nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment