Skip to content

Commit

Permalink
add msg_server_withdraw_tip_legacy for tips before
Browse files Browse the repository at this point in the history
upgrade
  • Loading branch information
akremstudy committed Dec 10, 2024
1 parent acb3267 commit 77d55ab
Show file tree
Hide file tree
Showing 10 changed files with 1,614 additions and 144 deletions.
1,122 changes: 1,041 additions & 81 deletions api/layer/reporter/tx.pulsar.go

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions api/layer/reporter/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestLayerUpgrade(t *testing.T) {
}

func ChainUpgradeTest(t *testing.T, chainName, upgradeContainerRepo, upgradeVersion, upgradeName string) {
t.Helper()
if testing.Short() {
t.Skip("skipping in short mode")
}
Expand Down
14 changes: 14 additions & 0 deletions proto/layer/reporter/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ service Msg {

// WithdrawTip defines a method to withdraw tip from a reporter module.
rpc WithdrawTip(MsgWithdrawTip) returns (MsgWithdrawTipResponse);

// WithdrawTipLegacy defines a method to withdraw tip from a reporter module before v2 upgrade.
rpc WithdrawTipLegacy(MsgWithdrawTipLegacy) returns (MsgWithdrawTipLegacyResponse);

}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand Down Expand Up @@ -141,3 +145,13 @@ message MsgWithdrawTip {

// MsgWithdrawTipResponse defines the Msg/WithdrawTip response type.
message MsgWithdrawTipResponse {}

// MsgWithdrawTipLegacy defines the Msg/WithdrawTipLegacy request type.
message MsgWithdrawTipLegacy {
option (cosmos.msg.v1.signer) = "selector_address";
string selector_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
}

// MsgWithdrawTiLegacypResponse defines the Msg/WithdrawTipLegacy response type.
message MsgWithdrawTipLegacyResponse {}
8 changes: 4 additions & 4 deletions x/oracle/mocks/ReporterKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/oracle/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ReporterKeeper interface {
// Methods imported from reporter should be defined here
ReporterStake(ctx context.Context, repAddress sdk.AccAddress, queryId []byte) (math.Int, error)
AddTip(ctx context.Context, metaId uint64, tip Reward) error
AddTbr(ctx context.Context, metaId uint64, tbr Reward) error
AddTbr(ctx context.Context, height uint64, tbr Reward) error
}

type RegistryHooks interface {
Expand Down
4 changes: 2 additions & 2 deletions x/reporter/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func (k Keeper) AddTip(ctx context.Context, metaId uint64, tip oracletypes.Rewar
return k.Tip.Set(ctx, metaId, tip)
}

func (k Keeper) AddTbr(ctx context.Context, metaId uint64, tbr oracletypes.Reward) error {
return k.Tbr.Set(ctx, metaId, tbr)
func (k Keeper) AddTbr(ctx context.Context, height uint64, tbr oracletypes.Reward) error {
return k.Tbr.Set(ctx, height, tbr)
}

func (k Keeper) RewardByReporter(ctx context.Context, selAddr, repAddr sdk.AccAddress, metaId uint64, queryId []byte) (math.LegacyDec, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/reporter/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func TestRewardTrb(t *testing.T) {
BlockHeight: height + 1,
}))

require.NoError(t, k.AddTbr(ctx, metaId, oracletypes.Reward{
require.NoError(t, k.AddTbr(ctx, height+1, oracletypes.Reward{
TotalPower: 45,
Amount: reward.ToLegacyDec(),
CycleList: true,
Expand Down
74 changes: 74 additions & 0 deletions x/reporter/keeper/msg_server_withdraw_tip_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package keeper

import (
"context"
"errors"

layertypes "github.com/tellor-io/layer/types"
"github.com/tellor-io/layer/x/reporter/types"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// Msg: WithdrawTipLegacy, allows selectors to directly withdraw reporting rewards and stake them with a BONDED validator
func (k msgServer) WithdrawTipLegacy(goCtx context.Context, msg *types.MsgWithdrawTipLegacy) (*types.MsgWithdrawTipLegacyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
delAddr := sdk.MustAccAddressFromBech32(msg.SelectorAddress)
shares, err := k.Keeper.SelectorTips.Get(ctx, delAddr)
if err != nil {
return nil, err
}

valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, err
}
val, err := k.stakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
return nil, err
}

if !val.IsBonded() {
return nil, errors.New("chosen validator must be bonded")
}
amtToDelegate := shares.TruncateInt()
if amtToDelegate.IsZero() {
return nil, errors.New("no tips to withdraw")
}
_, err = k.Keeper.stakingKeeper.Delegate(ctx, delAddr, amtToDelegate, val.Status, val, false)
if err != nil {
return nil, err
}

// isolate decimals from shares
remainder := shares.Sub(shares.TruncateDec())
if remainder.IsZero() {
err = k.Keeper.SelectorTips.Remove(ctx, delAddr)
if err != nil {
return nil, err
}
} else {
err = k.Keeper.SelectorTips.Set(ctx, delAddr, remainder)
if err != nil {
return nil, err
}
}

// send coins
err = k.Keeper.bankKeeper.SendCoinsFromModuleToModule(ctx, types.TipsEscrowPool, stakingtypes.BondedPoolName, sdk.NewCoins(sdk.NewCoin(layertypes.BondDenom, math.NewInt(int64(amtToDelegate.Uint64())))))
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
"tip_withdrawn",
sdk.NewAttribute("selector", msg.SelectorAddress),
sdk.NewAttribute("validator", msg.ValidatorAddress),
sdk.NewAttribute("amount", amtToDelegate.String()),
),
})
return &types.MsgWithdrawTipLegacyResponse{}, nil
}
Loading

0 comments on commit 77d55ab

Please sign in to comment.