Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion db/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/db.<Symb
have several rows. `UpdateReceive` writes the proofs in the transaction
that moves the receive, so the wallet holds a receive's assets exactly
when the receive reads confirmed.
- `LatestMigrationVersion = 32` — current schema version.
- `LatestMigrationVersion = 33` — current schema version.
- `PendingIntentPersistenceStore` — implements `wallet.PendingIntentStore`,
the persistence half of the generic restart-safe intent outbox (header
`pending_intents` + per-kind detail tables + `pending_intent_anchors`).
Expand Down Expand Up @@ -555,6 +555,9 @@ when adding one.
orders rows of the same creation time. The receive state has no upper
bound in the schema, because the engine defines the states.

- `000033_fee_input_split` stores the signed wallet split and its output
assignments until the broadcaster reconciles it.

## Deep Docs

- [ARCHITECTURE.md](../ARCHITECTURE.md) — System-wide package map.
Expand Down
5 changes: 4 additions & 1 deletion db/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/db.<Symb
have several rows. `UpdateReceive` writes the proofs in the transaction
that moves the receive, so the wallet holds a receive's assets exactly
when the receive reads confirmed.
- `LatestMigrationVersion = 32` — current schema version.
- `LatestMigrationVersion = 33` — current schema version.
- `PendingIntentPersistenceStore` — implements `wallet.PendingIntentStore`,
the persistence half of the generic restart-safe intent outbox (header
`pending_intents` + per-kind detail tables + `pending_intent_anchors`).
Expand Down Expand Up @@ -555,6 +555,9 @@ when adding one.
orders rows of the same creation time. The receive state has no upper
bound in the schema, because the engine defines the states.

- `000033_fee_input_split` stores the signed wallet split and its output
assignments until the broadcaster reconciles it.

## Deep Docs

- [ARCHITECTURE.md](../ARCHITECTURE.md) — System-wide package map.
Expand Down
2 changes: 1 addition & 1 deletion db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
// daemon.
//
// NOTE: This MUST be updated when a new migration is added.
LatestMigrationVersion uint = 32
LatestMigrationVersion uint = 33
)

// MigrationTarget is a functional option that can be passed to applyMigrations
Expand Down
1 change: 1 addition & 0 deletions db/sqlc/migrations/000033_fee_input_split.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS wallet_fee_split;
6 changes: 6 additions & 0 deletions db/sqlc/migrations/000033_fee_input_split.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- The shared fee broadcaster has one pending wallet split. Store its signed
-- bytes and output assignments before the first publication attempt.
CREATE TABLE wallet_fee_split (
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
candidate BLOB NOT NULL
);
5 changes: 5 additions & 0 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions db/sqlc/queries/wallet_fee_split.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- name: SaveWalletFeeSplit :execrows
INSERT INTO wallet_fee_split (singleton, candidate) VALUES (1, $1)
ON CONFLICT (singleton) DO UPDATE SET candidate = excluded.candidate
WHERE wallet_fee_split.candidate = excluded.candidate;

-- name: LoadWalletFeeSplit :one
SELECT candidate FROM wallet_fee_split WHERE singleton = 1;

-- name: ClearWalletFeeSplit :exec
DELETE FROM wallet_fee_split WHERE singleton = 1 AND candidate = $1;
5 changes: 5 additions & 0 deletions db/sqlc/schemas/generated_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,11 @@ CREATE TABLE vtxos (
FOREIGN KEY (round_id) REFERENCES rounds(round_id)
);

CREATE TABLE wallet_fee_split (
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
candidate BLOB NOT NULL
);

CREATE TABLE wallet_utxo_log (
entry_id INTEGER PRIMARY KEY AUTOINCREMENT,

Expand Down
44 changes: 44 additions & 0 deletions db/sqlc/wallet_fee_split.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions db/wallet_fee_split_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package db

import (
"database/sql"
"testing"

"github.com/stretchr/testify/require"
)

// TestWalletFeeSplitOwnership proves that retries keep the original signed
// candidate and that clearing a stale candidate cannot erase a new one.
func TestWalletFeeSplitOwnership(t *testing.T) {
t.Parallel()
store := NewTestDB(t)
ctx := t.Context()
first, second := []byte("first signed transaction"), []byte(
"second signed transaction",
)
// A retry with identical bytes must acquire the same journal slot.
for _, candidate := range [][]byte{first, first} {
count, err := store.SaveWalletFeeSplit(ctx, candidate)
require.NoError(t, err)
require.EqualValues(t, 1, count)
}
// A competing candidate can neither replace nor clear the owner.
count, err := store.SaveWalletFeeSplit(ctx, second)
require.NoError(t, err)
require.Zero(t, count)
require.NoError(t, store.ClearWalletFeeSplit(ctx, second))
saved, err := store.LoadWalletFeeSplit(ctx)
require.NoError(t, err)
require.Equal(t, first, saved)
require.NoError(t, store.ClearWalletFeeSplit(ctx, first))
_, err = store.LoadWalletFeeSplit(ctx)
require.ErrorIs(t, err, sql.ErrNoRows)
}
4 changes: 4 additions & 0 deletions txconfirm/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/txconfir
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.
The daemon journals the signed split, assignments and height hint before
first submit. Startup restores its input exclusions and confirmation watch.
A committed save with a lost receipt is retried with the same bytes.
A confirmed split whose journal clear fails retries on a later block.
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.
Expand Down
4 changes: 4 additions & 0 deletions txconfirm/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ For field-level detail, use `go doc github.com/lightninglabs/wavelength/txconfir
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.
The daemon journals the signed split, assignments and height hint before
first submit. Startup restores its input exclusions and confirmation watch.
A committed save with a lost receipt is retried with the same bytes.
A confirmed split whose journal clear fails retries on a later block.
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.
Expand Down
75 changes: 74 additions & 1 deletion txconfirm/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ var ErrEnsureParamsMismatch = errors.New("ensure params mismatch existing " +

// Config configures the generic shared tx confirmation actor.
type Config struct {
// FeeSplitJournal persists signed wallet splits before publication.
FeeSplitJournal FeeSplitJournal

// FeeSplitInputScript looks up the original wallet output, including
// after it was spent. The chain notifier needs its script to find a
// competing spend that confirmed while the broadcaster was offline.
FeeSplitInputScript func(context.Context, wire.OutPoint) ([]byte, error)

// ChainSource provides the blockchain interface for best-height
// queries,
// confirmation watches, block subscriptions, fee estimation, and
Expand Down Expand Up @@ -182,6 +190,13 @@ type TxBroadcasterActor struct {
// than returned, which would tear the long-lived FSM down).
feeBumpEnv *feeBumpEnvironment

// fanoutWatchActive is true after chainsource acknowledged the watch.
fanoutWatchActive bool

// splitSpendWatches stores requested registrations for the current
// candidate. The script is part of the key used to unsubscribe.
splitSpendWatches map[wire.OutPoint][]byte

// tracked maps txid to its shared confirmation state.
tracked map[chainhash.Hash]*trackedTx

Expand Down Expand Up @@ -411,7 +426,8 @@ func NewTxBroadcasterActor(cfg Config) *TxBroadcasterActor {
})

log := cfg.Log.UnwrapOr(btclog.Disabled)
feeBumpFSM, feeBumpEnv := newFeeBumpStateMachine(broadcaster, log)
feeBumpFSM, feeBumpEnv := newFeeBumpStateMachine(broadcaster, log, nil)
feeBumpEnv.journal = cfg.FeeSplitJournal

return &TxBroadcasterActor{
cfg: cfg,
Expand All @@ -435,6 +451,13 @@ func (a *TxBroadcasterActor) Receive(ctx context.Context,
msg Msg) fn.Result[Resp] {

switch req := msg.(type) {
case *ResumeFeeSplit:
if err := a.resumeFeeSplit(ctx); err != nil {
return fn.Err[Resp](err)
}

return fn.Ok[Resp](&EnsureConfirmedResp{})

case *EnsureConfirmedReq:
resp, err := a.handleEnsure(ctx, req)
if err != nil {
Expand All @@ -459,6 +482,39 @@ func (a *TxBroadcasterActor) Receive(ctx context.Context,

return fn.Ok[Resp](resp)

case *splitWatchResult:
pending := a.feeBumpPending()
if req.err != nil && pending != nil &&
req.candidate == pending.txid {

delete(a.splitSpendWatches, req.input)
a.log.WarnS(ctx, "Register split input spend", req.err,
"outpoint", req.input)
}

return fn.Ok[Resp](&EnsureConfirmedResp{})

case *splitSpendObserved:
pending := a.feeBumpPending()
if pending != nil && pending.txid == req.candidate &&
req.height > 0 {

if req.spender == pending.txid {
a.handleFanoutConfirmed(
ctx, &confirmationObservedMsg{
txid: pending.txid,
},
)
} else {
a.driveFeeBump(ctx, &feeBumpFanoutResolved{
txid: pending.txid,
spender: req.spender,
})
}
}

return fn.Ok[Resp](&EnsureConfirmedResp{})

case *confirmationObservedMsg:
a.handleConfirmationObserved(ctx, req)

Expand Down Expand Up @@ -1437,6 +1493,23 @@ func (a *TxBroadcasterActor) handleBlockObserved(ctx context.Context,
a.hasBestHeight = true
}

// A recorded split remains wallet work even if every original
// subscriber stopped or the daemon restarted before any job resumed.
if pending := a.feeBumpPending(); pending != nil &&
a.cfg.FeeSplitJournal != nil {

if err := a.watchFeeSplit(ctx, pending); err != nil {
a.log.WarnS(ctx, "Unable to restore fee split watch",
err,
)
}
a.driveFeeBump(ctx, &feeBumpDemandsObserved{
height: msg.height, retryInterval: a.
cfg.
FeeBumpIntervalBlocks,
})
}

for _, entry := range a.tracked {
if a.retryPendingFailure(ctx, entry) {
continue
Expand Down
6 changes: 3 additions & 3 deletions txconfirm/broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3719,8 +3719,8 @@ func (c *testFeeBump) OnFanoutConfirmed(ctx context.Context,

c.t.Helper()

_, err := c.fsm.AskEvent(ctx, &feeBumpFanoutConfirmedEvent{
txid: txid,
_, err := c.fsm.AskEvent(ctx, &feeBumpFanoutResolved{
txid: txid, spender: txid,
}).Await(ctx).Unpack()
require.NoError(c.t, err)
}
Expand All @@ -3742,7 +3742,7 @@ func (c *testFeeBump) PruneParent(ctx context.Context, parent chainhash.Hash) {
func newTestFeeBumpController(t *testing.T, b *CPFPBroadcaster) *testFeeBump {
t.Helper()

fsm, env := newFeeBumpStateMachine(b, btclog.Disabled)
fsm, env := newFeeBumpStateMachine(b, btclog.Disabled, nil)
fsm.Start(t.Context())
t.Cleanup(fsm.Stop)

Expand Down
Loading
Loading