mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-05-11 20:30:04 +02:00
sweep: apply fn.NewSet
and fn.Map
in validateInputs
This commit is contained in:
parent
4c13ea1747
commit
54ade99ce2
@ -657,19 +657,19 @@ func TestBudgetAggregatorCreateInputSets(t *testing.T) {
|
|||||||
pi1 := SweeperInput{
|
pi1 := SweeperInput{
|
||||||
Input: mockInput1,
|
Input: mockInput1,
|
||||||
params: Params{
|
params: Params{
|
||||||
DeadlineHeight: fn.Some(int32(1)),
|
DeadlineHeight: fn.Some(testHeight),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pi2 := SweeperInput{
|
pi2 := SweeperInput{
|
||||||
Input: mockInput2,
|
Input: mockInput2,
|
||||||
params: Params{
|
params: Params{
|
||||||
DeadlineHeight: fn.Some(int32(1)),
|
DeadlineHeight: fn.Some(testHeight),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pi3 := SweeperInput{
|
pi3 := SweeperInput{
|
||||||
Input: mockInput3,
|
Input: mockInput3,
|
||||||
params: Params{
|
params: Params{
|
||||||
DeadlineHeight: fn.Some(int32(1)),
|
DeadlineHeight: fn.Some(testHeight),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pi4 := SweeperInput{
|
pi4 := SweeperInput{
|
||||||
@ -678,7 +678,7 @@ func TestBudgetAggregatorCreateInputSets(t *testing.T) {
|
|||||||
// This input has a deadline height that is different
|
// This input has a deadline height that is different
|
||||||
// from the other inputs. When grouped with other
|
// from the other inputs. When grouped with other
|
||||||
// inputs, it will cause an error to be returned.
|
// inputs, it will cause an error to be returned.
|
||||||
DeadlineHeight: fn.Some(int32(2)),
|
DeadlineHeight: fn.Some(testHeight + 1),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -561,30 +561,43 @@ var _ InputSet = (*BudgetInputSet)(nil)
|
|||||||
|
|
||||||
// validateInputs is used when creating new BudgetInputSet to ensure there are
|
// validateInputs is used when creating new BudgetInputSet to ensure there are
|
||||||
// no duplicate inputs and they all share the same deadline heights, if set.
|
// no duplicate inputs and they all share the same deadline heights, if set.
|
||||||
func validateInputs(inputs []SweeperInput) error {
|
func validateInputs(inputs []SweeperInput, deadlineHeight int32) error {
|
||||||
// Sanity check the input slice to ensure it's non-empty.
|
// Sanity check the input slice to ensure it's non-empty.
|
||||||
if len(inputs) == 0 {
|
if len(inputs) == 0 {
|
||||||
return fmt.Errorf("inputs slice is empty")
|
return fmt.Errorf("inputs slice is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
// dedupInputs is a map used to track unique outpoints of the inputs.
|
// inputDeadline tracks the input's deadline height. It will be updated
|
||||||
dedupInputs := make(map[wire.OutPoint]struct{})
|
// if the input has a different deadline than the specified
|
||||||
|
// deadlineHeight.
|
||||||
|
inputDeadline := deadlineHeight
|
||||||
|
|
||||||
// deadlineSet stores unique deadline heights.
|
// dedupInputs is a set used to track unique outpoints of the inputs.
|
||||||
deadlineSet := make(map[fn.Option[int32]]struct{})
|
dedupInputs := fn.NewSet(
|
||||||
|
// Iterate all the inputs and map the function.
|
||||||
|
fn.Map(func(inp SweeperInput) wire.OutPoint {
|
||||||
|
// If the input has a deadline height, we'll check if
|
||||||
|
// it's the same as the specified.
|
||||||
|
inp.params.DeadlineHeight.WhenSome(func(h int32) {
|
||||||
|
// Exit early if the deadlines matched.
|
||||||
|
if h == deadlineHeight {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, input := range inputs {
|
// Update the deadline height if it's
|
||||||
input.params.DeadlineHeight.WhenSome(func(h int32) {
|
// different.
|
||||||
deadlineSet[input.params.DeadlineHeight] = struct{}{}
|
inputDeadline = h
|
||||||
})
|
})
|
||||||
|
|
||||||
dedupInputs[input.OutPoint()] = struct{}{}
|
return inp.OutPoint()
|
||||||
}
|
}, inputs)...,
|
||||||
|
)
|
||||||
|
|
||||||
// Make sure the inputs share the same deadline height when there is
|
// Make sure the inputs share the same deadline height when there is
|
||||||
// one.
|
// one.
|
||||||
if len(deadlineSet) > 1 {
|
if inputDeadline != deadlineHeight {
|
||||||
return fmt.Errorf("inputs have different deadline heights")
|
return fmt.Errorf("input deadline height not matched: want "+
|
||||||
|
"%d, got %d", deadlineHeight, inputDeadline)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide a defensive check to ensure that we don't have any duplicate
|
// Provide a defensive check to ensure that we don't have any duplicate
|
||||||
@ -601,7 +614,7 @@ func NewBudgetInputSet(inputs []SweeperInput,
|
|||||||
deadlineHeight int32) (*BudgetInputSet, error) {
|
deadlineHeight int32) (*BudgetInputSet, error) {
|
||||||
|
|
||||||
// Validate the supplied inputs.
|
// Validate the supplied inputs.
|
||||||
if err := validateInputs(inputs); err != nil {
|
if err := validateInputs(inputs, deadlineHeight); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,21 +282,34 @@ func TestNewBudgetInputSet(t *testing.T) {
|
|||||||
DeadlineHeight: fn.Some(int32(2)),
|
DeadlineHeight: fn.Some(int32(2)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
input3 := SweeperInput{
|
||||||
|
Input: inp2,
|
||||||
|
params: Params{
|
||||||
|
Budget: 100,
|
||||||
|
DeadlineHeight: fn.Some(testHeight),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Pass a slice of inputs with different deadline heights.
|
// Pass a slice of inputs with different deadline heights.
|
||||||
set, err = NewBudgetInputSet([]SweeperInput{input1, input2}, testHeight)
|
set, err = NewBudgetInputSet([]SweeperInput{input1, input2}, testHeight)
|
||||||
rt.ErrorContains(err, "inputs have different deadline heights")
|
rt.ErrorContains(err, "input deadline height not matched")
|
||||||
rt.Nil(set)
|
rt.Nil(set)
|
||||||
|
|
||||||
// Pass a slice of inputs that only one input has the deadline height.
|
// Pass a slice of inputs that only one input has the deadline height,
|
||||||
|
// but it has a different value than the specified testHeight.
|
||||||
set, err = NewBudgetInputSet([]SweeperInput{input0, input2}, testHeight)
|
set, err = NewBudgetInputSet([]SweeperInput{input0, input2}, testHeight)
|
||||||
rt.NoError(err)
|
rt.ErrorContains(err, "input deadline height not matched")
|
||||||
rt.NotNil(set)
|
rt.Nil(set)
|
||||||
|
|
||||||
// Pass a slice of inputs that are duplicates.
|
// Pass a slice of inputs that are duplicates.
|
||||||
set, err = NewBudgetInputSet([]SweeperInput{input1, input1}, testHeight)
|
set, err = NewBudgetInputSet([]SweeperInput{input3, input3}, testHeight)
|
||||||
rt.ErrorContains(err, "duplicate inputs")
|
rt.ErrorContains(err, "duplicate inputs")
|
||||||
rt.Nil(set)
|
rt.Nil(set)
|
||||||
|
|
||||||
|
// Pass a slice of inputs that only one input has the deadline height,
|
||||||
|
set, err = NewBudgetInputSet([]SweeperInput{input0, input3}, testHeight)
|
||||||
|
rt.NoError(err)
|
||||||
|
rt.NotNil(set)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestBudgetInputSetAddInput checks that `addInput` correctly updates the
|
// TestBudgetInputSetAddInput checks that `addInput` correctly updates the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user