lnd/aliasmgr/aliasmgr_test.go
eugene d7d84a1f7e aliasmgr: new data store containing our, peer's aliases
This introduces the a store for managing all things alias-related.

There are two maps:

* baseToSet:
  This stores the "base" short channel id as the key. The value is
  the set of all aliases issued for this channel. The "base" SCID is
  whatever is stored in the OpenChannel's ShortChannelID member. For
  everything other than zero-conf channels, this is the confirmed SCID.
  For zero-conf channels, this is the very first alias assigned. This is
  used mostly by the Switch to retrieve a set of aliases and determine
  if it is safe to forward.

* aliasToBase:
  This stores the reverse mapping of baseToSet. Each key is an alias
  SCID and the value is the "base" SCID. This is exclusively used by
  the gossiper to determine if an alias in a ChannelUpdate our peer
  sends actually references a channel we know of.

The functions make use of the above two maps:

* AddLocalAlias:
  This persists the {alias, base} pair in the database. The baseToSet
  map is populated. The aliasToBase is optionally populated depending on
  where this function is called from. Upgrade cases, where the
  scid-alias feature bit is toggled and channels already exist, will
  not persist to the gossip map. This is mainly to simplify the tangle
  of logic that would otherwise occur.

* GetAliases:
  This fetches the set of aliases by using the passed-in base SCID. This
  is used in the Switch and other places where the alias set is needed.

* FindBaseSCID:
  This fetches the base given an alias. This is used in the gossiper to
  determine validity of a peer's ChannelUpdate that contains an alias.

* DeleteSixConfs:
  This removes the aliasToBase map entry for the given "base". This is
  used when the gossiper mappings are no longer needed, i.e. when the
  channel has six confirmations and is public.

* PutPeerAlias:
  Stores the peer's alias.

* GetPeerAlias:
  Fetches the peer's alias.

* RequestAlias:
  Generates an alias for us in the range 16000000:0:0 and
  16250000:16777215:65535
2022-08-12 16:58:16 -07:00

147 lines
3.5 KiB
Go

package aliasmgr
import (
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
// TestAliasStorePeerAlias tests that putting and retrieving a peer's alias
// works properly.
func TestAliasStorePeerAlias(t *testing.T) {
t.Parallel()
// Create the backend database and use this to create the aliasStore.
dbDir, err := ioutil.TempDir("", "aliasStore")
require.NoError(t, err)
defer os.RemoveAll(dbDir)
dbPath := filepath.Join(dbDir, "testdb")
db, err := kvdb.Create(
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
)
require.NoError(t, err)
defer db.Close()
aliasStore, err := NewManager(db)
require.NoError(t, err)
var chanID1 [32]byte
_, err = rand.Read(chanID1[:])
require.NoError(t, err)
// Test that we can put the (chanID, alias) mapping in the database.
// Also check that we retrieve exactly what we put in.
err = aliasStore.PutPeerAlias(chanID1, startingAlias)
require.NoError(t, err)
storedAlias, err := aliasStore.GetPeerAlias(chanID1)
require.NoError(t, err)
require.Equal(t, startingAlias, storedAlias)
}
// TestAliasStoreRequest tests that the aliasStore delivers the expected SCID.
func TestAliasStoreRequest(t *testing.T) {
t.Parallel()
// Create the backend database and use this to create the aliasStore.
dbDir, err := ioutil.TempDir("", "aliasStore")
require.NoError(t, err)
defer os.RemoveAll(dbDir)
dbPath := filepath.Join(dbDir, "testdb")
db, err := kvdb.Create(
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
)
require.NoError(t, err)
defer db.Close()
aliasStore, err := NewManager(db)
require.NoError(t, err)
// We'll assert that the very first alias we receive is startingAlias.
alias1, err := aliasStore.RequestAlias()
require.NoError(t, err)
require.Equal(t, startingAlias, alias1)
// The next alias should be the result of passing in startingAlias to
// getNextScid.
nextAlias := getNextScid(alias1)
alias2, err := aliasStore.RequestAlias()
require.NoError(t, err)
require.Equal(t, nextAlias, alias2)
}
// TestGetNextScid tests that given a current lnwire.ShortChannelID,
// getNextScid returns the expected alias to use next.
func TestGetNextScid(t *testing.T) {
tests := []struct {
name string
current lnwire.ShortChannelID
expected lnwire.ShortChannelID
}{
{
name: "starting alias",
current: startingAlias,
expected: lnwire.ShortChannelID{
BlockHeight: uint32(startingBlockHeight),
TxIndex: 0,
TxPosition: 1,
},
},
{
name: "txposition rollover",
current: lnwire.ShortChannelID{
BlockHeight: 16_100_000,
TxIndex: 15,
TxPosition: 65535,
},
expected: lnwire.ShortChannelID{
BlockHeight: 16_100_000,
TxIndex: 16,
TxPosition: 0,
},
},
{
name: "txindex max no rollover",
current: lnwire.ShortChannelID{
BlockHeight: 16_100_000,
TxIndex: 16777215,
TxPosition: 15,
},
expected: lnwire.ShortChannelID{
BlockHeight: 16_100_000,
TxIndex: 16777215,
TxPosition: 16,
},
},
{
name: "txindex rollover",
current: lnwire.ShortChannelID{
BlockHeight: 16_100_000,
TxIndex: 16777215,
TxPosition: 65535,
},
expected: lnwire.ShortChannelID{
BlockHeight: 16_100_001,
TxIndex: 0,
TxPosition: 0,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
nextScid := getNextScid(test.current)
require.Equal(t, test.expected, nextScid)
})
}
}