Skip to content

Commit

Permalink
Fix: withdraw delegator rewards in exec (#159)
Browse files Browse the repository at this point in the history
* Fix: jail validator on withdraw full stake

* Fix: withdraw rewards in exec
  • Loading branch information
aopoltorzhicky authored Mar 12, 2024
1 parent 68fc8b5 commit 172b0ff
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pkg/indexer/parser/events/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,29 @@ func handleExec(ctx *context.Context, events []storage.Event, msg *storage.Messa
if err := processWithdrawValidatorCommission(ctx, events, msg, idx); err != nil {
return err
}
case "cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward":
if err := processWithdrawDelegatorRewards(ctx, events, msg, idx); err != nil {
return err
}
case "/cosmos.slashing.v1beta1.MsgUnjail":
if err := processUnjail(ctx, events, idx); err != nil {
return err
}
default:
for i := *idx; i < len(events); i++ {
*idx += 1
if action := decoder.StringFromMap(events[*idx].Data, "action"); action != "" {
break
}
authMsgIdx, err := decoder.Int64FromMap(events[*idx].Data, "authz_msg_index")
if err != nil {
return err
}
if authMsgIdx != int64(i) || len(events)-1 == *idx {
break
}
}

}
}

Expand Down
169 changes: 167 additions & 2 deletions pkg/indexer/parser/events/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func Test_handleExec(t *testing.T) {
},
},
msg: &storage.Message{
Type: types.MsgDelegate,
Type: types.MsgExec,
Height: 844359,
InternalMsgs: []string{
"/cosmos.staking.v1beta1.MsgDelegate",
Expand Down Expand Up @@ -495,7 +495,7 @@ func Test_handleExec(t *testing.T) {
},
},
msg: &storage.Message{
Type: types.MsgDelegate,
Type: types.MsgExec,
Height: 595997,
InternalMsgs: []string{
"/cosmos.staking.v1beta1.MsgUndelegate",
Expand All @@ -504,6 +504,171 @@ func Test_handleExec(t *testing.T) {
},
},
idx: testsuite.Ptr(0),
}, {
name: "MsgWithdrawDelegatorReward",
events: []storage.Event{
{
Height: 977944,
Type: "coin_spent",
Data: map[string]any{
"amount": "57268utia",
"spender": "celestia1gsvxuzts55h70c4338cmypzphv7l0exwc33jmp",
},
}, {
Height: 977944,
Type: "coin_received",
Data: map[string]any{
"amount": "57268utia",
"receiver": "celestia17xpfvakm2amg962yls6f84z3kell8c5lpnjs3s",
},
}, {
Height: 977944,
Type: "transfer",
Data: map[string]any{
"amount": "57268utia",
"recipient": "celestia17xpfvakm2amg962yls6f84z3kell8c5lpnjs3s",
"sender": "celestia1gsvxuzts55h70c4338cmypzphv7l0exwc33jmp",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"sender": "celestia1gsvxuzts55h70c4338cmypzphv7l0exwc33jmp",
},
}, {
Height: 977944,
Type: "tx",
Data: map[string]any{
"fee": "57268utia",
"fee_payer": "celestia1gsvxuzts55h70c4338cmypzphv7l0exwc33jmp",
},
}, {
Height: 977944,
Type: "tx",
Data: map[string]any{
"acc_seq": "celestia1gsvxuzts55h70c4338cmypzphv7l0exwc33jmp/4",
},
}, {
Height: 977944,
Type: "tx",
Data: map[string]any{
"signature": "KwNCgyd6IKCxDzqwh2s8uwS88HbXBNt+gBrx3MJPsI91xY9UKZXJhPIYifMdUS/Fdo0R6EL91VRWc7RXMGK0yA==",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"action": "/cosmos.authz.v1beta1.MsgExec",
},
}, {
Height: 977944,
Type: "coin_spent",
Data: map[string]any{
"amount": "77utia",
"authz_msg_index": "0",
"spender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "coin_received",
Data: map[string]any{
"amount": "77utia",
"authz_msg_index": "0",
"receiver": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
},
}, {
Height: 977944,
Type: "transfer",
Data: map[string]any{
"amount": "77utia",
"authz_msg_index": "0",
"recipient": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
"sender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"authz_msg_index": "0",
"sender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "withdraw_rewards",
Data: map[string]any{
"amount": "77utia",
"authz_msg_index": "0",
"delegator": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
"validator": "celestiavaloper1pnzrk7yzx0nr9xrcjyswj7ram4qxlrfz28xvn6",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"authz_msg_index": "0",
"module": "distribution",
"sender": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
},
}, {
Height: 977944,
Type: "coin_spent",
Data: map[string]any{
"amount": "127utia",
"authz_msg_index": "1",
"spender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "coin_received",
Data: map[string]any{
"amount": "127utia",
"authz_msg_index": "1",
"receiver": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
},
}, {
Height: 977944,
Type: "transfer",
Data: map[string]any{
"amount": "127utia",
"authz_msg_index": "1",
"recipient": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
"sender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"authz_msg_index": "1",
"sender": "celestia1jv65s3grqf6v6jl3dp4t6c9t9rk99cd8k44vnj",
},
}, {
Height: 977944,
Type: "withdraw_rewards",
Data: map[string]any{
"amount": "127utia",
"authz_msg_index": "1",
"delegator": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
"validator": "celestiavaloper1nwu3ugynh8m6r7aphv0uxnca84t7gnruvyye9c",
},
}, {
Height: 977944,
Type: "message",
Data: map[string]any{
"authz_msg_index": "1",
"module": "distribution",
"sender": "celestia1zq2atge5df93w0l6xhm87r8uspjva632aqz4fe",
},
},
},
msg: &storage.Message{
Type: types.MsgExec,
Height: 977944,
InternalMsgs: []string{
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
},
},
idx: testsuite.Ptr(7),
},
}
for _, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions pkg/indexer/parser/events/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var eventHandlers = map[storageTypes.MsgType]EventHandler{
storageTypes.MsgCancelUnbondingDelegation: handleCancelUnbonding,
storageTypes.MsgExec: handleExec,
storageTypes.MsgWithdrawValidatorCommission: handleWithdrawValidatorCommission,
storageTypes.MsgWithdrawDelegatorReward: handleWithdrawDelegatorRewards,
storageTypes.MsgUnjail: handleUnjail,
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/indexer/parser/events/withdraw_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode/context"
"github.com/celenium-io/celestia-indexer/pkg/indexer/decode/decoder"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)

Expand Down Expand Up @@ -44,3 +46,35 @@ func parseWithdrawRewards(ctx *context.Context, msg *storage.Message, data map[s

return nil
}

func handleWithdrawDelegatorRewards(ctx *context.Context, events []storage.Event, msg *storage.Message, idx *int) error {
if idx == nil {
return errors.New("nil event index")
}
if msg == nil {
return errors.New("nil message in events hanler")
}
if action := decoder.StringFromMap(events[*idx].Data, "action"); action != "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward" {
return errors.Errorf("unexpected event action %s for message type %s", action, msg.Type.String())
}
*idx += 1
return processWithdrawDelegatorRewards(ctx, events, msg, idx)
}

func processWithdrawDelegatorRewards(ctx *context.Context, events []storage.Event, msg *storage.Message, idx *int) error {
for i := *idx; i < len(events); i++ {
switch events[i].Type {
case storageTypes.EventTypeMessage:
if module := decoder.StringFromMap(events[i].Data, "module"); module == storageTypes.ModuleNameDistribution.String() {
*idx = i + 1
return nil
}
case storageTypes.EventTypeWithdrawRewards:
if err := parseWithdrawRewards(ctx, msg, events[i].Data); err != nil {
return err
}
}
}
*idx = len(events) - 1
return nil
}

0 comments on commit 172b0ff

Please sign in to comment.