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: limit sidechain consensus addr update interval #318

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 15 additions & 14 deletions types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
var UpgradeMgr = NewUpgradeManager(UpgradeConfig{})

const (
FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes
BEP9 = "BEP9" // https://github.com/bnb-chain/BEPs/pull/9
BEP12 = "BEP12" // https://github.com/bnb-chain/BEPs/pull/17
BEP3 = "BEP3" // https://github.com/bnb-chain/BEPs/pull/30
BEP8 = "BEP8" // https://github.com/bnb-chain/BEPs/pull/69
LaunchBscUpgrade = "LaunchBscUpgrade"
BEP82 = "BEP82" // https://github.com/bnb-chain/BEPs/pull/82
FixFailAckPackage = "FixFailAckPackage"
BEP128 = "BEP128" // https://github.com/bnb-chain/BEPs/pull/128
BEP153 = "BEP153" // https://github.com/bnb-chain/BEPs/pull/153
BEP159 = "BEP159" // https://github.com/bnb-chain/BEPs/pull/159
BEP159Phase2 = "BEP159Phase2" // phase 2 activation height of BEP159, enable create validator and active oracle relayer whitelist
BEP173 = "BEP173" // https://github.com/bnb-chain/BEPs/pull/173
FixDoubleSignChainId = "FixDoubleSignChainId"
FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes
BEP9 = "BEP9" // https://github.com/bnb-chain/BEPs/pull/9
BEP12 = "BEP12" // https://github.com/bnb-chain/BEPs/pull/17
BEP3 = "BEP3" // https://github.com/bnb-chain/BEPs/pull/30
BEP8 = "BEP8" // https://github.com/bnb-chain/BEPs/pull/69
LaunchBscUpgrade = "LaunchBscUpgrade"
BEP82 = "BEP82" // https://github.com/bnb-chain/BEPs/pull/82
FixFailAckPackage = "FixFailAckPackage"
BEP128 = "BEP128" // https://github.com/bnb-chain/BEPs/pull/128
BEP153 = "BEP153" // https://github.com/bnb-chain/BEPs/pull/153
BEP159 = "BEP159" // https://github.com/bnb-chain/BEPs/pull/159
BEP159Phase2 = "BEP159Phase2" // phase 2 activation height of BEP159, enable create validator and active oracle relayer whitelist
LimitConsAddrUpdateInterval = "LimitConsAddrUpdateInterval"
BEP173 = "BEP173" // https://github.com/bnb-chain/BEPs/pull/173
FixDoubleSignChainId = "FixDoubleSignChainId"
)

var MainNetConfig = UpgradeConfig{
Expand Down
11 changes: 11 additions & 0 deletions x/stake/handler_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ func handleMsgEditSideChainValidator(ctx sdk.Context, msg MsgEditSideChainValida
if found {
return ErrValidatorSideConsAddrExist(k.Codespace()).Result()
}
if sdk.IsUpgrade(sdk.LimitConsAddrUpdateInterval) {
// check update sideConsAddr interval
latestUpdateConsAddrTime, err := k.GetValLatestUpdateConsAddrTime(ctx, validator.OperatorAddr)
if err != nil {
return sdk.ErrInternal(fmt.Sprintf("failed to get latest update cons addr time: %s", err)).Result()
}
if ctx.BlockHeader().Time.Sub(latestUpdateConsAddrTime).Hours() < 24*30 {
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
return types.ErrConsAddrUpdateTime().Result()
}
k.SetValLatestUpdateConsAddrTime(ctx, validator.OperatorAddr, ctx.BlockHeader().Time)
}
// here consAddr is the sideConsAddr
k.UpdateSideValidatorConsAddr(ctx, validator, msg.SideConsAddr)
validator.SideConsAddr = msg.SideConsAddr
Expand Down
7 changes: 6 additions & 1 deletion x/stake/keeper/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// TODO remove some of these prefixes once have working multistore

//nolint
// nolint
var (
// Keys for store prefixes
// TODO DEPRECATED: delete in next release and reorder keys
Expand Down Expand Up @@ -38,6 +38,7 @@ var (
RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator
DelegationKeyByVal = []byte{0x37} // prefix for each key for a delegation, by validator operator and delegator
SimplifiedDelegationsKey = []byte{0x38} // prefix for each key for an simplifiedDelegations, by height and validator operator
ValLatestUpdateConsAddrTimeKey = []byte{0x39} // prefix for each key for an latest update ConsAddr time, by validator operator

UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue
RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue
Expand Down Expand Up @@ -330,3 +331,7 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr
GetREDsToValDstIndexKey(valDstAddr),
delAddr.Bytes()...)
}

func GetValLatestUpdateConsAddrTimeKey(valAddr sdk.ValAddress) []byte {
return append(ValLatestUpdateConsAddrTimeKey, valAddr.Bytes()...)
}
16 changes: 16 additions & 0 deletions x/stake/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,19 @@ func (k Keeper) UnbondAllMatureValidatorQueue(ctx sdk.Context) {
store.Delete(validatorTimesliceIterator.Key())
}
}

func (k Keeper) GetValLatestUpdateConsAddrTime(ctx sdk.Context, addr sdk.ValAddress) (t time.Time, err error) {
store := ctx.KVStore(k.storeKey)
value := store.Get(GetValLatestUpdateConsAddrTimeKey(addr))
if value == nil {
return
}
t, err = sdk.ParseTimeBytes(value)
return
}

func (k Keeper) SetValLatestUpdateConsAddrTime(ctx sdk.Context, addr sdk.ValAddress, t time.Time) {
store := ctx.KVStore(k.storeKey)
bz := sdk.FormatTimeBytes(t)
store.Set(GetValLatestUpdateConsAddrTimeKey(addr), bz)
}
7 changes: 6 additions & 1 deletion x/stake/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ const (
CodeExpiredCrossStakeSyncPackage CodeType = 109
CodeCrossStakingNoBalance CodeType = 110
CodeCrossStakingNotEnoughBalance CodeType = 111
CodeInvalidConsAddrUpdateTime CodeType = 112
CodeInvalidAddress CodeType = sdk.CodeInvalidAddress
CodeUnauthorized CodeType = sdk.CodeUnauthorized
CodeInternal CodeType = sdk.CodeInternal
CodeUnknownRequest CodeType = sdk.CodeUnknownRequest
)

//validator
// validator
func ErrNilValidatorAddr(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeInvalidInput, "validator address is nil")
}
Expand Down Expand Up @@ -259,3 +260,7 @@ func ErrNotEnoughBalance(msg string) sdk.Error {
func ErrNotSelfDelegate(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeInvalidValidator, "only self delegate is allowed")
}

func ErrConsAddrUpdateTime() sdk.Error {
return sdk.NewError(DefaultCodespace, CodeInvalidConsAddrUpdateTime, "ConsAddr cannot be changed more than once in one month")
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
}