Skip to content

Commit

Permalink
Merge branch 'main' into cache-swept-deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Apr 24, 2024
2 parents ee32326 + c3cef04 commit 3404496
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
40 changes: 39 additions & 1 deletion pkg/tbtc/moving_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,26 @@ func ValidateMovingFundsProposal(
mainUTXO *bitcoin.UnspentTransactionOutput,
proposal *MovingFundsProposal,
) error

GetWallet(walletPublicKeyHash [20]byte) (*WalletChainData, error)
},
) error {
validateProposalLogger.Infof("calling chain for proposal validation")

err := chain.ValidateMovingFundsProposal(
walletChainData, err := chain.GetWallet(walletPublicKeyHash)
if err != nil {
return fmt.Errorf(
"cannot get wallet's chain data: [%w]",
err,
)
}

err = ValidateMovingFundsSafetyMargin(walletChainData)
if err != nil {
return fmt.Errorf("moving funds proposal is invalid: [%v]", err)
}

err = chain.ValidateMovingFundsProposal(
walletPublicKeyHash,
mainUTXO,
proposal,
Expand All @@ -329,6 +344,29 @@ func ValidateMovingFundsProposal(
return nil
}

// ValidateMovingFundsSafetyMargin checks if the moving funds safety margin
// is in force.
//
// Wallets that just entered the MovingFunds state may have received some last
// minute deposits just before. Even though deposit sweep typically occurs
// before moving funds, such deposits may not be mature enough or have enough
// confirmations to be swept yet. MovingFunds wallets cannot receive new
// deposits so, it makes sense to preserve a safety margin before moving
// funds to give the last minute deposits a chance to become eligible for
// deposit sweep.
func ValidateMovingFundsSafetyMargin(
walletChainData *WalletChainData,
) error {
safetyMargin := time.Duration(24) * time.Hour
safetyMarginExpiresAt := walletChainData.MovingFundsRequestedAt.Add(safetyMargin)

if time.Now().Before(safetyMarginExpiresAt) {
return fmt.Errorf("safety margin in force")
}

return nil
}

func (mfa *movingFundsAction) wallet() wallet {
return mfa.movingFundsWallet
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/tbtcpg/moving_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func (mft *MovingFundsTask) Run(request *tbtc.CoordinationProposalRequest) (
return nil, false, nil
}

// Check the safety margin for moving funds early. This will prevent
// commitment submission if the wallet is not safe to move funds.
err = tbtc.ValidateMovingFundsSafetyMargin(walletChainData)
if err != nil {
taskLogger.Infof("source wallet moving funds safety margin validation failed: [%v]", err)
return nil, false, nil
}

if walletChainData.PendingRedemptionsValue > 0 {
taskLogger.Infof("source wallet has pending redemptions")
return nil, false, nil
Expand All @@ -103,12 +111,14 @@ func (mft *MovingFundsTask) Run(request *tbtc.CoordinationProposalRequest) (
return nil, false, nil
}

// The wallet should not have any unswept deposits.
// The wallet should not have any unswept deposits. It's enough to find at
// least one unswept deposit. A single unswept deposit means that the wallet
// should not move funds yet.
unsweptDeposits, err := FindDeposits(
mft.chain,
mft.btcChain,
walletPublicKeyHash,
0,
1,
true,
true,
)
Expand Down
7 changes: 7 additions & 0 deletions pkg/tbtcpg/moving_funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,13 @@ func TestMovingFundsAction_ProposeMovingFunds(t *testing.T) {

btcChain.SetEstimateSatPerVByteFee(1, 25)

tbtcChain.SetWallet(
walletPublicKeyHash,
&tbtc.WalletChainData{
MovingFundsRequestedAt: time.Now().Add(-25 * time.Hour),
},
)

tbtcChain.SetMovingFundsParameters(
txMaxTotalFee,
0,
Expand Down

0 comments on commit 3404496

Please sign in to comment.