Skip to content

Commit

Permalink
feat(logic): migrate logic module
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jan 17, 2024
1 parent ca000b2 commit a2b5286
Show file tree
Hide file tree
Showing 30 changed files with 249 additions and 216 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ func New(
icatypes.ModuleName,
ibcfeetypes.ModuleName,
wasmtypes.ModuleName,
logicmoduletypes.ModuleName,
)

app.ModuleManager.SetOrderEndBlockers(
Expand Down
12 changes: 7 additions & 5 deletions x/logic/fs/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"net/url"
"testing"

"cosmossdk.io/store/metrics"

"github.com/golang/mock/gomock"

. "github.com/smartystreets/goconvey/convey"

tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
"cosmossdk.io/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/store"
"cosmossdk.io/store"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/okp4/okp4d/x/logic/testutil"
Expand Down Expand Up @@ -122,8 +124,8 @@ func TestWasmHandler(t *testing.T) {
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the uri #%d: %s", nc, tc.uri), func() {
Convey("and a wasm keeper initialized with the given values", func() {
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
wasmKeeper := testutil.NewMockWasmKeeper(ctrl)
ctx := sdk.
NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
6 changes: 3 additions & 3 deletions x/logic/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"io"
"io/fs"

storetypes "cosmossdk.io/store/types"

"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// Predicates is a map of predicate names to their execution costs.
Expand All @@ -20,7 +20,7 @@ type Option func(*prolog.Interpreter) error

// WithPredicates configures the interpreter to register the specified predicates.
// The predicates names must be present in the registry, otherwise the function will return an error.
func WithPredicates(_ goctx.Context, predicates Predicates, meter sdk.GasMeter) Option {
func WithPredicates(_ goctx.Context, predicates Predicates, meter storetypes.GasMeter) Option {
return func(i *prolog.Interpreter) error {
for predicate, cost := range predicates {
if err := Register(i, predicate, cost, meter); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strconv"
"strings"

storetypes "cosmossdk.io/store/types"

"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/okp4/okp4d/x/logic/predicate"
)

Expand Down Expand Up @@ -138,7 +138,7 @@ var RegistryNames = func() []string {
// executing the predicate(ctx).
//
//nolint:lll
func Register(i *prolog.Interpreter, name string, cost uint64, meter sdk.GasMeter) error {
func Register(i *prolog.Interpreter, name string, cost uint64, meter storetypes.GasMeter) error {
if p, ok := registry[name]; ok {
parts := strings.Split(name, "/")
if len(parts) == 2 {
Expand All @@ -148,7 +148,7 @@ func Register(i *prolog.Interpreter, name string, cost uint64, meter sdk.GasMete
return err
}

hook := func() sdk.Gas {
hook := func() storetypes.Gas {
meter.ConsumeGas(cost, fmt.Sprintf("predicate %s", name))

return meter.GasRemaining()
Expand Down
6 changes: 4 additions & 2 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
goctx "context"

storetypes "cosmossdk.io/store/types"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -26,7 +28,7 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
sdkCtx = withGasMeter(sdkCtx, limits)
defer func() {
if r := recover(); r != nil {
if gasError, ok := r.(sdk.ErrorOutOfGas); ok {
if gasError, ok := r.(storetypes.ErrorOutOfGas); ok {
response, err = nil, errorsmod.Wrapf(
types.LimitExceeded, "out of gas: %s <%s> (%d/%d)",
types.ModuleName, gasError.Descriptor, sdkCtx.GasMeter().GasConsumed(), sdkCtx.GasMeter().Limit())
Expand All @@ -49,7 +51,7 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
// withGasMeter returns a new context with a gas meter that has the given limit.
// The gas meter is go-router-safe.
func withGasMeter(sdkCtx sdk.Context, limits types.Limits) sdk.Context {
gasMeter := meter.WithSafeMeter(sdk.NewGasMeter(limits.MaxGas.Uint64()))
gasMeter := meter.WithSafeMeter(storetypes.NewGasMeter(limits.MaxGas.Uint64()))

return sdkCtx.WithGasMeter(gasMeter)
}
2 changes: 1 addition & 1 deletion x/logic/keeper/grpc_query_ask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

. "github.com/smartystreets/goconvey/convey"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down
2 changes: 1 addition & 1 deletion x/logic/keeper/grpc_query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"cosmossdk.io/math"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down
4 changes: 3 additions & 1 deletion x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
goctx "context"
"math"

storetypes "cosmossdk.io/store/types"

"github.com/ichiban/prolog"
"github.com/samber/lo"

Expand Down Expand Up @@ -98,7 +100,7 @@ func (k Keeper) solsToAnswer(sdkCtx sdk.Context, sols *prolog.Solutions) (*types

if err := sols.Err(); err != nil {
if sdkCtx.GasMeter().IsOutOfGas() {
panic(sdk.ErrorOutOfGas{Descriptor: "Prolog interpreter execution"})
panic(storetypes.ErrorOutOfGas{Descriptor: "Prolog interpreter execution"})
}
solError = sols.Err().Error()
}
Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"io/fs"

"github.com/cometbft/cometbft/libs/log"
"cosmossdk.io/log"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/okp4/okp4d/x/logic/types"
Expand Down
2 changes: 1 addition & 1 deletion x/logic/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

. "github.com/smartystreets/goconvey/convey"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down
10 changes: 5 additions & 5 deletions x/logic/meter/safe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package meter
import (
"sync"

sdk "github.com/cosmos/cosmos-sdk/types"
storetypes "cosmossdk.io/store/types"
)

// safeMeterDecorater is a wrapper around sdk.GasMeter that provides go-routine-safe access to the underlying gas meter.
// safeMeterDecorater is a wrapper around storetypes.GasMeter that provides go-routine-safe access to the underlying gas meter.
// This is needed because the interpreter uses multiple go-routines, and the gas meter is shared between multiple
// goroutines.
type safeMeterDecorater struct {
gasMeter sdk.GasMeter
gasMeter storetypes.GasMeter
mutex sync.RWMutex
}

// WithSafeMeter returns a new instance of sdk.GasMeter that is go-routine-safe.
func WithSafeMeter(gasMeter sdk.GasMeter) sdk.GasMeter {
// WithSafeMeter returns a new instance of storetypes.GasMeter that is go-routine-safe.
func WithSafeMeter(gasMeter storetypes.GasMeter) storetypes.GasMeter {
return &safeMeterDecorater{
gasMeter: gasMeter,
}
Expand Down
18 changes: 9 additions & 9 deletions x/logic/meter/weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@ package meter
import (
"math"

"github.com/cosmos/cosmos-sdk/types"
storetypes "cosmossdk.io/store/types"
)

// weightedMeterDecorator is decorator that wraps a gas meter and adds a weight multiplier to the consumed gas.
type weightedMeterDecorator struct {
decorated types.GasMeter
decorated storetypes.GasMeter
weight uint64
}

// WithWeightedMeter returns a new weightedMeterDecorator with the given weight.
func WithWeightedMeter(decorated types.GasMeter, weight uint64) types.GasMeter {
func WithWeightedMeter(decorated storetypes.GasMeter, weight uint64) storetypes.GasMeter {
return &weightedMeterDecorator{
decorated: decorated,
weight: weight,
}
}

// GasConsumed returns the amount of gas consumed by the decorated gas meter.
func (m *weightedMeterDecorator) GasConsumed() types.Gas {
func (m *weightedMeterDecorator) GasConsumed() storetypes.Gas {
return m.decorated.GasConsumed()
}

// GasConsumedToLimit returns the amount of gas consumed by the decorated gas meter.
func (m *weightedMeterDecorator) GasConsumedToLimit() types.Gas {
func (m *weightedMeterDecorator) GasConsumedToLimit() storetypes.Gas {
return m.decorated.GasConsumedToLimit()
}

// GasRemaining returns the amount of gas remaining in the decorated gas meter.
func (m *weightedMeterDecorator) GasRemaining() types.Gas {
func (m *weightedMeterDecorator) GasRemaining() storetypes.Gas {
return m.decorated.GasRemaining()
}

// Limit returns the limit of the decorated gas meter.
func (m *weightedMeterDecorator) Limit() types.Gas {
func (m *weightedMeterDecorator) Limit() storetypes.Gas {
return m.decorated.Limit()
}

// ConsumeGas consumes the given amount of gas from the decorated gas meter.
func (m *weightedMeterDecorator) ConsumeGas(amount types.Gas, descriptor string) {
func (m *weightedMeterDecorator) ConsumeGas(amount storetypes.Gas, descriptor string) {
consumed, overflow := multiplyUint64Overflow(m.weight, amount)
if overflow {
m.decorated.ConsumeGas(math.MaxUint64, descriptor)
Expand All @@ -51,7 +51,7 @@ func (m *weightedMeterDecorator) ConsumeGas(amount types.Gas, descriptor string)
}

// RefundGas refunds the given amount of gas to the decorated gas meter.
func (m *weightedMeterDecorator) RefundGas(amount types.Gas, descriptor string) {
func (m *weightedMeterDecorator) RefundGas(amount storetypes.Gas, descriptor string) {
consumed, overflow := multiplyUint64Overflow(m.decorated.GasConsumed(), amount)
if overflow {
m.decorated.RefundGas(math.MaxUint64, descriptor)
Expand Down
Loading

0 comments on commit a2b5286

Please sign in to comment.