码迷,mamicode.com
首页 > 其他好文 > 详细

HyperLeger Fabric2.2简单链码编写

时间:2021-03-02 12:05:13      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:code   copy   simple   %s   nil   abr   ini   bsp   ror   

1、copyright.go 文件

package main

import (
    "fmt"

    "github.com/hyperledger/fabric-chaincode-go/shim"
    "github.com/hyperledger/fabric-protos-go/peer"
)

type CopyrightAsset struct {
}

func (t *CopyrightAsset) Init(stub shim.ChaincodeStubInterface) peer.Response {
    stub.GetStringArgs()
    return shim.Success(nil)
}

func (t *CopyrightAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
    fn, args := stub.GetFunctionAndParameters()

    var result string
    var err error
    if fn == "set" {
        result, err = set(stub, args)
    } else if fn == "del" {
        result, err = del(stub, args)
    } else { // assume ‘get‘ even if fn is nil
        result, err = get(stub, args)
    }
    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success([]byte(result))
}

func set(stub shim.ChaincodeStubInterface, args []string) (string, error) {
    if len(args) != 2 {
        return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value")
    }

    err := stub.PutState(args[0], []byte(args[1]))
    if err != nil {
        return "", fmt.Errorf("Failed to set asset: %s", args[0])
    }
    return args[1], nil
}

func get(stub shim.ChaincodeStubInterface, args []string) (string, error) {
    if len(args) != 1 {
        return "", fmt.Errorf("Incorrect arguments. Expecting a key")
    }

    value, err := stub.GetState(args[0])
    if err != nil {
        return "", fmt.Errorf("Failed to get asset: %s with error: %s", args[0], err)
    }
    if value == nil {
        return "", fmt.Errorf("Asset not found: %s", args[0])
    }
    return string(value), nil
}

func del(stub shim.ChaincodeStubInterface, args []string) (string, error) {
    if len(args) != 1 {
        return "", fmt.Errorf("Incorrect arguments. Expecting a key")
    }

    err := stub.DelState(args[0])
    if err != nil {
        return "", fmt.Errorf("Failed to get asset: %s with error: %s", args[0], err)
    }

    return "", nil
}

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

 

2、copyright_test.go 文件

package main

import (
    "fmt"
    "github.com/hyperledger/fabric-chaincode-go/shimtest"
    "testing"
)

func TestInit(t *testing.T) {
    cc := new(CopyrightAsset)                     // 创建Chaincode对象
    stub := shimtest.NewMockStub("copyright", cc) // 创建MockStub对象
    // 调用Init接口,传参
    stub.MockInit("1", [][]byte{[]byte("hash")})
    // 调用Init接口,不传参
    stub.MockInit("2", [][]byte{[]byte("")})
}

func TestParams(t *testing.T) {
    cc := new(CopyrightAsset)                     // 创建Chaincode对象
    stub := shimtest.NewMockStub("copyright", cc) // 创建MockStub对象
    // 调用set接口设置a为100
    stub.MockInvoke(
        "3",
        [][]byte{
            []byte("set"),
            []byte("a"),
            []byte("100"),
        })
    // 再次查询a的值
    res := stub.MockInvoke(
        "3",
        [][]byte{
            []byte("get"),
            []byte("a"),
        })

    fmt.Println("The new value of a is ", string(res.Payload))
}

func TestDel(t *testing.T)  {
    cc := new(CopyrightAsset)                     // 创建Chaincode对象
    stub := shimtest.NewMockStub("copyright", cc) // 创建MockStub对象
    stub.MockInvoke(
        "3",
        [][]byte{
            []byte("del"),
            []byte("a"),
        })
}

 

HyperLeger Fabric2.2简单链码编写

标签:code   copy   simple   %s   nil   abr   ini   bsp   ror   

原文地址:https://www.cnblogs.com/bpsh/p/14463788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!