diff --git a/Makefile b/Makefile index b05b9bd..223e92b 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test: COV_ROOT="/tmp/tokenfactory-coverage" COV_UNIT_E2E="${COV_ROOT}/unit-e2e" COV_SIMULATION="${COV_ROOT}/simulation" -COV_PKG="github.com/strangelove-ventures/tokenfactory/..." +COV_PKG="github.com/fetchai/tokenfactory/..." COV_SIM_CMD=${COV_SIMULATION}/simulation.test COV_SIM_COMMON=-Enabled=True -NumBlocks=100 -Commit=true -Period=5 -Verbose=false -test.v -test.gocoverdir=${COV_SIMULATION} diff --git a/app/app.go b/app/app.go index ccc9c31..1e44d9a 100644 --- a/app/app.go +++ b/app/app.go @@ -552,7 +552,6 @@ func NewApp( app.BankKeeper, app.DistrKeeper, tokenFactoryCapabilities, - tokenfactorykeeper.DefaultIsSudoAdminFunc, govModAddress, ) wasmOpts = append(wasmOpts, bindings.RegisterCustomPlugins(app.BankKeeper, &app.TokenFactoryKeeper)...) diff --git a/go.mod b/go.mod index d42d81b..af0889b 100644 --- a/go.mod +++ b/go.mod @@ -228,7 +228,6 @@ require ( github.com/cloudwego/base64x v0.1.5 // indirect github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 // indirect github.com/cockroachdb/fifo v0.0.0-20240616162244-4768e80dfb9a // indirect - github.com/cosmos/ibc-go/v10 v10.3.0 // indirect github.com/dgraph-io/badger/v4 v4.2.0 // indirect github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect diff --git a/go.sum b/go.sum index 75ca9c0..aa9f3b2 100644 --- a/go.sum +++ b/go.sum @@ -835,12 +835,10 @@ github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fr github.com/cosmos/gogoproto v1.7.0/go.mod h1:yWChEv5IUEYURQasfyBW5ffkMHR/90hiHgbNgrtp4j0= github.com/cosmos/iavl v1.2.4 h1:IHUrG8dkyueKEY72y92jajrizbkZKPZbMmG14QzsEkw= github.com/cosmos/iavl v1.2.4/go.mod h1:GiM43q0pB+uG53mLxLDzimxM9l/5N9UuSY3/D0huuVw= -github.com/cosmos/ibc-go/modules/capability v1.0.0 h1:r/l++byFtn7jHYa09zlAdSeevo8ci1mVZNO9+V0xsLE= -github.com/cosmos/ibc-go/modules/capability v1.0.0/go.mod h1:D81ZxzjZAe0ZO5ambnvn1qedsFQ8lOwtqicG6liLBco= +github.com/cosmos/ibc-go/modules/capability v1.0.1 h1:ibwhrpJ3SftEEZRxCRkH0fQZ9svjthrX2+oXdZvzgGI= +github.com/cosmos/ibc-go/modules/capability v1.0.1/go.mod h1:rquyOV262nGJplkumH+/LeYs04P3eV8oB7ZM4Ygqk4E= github.com/cosmos/ibc-go/v10 v10.3.0 h1:w5DkHih8qn15deAeFoTk778WJU+xC1krJ5kDnicfUBc= github.com/cosmos/ibc-go/v10 v10.3.0/go.mod h1:CthaR7n4d23PJJ7wZHegmNgbVcLXCQql7EwHrAXnMtw= -github.com/cosmos/ibc-go/v10 v8.2.0 h1:7oCzyy1sZCcgpeQLnHxC56brsSz3KWwQGKXalXwXFzE= -github.com/cosmos/ibc-go/v10 v8.2.0/go.mod h1:wj3qx75iC/XNnsMqbPDCIGs0G6Y3E/lo3bdqCyoCy+8= github.com/cosmos/ics23/go v0.11.0 h1:jk5skjT0TqX5e5QJbEnwXIS2yI2vnmLOgpQPeM5RtnU= github.com/cosmos/ics23/go v0.11.0/go.mod h1:A8OjxPE67hHST4Icw94hOxxFEJMBG031xIGF/JHNIY0= github.com/cosmos/ledger-cosmos-go v0.14.0 h1:WfCHricT3rPbkPSVKRH+L4fQGKYHuGOK9Edpel8TYpE= diff --git a/proto/osmosis/tokenfactory/v1beta1/genesis.proto b/proto/osmosis/tokenfactory/v1beta1/genesis.proto index 83d0cff..138c35f 100755 --- a/proto/osmosis/tokenfactory/v1beta1/genesis.proto +++ b/proto/osmosis/tokenfactory/v1beta1/genesis.proto @@ -4,6 +4,7 @@ package osmosis.tokenfactory.v1beta1; import "gogoproto/gogo.proto"; import "osmosis/tokenfactory/v1beta1/authorityMetadata.proto"; import "osmosis/tokenfactory/v1beta1/params.proto"; +import "cosmos_proto/cosmos.proto"; option go_package = "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types"; @@ -16,6 +17,12 @@ message GenesisState { (gogoproto.moretags) = "yaml:\"factory_denoms\"", (gogoproto.nullable) = false ]; + + repeated string sudo_admins = 3 [ + (cosmos_proto.scalar) = "cosmos.AddressString", + (gogoproto.moretags) = "yaml:\"sudo_admins\"", + (gogoproto.nullable) = false + ]; } // GenesisDenom defines a tokenfactory denom that is defined within genesis diff --git a/x/tokenfactory/keeper/genesis.go b/x/tokenfactory/keeper/genesis.go index b161d07..0d95e5d 100644 --- a/x/tokenfactory/keeper/genesis.go +++ b/x/tokenfactory/keeper/genesis.go @@ -30,11 +30,18 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { panic(err) } } + + sa := SudoAdmins{Keeper: k} + for _, genAdmin := range genState.GetSudoAdmins() { + if err := sa.AddSudoAdmin(ctx, genAdmin); err != nil { + panic(err) + } + } } // ExportGenesis returns the tokenfactory module's exported genesis. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { - genDenoms := []types.GenesisDenom{} + var genDenoms []types.GenesisDenom iterator := k.GetAllDenomsIterator(ctx) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { @@ -51,8 +58,11 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { }) } + sa := SudoAdmins{Keeper: k} + return &types.GenesisState{ FactoryDenoms: genDenoms, Params: k.GetParams(ctx), + SudoAdmins: sa.GetAllSudoAdmins(ctx), } } diff --git a/x/tokenfactory/keeper/genesis_test.go b/x/tokenfactory/keeper/genesis_test.go index 9051ced..ee5a060 100644 --- a/x/tokenfactory/keeper/genesis_test.go +++ b/x/tokenfactory/keeper/genesis_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/keeper" "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,6 +30,10 @@ func (suite *KeeperTestSuite) TestGenesis() { }, }, }, + SudoAdmins: []string{ + "cosmos1t7egva48prqmzl59x5ngv4zx0dtrwewcdqdjr8", + "cosmos15czt5nhlnvayqq37xun9s9yus0d6y26dx74r5p", + }, } suite.SetupTestForInitGenesis() @@ -51,3 +56,18 @@ func (suite *KeeperTestSuite) TestGenesis() { suite.Require().NotNil(exportedGenesis) suite.Require().Equal(genesisState, *exportedGenesis) } + +func (suite *KeeperTestSuite) TestGenesisStoresSudoAdmins() { + genesisState := types.GenesisState{ + SudoAdmins: []string{ + suite.TestAccs[0].String(), + suite.TestAccs[1].String(), + }, + } + + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + suite.App.TokenFactoryKeeper.InitGenesis(suite.Ctx, genesisState) + + suite.Require().True(sa.IsSudoAdmin(suite.Ctx, suite.TestAccs[0].String())) + suite.Require().True(sa.IsSudoAdmin(suite.Ctx, suite.TestAccs[1].String())) +} diff --git a/x/tokenfactory/keeper/keeper.go b/x/tokenfactory/keeper/keeper.go index c7b7089..c1ecc79 100644 --- a/x/tokenfactory/keeper/keeper.go +++ b/x/tokenfactory/keeper/keeper.go @@ -1,7 +1,6 @@ package keeper import ( - "context" "fmt" "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types" @@ -16,9 +15,6 @@ import ( ) type ( - // IsAdmin is a function signature that checks if an address is an admin. - IsSudoAdmin func(ctx context.Context, addr string) bool - Keeper struct { cdc codec.BinaryCodec storeKey store.StoreKey @@ -33,8 +29,6 @@ type ( // the address capable of executing a MsgUpdateParams message. Typically, this // should be the x/gov module account. authority string - - IsSudoAdminFunc IsSudoAdmin } ) @@ -47,8 +41,6 @@ func NewKeeper( bankKeeper types.BankKeeper, communityPoolKeeper types.CommunityPoolKeeper, enabledCapabilities []string, - // use DefaultIsSudoAdminFunc if you don't have a custom one - isSudoAdminFunc IsSudoAdmin, authority string, ) Keeper { permAddrs := make(map[string]authtypes.PermissionsForAddress) @@ -56,7 +48,7 @@ func NewKeeper( permAddrs[name] = authtypes.NewPermissionsForAddress(name, perms) } - return Keeper{ + k := Keeper{ cdc: cdc, storeKey: storeKey, permAddrs: permAddrs, @@ -68,14 +60,9 @@ func NewKeeper( authority: authority, enabledCapabilities: enabledCapabilities, - - IsSudoAdminFunc: isSudoAdminFunc, } -} -// DefaultIsSudoAdminFunc returns false for all addresses. -func DefaultIsSudoAdminFunc(_ context.Context, _ string) bool { - return false + return k } // GetAuthority returns the x/mint module's authority. diff --git a/x/tokenfactory/keeper/keeper_test.go b/x/tokenfactory/keeper/keeper_test.go index f895a1e..b43b887 100644 --- a/x/tokenfactory/keeper/keeper_test.go +++ b/x/tokenfactory/keeper/keeper_test.go @@ -14,6 +14,9 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" ) +// NativeDenom is denomination which is *NOT* related to tokenfactory +const NativeDenom = "any_non_tokenfactory_denom" + type KeeperTestSuite struct { apptesting.KeeperTestHelper @@ -40,6 +43,7 @@ func (suite *KeeperTestSuite) SetupTest() { suite.queryClient = types.NewQueryClient(suite.QueryHelper) suite.bankQueryClient = banktypes.NewQueryClient(suite.QueryHelper) suite.msgServer = keeper.NewMsgServerImpl(suite.App.TokenFactoryKeeper) + } func (suite *KeeperTestSuite) CreateDefaultDenom() { diff --git a/x/tokenfactory/keeper/msg_server.go b/x/tokenfactory/keeper/msg_server.go index d56645e..d0a347f 100644 --- a/x/tokenfactory/keeper/msg_server.go +++ b/x/tokenfactory/keeper/msg_server.go @@ -53,7 +53,8 @@ func (server msgServer) Mint(goCtx context.Context, msg *types.MsgMint) (*types. ctx := sdk.UnwrapSDKContext(goCtx) sudoEnabled := types.IsCapabilityEnabled(server.Keeper.enabledCapabilities, types.EnableSudoMint) - senderIsSudoAble := server.IsSudoAdminFunc(goCtx, msg.Sender) + sa := SudoAdmins{Keeper: server.Keeper} + senderIsSudoAble := sa.IsSudoAdmin(goCtx, msg.Sender) isSudo := sudoEnabled && senderIsSudoAble if !isSudo { @@ -116,7 +117,8 @@ func (server msgServer) Burn(goCtx context.Context, msg *types.MsgBurn) (*types. } sudoEnabled := types.IsCapabilityEnabled(server.Keeper.enabledCapabilities, types.EnableSudoMint) - senderIsSudoAble := server.Keeper.IsSudoAdminFunc(goCtx, msg.Sender) + sa := SudoAdmins{Keeper: server.Keeper} + senderIsSudoAble := sa.IsSudoAdmin(goCtx, msg.Sender) isSudo := sudoEnabled && senderIsSudoAble if !isSudo { diff --git a/x/tokenfactory/keeper/msg_server_test.go b/x/tokenfactory/keeper/msg_server_test.go index 05bf533..2f2e628 100644 --- a/x/tokenfactory/keeper/msg_server_test.go +++ b/x/tokenfactory/keeper/msg_server_test.go @@ -1,9 +1,9 @@ package keeper_test import ( - "context" "fmt" + "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/keeper" "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types" sdkmath "cosmossdk.io/math" @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestMintDenomMsg() { desc string amount int64 mintDenom string - admin string + sender string sudoer string expectedMessageEvents int // the valid case should emit >= 1 }{ @@ -29,13 +29,13 @@ func (suite *KeeperTestSuite) TestMintDenomMsg() { desc: "denom does not exist", amount: 10, mintDenom: "factory/osmo1t7egva48prqmzl59x5ngv4zx0dtrwewc9m7z44/evmos", - admin: suite.TestAccs[0].String(), + sender: suite.TestAccs[0].String(), }, { desc: "success case tokenfactory", amount: 10, mintDenom: suite.defaultDenom, - admin: suite.TestAccs[0].String(), + sender: suite.TestAccs[0].String(), expectedMessageEvents: 1, }, // Sudo Mints @@ -43,31 +43,36 @@ func (suite *KeeperTestSuite) TestMintDenomMsg() { desc: "successful sudo mint executed by an allowed sudoer", amount: 10, mintDenom: "unique", - admin: suite.TestAccs[0].String(), + sender: suite.TestAccs[0].String(), sudoer: suite.TestAccs[0].String(), // this user can sudo mint expectedMessageEvents: 1, }, { - desc: "invalid sudo mint from a non admin", - amount: 10, - mintDenom: "unique", - admin: suite.TestAccs[0].String(), - sudoer: "nope", + desc: "invalid sudo mint from a non admin", + amount: 10, + mintDenom: "unique", + sender: suite.TestAccs[0].String(), + sudoer: suite.TestAccs[1].String(), + expectedMessageEvents: 0, }, } { suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { ctx := suite.Ctx.WithEventManager(sdk.NewEventManager()) suite.Require().Equal(0, len(ctx.EventManager().Events())) - // Override the default IsSudoAdminFunc for testing - suite.App.TokenFactoryKeeper.IsSudoAdminFunc = func(_ context.Context, addr string) bool { - return tc.sudoer == addr + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + if tc.sudoer != "" { + suite.NoError(sa.AddSudoAdmin(suite.Ctx, tc.sudoer)) + + defer func() { + suite.NoError(sa.RemoveSudoAdmin(suite.Ctx, tc.sudoer)) + }() } suite.OverrideMsgServer(suite.App.TokenFactoryKeeper) // Test mint message - suite.msgServer.Mint(ctx, types.NewMsgMint(tc.admin, sdk.NewInt64Coin(tc.mintDenom, tc.amount))) //nolint:errcheck + suite.msgServer.Mint(ctx, types.NewMsgMint(tc.sender, sdk.NewInt64Coin(tc.mintDenom, tc.amount))) //nolint:errcheck // Ensure current number and type of event is emitted suite.AssertEventEmitted(ctx, types.TypeMsgMint, tc.expectedMessageEvents) diff --git a/x/tokenfactory/keeper/sudo_admins.go b/x/tokenfactory/keeper/sudo_admins.go new file mode 100644 index 0000000..6ebd580 --- /dev/null +++ b/x/tokenfactory/keeper/sudo_admins.go @@ -0,0 +1,80 @@ +package keeper + +import ( + "context" + + "cosmossdk.io/store/prefix" + store "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types" +) + +type SudoAdmins struct { + Keeper +} + +// GetSudoAdminsStore returns the substore for sudoers +func (k SudoAdmins) GetSudoAdminsStore(ctx sdk.Context) store.KVStore { + store := ctx.KVStore(k.storeKey) + return prefix.NewStore(store, types.GetSudoAdminsPrefix()) +} + +func (k SudoAdmins) AddSudoAdmin(ctx context.Context, admin string) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := k.GetSudoAdminsStore(sdkCtx) + + addr, err := sdk.AccAddressFromBech32(admin) + if err != nil { + return err + } + + // key = canonical address bytes + // value = dummy marker + store.Set(addr.Bytes(), []byte{1}) + + return nil +} + +func (k SudoAdmins) RemoveSudoAdmin(ctx context.Context, admin string) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := k.GetSudoAdminsStore(sdkCtx) + + addr, err := sdk.AccAddressFromBech32(admin) + if err != nil { + return err + } + + store.Delete(addr.Bytes()) + return nil +} + +func (k SudoAdmins) IsSudoAdmin(ctx context.Context, admin string) bool { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := k.GetSudoAdminsStore(sdkCtx) + + addr, err := sdk.AccAddressFromBech32(admin) + if err != nil { + return false + } + + return store.Has(addr.Bytes()) +} + +func (k SudoAdmins) GetAllSudoAdmins(ctx context.Context) []string { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := k.GetSudoAdminsStore(sdkCtx) + iterator := store.Iterator(nil, nil) + defer iterator.Close() + + prefix := sdk.GetConfig().GetBech32AccountAddrPrefix() + var admins []string + for ; iterator.Valid(); iterator.Next() { + admin, err := sdk.Bech32ifyAddressBytes(prefix, iterator.Key()) + if err != nil { + panic(err) + } + admins = append(admins, admin) + } + + return admins +} diff --git a/x/tokenfactory/keeper/sudo_admins_test.go b/x/tokenfactory/keeper/sudo_admins_test.go new file mode 100644 index 0000000..39ac68b --- /dev/null +++ b/x/tokenfactory/keeper/sudo_admins_test.go @@ -0,0 +1,90 @@ +package keeper_test + +import ( + "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/keeper" + "github.com/strangelove-ventures/tokenfactory/x/tokenfactory/types" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (suite *KeeperTestSuite) TestSudoAdminsStore() { + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + sudoAdmin := suite.TestAccs[0].String() + + suite.Require().False(sa.IsSudoAdmin(suite.Ctx, sudoAdmin)) + suite.Require().NoError(sa.AddSudoAdmin(suite.Ctx, sudoAdmin)) + suite.Require().True(sa.IsSudoAdmin(suite.Ctx, sudoAdmin)) + suite.Require().Contains(sa.GetAllSudoAdmins(suite.Ctx), sudoAdmin) + + suite.Require().NoError(sa.RemoveSudoAdmin(suite.Ctx, sudoAdmin)) + suite.Require().False(sa.IsSudoAdmin(suite.Ctx, sudoAdmin)) + suite.Require().NotContains(sa.GetAllSudoAdmins(suite.Ctx), sudoAdmin) +} + +func (suite *KeeperTestSuite) TestSudoAdminsStoreRejectsInvalidAddress() { + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + + suite.Require().Error(sa.AddSudoAdmin(suite.Ctx, "not-a-bech32-address")) + suite.Require().False(sa.IsSudoAdmin(suite.Ctx, "not-a-bech32-address")) + suite.Require().Error(sa.RemoveSudoAdmin(suite.Ctx, "not-a-bech32-address")) +} + +func (suite *KeeperTestSuite) TestSudoAdminMintAnyDenom() { + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + suite.Require().NoError(sa.AddSudoAdmin(suite.Ctx, suite.TestAccs[1].String())) + + before := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + + _, err := suite.msgServer.Mint( + suite.Ctx, + types.NewMsgMintTo( + suite.TestAccs[1].String(), + sdk.NewInt64Coin(NativeDenom, 77), + suite.TestAccs[2].String(), + ), + ) + suite.Require().NoError(err) + + after := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + suite.Require().Equal(before.AddRaw(77), after) +} + +func (suite *KeeperTestSuite) TestSudoAdminMintFailsWhenCapabilityDisabled() { + sa := keeper.SudoAdmins{Keeper: suite.App.TokenFactoryKeeper} + suite.Require().NoError(sa.AddSudoAdmin(suite.Ctx, suite.TestAccs[1].String())) + + suite.App.TokenFactoryKeeper.SetEnabledCapabilities(suite.Ctx, []string{}) + suite.OverrideMsgServer(suite.App.TokenFactoryKeeper) + + before := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + + _, err := suite.msgServer.Mint( + suite.Ctx, + types.NewMsgMintTo( + suite.TestAccs[1].String(), + sdk.NewInt64Coin(NativeDenom, 77), + suite.TestAccs[2].String(), + ), + ) + suite.Require().Error(err) + + after := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + suite.Require().Equal(before, after) +} + +func (suite *KeeperTestSuite) TestNonSudoCannotMintAnyDenom() { + before := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + + _, err := suite.msgServer.Mint( + suite.Ctx, + types.NewMsgMintTo( + suite.TestAccs[1].String(), + sdk.NewInt64Coin(NativeDenom, 77), + suite.TestAccs[2].String(), + ), + ) + suite.Require().Error(err) + + after := suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[2], NativeDenom).Amount + suite.Require().Equal(before, after) +} diff --git a/x/tokenfactory/types/capabilities.go b/x/tokenfactory/types/capabilities.go index e201484..57e4271 100644 --- a/x/tokenfactory/types/capabilities.go +++ b/x/tokenfactory/types/capabilities.go @@ -7,11 +7,10 @@ const ( // If disabled (not present), only admin of a denom or sudoer can execute the burn. EnableBurnOwn = "enable_burn_own" EnableBurnFrom = "enable_burn_from" - // Allows addresses of your choosing to mint tokens based on specific conditions. - // via the IsSudoAdminFunc. + // Allows addresses registered as sudo admins imn genesis store to mint tokens of *ANY* denominations. // NOTE: with SudoMint enabled, the sudo admin can mint `any` token, not just tokenfactory tokens. // This is intended behavior as requested by other teams, rather than having its own module with very minor logic. - // If you do not wish for this behavior, write your own and do not use this capability. + // If you do not wish for this behavior, then either do NOT enable this capability, or implement your own logic. EnableSudoMint = "enable_admin_sudo_mint" // EnableCommunityPoolFeeFunding sends tokens to the community pool when a new fee is charged (if one is set in params). // This is useful for ICS chains, or networks who wish to just have the fee tokens burned (not gas fees, just the extra on top). diff --git a/x/tokenfactory/types/genesis.go b/x/tokenfactory/types/genesis.go index 408dc5f..8e65101 100644 --- a/x/tokenfactory/types/genesis.go +++ b/x/tokenfactory/types/genesis.go @@ -16,6 +16,7 @@ func DefaultGenesis() *GenesisState { return &GenesisState{ Params: DefaultParams(), FactoryDenoms: []GenesisDenom{}, + SudoAdmins: []string{}, } } @@ -27,13 +28,14 @@ func (gs GenesisState) Validate() error { return err } - seenDenoms := map[string]bool{} + seenDenoms := NewSet[string]() + seenSudoAdmins := NewSet[string]() for _, denom := range gs.GetFactoryDenoms() { - if seenDenoms[denom.GetDenom()] { + if seenDenoms.Contains(denom.GetDenom()) { return errorsmod.Wrapf(ErrInvalidGenesis, "duplicate denom: %s", denom.GetDenom()) } - seenDenoms[denom.GetDenom()] = true + seenDenoms.Add(denom.GetDenom()) _, _, err := DeconstructDenom(denom.GetDenom()) if err != nil { @@ -48,5 +50,16 @@ func (gs GenesisState) Validate() error { } } + for _, sudoAdmin := range gs.GetSudoAdmins() { + if seenSudoAdmins.Contains(sudoAdmin) { + return errorsmod.Wrapf(ErrInvalidGenesis, "duplicate sudo admin: %s", sudoAdmin) + } + seenSudoAdmins.Add(sudoAdmin) + + if _, err := sdk.AccAddressFromBech32(sudoAdmin); err != nil { + return errorsmod.Wrapf(ErrInvalidGenesis, "invalid sudo admin address (%s)", err) + } + } + return nil } diff --git a/x/tokenfactory/types/genesis.pb.go b/x/tokenfactory/types/genesis.pb.go index 26b7374..8d3c762 100644 --- a/x/tokenfactory/types/genesis.pb.go +++ b/x/tokenfactory/types/genesis.pb.go @@ -5,6 +5,7 @@ package types import ( fmt "fmt" + _ "github.com/cosmos/cosmos-proto" _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/cosmos/gogoproto/proto" io "io" @@ -28,6 +29,7 @@ type GenesisState struct { // params defines the parameters of the module. Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"` FactoryDenoms []GenesisDenom `protobuf:"bytes,2,rep,name=factory_denoms,json=factoryDenoms,proto3" json:"factory_denoms" yaml:"factory_denoms"` + SudoAdmins []string `protobuf:"bytes,3,rep,name=sudo_admins,json=sudoAdmins,proto3" json:"sudo_admins,omitempty" yaml:"sudo_admins"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -77,6 +79,13 @@ func (m *GenesisState) GetFactoryDenoms() []GenesisDenom { return nil } +func (m *GenesisState) GetSudoAdmins() []string { + if m != nil { + return m.SudoAdmins + } + return nil +} + // GenesisDenom defines a tokenfactory denom that is defined within genesis // state. The structure contains DenomAuthorityMetadata which defines the // denom's admin. @@ -142,31 +151,35 @@ func init() { } var fileDescriptor_5749c3f71850298b = []byte{ - // 372 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xd2, 0xca, 0x2f, 0xce, 0xcd, - 0x2f, 0xce, 0x2c, 0xd6, 0x2f, 0xc9, 0xcf, 0x4e, 0xcd, 0x4b, 0x4b, 0x4c, 0x2e, 0xc9, 0x2f, 0xaa, - 0xd4, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, - 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x81, 0xaa, 0xd5, 0x43, 0x56, 0xab, 0x07, 0x55, - 0x2b, 0x25, 0x92, 0x9e, 0x9f, 0x9e, 0x0f, 0x56, 0xa8, 0x0f, 0x62, 0x41, 0xf4, 0x48, 0x99, 0xe0, - 0x35, 0x3f, 0xb1, 0xb4, 0x24, 0x23, 0xbf, 0x28, 0xb3, 0xa4, 0xd2, 0x37, 0xb5, 0x24, 0x31, 0x25, - 0xb1, 0x24, 0x11, 0xaa, 0x4b, 0x13, 0xaf, 0xae, 0x82, 0xc4, 0xa2, 0xc4, 0x5c, 0xa8, 0xa3, 0x94, - 0x8e, 0x30, 0x72, 0xf1, 0xb8, 0x43, 0x9c, 0x19, 0x5c, 0x92, 0x58, 0x92, 0x2a, 0xe4, 0xc4, 0xc5, - 0x06, 0x51, 0x20, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x6d, 0xa4, 0xa2, 0x87, 0xcf, 0xd9, 0x7a, 0x01, - 0x60, 0xb5, 0x4e, 0x2c, 0x27, 0xee, 0xc9, 0x33, 0x04, 0x41, 0x75, 0x0a, 0x15, 0x70, 0xf1, 0x41, - 0xd5, 0xc5, 0xa7, 0xa4, 0xe6, 0xe5, 0xe7, 0x16, 0x4b, 0x30, 0x29, 0x30, 0x6b, 0x70, 0x1b, 0x69, - 0xe1, 0x37, 0x0b, 0xea, 0x0e, 0x17, 0x90, 0x16, 0x27, 0x59, 0x90, 0x89, 0x9f, 0xee, 0xc9, 0x8b, - 0x56, 0x26, 0xe6, 0xe6, 0x58, 0x29, 0xa1, 0x9a, 0xa7, 0x14, 0xc4, 0x0b, 0x15, 0x70, 0x81, 0xf0, - 0x8f, 0x22, 0xbc, 0x01, 0x16, 0x11, 0x52, 0xe3, 0x62, 0x05, 0x2b, 0x05, 0xfb, 0x82, 0xd3, 0x49, - 0xe0, 0xd3, 0x3d, 0x79, 0x1e, 0x88, 0x49, 0x60, 0x61, 0xa5, 0x20, 0x88, 0xb4, 0x50, 0x1b, 0x23, - 0x97, 0x10, 0x3c, 0x18, 0xe3, 0x73, 0xa1, 0xe1, 0x28, 0xc1, 0x04, 0xf6, 0xbb, 0x09, 0x7e, 0xf7, - 0x82, 0x6d, 0x72, 0x44, 0x8f, 0x03, 0x27, 0x45, 0xa8, 0xcb, 0x25, 0x21, 0xf6, 0x61, 0x9a, 0xae, - 0x14, 0x24, 0x88, 0x11, 0x73, 0x56, 0x2c, 0x2f, 0x16, 0xc8, 0x33, 0x3a, 0x45, 0x9f, 0x78, 0x24, - 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, - 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x63, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, - 0x72, 0x7e, 0xae, 0x7e, 0x71, 0x49, 0x51, 0x62, 0x5e, 0x7a, 0x6a, 0x4e, 0x7e, 0x59, 0xaa, 0x6e, - 0x59, 0x6a, 0x5e, 0x49, 0x69, 0x51, 0x2a, 0x5a, 0x5c, 0x57, 0xa0, 0x72, 0x4b, 0x2a, 0x0b, 0x52, - 0x8b, 0x93, 0xd8, 0xc0, 0x51, 0x6e, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x83, 0x56, 0x6d, 0x5f, - 0xb5, 0x02, 0x00, 0x00, + // 438 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0x4f, 0x8b, 0xd3, 0x40, + 0x18, 0xc6, 0x33, 0xed, 0xba, 0xb0, 0xd3, 0x55, 0x74, 0x58, 0x21, 0x5b, 0x34, 0xa9, 0x41, 0xa4, + 0x2e, 0x6c, 0xc2, 0xd6, 0x3d, 0xed, 0x2d, 0x41, 0xf0, 0x24, 0x48, 0x7a, 0xd3, 0x43, 0x98, 0x36, + 0x63, 0x1a, 0x6c, 0x32, 0x61, 0x66, 0x52, 0xcc, 0x17, 0xf0, 0xec, 0xd5, 0x9b, 0x1f, 0xc2, 0xaf, + 0x20, 0xf4, 0x58, 0x3c, 0x79, 0x2a, 0xd2, 0x5e, 0x3c, 0xf7, 0x13, 0x2c, 0x99, 0x19, 0x4a, 0xff, + 0x40, 0x6e, 0xc9, 0xcc, 0xef, 0x79, 0xde, 0x79, 0x9f, 0xf7, 0x85, 0x57, 0x94, 0x67, 0x94, 0xa7, + 0xdc, 0x13, 0xf4, 0x0b, 0xc9, 0x3f, 0xe3, 0xb1, 0xa0, 0xac, 0xf2, 0x66, 0x37, 0x23, 0x22, 0xf0, + 0x8d, 0x97, 0x90, 0x9c, 0xf0, 0x94, 0xbb, 0x05, 0xa3, 0x82, 0xa2, 0x67, 0x9a, 0x75, 0x77, 0x59, + 0x57, 0xb3, 0xdd, 0x8b, 0x84, 0x26, 0x54, 0x82, 0x5e, 0xfd, 0xa5, 0x34, 0xdd, 0xdb, 0x46, 0x7f, + 0x5c, 0x8a, 0x09, 0x65, 0xa9, 0xa8, 0xde, 0x13, 0x81, 0x63, 0x2c, 0xb0, 0x56, 0xbd, 0x6e, 0x54, + 0x15, 0x98, 0xe1, 0x4c, 0x3f, 0xaa, 0x7b, 0x39, 0x96, 0x6c, 0xa4, 0x2a, 0xab, 0x1f, 0x75, 0xe5, + 0xfc, 0x68, 0xc1, 0xf3, 0x77, 0xaa, 0x83, 0xa1, 0xc0, 0x82, 0xa0, 0x00, 0x9e, 0x2a, 0xad, 0x09, + 0x7a, 0xa0, 0xdf, 0x19, 0xbc, 0x74, 0x9b, 0x3a, 0x72, 0x3f, 0x48, 0x36, 0x38, 0x99, 0x2f, 0x6d, + 0x23, 0xd4, 0x4a, 0x54, 0xc0, 0x47, 0x9a, 0x8b, 0x62, 0x92, 0xd3, 0x8c, 0x9b, 0xad, 0x5e, 0xbb, + 0xdf, 0x19, 0x5c, 0x35, 0x7b, 0xe9, 0x77, 0xbc, 0xad, 0x25, 0xc1, 0xf3, 0xda, 0x71, 0xb3, 0xb4, + 0x9f, 0x56, 0x38, 0x9b, 0xde, 0x39, 0xfb, 0x7e, 0x4e, 0xf8, 0x50, 0x1f, 0x48, 0x98, 0xa3, 0x21, + 0xec, 0xf0, 0x32, 0xa6, 0x11, 0x8e, 0xb3, 0x34, 0xe7, 0x66, 0xbb, 0xd7, 0xee, 0x9f, 0x05, 0x03, + 0x6d, 0x81, 0x94, 0xc5, 0x0e, 0xe0, 0xfc, 0xf9, 0x75, 0x7d, 0xa1, 0x73, 0xf0, 0xe3, 0x98, 0x11, + 0xce, 0x87, 0x82, 0xa5, 0x79, 0x12, 0xc2, 0x9a, 0xf2, 0x15, 0xf4, 0x1b, 0x6c, 0xb3, 0x91, 0x65, + 0xd0, 0x2b, 0xf8, 0x40, 0xd6, 0x97, 0xd1, 0x9c, 0x05, 0x8f, 0x37, 0x4b, 0xfb, 0x5c, 0x79, 0xcb, + 0x63, 0x27, 0x54, 0xd7, 0xe8, 0x1b, 0x80, 0x68, 0x3b, 0xb6, 0x28, 0xd3, 0x73, 0x33, 0x5b, 0x32, + 0xd0, 0xdb, 0xe6, 0x10, 0x64, 0x25, 0xff, 0x70, 0xe6, 0xc1, 0x0b, 0xdd, 0xcb, 0xa5, 0xaa, 0x77, + 0xec, 0xee, 0x84, 0x4f, 0x8e, 0x36, 0xe5, 0xee, 0xe4, 0xff, 0x4f, 0x1b, 0x04, 0x9f, 0xe6, 0x2b, + 0x0b, 0x2c, 0x56, 0x16, 0xf8, 0xb7, 0xb2, 0xc0, 0xf7, 0xb5, 0x65, 0x2c, 0xd6, 0x96, 0xf1, 0x77, + 0x6d, 0x19, 0x1f, 0xfd, 0x24, 0x15, 0x93, 0x72, 0xe4, 0x8e, 0x69, 0xe6, 0x71, 0xc1, 0x70, 0x9e, + 0x90, 0x29, 0x9d, 0x91, 0xeb, 0x19, 0xc9, 0x45, 0xc9, 0xc8, 0xc1, 0x6e, 0x7d, 0xdd, 0xff, 0x15, + 0x55, 0x41, 0xf8, 0xe8, 0x54, 0xee, 0xd1, 0x9b, 0xfb, 0x00, 0x00, 0x00, 0xff, 0xff, 0x1a, 0x9c, + 0x73, 0x96, 0x25, 0x03, 0x00, 0x00, } func (this *GenesisDenom) Equal(that interface{}) bool { @@ -216,6 +229,15 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.SudoAdmins) > 0 { + for iNdEx := len(m.SudoAdmins) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.SudoAdmins[iNdEx]) + copy(dAtA[i:], m.SudoAdmins[iNdEx]) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.SudoAdmins[iNdEx]))) + i-- + dAtA[i] = 0x1a + } + } if len(m.FactoryDenoms) > 0 { for iNdEx := len(m.FactoryDenoms) - 1; iNdEx >= 0; iNdEx-- { { @@ -308,6 +330,12 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if len(m.SudoAdmins) > 0 { + for _, s := range m.SudoAdmins { + l = len(s) + n += 1 + l + sovGenesis(uint64(l)) + } + } return n } @@ -428,6 +456,38 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SudoAdmins", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SudoAdmins = append(m.SudoAdmins, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/x/tokenfactory/types/genesis_test.go b/x/tokenfactory/types/genesis_test.go index b5cb8de..fecebd9 100644 --- a/x/tokenfactory/types/genesis_test.go +++ b/x/tokenfactory/types/genesis_test.go @@ -85,6 +85,36 @@ func TestGenesisState_Validate(t *testing.T) { }, valid: false, }, + + { + desc: "valid sudo admins", + genState: &types.GenesisState{ + SudoAdmins: []string{ + "cosmos1t7egva48prqmzl59x5ngv4zx0dtrwewcdqdjr8", + "cosmos15czt5nhlnvayqq37xun9s9yus0d6y26dx74r5p", + }, + }, + valid: true, + }, + { + desc: "invalid sudo admin", + genState: &types.GenesisState{ + SudoAdmins: []string{ + "moose", + }, + }, + valid: false, + }, + { + desc: "duplicate sudo admin", + genState: &types.GenesisState{ + SudoAdmins: []string{ + "cosmos1t7egva48prqmzl59x5ngv4zx0dtrwewcdqdjr8", + "cosmos1t7egva48prqmzl59x5ngv4zx0dtrwewcdqdjr8", + }, + }, + valid: false, + }, { desc: "multiple denoms", genState: &types.GenesisState{ diff --git a/x/tokenfactory/types/keys.go b/x/tokenfactory/types/keys.go index bb52b5d..9fba92c 100644 --- a/x/tokenfactory/types/keys.go +++ b/x/tokenfactory/types/keys.go @@ -30,7 +30,7 @@ var ( DenomAuthorityMetadataKey = "authoritymetadata" DenomsPrefixKey = "denoms" CreatorPrefixKey = "creator" - AdminPrefixKey = "admin" + SudoAdminsPrefixKey = "sudoadmins" ) // GetDenomPrefixStore returns the store prefix where all the data associated with a specific denom @@ -49,3 +49,8 @@ func GetCreatorPrefix(creator string) []byte { func GetCreatorsPrefix() []byte { return []byte(strings.Join([]string{CreatorPrefixKey, ""}, KeySeparator)) } + +// GetSudoAdminsPrefix returns the store prefix where a list of all sudo admin addresses are stored +func GetSudoAdminsPrefix() []byte { + return []byte(strings.Join([]string{SudoAdminsPrefixKey, ""}, KeySeparator)) +} diff --git a/x/tokenfactory/types/set.go b/x/tokenfactory/types/set.go new file mode 100644 index 0000000..ca5ee0c --- /dev/null +++ b/x/tokenfactory/types/set.go @@ -0,0 +1,185 @@ +package types + +// Set is a generic set implementation using map +type Set[T comparable] struct { + m map[T]struct{} +} + +// NewSet creates a new set with optional initial values +func NewSet[T comparable](values ...T) *Set[T] { + s := &Set[T]{m: make(map[T]struct{})} + s.Add(values...) + return s +} + +// SetFromSlice creates a set from a slice +func SetFromSlice[T comparable](values []T) *Set[T] { + return NewSet(values...) +} + +// Add inserts one or more elements +func (s *Set[T]) Add(values ...T) { + for _, v := range values { + s.m[v] = struct{}{} + } +} + +// Remove deletes one or more elements +func (s *Set[T]) Remove(values ...T) { + for _, v := range values { + delete(s.m, v) + } +} + +// Contains checks if element exists +func (s *Set[T]) Contains(v T) bool { + _, ok := s.m[v] + return ok +} + +// Len returns number of elements +func (s *Set[T]) Len() int { + return len(s.m) +} + +// IsEmpty checks if set is empty +func (s *Set[T]) IsEmpty() bool { + return len(s.m) == 0 +} + +// Clear removes all elements +func (s *Set[T]) Clear() { + s.m = make(map[T]struct{}) +} + +// ToSlice returns elements as slice +// Warning: NON-deterministic! +func (s *Set[T]) ToSlice() []T { + result := make([]T, 0, len(s.m)) + for v := range s.m { + result = append(result, v) + } + return result +} + +// Copy creates a shallow copy +func (s *Set[T]) Copy() *Set[T] { + newSet := NewSet[T]() + for v := range s.m { + newSet.m[v] = struct{}{} + } + return newSet +} + +// Equals checks if two sets are equal +func (s *Set[T]) Equals(other *Set[T]) bool { + if s.Len() != other.Len() { + return false + } + for v := range s.m { + if !other.Contains(v) { + return false + } + } + return true +} + +// IsSubset checks if s ⊆ other +func (s *Set[T]) IsSubset(other *Set[T]) bool { + for v := range s.m { + if !other.Contains(v) { + return false + } + } + return true +} + +// IsSuperset checks if s ⊇ other +func (s *Set[T]) IsSuperset(other *Set[T]) bool { + return other.IsSubset(s) +} + +// Union returns a new set with elements from both sets +func (s *Set[T]) Union(other *Set[T]) *Set[T] { + result := s.Copy() + for v := range other.m { + result.m[v] = struct{}{} + } + return result +} + +// Intersection returns common elements +func (s *Set[T]) Intersection(other *Set[T]) *Set[T] { + result := NewSet[T]() + for v := range s.m { + if other.Contains(v) { + result.m[v] = struct{}{} + } + } + return result +} + +// Difference returns elements in s but not in other +func (s *Set[T]) Difference(other *Set[T]) *Set[T] { + result := NewSet[T]() + for v := range s.m { + if !other.Contains(v) { + result.m[v] = struct{}{} + } + } + return result +} + +// SymmetricDifference returns elements in either set but not both +func (s *Set[T]) SymmetricDifference(other *Set[T]) *Set[T] { + result := NewSet[T]() + for v := range s.m { + if !other.Contains(v) { + result.m[v] = struct{}{} + } + } + for v := range other.m { + if !s.Contains(v) { + result.m[v] = struct{}{} + } + } + return result +} + +// ForEach iterates over elements +func (s *Set[T]) ForEach(fn func(T)) { + for v := range s.m { + fn(v) + } +} + +// Filter returns a new set with elements matching predicate +func (s *Set[T]) Filter(fn func(T) bool) *Set[T] { + result := NewSet[T]() + for v := range s.m { + if fn(v) { + result.m[v] = struct{}{} + } + } + return result +} + +// Any returns true if any element matches predicate +func (s *Set[T]) Any(fn func(T) bool) bool { + for v := range s.m { + if fn(v) { + return true + } + } + return false +} + +// All returns true if all elements match predicate +func (s *Set[T]) All(fn func(T) bool) bool { + for v := range s.m { + if !fn(v) { + return false + } + } + return true +}