From a50cb81a24c5e057aedaca1d704f4ea8cd6ef6fe Mon Sep 17 00:00:00 2001 From: elnosh Date: Fri, 13 Dec 2024 09:40:58 -0500 Subject: [PATCH] mint - embed migration files and remove it from config --- cmd/mint/mint.go | 1 - go.mod | 10 +++--- go.sum | 20 +++++------ mint/config.go | 1 - mint/mint.go | 2 +- mint/mint_integration_test.go | 20 +++++------ mint/storage/sqlite/sqlite.go | 56 ++++++++++++++++++++++++++++-- mint/storage/sqlite/sqlite_test.go | 3 +- testutils/utils.go | 8 ++--- wallet/wallet_integration_test.go | 21 ++++++----- 10 files changed, 92 insertions(+), 50 deletions(-) diff --git a/cmd/mint/mint.go b/cmd/mint/mint.go index 00b082a..52f430c 100644 --- a/cmd/mint/mint.go +++ b/cmd/mint/mint.go @@ -187,7 +187,6 @@ func configFromEnv() (*mint.Config, error) { DerivationPathIdx: uint32(derivationPathIdx), Port: port, MintPath: mintPath, - DBMigrationPath: dbMigrations, InputFeePpk: inputFeePpk, MintInfo: mintInfo, Limits: mintLimits, diff --git a/go.mod b/go.mod index c66fcdc..46eb8ad 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b63c3af..6b054b0 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/mint/config.go b/mint/config.go index e084b61..be8c8df 100644 --- a/mint/config.go +++ b/mint/config.go @@ -19,7 +19,6 @@ type Config struct { DerivationPathIdx uint32 Port string MintPath string - DBMigrationPath string InputFeePpk uint MintInfo MintInfo Limits MintLimits diff --git a/mint/mint.go b/mint/mint.go index d0a403d..7abdc01 100644 --- a/mint/mint.go +++ b/mint/mint.go @@ -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) } diff --git a/mint/mint_integration_test.go b/mint/mint_integration_test.go index b27bd76..4968b80 100644 --- a/mint/mint_integration_test.go +++ b/mint/mint_integration_test.go @@ -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) { @@ -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 } @@ -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) } @@ -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) } @@ -1072,7 +1071,6 @@ func TestMintLimits(t *testing.T) { limitsMint, err := testutils.CreateTestMint( lnd1, limitsMintPath, - dbMigrationPath, 100, mintLimits, ) @@ -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) } diff --git a/mint/storage/sqlite/sqlite.go b/mint/storage/sqlite/sqlite.go index b93fcdd..e41a0fb 100644 --- a/mint/storage/sqlite/sqlite.go +++ b/mint/storage/sqlite/sqlite.go @@ -2,9 +2,12 @@ package sqlite import ( "database/sql" + "embed" "encoding/hex" "errors" "fmt" + "io" + "os" "path/filepath" "strings" @@ -19,11 +22,54 @@ 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 { @@ -31,7 +77,13 @@ func InitSQLite(path, migrationPath string) (*SQLiteDB, error) { } 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 } diff --git a/mint/storage/sqlite/sqlite_test.go b/mint/storage/sqlite/sqlite_test.go index cc8e11c..4b40279 100644 --- a/mint/storage/sqlite/sqlite_test.go +++ b/mint/storage/sqlite/sqlite_test.go @@ -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 } diff --git a/testutils/utils.go b/testutils/utils.go index a6e23e5..1f7f20b 100644 --- a/testutils/utils.go +++ b/testutils/utils.go @@ -194,7 +194,6 @@ func MintConfig( port string, derivationPathIdx uint32, dbpath string, - dbMigrationPath string, inputFeePpk uint, limits mint.MintLimits, ) (*mint.Config, error) { @@ -207,7 +206,6 @@ func MintConfig( DerivationPathIdx: derivationPathIdx, Port: port, MintPath: dbpath, - DBMigrationPath: dbMigrationPath, InputFeePpk: inputFeePpk, Limits: limits, LightningClient: backend, @@ -269,7 +267,6 @@ 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) { @@ -277,7 +274,7 @@ func CreateTestMint( 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 } @@ -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 } diff --git a/wallet/wallet_integration_test.go b/wallet/wallet_integration_test.go index 4a0ab05..21f377f 100644 --- a/wallet/wallet_integration_test.go +++ b/wallet/wallet_integration_test.go @@ -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" @@ -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 } @@ -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 } @@ -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 } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) }