基于hyperledger-fabric 的helloworld版智能合约
package main
import (
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type SimpleChaincode struct {
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
_, args := stub.GetFunctionAndParameters()
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
err := stub.PutState("hello_world", []byte(args[0]))
if err != nil {
return nil, err
}
return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
fmt.Println("invoke is running " + function)
if function == "wirte" {
return t.write(stub, args)
}
fmt.Println("invoke did not find function: " + function)
return nil, errors.New("Received unknown function invocation: " + function)
}
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface) ([]byte, error) {
function, args := stub.GetFunctionAndParameters()
fmt.Println("query is running " + function)
if function == "read" {
return t.read(stub, args)
}
fmt.Println("query did not find func: " + function)
return nil, errors.New("Received unknown function query: " + function)
}
func (t *SimpleChaincode) write(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, value string
var err error
fmt.Println("runing wirte()")
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2. name of the key and value to set")
}
key = args[0]
value = args[1]
err = stub.PutState(key, []byte(value))
if err != nil {
return nil, err
}
return nil, nil
}
func (t *SimpleChaincode) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, jsonResp string
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
}
key = args[0]
valAsbytes, err := stub.GetState(key)
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}"
return nil, errors.New(jsonResp)
}
return valAsbytes, nil
}
old version:
package main
import (
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type SimpleChaincode struct {
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface,function string,args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
err := stub.PutState("hello_world", []byte(args[0]))
if err != nil {
return nil, err
}
return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface,function string,args []string) ([]byte, error) {
fmt.Println("invoke is running " + function)
if function == "init" {
return t.Init(stub,"init",args)
} else if function == "wirte" {
return t.write(stub, args)
}
fmt.Println("invoke did not find function: " + function)
return nil, errors.New("Received unknown function invocation: " + function)
}
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface,function string,args []string) ([]byte, error) {
fmt.Println("query is running " + function)
if function == "read" {
return t.read(stub, args)
}
fmt.Println("query did not find func: " + function)
return nil, errors.New("Received unknown function query: " + function)
}
func (t *SimpleChaincode) write(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, value string
var err error
fmt.Println("runing wirte()")
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2. name of the key and value to set")
}
key = args[0]
value = args[1]
err = stub.PutState(key, []byte(value))
if err != nil {
return nil, err
}
return nil, nil
}
func (t *SimpleChaincode) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, jsonResp string
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
}
key = args[0]
valAsbytes, err := stub.GetState(key)
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}"
return nil, errors.New(jsonResp)
}
return valAsbytes, nil
}
Secure Enrollment
{
"enrollId": "<YOUR_USER_HERE>",
"enrollSecret": "<YOUR_SECRET_HERE>"
}
Deploying the chaincode
{
"jsonrpc": "2.0",
"method": "deploy",
"params": {
"type": 1,
"chaincodeID": {
"path": "<YOUR_CHAINCODE_PATH_HERE>"
},
"ctorMsg": {
"function": "init",
"args": [
"hi there"
]
},
"secureContext": "<YOUR_USER_HERE>"
},
"id": 1
}
Query
{
"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID": {
"name": "<CHAINCODE_HASH_HERE>"
},
"ctorMsg": {
"function": "read",
"args": [
"hello_world"
]
},
"secureContext": "<YOUR_USER_HERE>"
},
"id": 2
}
Invoke
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID": {
"name": "<CHAINCODE_HASH_HERE>"
},
"ctorMsg": {
"function": "write",
"args": [
"hello_world", "go away"
]
},
"secureContext": "<YOUR_USER_HERE>"
},
"id": 3
}
exemples:
{
"enrollId":"lukas",
"enrollSecret":"NPKYL39uKbkj"
}
{
"jsonrpc":"2.0",
"method": "deploy",
"params": {
"type": 1,
"chaincodeID":{
"path": "blockchain/chaincode/helloworld"
},
"ctorMsg": {
"function": "init",
"args":["chaincode"]
},
"secureContext": "lukas"
},
"id": 1
}
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID":{
"name":"43c91fa4e6047dbca582e5e6c6c3263833530d49ef22e0b1468b937b6d7cd724c903dc9a3b2ca3a3566a14ae5e77711effb55e60dd942f143bcfdca4113284a8"
},
"ctorMsg": {
"function":"wirte",
"args":["hi","pingan"]
},
"secureContext": "lukas"
},
"id": 1
}
{
"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID":{
"name":"43c91fa4e6047dbca582e5e6c6c3263833530d49ef22e0b1468b937b6d7cd724c903dc9a3b2ca3a3566a14ae5e77711effb55e60dd942f143bcfdca4113284a8"
},
"ctorMsg": {
"function":"read",
"args":["hi"]
},
"secureContext": "lukas"
},
"id": 5
}