2023-05-17 17:07:58 +00:00
|
|
|
package loopdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
2023-08-07 09:04:32 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-05-17 17:07:58 +00:00
|
|
|
"testing"
|
2023-08-07 09:04:32 +00:00
|
|
|
"time"
|
2023-05-17 17:07:58 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
sqlite_migrate "github.com/golang-migrate/migrate/v4/database/sqlite"
|
|
|
|
"github.com/lightninglabs/loop/loopdb/sqlc"
|
2023-11-07 23:56:57 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2023-05-17 17:07:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
_ "modernc.org/sqlite" // Register relevant drivers.
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sqliteOptionPrefix is the string prefix sqlite uses to set various
|
|
|
|
// options. This is used in the following format:
|
|
|
|
// * sqliteOptionPrefix || option_name = option_value.
|
|
|
|
sqliteOptionPrefix = "_pragma"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SqliteConfig holds all the config arguments needed to interact with our
|
|
|
|
// sqlite DB.
|
|
|
|
type SqliteConfig struct {
|
|
|
|
// SkipMigrations if true, then all the tables will be created on start
|
|
|
|
// up if they don't already exist.
|
|
|
|
SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
|
|
|
|
|
|
|
|
// DatabaseFileName is the full file path where the database file can be
|
|
|
|
// found.
|
|
|
|
DatabaseFileName string `long:"dbfile" description:"The full path to the database."`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SqliteSwapStore is a sqlite3 based database for the loop daemon.
|
|
|
|
type SqliteSwapStore struct {
|
|
|
|
cfg *SqliteConfig
|
|
|
|
|
|
|
|
*BaseDB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSqliteStore attempts to open a new sqlite database based on the passed
|
|
|
|
// config.
|
|
|
|
func NewSqliteStore(cfg *SqliteConfig, network *chaincfg.Params) (*SqliteSwapStore, error) {
|
|
|
|
// The set of pragma options are accepted using query options. For now
|
|
|
|
// we only want to ensure that foreign key constraints are properly
|
|
|
|
// enforced.
|
|
|
|
pragmaOptions := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "foreign_keys",
|
|
|
|
value: "on",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "journal_mode",
|
|
|
|
value: "WAL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "busy_timeout",
|
|
|
|
value: "5000",
|
|
|
|
},
|
2024-03-04 15:23:50 +00:00
|
|
|
{
|
|
|
|
// With the WAL mode, this ensures that we also do an
|
|
|
|
// extra WAL sync after each transaction. The normal
|
|
|
|
// sync mode skips this and gives better performance,
|
|
|
|
// but risks durability.
|
|
|
|
name: "synchronous",
|
|
|
|
value: "full",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This is used to ensure proper durability for users
|
|
|
|
// running on Mac OS. It uses the correct fsync system
|
|
|
|
// call to ensure items are fully flushed to disk.
|
|
|
|
name: "fullfsync",
|
|
|
|
value: "true",
|
|
|
|
},
|
2023-05-17 17:07:58 +00:00
|
|
|
}
|
|
|
|
sqliteOptions := make(url.Values)
|
|
|
|
for _, option := range pragmaOptions {
|
|
|
|
sqliteOptions.Add(
|
|
|
|
sqliteOptionPrefix,
|
|
|
|
fmt.Sprintf("%v=%v", option.name, option.value),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the DSN which is just the database file name, appended
|
|
|
|
// with the series of pragma options as a query URL string. For more
|
|
|
|
// details on the formatting here, see the modernc.org/sqlite docs:
|
|
|
|
// https://pkg.go.dev/modernc.org/sqlite#Driver.Open.
|
|
|
|
dsn := fmt.Sprintf(
|
|
|
|
"%v?%v", cfg.DatabaseFileName, sqliteOptions.Encode(),
|
|
|
|
)
|
|
|
|
db, err := sql.Open("sqlite", dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfg.SkipMigrations {
|
|
|
|
// Now that the database is open, populate the database with
|
|
|
|
// our set of schemas based on our embedded in-memory file
|
|
|
|
// system.
|
|
|
|
//
|
|
|
|
// First, we'll need to open up a new migration instance for
|
|
|
|
// our current target database: sqlite.
|
|
|
|
driver, err := sqlite_migrate.WithInstance(
|
|
|
|
db, &sqlite_migrate.Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = applyMigrations(
|
|
|
|
sqlSchemas, driver, "sqlc/migrations", "sqlc",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queries := sqlc.New(db)
|
|
|
|
|
2023-08-07 09:04:32 +00:00
|
|
|
baseDB := &BaseDB{
|
|
|
|
DB: db,
|
|
|
|
Queries: queries,
|
|
|
|
network: network,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix faulty timestamps in the database.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-08-08 13:05:52 +00:00
|
|
|
err = baseDB.FixFaultyTimestamps(ctx)
|
2023-08-07 09:04:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to fix faulty timestamps: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:07:58 +00:00
|
|
|
return &SqliteSwapStore{
|
2023-08-07 09:04:32 +00:00
|
|
|
cfg: cfg,
|
|
|
|
BaseDB: baseDB,
|
2023-05-17 17:07:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestSqliteDB is a helper function that creates an SQLite database for
|
|
|
|
// testing.
|
|
|
|
func NewTestSqliteDB(t *testing.T) *SqliteSwapStore {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
t.Logf("Creating new SQLite DB for testing")
|
|
|
|
|
|
|
|
dbFileName := filepath.Join(t.TempDir(), "tmp.db")
|
2023-08-07 09:04:32 +00:00
|
|
|
|
2023-05-17 17:07:58 +00:00
|
|
|
sqlDB, err := NewSqliteStore(&SqliteConfig{
|
|
|
|
DatabaseFileName: dbFileName,
|
|
|
|
SkipMigrations: false,
|
|
|
|
}, &chaincfg.MainNetParams)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, sqlDB.DB.Close())
|
|
|
|
})
|
|
|
|
|
|
|
|
return sqlDB
|
|
|
|
}
|
|
|
|
|
|
|
|
// BaseDB is the base database struct that each implementation can embed to
|
|
|
|
// gain some common functionality.
|
|
|
|
type BaseDB struct {
|
|
|
|
network *chaincfg.Params
|
|
|
|
|
|
|
|
*sql.DB
|
|
|
|
|
|
|
|
*sqlc.Queries
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginTx wraps the normal sql specific BeginTx method with the TxOptions
|
|
|
|
// interface. This interface is then mapped to the concrete sql tx options
|
|
|
|
// struct.
|
|
|
|
func (db *BaseDB) BeginTx(ctx context.Context,
|
|
|
|
opts TxOptions) (*sql.Tx, error) {
|
|
|
|
|
|
|
|
sqlOptions := sql.TxOptions{
|
|
|
|
ReadOnly: opts.ReadOnly(),
|
|
|
|
}
|
|
|
|
return db.DB.BeginTx(ctx, &sqlOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecTx is a wrapper for txBody to abstract the creation and commit of a db
|
2024-06-19 03:39:52 +00:00
|
|
|
// transaction. The db transaction is embedded in a `*sqlc.Queries` that
|
2023-05-17 17:07:58 +00:00
|
|
|
// txBody needs to use when executing each one of the queries that need to be
|
|
|
|
// applied atomically.
|
|
|
|
func (db *BaseDB) ExecTx(ctx context.Context, txOptions TxOptions,
|
|
|
|
txBody func(*sqlc.Queries) error) error {
|
|
|
|
|
|
|
|
// Create the db transaction.
|
|
|
|
tx, err := db.BeginTx(ctx, txOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback is safe to call even if the tx is already closed, so if
|
|
|
|
// the tx commits successfully, this is a no-op.
|
|
|
|
defer tx.Rollback() //nolint: errcheck
|
|
|
|
|
|
|
|
if err := txBody(db.Queries.WithTx(tx)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction.
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 09:04:32 +00:00
|
|
|
// FixFaultyTimestamps fixes faulty timestamps in the database, caused
|
|
|
|
// by using milliseconds instead of seconds as the publication deadline.
|
2024-06-20 14:58:46 +00:00
|
|
|
func (db *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
|
2023-08-07 09:04:32 +00:00
|
|
|
// Manually fetch all the loop out swaps.
|
2024-06-20 14:58:46 +00:00
|
|
|
rows, err := db.DB.QueryContext(
|
2023-11-07 23:56:57 +00:00
|
|
|
ctx, "SELECT swap_hash, swap_invoice, publication_deadline FROM loopout_swaps",
|
2023-08-07 09:04:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-28 11:16:01 +00:00
|
|
|
defer func() {
|
|
|
|
_ = rows.Close()
|
|
|
|
}()
|
2023-08-07 09:04:32 +00:00
|
|
|
|
|
|
|
// Parse the rows into a struct. We need to do this manually because
|
|
|
|
// the sqlite driver will fail on faulty timestamps.
|
|
|
|
type LoopOutRow struct {
|
|
|
|
Hash []byte `json:"swap_hash"`
|
2023-11-07 23:56:57 +00:00
|
|
|
SwapInvoice string `json:"swap_invoice"`
|
2023-08-07 09:04:32 +00:00
|
|
|
PublicationDeadline string `json:"publication_deadline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var loopOutSwaps []LoopOutRow
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var swap LoopOutRow
|
|
|
|
err := rows.Scan(
|
2023-11-07 23:56:57 +00:00
|
|
|
&swap.Hash, &swap.SwapInvoice, &swap.PublicationDeadline,
|
2023-08-07 09:04:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
loopOutSwaps = append(loopOutSwaps, swap)
|
|
|
|
}
|
|
|
|
|
2023-09-28 11:16:01 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-20 14:58:46 +00:00
|
|
|
tx, err := db.BeginTx(ctx, &SqliteTxOptions{})
|
2023-08-07 09:04:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-28 11:16:01 +00:00
|
|
|
defer func() {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
2023-08-07 09:04:32 +00:00
|
|
|
|
|
|
|
for _, swap := range loopOutSwaps {
|
2023-08-22 18:08:37 +00:00
|
|
|
// Get the year of the timestamp.
|
|
|
|
year, err := getTimeStampYear(swap.PublicationDeadline)
|
2023-08-07 09:04:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:08:37 +00:00
|
|
|
// Skip if the year is not in the future.
|
|
|
|
thisYear := time.Now().Year()
|
2023-11-07 23:56:57 +00:00
|
|
|
if year > 2020 && year <= thisYear {
|
2023-08-07 09:04:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-20 14:58:46 +00:00
|
|
|
payReq, err := zpay32.Decode(swap.SwapInvoice, db.network)
|
2023-08-22 18:08:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-07 23:56:57 +00:00
|
|
|
fixedTime := payReq.Timestamp.Add(time.Minute * 30)
|
2023-08-22 18:08:37 +00:00
|
|
|
|
2023-08-07 09:04:32 +00:00
|
|
|
// Update the faulty time to a valid time.
|
|
|
|
_, err = tx.ExecContext(
|
|
|
|
ctx, `
|
|
|
|
UPDATE
|
|
|
|
loopout_swaps
|
|
|
|
SET
|
|
|
|
publication_deadline = $1
|
|
|
|
WHERE
|
|
|
|
swap_hash = $2;
|
|
|
|
`,
|
2023-08-22 18:08:37 +00:00
|
|
|
fixedTime, swap.Hash,
|
2023-08-07 09:04:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:07:58 +00:00
|
|
|
// TxOptions represents a set of options one can use to control what type of
|
2024-01-02 05:03:38 +00:00
|
|
|
// database transaction is created. Transaction can whether be read or write.
|
2023-05-17 17:07:58 +00:00
|
|
|
type TxOptions interface {
|
|
|
|
// ReadOnly returns true if the transaction should be read only.
|
|
|
|
ReadOnly() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SqliteTxOptions defines the set of db txn options the KeyStore
|
|
|
|
// understands.
|
|
|
|
type SqliteTxOptions struct {
|
|
|
|
// readOnly governs if a read only transaction is needed or not.
|
|
|
|
readOnly bool
|
|
|
|
}
|
|
|
|
|
2024-06-14 03:05:27 +00:00
|
|
|
// NewSqlReadOpts returns a new KeyStoreTxOptions instance that triggers a read
|
2023-05-17 17:07:58 +00:00
|
|
|
// transaction.
|
|
|
|
func NewSqlReadOpts() *SqliteTxOptions {
|
|
|
|
return &SqliteTxOptions{
|
|
|
|
readOnly: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-14 03:05:27 +00:00
|
|
|
// NewSqlWriteOpts returns a new KeyStoreTxOptions instance that triggers a
|
|
|
|
// write (regular) transaction.
|
|
|
|
func NewSqlWriteOpts() *SqliteTxOptions {
|
|
|
|
return &SqliteTxOptions{
|
|
|
|
readOnly: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:07:58 +00:00
|
|
|
// ReadOnly returns true if the transaction should be read only.
|
|
|
|
//
|
2023-09-28 11:16:01 +00:00
|
|
|
// NOTE: This implements the TxOptions interface.
|
2023-05-17 17:07:58 +00:00
|
|
|
func (r *SqliteTxOptions) ReadOnly() bool {
|
|
|
|
return r.readOnly
|
|
|
|
}
|
2023-08-07 09:04:32 +00:00
|
|
|
|
2023-08-22 06:13:54 +00:00
|
|
|
// getTimeStampYear returns the year of a timestamp string.
|
|
|
|
func getTimeStampYear(dateTimeStr string) (int, error) {
|
|
|
|
parts := strings.Split(dateTimeStr, "-")
|
|
|
|
if len(parts) < 1 {
|
|
|
|
return 0, fmt.Errorf("invalid timestamp format: %v",
|
|
|
|
dateTimeStr)
|
2023-08-07 09:04:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 06:13:54 +00:00
|
|
|
year, err := strconv.Atoi(parts[0])
|
2023-08-07 09:04:32 +00:00
|
|
|
if err != nil {
|
2023-08-22 06:13:54 +00:00
|
|
|
return 0, fmt.Errorf("unable to parse year: %v", err)
|
2023-08-07 09:04:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 06:13:54 +00:00
|
|
|
return year, nil
|
2023-08-07 09:04:32 +00:00
|
|
|
}
|