Skip to content

blzhang/zhangbllib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

/* 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 ( "errors" "fmt" "strconv"

"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/util"

)

// This chaincode is a test for chaincode invoking another chaincode - invokes chaincode_example02

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

func (t *SimpleChaincode) GetChaincodeToCall() string { //This is the hashcode for github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02 //if the example is modifed this hashcode will change!! chainCodeToCall := "fbe49a7a9ed4fd8643ac6d08537c3833e29b2178664e7bec13c7d95125d44d4581e8bc51044e4222df045d91270996407c947d45435b5ab72063597757e93fb3" return chainCodeToCall }

// Init takes two arguements, a string and int. These are stored in the key/value pair in the state func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) { var event string // Indicates whether event has happened. Initially 0 var eventVal int // State of event var err error

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

// Initialize the chaincode
event = args[0]
eventVal, err = strconv.Atoi(args[1])
if err != nil {
	return nil, errors.New("Expecting integer value for event status")
}
fmt.Printf("eventVal = %d\n", eventVal)

err = stub.PutState(event, []byte(strconv.Itoa(eventVal)))
if err != nil {
	return nil, err
}

return nil, nil

}

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

event = args[0]
eventVal = args[1]
chainCodeToCall := args[2] // Get the chaincode to call from the ledger

if eventVal == "middleSchool" {
	amount = 2000
} else {
	amount = 5000
}

// 转账操作
invokeArgs := util.ToChaincodeArgs("transferAcc", args[3], args[4], strconv.Itoa(amount))
response, err := stub.InvokeChaincode(chainCodeToCall, invokeArgs)
if err != nil {
	errStr := fmt.Sprintf("Failed to invoke chaincode. Got error: %s", err.Error())
	fmt.Printf(errStr)
	return nil, errors.New(errStr)
}

fmt.Printf("Invoke chaincode successful. Got response %s", string(response))

// Write the event state back to the ledger
err = stub.PutState(event, []byte(args[1]))
if err != nil {
	return nil, err
}

return nil, nil

}

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

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

event = args[0]

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

if eventValbytes == nil {
	jsonResp := "{\"Error\":\"Nil value for " + event + "\"}"
	return nil, errors.New(jsonResp)
}

jsonResp := "{\"Name\":\"" + event + "\",\"Amount\":\"" + string(eventValbytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(jsonResp), nil

}

func main() { err := shim.Start(new(SimpleChaincode)) if err != nil { fmt.Printf("Error starting Simple chaincode: %s", err) } }

About

a test repository

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages