-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "FAB-1129 Add cc return value to proposal response"
- Loading branch information
Showing
3 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
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
144 changes: 144 additions & 0 deletions
144
examples/chaincode/go/invokereturnsvalue/invokereturnsvalue.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
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 | ||
|
||
//WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of | ||
//calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has | ||
//to be modified as well with the new ID of chaincode_example02. | ||
//chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of | ||
//hard-coding. | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/hyperledger/fabric/core/chaincode/shim" | ||
) | ||
|
||
// SimpleChaincode example simple Chaincode implementation | ||
type SimpleChaincode struct { | ||
} | ||
|
||
// Init method of chaincode | ||
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) { | ||
_, args := stub.GetFunctionAndParameters() | ||
var A, B string // Entities | ||
var Aval, Bval int // Asset holdings | ||
var err error | ||
|
||
if len(args) != 4 { | ||
return nil, errors.New("Incorrect number of arguments. Expecting 4") | ||
} | ||
|
||
// Initialize the chaincode | ||
A = args[0] | ||
Aval, err = strconv.Atoi(args[1]) | ||
if err != nil { | ||
return nil, errors.New("Expecting integer value for asset holding") | ||
} | ||
B = args[2] | ||
Bval, err = strconv.Atoi(args[3]) | ||
if err != nil { | ||
return nil, errors.New("Expecting integer value for asset holding") | ||
} | ||
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) | ||
|
||
// Write the state to the ledger | ||
err = stub.PutState(A, []byte(strconv.Itoa(Aval))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = stub.PutState(B, []byte(strconv.Itoa(Bval))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte("OK"), nil | ||
} | ||
|
||
// Invoke transaction makes payment of X units from A to B | ||
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) { | ||
_, args := stub.GetFunctionAndParameters() | ||
|
||
var A, B string // Entities | ||
var Aval, Bval int // Asset holdings | ||
var X int // Transaction value | ||
var err error | ||
|
||
if len(args) != 3 { | ||
return nil, errors.New("Incorrect number of arguments. Expecting 3") | ||
} | ||
|
||
A = args[0] | ||
B = args[1] | ||
|
||
// 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 nil, errors.New("Failed to get state") | ||
} | ||
if Avalbytes == nil { | ||
return nil, errors.New("Entity not found") | ||
} | ||
Aval, _ = strconv.Atoi(string(Avalbytes)) | ||
|
||
Bvalbytes, err := stub.GetState(B) | ||
if err != nil { | ||
return nil, errors.New("Failed to get state") | ||
} | ||
if Bvalbytes == nil { | ||
return nil, errors.New("Entity not found") | ||
} | ||
Bval, _ = strconv.Atoi(string(Bvalbytes)) | ||
|
||
// Perform the execution | ||
X, err = strconv.Atoi(args[2]) | ||
if err != nil { | ||
return nil, errors.New("Invalid transaction amount, expecting a integer value") | ||
} | ||
Aval = Aval - X | ||
Bval = Bval + X | ||
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) | ||
|
||
// Write the state back to the ledger | ||
err = stub.PutState(A, []byte(strconv.Itoa(Aval))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = stub.PutState(B, []byte(strconv.Itoa(Bval))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(fmt.Sprintf("{%d,%d}", Aval, Bval)), nil | ||
} | ||
|
||
// Query callback representing the query of a chaincode. Not used in 1.0 (remove when interface is removed) | ||
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
func main() { | ||
err := shim.Start(new(SimpleChaincode)) | ||
if err != nil { | ||
fmt.Printf("Error starting Simple chaincode: %s", err) | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
examples/chaincode/go/invokereturnsvalue/invokereturnsvalue_test.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/chaincode/shim" | ||
) | ||
|
||
func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte, retval []byte) { | ||
result, err := stub.MockInit("1", args) | ||
if err != nil { | ||
fmt.Println("Init failed", err) | ||
t.FailNow() | ||
} | ||
if retval != nil { | ||
if result == nil { | ||
fmt.Printf("Init returned nil, expected %s", string(retval)) | ||
t.FailNow() | ||
} | ||
if string(result) != string(retval) { | ||
fmt.Printf("Init returned %s, expected %s", string(result), string(retval)) | ||
t.FailNow() | ||
} | ||
} | ||
} | ||
|
||
func checkState(t *testing.T, stub *shim.MockStub, name string, value string) { | ||
bytes := stub.State[name] | ||
if bytes == nil { | ||
fmt.Println("State", name, "failed to get value") | ||
t.FailNow() | ||
} | ||
if string(bytes) != value { | ||
fmt.Println("State value", name, "was not", value, "as expected") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte, retval []byte) { | ||
result, err := stub.MockInvoke("1", args) | ||
if err != nil { | ||
fmt.Println("Invoke", args, "failed", err) | ||
t.FailNow() | ||
} | ||
|
||
if retval != nil { | ||
if result == nil { | ||
fmt.Printf("Invoke returned nil, expected %s", string(retval)) | ||
t.FailNow() | ||
} | ||
if string(result) != string(retval) { | ||
fmt.Printf("Invoke returned %s, expected %s", string(result), string(retval)) | ||
t.FailNow() | ||
} | ||
} | ||
} | ||
|
||
func Test_Init(t *testing.T) { | ||
scc := new(SimpleChaincode) | ||
stub := shim.NewMockStub("ex02", scc) | ||
|
||
// Init A=123 B=234 | ||
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123"), []byte("B"), []byte("234")}, []byte("OK")) | ||
|
||
checkState(t, stub, "A", "123") | ||
checkState(t, stub, "B", "234") | ||
} | ||
|
||
func Test_Invoke(t *testing.T) { | ||
scc := new(SimpleChaincode) | ||
stub := shim.NewMockStub("ex02", scc) | ||
|
||
// Init A=567 B=678 | ||
checkInit(t, stub, [][]byte{[]byte("init"), []byte("A"), []byte("567"), []byte("B"), []byte("678")}, []byte("OK")) | ||
|
||
// Invoke A->B for 123 | ||
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("A"), []byte("B"), []byte("123")}, []byte("{444,801}")) | ||
|
||
// Invoke B->A for 234 | ||
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte("B"), []byte("A"), []byte("234")}, []byte("{567,678}")) | ||
} |