douding_1073 2019-03-29 01:55
浏览 154

为什么putstate在Go Chaincode中的Init外部不起作用

I am writing my own chaincode following the marble example. However, putstate is not working outside the Init function. GetState is working fine. I can getstate of those things I putstate in the Init function. If I try to call my custom function that try to putstate another new record, it return success, but I cannot read the record. Even I copy the chaincode_example02.go functions into my chaincode, it still doesn't work. Then I discover that chaincode_example02.go has the same problem in my network. Can someone help me? Thanks

package main
import (
    "encoding/json"
    "fmt"
    "strconv"
    // "github.com/hyperledger/fabric/discovery/support/chaincode"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
)

type SmartContract struct {
}

func (t *SmartContract) Init(stub shim.ChaincodeStubInterface) pb.Response {

    var Aval, Bval int // Asset holdings

    // Initialize the chaincode
    Aval, err = strconv.Atoi("10")
    Bval, err = strconv.Atoi("20")
    fmt.Printf("Aval = %d, Bval = %d
", Aval, Bval)

    // Write the state to the ledger
    err = stub.PutState("A", []byte(strconv.Itoa(Aval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    err = stub.PutState("B", []byte(strconv.Itoa(Bval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success(nil)
}

func (t *SmartContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    fnc, args := stub.GetFunctionAndParameters()
    fmt.Println(" ")
    fmt.Println("Invoke, with function - " + fnc)

    if fnc == "init" {
        return t.Init(stub)
    } else if fnc == "invoke" {
        return t.invoke(stub)
    } else if fnc == "query" {
        return t.query(stub, args)
    }
    return shim.Error(fmt.Sprintf("Invalid function name: %s", fnc))
}

func (t *SmartContract) invoke(stub shim.ChaincodeStubInterface) pb.Response {
    var A, B string    // Entities
    var Aval, Bval int // Asset holdings
    var X int          // Transaction value
    var err error

    A = "A"
    B = "B"
    X = 5

    Avalbytes, err := stub.GetState(A)
    if err != nil {
        return shim.Error("Failed to get state")
    }
    if Avalbytes == nil {
        return shim.Error("Entity not found")
    }
    Aval, _ = strconv.Atoi(string(Avalbytes))

    Bvalbytes, err := stub.GetState(B)
    if err != nil {
        return shim.Error("Failed to get state")
    }
    if Bvalbytes == nil {
        return shim.Error("Entity not found")
    }
    Bval, _ = strconv.Atoi(string(Bvalbytes))

    // Perform the execution
    Aval = Aval - X
    Bval = Bval + X

    err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success(nil)
}

func (t *SmartContract) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    var A string // Entities
    var err error

    A = args[0]

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

    if Avalbytes == nil {
        jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
        return shim.Error(jsonResp)
    }

    jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
    return shim.Success(Avalbytes)
}

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

  • 写回答

1条回答 默认 最新

  • xxwzlxz 2020-06-16 17:56
    关注

    应该是没有使用没有使用submit transaction

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大