multi: add itest testSweepAnchorCPFPLocalForceClose

This commit adds an itest case that focuses on validating the CPFP logic
in anchor sweeping.
This commit is contained in:
yyforyongyu
2024-04-01 21:28:54 +08:00
parent a2b8f4e19c
commit 94e0e32c74
6 changed files with 434 additions and 7 deletions

View File

@@ -1979,9 +1979,9 @@ func (h *HarnessTest) CalculateTxFee(tx *wire.MsgTx) btcutil.Amount {
parentHash := in.PreviousOutPoint.Hash
rawTx := h.Miner.GetRawTransaction(&parentHash)
parent := rawTx.MsgTx()
balance += btcutil.Amount(
parent.TxOut[in.PreviousOutPoint.Index].Value,
)
value := parent.TxOut[in.PreviousOutPoint.Index].Value
balance += btcutil.Amount(value)
}
for _, out := range tx.TxOut {
@@ -1991,6 +1991,24 @@ func (h *HarnessTest) CalculateTxFee(tx *wire.MsgTx) btcutil.Amount {
return balance
}
// CalculateTxWeight calculates the weight for a given tx.
//
// TODO(yy): use weight estimator to get more accurate result.
func (h *HarnessTest) CalculateTxWeight(tx *wire.MsgTx) int64 {
utx := btcutil.NewTx(tx)
return blockchain.GetTransactionWeight(utx)
}
// CalculateTxFeeRate calculates the fee rate for a given tx.
func (h *HarnessTest) CalculateTxFeeRate(
tx *wire.MsgTx) chainfee.SatPerKWeight {
w := h.CalculateTxWeight(tx)
fee := h.CalculateTxFee(tx)
return chainfee.NewSatPerKWeight(fee, uint64(w))
}
// CalculateTxesFeeRate takes a list of transactions and estimates the fee rate
// used to sweep them.
//

View File

@@ -2603,8 +2603,32 @@ func (h *HarnessTest) AssertNumPendingSweeps(hn *node.HarnessNode, n int) {
return nil
}
return fmt.Errorf("want %d , got %d", n, num)
desc := "\n"
for _, s := range resp.PendingSweeps {
desc += fmt.Sprintf("op=%v:%v, amt=%v, type=%v\n",
s.Outpoint.TxidStr, s.Outpoint.OutputIndex,
s.AmountSat, s.WitnessType)
}
return fmt.Errorf("want %d , got %d, sweeps: %s", n, num, desc)
}, DefaultTimeout)
require.NoErrorf(h, err, "%s: check pending sweeps timeout", hn.Name())
}
// FindSweepingTxns asserts the expected number of sweeping txns are found in
// the txns specified and return them.
func (h *HarnessTest) FindSweepingTxns(txns []*wire.MsgTx,
expectedNumSweeps int, closeTxid chainhash.Hash) []*wire.MsgTx {
var sweepTxns []*wire.MsgTx
for _, tx := range txns {
if tx.TxIn[0].PreviousOutPoint.Hash == closeTxid {
sweepTxns = append(sweepTxns, tx)
}
}
require.Len(h, sweepTxns, expectedNumSweeps, "unexpected num of sweeps")
return sweepTxns
}