Skip to content

Commit

Permalink
[FAB-11134] Add reset to example_cc
Browse files Browse the repository at this point in the history
This change modifies example_cc to be resetable. This allows
integration tests to reset the state without instantiating a new
chaincode.

The CCPackager unit test data is also deceoupled from the integration
test fixtures.

Change-Id: Iba2e2535af0111471c8aaedcb65bbb717104051e
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Jul 13, 2018
1 parent 71c7f7f commit 4a8d4ae
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/fab/ccpackager/gopackager/packager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewCCPackage(t *testing.T) {
pwd, err := os.Getwd()
assert.Nil(t, err, "error from os.Getwd %s", err)

ccPackage, err := NewCCPackage("github.com", path.Join(pwd, "../../../../test/fixtures/testdata"))
ccPackage, err := NewCCPackage("github.com", path.Join(pwd, "testdata"))
assert.Nil(t, err, "error from Create %s", err)

r := bytes.NewReader(ccPackage.Code)
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestBadPackagePathGoLangCC(t *testing.T) {
t.Fatalf("error from os.Getwd %s", err)
}

_, err = NewCCPackage("github.com", path.Join(pwd, "../../../../test/fixturesABC"))
_, err = NewCCPackage("github.com", path.Join(pwd, "testdata/fixturesABC"))
if err == nil {
t.Fatalf("error expected from Create %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"encoding/json"
"fmt"
"strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

// Init ...
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
txID := stub.GetTxID()
fmt.Printf("[txID %s] ########### example_cc Init ###########\n", txID)
_, args := stub.GetFunctionAndParameters()
var A, B string // Entities
var Aval, Bval int // Asset holdings
var err error

if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}

// Initialize the chaincode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
fmt.Printf("[txID %s] Aval = %d, Bval = %d\n", txID, Aval, Bval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return shim.Error(err.Error())
}

err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return shim.Error(err.Error())
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("[txID %s] Transient data in 'init' : %s\n", txID, transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)

}

// Query ...
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error("Unknown supported call")
}

// Invoke ...
// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Printf("[txID %s] ########### example_cc Invoke ###########\n", stub.GetTxID())
function, args := stub.GetFunctionAndParameters()

if function == "invokecc" {
return t.invokeCC(stub, args)
}

if function != "invoke" {
return shim.Error("Unknown function call")
}

if len(args) < 2 {
return shim.Error("Incorrect number of arguments. Expecting at least 2")
}

if args[0] == "delete" {
// Deletes an entity from its state
return t.delete(stub, args)
}

if args[0] == "query" {
// queries an entity state
return t.query(stub, args)
}
if args[0] == "move" {
eventID := "testEvent"
if len(args) >= 5 {
eventID = args[4]
}
if err := stub.SetEvent(eventID, []byte("Test Payload")); err != nil {
return shim.Error("Unable to set CC event: testEvent. Aborting transaction ...")
}
return t.move(stub, args)
}
return shim.Error("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'")
}

func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string) pb.Response {
txID := stub.GetTxID()
// must be an invoke
var A, B string // Entities
var Aval, Bval int // Asset holdings
var X int // Transaction value
var err error
if len(args) < 4 {
return shim.Error("Incorrect number of arguments. Expecting 4, function followed by 2 names and 1 value")
}

A = args[1]
B = args[2]

// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return shim.Error("Failed to get state")
}
if Avalbytes == nil {
return shim.Error("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))

Bvalbytes, err := stub.GetState(B)
if err != nil {
return shim.Error("Failed to get state")
}
if Bvalbytes == nil {
return shim.Error("Entity not found")
}
Bval, _ = strconv.Atoi(string(Bvalbytes))

// Perform the execution
X, err = strconv.Atoi(args[3])
if err != nil {
return shim.Error("Invalid transaction amount, expecting a integer value")
}
Aval = Aval - X
Bval = Bval + X
fmt.Printf("[txID %s] Aval = %d, Bval = %d\n", txID, Aval, Bval)

// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return shim.Error(err.Error())
}

err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return shim.Error(err.Error())
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("[txID %s] Transient data in 'move' : %s\n", txID, transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)
}

// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}

A := args[1]

// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return shim.Error("Failed to delete state")
}

return shim.Success(nil)
}

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var A string // Entities
var err error

if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
}

A = args[1]

// Get the state from the ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
return shim.Error(jsonResp)
}

if Avalbytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
return shim.Error(jsonResp)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
fmt.Printf("[txID %s] Query Response:%s\n", stub.GetTxID(), jsonResp)
return shim.Success(Avalbytes)
}

type argStruct struct {
Args []string `json:"Args"`
}

func asBytes(args []string) [][]byte {
bytes := make([][]byte, len(args))
for i, arg := range args {
bytes[i] = []byte(arg)
}
return bytes
}

// invokeCC invokes another chaincode
// arg0: ID of chaincode to invoke
// arg1: Chaincode arguments in the form: {"Args": ["arg0", "arg1",...]}
func (t *SimpleChaincode) invokeCC(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) < 2 {
return shim.Error("Incorrect number of arguments. Expecting ID of chaincode to invoke and args")
}

ccID := args[0]
invokeArgsJSON := args[1]

argStruct := argStruct{}
if err := json.Unmarshal([]byte(invokeArgsJSON), &argStruct); err != nil {
return shim.Error(fmt.Sprintf("Invalid invoke args: %s", err))
}

if err := stub.PutState(stub.GetTxID()+"_invokedcc", []byte(ccID)); err != nil {
return shim.Error(fmt.Sprintf("Error putting state: %s", err))
}

return stub.InvokeChaincode(ccID, asBytes(argStruct.Args), "")
}

func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
54 changes: 41 additions & 13 deletions test/fixtures/testdata/src/github.com/example_cc/example_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"encoding/json"
"errors"
"fmt"
"strconv"

Expand All @@ -34,46 +40,64 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
txID := stub.GetTxID()
fmt.Printf("[txID %s] ########### example_cc Init ###########\n", txID)
_, args := stub.GetFunctionAndParameters()

err := t.reset(stub, txID, args)
if err != nil {
return shim.Error(err.Error())
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("[txID %s] Transient data in 'init' : %s\n", txID, transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)

}

func (t *SimpleChaincode) resetCC(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Deletes an entity from its state
if err := t.reset(stub, stub.GetTxID(), args); err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}

func (t *SimpleChaincode) reset(stub shim.ChaincodeStubInterface, txID string, args []string) error {
var A, B string // Entities
var Aval, Bval int // Asset holdings
var err error

if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4")
return errors.New("Incorrect number of arguments. Expecting 4")
}

// Initialize the chaincode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
return errors.New("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
return errors.New("Expecting integer value for asset holding")
}
fmt.Printf("[txID %s] Aval = %d, Bval = %d\n", txID, Aval, Bval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return shim.Error(err.Error())
return err
}

err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return shim.Error(err.Error())
return err
}

if transientMap, err := stub.GetTransient(); err == nil {
if transientData, ok := transientMap["result"]; ok {
fmt.Printf("[txID %s] Transient data in 'init' : %s\n", txID, transientData)
return shim.Success(transientData)
}
}
return shim.Success(nil)

return nil
}

// Query ...
Expand All @@ -91,6 +115,10 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return t.invokeCC(stub, args)
}

if function == "reset" {
return t.resetCC(stub, args)
}

if function != "invoke" {
return shim.Error("Unknown function call")
}
Expand Down

0 comments on commit 4a8d4ae

Please sign in to comment.