Skip to content

Commit

Permalink
staking: customization for bfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Keefe Liu committed Nov 23, 2022
1 parent eb1e3eb commit c982c26
Show file tree
Hide file tree
Showing 4 changed files with 839 additions and 234 deletions.
49 changes: 41 additions & 8 deletions proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@ service Msg {
// EditValidator defines a method for editing an existing validator.
rpc EditValidator(MsgEditValidator) returns (MsgEditValidatorResponse);

// RemoveValidator defines a method for removing an existing validator.
rpc RemoveValidator(MsgRemoveValidator) returns (MsgRemoveValidatorResponse);

// SelfDelegate defines a method for performing a delegation of coins
// for a validator itself.
rpc SelfDelegate(MsgSelfDelegate) returns (MsgDelegateResponse);

// Delegate defines a method for performing a delegation of coins
// from a delegator to a validator.
rpc Delegate(MsgDelegate) returns (MsgDelegateResponse);
// rpc Delegate(MsgDelegate) returns (MsgDelegateResponse);

// BeginRedelegate defines a method for performing a redelegation
// of coins from a delegator and source validator to a destination validator.
rpc BeginRedelegate(MsgBeginRedelegate) returns (MsgBeginRedelegateResponse);
// rpc BeginRedelegate(MsgBeginRedelegate) returns (MsgBeginRedelegateResponse);

// Undelegate defines a method for performing an undelegation from a
// delegate and a validator.
rpc Undelegate(MsgUndelegate) returns (MsgUndelegateResponse);
// rpc Undelegate(MsgUndelegate) returns (MsgUndelegateResponse);

// CancelUnbondingDelegation defines a method for performing canceling the unbonding delegation
// and delegate back to previous validator.
//
// Since: cosmos-sdk 0.46
rpc CancelUnbondingDelegation(MsgCancelUnbondingDelegation) returns (MsgCancelUnbondingDelegationResponse);
// rpc CancelUnbondingDelegation(MsgCancelUnbondingDelegation) returns (MsgCancelUnbondingDelegationResponse);
}

// MsgCreateValidator defines a SDK message for creating a new validator.
Expand All @@ -60,8 +67,9 @@ message MsgCreateValidator {
];
string delegator_address = 4 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string validator_address = 5 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any pubkey = 6 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
cosmos.base.v1beta1.Coin value = 7 [(gogoproto.nullable) = false];
string operator_blskey = 6 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any pubkey = 7 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
cosmos.base.v1beta1.Coin value = 8 [(gogoproto.nullable) = false];
}

// MsgCreateValidatorResponse defines the Msg/CreateValidator response type.
Expand All @@ -76,20 +84,45 @@ message MsgEditValidator {

Description description = 1 [(gogoproto.nullable) = false];
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string operator_blskey = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// We pass a reference to the new commission rate and min self delegation as
// it's not mandatory to update. If not updated, the deserialized rate will be
// zero with no way to distinguish if an update was intended.
// REF: #2373
string commission_rate = 3
string commission_rate = 4
[(cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
string min_self_delegation = 4
string min_self_delegation = 5
[(cosmos_proto.scalar) = "cosmos.Int", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];
}

// MsgEditValidatorResponse defines the Msg/EditValidator response type.
message MsgEditValidatorResponse {}

// MsgRemoveValidator defines a SDK message for removing an existing validator.
message MsgRemoveValidator {
option (cosmos.msg.v1.signer) = "validator_address";

option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string validator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// MsgRemoveValidatorResponse defines the Msg/RemoveValidator response type.
message MsgRemoveValidatorResponse {}

// MsgSelfDelegate defines a SDK message for performing a delegation of coins
// for a validator itself.
message MsgSelfDelegate {
option (cosmos.msg.v1.signer) = "validator_address";

option (gogoproto.equal) = false;

string validator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false];
}

// MsgDelegate defines a SDK message for performing a delegation of coins
// from a delegator to a validator.
message MsgDelegate {
Expand Down
20 changes: 20 additions & 0 deletions x/staking/keeper/alias_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress,
}
}

// iterate through all of the delegations to a validator
func (k Keeper) IterateDelegationsToValidator(ctx sdk.Context, valAddr sdk.AccAddress,
fn func(del types.DelegationI) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.DelegationKey)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
del := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
if del.ValidatorAddress != valAddr.String() {
continue
}
stop := fn(del)
if stop {
break
}
}
}

// return all delegations used during genesis dump
// TODO: remove this func, change all usage for iterate functionality
func (k Keeper) GetAllSDKDelegations(ctx sdk.Context) (delegations []types.Delegation) {
Expand Down
39 changes: 39 additions & 0 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,45 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
return &types.MsgEditValidatorResponse{}, nil
}

// RemoveValidator defines a method for removing an existing validator
func (k msgServer) RemoveValidator(goCtx context.Context, msg *types.MsgRemoveValidator) (*types.MsgRemoveValidatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, err
}
// validator must already be registered
_, found := k.GetValidator(ctx, valAddr)
if !found {
return nil, types.ErrNoValidatorFound
}

k.IterateDelegationsToValidator(ctx, sdk.MustAccAddressFromBech32(msg.ValidatorAddress), func(del types.DelegationI) (stop bool) {
_, err = k.Keeper.Undelegate(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr(), del.GetShares())
if err != nil {
return true
}

// TODO: emit events for undelegation?
return false
})

if err != nil {
return nil, err
}

return &types.MsgRemoveValidatorResponse{}, nil
}

// SelfDelegate defines a method for performing a delegation of coins for a validator itself
func (k msgServer) SelfDelegate(goCtx context.Context, msg *types.MsgSelfDelegate) (*types.MsgDelegateResponse, error) {
return k.Delegate(goCtx, &types.MsgDelegate{
DelegatorAddress: msg.ValidatorAddress,
ValidatorAddress: msg.ValidatorAddress,
Amount: msg.Amount,
})
}

// Delegate defines a method for performing a delegation of coins from a delegator to a validator
func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
Loading

0 comments on commit c982c26

Please sign in to comment.