首页 > 解决方案 > 无法从超级账本结构中的链码实例将数据上传到谷歌云存储

问题描述

我尝试编写一个链码,这样当它在对等实例中执行时,它将数据上传到谷歌云存储桶。我要上传的文件实际上是以小文件块的形式存储在一个文件夹中,以便不同的对等方将不同的块上传到 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 函数的问题)(仅供参考:chaincode 执行仅运行 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())
    }
}

这是我在执行此链码 终端结果后得到的

标签: gogoogle-cloud-storagehyperledger-fabricchaincode

解决方案


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

在您的代码中,您正在使用 Fatal 记录一些错误。而不是 log & continue 方法,最好返回错误并失败。

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

我的意见是,如果要保持 I/O 操作分开,那么只需将散列放入分类帐。你的方法与链下数据概念有一些共同点。请在重新考虑之前查看它。


推荐阅读