Skip to content

Commit

Permalink
l402: migration of lsat.token* files
Browse files Browse the repository at this point in the history
This is needed to preserve old identifiers of nodes that have ever paid
for a Loop or Pool token.
  • Loading branch information
starius committed Apr 23, 2024
1 parent b44e066 commit 683093c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
31 changes: 31 additions & 0 deletions l402/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
71 changes: 70 additions & 1 deletion l402/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}

0 comments on commit 683093c

Please sign in to comment.