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.
This commit is contained in:
Boris Nagaev
2024-04-22 13:54:06 -03:00
parent b44e066424
commit 683093cf9e
2 changed files with 101 additions and 1 deletions

View File

@@ -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),

View File

@@ -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)
}