@@ -348,6 +348,124 @@ func (suite *TransferTestSuite) TestOnRecvPacket() {
348
348
}
349
349
}
350
350
351
+ func (suite * TransferTestSuite ) TestOnAcknowledgePacket () {
352
+ var (
353
+ path * ibctesting.Path
354
+ packet channeltypes.Packet
355
+ ack []byte
356
+ )
357
+
358
+ testCases := []struct {
359
+ name string
360
+ malleate func ()
361
+ expError error
362
+ expRefund bool
363
+ }{
364
+ {
365
+ "success" ,
366
+ func () {},
367
+ nil ,
368
+ false ,
369
+ },
370
+ {
371
+ "success: refund coins" ,
372
+ func () {
373
+ ack = channeltypes .NewErrorAcknowledgement (ibcerrors .ErrInsufficientFunds ).Acknowledgement ()
374
+ },
375
+ nil ,
376
+ true ,
377
+ },
378
+ {
379
+ "cannot refund ack on non-existent channel" ,
380
+ func () {
381
+ ack = channeltypes .NewErrorAcknowledgement (ibcerrors .ErrInsufficientFunds ).Acknowledgement ()
382
+
383
+ packet .SourceChannel = "channel-100"
384
+ },
385
+ errors .New ("unable to unescrow tokens" ),
386
+ false ,
387
+ },
388
+ {
389
+ "invalid packet data" ,
390
+ func () {
391
+ packet .Data = []byte ("invalid data" )
392
+ },
393
+ ibcerrors .ErrInvalidType ,
394
+ false ,
395
+ },
396
+ {
397
+ "invalid acknowledgement" ,
398
+ func () {
399
+ ack = []byte ("invalid ack" )
400
+ },
401
+ ibcerrors .ErrUnknownRequest ,
402
+ false ,
403
+ },
404
+ {
405
+ "cannot refund already acknowledged packet" ,
406
+ func () {
407
+ ack = channeltypes .NewErrorAcknowledgement (ibcerrors .ErrInsufficientFunds ).Acknowledgement ()
408
+
409
+ cbs , ok := suite .chainA .App .GetIBCKeeper ().PortKeeper .Route (ibctesting .TransferPort )
410
+ suite .Require ().True (ok )
411
+
412
+ suite .Require ().NoError (cbs .OnAcknowledgementPacket (suite .chainA .GetContext (), path .EndpointA .GetChannel ().Version , packet , ack , suite .chainA .SenderAccount .GetAddress ()))
413
+ },
414
+ errors .New ("unable to unescrow tokens" ),
415
+ false ,
416
+ },
417
+ }
418
+
419
+ for _ , tc := range testCases {
420
+ tc := tc
421
+ suite .Run (tc .name , func () {
422
+ suite .SetupTest () // reset
423
+
424
+ path = ibctesting .NewTransferPath (suite .chainA , suite .chainB )
425
+ path .Setup ()
426
+
427
+ timeoutHeight := suite .chainA .GetTimeoutHeight ()
428
+ msg := types .NewMsgTransfer (
429
+ path .EndpointA .ChannelConfig .PortID ,
430
+ path .EndpointA .ChannelID ,
431
+ ibctesting .TestCoin ,
432
+ suite .chainA .SenderAccount .GetAddress ().String (),
433
+ suite .chainB .SenderAccount .GetAddress ().String (),
434
+ timeoutHeight ,
435
+ 0 ,
436
+ "" ,
437
+ )
438
+ res , err := suite .chainA .SendMsgs (msg )
439
+ suite .Require ().NoError (err ) // message committed
440
+
441
+ packet , err = ibctesting .ParsePacketFromEvents (res .Events )
442
+ suite .Require ().NoError (err )
443
+
444
+ cbs , ok := suite .chainA .App .GetIBCKeeper ().PortKeeper .Route (ibctesting .TransferPort )
445
+ suite .Require ().True (ok )
446
+
447
+ ack = channeltypes .NewResultAcknowledgement ([]byte {byte (1 )}).Acknowledgement ()
448
+
449
+ tc .malleate () // change fields in packet
450
+
451
+ err = cbs .OnAcknowledgementPacket (suite .chainA .GetContext (), path .EndpointA .GetChannel ().Version , packet , ack , suite .chainA .SenderAccount .GetAddress ())
452
+
453
+ if tc .expError == nil {
454
+ suite .Require ().NoError (err )
455
+
456
+ if tc .expRefund {
457
+ escrowAddress := types .GetEscrowAddress (packet .GetSourcePort (), packet .GetSourceChannel ())
458
+ escrowBalanceAfter := suite .chainA .GetSimApp ().BankKeeper .GetBalance (suite .chainA .GetContext (), escrowAddress , sdk .DefaultBondDenom )
459
+ suite .Require ().Equal (sdkmath .NewInt (0 ), escrowBalanceAfter .Amount )
460
+ }
461
+ } else {
462
+ suite .Require ().Error (err )
463
+ suite .Require ().Contains (err .Error (), tc .expError .Error ())
464
+ }
465
+ })
466
+ }
467
+ }
468
+
351
469
func (suite * TransferTestSuite ) TestOnTimeoutPacket () {
352
470
var path * ibctesting.Path
353
471
var packet channeltypes.Packet
0 commit comments