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

Implement cap on uncompleted corks per validator to prevent storage DoS #218

Merged
merged 26 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b6886f7
Implement cap on uncompleted corks per validator to prevent storage DoS
cbrit Jun 16, 2023
5edeaa7
Merge branch 'main' into collin/validator-cork-cap
cbrit Nov 28, 2023
7402159
Fix proto binding merge error
cbrit Nov 28, 2023
7b62aa4
Merge branch 'main' into collin/validator-cork-cap
cbrit Dec 11, 2023
f9fe292
Merge branch 'main' into collin/validator-cork-cap
cbrit Dec 14, 2023
6286be2
Run gofmt
cbrit Dec 14, 2023
5f1aa26
manually call Validate on the genesis states
EricBolten Dec 14, 2023
3fa72fd
point to gravity v4.0.0 release
EricBolten Dec 14, 2023
56e8218
fix casing for some cellars, add Turbo EETH
EricBolten Dec 14, 2023
141c3cb
update slices sizes with new cellar added
EricBolten Dec 14, 2023
5af480a
log the ICA host param update
EricBolten Dec 14, 2023
ae07718
actually validate the minimum USD value parameter
EricBolten Dec 14, 2023
52533a2
normalize cellars with eth mainnet chain ID prefix
EricBolten Dec 14, 2023
844f79c
add arbitrum test cellar to pubsub
EricBolten Dec 14, 2023
6f6497e
Add test arbitrum cellar to axelarcork
EricBolten Dec 14, 2023
be6aebe
some comments
EricBolten Dec 14, 2023
67380d1
loosen ProofURL validation
EricBolten Dec 14, 2023
76dd34f
fix auction params test
EricBolten Dec 14, 2023
a867b7f
set LastUpdatedBlock for token prices in v7 upgrade
EricBolten Dec 14, 2023
fc3b675
ledger support for cork
EricBolten Dec 14, 2023
1101e63
Merge branch 'main' into bolten/v7-upgrade-tweaks
EricBolten Dec 14, 2023
e214bdf
GetSignBytes for cork
EricBolten Dec 14, 2023
d4e2f8d
Merge branch 'main' into bolten/v7-upgrade-tweaks
EricBolten Dec 14, 2023
0dcd331
appease linter
EricBolten Dec 14, 2023
fce7f41
Merge remote-tracking branch 'origin/bolten/v7-upgrade-tweaks' into c…
cbrit Dec 14, 2023
7414927
Merge branch 'main' into collin/validator-cork-cap
EricBolten Dec 14, 2023
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
1 change: 1 addition & 0 deletions integration_tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func (s *IntegrationTestSuite) initGenesis() {
// we add the first validator address as a cellar so that it will trigger the cellarfees hook
// when we send test fees
corkGenState.CellarIds = corktypes.CellarIDSet{Ids: []string{unusedGenesisContract.String(), s.chain.validators[0].ethereumKey.address}}
corkGenState.Params = corktypes.DefaultParams()
corkGenState.Params.VoteThreshold = corkVoteThreshold
bz, err = cdc.MarshalJSON(&corkGenState)
s.Require().NoError(err)
Expand Down
1 change: 1 addition & 0 deletions proto/cork/v2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
uint64 max_corks_per_validator = 2;
}
2 changes: 1 addition & 1 deletion x/auction/keeper/sdk_module_mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ func (suite *KeeperTestSuite) mockSendCoinsFromAccountToModule(ctx sdk.Context,

func (suite *KeeperTestSuite) mockSendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, receiverAcct sdk.AccAddress, amt sdk.Coins) {
suite.bankKeeper.EXPECT().SendCoinsFromModuleToAccount(ctx, senderModule, receiverAcct, amt).Return(nil)
}
}
34 changes: 34 additions & 0 deletions x/cork/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"bytes"
"encoding/binary"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -297,6 +298,7 @@ func (k Keeper) GetApprovedScheduledCorks(ctx sdk.Context) (approvedCorks []type
}

k.DeleteScheduledCork(ctx, currentBlockHeight, id, val, addr)
k.DecrementValidatorCorkCount(ctx, val)

return false
})
Expand Down Expand Up @@ -358,3 +360,35 @@ func (k Keeper) HasCellarID(ctx sdk.Context, address common.Address) (found bool

return found
}

///////////////////////////
// Validator Cork counts //
///////////////////////////

func (k Keeper) GetValidatorCorkCount(ctx sdk.Context, val sdk.ValAddress) (count uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetValidatorCorkCountKey(val))
if len(bz) == 0 {
return 0
}

return binary.BigEndian.Uint64(bz)
}

func (k Keeper) SetValidatorCorkCount(ctx sdk.Context, val sdk.ValAddress, count uint64) {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
ctx.KVStore(k.storeKey).Set(types.GetValidatorCorkCountKey(val), bz)
}

func (k Keeper) IncrementValidatorCorkCount(ctx sdk.Context, val sdk.ValAddress) {
count := k.GetValidatorCorkCount(ctx, val)
k.SetValidatorCorkCount(ctx, val, count+1)
}

func (k Keeper) DecrementValidatorCorkCount(ctx sdk.Context, val sdk.ValAddress) {
count := k.GetValidatorCorkCount(ctx, val)
if count > 0 {
k.SetValidatorCorkCount(ctx, val, count-1)
}
}
1 change: 1 addition & 0 deletions x/cork/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (k Keeper) ScheduleCork(c context.Context, msg *types.MsgScheduleCorkReques
}

corkID := k.SetScheduledCork(ctx, msg.BlockHeight, validatorAddr, *msg.Cork)
k.IncrementValidatorCorkCount(ctx, validatorAddr)

ctx.EventManager().EmitEvents(
sdk.Events{
Expand Down
4 changes: 2 additions & 2 deletions x/cork/mock/sdk_mocks.go

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

11 changes: 6 additions & 5 deletions x/cork/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

// x/cork module sentinel errors
var (
ErrInvalidEthereumAddress = errorsmod.Register(ModuleName, 2, "invalid ethereum address")
ErrUnmanagedCellarAddress = errorsmod.Register(ModuleName, 3, "cork sent to address that has not passed governance")
ErrEmptyContractCall = errorsmod.Register(ModuleName, 4, "cork has an empty contract call body")
ErrSchedulingInThePast = errorsmod.Register(ModuleName, 5, "cork is trying to be scheduled for a block that has already passed")
ErrInvalidJSON = errorsmod.Register(ModuleName, 6, "invalid json")
ErrInvalidEthereumAddress = errorsmod.Register(ModuleName, 2, "invalid ethereum address")
ErrUnmanagedCellarAddress = errorsmod.Register(ModuleName, 3, "cork sent to address that has not passed governance")
ErrEmptyContractCall = errorsmod.Register(ModuleName, 4, "cork has an empty contract call body")
ErrSchedulingInThePast = errorsmod.Register(ModuleName, 5, "cork is trying to be scheduled for a block that has already passed")
ErrInvalidJSON = errorsmod.Register(ModuleName, 6, "invalid json")
ErrValidatorCorkCapacityReached = errorsmod.Register(ModuleName, 7, "validator cork capacity reached")
)
93 changes: 65 additions & 28 deletions x/cork/types/genesis.pb.go

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

7 changes: 7 additions & 0 deletions x/cork/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (

// CorkResultPrefix - <prefix><id> -> CorkResult
CorkResultPrefix

// ValidatorCorkCountKey - <prefix><val_address> -> uint64(count)
ValidatorCorkCountKey
)

// GetCorkForValidatorAddressKey returns the key for a validators vote for a given address
Expand Down Expand Up @@ -81,3 +84,7 @@ func GetCorkResultPrefix() []byte {
func GetCorkResultKey(id []byte) []byte {
return append(GetCorkResultPrefix(), id...)
}

func GetValidatorCorkCountKey(val sdk.ValAddress) []byte {
return append([]byte{ValidatorCorkCountKey}, val.Bytes()...)
}
25 changes: 22 additions & 3 deletions x/cork/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

// Parameter keys
var (
KeyVotePeriod = []byte("voteperiod")
KeyVoteThreshold = []byte("votethreshold")
KeyVotePeriod = []byte("voteperiod")
KeyVoteThreshold = []byte("votethreshold")
KeyMaxCorksPerValidator = []byte("maxcorkspervalidator")
)

var _ paramtypes.ParamSet = &Params{}
Expand All @@ -25,14 +26,16 @@ func ParamKeyTable() paramtypes.KeyTable {
func DefaultParams() Params {
return Params{
// Deprecated
VoteThreshold: sdk.NewDecWithPrec(67, 2), // 67%
VoteThreshold: sdk.NewDecWithPrec(67, 2), // 67%
MaxCorksPerValidator: 1000,
}
}

// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyVoteThreshold, &p.VoteThreshold, validateVoteThreshold),
paramtypes.NewParamSetPair(KeyMaxCorksPerValidator, &p.MaxCorksPerValidator, validateMaxCorksPerValidator),
}
}

Expand All @@ -41,6 +44,9 @@ func (p *Params) ValidateBasic() error {
if err := validateVoteThreshold(p.VoteThreshold); err != nil {
return err
}
if err := validateMaxCorksPerValidator(p.MaxCorksPerValidator); err != nil {
return err
}
return nil
}

Expand All @@ -60,3 +66,16 @@ func validateVoteThreshold(i interface{}) error {

return nil
}

func validateMaxCorksPerValidator(i interface{}) error {
maxCorksPerValidator, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if maxCorksPerValidator == 0 {
return errors.New("max corks per validator cannot be 0")
}

return nil
}
19 changes: 9 additions & 10 deletions x/cork/types/proposal.pb.go

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

Loading