sweep: add new interface method Immediate

This prepares the following commit where we now let the fee bumpr
decides whether to broadcast immediately or not.
This commit is contained in:
yyforyongyu 2024-10-25 15:32:30 +08:00
parent 5f64280df4
commit d0c7fd8aac
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
5 changed files with 36 additions and 0 deletions

View file

@ -143,6 +143,10 @@ type BumpRequest struct {
// ExtraTxOut tracks if this bump request has an optional set of extra // ExtraTxOut tracks if this bump request has an optional set of extra
// outputs to add to the transaction. // outputs to add to the transaction.
ExtraTxOut fn.Option[SweepOutput] ExtraTxOut fn.Option[SweepOutput]
// Immediate is used to specify that the tx should be broadcast
// immediately.
Immediate bool
} }
// MaxFeeRateAllowed returns the maximum fee rate allowed for the given // MaxFeeRateAllowed returns the maximum fee rate allowed for the given

View file

@ -268,6 +268,13 @@ func (m *MockInputSet) StartingFeeRate() fn.Option[chainfee.SatPerKWeight] {
return args.Get(0).(fn.Option[chainfee.SatPerKWeight]) return args.Get(0).(fn.Option[chainfee.SatPerKWeight])
} }
// Immediate returns whether the inputs should be swept immediately.
func (m *MockInputSet) Immediate() bool {
args := m.Called()
return args.Bool(0)
}
// MockBumper is a mock implementation of the interface Bumper. // MockBumper is a mock implementation of the interface Bumper.
type MockBumper struct { type MockBumper struct {
mock.Mock mock.Mock

View file

@ -827,6 +827,7 @@ func (s *UtxoSweeper) sweep(set InputSet) error {
DeliveryAddress: sweepAddr, DeliveryAddress: sweepAddr,
MaxFeeRate: s.cfg.MaxFeeRate.FeePerKWeight(), MaxFeeRate: s.cfg.MaxFeeRate.FeePerKWeight(),
StartingFeeRate: set.StartingFeeRate(), StartingFeeRate: set.StartingFeeRate(),
Immediate: set.Immediate(),
// TODO(yy): pass the strategy here. // TODO(yy): pass the strategy here.
} }

View file

@ -704,11 +704,13 @@ func TestSweepPendingInputs(t *testing.T) {
setNeedWallet.On("Budget").Return(btcutil.Amount(1)).Once() setNeedWallet.On("Budget").Return(btcutil.Amount(1)).Once()
setNeedWallet.On("StartingFeeRate").Return( setNeedWallet.On("StartingFeeRate").Return(
fn.None[chainfee.SatPerKWeight]()).Once() fn.None[chainfee.SatPerKWeight]()).Once()
setNeedWallet.On("Immediate").Return(false).Once()
normalSet.On("Inputs").Return(nil).Maybe() normalSet.On("Inputs").Return(nil).Maybe()
normalSet.On("DeadlineHeight").Return(testHeight).Once() normalSet.On("DeadlineHeight").Return(testHeight).Once()
normalSet.On("Budget").Return(btcutil.Amount(1)).Once() normalSet.On("Budget").Return(btcutil.Amount(1)).Once()
normalSet.On("StartingFeeRate").Return( normalSet.On("StartingFeeRate").Return(
fn.None[chainfee.SatPerKWeight]()).Once() fn.None[chainfee.SatPerKWeight]()).Once()
normalSet.On("Immediate").Return(false).Once()
// Make pending inputs for testing. We don't need real values here as // Make pending inputs for testing. We don't need real values here as
// the returned clusters are mocked. // the returned clusters are mocked.

View file

@ -64,6 +64,13 @@ type InputSet interface {
// StartingFeeRate returns the max starting fee rate found in the // StartingFeeRate returns the max starting fee rate found in the
// inputs. // inputs.
StartingFeeRate() fn.Option[chainfee.SatPerKWeight] StartingFeeRate() fn.Option[chainfee.SatPerKWeight]
// Immediate returns a boolean to indicate whether the tx made from
// this input set should be published immediately.
//
// TODO(yy): create a new method `Params` to combine the informational
// methods DeadlineHeight, Budget, StartingFeeRate and Immediate.
Immediate() bool
} }
// createWalletTxInput converts a wallet utxo into an object that can be added // createWalletTxInput converts a wallet utxo into an object that can be added
@ -414,3 +421,18 @@ func (b *BudgetInputSet) StartingFeeRate() fn.Option[chainfee.SatPerKWeight] {
return startingFeeRate return startingFeeRate
} }
// Immediate returns whether the inputs should be swept immediately.
//
// NOTE: part of the InputSet interface.
func (b *BudgetInputSet) Immediate() bool {
for _, inp := range b.inputs {
// As long as one of the inputs is immediate, the whole set is
// immediate.
if inp.params.Immediate {
return true
}
}
return false
}