diff --git a/pkg/extensions/tbtc/tbtc.go b/pkg/extensions/tbtc/tbtc.go index 1637856bd..c9030aa44 100644 --- a/pkg/extensions/tbtc/tbtc.go +++ b/pkg/extensions/tbtc/tbtc.go @@ -472,6 +472,21 @@ func (t *tbtc) monitorProvideRedemptionProof( latestRedemptionRequestedEvent := redemptionRequestedEvents[len(redemptionRequestedEvents)-1] + // Deposit expects that the fee is always increased by a constant value + // equal to the fee of the initial redemption request. + feeBumpStep := big.NewInt(0) + if len(redemptionRequestedEvents) == 1 { + feeBumpStep = latestRedemptionRequestedEvent.RequestedFee // initial fee + } else { + // When there are many events on-chain we don't need to get the very + // first one, it is enough to calculate a difference between the + // latest fee and the one before the latest fee. + feeBumpStep = new(big.Int).Sub( + latestRedemptionRequestedEvent.RequestedFee, + redemptionRequestedEvents[len(redemptionRequestedEvents)-2].RequestedFee, + ) + } + previousOutputValue := new(big.Int).Sub( latestRedemptionRequestedEvent.UtxoValue, latestRedemptionRequestedEvent.RequestedFee, @@ -479,7 +494,7 @@ func (t *tbtc) monitorProvideRedemptionProof( newOutputValue := new(big.Int).Sub( previousOutputValue, - redemptionRequestedEvents[0].RequestedFee, // initial fee + feeBumpStep, ) err = t.chain.IncreaseRedemptionFee( diff --git a/pkg/extensions/tbtc/tbtc_test.go b/pkg/extensions/tbtc/tbtc_test.go index 0b861c2c1..8673a769f 100644 --- a/pkg/extensions/tbtc/tbtc_test.go +++ b/pkg/extensions/tbtc/tbtc_test.go @@ -1189,58 +1189,64 @@ func TestProvideRedemptionProof_TimeoutElapsed(t *testing.T) { t.Fatal(err) } - keepSignature, err := submitKeepSignature(depositAddress, tbtcChain) - if err != nil { - t.Fatal(err) - } - - err = tbtcChain.ProvideRedemptionSignature( - depositAddress, - keepSignature.V, - keepSignature.R, - keepSignature.S, - ) - if err != nil { - t.Fatal(err) - } - - // wait a bit longer than the monitoring timeout - // to make sure the potential transaction completes - time.Sleep(2 * timeout) + // Increase the fee at least 3 times to test fee increase step determination + // from the two latests events. + for i := 1; i <= 3; i++ { + keepSignature, err := submitKeepSignature(depositAddress, tbtcChain) + if err != nil { + t.Fatal(err) + } - expectedIncreaseRedemptionFeeCalls := 1 - actualIncreaseRedemptionFeeCalls := tbtcChain.Logger(). - IncreaseRedemptionFeeCalls() - if expectedIncreaseRedemptionFeeCalls != actualIncreaseRedemptionFeeCalls { - t.Errorf( - "unexpected number of IncreaseRedemptionFee calls\n"+ - "expected: [%v]\n"+ - "actual: [%v]", - expectedIncreaseRedemptionFeeCalls, - actualIncreaseRedemptionFeeCalls, + err = tbtcChain.ProvideRedemptionSignature( + depositAddress, + keepSignature.V, + keepSignature.R, + keepSignature.S, ) - } + if err != nil { + t.Fatal(err) + } - expectedDepositRedemptionFee := new(big.Int).Mul( - big.NewInt(2), - initialDepositRedemptionFee, - ) + // wait a bit longer than the monitoring timeout + // to make sure the potential transaction completes + time.Sleep(2 * timeout) + + expectedIncreaseRedemptionFeeCalls := i + actualIncreaseRedemptionFeeCalls := tbtcChain.Logger(). + IncreaseRedemptionFeeCalls() + if expectedIncreaseRedemptionFeeCalls != actualIncreaseRedemptionFeeCalls { + t.Errorf( + "unexpected number of IncreaseRedemptionFee calls after [%d] increase\n"+ + "expected: [%v]\n"+ + "actual: [%v]", + i, + expectedIncreaseRedemptionFeeCalls, + actualIncreaseRedemptionFeeCalls, + ) + } - actualDepositRedemptionFee, err := tbtcChain.DepositRedemptionFee( - depositAddress, - ) - if err != nil { - t.Fatal(err) - } + expectedDepositRedemptionFee := new(big.Int).Mul( + new(big.Int).Add(big.NewInt(1), big.NewInt(int64(i))), + initialDepositRedemptionFee, + ) - if expectedDepositRedemptionFee.Cmp(actualDepositRedemptionFee) != 0 { - t.Errorf( - "unexpected redemption fee value\n"+ - "expected: [%v]\n"+ - "actual: [%v]", - expectedDepositRedemptionFee.Text(10), - actualDepositRedemptionFee.Text(10), + actualDepositRedemptionFee, err := tbtcChain.DepositRedemptionFee( + depositAddress, ) + if err != nil { + t.Fatal(err) + } + + if expectedDepositRedemptionFee.Cmp(actualDepositRedemptionFee) != 0 { + t.Errorf( + "unexpected redemption fee value after [%d] increase\n"+ + "expected: [%v]\n"+ + "actual: [%v]", + i, + expectedDepositRedemptionFee.Text(10), + actualDepositRedemptionFee.Text(10), + ) + } } }