Skip to content

Commit f053533

Browse files
committed
sweep: add method isMature on SweeperInput
Also updated `handlePendingSweepsReq` to skip immature inputs so the returned results are the same as those in pre-0.18.0.
1 parent b20f32f commit f053533

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

sweep/sweeper.go

+33-14
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,30 @@ func (p *SweeperInput) terminated() bool {
221221
}
222222
}
223223

224+
// isMature returns a boolean indicating whether the input has a timelock that
225+
// has been reached or not. The locktime found is also returned.
226+
func (p *SweeperInput) isMature(currentHeight uint32) (bool, uint32) {
227+
locktime, _ := p.RequiredLockTime()
228+
if currentHeight < locktime {
229+
log.Debugf("Input %v has locktime=%v, current height is %v",
230+
p.OutPoint(), locktime, currentHeight)
231+
232+
return false, locktime
233+
}
234+
235+
// If the input has a CSV that's not yet reached, we will skip
236+
// this input and wait for the expiry.
237+
locktime = p.BlocksToMaturity() + p.HeightHint()
238+
if currentHeight+1 < locktime {
239+
log.Debugf("Input %v has CSV expiry=%v, current height is %v",
240+
p.OutPoint(), locktime, currentHeight)
241+
242+
return false, locktime
243+
}
244+
245+
return true, locktime
246+
}
247+
224248
// InputsMap is a type alias for a set of pending inputs.
225249
type InputsMap = map[wire.OutPoint]*SweeperInput
226250

@@ -1027,6 +1051,12 @@ func (s *UtxoSweeper) handlePendingSweepsReq(
10271051

10281052
resps := make(map[wire.OutPoint]*PendingInputResponse, len(s.inputs))
10291053
for _, inp := range s.inputs {
1054+
// Skip immature inputs for compatibility.
1055+
mature, _ := inp.isMature(uint32(s.currentHeight))
1056+
if !mature {
1057+
continue
1058+
}
1059+
10301060
// Only the exported fields are set, as we expect the response
10311061
// to only be consumed externally.
10321062
op := inp.OutPoint()
@@ -1477,20 +1507,9 @@ func (s *UtxoSweeper) updateSweeperInputs() InputsMap {
14771507

14781508
// If the input has a locktime that's not yet reached, we will
14791509
// skip this input and wait for the locktime to be reached.
1480-
locktime, _ := input.RequiredLockTime()
1481-
if uint32(s.currentHeight) < locktime {
1482-
log.Warnf("Skipping input %v due to locktime=%v not "+
1483-
"reached, current height is %v", op, locktime,
1484-
s.currentHeight)
1485-
1486-
continue
1487-
}
1488-
1489-
// If the input has a CSV that's not yet reached, we will skip
1490-
// this input and wait for the expiry.
1491-
locktime = input.BlocksToMaturity() + input.HeightHint()
1492-
if s.currentHeight < int32(locktime)-1 {
1493-
log.Infof("Skipping input %v due to CSV expiry=%v not "+
1510+
mature, locktime := input.isMature(uint32(s.currentHeight))
1511+
if !mature {
1512+
log.Infof("Skipping input %v due to locktime=%v not "+
14941513
"reached, current height is %v", op, locktime,
14951514
s.currentHeight)
14961515

sweep/sweeper_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ func TestUpdateSweeperInputs(t *testing.T) {
490490
// returned.
491491
inp2.On("RequiredLockTime").Return(
492492
uint32(s.currentHeight+1), true).Once()
493+
inp2.On("OutPoint").Return(wire.OutPoint{Index: 2}).Maybe()
493494
input7 := &SweeperInput{state: Init, Input: inp2}
494495

495496
// Mock the input to have a CSV expiry in the future so it will NOT be
@@ -498,6 +499,7 @@ func TestUpdateSweeperInputs(t *testing.T) {
498499
uint32(s.currentHeight), false).Once()
499500
inp3.On("BlocksToMaturity").Return(uint32(2)).Once()
500501
inp3.On("HeightHint").Return(uint32(s.currentHeight)).Once()
502+
inp3.On("OutPoint").Return(wire.OutPoint{Index: 3}).Maybe()
501503
input8 := &SweeperInput{state: Init, Input: inp3}
502504

503505
// Add the inputs to the sweeper. After the update, we should see the

0 commit comments

Comments
 (0)