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(singularity): add singularity to mint and evmstaking module #202

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/x/evmstaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func (k *Keeper) EndBlock(ctx context.Context) (abci.ValidatorUpdates, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
blockHeight := sdkCtx.BlockHeader().Height

isSingularity, err := k.IsSingularity(ctx)
if err != nil {
return nil, err
}

if isSingularity {
return nil, nil
}

valUpdates, unbondedEntries, err := k.stakingKeeper.EndBlockerWithUnbondedEntries(ctx)
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions client/x/evmstaking/keeper/redelegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import (
)

func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStakingRedelegate) error {
isInSingularity, err := k.IsSingularity(ctx)
if err != nil {
return errors.Wrap(err, "check if it is singularity")
}

if isInSingularity {
log.Info(ctx, "Relegation event detected, but it is not processed since current block is singularity")
return nil
}

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return errors.Wrap(err, "compress depositor pubkey")
Expand Down
23 changes: 23 additions & 0 deletions client/x/evmstaking/keeper/singularity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
"context"

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

func (k *Keeper) IsSingularity(ctx context.Context) (bool, error) {
params, err := k.GetParams(ctx)
if err != nil {
return false, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
blockHeight := sdkCtx.BlockHeader().Height

if blockHeight < int64(params.SingularityHeight) {
return true, nil
}

return false, nil
}
10 changes: 10 additions & 0 deletions client/x/evmstaking/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ func (k Keeper) EnqueueEligiblePartialWithdrawal(ctx context.Context, withdrawal
}

func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStakingWithdraw) error {
isInSingularity, err := k.IsSingularity(ctx)
if err != nil {
return errors.Wrap(err, "check if it is singularity")
}

if isInSingularity {
log.Info(ctx, "Withdraw event detected, but it is not processed since current block is singularity")
return nil
}

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return errors.Wrap(err, "compress depositor pubkey")
Expand Down
2 changes: 2 additions & 0 deletions client/x/evmstaking/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func TestNewGenesisState(t *testing.T) {
10,
20,
30,
30,
),
expectedGenesisState: &types.GenesisState{
Params: types.NewParams(
10,
20,
30,
30,
),
ValidatorSweepIndex: zeroVallidatorSweepIndex,
},
Expand Down
14 changes: 13 additions & 1 deletion client/x/evmstaking/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ const (
DefaultMaxSweepPerBlock uint32 = 64

DefaultMinPartialWithdrawalAmount uint64 = 600_000

DefaultSingularityHeight uint64 = 1209600 // 42 days with 35 seconds block time
)

// NewParams creates a new Params instance.
func NewParams(maxWithdrawalPerBlock uint32, maxSweepPerBlock uint32, minPartialWithdrawalAmount uint64) Params {
func NewParams(maxWithdrawalPerBlock uint32, maxSweepPerBlock uint32, minPartialWithdrawalAmount, singularityHeight uint64) Params {
return Params{
MaxWithdrawalPerBlock: maxWithdrawalPerBlock,
MaxSweepPerBlock: maxSweepPerBlock,
MinPartialWithdrawalAmount: minPartialWithdrawalAmount,
SingularityHeight: singularityHeight,
}
}

Expand All @@ -28,6 +31,7 @@ func DefaultParams() Params {
DefaultMaxWithdrawalPerBlock,
DefaultMaxSweepPerBlock,
DefaultMinPartialWithdrawalAmount,
DefaultSingularityHeight,
)
}

Expand Down Expand Up @@ -58,3 +62,11 @@ func ValidateMinPartialWithdrawalAmount(v uint64) error {

return nil
}

func ValidateSingularityHeight(v uint64) error {
if v == 0 {
return fmt.Errorf("singularity height must be positive: %d", v)
}

return nil
}
77 changes: 58 additions & 19 deletions client/x/evmstaking/types/params.pb.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/evmstaking/types/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ message Params {
uint64 min_partial_withdrawal_amount = 3 [
(gogoproto.moretags) = "yaml:\"min_partial_withdrawal_amount\""
];
// the block height until singularity is activated
uint64 singularity_height = 4 [
(gogoproto.moretags) = "yaml:\"singularity_height\""
];
}
5 changes: 3 additions & 2 deletions client/x/evmstaking/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (suite *ParamsTestSuite) SetupTest() {

func (suite *ParamsTestSuite) TestNewParams() {
require := suite.Require()
maxWithdrawalPerBlock, maxSweepPerBlock, minPartialWithdrawalAmount := uint32(1), uint32(2), uint64(3)
params := types.NewParams(maxWithdrawalPerBlock, maxSweepPerBlock, minPartialWithdrawalAmount)
maxWithdrawalPerBlock, maxSweepPerBlock, minPartialWithdrawalAmount, singularityHeight := uint32(1), uint32(2), uint64(3), uint64(10)
params := types.NewParams(maxWithdrawalPerBlock, maxSweepPerBlock, minPartialWithdrawalAmount, singularityHeight)
// check values are set correctly
require.Equal(maxWithdrawalPerBlock, params.MaxWithdrawalPerBlock)
require.Equal(maxSweepPerBlock, params.MaxSweepPerBlock)
Expand All @@ -35,6 +35,7 @@ func (suite *ParamsTestSuite) TestDefaultParams() {
require.Equal(types.DefaultMaxWithdrawalPerBlock, params.MaxWithdrawalPerBlock)
require.Equal(types.DefaultMaxSweepPerBlock, params.MaxSweepPerBlock)
require.Equal(types.DefaultMinPartialWithdrawalAmount, params.MinPartialWithdrawalAmount)
require.Equal(types.DefaultSingularityHeight, params.SingularityHeight)
}

func (suite *ParamsTestSuite) TestValidateMaxWithdrawalPerBlock() {
Expand Down
8 changes: 7 additions & 1 deletion client/x/mint/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/piplabs/story/client/x/mint/types"
"github.com/piplabs/story/lib/log"
)

// BeginBlocker mints new tokens for the previous block.
Expand All @@ -20,6 +21,12 @@ func (k Keeper) BeginBlocker(ctx context.Context, ic types.InflationCalculationF
return err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
if sdkCtx.BlockHeight() <= int64(params.SingularityHeight) {
log.Debug(ctx, "Skip minting during singularity")
return nil
}

// mint coins, update supply
mintedCoinAmt := ic(ctx, params, math.LegacyNewDec(0)) // NOTE: bondedRatio is not used in current implementation.
mintedCoin := sdk.NewCoin(params.MintDenom, mintedCoinAmt.TruncateInt())
Expand All @@ -37,7 +44,6 @@ func (k Keeper) BeginBlocker(ctx context.Context, ic types.InflationCalculationF
defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
Expand Down
1 change: 1 addition & 0 deletions client/x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (s *GenesisTestSuite) TestImportExportGenesis() {
"testDenom",
math.LegacyNewDec(24625000000000000.000000000000000000),
uint64(60*60*8766/5),
10,
)

s.keeper.InitGenesis(s.sdkCtx, s.accountKeeper, genesisState)
Expand Down
Loading
Loading