Skip to content

Commit

Permalink
FAB-11521 Have chaincode store return hash on save
Browse files Browse the repository at this point in the history
Rather than require the user to compute the hash of the chaincode, it
makes more sense to return it to them on install.  Instead of requiring
a lookup, this CR simply adds it as a return value from the persisting
to the store.

Makes #Done FAB-11521

Change-Id: I8846ac7385440fe401ea2603b1e8632f212039e0
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Aug 22, 2018
1 parent d360f22 commit c66b2c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
17 changes: 9 additions & 8 deletions core/chaincode/persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions core/chaincode/persistence/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"))
})
})
Expand All @@ -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"))
})
Expand All @@ -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"))
})
Expand All @@ -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"))
})
Expand All @@ -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"))
})
Expand Down

0 comments on commit c66b2c6

Please sign in to comment.