mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-29 03:01:52 +01:00
contractcourt: make sure sweep happens immediately on startup
This commit makes sure the time-sensitive outputs are swept immediately during startup.
This commit is contained in:
parent
7abefa7760
commit
49cfb91af1
@ -84,7 +84,7 @@ func (c *anchorResolver) ResolverKey() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Resolve offers the anchor output to the sweeper and waits for it to be swept.
|
// Resolve offers the anchor output to the sweeper and waits for it to be swept.
|
||||||
func (c *anchorResolver) Resolve() (ContractResolver, error) {
|
func (c *anchorResolver) Resolve(_ bool) (ContractResolver, error) {
|
||||||
// Attempt to update the sweep parameters to the post-confirmation
|
// Attempt to update the sweep parameters to the post-confirmation
|
||||||
// situation. We don't want to force sweep anymore, because the anchor
|
// situation. We don't want to force sweep anymore, because the anchor
|
||||||
// lost its special purpose to get the commitment confirmed. It is just
|
// lost its special purpose to get the commitment confirmed. It is just
|
||||||
|
@ -45,7 +45,9 @@ func (b *breachResolver) ResolverKey() []byte {
|
|||||||
|
|
||||||
// Resolve queries the BreachArbitrator to see if the justice transaction has
|
// Resolve queries the BreachArbitrator to see if the justice transaction has
|
||||||
// been broadcast.
|
// been broadcast.
|
||||||
func (b *breachResolver) Resolve() (ContractResolver, error) {
|
//
|
||||||
|
// TODO(yy): let sweeper handle the breach inputs.
|
||||||
|
func (b *breachResolver) Resolve(_ bool) (ContractResolver, error) {
|
||||||
if !b.subscribed {
|
if !b.subscribed {
|
||||||
complete, err := b.SubscribeBreachComplete(
|
complete, err := b.SubscribeBreachComplete(
|
||||||
&b.ChanPoint, b.replyChan,
|
&b.ChanPoint, b.replyChan,
|
||||||
|
@ -787,7 +787,7 @@ func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet,
|
|||||||
// TODO(roasbeef): this isn't re-launched?
|
// TODO(roasbeef): this isn't re-launched?
|
||||||
}
|
}
|
||||||
|
|
||||||
c.launchResolvers(unresolvedContracts)
|
c.launchResolvers(unresolvedContracts, true)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -1245,7 +1245,7 @@ func (c *ChannelArbitrator) stateStep(
|
|||||||
|
|
||||||
// Finally, we'll launch all the required contract resolvers.
|
// Finally, we'll launch all the required contract resolvers.
|
||||||
// Once they're all resolved, we're no longer needed.
|
// Once they're all resolved, we're no longer needed.
|
||||||
c.launchResolvers(resolvers)
|
c.launchResolvers(resolvers, false)
|
||||||
|
|
||||||
nextState = StateWaitingFullResolution
|
nextState = StateWaitingFullResolution
|
||||||
|
|
||||||
@ -1553,14 +1553,16 @@ func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// launchResolvers updates the activeResolvers list and starts the resolvers.
|
// launchResolvers updates the activeResolvers list and starts the resolvers.
|
||||||
func (c *ChannelArbitrator) launchResolvers(resolvers []ContractResolver) {
|
func (c *ChannelArbitrator) launchResolvers(resolvers []ContractResolver,
|
||||||
|
immediate bool) {
|
||||||
|
|
||||||
c.activeResolversLock.Lock()
|
c.activeResolversLock.Lock()
|
||||||
defer c.activeResolversLock.Unlock()
|
defer c.activeResolversLock.Unlock()
|
||||||
|
|
||||||
c.activeResolvers = resolvers
|
c.activeResolvers = resolvers
|
||||||
for _, contract := range resolvers {
|
for _, contract := range resolvers {
|
||||||
c.wg.Add(1)
|
c.wg.Add(1)
|
||||||
go c.resolveContract(contract)
|
go c.resolveContract(contract, immediate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2573,7 +2575,9 @@ func (c *ChannelArbitrator) replaceResolver(oldResolver,
|
|||||||
// contracts.
|
// contracts.
|
||||||
//
|
//
|
||||||
// NOTE: This MUST be run as a goroutine.
|
// NOTE: This MUST be run as a goroutine.
|
||||||
func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver) {
|
func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver,
|
||||||
|
immediate bool) {
|
||||||
|
|
||||||
defer c.wg.Done()
|
defer c.wg.Done()
|
||||||
|
|
||||||
log.Debugf("ChannelArbitrator(%v): attempting to resolve %T",
|
log.Debugf("ChannelArbitrator(%v): attempting to resolve %T",
|
||||||
@ -2594,7 +2598,7 @@ func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver) {
|
|||||||
default:
|
default:
|
||||||
// Otherwise, we'll attempt to resolve the current
|
// Otherwise, we'll attempt to resolve the current
|
||||||
// contract.
|
// contract.
|
||||||
nextContract, err := currentContract.Resolve()
|
nextContract, err := currentContract.Resolve(immediate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errResolverShuttingDown {
|
if err == errResolverShuttingDown {
|
||||||
return
|
return
|
||||||
|
@ -184,7 +184,9 @@ func (c *commitSweepResolver) getCommitTxConfHeight() (uint32, error) {
|
|||||||
// returned.
|
// returned.
|
||||||
//
|
//
|
||||||
// NOTE: This function MUST be run as a goroutine.
|
// NOTE: This function MUST be run as a goroutine.
|
||||||
func (c *commitSweepResolver) Resolve() (ContractResolver, error) {
|
//
|
||||||
|
//nolint:funlen
|
||||||
|
func (c *commitSweepResolver) Resolve(_ bool) (ContractResolver, error) {
|
||||||
// If we're already resolved, then we can exit early.
|
// If we're already resolved, then we can exit early.
|
||||||
if c.resolved {
|
if c.resolved {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -83,7 +83,7 @@ func (i *commitSweepResolverTestContext) resolve() {
|
|||||||
// Start resolver.
|
// Start resolver.
|
||||||
i.resolverResultChan = make(chan resolveResult, 1)
|
i.resolverResultChan = make(chan resolveResult, 1)
|
||||||
go func() {
|
go func() {
|
||||||
nextResolver, err := i.resolver.Resolve()
|
nextResolver, err := i.resolver.Resolve(false)
|
||||||
i.resolverResultChan <- resolveResult{
|
i.resolverResultChan <- resolveResult{
|
||||||
nextResolver: nextResolver,
|
nextResolver: nextResolver,
|
||||||
err: err,
|
err: err,
|
||||||
|
@ -43,7 +43,7 @@ type ContractResolver interface {
|
|||||||
// resolution, then another resolve is returned.
|
// resolution, then another resolve is returned.
|
||||||
//
|
//
|
||||||
// NOTE: This function MUST be run as a goroutine.
|
// NOTE: This function MUST be run as a goroutine.
|
||||||
Resolve() (ContractResolver, error)
|
Resolve(immediate bool) (ContractResolver, error)
|
||||||
|
|
||||||
// SupplementState allows the user of a ContractResolver to supplement
|
// SupplementState allows the user of a ContractResolver to supplement
|
||||||
// it with state required for the proper resolution of a contract.
|
// it with state required for the proper resolution of a contract.
|
||||||
|
@ -90,7 +90,9 @@ func (h *htlcIncomingContestResolver) processFinalHtlcFail() error {
|
|||||||
// as we have no remaining actions left at our disposal.
|
// as we have no remaining actions left at our disposal.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the ContractResolver interface.
|
// NOTE: Part of the ContractResolver interface.
|
||||||
func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
|
func (h *htlcIncomingContestResolver) Resolve(
|
||||||
|
_ bool) (ContractResolver, error) {
|
||||||
|
|
||||||
// If we're already full resolved, then we don't have anything further
|
// If we're already full resolved, then we don't have anything further
|
||||||
// to do.
|
// to do.
|
||||||
if h.resolved {
|
if h.resolved {
|
||||||
|
@ -396,7 +396,7 @@ func (i *incomingResolverTestContext) resolve() {
|
|||||||
i.resolveErr = make(chan error, 1)
|
i.resolveErr = make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
var err error
|
var err error
|
||||||
i.nextResolver, err = i.resolver.Resolve()
|
i.nextResolver, err = i.resolver.Resolve(false)
|
||||||
i.resolveErr <- err
|
i.resolveErr <- err
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -49,7 +49,9 @@ func newOutgoingContestResolver(res lnwallet.OutgoingHtlcResolution,
|
|||||||
// When either of these two things happens, we'll create a new resolver which
|
// When either of these two things happens, we'll create a new resolver which
|
||||||
// is able to handle the final resolution of the contract. We're only the pivot
|
// is able to handle the final resolution of the contract. We're only the pivot
|
||||||
// point.
|
// point.
|
||||||
func (h *htlcOutgoingContestResolver) Resolve() (ContractResolver, error) {
|
func (h *htlcOutgoingContestResolver) Resolve(
|
||||||
|
_ bool) (ContractResolver, error) {
|
||||||
|
|
||||||
// If we're already full resolved, then we don't have anything further
|
// If we're already full resolved, then we don't have anything further
|
||||||
// to do.
|
// to do.
|
||||||
if h.resolved {
|
if h.resolved {
|
||||||
|
@ -209,7 +209,7 @@ func (i *outgoingResolverTestContext) resolve() {
|
|||||||
// Start resolver.
|
// Start resolver.
|
||||||
i.resolverResultChan = make(chan resolveResult, 1)
|
i.resolverResultChan = make(chan resolveResult, 1)
|
||||||
go func() {
|
go func() {
|
||||||
nextResolver, err := i.resolver.Resolve()
|
nextResolver, err := i.resolver.Resolve(false)
|
||||||
i.resolverResultChan <- resolveResult{
|
i.resolverResultChan <- resolveResult{
|
||||||
nextResolver: nextResolver,
|
nextResolver: nextResolver,
|
||||||
err: err,
|
err: err,
|
||||||
|
@ -115,7 +115,9 @@ func (h *htlcSuccessResolver) ResolverKey() []byte {
|
|||||||
// TODO(roasbeef): create multi to batch
|
// TODO(roasbeef): create multi to batch
|
||||||
//
|
//
|
||||||
// NOTE: Part of the ContractResolver interface.
|
// NOTE: Part of the ContractResolver interface.
|
||||||
func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) {
|
func (h *htlcSuccessResolver) Resolve(
|
||||||
|
immediate bool) (ContractResolver, error) {
|
||||||
|
|
||||||
// If we're already resolved, then we can exit early.
|
// If we're already resolved, then we can exit early.
|
||||||
if h.resolved {
|
if h.resolved {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -124,12 +126,12 @@ func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) {
|
|||||||
// If we don't have a success transaction, then this means that this is
|
// If we don't have a success transaction, then this means that this is
|
||||||
// an output on the remote party's commitment transaction.
|
// an output on the remote party's commitment transaction.
|
||||||
if h.htlcResolution.SignedSuccessTx == nil {
|
if h.htlcResolution.SignedSuccessTx == nil {
|
||||||
return h.resolveRemoteCommitOutput()
|
return h.resolveRemoteCommitOutput(immediate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise this an output on our own commitment, and we must start by
|
// Otherwise this an output on our own commitment, and we must start by
|
||||||
// broadcasting the second-level success transaction.
|
// broadcasting the second-level success transaction.
|
||||||
secondLevelOutpoint, err := h.broadcastSuccessTx()
|
secondLevelOutpoint, err := h.broadcastSuccessTx(immediate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -163,7 +165,9 @@ func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) {
|
|||||||
// broadcasting the second-level success transaction. It returns the ultimate
|
// broadcasting the second-level success transaction. It returns the ultimate
|
||||||
// outpoint of the second-level tx, that we must wait to be spent for the
|
// outpoint of the second-level tx, that we must wait to be spent for the
|
||||||
// resolver to be fully resolved.
|
// resolver to be fully resolved.
|
||||||
func (h *htlcSuccessResolver) broadcastSuccessTx() (*wire.OutPoint, error) {
|
func (h *htlcSuccessResolver) broadcastSuccessTx(
|
||||||
|
immediate bool) (*wire.OutPoint, error) {
|
||||||
|
|
||||||
// If we have non-nil SignDetails, this means that have a 2nd level
|
// If we have non-nil SignDetails, this means that have a 2nd level
|
||||||
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
|
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
|
||||||
// (the case for anchor type channels). In this case we can re-sign it
|
// (the case for anchor type channels). In this case we can re-sign it
|
||||||
@ -171,7 +175,7 @@ func (h *htlcSuccessResolver) broadcastSuccessTx() (*wire.OutPoint, error) {
|
|||||||
// the checkpointed outputIncubating field to determine if we already
|
// the checkpointed outputIncubating field to determine if we already
|
||||||
// swept the HTLC output into the second level transaction.
|
// swept the HTLC output into the second level transaction.
|
||||||
if h.htlcResolution.SignDetails != nil {
|
if h.htlcResolution.SignDetails != nil {
|
||||||
return h.broadcastReSignedSuccessTx()
|
return h.broadcastReSignedSuccessTx(immediate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise we'll publish the second-level transaction directly and
|
// Otherwise we'll publish the second-level transaction directly and
|
||||||
@ -221,7 +225,9 @@ func (h *htlcSuccessResolver) broadcastSuccessTx() (*wire.OutPoint, error) {
|
|||||||
// broadcastReSignedSuccessTx handles the case where we have non-nil
|
// broadcastReSignedSuccessTx handles the case where we have non-nil
|
||||||
// SignDetails, and offers the second level transaction to the Sweeper, that
|
// SignDetails, and offers the second level transaction to the Sweeper, that
|
||||||
// will re-sign it and attach fees at will.
|
// will re-sign it and attach fees at will.
|
||||||
func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
|
//
|
||||||
|
//nolint:funlen
|
||||||
|
func (h *htlcSuccessResolver) broadcastReSignedSuccessTx(immediate bool) (
|
||||||
*wire.OutPoint, error) {
|
*wire.OutPoint, error) {
|
||||||
|
|
||||||
// Keep track of the tx spending the HTLC output on the commitment, as
|
// Keep track of the tx spending the HTLC output on the commitment, as
|
||||||
@ -278,6 +284,7 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
|
|||||||
sweep.Params{
|
sweep.Params{
|
||||||
Budget: budget,
|
Budget: budget,
|
||||||
DeadlineHeight: deadline,
|
DeadlineHeight: deadline,
|
||||||
|
Immediate: immediate,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -433,7 +440,7 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
|
|||||||
// resolveRemoteCommitOutput handles sweeping an HTLC output on the remote
|
// resolveRemoteCommitOutput handles sweeping an HTLC output on the remote
|
||||||
// commitment with the preimage. In this case we can sweep the output directly,
|
// commitment with the preimage. In this case we can sweep the output directly,
|
||||||
// and don't have to broadcast a second-level transaction.
|
// and don't have to broadcast a second-level transaction.
|
||||||
func (h *htlcSuccessResolver) resolveRemoteCommitOutput() (
|
func (h *htlcSuccessResolver) resolveRemoteCommitOutput(immediate bool) (
|
||||||
ContractResolver, error) {
|
ContractResolver, error) {
|
||||||
|
|
||||||
isTaproot := txscript.IsPayToTaproot(
|
isTaproot := txscript.IsPayToTaproot(
|
||||||
@ -482,6 +489,7 @@ func (h *htlcSuccessResolver) resolveRemoteCommitOutput() (
|
|||||||
sweep.Params{
|
sweep.Params{
|
||||||
Budget: budget,
|
Budget: budget,
|
||||||
DeadlineHeight: deadline,
|
DeadlineHeight: deadline,
|
||||||
|
Immediate: immediate,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -134,7 +134,7 @@ func (i *htlcResolverTestContext) resolve() {
|
|||||||
// Start resolver.
|
// Start resolver.
|
||||||
i.resolverResultChan = make(chan resolveResult, 1)
|
i.resolverResultChan = make(chan resolveResult, 1)
|
||||||
go func() {
|
go func() {
|
||||||
nextResolver, err := i.resolver.Resolve()
|
nextResolver, err := i.resolver.Resolve(false)
|
||||||
i.resolverResultChan <- resolveResult{
|
i.resolverResultChan <- resolveResult{
|
||||||
nextResolver: nextResolver,
|
nextResolver: nextResolver,
|
||||||
err: err,
|
err: err,
|
||||||
|
@ -418,7 +418,9 @@ func checkSizeAndIndex(witness wire.TxWitness, size, index int) bool {
|
|||||||
// see a direct sweep via the timeout clause.
|
// see a direct sweep via the timeout clause.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the ContractResolver interface.
|
// NOTE: Part of the ContractResolver interface.
|
||||||
func (h *htlcTimeoutResolver) Resolve() (ContractResolver, error) {
|
func (h *htlcTimeoutResolver) Resolve(
|
||||||
|
immediate bool) (ContractResolver, error) {
|
||||||
|
|
||||||
// If we're already resolved, then we can exit early.
|
// If we're already resolved, then we can exit early.
|
||||||
if h.resolved {
|
if h.resolved {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -427,7 +429,7 @@ func (h *htlcTimeoutResolver) Resolve() (ContractResolver, error) {
|
|||||||
// Start by spending the HTLC output, either by broadcasting the
|
// Start by spending the HTLC output, either by broadcasting the
|
||||||
// second-level timeout transaction, or directly if this is the remote
|
// second-level timeout transaction, or directly if this is the remote
|
||||||
// commitment.
|
// commitment.
|
||||||
commitSpend, err := h.spendHtlcOutput()
|
commitSpend, err := h.spendHtlcOutput(immediate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -471,7 +473,7 @@ func (h *htlcTimeoutResolver) Resolve() (ContractResolver, error) {
|
|||||||
|
|
||||||
// sweepSecondLevelTx sends a second level timeout transaction to the sweeper.
|
// sweepSecondLevelTx sends a second level timeout transaction to the sweeper.
|
||||||
// This transaction uses the SINLGE|ANYONECANPAY flag.
|
// This transaction uses the SINLGE|ANYONECANPAY flag.
|
||||||
func (h *htlcTimeoutResolver) sweepSecondLevelTx() error {
|
func (h *htlcTimeoutResolver) sweepSecondLevelTx(immediate bool) error {
|
||||||
log.Infof("%T(%x): offering second-layer timeout tx to sweeper: %v",
|
log.Infof("%T(%x): offering second-layer timeout tx to sweeper: %v",
|
||||||
h, h.htlc.RHash[:],
|
h, h.htlc.RHash[:],
|
||||||
spew.Sdump(h.htlcResolution.SignedTimeoutTx))
|
spew.Sdump(h.htlcResolution.SignedTimeoutTx))
|
||||||
@ -529,6 +531,7 @@ func (h *htlcTimeoutResolver) sweepSecondLevelTx() error {
|
|||||||
sweep.Params{
|
sweep.Params{
|
||||||
Budget: budget,
|
Budget: budget,
|
||||||
DeadlineHeight: h.incomingHTLCExpiryHeight,
|
DeadlineHeight: h.incomingHTLCExpiryHeight,
|
||||||
|
Immediate: immediate,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -564,14 +567,16 @@ func (h *htlcTimeoutResolver) sendSecondLevelTxLegacy() error {
|
|||||||
// used to spend the output into the next stage. If this is the remote
|
// used to spend the output into the next stage. If this is the remote
|
||||||
// commitment, the output will be swept directly without the timeout
|
// commitment, the output will be swept directly without the timeout
|
||||||
// transaction.
|
// transaction.
|
||||||
func (h *htlcTimeoutResolver) spendHtlcOutput() (*chainntnfs.SpendDetail, error) {
|
func (h *htlcTimeoutResolver) spendHtlcOutput(
|
||||||
|
immediate bool) (*chainntnfs.SpendDetail, error) {
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
// If we have non-nil SignDetails, this means that have a 2nd level
|
// If we have non-nil SignDetails, this means that have a 2nd level
|
||||||
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
|
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
|
||||||
// (the case for anchor type channels). In this case we can re-sign it
|
// (the case for anchor type channels). In this case we can re-sign it
|
||||||
// and attach fees at will. We let the sweeper handle this job.
|
// and attach fees at will. We let the sweeper handle this job.
|
||||||
case h.htlcResolution.SignDetails != nil && !h.outputIncubating:
|
case h.htlcResolution.SignDetails != nil && !h.outputIncubating:
|
||||||
if err := h.sweepSecondLevelTx(); err != nil {
|
if err := h.sweepSecondLevelTx(immediate); err != nil {
|
||||||
log.Errorf("Sending timeout tx to sweeper: %v", err)
|
log.Errorf("Sending timeout tx to sweeper: %v", err)
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -375,7 +375,7 @@ func TestHtlcTimeoutResolver(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
_, err := resolver.Resolve()
|
_, err := resolver.Resolve(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resolveErr <- err
|
resolveErr <- err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user