Skip to content

Commit

Permalink
feat: modifications for terra classic
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuyenthevinh2000 committed Jan 25, 2024
1 parent e62c19c commit d8cbb4c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
8 changes: 7 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/spf13/viper"

clientflags "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/store/cache"
"github.com/cosmos/cosmos-sdk/store/iavl"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -80,6 +82,9 @@ type BaseConfig struct {
// InterBlockCache enables inter-block caching.
InterBlockCache bool `mapstructure:"inter-block-cache"`

// InterBlockCacheSize set the size of the inter-block cache.
InterBlockCacheSize uint `mapstructure:"inter-block-cache-size"`

// IndexEvents defines the set of events in the form {eventType}.{attributeKey},
// which informs Tendermint what to index. If empty, all events will be indexed.
IndexEvents []string `mapstructure:"index-events"`
Expand Down Expand Up @@ -283,12 +288,13 @@ func DefaultConfig() *Config {
BaseConfig: BaseConfig{
MinGasPrices: defaultMinGasPrices,
InterBlockCache: true,
InterBlockCacheSize: cache.DefaultCommitKVStoreCacheSize,
Pruning: pruningtypes.PruningOptionDefault,
PruningKeepRecent: "0",
PruningInterval: "0",
MinRetainBlocks: 0,
IndexEvents: make([]string, 0),
IAVLCacheSize: 781250,
IAVLCacheSize: iavl.DefaultIAVLCacheSize,
IAVLDisableFastNode: false,
IAVLLazyLoading: false,
AppDBBackend: "",
Expand Down
11 changes: 10 additions & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,23 @@ min-retain-blocks = {{ .BaseConfig.MinRetainBlocks }}
# InterBlockCache enables inter-block caching.
inter-block-cache = {{ .BaseConfig.InterBlockCache }}
# InterBlockCacheSize set the size (the number of cache items) of interblock cache item
# Each item consumes 128 bytes, so the value should be dividend by 128
# Default cache size is 10mb.
# Ex) 100mb = 10,000,000 / 128 = 78,125
inter-block-cache-size = {{ .BaseConfig.InterBlockCacheSize }}
# IndexEvents defines the set of events in the form {eventType}.{attributeKey},
# which informs Tendermint what to index. If empty, all events will be indexed.
#
# Example:
# ["message.sender", "message.recipient"]
index-events = [{{ range .BaseConfig.IndexEvents }}{{ printf "%q, " . }}{{end}}]
# IavlCacheSize set the size of the iavl tree cache (in number of nodes).
# IAVLCacheSize set the cache size (the number of cache items) of the iavl tree.
# Each item size consumes 128 bytes, so the value should be dividend by 128
# Default cache size is 100mb.
# Ex) 100mb = 100,000,000 / 128 = 781,250
iavl-cache-size = {{ .BaseConfig.IAVLCacheSize }}
# IAVLDisableFastNode enables or disables the fast node feature of IAVL.
Expand Down
5 changes: 5 additions & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/cache"
"github.com/cosmos/cosmos-sdk/store/iavl"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -60,6 +62,7 @@ const (
FlagIndexEvents = "index-events"
FlagMinRetainBlocks = "min-retain-blocks"
FlagIAVLCacheSize = "iavl-cache-size"
FlagInterBlockCacheSize = "inter-block-cache-size"
FlagDisableIAVLFastNode = "iavl-disable-fastnode"
FlagIAVLLazyLoading = "iavl-lazy-loading"

Expand Down Expand Up @@ -172,6 +175,8 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
cmd.Flags().Uint64(FlagHaltHeight, 0, "Block height at which to gracefully halt the chain and shutdown the node")
cmd.Flags().Uint64(FlagHaltTime, 0, "Minimum block time (in Unix seconds) at which to gracefully halt the chain and shutdown the node")
cmd.Flags().Bool(FlagInterBlockCache, true, "Enable inter-block caching")
cmd.Flags().Uint(FlagInterBlockCacheSize, cache.DefaultCommitKVStoreCacheSize, "The size of inter-block caching store")
cmd.Flags().Uint64(FlagIAVLCacheSize, iavl.DefaultIAVLCacheSize, "The size of iavl caching store")
cmd.Flags().String(flagCPUProfile, "", "Enable CPU profiling and write to the provided file")
cmd.Flags().Bool(FlagTrace, false, "Provide full stack traces for errors in ABCI Log")
cmd.Flags().String(FlagPruning, pruningtypes.PruningOptionDefault, "Pruning strategy (default|nothing|everything|custom)")
Expand Down
13 changes: 10 additions & 3 deletions store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ var (
_ types.CommitKVStore = (*CommitKVStoreCache)(nil)
_ types.MultiStorePersistentCache = (*CommitKVStoreCacheManager)(nil)

// DefaultCommitKVStoreCacheSize defines the persistent ARC cache size for a
// CommitKVStoreCache.
DefaultCommitKVStoreCacheSize uint = 1000
// DefaultCommitKVStoreCacheSize defines the number of persistent ARC cache item for a
// CommitKVStoreCache, which is supposed to be 10MB size.
// Each cache item consumes 128 bytes, 64 bytes for the left sibling, and 64 bytes for the right sibling.
// The number of cache item is calculated as 10 MB = 10,000,000 / 128 = 78_125
DefaultCommitKVStoreCacheSize uint = 78_125
)

type (
Expand Down Expand Up @@ -59,6 +61,11 @@ func NewCommitKVStoreCacheManager(size uint) *CommitKVStoreCacheManager {
}
}

// SetCacheSize sets the cache size of the CommitKVStore.
func (cmgr *CommitKVStoreCacheManager) SetCacheSize(size uint) {
cmgr.cacheSize = size
}

// GetStoreCache returns a Cache from the CommitStoreCacheManager for a given
// StoreKey. If no Cache exists for the StoreKey, then one is created and set.
// The returned Cache is meant to be used in a persistent manner.
Expand Down
5 changes: 4 additions & 1 deletion store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
)

const (
DefaultIAVLCacheSize = 500000
// DefaultIAVLCacheSize defines the number of iavl cache item, which is supposed to be 100MB size.
// Each cache item consumes 128 bytes, 64 bytes for the left sibling, and 64 bytes for the right sibling.
// The number of cache item is calculated as 100 MB = 10,000,000 / 128 = 781_250
DefaultIAVLCacheSize = 781_250
)

var (
Expand Down
3 changes: 3 additions & 0 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ type MultiStorePersistentCache interface {
// cache.
GetStoreCache(key StoreKey, store CommitKVStore) CommitKVStore

// Sets the cache size of the provided CommitKVStore
SetCacheSize(size uint)

// Return the underlying CommitKVStore for a StoreKey.
Unwrap(key StoreKey) CommitKVStore

Expand Down
26 changes: 26 additions & 0 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

const ColumbusChainID = "columbus-5"

// GetDelegation returns a specific delegation.
func (k Keeper) GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (delegation types.Delegation, found bool) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -657,6 +659,30 @@ func (k Keeper) Delegate(

delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

// If Delegations are allowed again, limit validator power to 20%
if ctx.ChainID() == ColumbusChainID {
// Get the last Total Power of the validator set
lastPower := k.GetLastTotalPower(ctx)

// Get the power of the current validator power
validatorLastPower := sdk.TokensToConsensusPower(validator.Tokens, k.PowerReduction(ctx))

// Get the new power of the validator if delegated the bond amount
validatorNewPower := validatorLastPower + sdk.TokensToConsensusPower(bondAmt, k.PowerReduction(ctx))

// Compute what the Total Consensus Power would be if this Delegation goes through
newTotalPower := lastPower.Int64() + sdk.TokensToConsensusPower(bondAmt, k.PowerReduction(ctx))

// Compute what the new Validator voting power would be in relation to the new total power
// validatorIncreasedDelegationPercent := float32(validatorNewPower) / float32(newTotalPower)
validatorIncreasedDelegationPercent := sdk.NewDec(validatorNewPower).QuoInt64(newTotalPower)

// If Delegations are allowed, and the Delegation would have increased the Validator to over 20% of the staking power, do not allow the Delegation to proceed
if validatorIncreasedDelegationPercent.GT(sdk.NewDecWithPrec(20, 2)) {
panic("validator power is over the allowed limit")
}
}

// if subtractAccount is true then we are
// performing a delegation and not a redelegation, thus the source tokens are
// all non bonded
Expand Down

0 comments on commit d8cbb4c

Please sign in to comment.