multi: thread context through to AddrsForNode
This commit is contained in:
15 files changed
+88
-64
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
@@ -13,7 +14,8 @@ type AddrSource interface {
|
||||
// AddrsForNode returns all known addresses for the target node public
|
||||
// key. The returned boolean must indicate if the given node is unknown
|
||||
// to the backing source.
|
||||
AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr, error)
|
||||
AddrsForNode(ctx context.Context,
|
||||
nodePub *btcec.PublicKey) (bool, []net.Addr, error)
|
||||
}
|
||||
|
||||
// multiAddrSource is an implementation of AddrSource which gathers all the
|
||||
@@ -38,8 +40,8 @@ func NewMultiAddrSource(sources ...AddrSource) AddrSource {
|
||||
// node.
|
||||
//
|
||||
// NOTE: this implements the AddrSource interface.
|
||||
func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) (bool,
|
||||
[]net.Addr, error) {
|
||||
func (c *multiAddrSource) AddrsForNode(ctx context.Context,
|
||||
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
|
||||
|
||||
if len(c.sources) == 0 {
|
||||
return false, nil, errors.New("no address sources")
|
||||
@@ -55,7 +57,7 @@ func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) (bool,
|
||||
// Iterate over all the address sources and query each one for the
|
||||
// addresses it has for the node in question.
|
||||
for _, src := range c.sources {
|
||||
isKnown, addrs, err := src.AddrsForNode(nodePub)
|
||||
isKnown, addrs, err := src.AddrsForNode(ctx, nodePub)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
@@ -19,6 +20,7 @@ var (
|
||||
// deduplicates the results of a set of AddrSource implementations.
|
||||
func TestMultiAddrSource(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
|
||||
var pk1 = newTestPubKey(t)
|
||||
|
||||
@@ -35,12 +37,12 @@ func TestMultiAddrSource(t *testing.T) {
|
||||
})
|
||||
|
||||
// Let source 1 know of 2 addresses (addr 1 and 2) for node 1.
|
||||
src1.On("AddrsForNode", pk1).Return(
|
||||
src1.On("AddrsForNode", ctx, pk1).Return(
|
||||
true, []net.Addr{addr1, addr2}, nil,
|
||||
).Once()
|
||||
|
||||
// Let source 2 know of 2 addresses (addr 2 and 3) for node 1.
|
||||
src2.On("AddrsForNode", pk1).Return(
|
||||
src2.On("AddrsForNode", ctx, pk1).Return(
|
||||
true, []net.Addr{addr2, addr3}, nil,
|
||||
[]net.Addr{addr2, addr3}, nil,
|
||||
).Once()
|
||||
@@ -51,7 +53,7 @@ func TestMultiAddrSource(t *testing.T) {
|
||||
|
||||
// Query it for the addresses known for node 1. The results
|
||||
// should contain addr 1, 2 and 3.
|
||||
known, addrs, err := multiSrc.AddrsForNode(pk1)
|
||||
known, addrs, err := multiSrc.AddrsForNode(ctx, pk1)
|
||||
require.NoError(t, err)
|
||||
require.True(t, known)
|
||||
require.ElementsMatch(t, addrs, []net.Addr{addr1, addr2, addr3})
|
||||
@@ -70,10 +72,10 @@ func TestMultiAddrSource(t *testing.T) {
|
||||
})
|
||||
|
||||
// Let source 1 know of address 1 for node 1.
|
||||
src1.On("AddrsForNode", pk1).Return(
|
||||
src1.On("AddrsForNode", ctx, pk1).Return(
|
||||
true, []net.Addr{addr1}, nil,
|
||||
).Once()
|
||||
src2.On("AddrsForNode", pk1).Return(false, nil, nil).Once()
|
||||
src2.On("AddrsForNode", ctx, pk1).Return(false, nil, nil).Once()
|
||||
|
||||
// Create a multi-addr source that consists of both source 1
|
||||
// and 2.
|
||||
@@ -81,7 +83,7 @@ func TestMultiAddrSource(t *testing.T) {
|
||||
|
||||
// Query it for the addresses known for node 1. The results
|
||||
// should contain addr 1.
|
||||
known, addrs, err := multiSrc.AddrsForNode(pk1)
|
||||
known, addrs, err := multiSrc.AddrsForNode(ctx, pk1)
|
||||
require.NoError(t, err)
|
||||
require.True(t, known)
|
||||
require.ElementsMatch(t, addrs, []net.Addr{addr1})
|
||||
@@ -103,13 +105,13 @@ func TestMultiAddrSource(t *testing.T) {
|
||||
// and 2. Neither source known of node 1.
|
||||
multiSrc := NewMultiAddrSource(src1, src2)
|
||||
|
||||
src1.On("AddrsForNode", pk1).Return(false, nil, nil).Once()
|
||||
src2.On("AddrsForNode", pk1).Return(false, nil, nil).Once()
|
||||
src1.On("AddrsForNode", ctx, pk1).Return(false, nil, nil).Once()
|
||||
src2.On("AddrsForNode", ctx, pk1).Return(false, nil, nil).Once()
|
||||
|
||||
// Query it for the addresses known for node 1. It should return
|
||||
// false to indicate that the node is unknown to all backing
|
||||
// sources.
|
||||
known, addrs, err := multiSrc.AddrsForNode(pk1)
|
||||
known, addrs, err := multiSrc.AddrsForNode(ctx, pk1)
|
||||
require.NoError(t, err)
|
||||
require.False(t, known)
|
||||
require.Empty(t, addrs)
|
||||
@@ -127,10 +129,10 @@ func newMockAddrSource(t *testing.T) *mockAddrSource {
|
||||
return &mockAddrSource{t: t}
|
||||
}
|
||||
|
||||
func (m *mockAddrSource) AddrsForNode(pub *btcec.PublicKey) (bool, []net.Addr,
|
||||
error) {
|
||||
func (m *mockAddrSource) AddrsForNode(ctx context.Context,
|
||||
pub *btcec.PublicKey) (bool, []net.Addr, error) {
|
||||
|
||||
args := m.Called(pub)
|
||||
args := m.Called(ctx, pub)
|
||||
if args.Get(1) == nil {
|
||||
return args.Bool(0), nil, args.Error(2)
|
||||
}
|
||||
|
||||
+4
-1
@@ -2,6 +2,7 @@ package channeldb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -1532,7 +1533,9 @@ func (c *ChannelStateDB) RestoreChannelShells(channelShells ...*ChannelShell) er
|
||||
// unknown to the channel DB or not.
|
||||
//
|
||||
// NOTE: this is part of the AddrSource interface.
|
||||
func (d *DB) AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
|
||||
func (d *DB) AddrsForNode(_ context.Context, nodePub *btcec.PublicKey) (bool,
|
||||
[]net.Addr, error) {
|
||||
|
||||
linkNode, err := d.channelStateDB.linkNodeDB.FetchLinkNode(nodePub)
|
||||
// Only if the error is something other than ErrNodeNotFound do we
|
||||
// return it.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
"math"
|
||||
"math/rand"
|
||||
@@ -180,6 +181,7 @@ func TestFetchClosedChannelForID(t *testing.T) {
|
||||
// channel db and graph db.
|
||||
func TestMultiSourceAddrsForNode(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
|
||||
fullDB, err := MakeTestDB(t)
|
||||
require.NoError(t, err, "unable to make test database")
|
||||
@@ -194,7 +196,7 @@ func TestMultiSourceAddrsForNode(t *testing.T) {
|
||||
testNode := createTestVertex(t)
|
||||
nodePub, err := testNode.PubKey()
|
||||
require.NoError(t, err)
|
||||
graph.On("AddrsForNode", nodePub).Return(
|
||||
graph.On("AddrsForNode", ctx, nodePub).Return(
|
||||
true, []net.Addr{testAddr}, nil,
|
||||
).Once()
|
||||
|
||||
@@ -212,7 +214,7 @@ func TestMultiSourceAddrsForNode(t *testing.T) {
|
||||
|
||||
// Now that we've created a link node, as well as a vertex for the
|
||||
// node, we'll query for all its addresses.
|
||||
known, nodeAddrs, err := addrSource.AddrsForNode(nodePub)
|
||||
known, nodeAddrs, err := addrSource.AddrsForNode(ctx, nodePub)
|
||||
require.NoError(t, err, "unable to obtain node addrs")
|
||||
require.True(t, known)
|
||||
|
||||
|
||||
Reference in new issue
Block a user