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

refactor(x/staking)!: rename NewToOldConsKeyMap to ConsKeyToValidatorIdentifierMap #20696

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions x/staking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ ValidatorConsensusKeyRotationRecordIndexKey:`104 | valAddr | format(time) -> Pro

OldToNewConsKeyMap:`105 | byte(oldConsKey) -> byte(newConsKey)`

NewToOldConsKeyMap:`106 | byte(newConsKey) -> byte(oldConsKey)`
ConsKeyToValidatorIdentifierMap:`106 | byte(newConsKey) -> byte(initialConsKey)`

`ConsPubKeyRotationHistory` is used for querying the rotations of a validator

Expand All @@ -292,7 +292,7 @@ A `ConsPubKeyRotationHistory` object is created every time a consensus pubkey ro

An entry is added in `OldToNewConsKeyMap` collection for every rotation (Note: this is to handle the evidences when submitted with old cons key).

An entry is added in `NewToOldConsKeyMap` collection for every rotation, this entry is to block the rotation if the validator is rotating to the cons key which is involved in the history.
An entry is added in `ConsKeyToValidatorIdentifierMap` collection for every rotation, this entry is to block the rotation if the validator is rotating to the cons key which is involved in the history.

To prevent the spam:

Expand Down
14 changes: 7 additions & 7 deletions x/staking/keeper/cons_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ func (k Keeper) updateToNewPubkey(ctx context.Context, val types.Validator, oldP
return err
}

// sets a map: newConsKey -> oldConsKey
if err := k.setNewToOldConsKeyMap(ctx, sdk.ConsAddress(oldPk.Address()), sdk.ConsAddress(newPk.Address())); err != nil {
// sets a map: newConsKey -> initialConsKey
if err := k.setConsKeyToValidatorIdentifierMap(ctx, sdk.ConsAddress(oldPk.Address()), sdk.ConsAddress(newPk.Address())); err != nil {
return err
}

return k.Hooks().AfterConsensusPubKeyUpdate(ctx, oldPk, newPk, fee)
}

// setNewToOldConsKeyMap adds an entry in the state with the current consKey to the initial consKey of the validator.
// setConsKeyToValidatorIdentifierMap adds an entry in the state with the current consKey to the initial consKey of the validator.
// it tries to find the oldPk if there is a entry already present in the state
func (k Keeper) setNewToOldConsKeyMap(ctx context.Context, oldPk, newPk sdk.ConsAddress) error {
pk, err := k.NewToOldConsKeyMap.Get(ctx, oldPk)
func (k Keeper) setConsKeyToValidatorIdentifierMap(ctx context.Context, oldPk, newPk sdk.ConsAddress) error {
pk, err := k.ConsKeyToValidatorIdentifierMap.Get(ctx, oldPk)
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return err
}
Expand All @@ -129,13 +129,13 @@ func (k Keeper) setNewToOldConsKeyMap(ctx context.Context, oldPk, newPk sdk.Cons
oldPk = pk
}

return k.NewToOldConsKeyMap.Set(ctx, newPk, oldPk)
return k.ConsKeyToValidatorIdentifierMap.Set(ctx, newPk, oldPk)
}

// ValidatorIdentifier maps the new cons key to previous cons key (which is the address before the rotation).
// (that is: newConsKey -> oldConsKey)
func (k Keeper) ValidatorIdentifier(ctx context.Context, newPk sdk.ConsAddress) (sdk.ConsAddress, error) {
pk, err := k.NewToOldConsKeyMap.Get(ctx, newPk)
pk, err := k.ConsKeyToValidatorIdentifierMap.Get(ctx, newPk)
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ type Keeper struct {
ValidatorConsensusKeyRotationRecordIndexKey collections.KeySet[collections.Pair[[]byte, time.Time]]
// ValidatorConsensusKeyRotationRecordQueue: this key is used to set the unbonding period time on each rotation
ValidatorConsensusKeyRotationRecordQueue collections.Map[time.Time, types.ValAddrsOfRotatedConsKeys]
// NewToOldConsKeyMap: prefix for rotated old cons address to new cons address
NewToOldConsKeyMap collections.Map[[]byte, []byte]
// OldToNewConsKeyMap: prefix for rotated new cons address to old cons address
// ConsKeyToValidatorIdentifierMap: maps the new cons key to the initial cons key
ConsKeyToValidatorIdentifierMap collections.Map[[]byte, []byte]
// OldToNewConsKeyMap: maps the old cons key to the new cons key
OldToNewConsKeyMap collections.Map[[]byte, []byte]
// ValidatorConsPubKeyRotationHistory: consPubkey rotation history by validator
// A index is being added with key `BlockConsPubKeyRotationHistory`: consPubkey rotation history by height
Expand Down Expand Up @@ -280,8 +280,8 @@ func NewKeeper(
),

// key format is: 105 | consAddr
NewToOldConsKeyMap: collections.NewMap(
sb, types.NewToOldConsKeyMap,
ConsKeyToValidatorIdentifierMap: collections.NewMap(
sb, types.ConsKeyToValidatorIdentifierMapPrefix,
"new_to_old_cons_key_map",
collections.BytesKey,
collections.BytesValue,
Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func (k msgServer) RotateConsPubKey(ctx context.Context, msg *types.MsgRotateCon
}

// check cons key is already present in the key rotation history.
rotatedTo, err := k.NewToOldConsKeyMap.Get(ctx, pk.Address())
rotatedTo, err := k.ConsKeyToValidatorIdentifierMap.Get(ctx, pk.Address())
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
BlockConsPubKeyRotationHistoryKey = collections.NewPrefix(102) // prefix for consPubkey rotation history by height
ValidatorConsensusKeyRotationRecordQueueKey = collections.NewPrefix(103) // this key is used to set the unbonding period time on each rotation
ValidatorConsensusKeyRotationRecordIndexKey = collections.NewPrefix(104) // this key is used to restrict the validator next rotation within waiting (unbonding) period
NewToOldConsKeyMap = collections.NewPrefix(105) // prefix for rotated cons address to new cons address
ConsKeyToValidatorIdentifierMapPrefix = collections.NewPrefix(105) // prefix for rotated cons address to new cons address
OldToNewConsKeyMap = collections.NewPrefix(106) // prefix for rotated cons address to new cons address
)

Expand Down
Loading