chaincode-hellworld

基于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
}

















  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值