From 5326d0b2f97bb3ac036cdea2a34f93b8d8ca5c6c Mon Sep 17 00:00:00 2001 From: Morgan Date: Tue, 14 May 2024 21:03:32 +0200 Subject: [PATCH] fix(gnovm/store): copy store caches (#2105) Hotfix for the failing CI on master. Changes `cacheTypes`, `cacheNodes` and `cacheNativeTypes` to be copies when forking the store, so that the forked store doesn't modify its parent. cc @zivkovicmilos --- .../{map-delete.txtar => map_delete.txtar} | 0 ...r => realm_banker_issued_coin_denom.txtar} | 0 gnovm/pkg/gnolang/store.go | 37 +++++++++++++------ 3 files changed, 26 insertions(+), 11 deletions(-) rename gno.land/cmd/gnoland/testdata/{map-delete.txtar => map_delete.txtar} (100%) rename gno.land/cmd/gnoland/testdata/{realm-banker-issued-coin-denom.txtar => realm_banker_issued_coin_denom.txtar} (100%) diff --git a/gno.land/cmd/gnoland/testdata/map-delete.txtar b/gno.land/cmd/gnoland/testdata/map_delete.txtar similarity index 100% rename from gno.land/cmd/gnoland/testdata/map-delete.txtar rename to gno.land/cmd/gnoland/testdata/map_delete.txtar diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm_banker_issued_coin_denom.txtar similarity index 100% rename from gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar rename to gno.land/cmd/gnoland/testdata/realm_banker_issued_coin_denom.txtar diff --git a/gnovm/pkg/gnolang/store.go b/gnovm/pkg/gnolang/store.go index 7fc54c12b0f..3db53213f8b 100644 --- a/gnovm/pkg/gnolang/store.go +++ b/gnovm/pkg/gnolang/store.go @@ -2,6 +2,7 @@ package gnolang import ( "fmt" + "maps" "reflect" "slices" "strconv" @@ -619,18 +620,32 @@ func (ds *defaultStore) ClearObjectCache() { // This function is used to handle queries and checktx transactions. func (ds *defaultStore) Fork() Store { ds2 := &defaultStore{ - alloc: ds.alloc.Fork().Reset(), - pkgGetter: ds.pkgGetter, - cacheObjects: make(map[ObjectID]Object), // new cache. - cacheTypes: ds.cacheTypes, + alloc: ds.alloc.Fork().Reset(), + + // Re-initialize caches. Some are cloned for speed. + cacheObjects: make(map[ObjectID]Object), + cacheTypes: maps.Clone(ds.cacheTypes), + // XXX: This is bad to say the least (ds.cacheNodes is shared with a + // child Store); however, cacheNodes is _not_ a cache, but a proper + // data store instead. SetBlockNode does not write anything to + // the underlying baseStore, and cloning this map makes everything run + // 4x slower, so here we are, copying the reference. cacheNodes: ds.cacheNodes, - cacheNativeTypes: ds.cacheNativeTypes, - baseStore: ds.baseStore, - iavlStore: ds.iavlStore, - pkgInjector: ds.pkgInjector, - nativeStore: ds.nativeStore, - go2gnoStrict: ds.go2gnoStrict, - opslog: nil, // new ops log. + cacheNativeTypes: maps.Clone(ds.cacheNativeTypes), + + // baseStore and iavlStore should generally be changed using SwapStores. + baseStore: ds.baseStore, + iavlStore: ds.iavlStore, + + // native injections / store "config" + pkgGetter: ds.pkgGetter, + pkgInjector: ds.pkgInjector, + nativeStore: ds.nativeStore, + go2gnoStrict: ds.go2gnoStrict, + + // reset opslog and current. + opslog: nil, + current: nil, } ds2.SetCachePackage(Uverse()) return ds2