无法从 hyperledger fabric 中的链代码实例将数据上传到 google 云存储

Can't upload data to google cloud storage from a chaincode instance in hyperledger fabric

我尝试编写一个链代码,以便在对等实例中执行时将数据上传到 google 云存储桶。我要上传的文件实际上以小文件块的形式存储在一个文件夹中,因此不同的对等方将不同的块上传到 GCS 存储桶。我正在使用 fabcar 蓝图开发此链代码,并使用测试网络脚本文件来执行链代码。我用来上传数据的函数在本地执行时运行良好,但是当我尝试在链代码中使用时,它显示

Error: endorsement failure during invoke. response: status:500 message:"error in simulation: failed to execute transaction 49a9b96088ff2f32906a6b6c9ba1f4ac0a530779bf8d506b176fcdfb8818afe2: error sending: chaincode stream terminated"

(我所做的可能听起来很疯狂,但我是这个超级账本结构的新手)

下面是我正在执行的代码示例(我认为这是 uploadGCS 或 InitLedger 函数的问题)(仅供参考:链代码执行仅运行 InitLedger 函数,当然它使用了 uploadGCS 函数)

package main

import (
    "fmt"
    "os"
    "io"
    "log"
    "strings"
    "encoding/json"
    "encoding/hex"
    "github.com/hyperledger/fabric-contract-api-go/contractapi"
    "path/filepath"
    "strconv"
    "crypto/sha256"
    "time"
    "context"
    "cloud.google.com/go/storage"
    "google.golang.org/api/option"
    "golang.org/x/oauth2/google"
)

type SmartContract struct {
    contractapi.Contract
}

type Data struct {
    Owner  string `json:"owner"`
    File string `json:"file"`
    FileChunkNumber string `json:"filechunknumber"`
    SHA256 string `json:"sha256"`
}

func uploadGCS(owner, filechunklocation, uploadlocation string) error {
    ct := context.Background()
    creds, err := google.FindDefaultCredentials(ct, storage.ScopeReadOnly)
    if err != nil {
            log.Fatal("GoT an err %s", err)
    }

    client, err := storage.NewClient(ct, option.WithCredentials(creds))
    if err != nil {
        return fmt.Errorf("storage.NewClient: %v", err)
    }
    defer client.Close()

    // Open local file.
    f, err := os.Open(filechunklocation)
    if err != nil {
        return fmt.Errorf("os.Open: %v", err)
    }
    defer f.Close()

    ct, cancel := context.WithTimeout(ct, time.Second*50)
    defer cancel()

    // Upload an object with storage.Writer.
    wc := client.Bucket("btp2016bcs0015-cloud-storage").Object(uploadlocation).NewWriter(ct)
    if _, err = io.Copy(wc, f); err != nil {
        return fmt.Errorf("io.Copy: %v", err)
    }
    if err := wc.Close(); err != nil {
        return fmt.Errorf("Writer.Close: %v", err)
    }
    return nil
}

func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
    filelocation := "/home/busyfriend/go/src/github.com/hyperledger/fabric-samples/test-network/samplefile---pdf"
    data := []Data{
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "1", SHA256: "eb73a20d61c1fb294b0eba4d35568d10c8ddbfe2544a3cacc959d640077673f5"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "2", SHA256: "92dd8ea8aa0da4a48a2cb45ae38f70f17526b6b50ef80c44367a56de6ec9abf9"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "3", SHA256: "b97027d261d01f86d1e514a52886add096ddc4e66d15d01e53516dd9d5cfb20b"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "4", SHA256: "377582f5e62dc3b34e40741f2d70d8f37a029856f75cbe68a6659328258e23a3"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "5", SHA256: "afb6c6d112d446ac07d78b13957bb440105038411095032de444bf08e3bbdba8"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "6", SHA256: "e43b885c2bfb47130c54fa70528fb2a91d9d1af1417a0f7c5a4c22d8f16efb01"},
    }

    for i := range data {
        _, dir := filepath.Split(filelocation)
        dir_1 := strings.Split(dir,"---")
        filechunk := dir_1[0]+"_"+ data[i].FileChunkNumber
        filechunklocation := filepath.Join(filelocation, filechunk)
        uploadlocation :=  data[i].Owner + "/" + dir + "/" + filechunk

        err := uploadGCS(data[i].Owner, filechunklocation, uploadlocation)

        if err != nil {
            return fmt.Errorf("Got an error %s", err.Error())
        }
    }

    for i, putdata := range data {
        dataAsBytes, _ := json.Marshal(putdata)
        err := ctx.GetStub().PutState("DATA"+strconv.Itoa(i), dataAsBytes)

        if err != nil {
            return fmt.Errorf("Failed to put to world state. %s", err.Error())
        }
    }

    return nil
}

// Uploads new data to the world state with given details
func (s *SmartContract) uploadData(ctx contractapi.TransactionContextInterface, dataID string, owner string, filelocation string, filechunknumber string) error {
    //Uploads the filechunk to the cloud storage
    _, dir := filepath.Split(filelocation)
    dir_1 := strings.Split(dir,"---")
    filechunk := dir_1[0]+"_"+ filechunknumber
    filechunklocation := filepath.Join(filelocation, filechunk)
    uploadlocation :=  owner + "/" + dir + "/" + filechunk
    err := uploadGCS(owner, filechunklocation, uploadlocation)
    if err != nil {
        fmt.Println(err.Error())
        return err
    }

    //Creates SHA256 hash of the file chunk
    f, err := os.Open(filechunklocation)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    h := sha256.New()
    if _, err := io.Copy(h, f); err != nil {
        log.Fatal(err)
    }

    data := Data{
        Owner: owner,
        File: dir_1[0]+"."+dir_1[1],
        FileChunkNumber: filechunknumber,
        SHA256: hex.EncodeToString(h.Sum(nil)),
    }

    dataAsBytes, _ := json.Marshal(data)

    return ctx.GetStub().PutState(dataID, dataAsBytes)
}


func main() {

    chaincode, err := contractapi.NewChaincode(new(SmartContract))

    if err != nil {
        fmt.Printf("Error create cloud chaincode: %s", err.Error())
        return
    }

    if err := chaincode.Start(); err != nil {
        fmt.Printf("Error starting cloud chaincode: %s", err.Error())
    }
}

这是我执行这个链码后得到的东西 terminal result

你能检查链代码容器的日志吗?您可以找到创建的额外容器,其中包含您的链代码的名称和版本。如果您想查看链代码生成的日志,则需要查看那些容器(docker logs <container>)。

在您的代码中,您正在记录一些致命错误。与其记录并继续方法,不如 return 错误并失败。

请注意,根据背书策略(即AND(Org1.member, Org2.member)),一个调用请求可以在多个节点上执行。所有这些执行都必须 return 相同的结果和 put/get 完全 相同的数据 to/from 分类帐。这是一种确保具有相同链码 运行 的不同组织之间的信任的设计。 (即,如果您的不同同行在放入分类帐之前将同一对象转换为具有不同属性顺序的字符串,它将失败。)

我的意见是,如果要将 I/O 操作分开,那么只需将哈希值放入分类帐。您的方法与 链外 数据概念有一些共同点。复议前请看清楚。