Skip to content

Commit

Permalink
add key rotation history
Browse files Browse the repository at this point in the history
  • Loading branch information
GNaD13 committed Nov 12, 2023
1 parent 3bb2b1c commit c05c2d3
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 24 deletions.
16 changes: 16 additions & 0 deletions proto/centauri/keyrotation/v1beta1/rotation.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";
package centauri.keyrotation.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/any.proto";

option go_package = "x/keyrotation/types";

// GenesisState defines the mint module's genesis state.
message ConsPubKeyRotationHistory {
string operator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any old_key = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
google.protobuf.Any new_key = 3 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
uint64 block_height = 4;
}
2 changes: 1 addition & 1 deletion proto/centauri/keyrotation/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ service Msg {
}

message MsgRotateConsPubKey {
string validator_address = 1;
string validator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any pubkey = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
}

Expand Down
12 changes: 12 additions & 0 deletions x/keyrotation/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (k Keeper) handleMsgRotateConsPubKey(ctx sdk.Context, valAddress sdk.ValAdd
return err
}

oldpkAny := validator.ConsensusPubkey

// replace pubkey
validator.ConsensusPubkey = pkAny

Expand All @@ -88,5 +90,15 @@ func (k Keeper) handleMsgRotateConsPubKey(ctx sdk.Context, valAddress sdk.ValAdd
k.sk.SetValidator(ctx, validator)
k.sk.SetValidatorByConsAddr(ctx, validator)

// Set rotation history
consPubKeyRotationHistory := types.ConsPubKeyRotationHistory{
OperatorAddress: valAddress.String(),
OldKey: oldpkAny,
NewKey: pkAny,
BlockHeight: uint64(ctx.BlockHeight()),
}

k.SetKeyRotationHistory(ctx, consPubKeyRotationHistory)

return nil
}
26 changes: 26 additions & 0 deletions x/keyrotation/keeper/pubkey_rotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/keyrotation/types"
)

func (k Keeper) SetKeyRotationHistory(ctx sdk.Context, consRotationHistory types.ConsPubKeyRotationHistory) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&consRotationHistory)
store.Set(types.GetKeyRotationHistory(consRotationHistory.OperatorAddress), bz)
}

func (k Keeper) IterateRotationHistory(ctx sdk.Context, cb func(types.ConsPubKeyRotationHistory) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyRotation)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var rotationHistory types.ConsPubKeyRotationHistory
k.cdc.MustUnmarshal(iterator.Value(), &rotationHistory)
if cb(rotationHistory) {
break
}
}
}
12 changes: 12 additions & 0 deletions x/keyrotation/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ const (

RouterKey = ModuleName
)

func KeyPrefix(p string) []byte {
return []byte(p)
}

var (
KeyRotation = KeyPrefix("rotation")
)

func GetKeyRotationHistory(valAddress string) []byte {
return append(KeyRotation, []byte(valAddress)...)
}
Loading

0 comments on commit c05c2d3

Please sign in to comment.