我尝试编写一个链码,这样当它在对等实例中执行时,它将数据上传到谷歌云存储桶。我要上传的文件实际上是以小文件块的形式存储在一个文件夹中,以便不同的对等方将不同的块上传到 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())
}
}
这是我在执行此链码 终端结果后得到的