doutangu4978 2017-01-23 10:00
浏览 61
已采纳

Chaincode函数显示结构值

I'm trying to write a simple chaincode that uses a structure to store customer details. I have one setDetails func that works fine. I wish to write another getDetails func that takes UID as arguement and prints the details of the customer with that UID. Need Help!

package main

import (
    "errors"
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
)

type Customer struct {
    UID     string
    Name    string
    Address struct {
        StreetNo string
        Country  string
    }
}

type SimpleChaincode struct {
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
    fmt.Printf("initialization done!!!")
    fmt.Printf("initialization done!!!")

    return nil, nil
}

func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    if len(args) < 3 {
        return nil, errors.New("insert Into Table failed. Must include 3 column values")
    }

    customer := &Customer{}
    customer.UID = args[0]
    customer.Name = args[1]
    customer.Address.Country = args[2]

    return nil, nil
}

func (t *SimpleChaincode) getDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    //wish to print all details of an particular customer corresponding to the UID
    return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
    function, args := stub.GetFunctionAndParameters()
    fmt.Printf("Inside Invoke %s", function)
    if function == "setDetails" {
        return t.setDetails(stub, args)

    } else if function == "getDetails" {
        return t.getDetails(stub, args)
    }

    return nil, errors.New("Invalid invoke function name. Expecting  \"query\"")
}

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

1条回答 默认 最新

  • doubaoxue5788 2017-01-23 11:15
    关注

    I didn't know about Hyperledger until now, but after a look at the github documentation, I get you would use stub.PutState to store your customer information, then later use stub.GetState to get it back.

    As both methods request a byte slice, my guess would be something along these lines:

    func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
    
        if len(args) < 3 {
            return nil, errors.New("insert Into Table failed. Must include 3 column values")
        }
    
        customer := &Customer{}
        customer.UID = args[0]
        customer.Name = args[1]
        customer.Address.Country = args[2]
    
        raw, err := json.Marshal(customer)
        if err != nil {
            return nil, err
        }
    
        err := stub.PuState(customer.UID, raw)
        if err != nil {
            return nil, err
        }
    
        return nil, nil
    }
    
    func (t *SimpleChaincode) getDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
    
        if len(args) != 1 {
            return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
        }
    
        return stub.GetState(args[0])
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看