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

notify rewards to provider #954

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions proto/interchain_security/ccv/v1/ccv.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ message SlashPacketData {
cosmos.staking.v1beta1.InfractionType infraction = 3;
}

message NotifyRewardsPacketData {
int64 block_height = 1;
}

// MaturedUnbondingOps defines a list of ids corresponding to ids of matured unbonding operations.
message MaturedUnbondingOps {
repeated uint64 ids = 1;
Expand All @@ -65,6 +69,7 @@ message ConsumerPacketData {
oneof data {
SlashPacketData slashPacketData = 2;
VSCMaturedPacketData vscMaturedPacketData = 3;
NotifyRewardsPacketData notifyRewardsPacketData = 4;
}
}

Expand All @@ -86,4 +91,6 @@ enum ConsumerPacketDataType {
CONSUMER_PACKET_TYPE_SLASH = 1 [(gogoproto.enumvalue_customname) = "SlashPacket"];
// VSCMatured packet
CONSUMER_PACKET_TYPE_VSCM = 2 [(gogoproto.enumvalue_customname) = "VscMaturedPacket"];
// NotifyReward packet
CONSUMER_PACKET_TYPE_NOTIFY_REWARDS = 3 [(gogoproto.enumvalue_customname) = "NotifyRewardsPacket"];
}
Empty file modified scripts/protocgen.sh
100644 → 100755
Empty file.
25 changes: 5 additions & 20 deletions x/ccv/consumer/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,11 @@ import (
// Reward Distribution follows a simple model: send tokens to the fee pool
// of the provider validator set
func (k Keeper) EndBlockRD(ctx sdk.Context) {
// Split blocks rewards.
// It panics in case of marshalling / unmarshalling errors or
// if sending coins between module accounts fails.
k.DistributeRewardsInternally(ctx)

if !k.shouldSendRewardsToProvider(ctx) {
if !k.shouldNotifyRewardsToProvider(ctx) {
return
}

// Try to send rewards to provider
cachedCtx, writeCache := ctx.CacheContext()
if err := k.SendRewardsToProvider(cachedCtx); err != nil {
k.Logger(ctx).Error("attempt to sent rewards to provider failed", "error", err)
} else {
// The cached context is created with a new EventManager so we merge the event
// into the original context
ctx.EventManager().EmitEvents(cachedCtx.EventManager().Events())
// write cache
writeCache()
}
k.QueueNotifyRewardsPackets(ctx)

// Update LastTransmissionBlockHeight
newLtbh := types.LastTransmissionBlockHeight{
Expand Down Expand Up @@ -91,16 +76,16 @@ func (k Keeper) DistributeRewardsInternally(ctx sdk.Context) {
}

// Check whether it's time to send rewards to provider
func (k Keeper) shouldSendRewardsToProvider(ctx sdk.Context) bool {
func (k Keeper) shouldNotifyRewardsToProvider(ctx sdk.Context) bool {
bpdt := k.GetBlocksPerDistributionTransmission(ctx)
curHeight := ctx.BlockHeight()
ltbh := k.GetLastTransmissionBlockHeight(ctx)
return (curHeight - ltbh.Height) >= bpdt
}

// SendRewardsToProvider attempts to send to the provider (via IBC)
// SendRewardsNotificationToProvider attempts to send to the provider (via IBC)
// all the block rewards allocated for the provider
func (k Keeper) SendRewardsToProvider(ctx sdk.Context) error {
func (k Keeper) SendRewardsNotificationToProvider(ctx sdk.Context) error {
// empty out the toSendToProviderTokens address
ch := k.GetDistributionTransmissionChannel(ctx)
transferChannel, found := k.channelKeeper.GetChannel(ctx, transfertypes.PortID, ch)
Expand Down
23 changes: 22 additions & 1 deletion x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v4/modules/core/exported"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/interchain-security/x/ccv/consumer/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// OnRecvVSCPacket sets the pending validator set changes that will be flushed to ABCI on Endblock
Expand Down Expand Up @@ -87,6 +88,26 @@ func (k Keeper) OnRecvVSCPacket(ctx sdk.Context, packet channeltypes.Packet, new
return ack
}

func (k *Keeper) QueueNotifyRewardsPackets(ctx sdk.Context) {
packet := ccv.NewNotifyRewardsPacketData(ctx.BlockHeight())
k.AppendPendingPacket(ctx, ccv.ConsumerPacketData{
Type: ccv.NotifyRewardsPacket,
Data: &ccv.ConsumerPacketData_NotifyRewardsPacketData{NotifyRewardsPacketData: packet},
})

k.Logger(ctx).Info("NotifyRewardsPacket enqueued", "blockHeight", packet.BlockHeight)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventNotifyRewards,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeChainID, ctx.ChainID()),
sdk.NewAttribute(ccv.AttributeConsumerHeight, strconv.Itoa(int(ctx.BlockHeight()))),
sdk.NewAttribute(ccv.AttributeTimestamp, ctx.BlockTime().String()),
),
)
}

// QueueVSCMaturedPackets appends matured VSCs to an internal queue.
//
// Note: Per spec, a VSC reaching maturity on a consumer chain means that all the unbonding
Expand Down
4 changes: 4 additions & 0 deletions x/ccv/provider/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
porttypes "github.com/cosmos/ibc-go/v4/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v4/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v4/modules/core/exported"

"github.com/cosmos/interchain-security/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
Expand Down Expand Up @@ -190,6 +191,9 @@ func (am AppModule) OnRecvPacket(
case ccv.SlashPacket:
// handle SlashPacket
ack = am.keeper.OnRecvSlashPacket(ctx, packet, *consumerPacket.GetSlashPacketData())
case ccv.NotifyRewardsPacket:
// do nothing
ack = channeltypes.NewResultAcknowledgement([]byte{byte(1)})
default:
errAck := channeltypes.NewErrorAcknowledgement(fmt.Errorf("invalid consumer packet type: %q", consumerPacket.Type))
ack = &errAck
Expand Down
4 changes: 4 additions & 0 deletions x/ccv/types/ccv.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func NewSlashPacketData(validator abci.Validator, valUpdateId uint64, infraction
}
}

func NewNotifyRewardsPacketData(blockHeight int64) *NotifyRewardsPacketData {
return &NotifyRewardsPacketData{BlockHeight: blockHeight}
}

func (vdt SlashPacketData) ValidateBasic() error {
if len(vdt.Validator.Address) == 0 || vdt.Validator.Power == 0 {
return sdkerrors.Wrap(ErrInvalidPacketData, "validator fields cannot be empty")
Expand Down
Loading