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

Commit

Permalink
Merge pull request #611 from keep-network/fee-bump-step
Browse files Browse the repository at this point in the history
Increase redemption fee by a step from the two latest events

Deposit requires the fee to be bumped by the constant value equal to 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 getPastEvents. 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.
  • Loading branch information
pdyraga authored Nov 17, 2020
2 parents b658b02 + ea85d0b commit 207e019
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 47 deletions.
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

0 comments on commit 207e019

Please sign in to comment.