From 683093cf9e166043f17ff53767cdfafa09b01ab5 Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Mon, 22 Apr 2024 13:54:06 -0300 Subject: [PATCH] l402: migration of lsat.token* files This is needed to preserve old identifiers of nodes that have ever paid for a Loop or Pool token. --- l402/store.go | 31 ++++++++++++++++++++ l402/store_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/l402/store.go b/l402/store.go index ac7d0c1..c7e3c07 100644 --- a/l402/store.go +++ b/l402/store.go @@ -69,6 +69,37 @@ func NewFileStore(storeDir string) (*FileStore, error) { } } + // If token file and pending token file exist under old names and + // do not exist under current names, rename them. + renames := []struct { + oldName, newName string + }{ + { + oldName: "lsat.token", + newName: storeFileName, + }, + { + oldName: "lsat.token.pending", + newName: storeFileNamePending, + }, + } + for _, rename := range renames { + oldPath := filepath.Join(storeDir, rename.oldName) + newPath := filepath.Join(storeDir, rename.newName) + if _, err := os.Stat(newPath); !errors.Is(err, os.ErrNotExist) { + // New file already exists, skipping. + continue + } + if _, err := os.Stat(oldPath); err != nil { + // Failed to stat old file, skipping. + continue + } + if err := os.Rename(oldPath, newPath); err != nil { + return nil, fmt.Errorf("failed to rename %s to %s: %w", + oldPath, newPath, err) + } + } + return &FileStore{ fileName: filepath.Join(storeDir, storeFileName), fileNamePending: filepath.Join(storeDir, storeFileNamePending), diff --git a/l402/store_test.go b/l402/store_test.go index c62cc6d..2e95a7f 100644 --- a/l402/store_test.go +++ b/l402/store_test.go @@ -6,9 +6,10 @@ import ( "testing" "github.com/lightningnetwork/lnd/lntypes" + "github.com/stretchr/testify/require" ) -// TestStore tests the basic functionality of the file based store. +// TestFileStore tests the basic functionality of the file based store. func TestFileStore(t *testing.T) { t.Parallel() @@ -128,3 +129,71 @@ func TestFileStore(t *testing.T) { errNoReplace) } } + +// TestFileStorePendingMigration tests migration from lsat.token.pending +// to l402.token.pending. +func TestFileStorePendingMigration(t *testing.T) { + t.Parallel() + + tempDirName := t.TempDir() + + pendingToken := &Token{ + Preimage: zeroPreimage, + baseMac: makeMac(), + } + + store, err := NewFileStore(tempDirName) + require.NoError(t, err) + + // Write a pending token. + require.NoError(t, store.StoreToken(pendingToken)) + + // Rename file on disk to lsat.token.pending to emulate + // a pre-migration state. + newPath := filepath.Join(tempDirName, storeFileNamePending) + oldPath := filepath.Join(tempDirName, "lsat.token.pending") + require.NoError(t, os.Rename(newPath, oldPath)) + + // Open the same directory again. + store1, err := NewFileStore(tempDirName) + require.NoError(t, err) + + // Read the token and compare its value. + token, err := store1.CurrentToken() + require.NoError(t, err) + require.Equal(t, pendingToken.baseMac, token.baseMac) +} + +// TestFileStoreMigration tests migration from lsat.token to l402.token. +func TestFileStoreMigration(t *testing.T) { + t.Parallel() + + tempDirName := t.TempDir() + + paidPreimage := lntypes.Preimage{1, 2, 3, 4, 5} + paidToken := &Token{ + Preimage: paidPreimage, + baseMac: makeMac(), + } + + store, err := NewFileStore(tempDirName) + require.NoError(t, err) + + // Write a token. + require.NoError(t, store.StoreToken(paidToken)) + + // Rename file on disk to lsat.token.pending to emulate + // a pre-migration state. + newPath := filepath.Join(tempDirName, storeFileName) + oldPath := filepath.Join(tempDirName, "lsat.token") + require.NoError(t, os.Rename(newPath, oldPath)) + + // Open the same directory again. + store1, err := NewFileStore(tempDirName) + require.NoError(t, err) + + // Read the token and compare its value. + token, err := store1.CurrentToken() + require.NoError(t, err) + require.Equal(t, paidToken.baseMac, token.baseMac) +}