From ea85d0b65326ed36b2cb0fecbc8f7727541dfca9 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 17 Nov 2020 12:36:46 +0100 Subject: [PATCH] Increase fee by a step from the two latest events Deposit requires the fee to be bumped by the constant value equal the initial redemption fee. To obtain the initial fee we looked for the very first event, we also need the latest event. It worked fine until there were multiple fee increases and the number of blocks to reach the first event exceeded 10k that is a default limitation for get past events. In such situation the first event that was returned wasn't actually the very first event registered on chain for the deposit. To overcome this issue we decided to calculate the initial fee as a delta between fees of the two latest events, as the step value is constant for each fee increase. --- pkg/extensions/tbtc/tbtc.go | 17 +++++- pkg/extensions/tbtc/tbtc_test.go | 98 +++++++++++++++++--------------- 2 files changed, 68 insertions(+), 47 deletions(-) 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), + ) + } } }