Skip to content

Commit

Permalink
fix gas meter being global
Browse files Browse the repository at this point in the history
  • Loading branch information
piux2 committed Dec 6, 2024
1 parent 7026d2f commit fce2e4e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ var gnoStoreContextKey gnoStoreContextKeyType
func (vm *VMKeeper) newGnoTransactionStore(ctx sdk.Context) gno.TransactionStore {
base := ctx.Store(vm.baseKey)
iavl := ctx.Store(vm.iavlKey)
vm.gnoStore.SetGasMeter(ctx.GasMeter())
gasMeter := ctx.GasMeter()

return vm.gnoStore.BeginTransaction(base, iavl)
return vm.gnoStore.BeginTransaction(base, iavl, gasMeter)
}

func (vm *VMKeeper) MakeGnoTransactionStore(ctx sdk.Context) sdk.Context {
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ func evalStaticType(store Store, last BlockNode, x Expr) Type {
// See comment in evalStaticTypeOfRaw.
if store != nil && pn.PkgPath != uversePkgPath {
pv := pn.NewPackage() // temporary
store = store.BeginTransaction(nil, nil)
store = store.BeginTransaction(nil, nil, nil)
store.SetCachePackage(pv)
}
m := NewMachine(pn.PkgPath, store)
Expand Down Expand Up @@ -3183,7 +3183,7 @@ func evalStaticTypeOfRaw(store Store, last BlockNode, x Expr) (t Type) {
// yet predefined this time around.
if store != nil && pn.PkgPath != uversePkgPath {
pv := pn.NewPackage() // temporary
store = store.BeginTransaction(nil, nil)
store = store.BeginTransaction(nil, nil, nil)
store.SetCachePackage(pv)
}
m := NewMachine(pn.PkgPath, store)
Expand Down
12 changes: 4 additions & 8 deletions gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type NativeResolver func(pkgName string, name Name) func(m *Machine)
// blockchain, or the file system.
type Store interface {
// STABLE
BeginTransaction(baseStore, iavlStore store.Store) TransactionStore
BeginTransaction(baseStore, iavlStore store.Store, gasMeter store.GasMeter) TransactionStore
SetPackageGetter(PackageGetter)
GetPackage(pkgPath string, isImport bool) *PackageValue
SetCachePackage(*PackageValue)
Expand All @@ -51,7 +51,7 @@ type Store interface {
GetBlockNode(Location) BlockNode // to get a PackageNode, use PackageNodeLocation().
GetBlockNodeSafe(Location) BlockNode
SetBlockNode(BlockNode)
SetGasMeter(store.GasMeter)

// UNSTABLE
Go2GnoType(rt reflect.Type) Type
GetAllocator() *Allocator
Expand Down Expand Up @@ -171,7 +171,7 @@ func NewStore(alloc *Allocator, baseStore, iavlStore store.Store) *defaultStore
}

// If nil baseStore and iavlStore, the baseStores are re-used.
func (ds *defaultStore) BeginTransaction(baseStore, iavlStore store.Store) TransactionStore {
func (ds *defaultStore) BeginTransaction(baseStore, iavlStore store.Store, gasMeter store.GasMeter) TransactionStore {
if baseStore == nil {
baseStore = ds.baseStore
}
Expand All @@ -195,7 +195,7 @@ func (ds *defaultStore) BeginTransaction(baseStore, iavlStore store.Store) Trans
nativeResolver: ds.nativeResolver,

// gas meter
gasMeter: ds.gasMeter,
gasMeter: gasMeter,
gasConfig: ds.gasConfig,

// transient
Expand Down Expand Up @@ -744,10 +744,6 @@ func (ds *defaultStore) IterMemPackage() <-chan *gnovm.MemPackage {
}
}

func (ds *defaultStore) SetGasMeter(gm store.GasMeter) {
ds.gasMeter = gm
}

func (ds *defaultStore) consumeGas(gas int64, descriptor string) {
// In the tests, the defaultStore may not set the gas meter.
if ds.gasMeter != nil {
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestTransactionStore(t *testing.T) {

st := NewStore(nil, tm2Store, tm2Store)
wrappedTm2Store := tm2Store.CacheWrap()
txSt := st.BeginTransaction(wrappedTm2Store, wrappedTm2Store)
txSt := st.BeginTransaction(wrappedTm2Store, wrappedTm2Store, nil)
m := NewMachineWithOptions(MachineOptions{
PkgPath: "hello",
Store: txSt,
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestCopyFromCachedStore(t *testing.T) {
d1s := dbadapter.StoreConstructor(d1, storetypes.StoreOptions{})
d2s := dbadapter.StoreConstructor(d2, storetypes.StoreOptions{})
destStore := NewStore(nil, d1s, d2s)
destStoreTx := destStore.BeginTransaction(nil, nil) // CopyFromCachedStore requires a tx store.
destStoreTx := destStore.BeginTransaction(nil, nil, nil) // CopyFromCachedStore requires a tx store.
CopyFromCachedStore(destStoreTx, cachedStore, c1s, c2s)
destStoreTx.Write()

Expand Down

0 comments on commit fce2e4e

Please sign in to comment.