-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathtransfer_test.go
198 lines (154 loc) · 7.67 KB
/
transfer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package fee_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
)
// Integration test to ensure ics29 works with ics20
func (suite *FeeTestSuite) TestFeeTransfer() {
testCases := []struct {
name string
coinsToTransfer sdk.Coins
}{
{
"transfer single denom",
sdk.NewCoins(ibctesting.TestCoin),
},
{
"transfer multiple denoms",
sdk.NewCoins(ibctesting.TestCoin, ibctesting.SecondaryTestCoin),
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path := ibctesting.NewPath(suite.chainA, suite.chainB)
feeTransferVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.V2}))
path.EndpointA.ChannelConfig.Version = feeTransferVersion
path.EndpointB.ChannelConfig.Version = feeTransferVersion
path.EndpointA.ChannelConfig.PortID = transfertypes.PortID
path.EndpointB.ChannelConfig.PortID = transfertypes.PortID
path.Setup()
fee := types.Fee{
RecvFee: defaultRecvFee,
AckFee: defaultAckFee,
TimeoutFee: defaultTimeoutFee,
}
msgs := []sdk.Msg{
types.NewMsgPayPacketFee(fee, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, suite.chainA.SenderAccount.GetAddress().String(), nil),
transfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, tc.coinsToTransfer, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(1, 100), 0, "", nil),
}
res, err := suite.chainA.SendMsgs(msgs...)
suite.Require().NoError(err) // message committed
// after incentivizing the packets
originalChainASenderAccountBalance := sdk.NewCoins(suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), ibctesting.TestCoin.Denom))
packet, err := ibctesting.ParsePacketFromEvents(res.Events)
suite.Require().NoError(err)
// register counterparty address on chainB
payeeAddr, err := sdk.AccAddressFromBech32(ibctesting.TestAccAddress)
suite.Require().NoError(err)
msgRegister := types.NewMsgRegisterCounterpartyPayee(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, suite.chainB.SenderAccount.GetAddress().String(), payeeAddr.String())
_, err = suite.chainB.SendMsgs(msgRegister)
suite.Require().NoError(err) // message committed
// relay packet
err = path.RelayPacket(packet)
suite.Require().NoError(err) // relay committed
// ensure relayers got paid
// relayer for forward relay: chainB.SenderAccount
// relayer for reverse relay: chainA.SenderAccount
// check forward relay balance
suite.Require().Equal(
fee.RecvFee,
sdk.NewCoins(suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), payeeAddr, ibctesting.TestCoin.Denom)),
)
suite.Require().Equal(
fee.AckFee, // ack fee paid, no refund needed since timeout_fee = recv_fee + ack_fee
sdk.NewCoins(suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), ibctesting.TestCoin.Denom)).Sub(originalChainASenderAccountBalance[0]))
})
}
}
func (suite *FeeTestSuite) TestTransferFeeUpgrade() {
var path *ibctesting.Path
testCases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
// configure the initial path to create a regular transfer channel
path.EndpointA.ChannelConfig.PortID = transfertypes.PortID
path.EndpointB.ChannelConfig.PortID = transfertypes.PortID
path.EndpointA.ChannelConfig.Version = transfertypes.V2
path.EndpointB.ChannelConfig.Version = transfertypes.V2
path.Setup()
// configure the channel upgrade to an incentivized fee enabled transfer channel
upgradeVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.V2}))
path.EndpointA.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion
path.EndpointB.ChannelConfig.ProposedUpgrade.Fields.Version = upgradeVersion
tc.malleate()
err := path.EndpointA.ChanUpgradeInit()
suite.Require().NoError(err)
err = path.EndpointB.ChanUpgradeTry()
suite.Require().NoError(err)
err = path.EndpointA.ChanUpgradeAck()
suite.Require().NoError(err)
err = path.EndpointB.ChanUpgradeConfirm()
suite.Require().NoError(err)
err = path.EndpointA.ChanUpgradeOpen()
suite.Require().NoError(err)
if tc.expError == nil {
channelA := path.EndpointA.GetChannel()
suite.Require().Equal(upgradeVersion, channelA.Version)
channelB := path.EndpointB.GetChannel()
suite.Require().Equal(upgradeVersion, channelB.Version)
isFeeEnabled := suite.chainA.GetSimApp().IBCFeeKeeper.IsFeeEnabled(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
suite.Require().True(isFeeEnabled)
isFeeEnabled = suite.chainB.GetSimApp().IBCFeeKeeper.IsFeeEnabled(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
suite.Require().True(isFeeEnabled)
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
msgs := []sdk.Msg{
types.NewMsgPayPacketFee(fee, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, suite.chainA.SenderAccount.GetAddress().String(), nil),
transfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.NewCoins(ibctesting.TestCoin), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(1, 100), 0, "", nil),
}
res, err := suite.chainA.SendMsgs(msgs...)
suite.Require().NoError(err) // message committed
feeEscrowAddr := suite.chainA.GetSimApp().AuthKeeper.GetModuleAddress(types.ModuleName)
escrowBalance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), feeEscrowAddr, sdk.DefaultBondDenom)
suite.Require().Equal(escrowBalance.Amount, fee.Total().AmountOf(sdk.DefaultBondDenom))
packet, err := ibctesting.ParsePacketFromEvents(res.Events)
suite.Require().NoError(err)
err = path.RelayPacket(packet)
suite.Require().NoError(err) // relay committed
escrowBalance = suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), feeEscrowAddr, sdk.DefaultBondDenom)
suite.Require().True(escrowBalance.IsZero())
} else {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expError)
}
})
}
}
func (suite *FeeTestSuite) TestOnesidedFeeMiddlewareTransferHandshake() {
RemoveFeeMiddleware(suite.chainB) // remove fee middleware from chainB
path := ibctesting.NewPath(suite.chainA, suite.chainB)
feeTransferVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.V2}))
path.EndpointA.ChannelConfig.Version = feeTransferVersion // this will be renegotiated by the Try step
path.EndpointB.ChannelConfig.Version = "" // this will be overwritten by the Try step
path.EndpointA.ChannelConfig.PortID = transfertypes.PortID
path.EndpointB.ChannelConfig.PortID = transfertypes.PortID
path.Setup()
suite.Require().Equal(path.EndpointA.ChannelConfig.Version, transfertypes.V2)
suite.Require().Equal(path.EndpointB.ChannelConfig.Version, transfertypes.V2)
}