Endorsers Instantiate Chaincode with error "cannot find package" - go

I created a chaincode and I imported a package to it.
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"golang.org/x/crypto/bcrypt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer")
I can install that chaincode to all endorsers. But when I instantiated it to all endorsers, it faced the error:
endorser failed with error starting container: error starting container: Failed to generate platform-specific docker build: Error returned from build: 1 "/chaincode/input/src/github.com/marbles02/marbles_chaincode.go:85:2: cannot find package "golang.org/x/crypto/bcrypt" in any of:
/usr/local/go/src/golang.org/x/crypto/bcrypt (from $GOROOT)
/chaincode/input/src/golang.org/x/crypto/bcrypt (from $GOPATH)
/go/src/golang.org/x/crypto/bcrypt
I tried to copy golang.org/x/crypto/bcrypt package to the /usr/local/go/src/ in the root directory, but it has the same error.

Try Installing the dependencies in the chaincode directory using below commands:
go mod init will create go.mod and go.sum files.
go mod vendor for creating a vendor directory.
Also if using fabric:v2.X.X then shim and peer packages moved to different libraries.
so change the peer and shim packages to below in the chaincode.go file.
peer --> github.com/hyperledger/fabric-protos-go/peer.
shim --> github.com/hyperledger/fabric-chaincode-go/shim
Make sure to add peerand shim changes before running go mod initcommand OR if you are already have a vendor directory then try go mod tidy and then go mod vendor commands to update the packages.

Related

Why can't Heroku build my app with dependencies?

I have been working on a Go project and deploying it successfully with Heroku up until I added more packages. My Heroku app is set up to deploy the main branch of my github repo, but it fails to build the app each time I push. This is the build log:
-----> Using buildpack: heroku/go
-----> Go app detected
-----> Fetching stdlib.sh.v8... done
----->
Detected go modules via go.mod
----->
Detected Module Name: my-heroku-app
----->
-----> New Go Version, clearing old cache
-----> Installing go1.17.3
-----> Fetching go1.17.3.linux-amd64.tar.gz... done
-----> Determining packages to install
Detected the following main packages to install:
my-heroku-app
-----> Running: go install -v -tags heroku -mod=vendor my-heroku-app
webapp.go:6:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/go-sql-driver/mysql
webapp.go:7:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/context
webapp.go:8:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/sessions
webapp.go:9:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/golang.org/x/crypto/bcrypt
! Push rejected, failed to compile Go app.
! Push failed
Here is what my import statement looks like
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
"html/template"
"log"
"net/http"
"time"
"unicode"
)
I have tried reading through the guides and documentation about govendor both at Heroku and on their github page, and as far as I can tell, I have done my setup correctly. I am currently using govendor and I have a vendor folder containing a json file in my repo that has the info for the dependencies. Everything works when I run it locally
update: I made a fresh repo with minimal code but as soon as I add a dependency I got the same error. I also tried deploying on Digital Ocean as well but I think they use the open source Heroku buildpack so I got the same error
update 2: I got a pared down version of the app working. The repo has no vendor directory or vendor json whatsoever despite using an external package (build log shows successful install of external package)

Build dependencis for aws-sdk-go fails on Go version 1.13

I have been trying to build a module on Go v1.13 with dependencies on github.com/aws/aws-lambda-go and github.com/aws/aws-sdk-go which fail on the two imports:
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
The stderr is as follows for GOOS=linux GOARCH=amd64 go build -o dist/api ./api where the api directory contains my module definition:
api/main.go:11:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOPATH)
api/main.go:12:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3/s3manager" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOPATH)
Honestly, I have no clue why this is happening and any inputs would be appreciated.
Already tried using go get to ensure that the dependencies have been pulled:
$ go get github.com/aws/aws-sdk-go
and the requested import paths are present under $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3 and $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3/s3manager
Also, tried clearing the cache using go clean --cache --modcache whilst removing previously pulled modules.
On closer inspection, something that I completely overlooked, the import path is /u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 instead of /u/go/src/github.com/aws/aws-sdk-go/service/s3 with the former having an additional aws subpath inside aws-sdk-go.
Just realized the copy/paste error I had made in the code.
import (
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
)
instead of
import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

Adding external packages to Hyperledger fabric -go smartcontract

I am trying to add an external package to my go smartcontract (for installation , I ran : go get github.com/stripe/stripe-go) . I then imported files stripe "github.com/stripe/stripe-go" and charge "github.com/stripe/stripe-go/charge" , in my smartcontract (https://github.com/stripe/stripe-go). However, when I try to bring up my chaincode , I am getting the error :
Error: Error getting chaincode code chaincode: Error getting chaincode package bytes: Error obtaining dependencies for github.com/stripe/stripe-go: : failed with error: "exit status 1"
can't load package: package github.com/stripe/stripe-go: cannot find package "github.com/stripe/stripe-go" in any of:
/opt/go/src/github.com/stripe/stripe-go (from $GOROOT)
/opt/gopath/src/github.com/stripe/stripe-go (from $GOPATH)
I tried adding the package through govendor (http://hyperledger-fabric.readthedocs.io/en/release-1.0/Style-guides/go-style.html) , but that also is not working .
Request you all to please hep me on this . Thanks in advance.
I had the similar issue.
Govendor worked for me:
error getting chaincode package bytes: Error obtaining dependencies for github.com/go-ozzo/ozzo-validation/is
govendor add github.com/go-ozzo/ozzo-validation/is
govendor update +v
go build
And result was good:
Chaincode is installed on remote peer PEER2
UPDATE:
If you have a problem with vendor packages setup, here you go:
create folder vendor in project root directory with empty valid json file (vendor.json)
run command govendor add +external from root directory
you can also update all dependencies with update command.

Cannot find package under GOPATH

I am trying to install the dependencies of my project with glidebut unfortunately it fails with the following message:
main.go:7:2: cannot find package "github.com/arschles/go-in-5-minutes/episode13/models" in any of:
/Users/theo/go-workspace/src/github.com/thitami/go-in-5-minutes/episode13/vendor/github.com/arschles/go-in-5-minutes/episode13/models (vendor tree)
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOROOT)
/Users/theo/go-workspace/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOPATH)
Running a go env, this is my env variables of interest:
GOPATH="/Users/theo/go-workspace"
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
Please be advised that I am zsh and I am exporting the GOPATH inside the .zshrc file like this:
export GOPATH=HOME/go-workspace
Any ideas are appreciated
* UPDATE *
As requested this is the piece of code with the call to the models package:
import (
"database/sql"
"log"
"github.com/arschles/go-in-5-minutes/episode13/models"
_ "github.com/mxk/go-sqlite/sqlite3"
)
You can only set the gopath once and you'll have to reset it every time you change packages. Think of it as a virtualenv. One way around it is to install the packages global or by using something like gvm

Unwanted Glide imports

I am working on an App Engine app and am coming across this error running goapp serve:
Failed parsing input: parser: bad import "syscall" in vendor/golang.org/x/net/icmp/message.go
Which is apparently from Glide importing the entire golang.org/x/net package when I only wanted golang.org/x/net/context.
The import in my glide.yaml was:
- package: golang.org/x/net
subpackages:
- context
And the glide.lock file says
- name: golang.org/x/net
version: 07b51741c1d6423d4a6abab1c49940ec09cb1aaf
subpackages:
- context
How would I keep net/icmp out of my build in goapp serve?
I often had extra imports when using glide (a bit like in issue 101)
Just for testing, try re-populate your vendor folder using govendor
govendor list
govendor fetch +m
In my experience, the resulting list of dependencies is cleaner.

Resources