Skip to content

Commit

Permalink
feat(mint): customized mint module (#169)
Browse files Browse the repository at this point in the history
* feat(mint): port mint module from cosmos sdk v0.50.7

* feat(mint): port mint module from cosmos sdk v0.50.7

* feat(mint): port mint module from cosmos sdk v0.50.7

* feat(mint): port mint module from cosmos sdk v0.50.7

* feat(mint): remove useless parameters

* feat(mint): new inflation function

* feat(mint): mint parameters change event log processing

* feat(mint): mint module api

* feat(mint): placeholder for param change events

* feat(mint): fix unit test

* feat(mint): fix based on comments

* feat(mint): fix based on comments

* feat(mint): update readme

* feat(mint): update readme
  • Loading branch information
ezreal1997 authored Oct 8, 2024
1 parent e43f5e9 commit c7ee3a2
Show file tree
Hide file tree
Showing 44 changed files with 3,219 additions and 15 deletions.
7 changes: 6 additions & 1 deletion client/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/piplabs/story/client/app/keepers"
"github.com/piplabs/story/client/comet"
evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
mintkeeper "github.com/piplabs/story/client/x/mint/keeper"
"github.com/piplabs/story/lib/errors"
"github.com/piplabs/story/lib/ethclient"

Expand All @@ -36,7 +37,6 @@ import (
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/gov" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/slashing" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side-effects
)
Expand Down Expand Up @@ -95,6 +95,7 @@ func newApp(
&app.Keepers.EpochsKeeper,
&app.Keepers.EvmStakingKeeper,
&app.Keepers.EVMEngKeeper,
&app.Keepers.MintKeeper,
); err != nil {
return nil, errors.Wrap(err, "dep inject")
}
Expand Down Expand Up @@ -207,3 +208,7 @@ func (a App) GetDistrKeeper() distrkeeper.Keeper {
func (a App) GetUpgradeKeeper() *upgradekeeper.Keeper {
return a.Keepers.UpgradeKeeper
}

func (a App) GetMintKeeper() mintkeeper.Keeper {
return a.Keepers.MintKeeper
}
6 changes: 3 additions & 3 deletions client/app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
distrmodulev1 "cosmossdk.io/api/cosmos/distribution/module/v1"
genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1"
govmodulev1 "cosmossdk.io/api/cosmos/gov/module/v1"
mintmodulev1 "cosmossdk.io/api/cosmos/mint/module/v1"
slashingmodulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
Expand All @@ -26,7 +25,6 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Expand All @@ -36,6 +34,8 @@ import (
evmenginetypes "github.com/piplabs/story/client/x/evmengine/types"
evmstakingmodule "github.com/piplabs/story/client/x/evmstaking/module"
evmstakingtypes "github.com/piplabs/story/client/x/evmstaking/types"
mintmodule "github.com/piplabs/story/client/x/mint/module"
minttypes "github.com/piplabs/story/client/x/mint/types"
)

// Bech32HRP is the human-readable-part of the Bech32 address format.
Expand Down Expand Up @@ -215,7 +215,7 @@ var (
},
{
Name: minttypes.ModuleName,
Config: appconfig.WrapAny(&mintmodulev1.Module{}),
Config: appconfig.WrapAny(&mintmodule.Module{}),
},
},
})
Expand Down
2 changes: 2 additions & 0 deletions client/app/keepers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
epochskeeper "github.com/piplabs/story/client/x/epochs/keeper"
evmengkeeper "github.com/piplabs/story/client/x/evmengine/keeper"
evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
mintkeeper "github.com/piplabs/story/client/x/mint/keeper"
)

// Keepers includes all possible keepers. We separated it into a separate struct to make it easier to scaffold upgrades.
Expand All @@ -36,4 +37,5 @@ type Keepers struct {
EvmStakingKeeper *evmstakingkeeper.Keeper
EVMEngKeeper *evmengkeeper.Keeper
EpochsKeeper *epochskeeper.Keeper
MintKeeper mintkeeper.Keeper
}
28 changes: 28 additions & 0 deletions client/server/mint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package server

import (
"net/http"

"github.com/piplabs/story/client/server/utils"
"github.com/piplabs/story/client/x/mint/keeper"
minttypes "github.com/piplabs/story/client/x/mint/types"
)

func (s *Server) initMintRoute() {
s.httpMux.HandleFunc("/mint/params", utils.SimpleWrap(s.aminoCodec, s.GetMintParams))
}

// GetMintParams queries params of the mint module.
func (s *Server) GetMintParams(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetMintKeeper()).Params(queryContext, &minttypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}
3 changes: 3 additions & 0 deletions client/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/gorilla/mux"

evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
mintkeeper "github.com/piplabs/story/client/x/mint/keeper"
)

type Store interface {
Expand All @@ -39,6 +40,7 @@ type Store interface {
GetBankKeeper() bankkeeper.Keeper
GetDistrKeeper() distrkeeper.Keeper
GetUpgradeKeeper() *upgradekeeper.Keeper
GetMintKeeper() mintkeeper.Keeper
}

type Server struct {
Expand Down Expand Up @@ -121,6 +123,7 @@ func (s *Server) registerHandle() {
s.initSlashingRoute()
s.initStakingRoute()
s.initUpgradeRoute()
s.initMintRoute()
}

func (s *Server) createQueryContextByHeader(r *http.Request) (sdk.Context, error) {
Expand Down
10 changes: 6 additions & 4 deletions client/x/evmengine/keeper/abci_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestKeeper_PrepareProposal(t *testing.T) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

if tt.setupMocks != nil {
tt.setupMocks(esk)
Expand All @@ -230,7 +231,7 @@ func TestKeeper_PrepareProposal(t *testing.T) {
tt.mockEngine.EngineClient, err = ethclient.NewEngineMock(storeKey)
require.NoError(t, err)

k, err := NewKeeper(cdc, storeService, &tt.mockEngine, &tt.mockClient, txConfig, ak, esk, uk)
k, err := NewKeeper(cdc, storeService, &tt.mockEngine, &tt.mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
k.SetValidatorAddress(common.BytesToAddress([]byte("test")))
populateGenesisHead(ctx, t, k)
Expand Down Expand Up @@ -261,11 +262,11 @@ func TestKeeper_PrepareProposal(t *testing.T) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)

mk := moduletestutil.NewMockMintKeeper(ctrl)
// Expected call for PeekEligibleWithdrawals
esk.EXPECT().PeekEligibleWithdrawals(gomock.Any()).Return(nil, nil).AnyTimes()

keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
keeper.SetValidatorAddress(common.BytesToAddress([]byte("test")))
populateGenesisHead(ctx, t, keeper)
Expand Down Expand Up @@ -446,6 +447,7 @@ func TestKeeper_PostFinalize(t *testing.T) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

if tt.setupMocks != nil {
tt.setupMocks(esk)
Expand All @@ -464,7 +466,7 @@ func TestKeeper_PostFinalize(t *testing.T) {
tt.mockEngine.EngineClient, err = ethclient.NewEngineMock(storeKey)
require.NoError(t, err)

k, err := NewKeeper(cdc, storeService, &tt.mockEngine, &tt.mockClient, txConfig, ak, esk, uk)
k, err := NewKeeper(cdc, storeService, &tt.mockEngine, &tt.mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
k.SetCometAPI(cmtAPI)
k.SetValidatorAddress(nxtAddr)
Expand Down
3 changes: 3 additions & 0 deletions client/x/evmengine/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Keeper struct {
accountKeeper types.AccountKeeper
evmstakingKeeper types.EvmStakingKeeper
upgradeKeeper types.UpgradeKeeper
mintKeeper types.MintKeeper

upgradeContract *bindings.UpgradeEntrypoint

Expand All @@ -64,6 +65,7 @@ func NewKeeper(
ak types.AccountKeeper,
esk types.EvmStakingKeeper,
uk types.UpgradeKeeper,
mk types.MintKeeper,
) (*Keeper, error) {
schema := &ormv1alpha1.ModuleSchemaDescriptor{SchemaFile: []*ormv1alpha1.ModuleSchemaDescriptor_FileEntry{
{Id: 1, ProtoFileName: File_client_x_evmengine_keeper_evmengine_proto.Path()},
Expand Down Expand Up @@ -94,6 +96,7 @@ func NewKeeper(
evmstakingKeeper: esk,
upgradeKeeper: uk,
upgradeContract: upgradeContract,
mintKeeper: mk,
}, nil
}

Expand Down
6 changes: 4 additions & 2 deletions client/x/evmengine/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ func createTestKeeper(t *testing.T) (context.Context, *Keeper) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

ctx, storeKey, storeService := setupCtxStore(t, &header)
mockEngine, err := newMockEngineAPI(storeKey, 0)
require.NoError(t, err)

keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
keeper.SetCometAPI(cmtAPI)

Expand All @@ -78,11 +79,12 @@ func createKeeper(t *testing.T, args args) (sdk.Context, *mockCometAPI, *Keeper)
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

ctx, storeKey, storeService := setupCtxStore(t, &header)
mockEngine, err := newMockEngineAPI(storeKey, 0)
require.NoError(t, err)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
keeper.SetCometAPI(cmtAPI)
keeper.SetValidatorAddress(nxtAddr)
Expand Down
3 changes: 3 additions & 0 deletions client/x/evmengine/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (s msgServer) ExecutionPayload(ctx context.Context, msg *types.MsgExecution
if err := s.ProcessUpgradeEvents(ctx, payload.Number-1, msg.PrevPayloadEvents); err != nil {
return nil, errors.Wrap(err, "deliver upgrade-related event logs")
}
if err := s.mintKeeper.ProcessInflationEvents(ctx, payload.Number-1, msg.PrevPayloadEvents); err != nil {
return nil, errors.Wrap(err, "deliver inflation-related event logs")
}

if err := s.updateExecutionHead(ctx, payload); err != nil {
return nil, errors.Wrap(err, "update execution head")
Expand Down
4 changes: 3 additions & 1 deletion client/x/evmengine/keeper/msg_server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Test_msgServer_ExecutionPayload(t *testing.T) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

cmtAPI := newMockCometAPI(t, nil)
// set the header and proposer so we have the correct next proposer
Expand All @@ -53,7 +54,7 @@ func Test_msgServer_ExecutionPayload(t *testing.T) {
evmLogProc := mockLogProvider{deliverErr: errors.New("test error")}
mockEngine, err := newMockEngineAPI(storeKey, 2)
require.NoError(t, err)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
keeper.SetCometAPI(cmtAPI)
keeper.SetValidatorAddress(nxtAddr)
Expand Down Expand Up @@ -99,6 +100,7 @@ func Test_msgServer_ExecutionPayload(t *testing.T) {
setup: func(c context.Context) sdk.Context {
esk.EXPECT().DequeueEligibleWithdrawals(c).Return(nil, nil)
esk.EXPECT().ProcessStakingEvents(c, gomock.Any(), gomock.Any()).Return(nil)
mk.EXPECT().ProcessInflationEvents(c, gomock.Any(), gomock.Any()).Return(nil)

return sdk.UnwrapSDKContext(c)
},
Expand Down
4 changes: 2 additions & 2 deletions client/x/evmengine/keeper/proposal_server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func Test_proposalServer_ExecutionPayload(t *testing.T) {
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)

mk := moduletestutil.NewMockMintKeeper(ctrl)
esk.EXPECT().PeekEligibleWithdrawals(gomock.Any()).Return(nil, nil).AnyTimes()

sdkCtx, storeKey, storeService := setupCtxStore(t, &cmtproto.Header{AppHash: tutil.RandomHash().Bytes()})
sdkCtx = sdkCtx.WithExecMode(sdk.ExecModeFinalize)
mockEngine, err := newMockEngineAPI(storeKey, 0)
require.NoError(t, err)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
populateGenesisHead(sdkCtx, t, keeper)
propSrv := NewProposalServer(keeper)
Expand Down
3 changes: 2 additions & 1 deletion client/x/evmengine/keeper/upgrades_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,13 @@ func setupTestEnvironment(t *testing.T) (*Keeper, sdk.Context, *gomock.Controlle
ak := moduletestutil.NewMockAccountKeeper(ctrl)
esk := moduletestutil.NewMockEvmStakingKeeper(ctrl)
uk := moduletestutil.NewMockUpgradeKeeper(ctrl)
mk := moduletestutil.NewMockMintKeeper(ctrl)

ctx, storeKey, storeService := setupCtxStore(t, &header)
mockEngine, err := newMockEngineAPI(storeKey, 0)
require.NoError(t, err)

keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk)
keeper, err := NewKeeper(cdc, storeService, &mockEngine, mockClient, txConfig, ak, esk, uk, mk)
require.NoError(t, err)
keeper.SetCometAPI(cmtAPI)
nxtAddr, err := k1util.PubKeyToAddress(cmtAPI.validatorSet.Validators[1].PubKey)
Expand Down
2 changes: 2 additions & 0 deletions client/x/evmengine/module/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ModuleInputs struct {
AccountKeeper types.AccountKeeper
EvmStakingKeeper types.EvmStakingKeeper
UpgradeKeeper types.UpgradeKeeper
MintKeeper types.MintKeeper
}

type ModuleOutputs struct {
Expand All @@ -56,6 +57,7 @@ func ProvideModule(in ModuleInputs) (ModuleOutputs, error) {
in.AccountKeeper,
in.EvmStakingKeeper,
in.UpgradeKeeper,
in.MintKeeper,
)
if err != nil {
return ModuleOutputs{}, err
Expand Down
37 changes: 37 additions & 0 deletions client/x/evmengine/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions client/x/evmengine/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ type EvmStakingKeeper interface {
type UpgradeKeeper interface {
ScheduleUpgrade(ctx context.Context, plan upgradetypes.Plan) error
}

type MintKeeper interface {
ProcessInflationEvents(ctx context.Context, height uint64, logs []*EVMEvent) error
}
Loading

0 comments on commit c7ee3a2

Please sign in to comment.