Skip to content

Commit a57761a

Browse files
committed
sweep: update storeRecord to include utxo index
In this commit, we complete a recently added feature by ensuring that even if we go through the RBF loop to create a txn, that we still populate the `outpointToIndex` map. Unit tests have been updated to ensure this is always set as expected.
1 parent 4c9d25c commit a57761a

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

sweep/fee_bumper.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (t *TxPublisher) createRBFCompliantTx(req *BumpRequest,
454454
case err == nil:
455455
// The tx is valid, return the request ID.
456456
requestID := t.storeRecord(
457-
sweepCtx.tx, req, f, sweepCtx.fee,
457+
sweepCtx.tx, req, f, sweepCtx.fee, sweepCtx.outpointToTxIndex,
458458
)
459459

460460
log.Infof("Created tx %v for %v inputs: feerate=%v, "+
@@ -510,7 +510,8 @@ func (t *TxPublisher) createRBFCompliantTx(req *BumpRequest,
510510

511511
// storeRecord stores the given record in the records map.
512512
func (t *TxPublisher) storeRecord(tx *wire.MsgTx, req *BumpRequest,
513-
f FeeFunction, fee btcutil.Amount) uint64 {
513+
f FeeFunction, fee btcutil.Amount,
514+
outpointToTxIndex map[wire.OutPoint]int) uint64 {
514515

515516
// Increase the request counter.
516517
//
@@ -520,10 +521,11 @@ func (t *TxPublisher) storeRecord(tx *wire.MsgTx, req *BumpRequest,
520521

521522
// Register the record.
522523
t.records.Store(requestID, &monitorRecord{
523-
tx: tx,
524-
req: req,
525-
feeFunction: f,
526-
fee: fee,
524+
tx: tx,
525+
req: req,
526+
feeFunction: f,
527+
fee: fee,
528+
outpointToTxIndex: outpointToTxIndex,
527529
})
528530

529531
return requestID

sweep/fee_bumper_test.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,16 @@ func TestStoreRecord(t *testing.T) {
323323
// Get the current counter and check it's increased later.
324324
initialCounter := tp.requestCounter.Load()
325325

326+
op := wire.OutPoint{
327+
Hash: chainhash.Hash{1},
328+
Index: 0,
329+
}
330+
utxoIndex := map[wire.OutPoint]int{
331+
op: 0,
332+
}
333+
326334
// Call the method under test.
327-
requestID := tp.storeRecord(tx, req, feeFunc, fee)
335+
requestID := tp.storeRecord(tx, req, feeFunc, fee, utxoIndex)
328336

329337
// Check the request ID is as expected.
330338
require.Equal(t, initialCounter+1, requestID)
@@ -336,6 +344,7 @@ func TestStoreRecord(t *testing.T) {
336344
require.Equal(t, feeFunc, record.feeFunction)
337345
require.Equal(t, fee, record.fee)
338346
require.Equal(t, req, record.req)
347+
require.Equal(t, utxoIndex, record.outpointToTxIndex)
339348
}
340349

341350
// mockers wraps a list of mocked interfaces used inside tx publisher.
@@ -665,9 +674,17 @@ func TestTxPublisherBroadcast(t *testing.T) {
665674
feerate := chainfee.SatPerKWeight(1000)
666675
m.feeFunc.On("FeeRate").Return(feerate)
667676

677+
op := wire.OutPoint{
678+
Hash: chainhash.Hash{1},
679+
Index: 0,
680+
}
681+
utxoIndex := map[wire.OutPoint]int{
682+
op: 0,
683+
}
684+
668685
// Create a testing record and put it in the map.
669686
fee := btcutil.Amount(1000)
670-
requestID := tp.storeRecord(tx, req, m.feeFunc, fee)
687+
requestID := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
671688

672689
// Quickly check when the requestID cannot be found, an error is
673690
// returned.
@@ -754,6 +771,14 @@ func TestRemoveResult(t *testing.T) {
754771
// Create a testing record and put it in the map.
755772
fee := btcutil.Amount(1000)
756773

774+
op := wire.OutPoint{
775+
Hash: chainhash.Hash{1},
776+
Index: 0,
777+
}
778+
utxoIndex := map[wire.OutPoint]int{
779+
op: 0,
780+
}
781+
757782
testCases := []struct {
758783
name string
759784
setupRecord func() uint64
@@ -765,7 +790,7 @@ func TestRemoveResult(t *testing.T) {
765790
// removed.
766791
name: "remove on TxConfirmed",
767792
setupRecord: func() uint64 {
768-
id := tp.storeRecord(tx, req, m.feeFunc, fee)
793+
id := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
769794
tp.subscriberChans.Store(id, nil)
770795

771796
return id
@@ -780,7 +805,7 @@ func TestRemoveResult(t *testing.T) {
780805
// When the tx is failed, the records will be removed.
781806
name: "remove on TxFailed",
782807
setupRecord: func() uint64 {
783-
id := tp.storeRecord(tx, req, m.feeFunc, fee)
808+
id := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
784809
tp.subscriberChans.Store(id, nil)
785810

786811
return id
@@ -796,7 +821,7 @@ func TestRemoveResult(t *testing.T) {
796821
// Noop when the tx is neither confirmed or failed.
797822
name: "noop when tx is not confirmed or failed",
798823
setupRecord: func() uint64 {
799-
id := tp.storeRecord(tx, req, m.feeFunc, fee)
824+
id := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
800825
tp.subscriberChans.Store(id, nil)
801826

802827
return id
@@ -844,9 +869,17 @@ func TestNotifyResult(t *testing.T) {
844869
// Create a test tx.
845870
tx := &wire.MsgTx{LockTime: 1}
846871

872+
op := wire.OutPoint{
873+
Hash: chainhash.Hash{1},
874+
Index: 0,
875+
}
876+
utxoIndex := map[wire.OutPoint]int{
877+
op: 0,
878+
}
879+
847880
// Create a testing record and put it in the map.
848881
fee := btcutil.Amount(1000)
849-
requestID := tp.storeRecord(tx, req, m.feeFunc, fee)
882+
requestID := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
850883

851884
// Create a subscription to the event.
852885
subscriber := make(chan *BumpResult, 1)
@@ -1201,9 +1234,17 @@ func TestHandleTxConfirmed(t *testing.T) {
12011234
// Create a test tx.
12021235
tx := &wire.MsgTx{LockTime: 1}
12031236

1237+
op := wire.OutPoint{
1238+
Hash: chainhash.Hash{1},
1239+
Index: 0,
1240+
}
1241+
utxoIndex := map[wire.OutPoint]int{
1242+
op: 0,
1243+
}
1244+
12041245
// Create a testing record and put it in the map.
12051246
fee := btcutil.Amount(1000)
1206-
requestID := tp.storeRecord(tx, req, m.feeFunc, fee)
1247+
requestID := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
12071248
record, ok := tp.records.Load(requestID)
12081249
require.True(t, ok)
12091250

@@ -1273,9 +1314,17 @@ func TestHandleFeeBumpTx(t *testing.T) {
12731314
tx: tx,
12741315
}
12751316

1317+
op := wire.OutPoint{
1318+
Hash: chainhash.Hash{1},
1319+
Index: 0,
1320+
}
1321+
utxoIndex := map[wire.OutPoint]int{
1322+
op: 0,
1323+
}
1324+
12761325
// Create a testing record and put it in the map.
12771326
fee := btcutil.Amount(1000)
1278-
requestID := tp.storeRecord(tx, req, m.feeFunc, fee)
1327+
requestID := tp.storeRecord(tx, req, m.feeFunc, fee, utxoIndex)
12791328

12801329
// Create a subscription to the event.
12811330
subscriber := make(chan *BumpResult, 1)

0 commit comments

Comments
 (0)