Skip to content

Add testing for Golang Chaincode #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

debayangg
Copy link
Contributor

@debayangg debayangg commented May 2, 2025

Addresses the Issue #99 .

@debayangg
Copy link
Contributor Author

debayangg commented May 2, 2025

@kalio007 @dzikowski the e2e tests are failing due to test snapshots not matching which is due to using of golang in the the samples yaml file. It can be fixed by creating a new file or updating the snapshots but the main problem is that test-02-raft was was also failing when I checked it personally.

It seems to a docker problem, on my system I got :

Error: chaincode install failed with status: 500 - failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: docker build failed: docker image build failed: docker build failed: Error returned from build: 2 "google.golang.org/protobuf/internal/flags

A simple golang chaincode for setting and getting a key with 1 org, 1 orderer and 1 channel was working fine without errors.

Also regarding chaincode-kv-go, I mainly just followed the implementation of kv-node.

@debayangg debayangg marked this pull request as draft May 3, 2025 15:16
@debayangg
Copy link
Contributor Author

This is the result of test-02-raft in my repo, https://github.com/debayangg/fablo/actions/runs/14825376358/job/41617875740.
The error is again the same,

Error: chaincode install failed with status: 500 - failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: docker build failed: docker image build failed: docker build failed: Error returned from build: 2 "go: downloading github.com/hyperledger/fabric-contract-api-go v1.2.2

@dzikowski
Copy link
Contributor

dzikowski commented May 6, 2025

Yes, if I read action logs properly, it seems it requires Go in version 1.19 or higher. The chaincode is executed in a docker container, which uses hyperledger/fabric-ccenv image: https://hub.docker.com/r/hyperledger/fabric-ccenv. Fablo uses the same version of the image as the version of Hyperledger Fabric, which is in this case 2.4.3.

the output of:

docker run -it hyperledger/fabric-ccenv:2.4.3 go version

is:

go version go1.17.5

So I think the solution is to update the Fabric version in relevant fablo config file. You may choose for instance the newest 2.5.*

@dzikowski
Copy link
Contributor

I've read also your comment on failing Java chaincode for the newest version: #99 (comment)

Isn't it just a matter of upgrading Java library version?

Do you think Java library upgrade should be a separate issue?

@debayangg
Copy link
Contributor Author

No I dont think so, I will update the jdk version and try again. I had an exam today so didn't get time till now. Overall it all seems to be a problem of incompatible versions.

@debayangg
Copy link
Contributor Author

I went ahead and made the changes in the java fabric sdk, tried out different versions and also tried versions used in chaincode samples provided in the fabric samples, it seems that Java chaincode will also need to be updated. It will take some time on my part to read through Java chaincode and refactor it as simple automated refactors are not working.

@dzikowski
Copy link
Contributor

Perfect, thanks!

@joaormatos
Copy link

Yes, if I read action logs properly, it seems it requires Go in version 1.19 or higher. The chaincode is executed in a docker container, which uses hyperledger/fabric-ccenv image: https://hub.docker.com/r/hyperledger/fabric-ccenv. Fablo uses the same version of the image as the version of Hyperledger Fabric, which is in this case 2.4.3.

the output of:

docker run -it hyperledger/fabric-ccenv:2.4.3 go version

is:

go version go1.17.5

So I think the solution is to update the Fabric version in relevant fablo config file. You may choose for instance the newest 2.5.*

I suggest doing the opposite: modify the sample chaincode so that it's compatible with older versions of Go.
Specifically, ensure that the sample chaincode targets the oldest version of Go that's still used by some version of Fabric, among the versions of Fabric that Fablo intends to support.

A Go module declares its target version in go.mod using the go directive; the current sample is declaring Go 1.21 as its target Go version, as seen here

You can try updating the target version like this:

go mod tidy -go=1.17

Another thing: when packaging Go chaincode, go mod vendor should be executed prior to the packaging itself, to ensure the chaincode package includes all of its dependencies.
It's analogous to what happens when you run npm install and npm populates node_modules with the project dependencies.

References:
https://go.dev/ref/mod#go-mod-file-go
https://go.dev/ref/mod#go-mod-tidy
https://go.dev/ref/mod#vendoring

Note: Because the go directive was only advisory until Go 1.21, older versions like 1.17.x will still try and some times succeed in building chaincode packages that target newer versions, provided that the code in the module and its dependencies is effectively compatible with Go 1.17. That sounds like quite the potential source of confusion, so it might be helpful to warn users in such situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants