diff --git a/tools/linters/actorblock_baseline.txt b/tools/linters/actorblock_baseline.txt index 94524230f..2d531d371 100644 --- a/tools/linters/actorblock_baseline.txt +++ b/tools/linters/actorblock_baseline.txt @@ -36,8 +36,6 @@ github.com/lightninglabs/wavelength/txconfirm.TxBroadcasterActor.registerConfWat github.com/lightninglabs/wavelength/txconfirm.TxBroadcasterActor.registerFanoutConfWatch await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise github.com/lightninglabs/wavelength/txconfirm.TxBroadcasterActor.unregisterConfWatch await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise github.com/lightninglabs/wavelength/txconfirm.TxBroadcasterActor.unregisterFanoutConfWatch await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise -github.com/lightninglabs/wavelength/txconfirm.feeBumpStateFanoutPending.processDemands await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise -github.com/lightninglabs/wavelength/txconfirm.feeBumpStateIdle.processDemands await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise github.com/lightninglabs/wavelength/unroll.behavior.driveEvent await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise github.com/lightninglabs/wavelength/unroll.behavior.ensureBlockSubscription await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise github.com/lightninglabs/wavelength/unroll.behavior.ensureNodeConfirmed await=1 # legacy Await in a turn, migrate to AskThen or DetachAskPromise diff --git a/txconfirm/AGENTS.md b/txconfirm/AGENTS.md index 871ce645b..2a0a8acc6 100644 --- a/txconfirm/AGENTS.md +++ b/txconfirm/AGENTS.md @@ -130,6 +130,8 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/txconfir may already be mined while its notification is still queued. Pending fee-input fanouts likewise retain their candidate/watch on these errors; they must not rebuild merely because a confirmed rebroadcast was ambiguous. + A lost first split receipt retains the signed bytes, watches their local + txid and excludes the original split inputs from other fee children. Pending fanouts also retain their reservations on transport or unclassified rebroadcast failures. The operational error stays visible, and retries remain block-paced; only an explicit rejection uses the rebuild path. diff --git a/txconfirm/CLAUDE.md b/txconfirm/CLAUDE.md index 871ce645b..2a0a8acc6 100644 --- a/txconfirm/CLAUDE.md +++ b/txconfirm/CLAUDE.md @@ -130,6 +130,8 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/txconfir may already be mined while its notification is still queued. Pending fee-input fanouts likewise retain their candidate/watch on these errors; they must not rebuild merely because a confirmed rebroadcast was ambiguous. + A lost first split receipt retains the signed bytes, watches their local + txid and excludes the original split inputs from other fee children. Pending fanouts also retain their reservations on transport or unclassified rebroadcast failures. The operational error stays visible, and retries remain block-paced; only an explicit rejection uses the rebuild path. diff --git a/txconfirm/address_lifetime_test.go b/txconfirm/address_lifetime_test.go index de2a4e747..f8754c7a2 100644 --- a/txconfirm/address_lifetime_test.go +++ b/txconfirm/address_lifetime_test.go @@ -345,10 +345,9 @@ func TestFanoutFallbackKeepsDestinations(t *testing.T) { ) } -// TestFanoutPublishEndsScriptPlan models a broadcast timeout after the first -// candidate may have reached the network. A later funding attempt using a -// different input must not pay destinations from that published candidate. -func TestFanoutPublishEndsScriptPlan(t *testing.T) { +// TestFanoutAmbiguousPublishRetainsCandidate verifies that a lost initial +// receipt cannot charge a second wallet input when the next block arrives. +func TestFanoutAmbiguousPublishRetainsCandidate(t *testing.T) { t.Parallel() chain := newFakeChainSourceRef(100) @@ -383,22 +382,26 @@ func TestFanoutPublishEndsScriptPlan(t *testing.T) { first := chain.broadcastCalls[0].Tx.Copy() require.Equal(t, 1, wallet.scriptCalls) - // The old input remains unavailable after the ambiguous attempt. The - // existing funding retry can select another coin and publish both txs. + require.Equal(t, first.TxHash(), c.PendingFanout().txid) + require.Contains( + t, b.excludedOutpointsForOtherParents(demands[0].parentTxid), + first.TxIn[0].PreviousOutPoint, + ) + + // Even with new confirmed supply only the original bytes may be + // retried. wallet.utxos = []*walletcore.Utxo{makeWalletUTXOWithAmount(100_000, 8)} chain.broadcastErr = nil pending, err = c.EnsureSupply(t.Context(), demands, 5, 102, 1) require.NoError(t, err) - require.NotNil(t, pending) require.Len(t, chain.broadcastCalls, 2) - require.NotEqual( - t, first.TxIn[0].PreviousOutPoint, - pending.tx.TxIn[0].PreviousOutPoint, - ) - require.NotEqual( - t, first.TxOut[0].PkScript, pending.tx.TxOut[0].PkScript, - ) - require.Equal(t, 2, wallet.scriptCalls) + require.Equal(t, first, chain.broadcastCalls[1].Tx) + require.Equal(t, first.TxHash(), pending.txid) + require.Len(t, wallet.fundedTemplates, 1) + require.Equal(t, 1, wallet.scriptCalls) + c.OnFanoutConfirmed(t.Context(), first.TxHash()) + require.Empty(t, b.fanoutInputs) + require.Nil(t, c.PendingFanout()) } // TestFanoutPartialAllocationSurvivesFailure proves a failed later derivation diff --git a/txconfirm/broadcaster.go b/txconfirm/broadcaster.go index d5c0af41c..323c9de2c 100644 --- a/txconfirm/broadcaster.go +++ b/txconfirm/broadcaster.go @@ -389,6 +389,10 @@ type CPFPBroadcaster struct { // has terminally confirmed or failed. parentStates map[chainhash.Hash]*parentBumpState + // fanoutInputs belong to the pending split, including when its + // broadcast receipt was lost and the wallet still lists the inputs. + fanoutInputs []*wire.TxIn + // changeScripts owns one destination per tracked parent, including when // no fee input or accepted package exists yet. Keep it separate from // parentStates: allocating an address is not replacement-fee history, @@ -674,6 +678,10 @@ func (b *CPFPBroadcaster) excludedOutpointsForOtherParents( parentTxid chainhash.Hash) map[wire.OutPoint]struct{} { excluded := make(map[wire.OutPoint]struct{}) + // The wallet may still list inputs spent by an unconfirmed split. + for _, input := range b.fanoutInputs { + excluded[input.PreviousOutPoint] = struct{}{} + } for otherTxid, state := range b.parentStates { if otherTxid == parentTxid { continue diff --git a/txconfirm/fee_bump_fsm_logic.go b/txconfirm/fee_bump_fsm_logic.go index 0fe0a7297..14094e324 100644 --- a/txconfirm/fee_bump_fsm_logic.go +++ b/txconfirm/fee_bump_fsm_logic.go @@ -235,50 +235,33 @@ func (s *feeBumpStateIdle) processDemands(ctx context.Context, ) } - // Publishing ends this destination plan even when the response is - // ambiguous: the candidate may already be on chain. A later funded - // candidate can use other inputs and confirm alongside it, so it must - // not reuse these addresses. Pending retains its funded tx separately. + // Once submitted, the signed transaction may already be on chain. + // Track its local txid even when the backend loses the response. env.fanoutScripts = fanoutScriptPlan{} - - resp, err := b.cfg.ChainSource.Ask( - ctx, &chainsource.BroadcastTxRequest{ - Tx: fundedTx, - Label: "txconfirm-fee-input-fanout", - }, - ).Await(ctx).Unpack() - if err != nil { - env.releasePredictedFanoutOutputs(assignments) - - return s.stayIdle(env, fmt.Errorf("broadcast fanout: %w", err)) - } - - broadcastResp, ok := resp.(*chainsource.BroadcastTxResponse) - if !ok { - env.releasePredictedFanoutOutputs(assignments) - - return s.stayIdle( - env, fmt.Errorf("unexpected fanout response %T", resp), - ) - } - pending := &pendingFeeInputFanout{ - txid: broadcastResp.Txid, + txid: txid, tx: fundedTx.Copy(), watchScript: watchScript, assignments: assignments, lastBroadcastHeight: height, } - // The fanout is now on the wire. Log it at info as a lifecycle event: - // this is the moment several blocked parents gain a supply path. - env.log.InfoS(ctx, "Fee-input fanout broadcast", - slog.String("txid", broadcastResp.Txid.String()), + err = env.broadcastFanout(ctx, pending) + if fanoutRejected(err) { + env.releasePredictedFanoutOutputs(assignments) + + return s.stayIdle(env, err) + } + env.lastErr = err + + env.log.InfoS(ctx, "Watching submitted fee-input fanout", + slog.String("txid", txid.String()), slog.Int("num_outputs", len(fundedTx.TxOut)), slog.Int("num_demands", len(fanoutDemands)), ) env.consumeFanoutInputs(fundedTx.TxIn) + b.fanoutInputs = pending.tx.TxIn // Advance to fanout-pending and ask the actor to register a // confirmation watch on the fanout's chosen output script. The watch is @@ -381,59 +364,58 @@ func (s *feeBumpStateFanoutPending) processDemands(ctx context.Context, }, nil } - resp, err := env.broadcaster.cfg.ChainSource.Ask( + err := env.broadcastFanout(ctx, pending) + if fanoutRejected(err) { + env.log.WarnS(ctx, "Pending fee-input fanout rejected; "+ + "rebuilding", err, "txid", pending.txid) + + return env.clearAndRebuild(ctx, pending, event), nil + } + + env.lastErr = err + + return env.refreshRebroadcast(ctx, pending, height), nil +} + +// broadcastFanout submits the retained bytes. A malformed response or an +// unknown transport result leaves the caller responsible for the same txid. +func (env *feeBumpEnvironment) broadcastFanout(ctx context.Context, + pending *pendingFeeInputFanout) error { + + future := env.broadcaster.cfg.ChainSource.Ask( ctx, &chainsource.BroadcastTxRequest{ Tx: pending.tx.Copy(), Label: "txconfirm-fee-input-fanout", }, - ).Await(ctx).Unpack() + ) + + // A lost reply can follow a successful submit. Keep tracking the + // signed candidate when the backend cannot distinguish that case. + //actor:allow-await bounded chain submit has no callback + resp, err := future.Await(ctx).Unpack() if err != nil { - // Keep the existing watch on known transactions and ambiguous - // spent inputs. A confirmed fanout may return the latter on - // rebroadcast before its confirmation notification is - // processed. Neither case justifies discarding it and funding a - // new fanout. if IsIgnorableBroadcastError(err) || isAmbiguousSpendError(err) { - return env.refreshRebroadcast(ctx, pending, height), nil + return nil } - // A timeout, canceled RPC or unclassified backend response does - // not prove that the previously submitted fanout was rejected. - // Preserve its candidate, reservations and confirmation watch. - // Surface the operational failure without destroying recovery. - if ClassifyBroadcastFailure(err) == BroadcastFailureUnknown { - env.lastErr = fmt.Errorf("rebroadcast fanout: %w", err) - - return env.refreshRebroadcast(ctx, pending, height), nil - } - - // A hard rejection means the fanout is no longer valid; release - // it and return to idle, then immediately rebuild from scratch - // within this same turn by routing the demand event back into - // the (now idle) FSM as an internal event. The watch teardown - // for the abandoned fanout is left to the actor via the unwatch - // outbox. - env.log.WarnS(ctx, "Pending fee-input fanout rejected; "+ - "rebuilding", err, "txid", pending.txid) - - return env.clearAndRebuild(ctx, pending, event), nil + return fmt.Errorf("broadcast fanout: %w", err) } - broadcastResp, ok := resp.(*chainsource.BroadcastTxResponse) - if !ok { - env.lastErr = fmt.Errorf("unexpected fanout response %T", resp) - - return env.clearPendingFanout(ctx, pending), nil + // Only a receipt for our locally signed bytes acknowledges this split. + reply, ok := resp.(*chainsource.BroadcastTxResponse) + if !ok || reply == nil || reply.Txid != pending.txid { + return fmt.Errorf("invalid broadcast receipt for fanout %s", + pending.txid) } - if broadcastResp.Txid != pending.txid { - env.lastErr = fmt.Errorf("fanout rebroadcast txid changed") - - return env.clearPendingFanout(ctx, pending), nil - } + return nil +} - return env.refreshRebroadcast(ctx, pending, height), nil +// fanoutRejected reports an explicit rejection that permits a fresh attempt. +func fanoutRejected(err error) bool { + return err != nil && + ClassifyBroadcastFailure(err) != BroadcastFailureUnknown } // processConfirmed promotes a confirmed fanout's predicted outputs into used @@ -451,6 +433,7 @@ func (s *feeBumpStateFanoutPending) processConfirmed(ctx context.Context, }, nil } + env.broadcaster.fanoutInputs = nil env.promoteConfirmedFanout(ctx, pending) // Returning to idle: tell the actor to tear down the confirmation watch @@ -551,6 +534,7 @@ func (env *feeBumpEnvironment) releasePendingFanout(ctx context.Context, slog.String("txid", pending.txid.String()), ) + env.broadcaster.fanoutInputs = nil env.releasePredictedFanoutOutputs(pending.assignments) if pending.tx != nil { for _, txIn := range pending.tx.TxIn {