改变fabcar示例hyperledger-fabric

时间:2017-08-14 19:54:53

标签: hyperledger hyperledger-fabric

我一直在使用hyperledger-fabric样本/ fabcar示例一段时间,我可以在fabcar.go中添加更多字段和功能,但是当我完全更改字段时。我在部署中遇到错误。

以下是我在fabcar.go中所做的更改的一个示例。

type Car struct {
Name  string `json:"Name"`
College  string `json:"College"`
DOB  string `json:"DOB"`
SecretCode  string `json:"SecretCode"`
    }

func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
cars := []Car{
    Car{Name: "name1", College: "college1", DOB: "dob1", SecretCode: "secretcode1"},
    Car{Name: "name2", College: "college2", DOB: "dob2", SecretCode: "secretcode2"},
    Car{Name: "name3", College: "college3", DOB: "dob3", SecretCode: "secretcode3"},
    Car{Name: "name4", College: "college4", DOB: "dob4", SecretCode: "secretcode4"},
    }
i := 0
for i < len(cars) {
    fmt.Println("i is ", i)
    carAsBytes, _ := json.Marshal(cars[i])
    APIstub.PutState("CAR"+strconv.Itoa(i), carAsBytes)
    fmt.Println("Added", cars[i])
    i = i + 1
}

return shim.Success(nil)
}

以下是运行startFabric.sh

时出现的错误
  

错误:错误支持链码:rpc错误:code = Unknown desc =启动容器时出错:无法生成特定于平台的docker build:从build返回错误:2&#34;#github.com/fabcar   chaincode / input / src / github.com / fabcar / fabcar.go:132:notity car field&#39; Make&#39;在struct literal中   chaincode / input / src / github.com / fabcar / fabcar.go:132:notity car field&#39; Model&#39;在struct literal中   chaincode / input / src / github.com / fabcar / fabcar.go:132:未知汽车领域&#39;颜色&#39;在struct literal中   chaincode / input / src / github.com / fabcar / fabcar.go:132:未知汽车领域&#39;所有者&#39;在struct literal中   chaincode / input / src / github.com / fabcar / fabcar.go:193:car.Owner undefined(类型Car没有字段或方法所有者)   chaincode / input / src / github.com / fabcar / fabcar.go:211:car.Colour undefined(类型Car没有字段或方法颜色)   &#34;

我正在使用Mac OSX优胜美地,我尝试通过删除所有数据并再次启动来清理docker,我也尝试重命名go文件,但没有任何作用。我在这里做错了什么?

go build命令的输出:

  

去构建   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:74:8:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:77:8:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/chaincode.pb.go:9:8:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/init.go:21:2:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/bccsp/pkcs11/conf.go:25:2:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:80:2:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:81:2:不允许使用vendored package   ../../../../../go/src/github.com/hyperledger/fabric/common/flogging/grpclogger.go:21:2:不允许使用vendored package

2 个答案:

答案 0 :(得分:1)

当您将Car的结构更改为

时,您似乎遇到了编译错误
type Car struct {
     Name        string `json:"Name"`
     College     string `json:"College"`
     DOB         string `json:"DOB"`
     SecretCode  string `json:"SecretCode"`
}

虽然fabcar.go中仍有许多功能仍在尝试使用此结构&#34;旧时尚&#34;假设以前的型号:

type Car struct {
        Make   string `json:"make"`
        Model  string `json:"model"`
        Colour string `json:"colour"`
        Owner  string `json:"owner"`
}

例如:

func (s *SmartContract) createCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

        if len(args) != 5 {
                return shim.Error("Incorrect number of arguments. Expecting 5")
        }

        var car = Car{Make: args[1], Model: args[2], Colour: args[3], Owner: args[4]}

        carAsBytes, _ := json.Marshal(car)
        APIstub.PutState(args[0], carAsBytes)

        return shim.Success(nil)
}

我建议重新使用Car struct的声明,只需创建自己的并正确使用它:

type Student struct {
     Name        string `json:"Name"`
     College     string `json:"College"`
     DOB         string `json:"DOB"`
     SecretCode  string `json:"SecretCode"`
}

func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
   cars := []Student{
    Student{Name: "name1", College: "college1", DOB: "dob1", SecretCode: "secretcode1"},
    Student{Name: "name2", College: "college2", DOB: "dob2", SecretCode: "secretcode2"},
    Student{Name: "name3", College: "college3", DOB: "dob3", SecretCode: "secretcode3"},
    Student{Name: "name4", College: "college4", DOB: "dob4", SecretCode: "secretcode4"},
    }
   i := 0
   for i < len(cars) {
      fmt.Println("i is ", i)
      carAsBytes, _ := json.Marshal(cars[i])
      APIstub.PutState("CAR"+strconv.Itoa(i), carAsBytes)
      fmt.Println("Added", cars[i])
      i = i + 1
   } 

   return shim.Success(nil)
}

答案 1 :(得分:0)

好吧,请检查此链接fabcar.go chaincode of hyperledger does not accept the changes and modification and always run previous chaincode,当您与源代码进行交互时,您会在这里找到答案,不要忘记删除其先前使用的模块以完全暗示您的代码已更改!