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
2 changes: 1 addition & 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 = 33` — current schema version.
- `LatestMigrationVersion = 34` — 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
2 changes: 1 addition & 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 = 33` — current schema version.
- `LatestMigrationVersion = 34` — 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
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 = 33
LatestMigrationVersion uint = 34
)

// MigrationTarget is a functional option that can be passed to applyMigrations
Expand Down
12 changes: 12 additions & 0 deletions db/round_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ type RoundStore interface {
ctx context.Context, arg sqlc.UpdateVTXOStatusParams,
) error

GetManualExitOptions(context.Context,
sqlc.GetManualExitOptionsParams) (
sqlc.GetManualExitOptionsRow,
error,
)

PutManualExitOptions(context.Context,
sqlc.PutManualExitOptionsParams) error

DeleteManualExitOptions(context.Context,
sqlc.DeleteManualExitOptionsParams) error

// DeleteSpendingReservation removes a spending-reservation row so the
// VTXO store can atomically update a VTXO's status and drop its
// reservation in the same transaction when it leaves SpendingState.
Expand Down
1 change: 1 addition & 0 deletions db/sqlc/migrations/000034_manual_exit_options.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE vtxo_manual_exit_options;
17 changes: 17 additions & 0 deletions db/sqlc/migrations/000034_manual_exit_options.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Early mobile builds used version 33 for manual exit options. Ensure both
-- tables exist before reading either without replacing saved exit terms or
-- a pending signed split.
CREATE TABLE IF NOT EXISTS wallet_fee_split (
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
candidate BLOB NOT NULL
);

CREATE TABLE IF NOT EXISTS vtxo_manual_exit_options (
outpoint_hash BLOB NOT NULL,
outpoint_index INTEGER NOT NULL,
sweep_script BLOB NOT NULL,
fee_rate_sat_per_vbyte BIGINT NOT NULL CHECK (fee_rate_sat_per_vbyte >= 0),
PRIMARY KEY (outpoint_hash, outpoint_index),
FOREIGN KEY (outpoint_hash, outpoint_index)
REFERENCES vtxos (outpoint_hash, outpoint_index) ON DELETE CASCADE
);
7 changes: 7 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.

16 changes: 16 additions & 0 deletions db/sqlc/queries/vtxo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,19 @@ WHERE (outpoint_hash, outpoint_index) >
(sqlc.arg(through_hash), CAST(sqlc.arg(through_index) AS INTEGER))
ORDER BY outpoint_hash, outpoint_index
LIMIT sqlc.arg(limit_count);

-- name: PutManualExitOptions :exec
INSERT INTO vtxo_manual_exit_options (
outpoint_hash, outpoint_index, sweep_script, fee_rate_sat_per_vbyte
) VALUES ($1, $2, $3, $4)
ON CONFLICT (outpoint_hash, outpoint_index) DO UPDATE SET
sweep_script = EXCLUDED.sweep_script,
fee_rate_sat_per_vbyte = EXCLUDED.fee_rate_sat_per_vbyte;

-- name: GetManualExitOptions :one
SELECT sweep_script, fee_rate_sat_per_vbyte FROM vtxo_manual_exit_options
WHERE outpoint_hash = $1 AND outpoint_index = $2;

-- name: DeleteManualExitOptions :exec
DELETE FROM vtxo_manual_exit_options
WHERE outpoint_hash = $1 AND outpoint_index = $2;
10 changes: 10 additions & 0 deletions db/sqlc/schemas/generated_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,16 @@ CREATE TABLE "vtxo_ancestry_paths" (
CHECK (path_order >= 0 AND path_order < 64)
);

CREATE TABLE vtxo_manual_exit_options (
outpoint_hash BLOB NOT NULL,
outpoint_index INTEGER NOT NULL,
sweep_script BLOB NOT NULL,
fee_rate_sat_per_vbyte BIGINT NOT NULL CHECK (fee_rate_sat_per_vbyte >= 0),
PRIMARY KEY (outpoint_hash, outpoint_index),
FOREIGN KEY (outpoint_hash, outpoint_index)
REFERENCES vtxos (outpoint_hash, outpoint_index) ON DELETE CASCADE
);

CREATE TABLE vtxos (
-- outpoint_hash and outpoint_index form the VTXO outpoint (primary key).
outpoint_hash BLOB NOT NULL,
Expand Down
63 changes: 63 additions & 0 deletions db/sqlc/vtxo.sql.go

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

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

import (
"os"
"testing"

"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/btcsuite/btclog/v2"
"github.com/lightninglabs/wavelength/lib/actormsg"
"github.com/lightninglabs/wavelength/lib/tree"
"github.com/lightninglabs/wavelength/round"
"github.com/lightninglabs/wavelength/vtxo"
"github.com/lightningnetwork/lnd/clock"
"github.com/stretchr/testify/require"
)

// TestManualExitOptionsSurviveStatusReload checks the durable handoff before
// the exit registry sees the status transition, and a subsequent safe retry.
func TestManualExitOptionsSurviveStatusReload(t *testing.T) {
t.Parallel()

store, rounds, _ := newVTXOStoreForTest(t)
ctx := t.Context()
roundID := testRoundIDDB("manual-exit-options")
testRound := createTestRound(t, roundID)
trees := make(map[round.SignerKey]*tree.Tree)
require.NoError(
t,
rounds.CommitState(
ctx, testRound, &round.InputSigSentState{
RoundID: testRound.RoundID,
ClientTrees: trees,
},
),
)
desc := createTestVTXODescriptor(t, roundID, 7)
require.NoError(t, store.SaveVTXO(ctx, desc))

options := &actormsg.ManualExitOptions{
SweepScript: []byte{
0x51,
0x20,
0xde,
0xad,
},
FeeRateSatPerVByte: 7,
}
require.NoError(
t, store.UpdateVTXOStatusWithExitOptions(
ctx, desc.Outpoint, options, false,
),
)
saved, err := store.GetVTXO(ctx, desc.Outpoint)
require.NoError(t, err)
require.Equal(t, vtxo.VTXOStatusUnilateralExit, saved.Status)
require.Equal(t, options, saved.ManualExitOptions)

require.NoError(
t, store.UpdateVTXOStatus(
ctx, desc.Outpoint, vtxo.VTXOStatusLive,
),
)
require.NoError(
t, store.UpdateVTXOStatus(
ctx, desc.Outpoint, vtxo.VTXOStatusUnilateralExit,
),
)
saved, err = store.GetVTXO(ctx, desc.Outpoint)
require.NoError(t, err)
require.Nil(t, saved.ManualExitOptions)
}

// TestManualExitMigrationAfterFeeSplit preserves a pending signed split when
// upgrading from the public schema 33.
func TestManualExitMigrationAfterFeeSplit(t *testing.T) {
t.Parallel()

ctx := t.Context()
database := NewTestDBWithVersion(t, 33)
candidate := []byte("pending signed split")
_, err := database.SaveWalletFeeSplit(ctx, candidate)
require.NoError(t, err)

require.NoError(t, database.ExecuteMigrations(TargetLatest))
saved, err := database.LoadWalletFeeSplit(ctx)
require.NoError(t, err)
require.Equal(t, candidate, saved)
}

// TestManualExitMigrationFromMobile33 recreates the wallet schema used by
// early mobile builds and checks that upgrading supplies the missing journal.
func TestManualExitMigrationFromMobile33(t *testing.T) {
t.Parallel()

ctx := t.Context()
database := NewTestSqliteDBWithVersion(t, 33)
// The original mobile migration has the same table and constraints.
migration, err := os.ReadFile(
"sqlc/migrations/000034_manual_exit_options.up.sql",
)
require.NoError(t, err)
_, err = database.ExecContext(ctx, string(migration))
require.NoError(t, err)
_, err = database.ExecContext(ctx, "DROP TABLE wallet_fee_split")
require.NoError(t, err)

stores := NewStore(
database.DB, database.Queries, database.Backend(),
btclog.Disabled,
)
clk := clock.NewDefaultClock()
rounds := stores.NewRoundStore(&chaincfg.RegressionNetParams, clk)
store := stores.NewVTXOStore(clk)
roundID := testRoundIDDB("manual-exit-migration")
testRound := createTestRound(t, roundID)
trees := make(map[round.SignerKey]*tree.Tree)
require.NoError(
t,
rounds.CommitState(
ctx, testRound, &round.InputSigSentState{
RoundID: roundID,
ClientTrees: trees,
},
),
)
desc := createTestVTXODescriptor(t, roundID, 17)
require.NoError(t, store.SaveVTXO(ctx, desc))
options := &actormsg.ManualExitOptions{
SweepScript: []byte{
0x51,
}, FeeRateSatPerVByte: 7,
}
require.NoError(
t, store.UpdateVTXOStatusWithExitOptions(
ctx, desc.Outpoint, options, false,
),
)

require.NoError(t, database.ExecuteMigrations(TargetLatest))
saved, err := store.GetVTXO(ctx, desc.Outpoint)
require.NoError(t, err)
require.Equal(t, options, saved.ManualExitOptions)
_, err = database.SaveWalletFeeSplit(ctx, []byte("new split"))
require.NoError(t, err)
}
Loading
Loading