autopilot/pref_attachment: rename ConstrainedPrefAttachment->PrefAttachment

Since the ConstrainedPrefAttachment no longers require the heuristic to
be aware of the autopilot constraints, we rename it PrefAttachment.
This commit is contained in:
Johan T. Halseth
2018-12-19 14:54:54 +01:00
parent ccf4b7feab
commit 3739c19ef8
3 changed files with 33 additions and 100 deletions

View File

@@ -8,38 +8,29 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
// ConstrainedPrefAttachment is an implementation of the AttachmentHeuristic // PrefAttachment is an implementation of the AttachmentHeuristic interface
// interface that implement a constrained non-linear preferential attachment // that implement a non-linear preferential attachment heuristic. This means
// heuristic. This means that given a threshold to allocate to automatic // that given a threshold to allocate to automatic channel establishment, the
// channel establishment, the heuristic will attempt to favor connecting to // heuristic will attempt to favor connecting to nodes which already have a set
// nodes which already have a set amount of links, selected by sampling from a // amount of links, selected by sampling from a power law distribution. The
// power law distribution. The attachment is non-linear in that it favors // attachment is non-linear in that it favors nodes with a higher in-degree but
// nodes with a higher in-degree but less so that regular linear preferential // less so than regular linear preferential attachment. As a result, this
// attachment. As a result, this creates smaller and less clusters than regular // creates smaller and less clusters than regular linear preferential
// linear preferential attachment. // attachment.
// //
// TODO(roasbeef): BA, with k=-3 // TODO(roasbeef): BA, with k=-3
type ConstrainedPrefAttachment struct { type PrefAttachment struct {
constraints AgentConstraints
} }
// NewConstrainedPrefAttachment creates a new instance of a // NewPrefAttachment creates a new instance of a PrefAttachment heuristic.
// ConstrainedPrefAttachment heuristics given bounds on allowed channel sizes, func NewPrefAttachment() *PrefAttachment {
// and an allocation amount which is interpreted as a percentage of funds that
// is to be committed to channels at all times.
func NewConstrainedPrefAttachment(
cfg AgentConstraints) *ConstrainedPrefAttachment {
prand.Seed(time.Now().Unix()) prand.Seed(time.Now().Unix())
return &PrefAttachment{}
return &ConstrainedPrefAttachment{
constraints: cfg,
}
} }
// A compile time assertion to ensure ConstrainedPrefAttachment meets the // A compile time assertion to ensure PrefAttachment meets the
// AttachmentHeuristic interface. // AttachmentHeuristic interface.
var _ AttachmentHeuristic = (*ConstrainedPrefAttachment)(nil) var _ AttachmentHeuristic = (*PrefAttachment)(nil)
// NodeID is a simple type that holds an EC public key serialized in compressed // NodeID is a simple type that holds an EC public key serialized in compressed
// format. // format.
@@ -69,7 +60,7 @@ func NewNodeID(pub *btcec.PublicKey) NodeID {
// given to nodes already having high connectivity in the graph. // given to nodes already having high connectivity in the graph.
// //
// NOTE: This is a part of the AttachmentHeuristic interface. // NOTE: This is a part of the AttachmentHeuristic interface.
func (p *ConstrainedPrefAttachment) NodeScores(g ChannelGraph, chans []Channel, func (p *PrefAttachment) NodeScores(g ChannelGraph, chans []Channel,
chanSize btcutil.Amount, nodes map[NodeID]struct{}) ( chanSize btcutil.Amount, nodes map[NodeID]struct{}) (
map[NodeID]*AttachmentDirective, error) { map[NodeID]*AttachmentDirective, error) {

View File

@@ -71,25 +71,14 @@ var chanGraphs = []struct {
}, },
} }
// TestConstrainedPrefAttachmentSelectEmptyGraph ensures that when passed an // TestPrefAttachmentSelectEmptyGraph ensures that when passed an
// empty graph, the NodeSores function always returns a score of 0. // empty graph, the NodeSores function always returns a score of 0.
func TestConstrainedPrefAttachmentSelectEmptyGraph(t *testing.T) { func TestPrefAttachmentSelectEmptyGraph(t *testing.T) {
const ( const (
minChanSize = 0
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin) maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
chanLimit = 3
threshold = 0.5
) )
constraints := NewConstraints( prefAttach := NewPrefAttachment()
minChanSize,
maxChanSize,
chanLimit,
0,
threshold,
)
prefAttach := NewConstrainedPrefAttachment(constraints)
// Create a random public key, which we will query to get a score for. // Create a random public key, which we will query to get a score for.
pub, err := randKey() pub, err := randKey()
@@ -175,27 +164,16 @@ func completeGraph(t *testing.T, g testGraph, numNodes int) {
} }
} }
// TestConstrainedPrefAttachmentSelectTwoVertexes ensures that when passed a // TestPrefAttachmentSelectTwoVertexes ensures that when passed a
// graph with only two eligible vertexes, then both are given the same score, // graph with only two eligible vertexes, then both are given the same score,
// and the funds are appropriately allocated across each peer. // and the funds are appropriately allocated across each peer.
func TestConstrainedPrefAttachmentSelectTwoVertexes(t *testing.T) { func TestPrefAttachmentSelectTwoVertexes(t *testing.T) {
t.Parallel() t.Parallel()
prand.Seed(time.Now().Unix()) prand.Seed(time.Now().Unix())
const ( const (
minChanSize = 0
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin) maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
chanLimit = 3
threshold = 0.5
)
constraints := NewConstraints(
minChanSize,
maxChanSize,
chanLimit,
0,
threshold,
) )
for _, graph := range chanGraphs { for _, graph := range chanGraphs {
@@ -208,7 +186,7 @@ func TestConstrainedPrefAttachmentSelectTwoVertexes(t *testing.T) {
defer cleanup() defer cleanup()
} }
prefAttach := NewConstrainedPrefAttachment(constraints) prefAttach := NewPrefAttachment()
// For this set, we'll load the memory graph with two // For this set, we'll load the memory graph with two
// nodes, and a random channel connecting them. // nodes, and a random channel connecting them.
@@ -295,27 +273,16 @@ func TestConstrainedPrefAttachmentSelectTwoVertexes(t *testing.T) {
} }
} }
// TestConstrainedPrefAttachmentSelectInsufficientFunds ensures that if the // TestPrefAttachmentSelectInsufficientFunds ensures that if the
// balance of the backing wallet is below the set min channel size, then it // balance of the backing wallet is below the set min channel size, then it
// never recommends candidates to attach to. // never recommends candidates to attach to.
func TestConstrainedPrefAttachmentSelectInsufficientFunds(t *testing.T) { func TestPrefAttachmentSelectInsufficientFunds(t *testing.T) {
t.Parallel() t.Parallel()
prand.Seed(time.Now().Unix()) prand.Seed(time.Now().Unix())
const ( const (
minChanSize = 0
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin) maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
chanLimit = 3
threshold = 0.5
)
constraints := NewConstraints(
minChanSize,
maxChanSize,
chanLimit,
0,
threshold,
) )
for _, graph := range chanGraphs { for _, graph := range chanGraphs {
@@ -332,7 +299,7 @@ func TestConstrainedPrefAttachmentSelectInsufficientFunds(t *testing.T) {
// them. // them.
completeGraph(t, graph, 10) completeGraph(t, graph, 10)
prefAttach := NewConstrainedPrefAttachment(constraints) prefAttach := NewPrefAttachment()
nodes := make(map[NodeID]struct{}) nodes := make(map[NodeID]struct{})
if err := graph.ForEachNode(func(n Node) error { if err := graph.ForEachNode(func(n Node) error {
@@ -368,27 +335,16 @@ func TestConstrainedPrefAttachmentSelectInsufficientFunds(t *testing.T) {
} }
} }
// TestConstrainedPrefAttachmentSelectGreedyAllocation tests that if upon // TestPrefAttachmentSelectGreedyAllocation tests that if upon
// returning node scores, the NodeScores method will attempt to greedily // returning node scores, the NodeScores method will attempt to greedily
// allocate all funds to each vertex (up to the max channel size). // allocate all funds to each vertex (up to the max channel size).
func TestConstrainedPrefAttachmentSelectGreedyAllocation(t *testing.T) { func TestPrefAttachmentSelectGreedyAllocation(t *testing.T) {
t.Parallel() t.Parallel()
prand.Seed(time.Now().Unix()) prand.Seed(time.Now().Unix())
const ( const (
minChanSize = 0
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin) maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
chanLimit = 3
threshold = 0.5
)
constraints := NewConstraints(
minChanSize,
maxChanSize,
chanLimit,
0,
threshold,
) )
for _, graph := range chanGraphs { for _, graph := range chanGraphs {
@@ -401,7 +357,7 @@ func TestConstrainedPrefAttachmentSelectGreedyAllocation(t *testing.T) {
defer cleanup() defer cleanup()
} }
prefAttach := NewConstrainedPrefAttachment(constraints) prefAttach := NewPrefAttachment()
const chanCapacity = btcutil.SatoshiPerBitcoin const chanCapacity = btcutil.SatoshiPerBitcoin
@@ -525,27 +481,16 @@ func TestConstrainedPrefAttachmentSelectGreedyAllocation(t *testing.T) {
} }
} }
// TestConstrainedPrefAttachmentSelectSkipNodes ensures that if a node was // TestPrefAttachmentSelectSkipNodes ensures that if a node was
// already selected as a channel counterparty, then that node will get a score // already selected as a channel counterparty, then that node will get a score
// of zero during scoring. // of zero during scoring.
func TestConstrainedPrefAttachmentSelectSkipNodes(t *testing.T) { func TestPrefAttachmentSelectSkipNodes(t *testing.T) {
t.Parallel() t.Parallel()
prand.Seed(time.Now().Unix()) prand.Seed(time.Now().Unix())
const ( const (
minChanSize = 0
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin) maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
chanLimit = 3
threshold = 0.5
)
constraints := NewConstraints(
minChanSize,
maxChanSize,
chanLimit,
0,
threshold,
) )
for _, graph := range chanGraphs { for _, graph := range chanGraphs {
@@ -558,7 +503,7 @@ func TestConstrainedPrefAttachmentSelectSkipNodes(t *testing.T) {
defer cleanup() defer cleanup()
} }
prefAttach := NewConstrainedPrefAttachment(constraints) prefAttach := NewPrefAttachment()
// Next, we'll create a simple topology of two nodes, // Next, we'll create a simple topology of two nodes,
// with a single channel connecting them. // with a single channel connecting them.

View File

@@ -95,11 +95,8 @@ func initAutoPilot(svr *server, cfg *autoPilotConfig) *autopilot.ManagerCfg {
cfg.Allocation, cfg.Allocation,
) )
// First, we'll create the preferential attachment heuristic, // First, we'll create the preferential attachment heuristic.
// initialized with the passed auto pilot configuration parameters. prefAttachment := autopilot.NewPrefAttachment()
prefAttachment := autopilot.NewConstrainedPrefAttachment(
atplConstraints,
)
// With the heuristic itself created, we can now populate the remainder // With the heuristic itself created, we can now populate the remainder
// of the items that the autopilot agent needs to perform its duties. // of the items that the autopilot agent needs to perform its duties.