-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: small improvements for #1702 #2276
Changes from 8 commits
7289a9a
1d4ef92
c63ba9d
2b2212f
593db9a
4930964
eed932e
d7134db
395e263
e454cf4
d20ff79
b8e6373
23208e1
e16edff
2f5589f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,7 @@ pbbindings.go | |
# Test coverage leftovers | ||
cover.out | ||
coverage.out | ||
|
||
*.swp | ||
*.swo | ||
*.bak |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { | |
// NOTE: native functions/methods added here must be quick operations, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no real need to have this function at all... getPackage below might as well be a method on the store. |
||
// or account for gas before operation. | ||
// TODO: define criteria for inclusion, and solve gas calculations. | ||
getPackage := func(pkgPath string, newStore gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { | ||
getPackage := func(pkgPath string, store gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { | ||
// otherwise, built-in package value. | ||
// first, load from filepath. | ||
stdlibPath := filepath.Join(vm.stdlibsDir, pkgPath) | ||
|
@@ -34,10 +34,11 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { | |
PkgPath: "gno.land/r/stdlibs/" + pkgPath, | ||
// PkgPath: pkgPath, | ||
Output: os.Stdout, | ||
Store: newStore, | ||
Store: store, | ||
}) | ||
defer m2.Release() | ||
return m2.RunMemPackage(memPkg, true) | ||
pn, pv = m2.RunMemPackage(memPkg, true) | ||
return | ||
} | ||
store.SetPackageGetter(getPackage) | ||
store.SetNativeStore(stdlibs.NativeStore) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. diff artifact, not actually removed from the original code block. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,16 +9,21 @@ | |
"strings" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/amino" | ||
"github.com/gnolang/gno/tm2/pkg/colors" | ||
"github.com/gnolang/gno/tm2/pkg/std" | ||
"github.com/gnolang/gno/tm2/pkg/store" | ||
"github.com/gnolang/gno/tm2/pkg/store/types" | ||
"github.com/gnolang/gno/tm2/pkg/store/utils" | ||
stringz "github.com/gnolang/gno/tm2/pkg/strings" | ||
) | ||
|
||
// PackageGetter specifies how the store may retrieve packages which are not | ||
// already in its cache. PackageGetter should return nil when the requested | ||
// package does not exist. store should be used to run the machine, or otherwise | ||
// call any methods which may call store.GetPackage; avoid using any "global" | ||
// store as the one passed to the PackageGetter may be a fork of that (ie. | ||
// the original is not meant to be written to). | ||
// the original is not meant to be written to). Loading dependencies may | ||
// cause writes to happen to the store, such as MemPackages to iavlstore. | ||
type PackageGetter func(pkgPath string, store Store) (*PackageNode, *PackageValue) | ||
|
||
// inject natives into a new or loaded package (value and node) | ||
|
@@ -68,6 +73,8 @@ | |
LogSwitchRealm(rlmpath string) // to mark change of realm boundaries | ||
ClearCache() | ||
Print() | ||
Write() | ||
Flush() | ||
} | ||
|
||
// Used to keep track of in-mem objects during tx. | ||
|
@@ -86,7 +93,7 @@ | |
|
||
// transient | ||
opslog []StoreOp // for debugging and testing. | ||
current []string | ||
current []string // for detecting import cycles. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's still detecting import cycles. |
||
} | ||
|
||
func NewStore(alloc *Allocator, baseStore, iavlStore store.Store) *defaultStore { | ||
|
@@ -115,6 +122,7 @@ | |
|
||
// Gets package from cache, or loads it from baseStore, or gets it from package getter. | ||
func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue { | ||
// helper to detect circular imports | ||
if isImport { | ||
if slices.Contains(ds.current, pkgPath) { | ||
panic(fmt.Sprintf("import cycle detected: %q (through %v)", pkgPath, ds.current)) | ||
|
@@ -613,6 +621,7 @@ | |
ds.alloc.Reset() | ||
ds.cacheObjects = make(map[ObjectID]Object) // new cache. | ||
ds.opslog = nil // new ops log. | ||
//ds.current = nil | ||
ds.SetCachePackage(Uverse()) | ||
} | ||
|
||
|
@@ -672,8 +681,16 @@ | |
return nil | ||
} | ||
|
||
// Writes one level of cache to store. | ||
func (ds *defaultStore) Write() { | ||
ds.baseStore.(types.Writer).Write() | ||
ds.iavlStore.(types.Writer).Write() | ||
} | ||
|
||
// Flush cached writes to disk. | ||
func (ds *defaultStore) Flush() { | ||
// XXX | ||
ds.baseStore.(types.Flusher).Flush() | ||
ds.iavlStore.(types.Flusher).Flush() | ||
} | ||
|
||
// ---------------------------------------- | ||
|
@@ -755,22 +772,25 @@ | |
|
||
// for debugging | ||
func (ds *defaultStore) Print() { | ||
fmt.Println("//----------------------------------------") | ||
fmt.Println("defaultStore:baseStore...") | ||
store.Print(ds.baseStore) | ||
fmt.Println("//----------------------------------------") | ||
fmt.Println("defaultStore:iavlStore...") | ||
store.Print(ds.iavlStore) | ||
fmt.Println("//----------------------------------------") | ||
fmt.Println("defaultStore:cacheTypes...") | ||
fmt.Println(colors.Yellow("//----------------------------------------")) | ||
fmt.Println(colors.Green("defaultStore:baseStore...")) | ||
utils.Print(ds.baseStore) | ||
fmt.Println(colors.Yellow("//----------------------------------------")) | ||
fmt.Println(colors.Green("defaultStore:iavlStore...")) | ||
utils.Print(ds.iavlStore) | ||
fmt.Println(colors.Yellow("//----------------------------------------")) | ||
fmt.Println(colors.Green("defaultStore:cacheTypes...")) | ||
for tid, typ := range ds.cacheTypes { | ||
fmt.Printf("- %v: %v\n", tid, typ) | ||
fmt.Printf("- %v: %v\n", tid, | ||
stringz.TrimN(fmt.Sprintf("%v", typ), 50)) | ||
} | ||
fmt.Println("//----------------------------------------") | ||
fmt.Println("defaultStore:cacheNodes...") | ||
fmt.Println(colors.Yellow("//----------------------------------------")) | ||
fmt.Println(colors.Green("defaultStore:cacheNodes...")) | ||
for loc, bn := range ds.cacheNodes { | ||
fmt.Printf("- %v: %v\n", loc, bn) | ||
fmt.Printf("- %v: %v\n", loc, | ||
stringz.TrimN(fmt.Sprintf("%v", bn), 50)) | ||
} | ||
fmt.Println(colors.Red("//----------------------------------------")) | ||
} | ||
|
||
// ---------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,8 +61,8 @@ const ( | |
) | ||
|
||
// NOTE: this isn't safe, should only be used for testing. | ||
func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Writer, mode importMode) (store gno.Store) { | ||
getPackage := func(pkgPath string, newStore gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { | ||
func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Writer, mode importMode) (resStore gno.Store) { | ||
getPackage := func(pkgPath string, store gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { | ||
if pkgPath == "" { | ||
panic(fmt.Sprintf("invalid zero package path in testStore().pkgGetter")) | ||
} | ||
|
@@ -83,7 +83,7 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri | |
m2 := gno.NewMachineWithOptions(gno.MachineOptions{ | ||
PkgPath: "test", | ||
Output: stdout, | ||
Store: newStore, | ||
Store: store, | ||
Context: ctx, | ||
}) | ||
// pkg := gno.NewPackageNode(gno.Name(memPkg.Name), memPkg.Path, nil) | ||
|
@@ -96,7 +96,7 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri | |
// if stdlibs package is preferred , try to load it first. | ||
if mode == ImportModeStdlibsOnly || | ||
mode == ImportModeStdlibsPreferred { | ||
pn, pv = loadStdlib(rootDir, pkgPath, newStore, stdout) | ||
pn, pv = loadStdlib(rootDir, pkgPath, store, stdout) | ||
if pn != nil { | ||
return | ||
} | ||
|
@@ -375,7 +375,7 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri | |
|
||
// if native package is preferred, try to load stdlibs/* as backup. | ||
if mode == ImportModeNativePreferred { | ||
pn, pv = loadStdlib(rootDir, pkgPath, newStore, stdout) | ||
pn, pv = loadStdlib(rootDir, pkgPath, store, stdout) | ||
if pn != nil { | ||
return | ||
} | ||
|
@@ -394,23 +394,23 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri | |
m2 := gno.NewMachineWithOptions(gno.MachineOptions{ | ||
PkgPath: "test", | ||
Output: stdout, | ||
Store: newStore, | ||
Store: store, | ||
Context: ctx, | ||
}) | ||
pn, pv = m2.RunMemPackage(memPkg, true) | ||
return | ||
} | ||
return nil, nil | ||
} | ||
// NOTE: store is also used in closure above. | ||
db := memdb.NewMemDB() | ||
baseStore := dbadapter.StoreConstructor(db, stypes.StoreOptions{}) | ||
iavlStore := iavl.StoreConstructor(db, stypes.StoreOptions{}) | ||
store = gno.NewStore(nil, baseStore, iavlStore) | ||
store.SetPackageGetter(getPackage) | ||
store.SetNativeStore(teststdlibs.NativeStore) | ||
store.SetPackageInjector(testPackageInjector) | ||
store.SetStrictGo2GnoMapping(false) | ||
// make a new store | ||
resStore = gno.NewStore(nil, baseStore, iavlStore) | ||
resStore.SetPackageGetter(getPackage) | ||
resStore.SetNativeStore(teststdlibs.NativeStore) | ||
resStore.SetPackageInjector(testPackageInjector) | ||
resStore.SetStrictGo2GnoMapping(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they're both new stores, so name neither "newStore" :) |
||
return | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was always requried, but its absence (of MultiWrite) being masked by the fact that later transactions would commit the same side effect, e.g. populate the mem package for dependencies like "time" or "strconv". But i'm not sure. If that's true, then loadpkg in genesis would have no effect for our integration tests?
Can somebody look into it?