mirror of
https://github.com/lightninglabs/aperture.git
synced 2025-12-17 00:54:20 +01:00
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:
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user