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

Feat/smartaccounts/wasm #263

Merged
merged 12 commits into from
Feb 26, 2024
37 changes: 27 additions & 10 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
feesharekeeper "github.com/terra-money/core/v2/x/feeshare/keeper"

smartaccountante "github.com/terra-money/core/v2/x/smartaccount/ante"
smartaccountkeeper "github.com/terra-money/core/v2/x/smartaccount/keeper"

"github.com/cosmos/cosmos-sdk/client"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,19 +17,22 @@ import (

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
terrawasmkeeper "github.com/terra-money/core/v2/x/wasm/keeper"
)

// HandlerOptions extends the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions

IBCkeeper *ibckeeper.Keeper
FeeShareKeeper feesharekeeper.Keeper
BankKeeper bankKeeper.Keeper
TxCounterStoreKey storetypes.StoreKey
WasmConfig wasmTypes.WasmConfig
TxConfig client.TxConfig
IBCkeeper *ibckeeper.Keeper
FeeShareKeeper feesharekeeper.Keeper
BankKeeper bankKeeper.Keeper
SmartAccountKeeper *smartaccountkeeper.Keeper
WasmKeeper *terrawasmkeeper.Keeper
TxCounterStoreKey storetypes.StoreKey
WasmConfig wasmTypes.WasmConfig
TxConfig client.TxConfig
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
Expand All @@ -45,6 +51,14 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

if options.SmartAccountKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "smart account keeper is required for ante builder")
}

if options.WasmKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm keeper is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
Expand All @@ -60,11 +74,14 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
// TODO: remove the following line after the migration to the new signature verification decorator is done
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
// ante.NewSetPubKeyDecorator(options.AccountKeeper),
// ante.NewValidateSigCountDecorator(options.AccountKeeper),
// ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
// ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
smartaccountante.NewSmartAccountAuthDecorator(*options.SmartAccountKeeper, options.WasmKeeper, options.AccountKeeper, sigGasConsumer, options.SignModeHandler),
smartaccountante.NewPreTransactionHookDecorator(*options.SmartAccountKeeper, options.WasmKeeper),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCkeeper),
}
Expand Down
12 changes: 7 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ func NewTerraApp(
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: cosmosante.DefaultSigVerificationGasConsumer,
},
BankKeeper: app.Keepers.BankKeeper,
FeeShareKeeper: app.Keepers.FeeShareKeeper,
IBCkeeper: app.Keepers.IBCKeeper,
TxCounterStoreKey: app.keys[wasmtypes.StoreKey],
WasmConfig: wasmConfig,
BankKeeper: app.Keepers.BankKeeper,
SmartAccountKeeper: &app.Keepers.SmartAccountKeeper,
WasmKeeper: &app.Keepers.WasmKeeper,
FeeShareKeeper: app.Keepers.FeeShareKeeper,
IBCkeeper: app.Keepers.IBCKeeper,
TxCounterStoreKey: app.keys[wasmtypes.StoreKey],
WasmConfig: wasmConfig,
},
)
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package keepers

import (

// #nosec G702

"path/filepath"

"github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/router"
Expand Down Expand Up @@ -100,6 +97,9 @@ import (
feesharekeeper "github.com/terra-money/core/v2/x/feeshare/keeper"
feesharetypes "github.com/terra-money/core/v2/x/feeshare/types"

smartaccountkeeper "github.com/terra-money/core/v2/x/smartaccount/keeper"
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types"

terraappconfig "github.com/terra-money/core/v2/app/config"
// unnamed import of statik for swagger UI support
_ "github.com/terra-money/core/v2/client/docs/statik"
Expand All @@ -123,6 +123,7 @@ var maccPerms = map[string][]string{
tokenfactorytypes.ModuleName: {authtypes.Burner, authtypes.Minter},
alliancetypes.ModuleName: {authtypes.Burner, authtypes.Minter},
alliancetypes.RewardsPoolName: nil,
smartaccounttypes.ModuleName: nil,
}

type TerraAppKeepers struct {
Expand Down Expand Up @@ -157,6 +158,7 @@ type TerraAppKeepers struct {
AllianceKeeper alliancekeeper.Keeper
FeeShareKeeper feesharekeeper.Keeper
ICQKeeper icqkeeper.Keeper
SmartAccountKeeper smartaccountkeeper.Keeper

// IBC hooks
IBCHooksKeeper *ibchookskeeper.Keeper
Expand Down Expand Up @@ -469,6 +471,11 @@ func NewTerraAppKeepers(
wasmOpts...,
)

keepers.SmartAccountKeeper = smartaccountkeeper.NewKeeper(
appCodec,
keys[smartaccounttypes.StoreKey],
)

keepers.Ics20WasmHooks.ContractKeeper = keepers.WasmKeeper.Keeper
// Setup the contract keepers.WasmKeeper before the
// hook for the BankKeeper othrwise the WasmKeeper
Expand Down Expand Up @@ -571,6 +578,7 @@ func (app *TerraAppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legacyA
paramsKeeper.Subspace(tokenfactorytypes.ModuleName).WithKeyTable(tokenfactorytypes.ParamKeyTable())
paramsKeeper.Subspace(feesharetypes.ModuleName).WithKeyTable(feesharetypes.ParamKeyTable())
paramsKeeper.Subspace(alliancetypes.ModuleName).WithKeyTable(alliancetypes.ParamKeyTable())
paramsKeeper.Subspace(smartaccounttypes.ModuleName).WithKeyTable(smartaccounttypes.ParamKeyTable())

return paramsKeeper
}
Expand Down
3 changes: 3 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import (
alliancetypes "github.com/terra-money/alliance/x/alliance/types"
feesharetypes "github.com/terra-money/core/v2/x/feeshare/types"

smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types"

// unnamed import of statik for swagger UI support
_ "github.com/terra-money/core/v2/client/docs/statik"
)
Expand All @@ -61,6 +63,7 @@ func (keepers *TerraAppKeepers) GenerateKeys() {
consensusparamtypes.StoreKey, tokenfactorytypes.StoreKey, wasmtypes.StoreKey,
ibcfeetypes.StoreKey, ibchookstypes.StoreKey, crisistypes.StoreKey,
alliancetypes.StoreKey, feesharetypes.StoreKey, icqtypes.StoreKey,
smartaccounttypes.StoreKey,
)

keepers.tkeys = sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down
7 changes: 7 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
feegrantmodule "github.com/cosmos/cosmos-sdk/x/feegrant/module"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/terra-money/core/v2/x/smartaccount"
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types"

"github.com/cosmos/cosmos-sdk/x/gov"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -117,6 +119,7 @@ var ModuleBasics = module.NewBasicManager(
alliance.AppModuleBasic{},
feeshare.AppModuleBasic{},
icq.AppModuleBasic{},
smartaccount.AppModuleBasic{},
)

// NOTE: Any module instantiated in the module manager that is later modified
Expand Down Expand Up @@ -154,6 +157,7 @@ func appModules(app *TerraApp, encodingConfig terrappsparams.EncodingConfig, ski
alliance.NewAppModule(app.appCodec, app.Keepers.AllianceKeeper, app.Keepers.StakingKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.interfaceRegistry, app.GetSubspace(alliancetypes.ModuleName)),
feeshare.NewAppModule(app.Keepers.FeeShareKeeper, app.Keepers.AccountKeeper, app.GetSubspace(feesharetypes.ModuleName)),
icq.NewAppModule(app.Keepers.ICQKeeper),
smartaccount.NewAppModule(app.Keepers.SmartAccountKeeper, app.GetSubspace(smartaccounttypes.ModuleName)),
}
}

Expand Down Expand Up @@ -191,6 +195,7 @@ var initGenesisOrder = []string{
feesharetypes.ModuleName,
consensusparamtypes.ModuleName,
icqtypes.ModuleName,
smartaccounttypes.ModuleName,
}

var beginBlockersOrder = []string{
Expand Down Expand Up @@ -223,6 +228,7 @@ var beginBlockersOrder = []string{
feesharetypes.ModuleName,
consensusparamtypes.ModuleName,
icqtypes.ModuleName,
smartaccounttypes.ModuleName,
}

var endBlockerOrder = []string{
Expand Down Expand Up @@ -255,4 +261,5 @@ var endBlockerOrder = []string{
feesharetypes.ModuleName,
consensusparamtypes.ModuleName,
icqtypes.ModuleName,
smartaccounttypes.ModuleName,
}
17 changes: 12 additions & 5 deletions app/test_helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/suite"
Expand All @@ -16,6 +15,8 @@ import (
tokenfactorytypes "github.com/terra-money/core/v2/x/tokenfactory/types"

"github.com/cosmos/cosmos-sdk/baseapp"
secp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -32,6 +33,7 @@ type AppTestSuite struct {
Ctx sdk.Context
QueryHelper *baseapp.QueryServiceTestHelper
TestAccs []sdk.AccAddress
TestAccPrivs []cryptotypes.PrivKey
EncodingConfig appparams.EncodingConfig
}

Expand Down Expand Up @@ -80,7 +82,9 @@ func (s *AppTestSuite) Setup() {

s.App.Keepers.DistrKeeper.SetFeePool(s.Ctx, distrtypes.InitialFeePool())

s.TestAccs = s.CreateRandomAccounts(3)
testAccounts, privKeys := s.CreateRandomAccounts(3)
s.TestAccs = testAccounts
s.TestAccPrivs = privKeys
}

func (s *AppTestSuite) AssertEventEmitted(ctx sdk.Context, eventTypeExpected string, numEventsExpected int) {
Expand All @@ -96,17 +100,20 @@ func (s *AppTestSuite) AssertEventEmitted(ctx sdk.Context, eventTypeExpected str
}

// CreateRandomAccounts is a function return a list of randomly generated AccAddresses
func (s *AppTestSuite) CreateRandomAccounts(numAccts int) []sdk.AccAddress {
func (s *AppTestSuite) CreateRandomAccounts(numAccts int) ([]sdk.AccAddress, []cryptotypes.PrivKey) {
testAddrs := make([]sdk.AccAddress, numAccts)
testPrivKeys := make([]cryptotypes.PrivKey, numAccts)
for i := 0; i < numAccts; i++ {
pk := ed25519.GenPrivKey().PubKey()
priv := secp256k1.GenPrivKey()
pk := priv.PubKey()
testAddrs[i] = sdk.AccAddress(pk.Address())
testPrivKeys[i] = priv

err := s.FundAcc(testAddrs[i], sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)))
s.Require().NoError(err)
}

return testAddrs
return testAddrs, testPrivKeys
}

// FundAcc funds target address with specified amount.
Expand Down
12 changes: 12 additions & 0 deletions proto/terra/smartaccount/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package terra.smartaccount.v1;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "terra/smartaccount/v1/params.proto";
import "terra/smartaccount/v1/setting.proto";

option go_package = "github.com/terra-money/core/v2/x/smartaccount/types";

Expand All @@ -12,6 +13,10 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/terra/smartaccount/v1/params";
}

rpc Setting(QuerySettingRequest) returns (QuerySettingResponse) {
option (google.api.http).get = "/terra/smartaccount/v1/params/{address}";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
Expand All @@ -23,3 +28,10 @@ message QueryParamsResponse {
Params params = 1 [ (gogoproto.nullable) = false ];
}

message QuerySettingRequest {
string address = 1;
}

message QuerySettingResponse {
Setting setting = 1 [ (gogoproto.nullable) = false ];
}
Loading
Loading