Skip to content

Commit bde5124

Browse files
committed
sweep: shorten storeRecord method signature
This commit shortens the function signature of `storeRecord`, also makes sure we don't call `t.records.Store` directly but always using `storeRecord` instead so it's easier to trace the record creation.
1 parent c68b8e8 commit bde5124

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

sweep/fee_bumper.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ func (t *TxPublisher) storeInitialRecord(req *BumpRequest) (
440440
return requestID, record
441441
}
442442

443+
// storeRecord stores the given record in the records map.
444+
func (t *TxPublisher) storeRecord(requestID uint64, sweepCtx *sweepTxCtx,
445+
req *BumpRequest, f FeeFunction) {
446+
447+
// Register the record.
448+
t.records.Store(requestID, &monitorRecord{
449+
tx: sweepCtx.tx,
450+
req: req,
451+
feeFunction: f,
452+
fee: sweepCtx.fee,
453+
outpointToTxIndex: sweepCtx.outpointToTxIndex,
454+
})
455+
}
456+
443457
// NOTE: part of the `chainio.Consumer` interface.
444458
func (t *TxPublisher) Name() string {
445459
return "TxPublisher"
@@ -508,10 +522,7 @@ func (t *TxPublisher) createRBFCompliantTx(requestID uint64, req *BumpRequest,
508522
switch {
509523
case err == nil:
510524
// The tx is valid, store it.
511-
t.storeRecord(
512-
requestID, sweepCtx.tx, req, f, sweepCtx.fee,
513-
sweepCtx.outpointToTxIndex,
514-
)
525+
t.storeRecord(requestID, sweepCtx, req, f)
515526

516527
log.Infof("Created initial sweep tx=%v for %v inputs: "+
517528
"feerate=%v, fee=%v, inputs:\n%v",
@@ -565,21 +576,6 @@ func (t *TxPublisher) createRBFCompliantTx(requestID uint64, req *BumpRequest,
565576
}
566577
}
567578

568-
// storeRecord stores the given record in the records map.
569-
func (t *TxPublisher) storeRecord(requestID uint64, tx *wire.MsgTx,
570-
req *BumpRequest, f FeeFunction, fee btcutil.Amount,
571-
outpointToTxIndex map[wire.OutPoint]int) {
572-
573-
// Register the record.
574-
t.records.Store(requestID, &monitorRecord{
575-
tx: tx,
576-
req: req,
577-
feeFunction: f,
578-
fee: fee,
579-
outpointToTxIndex: outpointToTxIndex,
580-
})
581-
}
582-
583579
// createAndCheckTx creates a tx based on the given inputs, change output
584580
// script, and the fee rate. In addition, it validates the tx's mempool
585581
// acceptance before returning a tx that can be published directly, along with
@@ -1195,13 +1191,7 @@ func (t *TxPublisher) createAndPublishTx(requestID uint64,
11951191

11961192
// The tx has been created without any errors, we now register a new
11971193
// record by overwriting the same requestID.
1198-
t.records.Store(requestID, &monitorRecord{
1199-
tx: sweepCtx.tx,
1200-
req: r.req,
1201-
feeFunction: r.feeFunction,
1202-
fee: sweepCtx.fee,
1203-
outpointToTxIndex: sweepCtx.outpointToTxIndex,
1204-
})
1194+
t.storeRecord(requestID, sweepCtx, r.req, r.feeFunction)
12051195

12061196
// Attempt to broadcast this new tx.
12071197
result, err := t.broadcast(requestID)

sweep/fee_bumper_test.go

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,15 @@ func TestStoreRecord(t *testing.T) {
351351
op: 0,
352352
}
353353

354+
// Create a sweepTxCtx.
355+
sweepCtx := &sweepTxCtx{
356+
tx: tx,
357+
fee: fee,
358+
outpointToTxIndex: utxoIndex,
359+
}
360+
354361
// Call the method under test.
355-
tp.storeRecord(initialCounter, tx, req, feeFunc, fee, utxoIndex)
362+
tp.storeRecord(initialCounter, sweepCtx, req, feeFunc)
356363

357364
// Read the saved record and compare.
358365
record, ok := tp.records.Load(initialCounter)
@@ -698,7 +705,15 @@ func TestTxPublisherBroadcast(t *testing.T) {
698705
// Create a testing record and put it in the map.
699706
fee := btcutil.Amount(1000)
700707
requestID := uint64(1)
701-
tp.storeRecord(requestID, tx, req, m.feeFunc, fee, utxoIndex)
708+
709+
// Create a sweepTxCtx.
710+
sweepCtx := &sweepTxCtx{
711+
tx: tx,
712+
fee: fee,
713+
outpointToTxIndex: utxoIndex,
714+
}
715+
716+
tp.storeRecord(requestID, sweepCtx, req, m.feeFunc)
702717

703718
// Quickly check when the requestID cannot be found, an error is
704719
// returned.
@@ -796,6 +811,13 @@ func TestRemoveResult(t *testing.T) {
796811
// Create a test request ID counter.
797812
requestCounter := atomic.Uint64{}
798813

814+
// Create a sweepTxCtx.
815+
sweepCtx := &sweepTxCtx{
816+
tx: tx,
817+
fee: fee,
818+
outpointToTxIndex: utxoIndex,
819+
}
820+
799821
testCases := []struct {
800822
name string
801823
setupRecord func() uint64
@@ -808,9 +830,7 @@ func TestRemoveResult(t *testing.T) {
808830
name: "remove on TxConfirmed",
809831
setupRecord: func() uint64 {
810832
rid := requestCounter.Add(1)
811-
tp.storeRecord(
812-
rid, tx, req, m.feeFunc, fee, utxoIndex,
813-
)
833+
tp.storeRecord(rid, sweepCtx, req, m.feeFunc)
814834
tp.subscriberChans.Store(rid, nil)
815835

816836
return rid
@@ -826,9 +846,7 @@ func TestRemoveResult(t *testing.T) {
826846
name: "remove on TxFailed",
827847
setupRecord: func() uint64 {
828848
rid := requestCounter.Add(1)
829-
tp.storeRecord(
830-
rid, tx, req, m.feeFunc, fee, utxoIndex,
831-
)
849+
tp.storeRecord(rid, sweepCtx, req, m.feeFunc)
832850
tp.subscriberChans.Store(rid, nil)
833851

834852
return rid
@@ -845,9 +863,7 @@ func TestRemoveResult(t *testing.T) {
845863
name: "noop when tx is not confirmed or failed",
846864
setupRecord: func() uint64 {
847865
rid := requestCounter.Add(1)
848-
tp.storeRecord(
849-
rid, tx, req, m.feeFunc, fee, utxoIndex,
850-
)
866+
tp.storeRecord(rid, sweepCtx, req, m.feeFunc)
851867
tp.subscriberChans.Store(rid, nil)
852868

853869
return rid
@@ -906,7 +922,15 @@ func TestNotifyResult(t *testing.T) {
906922
// Create a testing record and put it in the map.
907923
fee := btcutil.Amount(1000)
908924
requestID := uint64(1)
909-
tp.storeRecord(requestID, tx, req, m.feeFunc, fee, utxoIndex)
925+
926+
// Create a sweepTxCtx.
927+
sweepCtx := &sweepTxCtx{
928+
tx: tx,
929+
fee: fee,
930+
outpointToTxIndex: utxoIndex,
931+
}
932+
933+
tp.storeRecord(requestID, sweepCtx, req, m.feeFunc)
910934

911935
// Create a subscription to the event.
912936
subscriber := make(chan *BumpResult, 1)
@@ -1208,7 +1232,15 @@ func TestHandleTxConfirmed(t *testing.T) {
12081232
// Create a testing record and put it in the map.
12091233
fee := btcutil.Amount(1000)
12101234
requestID := uint64(1)
1211-
tp.storeRecord(requestID, tx, req, m.feeFunc, fee, utxoIndex)
1235+
1236+
// Create a sweepTxCtx.
1237+
sweepCtx := &sweepTxCtx{
1238+
tx: tx,
1239+
fee: fee,
1240+
outpointToTxIndex: utxoIndex,
1241+
}
1242+
1243+
tp.storeRecord(requestID, sweepCtx, req, m.feeFunc)
12121244
record, ok := tp.records.Load(requestID)
12131245
require.True(t, ok)
12141246

@@ -1289,7 +1321,15 @@ func TestHandleFeeBumpTx(t *testing.T) {
12891321
// Create a testing record and put it in the map.
12901322
fee := btcutil.Amount(1000)
12911323
requestID := uint64(1)
1292-
tp.storeRecord(requestID, tx, req, m.feeFunc, fee, utxoIndex)
1324+
1325+
// Create a sweepTxCtx.
1326+
sweepCtx := &sweepTxCtx{
1327+
tx: tx,
1328+
fee: fee,
1329+
outpointToTxIndex: utxoIndex,
1330+
}
1331+
1332+
tp.storeRecord(requestID, sweepCtx, req, m.feeFunc)
12931333

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

0 commit comments

Comments
 (0)