Skip to content

Commit

Permalink
add test & min processable rent
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 4, 2023
1 parent 9c64198 commit 058072e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 41 deletions.
4 changes: 4 additions & 0 deletions proto/dex/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ message Params {
(gogoproto.jsontag) = "gas_allowance_per_settlement",
(gogoproto.moretags) = "yaml:\"gas_allowance_per_settlement\""
];
uint64 min_processable_rent = 9 [
(gogoproto.jsontag) = "min_processable_rent",
(gogoproto.moretags) = "yaml:\"min_processable_rent\""
];
}
4 changes: 2 additions & 2 deletions x/dex/contract/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo

// restore keeper in-memory state
newGoContext := context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, memStateCopy)
return filterNewValidContracts(ctx, env), getOutOfRentContracts(ctx, env), ctx.WithContext(newGoContext), false
return filterNewValidContracts(ctx, env), getOutOfRentContracts(env), ctx.WithContext(newGoContext), false
}

func newEnv(ctx sdk.Context, validContractsInfo []types.ContractInfoV2, keeper *keeper.Keeper) *environment {
Expand Down Expand Up @@ -271,7 +271,7 @@ func filterNewValidContracts(ctx sdk.Context, env *environment) []types.Contract
return newValidContracts
}

func getOutOfRentContracts(ctx sdk.Context, env *environment) []types.ContractInfoV2 {
func getOutOfRentContracts(env *environment) []types.ContractInfoV2 {
outOfRentContracts := []types.ContractInfoV2{}
for _, contract := range env.validContractsInfo {
if env.outOfRentContractAddresses.Contains(contract.ContractAddr) {
Expand Down
4 changes: 4 additions & 0 deletions x/dex/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (k Keeper) GetSettlementGasAllowance(ctx sdk.Context, numSettlements int) u
return k.GetParams(ctx).GasAllowancePerSettlement * uint64(numSettlements)
}

func (k Keeper) GetMinProcessableRent(ctx sdk.Context) uint64 {
return k.GetParams(ctx).MinProcessableRent
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.Paramstore.SetParamSet(ctx, &params)
Expand Down
2 changes: 1 addition & 1 deletion x/dex/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (am AppModule) getAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 {
// Do not process any contract that has zero rent balance
defer telemetry.MeasureSince(time.Now(), am.Name(), "get_all_contract_info")
allRegisteredContracts := am.keeper.GetAllContractInfo(ctx)
validContracts := utils.Filter(allRegisteredContracts, func(c types.ContractInfoV2) bool { return c.RentBalance > 0 })
validContracts := utils.Filter(allRegisteredContracts, func(c types.ContractInfoV2) bool { return c.RentBalance > am.keeper.GetMinProcessableRent(ctx) })
telemetry.SetGauge(float32(len(allRegisteredContracts)), am.Name(), "num_of_registered_contracts")
telemetry.SetGauge(float32(len(validContracts)), am.Name(), "num_of_valid_contracts")
telemetry.SetGauge(float32(len(allRegisteredContracts)-len(validContracts)), am.Name(), "num_of_zero_balance_contracts")
Expand Down
11 changes: 8 additions & 3 deletions x/dex/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ func TestEndBlockRollbackWithRentCharge(t *testing.T) {
Amount: sdk.MustNewDecFromStr("10000"),
},
)
// overwrite params for testing
params := dexkeeper.GetParams(ctx)
params.MinProcessableRent = 0
dexkeeper.SetParams(ctx, params)

ctx = ctx.WithBlockHeight(1)
creatorBalanceBefore := bankkeeper.GetBalance(ctx, testAccount, "usei")
Expand All @@ -625,8 +629,9 @@ func TestEndBlockRollbackWithRentCharge(t *testing.T) {
require.Equal(t, 0, len(matchResult.Orders))
// rent should still be charged even if the contract failed, so no rent should be sent to the creator after
// auto unregister
_, err = dexkeeper.GetContract(ctx, contractAddr.String())
require.NotNil(t, err) // auto-unregistered
c, err := dexkeeper.GetContract(ctx, contractAddr.String())
require.Nil(t, err) // out-of-rent contract should not be auto-unregistered
require.Equal(t, uint64(0), c.RentBalance) // rent balance should be drained
creatorBalanceAfter := bankkeeper.GetBalance(ctx, testAccount, "usei")
require.Equal(t, creatorBalanceBefore, creatorBalanceAfter)
}
Expand Down Expand Up @@ -671,6 +676,6 @@ func TestEndBlockContractWithoutPair(t *testing.T) {
ti := tracing.Info{
Tracer: &tr,
}
_, _, success := contract.EndBlockerAtomic(ctx, &testApp.DexKeeper, []types.ContractInfoV2{contractInfo}, &ti)
_, _, _, success := contract.EndBlockerAtomic(ctx, &testApp.DexKeeper, []types.ContractInfoV2{contractInfo}, &ti)
require.True(t, success)
}
4 changes: 4 additions & 0 deletions x/dex/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
KeyDefaultGasPerCancel = []byte("KeyDefaultGasPerCancel")
KeyMinRentDeposit = []byte("KeyMinRentDeposit")
KeyGasAllowancePerSettlement = []byte("KeyGasAllowancePerSettlement")
KeyMinProcessableRent = []byte("KeyMinProcessableRent")
)

const (
Expand All @@ -27,6 +28,7 @@ const (
DefaultDefaultGasPerCancel = 55000
DefaultMinRentDeposit = 10000000 // 10 sei
DefaultGasAllowancePerSettlement = 10000
DefaultMinProcessableRent = 100000
)

var DefaultSudoCallGasPrice = sdk.NewDecWithPrec(1, 1) // 0.1
Expand All @@ -49,6 +51,7 @@ func DefaultParams() Params {
DefaultGasPerCancel: DefaultDefaultGasPerCancel,
MinRentDeposit: DefaultMinRentDeposit,
GasAllowancePerSettlement: DefaultGasAllowancePerSettlement,
MinProcessableRent: DefaultMinProcessableRent,
}
}

Expand All @@ -63,6 +66,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyDefaultGasPerCancel, &p.DefaultGasPerCancel, validateUint64Param),
paramtypes.NewParamSetPair(KeyMinRentDeposit, &p.MinRentDeposit, validateUint64Param),
paramtypes.NewParamSetPair(KeyGasAllowancePerSettlement, &p.GasAllowancePerSettlement, validateUint64Param),
paramtypes.NewParamSetPair(KeyMinProcessableRent, &p.MinProcessableRent, validateUint64Param),
}
}

Expand Down
111 changes: 76 additions & 35 deletions x/dex/types/params.pb.go

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

0 comments on commit 058072e

Please sign in to comment.