Skip to content

Commit

Permalink
[FAB-1055] remove query from chaincode example
Browse files Browse the repository at this point in the history
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 <jiangyaoguo@gmail.com>
  • Loading branch information
jiangyaoguo committed Nov 29, 2016
1 parent 1a2bdb4 commit 6ddbefe
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 147 deletions.
7 changes: 4 additions & 3 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,9 @@ func chaincodeInvokeChaincode(t *testing.T, user string) (err error) {

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

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

time.Sleep(time.Second)

args := util.ToChaincodeArgs("", "i", "am", "satoshi")
args := util.ToChaincodeArgs("invoke", "i", "am", "satoshi")

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

Expand Down
15 changes: 5 additions & 10 deletions examples/chaincode/go/asset_management/asset_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,18 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
} else if function == "transfer" {
// Transfer ownership
return t.transfer(stub, args)
} else if function == "query" {
// Query owner
return t.query(stub, args)
}

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

// Query callback representing the query of a chaincode
// Supported functions are the following:
// "query(asset)": returns the owner of the asset.
// Anyone can invoke this function.
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
myLogger.Debugf("Query [%s]", function)

if function != "query" {
return nil, errors.New("Invalid query function name. Expecting 'query' but found '" + function + "'")
}

func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var err error

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

myLogger.Debugf("Arg [%s]", string(asset))
myLogger.Debugf("Query [%s]", string(asset))

var columns []shim.Column
col1 := shim.Column{Value: &shim.Column_String_{String_: asset}}
Expand Down
18 changes: 3 additions & 15 deletions examples/chaincode/go/asset_management02/asset_management02.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (t *AssetManagementChaincode) Init(stub shim.ChaincodeStubInterface) ([]byt
return nil, dHandler.createTable(stub)
}

// Invoke method is the interceptor of all invocation transactions, its job is to direct
// Invoke method is the interceptor of all invocation transactions, its job is to direct
// invocation transactions to intended APIs
func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
Expand All @@ -215,25 +215,13 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
} else if function == "transferOwnership" {
// Transfer ownership
return t.transferOwnership(stub, args)
}

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

// Query method is the interceptor of all invocation transactions, its job is to direct
// query transactions to intended APIs, and return the result back to callers
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
myLogger.Debugf("********************************Query****************************************")

// Handle different functions
if function == "getOwnerContactInformation" {
} else if function == "getOwnerContactInformation" {
return t.getOwnerContactInformation(stub, args)
} else if function == "getBalance" {
return t.getBalance(stub, args)
}

return nil, errors.New("Received unknown function query invocation with function " + function)
return nil, errors.New("Received unknown function invocation")
}

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,15 @@ func (t *AssetManagementChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]b
} else if function == "transfer" {
// Transfer ownership
return t.transfer(stub, args)
} else if function == "query" {
// Query asset
return t.query(stub, args)
}

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

// Query callback representing the query of a chaincode
func (t *AssetManagementChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting 'query' but found '" + function + "'")
}

func (t *AssetManagementChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var err error

if len(args) != 1 {
Expand Down
21 changes: 6 additions & 15 deletions examples/chaincode/go/attributes_to_state/attributes_to_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,17 @@ func (t *Attributes2State) Init(stub shim.ChaincodeStubInterface) ([]byte, error
return nil, nil
}

// Invoke takes two arguements, a key and value, and stores these in the state
func (t *Attributes2State) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function == "delete" {
return nil, t.delete(stub, args)
} else if function == "submit" {
return nil, t.setStateToAttributes(stub, args)
} else if function == "read" {
return t.query(stub, args)
}

if function != "submit" {
return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\"")
}
err := t.setStateToAttributes(stub, args)
if err != nil {
return nil, err
}
return nil, nil
return nil, errors.New("Invalid invoke function name. Expecting either \"delete\" or \"submit\" or \"read\"")
}

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

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

Expand Down
29 changes: 17 additions & 12 deletions examples/chaincode/go/authorizable_counter/authorizable_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func (t *AuthorizableCounterChaincode) Init(stub shim.ChaincodeStubInterface) ([
return nil, err
}

//Invoke Transaction makes increment counter
func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, _ := stub.GetFunctionAndParameters()
if function != "increment" {
return nil, errors.New("Invalid invoke function name. Expecting \"increment\"")
}
//Invoke makes increment counter
func (t *AuthorizableCounterChaincode) increment(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
val, err := stub.ReadCertAttribute("position")
fmt.Printf("Position => %v error %v \n", string(val), err)
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.
Expand All @@ -62,12 +58,7 @@ func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface)

}

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

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

// Invoke method is the interceptor of all invocation transactions, its job is to direct
// invocation transactions to intended APIs
func (t *AuthorizableCounterChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()

// Handle different functions
if function == "increment" {
return t.increment(stub, args)
} else if function == "read" {
return t.read(stub, args)
}
return nil, errors.New("Received unknown function invocation, Expecting \"increment\" \"read\"")
}

func main() {
err := shim.Start(new(AuthorizableCounterChaincode))
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions examples/chaincode/go/chaincode_example01/chaincode_example01.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
return nil, nil
}

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

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
return nil, nil
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function == "invoke" {
return t.invoke(stub, args)
}

return nil, errors.New("Invalid invoke function name. Expecting \"invoke\"")
}

func main() {
Expand Down
14 changes: 10 additions & 4 deletions examples/chaincode/go/chaincode_example02/chaincode_example02.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,24 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
return nil, nil
}

// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function == "delete" {
if function == "invoke" {
// Make payment of X units from A to B
return t.invoke(stub, args)
} else if function == "delete" {
// Deletes an entity from its state
return t.delete(stub, args)
}
if function == "query" {
} else if function == "query" {
// the old "Query" is now implemtned in invoke
return t.query(stub, args)
}

return nil, errors.New("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}

// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var A, B string // Entities
var Aval, Bval int // Asset holdings
var X int // Transaction value
Expand Down
14 changes: 7 additions & 7 deletions examples/chaincode/go/chaincode_example03/chaincode_example03.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)

// Invoke is a no-op
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
return nil, nil
}

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
if function == "query" {
return t.query(stub, args)
}

return nil, errors.New("Invalid invoke function name. Expecting \"query\"")
}

func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var A string // Entity
var Aval int // Asset holding
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {

func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
_, err := stub.MockInit("1", args)
bytes, err := scc.Query(stub)
bytes, err := scc.Invoke(stub)
if err != nil {
// expected failure
fmt.Println("Query below is expected to fail")
Expand Down
31 changes: 17 additions & 14 deletions examples/chaincode/go/chaincode_example04/chaincode_example04.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,18 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error)
}

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

function, args := stub.GetFunctionAndParameters()

if function == "query" {
return t.query(stub, args)
if len(args) != 3 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}

if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}

chainCodeToCall := function

event = args[0]
eventVal, err = strconv.Atoi(args[1])
chainCodeToCall := args[0]
event = args[1]
eventVal, err = strconv.Atoi(args[2])
if err != nil {
return nil, errors.New("Expected integer value for event state change")
}
Expand Down Expand Up @@ -106,7 +99,6 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, erro
return nil, nil
}

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

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
if function == "invoke" {
return t.invoke(stub, args)
} else if function == "query" {
return t.query(stub, args)
}

return nil, errors.New("Invalid invoke function name. Expecting \"invoke\" \"query\"")
}

func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func TestExample04_Invoke(t *testing.T) {
checkInit(t, stub, [][]byte{[]byte("init"), []byte("Event"), []byte("1")})

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

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

0 comments on commit 6ddbefe

Please sign in to comment.