Skip to content

Commit

Permalink
contractcourt: remove the immediate param used in Resolve
Browse files Browse the repository at this point in the history
This `immediate` flag was added as a hack so during a restart, the
pending resolvers would offer the inputs to the sweeper and ask it to
sweep them immediately. This is no longer need due to `blockbeat`, as
now during restart, a block is always sent to all subsystems via the
flow `ChainArb` -> `ChannelArb` -> resolvers -> sweeper. Thus, when
there are pending inputs offered, they will be processed by the sweeper
immediately.
  • Loading branch information
yyforyongyu committed Jul 2, 2024
1 parent e01589e commit af06828
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 53 deletions.
2 changes: 1 addition & 1 deletion contractcourt/anchor_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *anchorResolver) ResolverKey() []byte {
}

// Resolve offers the anchor output to the sweeper and waits for it to be swept.
func (c *anchorResolver) Resolve(_ bool) (ContractResolver, error) {
func (c *anchorResolver) Resolve() (ContractResolver, error) {
// Attempt to update the sweep parameters to the post-confirmation
// situation. We don't want to force sweep anymore, because the anchor
// lost its special purpose to get the commitment confirmed. It is just
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/breach_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (b *breachResolver) ResolverKey() []byte {
// been broadcast.
//
// TODO(yy): let sweeper handle the breach inputs.
func (b *breachResolver) Resolve(_ bool) (ContractResolver, error) {
func (b *breachResolver) Resolve() (ContractResolver, error) {
if !b.subscribed {
complete, err := b.SubscribeBreachComplete(
&b.ChanPoint, b.replyChan,
Expand Down
20 changes: 8 additions & 12 deletions contractcourt/channel_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet,
// TODO(roasbeef): this isn't re-launched?
}

c.launchResolvers(unresolvedContracts, true)
c.launchResolvers(unresolvedContracts)

return nil
}
Expand Down Expand Up @@ -1247,7 +1247,7 @@ func (c *ChannelArbitrator) stateStep(

// Finally, we'll launch all the required contract resolvers.
// Once they're all resolved, we're no longer needed.
c.launchResolvers(resolvers, false)
c.launchResolvers(resolvers)

nextState = StateWaitingFullResolution

Expand Down Expand Up @@ -1578,16 +1578,14 @@ func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
}

// launchResolvers updates the activeResolvers list and starts the resolvers.
func (c *ChannelArbitrator) launchResolvers(resolvers []ContractResolver,
immediate bool) {

func (c *ChannelArbitrator) launchResolvers(resolvers []ContractResolver) {
c.activeResolversLock.Lock()
defer c.activeResolversLock.Unlock()

c.activeResolvers = resolvers
c.activeResolversLock.Unlock()

for _, contract := range resolvers {
c.wg.Add(1)
go c.resolveContract(contract, immediate)
go c.resolveContract(contract)
}
}

Expand Down Expand Up @@ -2600,9 +2598,7 @@ func (c *ChannelArbitrator) replaceResolver(oldResolver,
// contracts.
//
// NOTE: This MUST be run as a goroutine.
func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver,
immediate bool) {

func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver) {
defer c.wg.Done()

log.Debugf("ChannelArbitrator(%v): attempting to resolve %T",
Expand All @@ -2623,7 +2619,7 @@ func (c *ChannelArbitrator) resolveContract(currentContract ContractResolver,
default:
// Otherwise, we'll attempt to resolve the current
// contract.
nextContract, err := currentContract.Resolve(immediate)
nextContract, err := currentContract.Resolve()
if err != nil {
if err == errResolverShuttingDown {
return
Expand Down
4 changes: 1 addition & 3 deletions contractcourt/commit_sweep_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func (c *commitSweepResolver) getCommitTxConfHeight() (uint32, error) {
// returned.
//
// NOTE: This function MUST be run as a goroutine.
//
//nolint:funlen
func (c *commitSweepResolver) Resolve(_ bool) (ContractResolver, error) {
func (c *commitSweepResolver) Resolve() (ContractResolver, error) {
// If we're already resolved, then we can exit early.
if c.resolved {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/commit_sweep_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *commitSweepResolverTestContext) resolve() {
// Start resolver.
i.resolverResultChan = make(chan resolveResult, 1)
go func() {
nextResolver, err := i.resolver.Resolve(false)
nextResolver, err := i.resolver.Resolve()
i.resolverResultChan <- resolveResult{
nextResolver: nextResolver,
err: err,
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/contract_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ContractResolver interface {
// resolution, then another resolve is returned.
//
// NOTE: This function MUST be run as a goroutine.
Resolve(immediate bool) (ContractResolver, error)
Resolve() (ContractResolver, error)

// SupplementState allows the user of a ContractResolver to supplement
// it with state required for the proper resolution of a contract.
Expand Down
4 changes: 1 addition & 3 deletions contractcourt/htlc_incoming_contest_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ func (h *htlcIncomingContestResolver) processFinalHtlcFail() error {
// as we have no remaining actions left at our disposal.
//
// NOTE: Part of the ContractResolver interface.
func (h *htlcIncomingContestResolver) Resolve(
_ bool) (ContractResolver, error) {

func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
// If we're already full resolved, then we don't have anything further
// to do.
if h.resolved {
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/htlc_incoming_contest_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (i *incomingResolverTestContext) resolve() {
i.resolveErr = make(chan error, 1)
go func() {
var err error
i.nextResolver, err = i.resolver.Resolve(false)
i.nextResolver, err = i.resolver.Resolve()
i.resolveErr <- err
}()

Expand Down
4 changes: 1 addition & 3 deletions contractcourt/htlc_outgoing_contest_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func newOutgoingContestResolver(res lnwallet.OutgoingHtlcResolution,
// 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
// point.
func (h *htlcOutgoingContestResolver) Resolve(
_ bool) (ContractResolver, error) {

func (h *htlcOutgoingContestResolver) Resolve() (ContractResolver, error) {
// If we're already full resolved, then we don't have anything further
// to do.
if h.resolved {
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/htlc_outgoing_contest_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (i *outgoingResolverTestContext) resolve() {
// Start resolver.
i.resolverResultChan = make(chan resolveResult, 1)
go func() {
nextResolver, err := i.resolver.Resolve(false)
nextResolver, err := i.resolver.Resolve()
i.resolverResultChan <- resolveResult{
nextResolver: nextResolver,
err: err,
Expand Down
24 changes: 9 additions & 15 deletions contractcourt/htlc_success_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ func (h *htlcSuccessResolver) ResolverKey() []byte {
// TODO(roasbeef): create multi to batch
//
// NOTE: Part of the ContractResolver interface.
func (h *htlcSuccessResolver) Resolve(
immediate bool) (ContractResolver, error) {

func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) {
// If we're already resolved, then we can exit early.
if h.resolved {
return nil, nil
Expand All @@ -126,12 +124,12 @@ func (h *htlcSuccessResolver) Resolve(
// If we don't have a success transaction, then this means that this is
// an output on the remote party's commitment transaction.
if h.htlcResolution.SignedSuccessTx == nil {
return h.resolveRemoteCommitOutput(immediate)
return h.resolveRemoteCommitOutput()
}

// Otherwise this an output on our own commitment, and we must start by
// broadcasting the second-level success transaction.
secondLevelOutpoint, err := h.broadcastSuccessTx(immediate)
secondLevelOutpoint, err := h.broadcastSuccessTx()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,8 +163,8 @@ func (h *htlcSuccessResolver) Resolve(
// 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
// resolver to be fully resolved.
func (h *htlcSuccessResolver) broadcastSuccessTx(
immediate bool) (*wire.OutPoint, error) {
func (h *htlcSuccessResolver) broadcastSuccessTx() (
*wire.OutPoint, error) {

// If we have non-nil SignDetails, this means that have a 2nd level
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
Expand All @@ -175,7 +173,7 @@ func (h *htlcSuccessResolver) broadcastSuccessTx(
// the checkpointed outputIncubating field to determine if we already
// swept the HTLC output into the second level transaction.
if h.htlcResolution.SignDetails != nil {
return h.broadcastReSignedSuccessTx(immediate)
return h.broadcastReSignedSuccessTx()
}

// Otherwise we'll publish the second-level transaction directly and
Expand Down Expand Up @@ -225,10 +223,8 @@ func (h *htlcSuccessResolver) broadcastSuccessTx(
// broadcastReSignedSuccessTx handles the case where we have non-nil
// SignDetails, and offers the second level transaction to the Sweeper, that
// will re-sign it and attach fees at will.
//
//nolint:funlen
func (h *htlcSuccessResolver) broadcastReSignedSuccessTx(immediate bool) (
*wire.OutPoint, error) {
func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (*wire.OutPoint,
error) {

// Keep track of the tx spending the HTLC output on the commitment, as
// this will be the confirmed second-level tx we'll ultimately sweep.
Expand Down Expand Up @@ -284,7 +280,6 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx(immediate bool) (
sweep.Params{
Budget: budget,
DeadlineHeight: deadline,
Immediate: immediate,
},
)
if err != nil {
Expand Down Expand Up @@ -416,7 +411,7 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx(immediate bool) (
// resolveRemoteCommitOutput handles sweeping an HTLC output on the remote
// commitment with the preimage. In this case we can sweep the output directly,
// and don't have to broadcast a second-level transaction.
func (h *htlcSuccessResolver) resolveRemoteCommitOutput(immediate bool) (
func (h *htlcSuccessResolver) resolveRemoteCommitOutput() (
ContractResolver, error) {

isTaproot := txscript.IsPayToTaproot(
Expand Down Expand Up @@ -465,7 +460,6 @@ func (h *htlcSuccessResolver) resolveRemoteCommitOutput(immediate bool) (
sweep.Params{
Budget: budget,
DeadlineHeight: deadline,
Immediate: immediate,
},
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/htlc_success_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (i *htlcResolverTestContext) resolve() {
// Start resolver.
i.resolverResultChan = make(chan resolveResult, 1)
go func() {
nextResolver, err := i.resolver.Resolve(false)
nextResolver, err := i.resolver.Resolve()
i.resolverResultChan <- resolveResult{
nextResolver: nextResolver,
err: err,
Expand Down
15 changes: 6 additions & 9 deletions contractcourt/htlc_timeout_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ func checkSizeAndIndex(witness wire.TxWitness, size, index int) bool {
// see a direct sweep via the timeout clause.
//
// NOTE: Part of the ContractResolver interface.
func (h *htlcTimeoutResolver) Resolve(
immediate bool) (ContractResolver, error) {

func (h *htlcTimeoutResolver) Resolve() (ContractResolver, error) {
// If we're already resolved, then we can exit early.
if h.resolved {
return nil, nil
Expand All @@ -429,7 +427,7 @@ func (h *htlcTimeoutResolver) Resolve(
// Start by spending the HTLC output, either by broadcasting the
// second-level timeout transaction, or directly if this is the remote
// commitment.
commitSpend, err := h.spendHtlcOutput(immediate)
commitSpend, err := h.spendHtlcOutput()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -473,7 +471,7 @@ func (h *htlcTimeoutResolver) Resolve(

// sweepSecondLevelTx sends a second level timeout transaction to the sweeper.
// This transaction uses the SINLGE|ANYONECANPAY flag.
func (h *htlcTimeoutResolver) sweepSecondLevelTx(immediate bool) error {
func (h *htlcTimeoutResolver) sweepSecondLevelTx() error {
log.Infof("%T(%x): offering second-layer timeout tx to sweeper: %v",
h, h.htlc.RHash[:],
spew.Sdump(h.htlcResolution.SignedTimeoutTx))
Expand Down Expand Up @@ -531,7 +529,6 @@ func (h *htlcTimeoutResolver) sweepSecondLevelTx(immediate bool) error {
sweep.Params{
Budget: budget,
DeadlineHeight: h.incomingHTLCExpiryHeight,
Immediate: immediate,
},
)
if err != nil {
Expand Down Expand Up @@ -567,16 +564,16 @@ func (h *htlcTimeoutResolver) sendSecondLevelTxLegacy() error {
// used to spend the output into the next stage. If this is the remote
// commitment, the output will be swept directly without the timeout
// transaction.
func (h *htlcTimeoutResolver) spendHtlcOutput(
immediate bool) (*chainntnfs.SpendDetail, error) {
func (h *htlcTimeoutResolver) spendHtlcOutput() (
*chainntnfs.SpendDetail, error) {

switch {
// If we have non-nil SignDetails, this means that have a 2nd level
// HTLC transaction that is signed using sighash SINGLE|ANYONECANPAY
// (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.
case h.htlcResolution.SignDetails != nil && !h.outputIncubating:
if err := h.sweepSecondLevelTx(immediate); err != nil {
if err := h.sweepSecondLevelTx(); err != nil {
log.Errorf("Sending timeout tx to sweeper: %v", err)

return nil, err
Expand Down
2 changes: 1 addition & 1 deletion contractcourt/htlc_timeout_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func TestHtlcTimeoutResolver(t *testing.T) {
go func() {
defer wg.Done()

_, err := resolver.Resolve(false)
_, err := resolver.Resolve()
if err != nil {
resolveErr <- err
}
Expand Down

0 comments on commit af06828

Please sign in to comment.