Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Increase redemption fee by a step from the two latest events #611

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pkg/extensions/tbtc/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,29 @@ 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,
)

newOutputValue := new(big.Int).Sub(
previousOutputValue,
redemptionRequestedEvents[0].RequestedFee, // initial fee
feeBumpStep,
)

err = t.chain.IncreaseRedemptionFee(
Expand Down
98 changes: 52 additions & 46 deletions pkg/extensions/tbtc/tbtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
}
}

Expand Down