报错:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x488c7d]
goroutine 29 [running]:
panic(0xa0cb60, 0xc420016040)
/opt/go/src/runtime/panic.go:500 +0x1a1
github.com/hyperledger/fabric-samples/chaincode/cpabe/vendor/github.com/fentec-project/gofe/abe.(*FAME).Encrypt(0xc4201321c8, 0xc4204fc170, 0xf, 0xc420135540, 0x0, 0x0, 0x0, 0x0)
/chaincode/input/src/github.com/hyperledger/fabric-samples/chaincode/cpabe/vendor/github.com/fentec-project/gofe/abe/fame.go:134 +0x44d
main.(*SmartContract).Encryption(0xfcc8a0, 0xf93c40, 0xc420078080, 0xc4201b8520, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, ...)
/chaincode/input/src/github.com/hyperledger/fabric-samples/chaincode/cpabe/cpabe.go:172 +0x92a
main.(*SmartContract).Invoke(0xfcc8a0, 0xf93c40, 0xc420078080, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/chaincode/input/src/github.com/hyperledger/fabric-samples/chaincode/cpabe/cpabe.go:67 +0x208
github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction.func1(0xc4201482a0, 0xc420246410)
/opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:317 +0x483
created by github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction
/opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:328 +0x49
解决方案
找到line134前面是一个空值判断
打印函数传递的参数发现确实有空值,给参数正确赋值就能解决。可见,这个bug的原因应该就是函数调用时传了一个空值。
一个需要注意的坑是go语言中,json无法访问我们的结构体里小写字母开头的成员,所以用json.Marshal()和json.Unmarshal()时很可能得到一个空值。