watchtower: make use of the new AddressIterator
This commit upgrades the wtclient package to make use of the new `AddressIterator`. It does so by first creating new `Tower` and `ClientSession` types. The new `Tower` type has an `AddressIterator` instead of a list of addresses. The `ClientSession` type contains a `Tower`.
This commit is contained in:
13 files changed
+274
-184
No files matched your search
@@ -33,31 +33,38 @@ func randAddr(t *testing.T) net.Addr {
|
||||
}
|
||||
}
|
||||
|
||||
func randTower(t *testing.T) *wtdb.Tower {
|
||||
func randTower(t *testing.T) *Tower {
|
||||
t.Helper()
|
||||
|
||||
priv, err := btcec.NewPrivateKey()
|
||||
require.NoError(t, err, "unable to create private key")
|
||||
pubKey := priv.PubKey()
|
||||
return &wtdb.Tower{
|
||||
addrs, err := newAddressIterator(randAddr(t))
|
||||
require.NoError(t, err)
|
||||
|
||||
return &Tower{
|
||||
ID: wtdb.TowerID(rand.Uint64()),
|
||||
IdentityKey: pubKey,
|
||||
Addresses: []net.Addr{randAddr(t)},
|
||||
Addresses: addrs,
|
||||
}
|
||||
}
|
||||
|
||||
func copyTower(tower *wtdb.Tower) *wtdb.Tower {
|
||||
t := &wtdb.Tower{
|
||||
func copyTower(t *testing.T, tower *Tower) *Tower {
|
||||
t.Helper()
|
||||
|
||||
addrs := tower.Addresses.GetAll()
|
||||
addrIterator, err := newAddressIterator(addrs...)
|
||||
require.NoError(t, err)
|
||||
|
||||
return &Tower{
|
||||
ID: tower.ID,
|
||||
IdentityKey: tower.IdentityKey,
|
||||
Addresses: make([]net.Addr, len(tower.Addresses)),
|
||||
Addresses: addrIterator,
|
||||
}
|
||||
copy(t.Addresses, tower.Addresses)
|
||||
return t
|
||||
}
|
||||
|
||||
func assertActiveCandidate(t *testing.T, i TowerCandidateIterator,
|
||||
c *wtdb.Tower, active bool) {
|
||||
func assertActiveCandidate(t *testing.T, i TowerCandidateIterator, c *Tower,
|
||||
active bool) {
|
||||
|
||||
t.Helper()
|
||||
|
||||
@@ -71,12 +78,14 @@ func assertActiveCandidate(t *testing.T, i TowerCandidateIterator,
|
||||
c.ID)
|
||||
}
|
||||
|
||||
func assertNextCandidate(t *testing.T, i TowerCandidateIterator, c *wtdb.Tower) {
|
||||
func assertNextCandidate(t *testing.T, i TowerCandidateIterator, c *Tower) {
|
||||
t.Helper()
|
||||
|
||||
tower, err := i.Next()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, c, tower)
|
||||
require.True(t, tower.IdentityKey.IsEqual(c.IdentityKey))
|
||||
require.Equal(t, tower.ID, c.ID)
|
||||
require.Equal(t, tower.Addresses.GetAll(), c.Addresses.GetAll())
|
||||
}
|
||||
|
||||
// TestTowerCandidateIterator asserts the internal state of a
|
||||
@@ -88,13 +97,13 @@ func TestTowerCandidateIterator(t *testing.T) {
|
||||
// towers. We'll use copies of these towers within the iterator to
|
||||
// ensure the iterator properly updates the state of its candidates.
|
||||
const numTowers = 4
|
||||
towers := make([]*wtdb.Tower, 0, numTowers)
|
||||
towers := make([]*Tower, 0, numTowers)
|
||||
for i := 0; i < numTowers; i++ {
|
||||
towers = append(towers, randTower(t))
|
||||
}
|
||||
towerCopies := make([]*wtdb.Tower, 0, numTowers)
|
||||
towerCopies := make([]*Tower, 0, numTowers)
|
||||
for _, tower := range towers {
|
||||
towerCopies = append(towerCopies, copyTower(tower))
|
||||
towerCopies = append(towerCopies, copyTower(t, tower))
|
||||
}
|
||||
towerIterator := newTowerListIterator(towerCopies...)
|
||||
|
||||
@@ -112,13 +121,13 @@ func TestTowerCandidateIterator(t *testing.T) {
|
||||
towerIterator.Reset()
|
||||
|
||||
// We'll then attempt to test the RemoveCandidate behavior of the
|
||||
// iterator. We'll remove the address of the first tower, which should
|
||||
// result in it not having any addresses left, but still being an active
|
||||
// candidate.
|
||||
// iterator. We'll attempt to remove the address of the first tower,
|
||||
// which should result in an error due to it being the last address of
|
||||
// the tower.
|
||||
firstTower := towers[0]
|
||||
firstTowerAddr := firstTower.Addresses[0]
|
||||
firstTower.RemoveAddress(firstTowerAddr)
|
||||
towerIterator.RemoveCandidate(firstTower.ID, firstTowerAddr)
|
||||
firstTowerAddr := firstTower.Addresses.Peek()
|
||||
err = towerIterator.RemoveCandidate(firstTower.ID, firstTowerAddr)
|
||||
require.ErrorIs(t, err, wtdb.ErrLastTowerAddr)
|
||||
assertActiveCandidate(t, towerIterator, firstTower, true)
|
||||
assertNextCandidate(t, towerIterator, firstTower)
|
||||
|
||||
@@ -126,7 +135,8 @@ func TestTowerCandidateIterator(t *testing.T) {
|
||||
// not providing the optional address. Since it's been removed, we
|
||||
// should expect to see the third tower next.
|
||||
secondTower, thirdTower := towers[1], towers[2]
|
||||
towerIterator.RemoveCandidate(secondTower.ID, nil)
|
||||
err = towerIterator.RemoveCandidate(secondTower.ID, nil)
|
||||
require.NoError(t, err)
|
||||
assertActiveCandidate(t, towerIterator, secondTower, false)
|
||||
assertNextCandidate(t, towerIterator, thirdTower)
|
||||
|
||||
@@ -135,7 +145,7 @@ func TestTowerCandidateIterator(t *testing.T) {
|
||||
// iterator, but the new address should be.
|
||||
fourthTower := towers[3]
|
||||
assertActiveCandidate(t, towerIterator, fourthTower, true)
|
||||
fourthTower.AddAddress(randAddr(t))
|
||||
fourthTower.Addresses.Add(randAddr(t))
|
||||
towerIterator.AddCandidate(fourthTower)
|
||||
assertNextCandidate(t, towerIterator, fourthTower)
|
||||
|
||||
|
||||
Reference in new issue
Block a user