2017-04-18 20 views
0
package main 

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

var matchLogger = shim.NewLogger("Helper") 

type PlaceHolder struct { 
    ValA string `json:"ValA"` 
    ValB string `json:"ValB"` 
    Match bool `json:"Match"` 
} 

type Agreement struct { 
    TradeNumber   PlaceHolder `json:"TradeNumber"` 
    BuyerName   PlaceHolder `json:"BuyerName"` 
    SellerName   PlaceHolder `json:"SellerName"` 
    BuyerID    PlaceHolder `json:"BuyerID"` 
    SellerID   PlaceHolder `json:"SellerID"` 
} 

type ContractContaineer struct { 
    FirmID  string `json:"FirmID"` 
    ContractList []string `json:"contractList"` 
} 


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

    xx, err1 := stub.GetCallerCertificate() 
    if err1 != nil { 
     matchLogger.Info(err1) 
    } 
    matchLogger.Info("Cert ----") 
    matchLogger.Info(string(xx)) 
    matchLogger.Info("----") 

    xy, err3 := stub.GetCallerMetadata() 
    if err3 != nil { 
     matchLogger.Info(err3) 
    } 
    matchLogger.Info("Meta ----") 
    matchLogger.Info(string(xy)) 
    matchLogger.Info("----") 

    yy, err4 := stub.GetPayload() 
    if err4 != nil { 
     matchLogger.Info(err4) 
    } 
    matchLogger.Info("PLD ----") 
    matchLogger.Info(string(yy)) 
    matchLogger.Info("----") 


    return shim.Success(nil) 
} 


func (t *Agreement) Query(stub shim.ChaincodeStubInterface) pb.Response { 
     return shim.Success(nil) 
} 


// Transaction makes payment of X units from A to B 
func (t *Agreement) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 

    xx, err1 := stub.GetCallerCertificate() 
    if err1 != nil { 
     matchLogger.Info(err1) 
    } 
    matchLogger.Info("Cert ----") 
    matchLogger.Info(string(xx)) 
    matchLogger.Info("----") 

    xy, err3 := stub.GetCallerMetadata() 
    if err3 != nil { 
     matchLogger.Info(err3) 
    } 
    matchLogger.Info("Meta ----") 
    matchLogger.Info(string(xy)) 
    matchLogger.Info("----") 

    yy, err4 := stub.GetPayload() 
    if err4 != nil { 
     matchLogger.Info(err4) 
    } 
    matchLogger.Info("PLD ----") 
    matchLogger.Info(string(yy)) 
    matchLogger.Info("----") 

    return shim.Success(nil) 
} 


func main() { 

    lld, _ := shim.LogLevel("INFO") 
    matchLogger.Info(lld) 
    matchLogger.SetLevel(lld) 
    matchLogger.Info(matchLogger.IsEnabledFor(lld)) 
    err := shim.Start(new(Agreement)) 
    if err != nil { 
     fmt.Printf("Error starting Simple chaincode: %s", err) 
    } 
} 

這是我的完整GO文件。請提出我做錯了什麼。記錄器輸出對於xx,xy,yy來說是空白的。錯誤也是零。我已經實現了所有3種方法,即init,invoke和query。代碼編譯得很好stub.GetCallerCertificate(),stub.GetCallerMetadata(),stub.GetPayload()返回空白

+0

第一關閉你忽略錯誤...檢查錯誤並確保沒有其他問題發生。 –

+0

雖然取決於你使用的那個'stub'的實現可能總是'nil' https://github.com/hyperledger-archives/fabric/blob/master/core/chaincode/shim/chaincode.go# L628 –

回答

0

如果您使用的是https://github.com/hyperledger-archives/fabric/blob/master/core/chaincode/shim/chaincode.go#L628我懷疑shim.ChainCodeStubInterface在傳入此方法之前未正確初始化。

ChainCodeStub每個實現需要滿足this interface

type Chaincode interface { 
    // Init is called during Deploy transaction after the container has been 
    // established, allowing the chaincode to initialize its internal data 
    Init(stub *ChaincodeStub, function string, args []string) ([]byte, error) 

    // Invoke is called for every Invoke transactions. The chaincode may change 
    // its state variables 
    Invoke(stub *ChaincodeStub, function string, args []string) ([]byte, error) 

    // Query is called for Query transactions. The chaincode may only read 
    // (but not modify) its state variables and return the result 
    Query(stub *ChaincodeStub, function string, args []string) ([]byte, error) 
} 

而且Init方法是我希望見到的初始化的存根,以便你的價值沒有得到返回空

+0

嗨,約翰,我發佈了我的整個GO文件。請提出一些解決方案。 – Manoo