Skip to content

Commit

Permalink
fix(sdk/vm): re-load iavl store from backup if empty (#2568)
Browse files Browse the repository at this point in the history
This is a hack; during initialization, the iavlStore is backed up to the
baseStore, and it is then recovered in Initialize if the iavlStore is
found to be empty (but there's supposed to be packages inside).

This is a hotfix for test4 non-validator nodes.

It still requires to start up the non-validator node once, with the new
changes applied, with a fresh db
  • Loading branch information
thehowl authored Jul 10, 2024
1 parent b88d77c commit c162dc0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func uncachedPackageLoad(

loadStdlib(stdlibsDir, gnoStore)

// XXX Quick and dirty to make this function work on non-validator nodes
iter := iavlStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
baseStore.Set(append(iavlBackupPrefix, iter.Key()...), iter.Value())
}
iter.Close()

logger.Debug("Standard libraries initialized",
"elapsed", time.Since(start))
} else {
Expand All @@ -126,6 +133,18 @@ func uncachedPackageLoad(
// and memory management across many objects/types/nodes/packages.
start := time.Now()

// XXX Quick and dirty to make this function work on non-validator nodes
if isStoreEmpty(iavlStore) {
iter := baseStore.Iterator(iavlBackupPrefix, nil)
for ; iter.Valid(); iter.Next() {
if !bytes.HasPrefix(iter.Key(), iavlBackupPrefix) {
break
}
iavlStore.Set(iter.Key()[len(iavlBackupPrefix):], iter.Value())
}
iter.Close()
}

m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Expand All @@ -143,6 +162,17 @@ func uncachedPackageLoad(
return gnoStore
}

var iavlBackupPrefix = []byte("init_iavl_backup:")

func isStoreEmpty(st store.Store) bool {
iter := st.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
return false
}
return true
}

func cachedStdlibLoad(stdlibsDir string, baseStore, iavlStore store.Store) gno.Store {
cachedStdlibOnce.Do(func() {
cachedStdlibBase = memdb.NewMemDB()
Expand Down

0 comments on commit c162dc0

Please sign in to comment.