Skip to content

Commit

Permalink
Parse memo before looking up hash (#501)
Browse files Browse the repository at this point in the history
* Parse memo before looking up hash

* fix tests

* parseMemo correctly

---------

Co-authored-by: Ajaz Ahmed Ansari <[email protected]>
  • Loading branch information
Joe Bowman and ajansari95 authored Jul 13, 2023
1 parent ac7f003 commit a3fea64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 6 additions & 3 deletions x/interchainstaking/keeper/ibc_packet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ func (k *Keeper) handleSendToDelegate(ctx sdk.Context, zone *types.Zone, msg *ba
// if no other withdrawal records exist for this triple (i.e. no further withdrawal from this delegator account for this user (i.e. different validator))
// then burn the withdrawal_record's burn_amount.
func (k *Keeper) HandleWithdrawForUser(ctx sdk.Context, zone *types.Zone, msg *banktypes.MsgSend, memo string) error {
withdrawalRecord, found := k.GetWithdrawalRecord(ctx, zone.ChainId, memo, types.WithdrawStatusSend)
txHash, err := types.ParseTxMsgMemo(memo, types.MsgTypeUnbondSend)
if err != nil {
return err
}

withdrawalRecord, found := k.GetWithdrawalRecord(ctx, zone.ChainId, txHash, types.WithdrawStatusSend)
if !found {
return errors.New("no matching withdrawal record found")
}
Expand All @@ -433,7 +438,6 @@ func (k *Keeper) HandleWithdrawForUser(ctx sdk.Context, zone *types.Zone, msg *b
// if we can't burn the coins, fail.
return err
}
k.SetWithdrawalRecord(ctx, withdrawalRecord)
k.Logger(ctx).Info("burned coins post-withdrawal", "coins", withdrawalRecord.BurnAmount)
} else {

Expand All @@ -452,7 +456,6 @@ func (k *Keeper) HandleWithdrawForUser(ctx sdk.Context, zone *types.Zone, msg *b
// if we can't burn the coins, fail.
return err
}
k.SetWithdrawalRecord(ctx, withdrawalRecord)
k.Logger(ctx).Info("burned coins post-withdrawal", "coins", withdrawalRecord.BurnAmount)
}
break
Expand Down
24 changes: 15 additions & 9 deletions x/interchainstaking/keeper/ibc_packet_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUser() {
}
},
message: banktypes.MsgSend{},
memo: "7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
memo: "unbondSend/7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
err: true,
},
{
Expand All @@ -545,7 +545,7 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUser() {
message: banktypes.MsgSend{
Amount: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(4000000))),
},
memo: "7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
memo: "unbondSend/7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
err: false,
},
{
Expand Down Expand Up @@ -587,7 +587,7 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUser() {
message: banktypes.MsgSend{
Amount: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(15000000))),
},
memo: "d786f7d4c94247625c2882e921a790790eb77a00d0534d5c3154d0a9c5ab68f5",
memo: "unbondSend/d786f7d4c94247625c2882e921a790790eb77a00d0534d5c3154d0a9c5ab68f5",
err: false,
},
}
Expand Down Expand Up @@ -624,15 +624,18 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUser() {
suite.Require().NoError(err)
}

hash, err := icstypes.ParseTxMsgMemo(test.memo, icstypes.MsgTypeUnbondSend)
suite.Require().NoError(err)

quicksilver.InterchainstakingKeeper.IterateZoneStatusWithdrawalRecords(ctx, zone.ChainId, icstypes.WithdrawStatusSend, func(idx int64, withdrawal icstypes.WithdrawalRecord) bool {
if withdrawal.Txhash == test.memo {
if withdrawal.Txhash == hash {
suite.Require().Fail("unexpected withdrawal record; status should be Completed.")
}
return false
})

quicksilver.InterchainstakingKeeper.IterateZoneStatusWithdrawalRecords(ctx, zone.ChainId, icstypes.WithdrawStatusCompleted, func(idx int64, withdrawal icstypes.WithdrawalRecord) bool {
if withdrawal.Txhash != test.memo {
if withdrawal.Txhash != hash {
suite.Require().Fail("unexpected withdrawal record; status should be Completed.")
}
return false
Expand Down Expand Up @@ -674,7 +677,7 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUserLSM() {
{Amount: sdk.NewCoins(sdk.NewCoin(v1+"1", sdk.NewInt(1000000)))},
{Amount: sdk.NewCoins(sdk.NewCoin(v2+"2", sdk.NewInt(1000000)))},
},
memo: "7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
memo: "unbondSend/7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
err: false,
},
{
Expand All @@ -700,7 +703,7 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUserLSM() {
{Amount: sdk.NewCoins(sdk.NewCoin(v2+"1", sdk.NewInt(1500000)))},
{Amount: sdk.NewCoins(sdk.NewCoin(v1+"2", sdk.NewInt(1000000)))},
},
memo: "7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
memo: "unbondSend/7C8B95EEE82CB63771E02EBEB05E6A80076D70B2E0A1C457F1FD1A0EF2EA961D",
err: false,
},
}
Expand Down Expand Up @@ -740,15 +743,18 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUserLSM() {
}
}

hash, err := icstypes.ParseTxMsgMemo(test.memo, icstypes.MsgTypeUnbondSend)
suite.Require().NoError(err)

quicksilver.InterchainstakingKeeper.IterateZoneStatusWithdrawalRecords(ctx, zone.ChainId, icstypes.WithdrawStatusSend, func(idx int64, withdrawal icstypes.WithdrawalRecord) bool {
if withdrawal.Txhash == test.memo {
if withdrawal.Txhash == hash {
suite.Require().Fail("unexpected withdrawal record; status should be Completed.")
}
return false
})

quicksilver.InterchainstakingKeeper.IterateZoneStatusWithdrawalRecords(ctx, zone.ChainId, icstypes.WithdrawStatusCompleted, func(idx int64, withdrawal icstypes.WithdrawalRecord) bool {
if withdrawal.Txhash != test.memo {
if withdrawal.Txhash != hash {
suite.Require().Fail("unexpected withdrawal record; status should be Completed.")
}
return false
Expand Down

0 comments on commit a3fea64

Please sign in to comment.