我已经使用hyperledger-fabric samples/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时得到的错误:
>Error: Error endorsing chaincode: rpc error: code = Unknown desc = Error starting container: Failed to generate platform-specific docker build: Error returned from build: 2 "# github.com/fabcar
chaincode/input/src/github.com/fabcar/fabcar.go:132: unknown Car field 'Make' in struct literal
chaincode/input/src/github.com/fabcar/fabcar.go:132: unknown Car field 'Model' in struct literal
chaincode/input/src/github.com/fabcar/fabcar.go:132: unknown Car field 'Colour' in struct literal
chaincode/input/src/github.com/fabcar/fabcar.go:132: unknown Car field 'Owner' in struct literal
chaincode/input/src/github.com/fabcar/fabcar.go:193: car.Owner undefined (type Car has no field or method Owner)
chaincode/input/src/github.com/fabcar/fabcar.go:211: car.Colour undefined (type Car has no field or method Colour)
"我正在使用Mac OSX yosemite,我已经尝试通过删除所有数据并重新启动来清理docker,我还尝试重命名go文件,但都不起作用。我在这里做错了什么?
go build命令的输出:
>go build
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:74:8: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:77:8: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/chaincode.pb.go:9:8: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/init.go:21:2: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/bccsp/pkcs11/conf.go:25:2: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:80:2: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/protos/peer/admin.pb.go:81:2: use of vendored package not allowed
../../../../../go/src/github.com/hyperledger/fabric/common/flogging/grpclogger.go:21:2: use of vendored package not allowed发布于 2017-08-15 04:41:27
您似乎遇到了编译错误,而您将Car的结构改为
type Car struct {
Name string `json:"Name"`
College string `json:"College"`
DOB string `json:"DOB"`
SecretCode string `json:"SecretCode"`
}虽然许多保留在fabcar.go中的函数仍在尝试使用这种结构“老式”方式,但假设以前的模型:
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结构的声明,只需创建自己的结构并正确使用即可:
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)
}发布于 2019-07-09 12:14:56
好了,检查这个链接fabcar.go chaincode of hyperledger does not accept the changes and modification and always run previous chaincode,你会在那里找到答案,当你与源代码交互时,不要忘记删除它以前使用过的模块,以完全暗示你的代码更改!
https://stackoverflow.com/questions/45682089
复制相似问题