Skip to content

Commit 6ddbefe

Browse files
committed
[FAB-1055] remove query from chaincode example
Part of FAB-1055. Chaincode query interface will not be used in v1.0. It will be removed by several step. This commit change old "Query" function in go examples to "Invoke". Change-Id: Ic00828b8cd1b8a7e7dfb12df2e3be727ff5c4e17 Signed-off-by: jiangyaoguo <[email protected]>
1 parent 1a2bdb4 commit 6ddbefe

File tree

18 files changed

+109
-147
lines changed

18 files changed

+109
-147
lines changed

core/chaincode/exectransaction_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,9 @@ func chaincodeInvokeChaincode(t *testing.T, user string) (err error) {
700700

701701
// Invoke second chaincode passing the first chaincode's name as first param,
702702
// which will inturn invoke the first chaincode
703-
f = spec1.ChaincodeID.Name
704-
args = util.ToChaincodeArgs(f, "e", "1")
703+
f = "invoke"
704+
cid := spec1.ChaincodeID.Name
705+
args = util.ToChaincodeArgs(f, cid, "e", "1")
705706

706707
spec2 = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID2, CtorMsg: &pb.ChaincodeInput{Args: args}, SecureContext: user}
707708
// Invoke chaincode
@@ -890,7 +891,7 @@ func TestGetEvent(t *testing.T) {
890891

891892
time.Sleep(time.Second)
892893

893-
args := util.ToChaincodeArgs("", "i", "am", "satoshi")
894+
args := util.ToChaincodeArgs("invoke", "i", "am", "satoshi")
894895

895896
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: cID, CtorMsg: &pb.ChaincodeInput{Args: args}}
896897

examples/chaincode/go/asset_management/asset_management.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,18 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
252252
} else if function == "transfer" {
253253
// Transfer ownership
254254
return t.transfer(stub, args)
255+
} else if function == "query" {
256+
// Query owner
257+
return t.query(stub, args)
255258
}
256259

257260
return nil, errors.New("Received unknown function invocation")
258261
}
259262

260-
// Query callback representing the query of a chaincode
261263
// Supported functions are the following:
262264
// "query(asset)": returns the owner of the asset.
263265
// Anyone can invoke this function.
264-
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
265-
function, args := stub.GetFunctionAndParameters()
266-
myLogger.Debugf("Query [%s]", function)
267-
268-
if function != "query" {
269-
return nil, errors.New("Invalid query function name. Expecting 'query' but found '" + function + "'")
270-
}
271-
266+
func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
272267
var err error
273268

274269
if len(args) != 1 {
@@ -279,7 +274,7 @@ func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]by
279274
// Who is the owner of the asset?
280275
asset := args[0]
281276

282-
myLogger.Debugf("Arg [%s]", string(asset))
277+
myLogger.Debugf("Query [%s]", string(asset))
283278

284279
var columns []shim.Column
285280
col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}

examples/chaincode/go/asset_management02/asset_management02.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (t *AssetManagementChaincode) Init(stub shim.ChaincodeStubInterface) ([]byt
202202
return nil, dHandler.createTable(stub)
203203
}
204204

205-
// Invoke method is the interceptor of all invocation transactions, its job is to direct
205+
// Invoke method is the interceptor of all invocation transactions, its job is to direct
206206
// invocation transactions to intended APIs
207207
func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
208208
function, args := stub.GetFunctionAndParameters()
@@ -215,25 +215,13 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
215215
} else if function == "transferOwnership" {
216216
// Transfer ownership
217217
return t.transferOwnership(stub, args)
218-
}
219-
220-
return nil, errors.New("Received unknown function invocation")
221-
}
222-
223-
// Query method is the interceptor of all invocation transactions, its job is to direct
224-
// query transactions to intended APIs, and return the result back to callers
225-
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
226-
function, args := stub.GetFunctionAndParameters()
227-
myLogger.Debugf("********************************Query****************************************")
228-
229-
// Handle different functions
230-
if function == "getOwnerContactInformation" {
218+
} else if function == "getOwnerContactInformation" {
231219
return t.getOwnerContactInformation(stub, args)
232220
} else if function == "getBalance" {
233221
return t.getBalance(stub, args)
234222
}
235223

236-
return nil, errors.New("Received unknown function query invocation with function " + function)
224+
return nil, errors.New("Received unknown function invocation")
237225
}
238226

239227
func main() {

examples/chaincode/go/asset_management_with_roles/asset_management_with_roles.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,15 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
228228
} else if function == "transfer" {
229229
// Transfer ownership
230230
return t.transfer(stub, args)
231+
} else if function == "query" {
232+
// Query asset
233+
return t.query(stub, args)
231234
}
232235

233236
return nil, errors.New("Received unknown function invocation")
234237
}
235238

236-
// Query callback representing the query of a chaincode
237-
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
238-
function, args := stub.GetFunctionAndParameters()
239-
if function != "query" {
240-
return nil, errors.New("Invalid query function name. Expecting 'query' but found '" + function + "'")
241-
}
242-
239+
func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
243240
var err error
244241

245242
if len(args) != 1 {

examples/chaincode/go/attributes_to_state/attributes_to_state.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,17 @@ func (t *Attributes2State) Init(stub shim.ChaincodeStubInterface) ([]byte, error
5858
return nil, nil
5959
}
6060

61-
// Invoke takes two arguements, a key and value, and stores these in the state
6261
func (t *Attributes2State) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
6362
function, args := stub.GetFunctionAndParameters()
6463
if function == "delete" {
6564
return nil, t.delete(stub, args)
65+
} else if function == "submit" {
66+
return nil, t.setStateToAttributes(stub, args)
67+
} else if function == "read" {
68+
return t.query(stub, args)
6669
}
6770

68-
if function != "submit" {
69-
return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\"")
70-
}
71-
err := t.setStateToAttributes(stub, args)
72-
if err != nil {
73-
return nil, err
74-
}
75-
return nil, nil
71+
return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\" or \"read\"")
7672
}
7773

7874
// delete Deletes an entity from the state, returning error if the entity was not found in the state.
@@ -106,12 +102,7 @@ func (t *Attributes2State) delete(stub shim.ChaincodeStubInterface, args []strin
106102
return nil
107103
}
108104

109-
// Query callback representing the query of a chaincode
110-
func (t *Attributes2State) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
111-
function, args := stub.GetFunctionAndParameters()
112-
if function != "read" {
113-
return nil, errors.New("Invalid query function name. Expecting \"read\"")
114-
}
105+
func (t *Attributes2State) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
115106
var attributeName string // Name of the attributeName to query.
116107
var err error
117108

examples/chaincode/go/authorizable_counter/authorizable_counter.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ func (t *AuthorizableCounterChaincode) Init(stub shim.ChaincodeStubInterface) ([
3535
return nil, err
3636
}
3737

38-
//Invoke Transaction makes increment counter
39-
func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
40-
function, _ := stub.GetFunctionAndParameters()
41-
if function != "increment" {
42-
return nil, errors.New("Invalid invoke function name. Expecting \"increment\"")
43-
}
38+
//Invoke makes increment counter
39+
func (t *AuthorizableCounterChaincode) increment(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
4440
val, err := stub.ReadCertAttribute("position")
4541
fmt.Printf("Position => %v error %v \n", string(val), err)
4642
isOk, _ := stub.VerifyAttribute("position", []byte("Software Engineer")) // Here the ABAC API is called to verify the attribute, just if the value is verified the counter will be incremented.
@@ -62,12 +58,7 @@ func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface)
6258

6359
}
6460

65-
// Query callback representing the query of a chaincode
66-
func (t *AuthorizableCounterChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
67-
function, _ := stub.GetFunctionAndParameters()
68-
if function != "read" {
69-
return nil, errors.New("Invalid query function name. Expecting \"read\"")
70-
}
61+
func (t *AuthorizableCounterChaincode) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
7162
var err error
7263

7364
// Get the state from the ledger
@@ -87,6 +78,20 @@ func (t *AuthorizableCounterChaincode) Query(stub shim.ChaincodeStubInterface) (
8778
return Avalbytes, nil
8879
}
8980

81+
// Invoke method is the interceptor of all invocation transactions, its job is to direct
82+
// invocation transactions to intended APIs
83+
func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
84+
function, args := stub.GetFunctionAndParameters()
85+
86+
// Handle different functions
87+
if function == "increment" {
88+
return t.increment(stub, args)
89+
} else if function == "read" {
90+
return t.read(stub, args)
91+
}
92+
return nil, errors.New("Received unknown function invocation, Expecting \"increment\" \"read\"")
93+
}
94+
9095
func main() {
9196
err := shim.Start(new(AuthorizableCounterChaincode))
9297
if err != nil {

examples/chaincode/go/chaincode_example01/chaincode_example01.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
6969
return nil, nil
7070
}
7171

72-
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
73-
_, args := stub.GetFunctionAndParameters()
72+
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
7473
// Transaction makes payment of X units from A to B
7574
var err error
7675
X, err = strconv.Atoi(args[0])
@@ -84,9 +83,13 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, erro
8483
return nil, err
8584
}
8685

87-
// Query callback representing the query of a chaincode
88-
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
89-
return nil, nil
86+
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
87+
function, args := stub.GetFunctionAndParameters()
88+
if function == "invoke" {
89+
return t.invoke(stub, args)
90+
}
91+
92+
return nil, errors.New("Invalid invoke function name. Expecting \"invoke\"")
9093
}
9194

9295
func main() {

examples/chaincode/go/chaincode_example02/chaincode_example02.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,24 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
7171
return nil, nil
7272
}
7373

74-
// Transaction makes payment of X units from A to B
7574
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
7675
function, args := stub.GetFunctionAndParameters()
77-
if function == "delete" {
76+
if function == "invoke" {
77+
// Make payment of X units from A to B
78+
return t.invoke(stub, args)
79+
} else if function == "delete" {
7880
// Deletes an entity from its state
7981
return t.delete(stub, args)
80-
}
81-
if function == "query" {
82+
} else if function == "query" {
8283
// the old "Query" is now implemtned in invoke
8384
return t.query(stub, args)
8485
}
8586

87+
return nil, errors.New("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
88+
}
89+
90+
// Transaction makes payment of X units from A to B
91+
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
8692
var A, B string // Entities
8793
var Aval, Bval int // Asset holdings
8894
var X int // Transaction value

examples/chaincode/go/chaincode_example03/chaincode_example03.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
5858

5959
// Invoke is a no-op
6060
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
61-
return nil, nil
62-
}
63-
64-
// Query callback representing the query of a chaincode
65-
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
6661
function, args := stub.GetFunctionAndParameters()
67-
if function != "query" {
68-
return nil, errors.New("Invalid query function name. Expecting \"query\"")
62+
if function == "query" {
63+
return t.query(stub, args)
6964
}
65+
66+
return nil, errors.New("Invalid invoke function name. Expecting \"query\"")
67+
}
68+
69+
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
7070
var A string // Entity
7171
var Aval int // Asset holding
7272
var err error

examples/chaincode/go/chaincode_example03/chaincode_example03_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
4444

4545
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
4646
_, err := stub.MockInit("1", args)
47-
bytes, err := scc.Query(stub)
47+
bytes, err := scc.Invoke(stub)
4848
if err != nil {
4949
// expected failure
5050
fmt.Println("Query below is expected to fail")

examples/chaincode/go/chaincode_example04/chaincode_example04.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,18 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
5858
}
5959

6060
// Invoke invokes another chaincode - chaincode_example02, upon receipt of an event and changes event state
61-
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
61+
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
6262
var event string // Event entity
6363
var eventVal int // State of event
6464
var err error
6565

66-
function, args := stub.GetFunctionAndParameters()
67-
68-
if function == "query" {
69-
return t.query(stub, args)
66+
if len(args) != 3 {
67+
return nil, errors.New("Incorrect number of arguments. Expecting 3")
7068
}
7169

72-
if len(args) != 2 {
73-
return nil, errors.New("Incorrect number of arguments. Expecting 2")
74-
}
75-
76-
chainCodeToCall := function
77-
78-
event = args[0]
79-
eventVal, err = strconv.Atoi(args[1])
70+
chainCodeToCall := args[0]
71+
event = args[1]
72+
eventVal, err = strconv.Atoi(args[2])
8073
if err != nil {
8174
return nil, errors.New("Expected integer value for event state change")
8275
}
@@ -106,7 +99,6 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, erro
10699
return nil, nil
107100
}
108101

109-
// query callback representing the query of a chaincode
110102
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
111103
var event string // Event entity
112104
var err error
@@ -134,6 +126,17 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
134126
return []byte(jsonResp), nil
135127
}
136128

129+
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
130+
function, args := stub.GetFunctionAndParameters()
131+
if function == "invoke" {
132+
return t.invoke(stub, args)
133+
} else if function == "query" {
134+
return t.query(stub, args)
135+
}
136+
137+
return nil, errors.New("Invalid invoke function name. Expecting \"invoke\" \"query\"")
138+
}
139+
137140
func main() {
138141
err := shim.Start(new(SimpleChaincode))
139142
if err != nil {

examples/chaincode/go/chaincode_example04/chaincode_example04_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func TestExample04_Invoke(t *testing.T) {
106106
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})
107107

108108
// Invoke A->B for 10 via Example04's chaincode
109-
checkInvoke(t, stub, [][]byte{[]byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
109+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
110110
checkQuery(t, stub, "Event", eventResponse)
111111
checkQuery(t, stubEx2, "a", "101")
112112
checkQuery(t, stubEx2, "b", "232")
113113

114114
// Invoke A->B for 10 via Example04's chaincode
115-
checkInvoke(t, stub, [][]byte{[]byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
115+
checkInvoke(t, stub, [][]byte{[]byte("invoke"), []byte(chaincodeToInvoke), []byte("Event"), []byte("1")})
116116
checkQuery(t, stub, "Event", eventResponse)
117117
checkQuery(t, stubEx2, "a", "91")
118118
checkQuery(t, stubEx2, "b", "242")

0 commit comments

Comments
 (0)