Skip to content

Commit

Permalink
Append new version to MemPackageInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Feb 8, 2024
1 parent afee242 commit 0d14e77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
29 changes: 23 additions & 6 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
"golang.org/x/mod/semver"
)

//----------------------------------------
Expand Down Expand Up @@ -272,15 +273,31 @@ func (m *Machine) runMemPackage(memPkg *std.MemPackage, save, overrides bool) (*
m.RunFiles(files.Files...)
// maybe save package value and mempackage.
if save {
memPkgInfo, err := m.Store.GetMemPackageInfo(memPkg.ModFile.ImportPath)
if err != nil {
memPkgInfo = &std.MemPackageInfo{
Name: memPkg.Name,
Path: memPkg.ModFile.ImportPath,
Versions: []*std.MemPackage{memPkg},
}
} else {
versionCount := len(memPkgInfo.Versions)
if versionCount < 1 {
panic("empty memPackageInfo")
}
latest := memPkgInfo.Versions[versionCount-1]
if semver.Compare(memPkg.ModFile.Version, latest.ModFile.Version) <= 0 {
panic(fmt.Sprintf("version should be greater then %q", latest.ModFile.Version))
}
memPkgInfo.Versions = append(memPkgInfo.Versions, memPkg)
}

// store package values and types
m.savePackageValuesAndTypes()
// store mempackage
// TODO(hariom): check if MemPackageInfo already exists, if it does, append to it
m.Store.AddMemPackage(&std.MemPackageInfo{
Name: memPkg.Name,
Path: memPkg.ModFile.ImportPath,
Versions: []*std.MemPackage{memPkg},
})
// TODO(hariom): Maintain 2 separate entries? to save gas cost?
// MemPackageInfo and Mempackage?
m.Store.AddMemPackageInfo(memPkgInfo)
}
return pn, pv
}
Expand Down
20 changes: 11 additions & 9 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type Store interface {
// Upon restart, all packages will be re-preprocessed; This
// loads BlockNodes and Types onto the store for persistence
// version 1.
AddMemPackage(memPkg *std.MemPackageInfo)
AddMemPackageInfo(memPkg *std.MemPackageInfo)
GetMemPackage(path, version string) *std.MemPackage
GetMemPackageInfo(path string) *std.MemPackageInfo
GetMemPackageInfo(path string) (*std.MemPackageInfo, error)
GetMemFile(path, version, name string) *std.MemFile
IterMemPackageInfo() <-chan *std.MemPackageInfo
ClearObjectCache() // for each delivertx.
Expand Down Expand Up @@ -529,7 +529,7 @@ func (ds *defaultStore) incGetPackageIndexCounter() uint64 {
}
}

func (ds *defaultStore) AddMemPackage(memPkgInfo *std.MemPackageInfo) {
func (ds *defaultStore) AddMemPackageInfo(memPkgInfo *std.MemPackageInfo) {
memPkgInfo.Validate() // NOTE: duplicate validation.
ctr := ds.incGetPackageIndexCounter()
idxkey := []byte(backendPackageIndexKey(ctr))
Expand All @@ -539,16 +539,15 @@ func (ds *defaultStore) AddMemPackage(memPkgInfo *std.MemPackageInfo) {
ds.iavlStore.Set(pathkey, bz)
}

func (ds *defaultStore) GetMemPackageInfo(path string) *std.MemPackageInfo {
func (ds *defaultStore) GetMemPackageInfo(path string) (*std.MemPackageInfo, error) {
pathkey := []byte(backendPackagePathKey(path))
bz := ds.iavlStore.Get(pathkey)
if bz == nil {
panic(fmt.Sprintf(
"missing package at path %s", string(pathkey)))
return nil, fmt.Errorf("missing package at path %s", string(pathkey))
}
var memPkgInfo *std.MemPackageInfo
amino.MustUnmarshal(bz, &memPkgInfo)
return memPkgInfo
return memPkgInfo, nil
}

func (ds *defaultStore) GetMemPackage(path, version string) *std.MemPackage {
Expand Down Expand Up @@ -599,7 +598,10 @@ func (ds *defaultStore) IterMemPackageInfo() <-chan *std.MemPackageInfo {
panic(fmt.Sprintf(
"missing package index %d", i))
}
memPkgInfo := ds.GetMemPackageInfo(string(path))
memPkgInfo, err := ds.GetMemPackageInfo(string(path))
if err != nil {
panic(err)
}
ch <- memPkgInfo
}
close(ch)
Expand Down Expand Up @@ -795,7 +797,7 @@ func backendPackageIndexKey(index uint64) string {
}

func backendPackagePathKey(path string) string {
return fmt.Sprintf("pkg:" + path)
return fmt.Sprintf("pkg:%s", path)
}

// ----------------------------------------
Expand Down

0 comments on commit 0d14e77

Please sign in to comment.