From 7289a9a8b006e2226068e30ea12b7ac094e08632 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 20:22:04 -0700 Subject: [PATCH 01/14] remove unnecessary store argument --- gno.land/pkg/sdk/vm/builtins.go | 4 ++-- gnovm/pkg/gnolang/debugger_test.go | 3 ++- gnovm/pkg/gnolang/gonative_test.go | 2 +- gnovm/pkg/gnolang/store.go | 13 ++++++------- gnovm/tests/imports.go | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index 368ada6ff82..63062847e01 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -16,7 +16,7 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // NOTE: native functions/methods added here must be quick operations, // 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) (pn *gno.PackageNode, pv *gno.PackageValue) { // otherwise, built-in package value. // first, load from filepath. stdlibPath := filepath.Join(vm.stdlibsDir, pkgPath) @@ -34,7 +34,7 @@ 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) diff --git a/gnovm/pkg/gnolang/debugger_test.go b/gnovm/pkg/gnolang/debugger_test.go index ad78cb143ee..738249ad2e9 100644 --- a/gnovm/pkg/gnolang/debugger_test.go +++ b/gnovm/pkg/gnolang/debugger_test.go @@ -196,7 +196,8 @@ func TestRemoteDebug(t *testing.T) { func TestRemoteError(t *testing.T) { _, err := evalTest(":xxx", "", debugTarget) t.Log("err:", err) - if !strings.Contains(err, "tcp/xxx: unknown port") { + if !strings.Contains(err, "tcp/xxx: unknown port") && + !strings.Contains(err, "tcp/xxx: nodename nor servname provided, or not known") { t.Error(err) } } diff --git a/gnovm/pkg/gnolang/gonative_test.go b/gnovm/pkg/gnolang/gonative_test.go index 42729b43699..33e687cd40a 100644 --- a/gnovm/pkg/gnolang/gonative_test.go +++ b/gnovm/pkg/gnolang/gonative_test.go @@ -15,7 +15,7 @@ import ( // and the odd index items are corresponding package values. func gonativeTestStore(args ...interface{}) Store { store := NewStore(nil, nil, nil) - store.SetPackageGetter(func(pkgPath string, _ Store) (*PackageNode, *PackageValue) { + store.SetPackageGetter(func(pkgPath string) (*PackageNode, *PackageValue) { for i := 0; i < len(args)/2; i++ { pn := args[i*2].(*PackageNode) pv := args[i*2+1].(*PackageValue) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 3db53213f8b..0c87770e8ae 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -15,11 +15,8 @@ import ( // 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). -type PackageGetter func(pkgPath string, store Store) (*PackageNode, *PackageValue) +// package does not exist. +type PackageGetter func(pkgPath string) (*PackageNode, *PackageValue) // inject natives into a new or loaded package (value and node) type PackageInjector func(store Store, pn *PackageNode) @@ -86,7 +83,7 @@ type defaultStore struct { // transient opslog []StoreOp // for debugging and testing. - current []string + current []string // for detecting import cycles. } func NewStore(alloc *Allocator, baseStore, iavlStore store.Store) *defaultStore { @@ -115,6 +112,7 @@ func (ds *defaultStore) SetPackageGetter(pg PackageGetter) { // 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)) @@ -167,7 +165,7 @@ func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue } // otherwise, fetch from pkgGetter. if ds.pkgGetter != nil { - if pn, pv := ds.pkgGetter(pkgPath, ds); pv != nil { + if pn, pv := ds.pkgGetter(pkgPath); pv != nil { // e.g. tests/imports_tests loads example/gno.land/r/... realms. // if pv.IsRealm() { // panic("realm packages cannot be gotten from pkgGetter") @@ -613,6 +611,7 @@ func (ds *defaultStore) ClearObjectCache() { ds.alloc.Reset() ds.cacheObjects = make(map[ObjectID]Object) // new cache. ds.opslog = nil // new ops log. + ds.current = nil ds.SetCachePackage(Uverse()) } diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index 6a3e6ab2bbb..0464da806de 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -62,7 +62,7 @@ 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) { + getPackage := func(pkgPath string) (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,7 +394,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, }) pn, pv = m2.RunMemPackage(memPkg, true) From 1d4ef92f2f695c651c578728da4e558bb05ce321 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 20:43:25 -0700 Subject: [PATCH 02/14] ... --- gno.land/pkg/sdk/vm/builtins.go | 2 ++ gnovm/pkg/gnolang/store.go | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index 63062847e01..ad74b162e48 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -1,6 +1,7 @@ package vm import ( + "fmt" "os" "path/filepath" @@ -17,6 +18,7 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // or account for gas before operation. // TODO: define criteria for inclusion, and solve gas calculations. getPackage := func(pkgPath string) (pn *gno.PackageNode, pv *gno.PackageValue) { + fmt.Println("!!!! getPackage in VMKeeper builtins.go", pkgPath) // otherwise, built-in package value. // first, load from filepath. stdlibPath := filepath.Join(vm.stdlibsDir, pkgPath) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 0c87770e8ae..4703262b457 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -114,11 +114,14 @@ func (ds *defaultStore) SetPackageGetter(pg PackageGetter) { func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue { // helper to detect circular imports if isImport { + fmt.Println(">>>>>>>>>>> ? ", pkgPath) if slices.Contains(ds.current, pkgPath) { panic(fmt.Sprintf("import cycle detected: %q (through %v)", pkgPath, ds.current)) } + fmt.Println(">>>>>>>>>>> ADD", pkgPath) ds.current = append(ds.current, pkgPath) defer func() { + fmt.Println(">>>>>>>>>>> REMOVE", pkgPath) ds.current = ds.current[:len(ds.current)-1] }() } @@ -611,7 +614,7 @@ func (ds *defaultStore) ClearObjectCache() { ds.alloc.Reset() ds.cacheObjects = make(map[ObjectID]Object) // new cache. ds.opslog = nil // new ops log. - ds.current = nil + //ds.current = nil ds.SetCachePackage(Uverse()) } From c63ba9deca4afac779746d8bb30b09fc1081f8c4 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 21:16:34 -0700 Subject: [PATCH 03/14] more debugging --- gno.land/pkg/sdk/vm/builtins.go | 3 +++ gnovm/pkg/gnolang/go2gno.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index ad74b162e48..84e99d3eb8b 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -19,6 +19,9 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // TODO: define criteria for inclusion, and solve gas calculations. getPackage := func(pkgPath string) (pn *gno.PackageNode, pv *gno.PackageValue) { fmt.Println("!!!! getPackage in VMKeeper builtins.go", pkgPath) + defer func() { + fmt.Println("!!!! getPackage returned", pv) + }() // otherwise, built-in package value. // first, load from filepath. stdlibPath := filepath.Join(vm.stdlibsDir, pkgPath) diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 5c74621c973..5b2b616d1cf 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -478,6 +478,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { //---------------------------------------- // type checking (using go/types) +// XXX move to gotypecheck.go. // MemPackageGetter implements the GetMemPackage() method. It is a subset of // [Store], separated for ease of testing. @@ -534,11 +535,14 @@ func (e importNotFoundError) Error() string { return "import not found: " + stri // ImportFrom returns the imported package for the given import // path when imported by a package file located in dir. func (g *gnoImporter) ImportFrom(path, _ string, _ types.ImportMode) (*types.Package, error) { + fmt.Println("gnoImporter.ImportFrom", path) if pkg, ok := g.cache[path]; ok { return pkg.pkg, pkg.err } + fmt.Println("gnoImporter.ImportFrom/getter.GetMemPackage", path) mpkg := g.getter.GetMemPackage(path) if mpkg == nil { + fmt.Println("gnoImporter.ImportFrom/was nil", path) err := importNotFoundError(path) g.cache[path] = gnoImporterResult{err: err} return nil, err From 2b2212f25d1bc82107e94d3b70bc8fb0c0aba938 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 21:32:18 -0700 Subject: [PATCH 04/14] ... --- gnovm/pkg/gnolang/go2gno.go | 2 +- gnovm/pkg/gnolang/store.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 5b2b616d1cf..9f6d6a05be0 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -544,7 +544,7 @@ func (g *gnoImporter) ImportFrom(path, _ string, _ types.ImportMode) (*types.Pac if mpkg == nil { fmt.Println("gnoImporter.ImportFrom/was nil", path) err := importNotFoundError(path) - g.cache[path] = gnoImporterResult{err: err} + // XXX g.cache[path] = gnoImporterResult{err: err} return nil, err } result, err := g.parseCheckMemPackage(mpkg) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 4703262b457..9edbc042c97 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -531,6 +531,7 @@ func (ds *defaultStore) incGetPackageIndexCounter() uint64 { } func (ds *defaultStore) AddMemPackage(memPkg *std.MemPackage) { + fmt.Println("defaultStore.AddMemPackage", memPkg.Path) memPkg.Validate() // NOTE: duplicate validation. ctr := ds.incGetPackageIndexCounter() idxkey := []byte(backendPackageIndexKey(ctr)) From 593db9a464f1113ed8cf9e9d3301ed731bf0cc3c Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 21:35:04 -0700 Subject: [PATCH 05/14] ... --- gno.land/pkg/sdk/vm/builtins.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index 84e99d3eb8b..37ecb896107 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -18,9 +18,9 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // or account for gas before operation. // TODO: define criteria for inclusion, and solve gas calculations. getPackage := func(pkgPath string) (pn *gno.PackageNode, pv *gno.PackageValue) { - fmt.Println("!!!! getPackage in VMKeeper builtins.go", pkgPath) + fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage", pkgPath) defer func() { - fmt.Println("!!!! getPackage returned", pv) + fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage returned nil?", pkgPath, pv == nil) }() // otherwise, built-in package value. // first, load from filepath. From 4930964e7852843326b3b173ca90854e909e0f4b Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 2 Jun 2024 21:44:35 -0700 Subject: [PATCH 06/14] ... --- gnovm/pkg/gnolang/store.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 9edbc042c97..6ccf7b91b42 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -538,6 +538,7 @@ func (ds *defaultStore) AddMemPackage(memPkg *std.MemPackage) { bz := amino.MustMarshal(memPkg) ds.baseStore.Set(idxkey, []byte(memPkg.Path)) pathkey := []byte(backendPackagePathKey(memPkg.Path)) + fmt.Printf("defaultStore.AddMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) ds.iavlStore.Set(pathkey, bz) } @@ -550,6 +551,7 @@ func (ds *defaultStore) GetMemPackage(path string) *std.MemPackage { func (ds *defaultStore) getMemPackage(path string, isRetry bool) *std.MemPackage { pathkey := []byte(backendPackagePathKey(path)) bz := ds.iavlStore.Get(pathkey) + fmt.Printf("defaultStore.getMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) if bz == nil { // If this is the first try, attempt using GetPackage to retrieve the // package, first. GetPackage can leverage pkgGetter, which in most From eed932e55ab03d9535a6f9b8a44d4198feffc068 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 00:40:10 -0700 Subject: [PATCH 07/14] most working --- .gitignore | 3 + gno.land/pkg/gnoclient/client_txs.go | 13 +++ gno.land/pkg/gnoland/app.go | 31 +++++- .../pkg/integration/testing_integration.go | 3 + gno.land/pkg/sdk/vm/builtins.go | 23 +++- gno.land/pkg/sdk/vm/keeper.go | 14 +++ gnovm/pkg/gnolang/go2gno.go | 6 +- gnovm/pkg/gnolang/store.go | 101 ++++++++++++++---- tm2/pkg/crypto/keys/client/maketx.go | 12 +++ tm2/pkg/db/goleveldb/go_level_db.go | 8 +- tm2/pkg/db/memdb/mem_db.go | 25 +++-- tm2/pkg/db/prefix_db.go | 15 ++- tm2/pkg/sdk/auth/ante.go | 51 +++++++++ tm2/pkg/sdk/auth/handler.go | 18 ++++ tm2/pkg/sdk/baseapp.go | 5 + tm2/pkg/store/cache/store.go | 86 ++++++++++++++- tm2/pkg/store/gas/store.go | 34 ++++++ tm2/pkg/store/store.go | 26 ----- tm2/pkg/store/types/store.go | 27 ++++- tm2/pkg/store/utils/print.go | 40 +++++++ tm2/pkg/strings/string.go | 9 ++ 21 files changed, 472 insertions(+), 78 deletions(-) create mode 100644 tm2/pkg/store/utils/print.go diff --git a/.gitignore b/.gitignore index 019c0be3c98..e622be821de 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ pbbindings.go # Test coverage leftovers cover.out coverage.out + +*.swp +*.swo diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index e306d737ede..0523016deab 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -295,6 +295,19 @@ func (c *Client) signAndBroadcastTxCommit(tx std.Tx, accountNumber, sequenceNumb sequenceNumber = account.Sequence } + /* + panic("WTF") + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(">>>", sequenceNumber, accountNumber) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) + */ + signCfg := SignCfg{ UnsignedTX: tx, SequenceNumber: sequenceNumber, diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index 7669d5cce95..1691079ff5c 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -18,6 +18,7 @@ import ( "github.com/gnolang/gno/tm2/pkg/store" "github.com/gnolang/gno/tm2/pkg/store/dbadapter" "github.com/gnolang/gno/tm2/pkg/store/iavl" + stypes "github.com/gnolang/gno/tm2/pkg/store/types" // Only goleveldb is supported for now. _ "github.com/gnolang/gno/tm2/pkg/db/_tags" @@ -56,6 +57,10 @@ func (c *AppOptions) validate() error { return nil } +type Printer interface { + Print() +} + // NewApp creates the GnoLand application. func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { if err := cfg.validate(); err != nil { @@ -120,7 +125,20 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { } // Initialize the VMKeeper. - vmKpr.Initialize(baseApp.GetCacheMultiStore()) + ms := baseApp.GetCacheMultiStore() + vmKpr.Initialize(ms) + mainStore := ms.GetStore(mainKey) + baseStore := ms.GetStore(baseKey) + if false { // XXX + fmt.Println("========== INiTIALIZE") + if ps, ok := mainStore.(stypes.Printer); ok { + ps.Print() + } + if ps, ok := baseStore.(stypes.Printer); ok { + ps.Print() + } + fmt.Println("========== INiTIALIZE END") + } return baseApp, nil } @@ -196,6 +214,17 @@ func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank // XXX not used yet. func EndBlocker(vmk vm.VMKeeperI) func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { + /* + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) + */ return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { return abci.ResponseEndBlock{} } diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 654dda0b45e..3b36fe580f9 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -226,6 +226,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { tsValidateError(ts, "gnokey", neg, err) } + // fmt.Println("sleeping 10 seconds") + //time.Sleep(10 * time.Second) + // Setup IO command io := commands.NewTestIO() io.SetOut(commands.WriteNopCloser(ts.Stdout())) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index 37ecb896107..bf2820e958f 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -7,6 +7,7 @@ import ( gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/stdlibs" + "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/crypto" osm "github.com/gnolang/gno/tm2/pkg/os" "github.com/gnolang/gno/tm2/pkg/sdk" @@ -18,10 +19,14 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // or account for gas before operation. // TODO: define criteria for inclusion, and solve gas calculations. getPackage := func(pkgPath string) (pn *gno.PackageNode, pv *gno.PackageValue) { - fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage", pkgPath) - defer func() { - fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage returned nil?", pkgPath, pv == nil) - }() + /* + // fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage", pkgPath) + defer func() { + // fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage returned nil?", pkgPath, pv == nil) + // XXX + // store.Print() + }() + */ // otherwise, built-in package value. // first, load from filepath. stdlibPath := filepath.Join(vm.stdlibsDir, pkgPath) @@ -42,7 +47,15 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { Store: store, }) defer m2.Release() - return m2.RunMemPackage(memPkg, true) + pn, pv = m2.RunMemPackage(memPkg, true) + // XXX + if false { + fmt.Println(colors.Red("VMKeeper.init.getPackage ========================================= before wrote")) + store.Print() + fmt.Println(colors.Red("VMKeeper.init.getPackage ========================================= before wrote end")) + } + store.Write() // XXX XXX XXX or flush? + return } store.SetPackageGetter(getPackage) store.SetNativeStore(stdlibs.NativeStore) diff --git a/gno.land/pkg/sdk/vm/keeper.go b/gno.land/pkg/sdk/vm/keeper.go index a77ddac3e28..3e09af9da12 100644 --- a/gno.land/pkg/sdk/vm/keeper.go +++ b/gno.land/pkg/sdk/vm/keeper.go @@ -17,6 +17,7 @@ import ( "github.com/gnolang/gno/tm2/pkg/sdk/bank" "github.com/gnolang/gno/tm2/pkg/std" "github.com/gnolang/gno/tm2/pkg/store" + stypes "github.com/gnolang/gno/tm2/pkg/store/types" "github.com/gnolang/gno/tm2/pkg/telemetry" "github.com/gnolang/gno/tm2/pkg/telemetry/metrics" "go.opentelemetry.io/otel/attribute" @@ -97,6 +98,11 @@ func (vm *VMKeeper) Initialize(ms store.MultiStore) { m2.PreprocessAllFilesAndSaveBlockNodes() gno.EnableDebug() } + if false { // XXX + fmt.Println("AFTER INITIALIZE") + iavlSDKStore.(stypes.Printer).Print() + fmt.Println("AFTER INITIALIZE END") + } } func (vm *VMKeeper) getGnoStore(ctx sdk.Context) gno.Store { @@ -158,6 +164,14 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) { return ErrInvalidPkgPath("package already exists: " + pkgPath) } + // XXX + // gnostore.(stypes.ClearThrougher).ClearThrough() + if false { + fmt.Println("XXXXXXXXXXXXXXXXXXXX") + gnostore.Print() + fmt.Println("XXXXXXXXXXXXXXXXXXXX") + } + if gno.ReGnoRunPath.MatchString(pkgPath) { return ErrInvalidPkgPath("reserved package name: " + pkgPath) } diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 9f6d6a05be0..b12b35c501a 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -535,14 +535,14 @@ func (e importNotFoundError) Error() string { return "import not found: " + stri // ImportFrom returns the imported package for the given import // path when imported by a package file located in dir. func (g *gnoImporter) ImportFrom(path, _ string, _ types.ImportMode) (*types.Package, error) { - fmt.Println("gnoImporter.ImportFrom", path) + // fmt.Println("gnoImporter.ImportFrom", path) if pkg, ok := g.cache[path]; ok { return pkg.pkg, pkg.err } - fmt.Println("gnoImporter.ImportFrom/getter.GetMemPackage", path) + // fmt.Println("gnoImporter.ImportFrom/getter.GetMemPackage", path) mpkg := g.getter.GetMemPackage(path) if mpkg == nil { - fmt.Println("gnoImporter.ImportFrom/was nil", path) + // fmt.Println("gnoImporter.ImportFrom/was nil", path) err := importNotFoundError(path) // XXX g.cache[path] = gnoImporterResult{err: err} return nil, err diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 6ccf7b91b42..2a40e3ae6d5 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -9,8 +9,12 @@ import ( "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 @@ -65,6 +69,8 @@ type Store interface { LogSwitchRealm(rlmpath string) // to mark change of realm boundaries ClearCache() Print() + Write() + Flush() } // Used to keep track of in-mem objects during tx. @@ -114,14 +120,14 @@ func (ds *defaultStore) SetPackageGetter(pg PackageGetter) { func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue { // helper to detect circular imports if isImport { - fmt.Println(">>>>>>>>>>> ? ", pkgPath) + // fmt.Println(">>>>>>>>>>> ? ", pkgPath) if slices.Contains(ds.current, pkgPath) { panic(fmt.Sprintf("import cycle detected: %q (through %v)", pkgPath, ds.current)) } - fmt.Println(">>>>>>>>>>> ADD", pkgPath) + // fmt.Println(">>>>>>>>>>> ADD", pkgPath) ds.current = append(ds.current, pkgPath) defer func() { - fmt.Println(">>>>>>>>>>> REMOVE", pkgPath) + // fmt.Println(">>>>>>>>>>> REMOVE", pkgPath) ds.current = ds.current[:len(ds.current)-1] }() } @@ -531,14 +537,14 @@ func (ds *defaultStore) incGetPackageIndexCounter() uint64 { } func (ds *defaultStore) AddMemPackage(memPkg *std.MemPackage) { - fmt.Println("defaultStore.AddMemPackage", memPkg.Path) + // fmt.Println("defaultStore.AddMemPackage", memPkg.Path) memPkg.Validate() // NOTE: duplicate validation. ctr := ds.incGetPackageIndexCounter() idxkey := []byte(backendPackageIndexKey(ctr)) bz := amino.MustMarshal(memPkg) ds.baseStore.Set(idxkey, []byte(memPkg.Path)) pathkey := []byte(backendPackagePathKey(memPkg.Path)) - fmt.Printf("defaultStore.AddMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) + // fmt.Printf("defaultStore.AddMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) ds.iavlStore.Set(pathkey, bz) } @@ -550,8 +556,16 @@ func (ds *defaultStore) GetMemPackage(path string) *std.MemPackage { func (ds *defaultStore) getMemPackage(path string, isRetry bool) *std.MemPackage { pathkey := []byte(backendPackagePathKey(path)) + if false { // XXX + fmt.Println("defaultStore.getMemPackage ========================0") + fmt.Println("defaultStore.getMemPackage", path, "isRetry", isRetry) + if ps, ok := ds.iavlStore.(types.Printer); ok { + ps.Print() + } + fmt.Println("defaultStore.getMemPackage ========================0 END") + } bz := ds.iavlStore.Get(pathkey) - fmt.Printf("defaultStore.getMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) + // fmt.Printf("defaultStore.getMemPackage iavlStore %p %d\n", ds.iavlStore, len(bz)) if bz == nil { // If this is the first try, attempt using GetPackage to retrieve the // package, first. GetPackage can leverage pkgGetter, which in most @@ -560,11 +574,47 @@ func (ds *defaultStore) getMemPackage(path string, isRetry bool) *std.MemPackage // Some packages may never be persisted, thus why we only attempt this twice. if !isRetry { if pv := ds.GetPackage(path, false); pv != nil { + if false { // XXX + fmt.Println("defaultStore.getMemPackage============1 not retry") + if ps, ok := ds.iavlStore.(types.Printer); ok { + ps.Print() + } + fmt.Println("defaultStore.getMemPackage============1 not retry ENDPRINT") + } + // XXX XXX XXX hmmmmmmmm unauthorized error? + //ds.iavlStore.(types.WriteThrougher).WriteThrough(1) + //ds.baseStore.(types.WriteThrougher).WriteThrough(2) + // ds.Flush() + // XXX clear cache layers. + if cts, ok := ds.iavlStore.(types.ClearThrougher); ok { + cts.ClearThrough() + } + if cts, ok := ds.baseStore.(types.ClearThrougher); ok { + cts.ClearThrough() + } + if false { // XXX + fmt.Println("defaultStore.getMemPackage============2 not retry") + if ps, ok := ds.iavlStore.(types.Printer); ok { + ps.Print() + } + fmt.Println("defaultStore.getMemPackage============2 not retry ENDPRINT") + } + /* + if fs, ok := ds.iavlStore.(Forgetter); ok { + fmt.Println("FORGET") + fs.Forget(pathkey) + } else { + fmt.Println("NOTFORGET", reflect.TypeOf(ds.iavlStore)) + } + */ return ds.getMemPackage(path, true) } } return nil } + if false { + fmt.Println("defaultStore.getMemPackage ======================= ENDPRINT") + } var memPkg *std.MemPackage amino.MustUnmarshal(bz, &memPkg) return memPkg @@ -652,6 +702,8 @@ func (ds *defaultStore) Fork() Store { opslog: nil, current: nil, } + //fmt.Printf("defaultStore.Fork %p .iavlStore %p\n", ds, ds.iavlStore) + //dbm.PrintStack() ds2.SetCachePackage(Uverse()) return ds2 } @@ -677,8 +729,16 @@ func (ds *defaultStore) GetNative(pkgPath string, name Name) func(m *Machine) { 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() } // ---------------------------------------- @@ -760,22 +820,25 @@ func (ds *defaultStore) ClearCache() { // 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("//----------------------------------------")) } // ---------------------------------------- diff --git a/tm2/pkg/crypto/keys/client/maketx.go b/tm2/pkg/crypto/keys/client/maketx.go index 2afccf9141c..ea5d8fb5a72 100644 --- a/tm2/pkg/crypto/keys/client/maketx.go +++ b/tm2/pkg/crypto/keys/client/maketx.go @@ -163,6 +163,18 @@ func SignAndBroadcastHandler( return nil, fmt.Errorf("unable to sign transaction, %w", err) } + /* + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(string(qres.Response.Data)) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) + */ + // broadcast signed tx bopts := &BroadcastCfg{ RootCfg: baseopts, diff --git a/tm2/pkg/db/goleveldb/go_level_db.go b/tm2/pkg/db/goleveldb/go_level_db.go index 27db61bb680..cbb9c5edc0b 100644 --- a/tm2/pkg/db/goleveldb/go_level_db.go +++ b/tm2/pkg/db/goleveldb/go_level_db.go @@ -6,8 +6,10 @@ import ( "fmt" "path/filepath" + "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/db/internal" + "github.com/gnolang/gno/tm2/pkg/strings" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -115,9 +117,9 @@ func (db *GoLevelDB) Print() { itr := db.db.NewIterator(nil, nil) for itr.Next() { - key := itr.Key() - value := itr.Value() - fmt.Printf("[%X]:\t[%X]\n", key, value) + key := colors.ColoredBytes([]byte(strings.TrimN(string(itr.Key()), 50)), colors.Blue, colors.Green) + value := colors.ColoredBytes([]byte(strings.TrimN(string(itr.Value()), 50)), colors.Blue, colors.Green) + fmt.Printf("%v:\t%v\n", key, value) } } diff --git a/tm2/pkg/db/memdb/mem_db.go b/tm2/pkg/db/memdb/mem_db.go index 6d1d6a35af9..64d4ff7386d 100644 --- a/tm2/pkg/db/memdb/mem_db.go +++ b/tm2/pkg/db/memdb/mem_db.go @@ -5,6 +5,7 @@ import ( "sort" "sync" + "github.com/gnolang/gno/tm2/pkg/colors" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/db/internal" "github.com/gnolang/gno/tm2/pkg/strings" @@ -128,16 +129,20 @@ func (db *MemDB) Print() { for key, value := range db.db { var keystr, valstr string - if strings.IsASCIIText(key) { - keystr = key - } else { - keystr = fmt.Sprintf("0x%X", []byte(key)) - } - if strings.IsASCIIText(string(value)) { - valstr = string(value) - } else { - valstr = fmt.Sprintf("0x%X", value) - } + keystr = colors.ColoredBytes([]byte(strings.TrimN(string(key), 50)), colors.Green, colors.Blue) + valstr = colors.ColoredBytes([]byte(strings.TrimN(string(value), 50)), colors.Green, colors.Blue) + /* + if strings.IsASCIIText(key) { + keystr = key + } else { + keystr = fmt.Sprintf("0x%X", []byte(key)) + } + if strings.IsASCIIText(string(value)) { + valstr = string(value) + } else { + valstr = fmt.Sprintf("0x%X", value) + } + */ fmt.Printf("%s:\t%s\n", keystr, valstr) } } diff --git a/tm2/pkg/db/prefix_db.go b/tm2/pkg/db/prefix_db.go index 29ed53639e8..cad0db3685a 100644 --- a/tm2/pkg/db/prefix_db.go +++ b/tm2/pkg/db/prefix_db.go @@ -4,6 +4,8 @@ import ( "bytes" "fmt" "sync" + + "github.com/gnolang/gno/tm2/pkg/colors" ) // IteratePrefix is a convenience function for iterating over a key domain @@ -188,15 +190,10 @@ func (pdb *PrefixDB) Close() { // Implements DB. func (pdb *PrefixDB) Print() { - fmt.Printf("prefix: %X\n", pdb.prefix) - - itr := pdb.Iterator(nil, nil) - defer itr.Close() - for ; itr.Valid(); itr.Next() { - key := itr.Key() - value := itr.Value() - fmt.Printf("[%X]:\t[%X]\n", key, value) - } + fmt.Println(colors.Blue("prefix ---------------------")) + fmt.Printf("prefix: %v\n", colors.ColoredBytes(pdb.prefix, colors.Green, colors.Blue)) + pdb.db.Print() + fmt.Println(colors.Blue("prefix --------------------- end")) } // Implements DB. diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index 5066a7b1fde..7286017842d 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -157,6 +157,27 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature if err != nil { return newCtx, res, true } + /* + getCustomSignBytes = func(n uint64) []byte { + signBytes, err := GetSignBytes2(newCtx.ChainID(), tx, sacc, isGenesis, n) + if err != nil { + panic("should not happen") + } + return signBytes + } + */ + + /* + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println("expected sequence", sacc.GetSequence()) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + */ signerAccs[i], res = processSig(newCtx, sacc, stdSigs[i], signBytes, simulate, params, sigGasConsumer) if !res.IsOK() { @@ -249,6 +270,18 @@ func processSig( panic(err) } + /* + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println("incremented sequence to", acc.GetSequence(), simulate) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) + */ + return acc, res } @@ -452,6 +485,24 @@ func GetSignBytes(chainID string, tx std.Tx, acc std.Account, genesis bool) ([]b ) } +func GetSignBytes2(chainID string, tx std.Tx, acc std.Account, genesis bool, seq uint64) ([]byte, error) { + var accNum uint64 + if !genesis { + accNum = acc.GetAccountNumber() + } + + return std.GetSignaturePayload( + std.SignDoc{ + ChainID: chainID, + AccountNumber: accNum, + Sequence: seq, + Fee: tx.Fee, + Msgs: tx.Msgs, + Memo: tx.Memo, + }, + ) +} + func abciResult(err error) sdk.Result { return sdk.ABCIResultFromError(err) } diff --git a/tm2/pkg/sdk/auth/handler.go b/tm2/pkg/sdk/auth/handler.go index 5636c7b8825..9a7f58d5668 100644 --- a/tm2/pkg/sdk/auth/handler.go +++ b/tm2/pkg/sdk/auth/handler.go @@ -68,6 +68,24 @@ func (ah authHandler) queryAccount(ctx sdk.Context, req abci.RequestQuery) (res return } + /* + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(b32addr, string(bz)) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) + */ + res.Data = bz return } diff --git a/tm2/pkg/sdk/baseapp.go b/tm2/pkg/sdk/baseapp.go index f7c7f5c73b0..08b3db1ded6 100644 --- a/tm2/pkg/sdk/baseapp.go +++ b/tm2/pkg/sdk/baseapp.go @@ -615,6 +615,9 @@ func (app *BaseApp) getContextForTx(mode RunTxMode, txBytes []byte) (ctx Context WithVoteInfos(app.voteInfos). WithConsensusParams(app.consensusParams) + // NOTE: This is especially required to simulate transactions because + // otherwise baseapp writes the antehandler mods (sequence and balance) + // to the underlying store for deliver and checktx. if mode == RunTxModeSimulate { ctx, _ = ctx.CacheContext() } @@ -835,6 +838,8 @@ func (app *BaseApp) runTx(mode RunTxMode, txBytes []byte, tx Tx) (result Result) } // only update state if all messages pass + // XXX + //if mode != RunTxModeSimulate && result.IsOK() { if result.IsOK() { msCache.MultiWrite() } diff --git a/tm2/pkg/store/cache/store.go b/tm2/pkg/store/cache/store.go index 1c5c2e27783..35b1116ea7e 100644 --- a/tm2/pkg/store/cache/store.go +++ b/tm2/pkg/store/cache/store.go @@ -3,13 +3,18 @@ package cache import ( "bytes" "container/list" + "fmt" + "reflect" "sort" "sync" + "github.com/gnolang/gno/tm2/pkg/colors" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/std" + "github.com/gnolang/gno/tm2/pkg/strings" "github.com/gnolang/gno/tm2/pkg/store/types" + "github.com/gnolang/gno/tm2/pkg/store/utils" ) // If value is nil but deleted is false, it means the parent doesn't have the @@ -20,6 +25,12 @@ type cValue struct { dirty bool } +func (cv cValue) String() string { + return fmt.Sprintf("cValue{%s,%v,%v}", + string(colors.ColoredBytes(cv.value, colors.Blue, colors.Green)), + cv.value, cv.deleted, cv.dirty) +} + // cacheStore wraps an in-memory cache around an underlying types.Store. type cacheStore struct { mtx sync.Mutex @@ -32,12 +43,17 @@ type cacheStore struct { var _ types.Store = (*cacheStore)(nil) func New(parent types.Store) *cacheStore { - return &cacheStore{ + cs := &cacheStore{ cache: make(map[string]*cValue), unsortedCache: make(map[string]struct{}), sortedCache: list.New(), parent: parent, } + // XXX + //fmt.Printf("======= NEW CACHESTORE ======= %p(%p)\n", cs, parent) + //debug.PrintStack() + //fmt.Printf("======= NEW CACHESTORE ======= %p(%p)\n", cs, parent) + return cs } // Implements types.Store. @@ -112,11 +128,58 @@ func (store *cacheStore) Write() { } // Clear the cache + store.clear() +} + +func (store *cacheStore) WriteThrough(n int) { + if n <= 0 { + panic("should not happen") + } + store.Write() + if n >= 2 { + store.parent.(types.WriteThrougher).WriteThrough(n - 1) + } +} + +func (store *cacheStore) Flush() { + store.Write() + if fs, ok := store.parent.(types.Flusher); ok { + fs.Flush() + } +} + +func (store *cacheStore) clear() { store.cache = make(map[string]*cValue) store.unsortedCache = make(map[string]struct{}) store.sortedCache = list.New() } +func (store *cacheStore) clearClean() { + for key, cvalue := range store.cache { + if !cvalue.dirty { + delete(store.cache, key) + delete(store.unsortedCache, key) + } + // XXX delete from sortedCache too. + } +} + +// Clears the cache. If true, clears parent recursively +// for all cache wraps. +func (store *cacheStore) ClearThrough() { + store.mtx.Lock() + defer store.mtx.Unlock() + + // Clear the cache + // XXX clear vs clearClean + store.clearClean() + + // Clear parents recursively. + if cts, ok := store.parent.(types.ClearThrougher); ok { + cts.ClearThrough() + } +} + // ---------------------------------------- // To cache-wrap this Store further. @@ -209,3 +272,24 @@ func (store *cacheStore) setCacheValue(key, value []byte, deleted bool, dirty bo store.unsortedCache[string(key)] = struct{}{} } } + +func (store *cacheStore) Print() { + fmt.Println(colors.Cyan("cacheStore.Print"), fmt.Sprintf("%p", store)) + for key, value := range store.cache { + fmt.Println(colors.Yellow(key), + string(colors.ColoredBytes([]byte(strings.TrimN(string(value.value), 550)), colors.Green, colors.Blue)), + "deleted", value.deleted, + "dirty", value.dirty, + ) + } + fmt.Println(colors.Cyan("cacheStore.Print"), fmt.Sprintf("%p", store), + "print parent", fmt.Sprintf("%p", store.parent), reflect.TypeOf(store.parent)) + if ps, ok := store.parent.(types.Printer); ok { + ps.Print() + } else { + utils.Print(store.parent) + //x := store.parent.Get([]byte("pkg:time")) + //fmt.Println("store.parent.Get('pkg:time') =", x) + } + fmt.Println(colors.Cyan("cacheStore.Print END"), fmt.Sprintf("%p", store)) +} diff --git a/tm2/pkg/store/gas/store.go b/tm2/pkg/store/gas/store.go index 4ffe46dc275..8d434c2cfb5 100644 --- a/tm2/pkg/store/gas/store.go +++ b/tm2/pkg/store/gas/store.go @@ -2,6 +2,7 @@ package gas import ( "github.com/gnolang/gno/tm2/pkg/store/types" + "github.com/gnolang/gno/tm2/pkg/store/utils" "github.com/gnolang/overflow" ) @@ -100,6 +101,39 @@ func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator { return gi } +func (gs *Store) Print() { + if ps, ok := gs.parent.(types.Printer); ok { + ps.Print() + } else { + utils.Print(gs.parent) + } +} + +// XXX? +func (gs *Store) ClearThrough() { + if cts, ok := gs.parent.(types.ClearThrougher); ok { + cts.ClearThrough() + } else { + panic("underlying store does not implement ClearThrough()") + } +} + +func (gs *Store) Flush() { + if cts, ok := gs.parent.(types.Flusher); ok { + cts.Flush() + } else { + panic("underlying store does not implement Flush()") + } +} + +func (gs *Store) WriteThrough(n int) { + if wts, ok := gs.parent.(types.WriteThrougher); ok { + wts.WriteThrough(n) + } else { + panic("underlying store does not implement WriteThrough()") + } +} + type gasIterator struct { gasMeter types.GasMeter gasConfig types.GasConfig diff --git a/tm2/pkg/store/store.go b/tm2/pkg/store/store.go index 2950937f951..ba939fa1793 100644 --- a/tm2/pkg/store/store.go +++ b/tm2/pkg/store/store.go @@ -1,10 +1,7 @@ package store import ( - "fmt" - dbm "github.com/gnolang/gno/tm2/pkg/db" - "github.com/gnolang/gno/tm2/pkg/strings" "github.com/gnolang/gno/tm2/pkg/store/rootmulti" "github.com/gnolang/gno/tm2/pkg/store/types" @@ -27,26 +24,3 @@ func NewPruningOptionsFromString(strategy string) (opt PruningOptions) { } return } - -// TODO move to another file. -func Print(store Store) { - fmt.Println("//----------------------------------------") - fmt.Println("// store:", store) - itr := store.Iterator(nil, nil) - defer itr.Close() - for ; itr.Valid(); itr.Next() { - key, value := itr.Key(), itr.Value() - var keystr, valuestr string - if strings.IsASCIIText(string(key)) { - keystr = string(key) - } else { - keystr = fmt.Sprintf("0x%X", key) - } - if strings.IsASCIIText(string(value)) { - valuestr = string(value) - } else { - valuestr = fmt.Sprintf("0x%X", value) - } - fmt.Printf("%s: %s\n", keystr, valuestr) - } -} diff --git a/tm2/pkg/store/types/store.go b/tm2/pkg/store/types/store.go index 68ee110586c..f8e3eced7d7 100644 --- a/tm2/pkg/store/types/store.go +++ b/tm2/pkg/store/types/store.go @@ -40,7 +40,8 @@ type Store interface { // Returns a cache-wrapped store. CacheWrap() Store - // If cache-wrapped store, flushes to underlying store. + // If cache-wrapped store, writes to underlying store. + // Does not writes through layers of cache. Write() } @@ -55,6 +56,30 @@ type Queryable interface { Query(abci.RequestQuery) abci.ResponseQuery } +type Printer interface { + Print() +} + +type WriteThrougher interface { + WriteThrough(int) +} + +// Can be called to clear empty reads all caches. +// NOTE: currently only works for *CacheStore +type ClearThrougher interface { + ClearThrough() +} + +// Can be called to write through all caches. +// NOTE: currently only works for *CacheStore +type Flusher interface { + Flush() +} + +type Writer interface { + Write() +} + // ---------------------------------------- // MultiStore diff --git a/tm2/pkg/store/utils/print.go b/tm2/pkg/store/utils/print.go new file mode 100644 index 00000000000..e133f3f9dfb --- /dev/null +++ b/tm2/pkg/store/utils/print.go @@ -0,0 +1,40 @@ +package utils + +import ( + "fmt" + "reflect" + + "github.com/gnolang/gno/tm2/pkg/colors" + "github.com/gnolang/gno/tm2/pkg/store/types" +) + +type Printer interface { + Print() +} + +// TODO move to another file. +func Print(store types.Store) { + fmt.Println(colors.Blue("//----------------------------------------")) + if ps, ok := store.(Printer); ok { + ps.Print() + } else { + fmt.Println(colors.Blue(fmt.Sprintf("// store:%p %v", store, reflect.TypeOf(store)))) + itr := store.Iterator(nil, nil) + defer itr.Close() + for ; itr.Valid(); itr.Next() { + key, value := itr.Key(), itr.Value() + var keystr, valuestr string + keystr = string(colors.ColoredBytes(key, colors.Green, colors.Blue)) + valuestr = fmt.Sprintf("(%d)", len(value)) + /* + if true || strings.IsASCIIText(string(value)) { + valuestr = string(value) + } else { + valuestr = fmt.Sprintf("0x%X", value) + } + */ + fmt.Printf("%s: %s\n", keystr, valuestr) + } + } + fmt.Println(colors.Blue("//------------------------------------ end")) +} diff --git a/tm2/pkg/strings/string.go b/tm2/pkg/strings/string.go index 46675923319..86ed70373a3 100644 --- a/tm2/pkg/strings/string.go +++ b/tm2/pkg/strings/string.go @@ -75,3 +75,12 @@ func StringSliceEqual(a, b []string) bool { } return true } + +// TrimN naively appens "..." to fit within n bytes. +func TrimN(s string, n int) string { + if len(s) <= n { + return s + } else { + return s[:n-3] + "..." + } +} From d7134dbe3d23ea7bbb02ca6e363127d97d54d24b Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 13:45:57 -0700 Subject: [PATCH 08/14] revert cache invalidation stuff --- .gitignore | 1 + gno.land/pkg/gnoclient/client_txs.go | 13 ---- gno.land/pkg/gnoland/app.go | 29 +-------- .../pkg/integration/testing_integration.go | 3 - gno.land/pkg/sdk/vm/builtins.go | 19 +----- gno.land/pkg/sdk/vm/keeper.go | 15 ----- gnovm/pkg/gnolang/go2gno.go | 4 +- gnovm/pkg/gnolang/gonative_test.go | 2 +- gnovm/pkg/gnolang/store.go | 62 +++---------------- gnovm/tests/imports.go | 16 ++--- tm2/pkg/crypto/keys/client/maketx.go | 12 ---- tm2/pkg/db/memdb/mem_db.go | 12 ---- tm2/pkg/sdk/auth/ante.go | 52 ---------------- tm2/pkg/sdk/auth/handler.go | 18 ------ tm2/pkg/sdk/baseapp.go | 6 +- tm2/pkg/store/cache/store.go | 44 +------------ tm2/pkg/store/gas/store.go | 17 ----- tm2/pkg/store/types/store.go | 17 ++--- 18 files changed, 30 insertions(+), 312 deletions(-) diff --git a/.gitignore b/.gitignore index e622be821de..7d3f3f92b41 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ coverage.out *.swp *.swo +*.bak diff --git a/gno.land/pkg/gnoclient/client_txs.go b/gno.land/pkg/gnoclient/client_txs.go index 0523016deab..e306d737ede 100644 --- a/gno.land/pkg/gnoclient/client_txs.go +++ b/gno.land/pkg/gnoclient/client_txs.go @@ -295,19 +295,6 @@ func (c *Client) signAndBroadcastTxCommit(tx std.Tx, accountNumber, sequenceNumb sequenceNumber = account.Sequence } - /* - panic("WTF") - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(">>>", sequenceNumber, accountNumber) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - fmt.Println(colors.Cyan("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")) - */ - signCfg := SignCfg{ UnsignedTX: tx, SequenceNumber: sequenceNumber, diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index 1691079ff5c..8f5f33d0d9a 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -18,7 +18,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/store" "github.com/gnolang/gno/tm2/pkg/store/dbadapter" "github.com/gnolang/gno/tm2/pkg/store/iavl" - stypes "github.com/gnolang/gno/tm2/pkg/store/types" // Only goleveldb is supported for now. _ "github.com/gnolang/gno/tm2/pkg/db/_tags" @@ -57,10 +56,6 @@ func (c *AppOptions) validate() error { return nil } -type Printer interface { - Print() -} - // NewApp creates the GnoLand application. func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { if err := cfg.validate(); err != nil { @@ -127,18 +122,7 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) { // Initialize the VMKeeper. ms := baseApp.GetCacheMultiStore() vmKpr.Initialize(ms) - mainStore := ms.GetStore(mainKey) - baseStore := ms.GetStore(baseKey) - if false { // XXX - fmt.Println("========== INiTIALIZE") - if ps, ok := mainStore.(stypes.Printer); ok { - ps.Print() - } - if ps, ok := baseStore.(stypes.Printer); ok { - ps.Print() - } - fmt.Println("========== INiTIALIZE END") - } + ms.MultiWrite() // XXX why was't this needed? return baseApp, nil } @@ -214,17 +198,6 @@ func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank // XXX not used yet. func EndBlocker(vmk vm.VMKeeperI) func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { - /* - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - fmt.Println(colors.Yellow("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")) - */ return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock { return abci.ResponseEndBlock{} } diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 3b36fe580f9..654dda0b45e 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -226,9 +226,6 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { tsValidateError(ts, "gnokey", neg, err) } - // fmt.Println("sleeping 10 seconds") - //time.Sleep(10 * time.Second) - // Setup IO command io := commands.NewTestIO() io.SetOut(commands.WriteNopCloser(ts.Stdout())) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index bf2820e958f..bbeea35134c 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -1,13 +1,11 @@ package vm import ( - "fmt" "os" "path/filepath" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/stdlibs" - "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/crypto" osm "github.com/gnolang/gno/tm2/pkg/os" "github.com/gnolang/gno/tm2/pkg/sdk" @@ -18,15 +16,7 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { // NOTE: native functions/methods added here must be quick operations, // or account for gas before operation. // TODO: define criteria for inclusion, and solve gas calculations. - getPackage := func(pkgPath string) (pn *gno.PackageNode, pv *gno.PackageValue) { - /* - // fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage", pkgPath) - defer func() { - // fmt.Println("builtins/VMKeeper.initBuiltinPackagesAndTypes/getPackage returned nil?", pkgPath, pv == nil) - // XXX - // store.Print() - }() - */ + 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) @@ -48,13 +38,6 @@ func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { }) defer m2.Release() pn, pv = m2.RunMemPackage(memPkg, true) - // XXX - if false { - fmt.Println(colors.Red("VMKeeper.init.getPackage ========================================= before wrote")) - store.Print() - fmt.Println(colors.Red("VMKeeper.init.getPackage ========================================= before wrote end")) - } - store.Write() // XXX XXX XXX or flush? return } store.SetPackageGetter(getPackage) diff --git a/gno.land/pkg/sdk/vm/keeper.go b/gno.land/pkg/sdk/vm/keeper.go index 3e09af9da12..30ffcfa5172 100644 --- a/gno.land/pkg/sdk/vm/keeper.go +++ b/gno.land/pkg/sdk/vm/keeper.go @@ -17,7 +17,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/sdk/bank" "github.com/gnolang/gno/tm2/pkg/std" "github.com/gnolang/gno/tm2/pkg/store" - stypes "github.com/gnolang/gno/tm2/pkg/store/types" "github.com/gnolang/gno/tm2/pkg/telemetry" "github.com/gnolang/gno/tm2/pkg/telemetry/metrics" "go.opentelemetry.io/otel/attribute" @@ -98,11 +97,6 @@ func (vm *VMKeeper) Initialize(ms store.MultiStore) { m2.PreprocessAllFilesAndSaveBlockNodes() gno.EnableDebug() } - if false { // XXX - fmt.Println("AFTER INITIALIZE") - iavlSDKStore.(stypes.Printer).Print() - fmt.Println("AFTER INITIALIZE END") - } } func (vm *VMKeeper) getGnoStore(ctx sdk.Context) gno.Store { @@ -163,15 +157,6 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) { if pv := gnostore.GetPackage(pkgPath, false); pv != nil { return ErrInvalidPkgPath("package already exists: " + pkgPath) } - - // XXX - // gnostore.(stypes.ClearThrougher).ClearThrough() - if false { - fmt.Println("XXXXXXXXXXXXXXXXXXXX") - gnostore.Print() - fmt.Println("XXXXXXXXXXXXXXXXXXXX") - } - if gno.ReGnoRunPath.MatchString(pkgPath) { return ErrInvalidPkgPath("reserved package name: " + pkgPath) } diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index b12b35c501a..6873ddbf0a1 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -535,16 +535,14 @@ func (e importNotFoundError) Error() string { return "import not found: " + stri // ImportFrom returns the imported package for the given import // path when imported by a package file located in dir. func (g *gnoImporter) ImportFrom(path, _ string, _ types.ImportMode) (*types.Package, error) { - // fmt.Println("gnoImporter.ImportFrom", path) if pkg, ok := g.cache[path]; ok { return pkg.pkg, pkg.err } - // fmt.Println("gnoImporter.ImportFrom/getter.GetMemPackage", path) mpkg := g.getter.GetMemPackage(path) if mpkg == nil { // fmt.Println("gnoImporter.ImportFrom/was nil", path) err := importNotFoundError(path) - // XXX g.cache[path] = gnoImporterResult{err: err} + g.cache[path] = gnoImporterResult{err: err} return nil, err } result, err := g.parseCheckMemPackage(mpkg) diff --git a/gnovm/pkg/gnolang/gonative_test.go b/gnovm/pkg/gnolang/gonative_test.go index 33e687cd40a..42729b43699 100644 --- a/gnovm/pkg/gnolang/gonative_test.go +++ b/gnovm/pkg/gnolang/gonative_test.go @@ -15,7 +15,7 @@ import ( // and the odd index items are corresponding package values. func gonativeTestStore(args ...interface{}) Store { store := NewStore(nil, nil, nil) - store.SetPackageGetter(func(pkgPath string) (*PackageNode, *PackageValue) { + store.SetPackageGetter(func(pkgPath string, _ Store) (*PackageNode, *PackageValue) { for i := 0; i < len(args)/2; i++ { pn := args[i*2].(*PackageNode) pv := args[i*2+1].(*PackageValue) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 2a40e3ae6d5..20f116dc9d7 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -19,8 +19,12 @@ import ( // 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. -type PackageGetter func(pkgPath string) (*PackageNode, *PackageValue) +// 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). 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) type PackageInjector func(store Store, pn *PackageNode) @@ -120,14 +124,11 @@ func (ds *defaultStore) SetPackageGetter(pg PackageGetter) { func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue { // helper to detect circular imports if isImport { - // fmt.Println(">>>>>>>>>>> ? ", pkgPath) if slices.Contains(ds.current, pkgPath) { panic(fmt.Sprintf("import cycle detected: %q (through %v)", pkgPath, ds.current)) } - // fmt.Println(">>>>>>>>>>> ADD", pkgPath) ds.current = append(ds.current, pkgPath) defer func() { - // fmt.Println(">>>>>>>>>>> REMOVE", pkgPath) ds.current = ds.current[:len(ds.current)-1] }() } @@ -174,7 +175,7 @@ func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue } // otherwise, fetch from pkgGetter. if ds.pkgGetter != nil { - if pn, pv := ds.pkgGetter(pkgPath); pv != nil { + if pn, pv := ds.pkgGetter(pkgPath, ds); pv != nil { // e.g. tests/imports_tests loads example/gno.land/r/... realms. // if pv.IsRealm() { // panic("realm packages cannot be gotten from pkgGetter") @@ -537,14 +538,12 @@ func (ds *defaultStore) incGetPackageIndexCounter() uint64 { } func (ds *defaultStore) AddMemPackage(memPkg *std.MemPackage) { - // fmt.Println("defaultStore.AddMemPackage", memPkg.Path) memPkg.Validate() // NOTE: duplicate validation. ctr := ds.incGetPackageIndexCounter() idxkey := []byte(backendPackageIndexKey(ctr)) bz := amino.MustMarshal(memPkg) ds.baseStore.Set(idxkey, []byte(memPkg.Path)) pathkey := []byte(backendPackagePathKey(memPkg.Path)) - // fmt.Printf("defaultStore.AddMemPackage iavlStore %p %p\n", ds.iavlStore, len(bz)) ds.iavlStore.Set(pathkey, bz) } @@ -556,16 +555,7 @@ func (ds *defaultStore) GetMemPackage(path string) *std.MemPackage { func (ds *defaultStore) getMemPackage(path string, isRetry bool) *std.MemPackage { pathkey := []byte(backendPackagePathKey(path)) - if false { // XXX - fmt.Println("defaultStore.getMemPackage ========================0") - fmt.Println("defaultStore.getMemPackage", path, "isRetry", isRetry) - if ps, ok := ds.iavlStore.(types.Printer); ok { - ps.Print() - } - fmt.Println("defaultStore.getMemPackage ========================0 END") - } bz := ds.iavlStore.Get(pathkey) - // fmt.Printf("defaultStore.getMemPackage iavlStore %p %d\n", ds.iavlStore, len(bz)) if bz == nil { // If this is the first try, attempt using GetPackage to retrieve the // package, first. GetPackage can leverage pkgGetter, which in most @@ -574,47 +564,11 @@ func (ds *defaultStore) getMemPackage(path string, isRetry bool) *std.MemPackage // Some packages may never be persisted, thus why we only attempt this twice. if !isRetry { if pv := ds.GetPackage(path, false); pv != nil { - if false { // XXX - fmt.Println("defaultStore.getMemPackage============1 not retry") - if ps, ok := ds.iavlStore.(types.Printer); ok { - ps.Print() - } - fmt.Println("defaultStore.getMemPackage============1 not retry ENDPRINT") - } - // XXX XXX XXX hmmmmmmmm unauthorized error? - //ds.iavlStore.(types.WriteThrougher).WriteThrough(1) - //ds.baseStore.(types.WriteThrougher).WriteThrough(2) - // ds.Flush() - // XXX clear cache layers. - if cts, ok := ds.iavlStore.(types.ClearThrougher); ok { - cts.ClearThrough() - } - if cts, ok := ds.baseStore.(types.ClearThrougher); ok { - cts.ClearThrough() - } - if false { // XXX - fmt.Println("defaultStore.getMemPackage============2 not retry") - if ps, ok := ds.iavlStore.(types.Printer); ok { - ps.Print() - } - fmt.Println("defaultStore.getMemPackage============2 not retry ENDPRINT") - } - /* - if fs, ok := ds.iavlStore.(Forgetter); ok { - fmt.Println("FORGET") - fs.Forget(pathkey) - } else { - fmt.Println("NOTFORGET", reflect.TypeOf(ds.iavlStore)) - } - */ return ds.getMemPackage(path, true) } } return nil } - if false { - fmt.Println("defaultStore.getMemPackage ======================= ENDPRINT") - } var memPkg *std.MemPackage amino.MustUnmarshal(bz, &memPkg) return memPkg @@ -702,8 +656,6 @@ func (ds *defaultStore) Fork() Store { opslog: nil, current: nil, } - //fmt.Printf("defaultStore.Fork %p .iavlStore %p\n", ds, ds.iavlStore) - //dbm.PrintStack() ds2.SetCachePackage(Uverse()) return ds2 } diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index 0464da806de..3dbd292ea68 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -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) (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")) } @@ -402,15 +402,15 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri } 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) return } diff --git a/tm2/pkg/crypto/keys/client/maketx.go b/tm2/pkg/crypto/keys/client/maketx.go index ea5d8fb5a72..2afccf9141c 100644 --- a/tm2/pkg/crypto/keys/client/maketx.go +++ b/tm2/pkg/crypto/keys/client/maketx.go @@ -163,18 +163,6 @@ func SignAndBroadcastHandler( return nil, fmt.Errorf("unable to sign transaction, %w", err) } - /* - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(string(qres.Response.Data)) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - fmt.Println(colors.Green("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")) - */ - // broadcast signed tx bopts := &BroadcastCfg{ RootCfg: baseopts, diff --git a/tm2/pkg/db/memdb/mem_db.go b/tm2/pkg/db/memdb/mem_db.go index 64d4ff7386d..4645d4ed19e 100644 --- a/tm2/pkg/db/memdb/mem_db.go +++ b/tm2/pkg/db/memdb/mem_db.go @@ -131,18 +131,6 @@ func (db *MemDB) Print() { var keystr, valstr string keystr = colors.ColoredBytes([]byte(strings.TrimN(string(key), 50)), colors.Green, colors.Blue) valstr = colors.ColoredBytes([]byte(strings.TrimN(string(value), 50)), colors.Green, colors.Blue) - /* - if strings.IsASCIIText(key) { - keystr = key - } else { - keystr = fmt.Sprintf("0x%X", []byte(key)) - } - if strings.IsASCIIText(string(value)) { - valstr = string(value) - } else { - valstr = fmt.Sprintf("0x%X", value) - } - */ fmt.Printf("%s:\t%s\n", keystr, valstr) } } diff --git a/tm2/pkg/sdk/auth/ante.go b/tm2/pkg/sdk/auth/ante.go index 7286017842d..49662b47a55 100644 --- a/tm2/pkg/sdk/auth/ante.go +++ b/tm2/pkg/sdk/auth/ante.go @@ -157,28 +157,6 @@ func NewAnteHandler(ak AccountKeeper, bank BankKeeperI, sigGasConsumer Signature if err != nil { return newCtx, res, true } - /* - getCustomSignBytes = func(n uint64) []byte { - signBytes, err := GetSignBytes2(newCtx.ChainID(), tx, sacc, isGenesis, n) - if err != nil { - panic("should not happen") - } - return signBytes - } - */ - - /* - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println("expected sequence", sacc.GetSequence()) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - */ - signerAccs[i], res = processSig(newCtx, sacc, stdSigs[i], signBytes, simulate, params, sigGasConsumer) if !res.IsOK() { return newCtx, res, true @@ -270,18 +248,6 @@ func processSig( panic(err) } - /* - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println("incremented sequence to", acc.GetSequence(), simulate) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - fmt.Println(colors.Blue("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")) - */ - return acc, res } @@ -485,24 +451,6 @@ func GetSignBytes(chainID string, tx std.Tx, acc std.Account, genesis bool) ([]b ) } -func GetSignBytes2(chainID string, tx std.Tx, acc std.Account, genesis bool, seq uint64) ([]byte, error) { - var accNum uint64 - if !genesis { - accNum = acc.GetAccountNumber() - } - - return std.GetSignaturePayload( - std.SignDoc{ - ChainID: chainID, - AccountNumber: accNum, - Sequence: seq, - Fee: tx.Fee, - Msgs: tx.Msgs, - Memo: tx.Memo, - }, - ) -} - func abciResult(err error) sdk.Result { return sdk.ABCIResultFromError(err) } diff --git a/tm2/pkg/sdk/auth/handler.go b/tm2/pkg/sdk/auth/handler.go index 9a7f58d5668..5636c7b8825 100644 --- a/tm2/pkg/sdk/auth/handler.go +++ b/tm2/pkg/sdk/auth/handler.go @@ -68,24 +68,6 @@ func (ah authHandler) queryAccount(ctx sdk.Context, req abci.RequestQuery) (res return } - /* - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(b32addr, string(bz)) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - fmt.Println(colors.Red("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")) - */ - res.Data = bz return } diff --git a/tm2/pkg/sdk/baseapp.go b/tm2/pkg/sdk/baseapp.go index 08b3db1ded6..a36b6b6f139 100644 --- a/tm2/pkg/sdk/baseapp.go +++ b/tm2/pkg/sdk/baseapp.go @@ -223,6 +223,10 @@ func (app *BaseApp) GetCacheMultiStore() store.MultiStore { return app.cms.MultiCacheWrap() } +func (app *BaseApp) GetCMS() store.CommitMultiStore { + return app.cms +} + // Router returns the router of the BaseApp. func (app *BaseApp) Router() Router { if app.sealed { @@ -838,8 +842,6 @@ func (app *BaseApp) runTx(mode RunTxMode, txBytes []byte, tx Tx) (result Result) } // only update state if all messages pass - // XXX - //if mode != RunTxModeSimulate && result.IsOK() { if result.IsOK() { msCache.MultiWrite() } diff --git a/tm2/pkg/store/cache/store.go b/tm2/pkg/store/cache/store.go index 35b1116ea7e..91530861303 100644 --- a/tm2/pkg/store/cache/store.go +++ b/tm2/pkg/store/cache/store.go @@ -49,10 +49,6 @@ func New(parent types.Store) *cacheStore { sortedCache: list.New(), parent: parent, } - // XXX - //fmt.Printf("======= NEW CACHESTORE ======= %p(%p)\n", cs, parent) - //debug.PrintStack() - //fmt.Printf("======= NEW CACHESTORE ======= %p(%p)\n", cs, parent) return cs } @@ -131,16 +127,6 @@ func (store *cacheStore) Write() { store.clear() } -func (store *cacheStore) WriteThrough(n int) { - if n <= 0 { - panic("should not happen") - } - store.Write() - if n >= 2 { - store.parent.(types.WriteThrougher).WriteThrough(n - 1) - } -} - func (store *cacheStore) Flush() { store.Write() if fs, ok := store.parent.(types.Flusher); ok { @@ -154,32 +140,6 @@ func (store *cacheStore) clear() { store.sortedCache = list.New() } -func (store *cacheStore) clearClean() { - for key, cvalue := range store.cache { - if !cvalue.dirty { - delete(store.cache, key) - delete(store.unsortedCache, key) - } - // XXX delete from sortedCache too. - } -} - -// Clears the cache. If true, clears parent recursively -// for all cache wraps. -func (store *cacheStore) ClearThrough() { - store.mtx.Lock() - defer store.mtx.Unlock() - - // Clear the cache - // XXX clear vs clearClean - store.clearClean() - - // Clear parents recursively. - if cts, ok := store.parent.(types.ClearThrougher); ok { - cts.ClearThrough() - } -} - // ---------------------------------------- // To cache-wrap this Store further. @@ -277,7 +237,7 @@ func (store *cacheStore) Print() { fmt.Println(colors.Cyan("cacheStore.Print"), fmt.Sprintf("%p", store)) for key, value := range store.cache { fmt.Println(colors.Yellow(key), - string(colors.ColoredBytes([]byte(strings.TrimN(string(value.value), 550)), colors.Green, colors.Blue)), + string(colors.ColoredBytes([]byte(strings.TrimN(string(value.value), 200)), colors.Green, colors.Blue)), "deleted", value.deleted, "dirty", value.dirty, ) @@ -288,8 +248,6 @@ func (store *cacheStore) Print() { ps.Print() } else { utils.Print(store.parent) - //x := store.parent.Get([]byte("pkg:time")) - //fmt.Println("store.parent.Get('pkg:time') =", x) } fmt.Println(colors.Cyan("cacheStore.Print END"), fmt.Sprintf("%p", store)) } diff --git a/tm2/pkg/store/gas/store.go b/tm2/pkg/store/gas/store.go index 8d434c2cfb5..db5ea7a79b0 100644 --- a/tm2/pkg/store/gas/store.go +++ b/tm2/pkg/store/gas/store.go @@ -109,15 +109,6 @@ func (gs *Store) Print() { } } -// XXX? -func (gs *Store) ClearThrough() { - if cts, ok := gs.parent.(types.ClearThrougher); ok { - cts.ClearThrough() - } else { - panic("underlying store does not implement ClearThrough()") - } -} - func (gs *Store) Flush() { if cts, ok := gs.parent.(types.Flusher); ok { cts.Flush() @@ -126,14 +117,6 @@ func (gs *Store) Flush() { } } -func (gs *Store) WriteThrough(n int) { - if wts, ok := gs.parent.(types.WriteThrougher); ok { - wts.WriteThrough(n) - } else { - panic("underlying store does not implement WriteThrough()") - } -} - type gasIterator struct { gasMeter types.GasMeter gasConfig types.GasConfig diff --git a/tm2/pkg/store/types/store.go b/tm2/pkg/store/types/store.go index f8e3eced7d7..a0e3925a265 100644 --- a/tm2/pkg/store/types/store.go +++ b/tm2/pkg/store/types/store.go @@ -56,26 +56,19 @@ type Queryable interface { Query(abci.RequestQuery) abci.ResponseQuery } +// Useful for debugging. type Printer interface { Print() } -type WriteThrougher interface { - WriteThrough(int) -} - -// Can be called to clear empty reads all caches. -// NOTE: currently only works for *CacheStore -type ClearThrougher interface { - ClearThrough() -} - -// Can be called to write through all caches. -// NOTE: currently only works for *CacheStore +// Write through all caches. +// You probably don't want this, rather write your program to Write() where +// appropriate. Not included in the main Store interface to discourage usage. type Flusher interface { Flush() } +// Write throgh type Writer interface { Write() } From 395e263277725d9740e2b4144d980d58eac7b827 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 14:45:31 -0700 Subject: [PATCH 09/14] improve colored bytes (faster) --- gnovm/pkg/gnolang/go2gno.go | 1 - tm2/pkg/colors/colors.go | 74 +++++++++++++++++++++++++++-- tm2/pkg/db/goleveldb/go_level_db.go | 7 ++- tm2/pkg/db/memdb/mem_db.go | 7 ++- tm2/pkg/db/prefix_db.go | 2 +- tm2/pkg/store/cache/store.go | 8 ++-- tm2/pkg/store/utils/print.go | 6 ++- 7 files changed, 85 insertions(+), 20 deletions(-) diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 6873ddbf0a1..d9c59c89541 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -540,7 +540,6 @@ func (g *gnoImporter) ImportFrom(path, _ string, _ types.ImportMode) (*types.Pac } mpkg := g.getter.GetMemPackage(path) if mpkg == nil { - // fmt.Println("gnoImporter.ImportFrom/was nil", path) err := importNotFoundError(path) g.cache[path] = gnoImporterResult{err: err} return nil, err diff --git a/tm2/pkg/colors/colors.go b/tm2/pkg/colors/colors.go index d2af56c892a..84a0353d880 100644 --- a/tm2/pkg/colors/colors.go +++ b/tm2/pkg/colors/colors.go @@ -96,14 +96,80 @@ func Gray(args ...interface{}) string { return treatAll(ANSIFgGray, args...) } -func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string { +// result may be 4 ASNSII chars longer than they should be to denote the +// elipses (...), and one for a trailing hex nibble in case the last byte is +// non-ascii. +// NOTE: it is annoying to try make this perfect and always fit within n, so we +// don't do this yet, but left as an exercise. :) +func ColoredBytesN(data []byte, n int, textColor, bytesColor func(...interface{}) string) string { + _n := 0 s := "" - for _, b := range data { + buf := "" // buffer + bufIsText := true // is buf text or hex + for i, b := range data { + RESTART: if 0x21 <= b && b < 0x7F { - s += textColor(string(b)) + if !bufIsText { + s += bytesColor(buf) + buf = "" + bufIsText = true + goto RESTART + } + buf += string(b) + _n += 1 + if n != 0 && _n >= n { + if i == len(data)-1 { + // done + s += textColor(buf) + buf = "" + } else { + s += textColor(buf) + "..." + buf = "" + } + break + } + } else { + if bufIsText { + s += textColor(buf) + buf = "" + bufIsText = false + goto RESTART + } + buf += fmt.Sprintf("%02X", b) + _n += 2 + if n != 0 && _n >= n { + if i == len(data)-1 { + // done + s += bytesColor(buf) + buf = "" + } else { + s += bytesColor(buf) + "..." + buf = "" + } + break + } + } + } + if buf != "" { + if bufIsText { + s += textColor(buf) + buf = "" } else { - s += bytesColor(fmt.Sprintf("%02X", b)) + s += bytesColor(buf) + buf = "" } } return s } + +func DefaultColoredBytesN(data []byte, n int) string { + return ColoredBytesN(data, n, Blue, Green) +} + +func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string { + return ColoredBytesN(data, 0, textColor, bytesColor) +} + +func DefaultColoredBytes(data []byte) string { + return ColoredBytes(data, Blue, Green) +} diff --git a/tm2/pkg/db/goleveldb/go_level_db.go b/tm2/pkg/db/goleveldb/go_level_db.go index cbb9c5edc0b..09d450dc5da 100644 --- a/tm2/pkg/db/goleveldb/go_level_db.go +++ b/tm2/pkg/db/goleveldb/go_level_db.go @@ -9,7 +9,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/db/internal" - "github.com/gnolang/gno/tm2/pkg/strings" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -117,9 +116,9 @@ func (db *GoLevelDB) Print() { itr := db.db.NewIterator(nil, nil) for itr.Next() { - key := colors.ColoredBytes([]byte(strings.TrimN(string(itr.Key()), 50)), colors.Blue, colors.Green) - value := colors.ColoredBytes([]byte(strings.TrimN(string(itr.Value()), 50)), colors.Blue, colors.Green) - fmt.Printf("%v:\t%v\n", key, value) + key := colors.DefaultColoredBytesN(itr.Key(), 50) + value := colors.DefaultColoredBytes(itr.Value(), 100) + fmt.Printf("%v: %v\n", key, value) } } diff --git a/tm2/pkg/db/memdb/mem_db.go b/tm2/pkg/db/memdb/mem_db.go index 4645d4ed19e..09b90b6be44 100644 --- a/tm2/pkg/db/memdb/mem_db.go +++ b/tm2/pkg/db/memdb/mem_db.go @@ -8,7 +8,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/colors" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/db/internal" - "github.com/gnolang/gno/tm2/pkg/strings" ) func init() { @@ -129,9 +128,9 @@ func (db *MemDB) Print() { for key, value := range db.db { var keystr, valstr string - keystr = colors.ColoredBytes([]byte(strings.TrimN(string(key), 50)), colors.Green, colors.Blue) - valstr = colors.ColoredBytes([]byte(strings.TrimN(string(value), 50)), colors.Green, colors.Blue) - fmt.Printf("%s:\t%s\n", keystr, valstr) + keystr = colors.DefaultColoredBytesN([]byte(key), 50) + valstr = colors.DefaultColoredBytesN(value, 100) + fmt.Printf("%s: %s\n", keystr, valstr) } } diff --git a/tm2/pkg/db/prefix_db.go b/tm2/pkg/db/prefix_db.go index cad0db3685a..a38850c2173 100644 --- a/tm2/pkg/db/prefix_db.go +++ b/tm2/pkg/db/prefix_db.go @@ -191,7 +191,7 @@ func (pdb *PrefixDB) Close() { // Implements DB. func (pdb *PrefixDB) Print() { fmt.Println(colors.Blue("prefix ---------------------")) - fmt.Printf("prefix: %v\n", colors.ColoredBytes(pdb.prefix, colors.Green, colors.Blue)) + fmt.Printf("prefix: %v\n", colors.DefaultColoredBytes(pdb.prefix)) pdb.db.Print() fmt.Println(colors.Blue("prefix --------------------- end")) } diff --git a/tm2/pkg/store/cache/store.go b/tm2/pkg/store/cache/store.go index 91530861303..c755a67c2b8 100644 --- a/tm2/pkg/store/cache/store.go +++ b/tm2/pkg/store/cache/store.go @@ -11,7 +11,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/colors" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/std" - "github.com/gnolang/gno/tm2/pkg/strings" "github.com/gnolang/gno/tm2/pkg/store/types" "github.com/gnolang/gno/tm2/pkg/store/utils" @@ -27,7 +26,7 @@ type cValue struct { func (cv cValue) String() string { return fmt.Sprintf("cValue{%s,%v,%v}", - string(colors.ColoredBytes(cv.value, colors.Blue, colors.Green)), + colors.DefaultColoredBytes(cv.value), cv.value, cv.deleted, cv.dirty) } @@ -236,8 +235,9 @@ func (store *cacheStore) setCacheValue(key, value []byte, deleted bool, dirty bo func (store *cacheStore) Print() { fmt.Println(colors.Cyan("cacheStore.Print"), fmt.Sprintf("%p", store)) for key, value := range store.cache { - fmt.Println(colors.Yellow(key), - string(colors.ColoredBytes([]byte(strings.TrimN(string(value.value), 200)), colors.Green, colors.Blue)), + fmt.Println( + colors.DefaultColoredBytesN([]byte(key), 50), + colors.DefaultColoredBytesN(value.value, 100), "deleted", value.deleted, "dirty", value.dirty, ) diff --git a/tm2/pkg/store/utils/print.go b/tm2/pkg/store/utils/print.go index e133f3f9dfb..63afb5c22e1 100644 --- a/tm2/pkg/store/utils/print.go +++ b/tm2/pkg/store/utils/print.go @@ -15,7 +15,9 @@ type Printer interface { // TODO move to another file. func Print(store types.Store) { fmt.Println(colors.Blue("//----------------------------------------")) - if ps, ok := store.(Printer); ok { + if store == nil { + fmt.Println("") + } else if ps, ok := store.(Printer); ok { ps.Print() } else { fmt.Println(colors.Blue(fmt.Sprintf("// store:%p %v", store, reflect.TypeOf(store)))) @@ -24,7 +26,7 @@ func Print(store types.Store) { for ; itr.Valid(); itr.Next() { key, value := itr.Key(), itr.Value() var keystr, valuestr string - keystr = string(colors.ColoredBytes(key, colors.Green, colors.Blue)) + keystr = colors.DefaultColoredBytesN(key, 100) valuestr = fmt.Sprintf("(%d)", len(value)) /* if true || strings.IsASCIIText(string(value)) { From e454cf499ec1b451ef1a5f9d07655c100c7275ee Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 14:51:26 -0700 Subject: [PATCH 10/14] remove initBuiltinPackagesAndTypes --- gno.land/pkg/sdk/vm/builtins.go | 55 +++++++++++++---------------- gno.land/pkg/sdk/vm/keeper.go | 3 +- tm2/pkg/db/goleveldb/go_level_db.go | 2 +- tm2/pkg/sdk/baseapp.go | 4 --- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index bbeea35134c..4a5239841d7 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -5,43 +5,38 @@ import ( "path/filepath" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - "github.com/gnolang/gno/gnovm/stdlibs" "github.com/gnolang/gno/tm2/pkg/crypto" osm "github.com/gnolang/gno/tm2/pkg/os" "github.com/gnolang/gno/tm2/pkg/sdk" "github.com/gnolang/gno/tm2/pkg/std" ) -func (vm *VMKeeper) initBuiltinPackagesAndTypes(store gno.Store) { - // NOTE: native functions/methods added here must be quick operations, - // or account for gas before operation. - // TODO: define criteria for inclusion, and solve gas calculations. - 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) - if !osm.DirExists(stdlibPath) { - // does not exist. - return nil, nil - } - memPkg := gno.ReadMemPackage(stdlibPath, pkgPath) - if memPkg.IsEmpty() { - // no gno files are present, skip this package - return nil, nil - } - - m2 := gno.NewMachineWithOptions(gno.MachineOptions{ - PkgPath: "gno.land/r/stdlibs/" + pkgPath, - // PkgPath: pkgPath, - Output: os.Stdout, - Store: store, - }) - defer m2.Release() - pn, pv = m2.RunMemPackage(memPkg, true) - return +// NOTE: native functions/methods added here must be quick operations, +// or account for gas before operation. +// TODO: define criteria for inclusion, and solve gas calculations(???). +func (vm *VMKeeper) getPackage(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) + if !osm.DirExists(stdlibPath) { + // does not exist. + return nil, nil + } + memPkg := gno.ReadMemPackage(stdlibPath, pkgPath) + if memPkg.IsEmpty() { + // no gno files are present, skip this package + return nil, nil } - store.SetPackageGetter(getPackage) - store.SetNativeStore(stdlibs.NativeStore) + + m2 := gno.NewMachineWithOptions(gno.MachineOptions{ + PkgPath: "gno.land/r/stdlibs/" + pkgPath, + // PkgPath: pkgPath, + Output: os.Stdout, + Store: store, + }) + defer m2.Release() + pn, pv = m2.RunMemPackage(memPkg, true) + return } // ---------------------------------------- diff --git a/gno.land/pkg/sdk/vm/keeper.go b/gno.land/pkg/sdk/vm/keeper.go index 30ffcfa5172..95f17ce09f0 100644 --- a/gno.land/pkg/sdk/vm/keeper.go +++ b/gno.land/pkg/sdk/vm/keeper.go @@ -81,7 +81,8 @@ func (vm *VMKeeper) Initialize(ms store.MultiStore) { baseSDKStore := ms.GetStore(vm.baseKey) iavlSDKStore := ms.GetStore(vm.iavlKey) vm.gnoStore = gno.NewStore(alloc, baseSDKStore, iavlSDKStore) - vm.initBuiltinPackagesAndTypes(vm.gnoStore) + vm.gnoStore.SetPackageGetter(vm.getPackage) + vm.gnoStore.SetNativeStore(stdlibs.NativeStore) if vm.gnoStore.NumMemPackages() > 0 { // for now, all mem packages must be re-run after reboot. // TODO remove this, and generally solve for in-mem garbage collection diff --git a/tm2/pkg/db/goleveldb/go_level_db.go b/tm2/pkg/db/goleveldb/go_level_db.go index 09d450dc5da..4f011cb8395 100644 --- a/tm2/pkg/db/goleveldb/go_level_db.go +++ b/tm2/pkg/db/goleveldb/go_level_db.go @@ -117,7 +117,7 @@ func (db *GoLevelDB) Print() { itr := db.db.NewIterator(nil, nil) for itr.Next() { key := colors.DefaultColoredBytesN(itr.Key(), 50) - value := colors.DefaultColoredBytes(itr.Value(), 100) + value := colors.DefaultColoredBytesN(itr.Value(), 100) fmt.Printf("%v: %v\n", key, value) } } diff --git a/tm2/pkg/sdk/baseapp.go b/tm2/pkg/sdk/baseapp.go index a36b6b6f139..748aa424111 100644 --- a/tm2/pkg/sdk/baseapp.go +++ b/tm2/pkg/sdk/baseapp.go @@ -223,10 +223,6 @@ func (app *BaseApp) GetCacheMultiStore() store.MultiStore { return app.cms.MultiCacheWrap() } -func (app *BaseApp) GetCMS() store.CommitMultiStore { - return app.cms -} - // Router returns the router of the BaseApp. func (app *BaseApp) Router() Router { if app.sealed { From d20ff79db8c6b3f3920bde26b0c1cf3ad0e0b89b Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 14:53:41 -0700 Subject: [PATCH 11/14] comment --- gno.land/pkg/sdk/vm/builtins.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/sdk/vm/builtins.go b/gno.land/pkg/sdk/vm/builtins.go index 4a5239841d7..cbf6df02e93 100644 --- a/gno.land/pkg/sdk/vm/builtins.go +++ b/gno.land/pkg/sdk/vm/builtins.go @@ -11,8 +11,11 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) -// NOTE: native functions/methods added here must be quick operations, -// or account for gas before operation. +// NOTE: this function may add loaded dependencies to store if they don't +// already exist, including mem packages. If this happens during a transaction +// with the tx context store, the transaction caller will pay for operations. +// NOTE: native functions/methods added here must be quick operations, or +// account for gas before operation. // TODO: define criteria for inclusion, and solve gas calculations(???). func (vm *VMKeeper) getPackage(pkgPath string, store gno.Store) (pn *gno.PackageNode, pv *gno.PackageValue) { // otherwise, built-in package value. From b8e6373eca8e3fc4843b35a40a48da8dc6a03908 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Tue, 4 Jun 2024 15:08:32 -0700 Subject: [PATCH 12/14] remove spurious printer --- tm2/pkg/store/utils/print.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tm2/pkg/store/utils/print.go b/tm2/pkg/store/utils/print.go index 63afb5c22e1..d47f4188db7 100644 --- a/tm2/pkg/store/utils/print.go +++ b/tm2/pkg/store/utils/print.go @@ -8,16 +8,12 @@ import ( "github.com/gnolang/gno/tm2/pkg/store/types" ) -type Printer interface { - Print() -} - // TODO move to another file. func Print(store types.Store) { fmt.Println(colors.Blue("//----------------------------------------")) if store == nil { fmt.Println("") - } else if ps, ok := store.(Printer); ok { + } else if ps, ok := store.(types.Printer); ok { ps.Print() } else { fmt.Println(colors.Blue(fmt.Sprintf("// store:%p %v", store, reflect.TypeOf(store)))) From e16edff4d174ce87dba24303fefe25f7d35e5741 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 23 Jun 2024 19:50:46 -0700 Subject: [PATCH 13/14] lint --- gnovm/pkg/gnolang/store.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 9ea36c3f52b..12666c3d7ad 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -621,7 +621,6 @@ func (ds *defaultStore) ClearObjectCache() { ds.alloc.Reset() ds.cacheObjects = make(map[ObjectID]Object) // new cache. ds.opslog = nil // new ops log. - //ds.current = nil ds.SetCachePackage(Uverse()) } From 2f5589f6c8f304f50a98065521f747096e1529d3 Mon Sep 17 00:00:00 2001 From: jaekwon Date: Sun, 23 Jun 2024 19:59:58 -0700 Subject: [PATCH 14/14] bug fix --- tm2/pkg/store/cache/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/store/cache/store.go b/tm2/pkg/store/cache/store.go index c755a67c2b8..6bc6ad3b71f 100644 --- a/tm2/pkg/store/cache/store.go +++ b/tm2/pkg/store/cache/store.go @@ -27,7 +27,7 @@ type cValue struct { func (cv cValue) String() string { return fmt.Sprintf("cValue{%s,%v,%v}", colors.DefaultColoredBytes(cv.value), - cv.value, cv.deleted, cv.dirty) + cv.deleted, cv.dirty) } // cacheStore wraps an in-memory cache around an underlying types.Store.