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

fix(sdk/vm): re-load iavl store from backup if empty #2568

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from 1 commit
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
29 changes: 29 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,12 @@ 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())
}

logger.Debug("Standard libraries initialized",
"elapsed", time.Since(start))
} else {
Expand All @@ -126,6 +132,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 +161,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
Loading