Skip to content

Commit

Permalink
chore: add events for sequencer rewards (#335)
Browse files Browse the repository at this point in the history
Co-authored-by: Omri <[email protected]>
Co-authored-by: Daniel T <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2024
1 parent c3ac5ed commit 80b02ec
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
14 changes: 11 additions & 3 deletions x/dist/keeper/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
disttypes "github.com/dymensionxyz/dymension-rdk/x/dist/types"
)

// AllocateTokens handles distribution of the collected fees
Expand All @@ -28,21 +29,28 @@ func (k Keeper) AllocateTokens(ctx sdk.Context, blockProposer sdk.ConsAddress) {

/* ---------------------------- Pay the proposer ---------------------------- */
// calculate and pay proposer reward
proposerValidator, found := k.seqKeeper.GetSequencerByConsAddr(ctx, blockProposer)
proposer, found := k.seqKeeper.GetSequencerByConsAddr(ctx, blockProposer)
if !found {
logger.Error("failed to find the validator for this block. reward not allocated")
} else {
proposerReward := feesCollected.MulDecTruncate(k.GetBaseProposerReward(ctx))
proposerCoins, proposerRemainder := proposerReward.TruncateDecimal()
if !proposerCoins.IsZero() {
err := k.AllocateTokensToSequencer(ctx, proposerValidator, proposerCoins)
err := k.AllocateTokensToSequencer(ctx, proposer, proposerCoins)
if err != nil {
logger.Error("failed to reward the proposer")
}

remainingFees = feesCollected.Sub(proposerReward).Add(proposerRemainder...)

//TODO: emit event for sequencer reward
// update outstanding rewards
ctx.EventManager().EmitEvent(
sdk.NewEvent(
disttypes.EventTypeDistSequencerRewards,
sdk.NewAttribute(sdk.AttributeKeyAmount, proposerCoins.String()),
sdk.NewAttribute(disttypes.AttributeKeySequencer, proposer.GetOperator().String()),
),
)
}
}

Expand Down
9 changes: 6 additions & 3 deletions x/dist/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

Expand All @@ -12,9 +14,10 @@ func HandleCommunityPoolSpendProposal(ctx sdk.Context, k Keeper, p *types.Commun
return addrErr
}

// if k.bankKeeper.BlockedAddr(recipient) {
// return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive external funds", p.Recipient)
// }
if k.bankKeeper.BlockedAddr(recipient) {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "not allowed to receive external funds: recipient: %s", p.Recipient)

}

err := k.DistributeFromFeePool(ctx, p.Amount, recipient)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions x/dist/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

// Minting module event types.
const (
EventTypeDistSequencerRewards = "sequencer_rewards"
AttributeKeySequencer = "sequencer"
)
10 changes: 6 additions & 4 deletions x/hub-genesis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
tenderminttypes "github.com/cosmos/ibc-go/v6/modules/light-clients/07-tendermint/types"
Expand Down Expand Up @@ -31,16 +33,16 @@ func (m msgServer) TriggerGenesisEvent(goCtx context.Context, msg *types.MsgHubG

_, clientState, err := m.channelKeeper.GetChannelClientState(ctx, "transfer", msg.ChannelId)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidGenesisChannelId, "failed to get client state for channel %s", msg.ChannelId)
return nil, errorsmod.Wrapf(types.ErrInvalidGenesisChannelId, "failed to get client state for channel %s", msg.ChannelId)
}

tmClientState, ok := clientState.(*tenderminttypes.ClientState)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrInvalidGenesisChannelId, "expected tendermint client state, got %T", clientState)
return nil, errorsmod.Wrapf(types.ErrInvalidGenesisChannelId, "expected tendermint client state, got %T", clientState)
}

if tmClientState.GetChainID() != msg.HubId {
return nil, sdkerrors.Wrapf(types.ErrInvalidGenesisChainId, "channel %s is connected to chain ID %s, expected %s",
return nil, errorsmod.Wrapf(types.ErrInvalidGenesisChainId, "channel %s is connected to chain ID %s, expected %s",
msg.ChannelId, tmClientState.GetChainID(), msg.HubId)
}

Expand All @@ -53,7 +55,7 @@ func (m msgServer) TriggerGenesisEvent(goCtx context.Context, msg *types.MsgHubG
hub := types.NewHub(msg.HubId, msg.ChannelId)

if err := m.lockRollappGenesisTokens(ctx, hub.ChannelId); err != nil {
return nil, sdkerrors.Wrapf(types.ErrLockingGenesisTokens, "failed to lock tokens: %v", err)
return nil, errorsmod.Wrapf(types.ErrLockingGenesisTokens, "failed to lock tokens: %v", err)
}

// we save the hub in order to prevent the genesis event from being triggered again
Expand Down
8 changes: 5 additions & 3 deletions x/hub-genesis/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/dymensionxyz/dymension-rdk/testutil/utils"
"github.com/stretchr/testify/suite"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension-rdk/testutil/app"
"github.com/dymensionxyz/dymension-rdk/testutil/ibctest"
Expand Down Expand Up @@ -135,7 +137,7 @@ func (suite *HubGenesisMsgServerTestSuite) TestTriggerGenesisEvent() {
rollappBalanceAfter: initialRollappBalance,
rollappEscrowBalanceAfter: sdk.NewCoin(rollappDenom, sdk.NewInt(0)),
hubPersisted: false,
expErr: sdkerrors.Wrapf(types.ErrInvalidGenesisChannelId, "failed to get client state for channel %s", "invalid-channel"),
expErr: errorsmod.Wrapf(types.ErrInvalidGenesisChannelId, "failed to get client state for channel %s", "invalid-channel"),
}, {
name: "invalid rollapp genesis event - invalid chain id",
genesisState: &types.GenesisState{
Expand All @@ -152,7 +154,7 @@ func (suite *HubGenesisMsgServerTestSuite) TestTriggerGenesisEvent() {
rollappBalanceAfter: initialRollappBalance,
rollappEscrowBalanceAfter: sdk.NewCoin(rollappDenom, sdk.NewInt(0)),
hubPersisted: false,
expErr: sdkerrors.Wrapf(types.ErrInvalidGenesisChainId, "channel %s is connected to chain ID %s, expected %s", path.EndpointA.ChannelID, "invalid-chain-id", path.EndpointB.Chain.ChainID),
expErr: errorsmod.Wrapf(types.ErrInvalidGenesisChainId, "channel %s is connected to chain ID %s, expected %s", path.EndpointA.ChannelID, "invalid-chain-id", path.EndpointB.Chain.ChainID),
}, {
name: "invalid rollapp genesis event - module account has no coins",
genesisState: &types.GenesisState{
Expand All @@ -174,7 +176,7 @@ func (suite *HubGenesisMsgServerTestSuite) TestTriggerGenesisEvent() {
suite.Require().NoError(err)
},
hubPersisted: false,
expErr: sdkerrors.Wrapf(types.ErrLockingGenesisTokens, "failed to lock tokens: %v", types.ErrGenesisNoCoinsOnModuleAcc),
expErr: errorsmod.Wrapf(types.ErrLockingGenesisTokens, "failed to lock tokens: %v", types.ErrGenesisNoCoinsOnModuleAcc),
},
}

Expand Down
7 changes: 4 additions & 3 deletions x/hub-genesis/types/message_hub_genesis_event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -41,15 +42,15 @@ func (msg *MsgHubGenesisEvent) GetSignBytes() []byte {
func (msg *MsgHubGenesisEvent) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address (%s)", err)
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address (%s)", err)
}

if msg.ChannelId == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "channel id cannot be empty")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "channel id cannot be empty")
}

if msg.HubId == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "hub id cannot be empty")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "hub id cannot be empty")
}

return nil
Expand Down

0 comments on commit 80b02ec

Please sign in to comment.