/
block_rate_limit_config.go
68 lines (55 loc) · 2.33 KB
/
block_rate_limit_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furyanprotocol/v4-chain/protocol/x/clob/rate_limit"
"github.com/furyanprotocol/v4-chain/protocol/x/clob/types"
)
// GetBlockRateLimitConfiguration gets the block rate limit configuration from state.
func (k Keeper) GetBlockRateLimitConfiguration(
ctx sdk.Context,
) (config types.BlockRateLimitConfiguration) {
store := ctx.KVStore(k.storeKey)
b := store.Get([]byte(types.BlockRateLimitConfigKey))
// The block rate limit configuration should be set in state by the genesis logic.
// If it's not found, then that indicates it was never set in state, which is invalid.
if b == nil {
panic("GetBlockRateLimitConfiguration: BlockRateLimitConfig was never set in state")
}
k.cdc.MustUnmarshal(b, &config)
return config
}
// InitalizeBlockRateLimitFromStateIfExists initializes the `placeOrderRateLimiter` and `cancelOrderRateLimiter`
// from state. Should be invoked during application start and before CLOB genesis.
func (k *Keeper) InitalizeBlockRateLimitFromStateIfExists(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
b := store.Get([]byte(types.BlockRateLimitConfigKey))
if b == nil {
return
}
var config types.BlockRateLimitConfiguration
k.cdc.MustUnmarshal(b, &config)
k.placeOrderRateLimiter = rate_limit.NewPlaceOrderRateLimiter(config)
k.cancelOrderRateLimiter = rate_limit.NewCancelOrderRateLimiter(config)
}
// InitializeBlockRateLimit initializes the block rate limit configuration in state and uses
// the configuration to initialize the `placeOrderRateLimiter` and `cancelOrderRateLimiter`.
// This function should only be called from CLOB genesis or when a block rate limit configuration
// change is accepted via governance.
//
// Note that any previously tracked rates will be reset.
func (k *Keeper) InitializeBlockRateLimit(
ctx sdk.Context,
config types.BlockRateLimitConfiguration,
) error {
// Validate the block rate limit config before writing it to state.
if err := config.Validate(); err != nil {
return err
}
// Write the rate limit configuration to state.
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&config)
store.Set([]byte(types.BlockRateLimitConfigKey), b)
k.placeOrderRateLimiter = rate_limit.NewPlaceOrderRateLimiter(config)
k.cancelOrderRateLimiter = rate_limit.NewCancelOrderRateLimiter(config)
return nil
}