Skip to content

Commit

Permalink
Merge branch 'master' into FAB-14083
Browse files Browse the repository at this point in the history
  • Loading branch information
pamandrejko authored Dec 12, 2019
2 parents 1432b66 + f93acfa commit 2420fa5
Show file tree
Hide file tree
Showing 41 changed files with 2,044 additions and 1,218 deletions.
2 changes: 1 addition & 1 deletion ci/scripts/publish_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ for image in baseos peer orderer ccenv tools; do
docker push "hyperledger/fabric-${image}:amd64-${RELEASE}"

./manifest-tool push from-args --platforms linux/amd64 --template "hyperledger/fabric-${image}:amd64-${RELEASE}" --target "hyperledger/fabric-${image}:${RELEASE}"
./manifest-tool push from-args --platforms linux/amd64 --template "hyperledger/fabric-${image}:amd64-${RELEASE}" --target "hyperledger/fabric-${image}:$(sed 's/..$//' <<< ${RELEASE})"
./manifest-tool push from-args --platforms linux/amd64 --template "hyperledger/fabric-${image}:amd64-${RELEASE}" --target "hyperledger/fabric-${image}:${TWO_DIGIT_RELEASE}"
done
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func initMockPeer(channelIDs ...string) (*peer.Peer, *ChaincodeSupport, func(),
}

containerRouter := &container.Router{
DockerVM: &dockercontroller.DockerVM{
DockerBuilder: &dockercontroller.DockerVM{
PlatformBuilder: &platforms.Builder{
Registry: platforms.NewRegistry(&golang.Platform{}),
Client: client,
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func initPeer(channelIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSupp
}

containerRouter := &container.Router{
DockerVM: &dockercontroller.DockerVM{
DockerBuilder: &dockercontroller.DockerVM{
PeerID: "",
NetworkID: "",
BuildMetrics: dockercontroller.NewBuildMetrics(&disabled.Provider{}),
Expand Down
48 changes: 39 additions & 9 deletions core/chaincode/persistence/chaincode_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,45 @@ type FallbackPackageLocator struct {
LegacyCCPackageLocator LegacyCCPackageLocator
}

func (fpl *FallbackPackageLocator) GetChaincodePackage(packageID string) (*ChaincodePackageMetadata, io.ReadCloser, error) {
func (fpl *FallbackPackageLocator) GetChaincodePackage(packageID string) (*ChaincodePackageMetadata, []byte, io.ReadCloser, error) {
// XXX, this path has too many return parameters. We could split it into two calls,
// or, we could deserialize the metadata where it's needed. But, as written was the
// fastest path to fixing a bug around the mutation of metadata.
streamer := fpl.ChaincodePackageLocator.ChaincodePackageStreamer(packageID)
if streamer.Exists() {
metadata, err := streamer.Metadata()
if err != nil {
return nil, nil, errors.WithMessagef(err, "error retrieving chaincode package metadata '%s'", packageID)
return nil, nil, nil, errors.WithMessagef(err, "error retrieving chaincode package metadata '%s'", packageID)
}

mdBytes, err := streamer.MetadataBytes()
if err != nil {
return nil, nil, nil, errors.WithMessagef(err, "error retrieving chaincode package metadata bytes '%s'", packageID)
}

tarStream, err := streamer.Code()
if err != nil {
return nil, nil, errors.WithMessagef(err, "error retrieving chaincode package code '%s'", packageID)
return nil, nil, nil, errors.WithMessagef(err, "error retrieving chaincode package code '%s'", packageID)
}

return metadata, tarStream, nil
return metadata, mdBytes, tarStream, nil
}

cds, err := fpl.LegacyCCPackageLocator.GetChaincodeDepSpec(string(packageID))
if err != nil {
return nil, nil, errors.WithMessagef(err, "could not get legacy chaincode package '%s'", packageID)
return nil, nil, nil, errors.WithMessagef(err, "could not get legacy chaincode package '%s'", packageID)
}

md := &ChaincodePackageMetadata{
Path: cds.ChaincodeSpec.ChaincodeId.Path,
Type: cds.ChaincodeSpec.Type.String(),
Label: cds.ChaincodeSpec.ChaincodeId.Name,
}

return &ChaincodePackageMetadata{
Path: cds.ChaincodeSpec.ChaincodeId.Path,
Type: cds.ChaincodeSpec.Type.String(),
},
mdBytes, err := json.Marshal(md)

return md,
mdBytes,
ioutil.NopCloser(bytes.NewBuffer(cds.CodePackage)),
nil
}
Expand Down Expand Up @@ -116,6 +130,22 @@ func (cps *ChaincodePackageStreamer) Metadata() (*ChaincodePackageMetadata, erro
return metadata, nil
}

func (cps *ChaincodePackageStreamer) MetadataBytes() ([]byte, error) {
tarFileStream, err := cps.File(MetadataFile)
if err != nil {
return nil, errors.WithMessage(err, "could not get metadata file")
}

defer tarFileStream.Close()

md, err := ioutil.ReadAll(tarFileStream)
if err != nil {
return nil, errors.WithMessage(err, "could read metadata file")
}

return md, nil
}

func (cps *ChaincodePackageStreamer) Code() (*TarFileStream, error) {
tarFileStream, err := cps.File(CodePackageFile)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions core/chaincode/persistence/chaincode_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ var _ = Describe("FallbackPackageLocator", func() {

Describe("GetChaincodePackage", func() {
It("gets the chaincode package metadata and stream", func() {
md, stream, err := fpl.GetChaincodePackage("good-package")
md, mdBytes, stream, err := fpl.GetChaincodePackage("good-package")
Expect(err).NotTo(HaveOccurred())
defer stream.Close()
Expect(md).To(Equal(&persistence.ChaincodePackageMetadata{
Type: "Fake-Type",
Path: "Fake-Path",
Label: "Real-Label",
}))
Expect(mdBytes).To(MatchJSON(`{"type":"Fake-Type","path":"Fake-Path","label":"Real-Label","extra_field":"extra-field-value"}`))
code, err := ioutil.ReadAll(stream)
Expect(err).NotTo(HaveOccurred())
Expect(code).To(Equal([]byte("package")))
Expand All @@ -54,14 +55,14 @@ var _ = Describe("FallbackPackageLocator", func() {

Context("when the package has bad metadata", func() {
It("wraps and returns the error", func() {
_, _, err := fpl.GetChaincodePackage("bad-metadata")
_, _, _, err := fpl.GetChaincodePackage("bad-metadata")
Expect(err).To(MatchError(ContainSubstring("error retrieving chaincode package metadata 'bad-metadata'")))
})
})

Context("when the package has bad code", func() {
It("wraps and returns the error", func() {
_, _, err := fpl.GetChaincodePackage("missing-codepackage")
_, _, _, err := fpl.GetChaincodePackage("missing-codepackage")
Expect(err).To(MatchError(ContainSubstring("error retrieving chaincode package code 'missing-codepackage'")))
})
})
Expand All @@ -82,13 +83,14 @@ var _ = Describe("FallbackPackageLocator", func() {
})

It("falls back to the legacy retriever", func() {
md, stream, err := fpl.GetChaincodePackage("legacy-package")
md, mdBytes, stream, err := fpl.GetChaincodePackage("legacy-package")
Expect(err).NotTo(HaveOccurred())
defer stream.Close()
Expect(md).To(Equal(&persistence.ChaincodePackageMetadata{
Path: "legacy-path",
Type: "GOLANG",
}))
Expect(mdBytes).To(MatchJSON(`{"type":"GOLANG","path":"legacy-path","label":""}`))
code, err := ioutil.ReadAll(stream)
Expect(err).NotTo(HaveOccurred())
Expect(code).To(Equal([]byte("legacy-code")))
Expand All @@ -100,7 +102,7 @@ var _ = Describe("FallbackPackageLocator", func() {
})

It("wraps and returns the error", func() {
_, _, err := fpl.GetChaincodePackage("legacy-package")
_, _, _, err := fpl.GetChaincodePackage("legacy-package")
Expect(err).To(MatchError("could not get legacy chaincode package 'legacy-package': fake-error"))
})
})
Expand Down
Binary file modified core/chaincode/persistence/testdata/good-package.tar.gz
Binary file not shown.
32 changes: 21 additions & 11 deletions core/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ import (

var vmLogger = flogging.MustGetLogger("container")

//go:generate counterfeiter -o mock/vm.go --fake-name VM . VM
//go:generate counterfeiter -o mock/docker_builder.go --fake-name DockerBuilder . DockerBuilder

//VM is an abstract virtual image for supporting arbitrary virual machines
type VM interface {
// DockerBuilder is what is exposed by the dockercontroller
type DockerBuilder interface {
Build(ccid string, metadata *persistence.ChaincodePackageMetadata, codePackageStream io.Reader) (Instance, error)
}

//go:generate counterfeiter -o mock/external_builder.go --fake-name ExternalBuilder . ExternalBuilder

// ExternalBuilder is what is exposed by the dockercontroller
type ExternalBuilder interface {
Build(ccid string, metadata []byte, codePackageStream io.Reader) (Instance, error)
}

//go:generate counterfeiter -o mock/instance.go --fake-name Instance . Instance

// Instance represents a built chaincode instance, because of the docker legacy, calling this a
Expand Down Expand Up @@ -60,12 +67,12 @@ func (UninitializedInstance) Wait() (int, error) {

// PackageProvider gets chaincode packages from the filesystem.
type PackageProvider interface {
GetChaincodePackage(packageID string) (*persistence.ChaincodePackageMetadata, io.ReadCloser, error)
GetChaincodePackage(packageID string) (md *persistence.ChaincodePackageMetadata, mdBytes []byte, codeStream io.ReadCloser, err error)
}

type Router struct {
ExternalVM VM
DockerVM VM
ExternalBuilder ExternalBuilder
DockerBuilder DockerBuilder
containers map[string]Instance
PackageProvider PackageProvider
mutex sync.Mutex
Expand All @@ -89,29 +96,32 @@ func (r *Router) getInstance(ccid string) Instance {
func (r *Router) Build(ccid string) error {
var instance Instance

if r.ExternalVM != nil {
if r.ExternalBuilder != nil {
// for now, the package ID we retrieve from the FS is always the ccid
// the chaincode uses for registration
metadata, codeStream, err := r.PackageProvider.GetChaincodePackage(ccid)
_, mdBytes, codeStream, err := r.PackageProvider.GetChaincodePackage(ccid)
if err != nil {
return errors.WithMessage(err, "failed to get chaincode package for external build")
}
defer codeStream.Close()

instance, err = r.ExternalVM.Build(ccid, metadata, codeStream)
instance, err = r.ExternalBuilder.Build(ccid, mdBytes, codeStream)
if err != nil {
return errors.WithMessage(err, "external builder failed")
}
}

if instance == nil {
metadata, codeStream, err := r.PackageProvider.GetChaincodePackage(ccid)
if r.DockerBuilder == nil {
return errors.New("no DockerBuilder, cannot build")
}
metadata, _, codeStream, err := r.PackageProvider.GetChaincodePackage(ccid)
if err != nil {
return errors.WithMessage(err, "failed to get chaincode package for docker build")
}
defer codeStream.Close()

instance, err = r.DockerVM.Build(ccid, metadata, codeStream)
instance, err = r.DockerBuilder.Build(ccid, metadata, codeStream)
if err != nil {
return errors.WithMessage(err, "docker build failed")
}
Expand Down
Loading

0 comments on commit 2420fa5

Please sign in to comment.