Commit 65f1c74b authored by Jonathan Levi (HACERA)'s avatar Jonathan Levi (HACERA) Committed by Gerrit Code Review
Browse files

Merge "FAB-11521 Have chaincode store return hash on save"

parents 35fc4132 c66b2c6a
......@@ -71,25 +71,26 @@ type Store struct {
// Save persists chaincode install package bytes with the given name
// and version
func (s *Store) Save(name, version string, ccInstallPkg []byte) error {
func (s *Store) Save(name, version string, ccInstallPkg []byte) ([]byte, error) {
metadataJSON, err := toJSON(name, version)
if err != nil {
return err
return nil, err
}
hashString := hex.EncodeToString(util.ComputeSHA256(ccInstallPkg))
hash := util.ComputeSHA256(ccInstallPkg)
hashString := hex.EncodeToString(hash)
metadataPath := filepath.Join(s.Path, hashString+".json")
if _, err := s.ReadWriter.Stat(metadataPath); err == nil {
return errors.Errorf("chaincode metadata already exists at %s", metadataPath)
return nil, errors.Errorf("chaincode metadata already exists at %s", metadataPath)
}
ccInstallPkgPath := filepath.Join(s.Path, hashString+".bin")
if _, err := s.ReadWriter.Stat(ccInstallPkgPath); err == nil {
return errors.Errorf("ChaincodeInstallPackage already exists at %s", ccInstallPkgPath)
return nil, errors.Errorf("ChaincodeInstallPackage already exists at %s", ccInstallPkgPath)
}
if err := s.ReadWriter.WriteFile(metadataPath, metadataJSON, 0600); err != nil {
return errors.Wrapf(err, "error writing metadata file to %s", metadataPath)
return nil, errors.Wrapf(err, "error writing metadata file to %s", metadataPath)
}
if err := s.ReadWriter.WriteFile(ccInstallPkgPath, ccInstallPkg, 0600); err != nil {
......@@ -100,10 +101,10 @@ func (s *Store) Save(name, version string, ccInstallPkg []byte) error {
if err2 := s.ReadWriter.Remove(metadataPath); err2 != nil {
logger.Errorf("error removing metadata file at %s: %s", metadataPath, err2)
}
return err
return nil, err
}
return nil
return hash, nil
}
// Load loads a persisted chaincode install package bytes with the given hash
......
......@@ -120,8 +120,9 @@ var _ = Describe("Persistence", func() {
})
It("saves successfully", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(err).NotTo(HaveOccurred())
Expect(hash).To(Equal(util.ComputeSHA256([]byte("testpkg"))))
})
Context("when the metadata file already exists", func() {
......@@ -130,8 +131,9 @@ var _ = Describe("Persistence", func() {
})
It("returns an error", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(err).To(HaveOccurred())
Expect(hash).To(BeNil())
Expect(err.Error()).To(Equal("chaincode metadata already exists at " + hashString + ".json"))
})
})
......@@ -143,7 +145,8 @@ var _ = Describe("Persistence", func() {
})
It("returns an error", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(hash).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("ChaincodeInstallPackage already exists at " + hashString + ".bin"))
})
......@@ -156,7 +159,8 @@ var _ = Describe("Persistence", func() {
})
It("returns an error", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(hash).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error writing metadata file"))
})
......@@ -169,7 +173,8 @@ var _ = Describe("Persistence", func() {
})
It("returns an error", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(hash).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error writing chaincode install package"))
})
......@@ -183,7 +188,8 @@ var _ = Describe("Persistence", func() {
})
It("returns an error", func() {
err := store.Save("testcc", "1.0", pkgBytes)
hash, err := store.Save("testcc", "1.0", pkgBytes)
Expect(hash).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error writing chaincode install package"))
})
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment