Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txtar for test4 cacheTypes issue #2605

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions gno.land/cmd/gnoland/testdata/restart_missing_type.txtar

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion gno.land/pkg/gnoland/node_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type InMemoryNodeConfig struct {
TMConfig *tmcfg.Config
GenesisTxHandler GenesisTxHandler
GenesisMaxVMCycles int64
DB *memdb.MemDB // will be initialized if nil
}

// NewMockedPrivValidator generate a new key
Expand Down Expand Up @@ -86,14 +87,19 @@ func NewInMemoryNode(logger *slog.Logger, cfg *InMemoryNodeConfig) (*node.Node,
}

evsw := events.NewEventSwitch()
// initialize db if nil
mdb := cfg.DB
if mdb == nil {
mdb = memdb.NewMemDB()
}

// Initialize the application with the provided options
gnoApp, err := NewAppWithOptions(&AppOptions{
Logger: logger,
GnoRootDir: cfg.TMConfig.RootDir,
GenesisTxHandler: cfg.GenesisTxHandler,
MaxCycles: cfg.GenesisMaxVMCycles,
DB: memdb.NewMemDB(),
DB: mdb,
EventSwitch: evsw,
CacheStdlibLoad: true,
})
Expand Down
26 changes: 25 additions & 1 deletion gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto/bip39"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
"github.com/gnolang/gno/tm2/pkg/crypto/keys/client"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
tm2Log "github.com/gnolang/gno/tm2/pkg/log"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/rogpeppe/go-internal/testscript"
Expand Down Expand Up @@ -71,6 +72,7 @@ func RunGnolandTestscripts(t *testing.T, txtarDir string) {

type testNode struct {
*node.Node
cfg *gnoland.InMemoryNodeConfig
nGnoKeyExec uint // Counter for execution of gnokey.
}

Expand Down Expand Up @@ -188,16 +190,38 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {

// setup genesis state
cfg.Genesis.AppState = *genesis
cfg.DB = memdb.NewMemDB() // so it can be reused when restarting.

n, remoteAddr := TestingInMemoryNode(t, logger, cfg)

// Register cleanup
nodes[sid] = &testNode{Node: n}
nodes[sid] = &testNode{Node: n, cfg: cfg}

// Add default environments
ts.Setenv("RPC_ADDR", remoteAddr)

fmt.Fprintln(ts.Stdout(), "node started successfully")
case "restart":
// XXX: unstable, should try to use it in a working scenario
n, ok := nodes[sid]
if !ok {
err = fmt.Errorf("node must be started before being restarted")
break
}

if stopErr := n.Stop(); stopErr != nil {
err = fmt.Errorf("error stopping node: %w", stopErr)
break
}

// Create new node with same config.
newNode, newRemoteAddr := TestingInMemoryNode(t, logger, n.cfg)

// Update testNode and environment variables.
n.Node = newNode
ts.Setenv("RPC_ADDR", newRemoteAddr)

fmt.Fprintln(ts.Stdout(), "node restarted successfully")
case "stop":
n, ok := nodes[sid]
if !ok {
Expand Down
41 changes: 13 additions & 28 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,21 @@ func (vm *VMKeeper) Initialize(
if vm.gnoStore != nil {
panic("should not happen")
}
baseSDKStore := ms.GetStore(vm.baseKey)
iavlSDKStore := ms.GetStore(vm.iavlKey)
baseStore := ms.GetStore(vm.baseKey)
iavlStore := ms.GetStore(vm.iavlKey)

if cacheStdlibLoad {
// Testing case (using the cache speeds up starting many nodes)
vm.gnoStore = cachedStdlibLoad(vm.stdlibsDir, baseSDKStore, iavlSDKStore)
} else {
// On-chain case
vm.gnoStore = uncachedPackageLoad(logger, vm.stdlibsDir, baseSDKStore, iavlSDKStore)
}
}

func uncachedPackageLoad(
logger *slog.Logger,
stdlibsDir string,
baseStore, iavlStore store.Store,
) gno.Store {
alloc := gno.NewAllocator(maxAllocTx)
gnoStore := gno.NewStore(alloc, baseStore, iavlStore)
gnoStore.SetNativeStore(stdlibs.NativeStore)
if gnoStore.NumMemPackages() == 0 {
vm.gnoStore = gno.NewStore(alloc, baseStore, iavlStore)
vm.gnoStore.SetNativeStore(stdlibs.NativeStore)
if vm.gnoStore.NumMemPackages() == 0 {
// No packages in the store; set up the stdlibs.
start := time.Now()

loadStdlib(stdlibsDir, gnoStore)
if cacheStdlibLoad {
cachedLoadStdlib(vm.stdlibsDir, vm.gnoStore, baseStore, iavlStore)
} else {
loadStdlib(vm.stdlibsDir, vm.gnoStore)
}

// XXX Quick and dirty to make this function work on non-validator nodes
iter := iavlStore.Iterator(nil, nil)
Expand Down Expand Up @@ -149,7 +139,7 @@ func uncachedPackageLoad(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Store: gnoStore,
Store: vm.gnoStore,
})
defer m2.Release()
gno.DisableDebug()
Expand All @@ -159,7 +149,6 @@ func uncachedPackageLoad(
logger.Debug("GnoVM packages preprocessed",
"elapsed", time.Since(start))
}
return gnoStore
}

var iavlBackupPrefix = []byte("init_iavl_backup:")
Expand All @@ -173,7 +162,7 @@ func isStoreEmpty(st store.Store) bool {
return true
}

func cachedStdlibLoad(stdlibsDir string, baseStore, iavlStore store.Store) gno.Store {
func cachedLoadStdlib(stdlibsDir string, gnoStore gno.Store, baseStore, iavlStore store.Store) {
cachedStdlibOnce.Do(func() {
cachedStdlibBase = memdb.NewMemDB()
cachedStdlibIavl = memdb.NewMemDB()
Expand All @@ -195,11 +184,7 @@ func cachedStdlibLoad(stdlibsDir string, baseStore, iavlStore store.Store) gno.S
iavlStore.Set(itr.Key(), itr.Value())
}

alloc := gno.NewAllocator(maxAllocTx)
gs := gno.NewStore(alloc, baseStore, iavlStore)
gs.SetNativeStore(stdlibs.NativeStore)
gno.CopyCachesFromStore(gs, cachedGnoStore)
return gs
gno.CopyCachesFromStore(gnoStore, cachedGnoStore)
}

var (
Expand Down
Loading