Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mint: embed migration files #96

Merged
merged 1 commit into from
Dec 14, 2024
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
1 change: 0 additions & 1 deletion cmd/mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func configFromEnv() (*mint.Config, error) {
DerivationPathIdx: uint32(derivationPathIdx),
Port: port,
MintPath: mintPath,
DBMigrationPath: dbMigrations,
InputFeePpk: inputFeePpk,
MintInfo: mintInfo,
Limits: mintLimits,
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -827,8 +827,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -888,13 +888,13 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -903,8 +903,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
1 change: 0 additions & 1 deletion mint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type Config struct {
DerivationPathIdx uint32
Port string
MintPath string
DBMigrationPath string
InputFeePpk uint
MintInfo MintInfo
Limits MintLimits
Expand Down
2 changes: 1 addition & 1 deletion mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func LoadMint(config Config) (*Mint, error) {
return nil, err
}

db, err := sqlite.InitSQLite(path, config.DBMigrationPath)
db, err := sqlite.InitSQLite(path)
if err != nil {
return nil, fmt.Errorf("error setting up sqlite: %v", err)
}
Expand Down
20 changes: 9 additions & 11 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ import (
)

var (
ctx context.Context
bitcoind *btcdocker.Bitcoind
lnd1 *btcdocker.Lnd
lnd2 *btcdocker.Lnd
testMint *mint.Mint
dbMigrationPath = "./storage/sqlite/migrations"
ctx context.Context
bitcoind *btcdocker.Bitcoind
lnd1 *btcdocker.Lnd
lnd2 *btcdocker.Lnd
testMint *mint.Mint
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -91,7 +90,7 @@ func testMain(m *testing.M) (int, error) {
}

testMintPath := filepath.Join(".", "testmint1")
testMint, err = testutils.CreateTestMint(lnd1, testMintPath, dbMigrationPath, 0, mint.MintLimits{})
testMint, err = testutils.CreateTestMint(lnd1, testMintPath, 0, mint.MintLimits{})
if err != nil {
return 1, err
}
Expand Down Expand Up @@ -321,7 +320,7 @@ func TestSwap(t *testing.T) {

// mint with fees
mintFeesPath := filepath.Join(".", "mintfees")
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100, mint.MintLimits{})
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, 100, mint.MintLimits{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -543,7 +542,7 @@ func TestMelt(t *testing.T) {

// mint with fees
mintFeesPath := filepath.Join(".", "mintfeesmelt")
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100, mint.MintLimits{})
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, 100, mint.MintLimits{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1072,7 +1071,6 @@ func TestMintLimits(t *testing.T) {
limitsMint, err := testutils.CreateTestMint(
lnd1,
limitsMintPath,
dbMigrationPath,
100,
mintLimits,
)
Expand Down Expand Up @@ -1171,7 +1169,7 @@ func TestNUT11P2PK(t *testing.T) {
lock, _ := btcec.NewPrivateKey()

p2pkMintPath := filepath.Join(".", "p2pkmint")
p2pkMint, err := testutils.CreateTestMint(lnd1, p2pkMintPath, dbMigrationPath, 0, mint.MintLimits{})
p2pkMint, err := testutils.CreateTestMint(lnd1, p2pkMintPath, 0, mint.MintLimits{})
if err != nil {
t.Fatal(err)
}
Expand Down
56 changes: 54 additions & 2 deletions mint/storage/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package sqlite

import (
"database/sql"
"embed"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

Expand All @@ -19,19 +22,68 @@ import (
_ "github.com/mattn/go-sqlite3"
)

//go:embed migrations
var migrations embed.FS

type SQLiteDB struct {
db *sql.DB
}

func InitSQLite(path, migrationPath string) (*SQLiteDB, error) {
// create a temporary directory with the migration files.
// migration files are embedded with go:embed. These are then read
// and copied to a temporary directory.
// This is needed to pass the directory to migrate.New
func migrationsDir() (string, error) {
tempDir, err := os.MkdirTemp("", "migrations")
if err != nil {
return "", err
}

migrationFiles, err := migrations.ReadDir("migrations")
if err != nil {
return "", err
}

for _, file := range migrationFiles {
filePath := filepath.Join(tempDir, file.Name())

migrationFilePath := filepath.Join("migrations", file.Name())
migrationFile, err := migrations.Open(migrationFilePath)
if err != nil {
return "", err
}
defer migrationFile.Close()

destFile, err := os.Create(filePath)
if err != nil {
return "", err
}
defer destFile.Close()

_, err = io.Copy(destFile, migrationFile)
if err != nil {
return "", err
}
}

return tempDir, nil
}

func InitSQLite(path string) (*SQLiteDB, error) {
dbpath := filepath.Join(path, "mint.sqlite.db")
db, err := sql.Open("sqlite3", dbpath)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)

m, err := migrate.New(fmt.Sprintf("file://%s", migrationPath), fmt.Sprintf("sqlite3://%s", dbpath))
tempMigrationsDir, err := migrationsDir()
if err != nil {
return nil, err
}
defer os.RemoveAll(tempMigrationsDir)

m, err := migrate.New(fmt.Sprintf("file://%s", tempMigrationsDir), fmt.Sprintf("sqlite3://%s", dbpath))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions mint/storage/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func testMain(m *testing.M) (int, error) {
return 1, err
}

migrations := "./migrations"
db, err = InitSQLite(dbpath, migrations)
db, err = InitSQLite(dbpath)
if err != nil {
return 1, err
}
Expand Down
8 changes: 2 additions & 6 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ func MintConfig(
port string,
derivationPathIdx uint32,
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
limits mint.MintLimits,
) (*mint.Config, error) {
Expand All @@ -207,7 +206,6 @@ func MintConfig(
DerivationPathIdx: derivationPathIdx,
Port: port,
MintPath: dbpath,
DBMigrationPath: dbMigrationPath,
InputFeePpk: inputFeePpk,
Limits: limits,
LightningClient: backend,
Expand Down Expand Up @@ -269,15 +267,14 @@ func LndClient(lnd *btcdocker.Lnd, dbpath string) (*lightning.LndClient, error)
func CreateTestMint(
lnd *btcdocker.Lnd,
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
limits mint.MintLimits,
) (*mint.Mint, error) {
lndClient, err := LndClient(lnd, dbpath)
if err != nil {
return nil, err
}
config, err := MintConfig(lndClient, "", 0, dbpath, dbMigrationPath, inputFeePpk, limits)
config, err := MintConfig(lndClient, "", 0, dbpath, inputFeePpk, limits)
if err != nil {
return nil, err
}
Expand All @@ -294,10 +291,9 @@ func CreateTestMintServer(
port string,
derivationPathIdx uint32,
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
) (*mint.MintServer, error) {
config, err := MintConfig(backend, port, derivationPathIdx, dbpath, dbMigrationPath, inputFeePpk, mint.MintLimits{})
config, err := MintConfig(backend, port, derivationPathIdx, dbpath, inputFeePpk, mint.MintLimits{})
if err != nil {
return nil, err
}
Expand Down
21 changes: 10 additions & 11 deletions wallet/wallet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (
)

var (
ctx context.Context
dbMigrationPath = "../mint/storage/sqlite/migrations"
nutshellMint *testutils.NutshellMintContainer
ctx context.Context
nutshellMint *testutils.NutshellMintContainer

mintURL1 = "http://127.0.0.1:3338"
mintURL2 = "http://127.0.0.1:3339"
Expand All @@ -50,7 +49,7 @@ func testMain(m *testing.M) (int, error) {

testMintPath := filepath.Join(".", "testmint1")
fakeBackend := &lightning.FakeBackend{}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "3338", 0, testMintPath, dbMigrationPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, "3338", 0, testMintPath, 0)
if err != nil {
return 1, err
}
Expand All @@ -63,7 +62,7 @@ func testMain(m *testing.M) (int, error) {

testMintPath2 := filepath.Join(".", "testmint2")
fakeBackend2 := &lightning.FakeBackend{}
testMint2, err := testutils.CreateTestMintServer(fakeBackend2, "3339", 0, testMintPath2, dbMigrationPath, 0)
testMint2, err := testutils.CreateTestMintServer(fakeBackend2, "3339", 0, testMintPath2, 0)
if err != nil {
return 1, err
}
Expand All @@ -76,7 +75,7 @@ func testMain(m *testing.M) (int, error) {

mintFeesPath := filepath.Join(".", "testmintwithfees")
fakeBackend3 := &lightning.FakeBackend{}
mintWithFees, err := testutils.CreateTestMintServer(fakeBackend3, "8080", 0, mintFeesPath, dbMigrationPath, 100)
mintWithFees, err := testutils.CreateTestMintServer(fakeBackend3, "8080", 0, mintFeesPath, 100)
if err != nil {
return 1, err
}
Expand Down Expand Up @@ -568,7 +567,7 @@ func TestPendingProofs(t *testing.T) {
testMintPath := filepath.Join(".", "testmint2")
// Setting delay so that it marks payments as pending
fakeBackend := &lightning.FakeBackend{PaymentDelay: int64(time.Minute) * 2}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8081", 0, testMintPath, dbMigrationPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8081", 0, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -762,7 +761,7 @@ func TestKeysetRotations(t *testing.T) {
testMintPath := filepath.Join(".", "testmintkeysetrotation")
var keysetDerivationIdx uint32 = 0
fakeBackend := &lightning.FakeBackend{}
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, dbMigrationPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -776,7 +775,7 @@ func TestKeysetRotations(t *testing.T) {
var bumpKeyset = func(mint *mint.MintServer) *mint.MintServer {
testMint.Shutdown()
keysetDerivationIdx++
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, dbMigrationPath, 0)
testMint, err := testutils.CreateTestMintServer(fakeBackend, "8082", keysetDerivationIdx, testMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -986,7 +985,7 @@ func TestHTLC(t *testing.T) {
func TestSendToPubkey(t *testing.T) {
p2pkMintPath := filepath.Join(".", "p2pkmint1")
fakeBackend := &lightning.FakeBackend{}
p2pkMint, err := testutils.CreateTestMintServer(fakeBackend, "8889", 0, p2pkMintPath, dbMigrationPath, 0)
p2pkMint, err := testutils.CreateTestMintServer(fakeBackend, "8889", 0, p2pkMintPath, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -998,7 +997,7 @@ func TestSendToPubkey(t *testing.T) {

p2pkMintPath2 := filepath.Join(".", "p2pkmint2")
fakeBackend2 := &lightning.FakeBackend{}
p2pkMint2, err := testutils.CreateTestMintServer(fakeBackend2, "8890", 0, p2pkMintPath2, dbMigrationPath, 0)
p2pkMint2, err := testutils.CreateTestMintServer(fakeBackend2, "8890", 0, p2pkMintPath2, 0)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading