autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
package autopilot
|
|
|
|
|
|
|
|
import (
|
2018-08-07 03:17:38 +02:00
|
|
|
"errors"
|
2018-08-31 10:12:20 +02:00
|
|
|
"fmt"
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type moreChansResp struct {
|
2018-12-19 14:54:53 +01:00
|
|
|
numMore uint32
|
|
|
|
amt btcutil.Amount
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 03:23:52 +02:00
|
|
|
type moreChanArg struct {
|
|
|
|
chans []Channel
|
|
|
|
balance btcutil.Amount
|
|
|
|
}
|
|
|
|
|
2018-12-19 14:54:53 +01:00
|
|
|
type mockConstraints struct {
|
2018-12-19 14:54:53 +01:00
|
|
|
moreChansResps chan moreChansResp
|
|
|
|
moreChanArgs chan moreChanArg
|
|
|
|
quit chan struct{}
|
2018-12-19 14:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConstraints) ChannelBudget(chans []Channel,
|
|
|
|
balance btcutil.Amount) (btcutil.Amount, uint32) {
|
2018-12-19 14:54:53 +01:00
|
|
|
|
|
|
|
if m.moreChanArgs != nil {
|
|
|
|
moreChan := moreChanArg{
|
|
|
|
chans: chans,
|
|
|
|
balance: balance,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m.moreChanArgs <- moreChan:
|
|
|
|
case <-m.quit:
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case resp := <-m.moreChansResps:
|
|
|
|
return resp.amt, resp.numMore
|
|
|
|
case <-m.quit:
|
|
|
|
return 0, 0
|
|
|
|
}
|
2018-12-19 14:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConstraints) MaxPendingOpens() uint16 {
|
|
|
|
return 10
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConstraints) MinChanSize() btcutil.Amount {
|
2019-03-05 13:58:31 +01:00
|
|
|
return 1e7
|
2018-12-19 14:54:53 +01:00
|
|
|
}
|
|
|
|
func (m *mockConstraints) MaxChanSize() btcutil.Amount {
|
|
|
|
return 1e8
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ AgentConstraints = (*mockConstraints)(nil)
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
type mockHeuristic struct {
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeScoresResps chan map[NodeID]*NodeScore
|
2018-11-22 23:18:09 +01:00
|
|
|
nodeScoresArgs chan directiveArg
|
2018-08-07 03:17:38 +02:00
|
|
|
|
|
|
|
quit chan struct{}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 03:23:52 +02:00
|
|
|
type directiveArg struct {
|
|
|
|
graph ChannelGraph
|
|
|
|
amt btcutil.Amount
|
2018-11-22 23:18:09 +01:00
|
|
|
chans []Channel
|
|
|
|
nodes map[NodeID]struct{}
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 14:54:54 +01:00
|
|
|
func (m *mockHeuristic) Name() string {
|
|
|
|
return "mock"
|
|
|
|
}
|
|
|
|
|
2018-11-22 23:18:09 +01:00
|
|
|
func (m *mockHeuristic) NodeScores(g ChannelGraph, chans []Channel,
|
2019-03-05 13:58:31 +01:00
|
|
|
chanSize btcutil.Amount, nodes map[NodeID]struct{}) (
|
2018-12-19 15:24:17 +01:00
|
|
|
map[NodeID]*NodeScore, error) {
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2018-11-22 23:18:09 +01:00
|
|
|
if m.nodeScoresArgs != nil {
|
2018-08-07 03:17:38 +02:00
|
|
|
directive := directiveArg{
|
2018-11-22 23:18:09 +01:00
|
|
|
graph: g,
|
2019-03-05 13:58:31 +01:00
|
|
|
amt: chanSize,
|
2018-11-22 23:18:09 +01:00
|
|
|
chans: chans,
|
|
|
|
nodes: nodes,
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
2018-08-07 03:17:38 +02:00
|
|
|
|
|
|
|
select {
|
2018-11-22 23:18:09 +01:00
|
|
|
case m.nodeScoresArgs <- directive:
|
2018-08-07 03:17:38 +02:00
|
|
|
case <-m.quit:
|
|
|
|
return nil, errors.New("exiting")
|
|
|
|
}
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 03:17:38 +02:00
|
|
|
select {
|
2018-11-22 23:18:09 +01:00
|
|
|
case resp := <-m.nodeScoresResps:
|
2018-08-07 03:17:38 +02:00
|
|
|
return resp, nil
|
|
|
|
case <-m.quit:
|
|
|
|
return nil, errors.New("exiting")
|
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ AttachmentHeuristic = (*mockHeuristic)(nil)
|
|
|
|
|
|
|
|
type openChanIntent struct {
|
2018-08-06 23:02:31 +02:00
|
|
|
target *btcec.PublicKey
|
|
|
|
amt btcutil.Amount
|
|
|
|
private bool
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockChanController struct {
|
|
|
|
openChanSignals chan openChanIntent
|
2018-08-06 23:02:31 +02:00
|
|
|
private bool
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 03:58:36 +02:00
|
|
|
func (m *mockChanController) OpenChannel(target *btcec.PublicKey,
|
|
|
|
amt btcutil.Amount) error {
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
m.openChanSignals <- openChanIntent{
|
2018-08-06 23:02:31 +02:00
|
|
|
target: target,
|
|
|
|
amt: amt,
|
|
|
|
private: m.private,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
2018-08-06 23:02:31 +02:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChanController) CloseChannel(chanPoint *wire.OutPoint) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockChanController) SpliceIn(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (m *mockChanController) SpliceOut(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ChannelController = (*mockChanController)(nil)
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
type testContext struct {
|
|
|
|
constraints *mockConstraints
|
|
|
|
heuristic *mockHeuristic
|
|
|
|
chanController ChannelController
|
|
|
|
graph testGraph
|
|
|
|
agent *Agent
|
|
|
|
walletBalance btcutil.Amount
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(t *testing.T, initialChans []Channel) (*testContext, func()) {
|
|
|
|
t.Helper()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// First, we'll create all the dependencies that we'll need in order to
|
|
|
|
// create the autopilot agent.
|
|
|
|
self, err := randKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
2018-12-10 20:32:05 +01:00
|
|
|
|
|
|
|
quit := make(chan struct{})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
heuristic := &mockHeuristic{
|
2019-03-05 13:58:06 +01:00
|
|
|
nodeScoresArgs: make(chan directiveArg),
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeScoresResps: make(chan map[NodeID]*NodeScore),
|
2018-12-10 20:32:05 +01:00
|
|
|
quit: quit,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
2018-12-19 14:54:53 +01:00
|
|
|
constraints := &mockConstraints{
|
|
|
|
moreChansResps: make(chan moreChansResp),
|
2019-03-05 13:58:06 +01:00
|
|
|
moreChanArgs: make(chan moreChanArg),
|
2018-12-19 14:54:53 +01:00
|
|
|
quit: quit,
|
|
|
|
}
|
2018-12-19 14:54:53 +01:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
chanController := &mockChanController{
|
|
|
|
openChanSignals: make(chan openChanIntent, 10),
|
|
|
|
}
|
|
|
|
memGraph, _, _ := newMemChanGraph()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// We'll keep track of the funds available to the agent, to make sure
|
|
|
|
// it correctly uses this value when querying the ChannelBudget.
|
|
|
|
var availableFunds btcutil.Amount = 10 * btcutil.SatoshiPerBitcoin
|
|
|
|
|
|
|
|
ctx := &testContext{
|
|
|
|
constraints: constraints,
|
|
|
|
heuristic: heuristic,
|
|
|
|
chanController: chanController,
|
|
|
|
graph: memGraph,
|
|
|
|
walletBalance: availableFunds,
|
|
|
|
quit: quit,
|
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// With the dependencies we created, we can now create the initial
|
|
|
|
// agent itself.
|
|
|
|
testCfg := Config{
|
|
|
|
Self: self,
|
|
|
|
Heuristic: heuristic,
|
|
|
|
ChanController: chanController,
|
|
|
|
WalletBalance: func() (btcutil.Amount, error) {
|
2019-03-05 13:58:06 +01:00
|
|
|
ctx.Lock()
|
|
|
|
defer ctx.Unlock()
|
|
|
|
return ctx.walletBalance, nil
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
},
|
2018-08-07 03:58:36 +02:00
|
|
|
ConnectToPeer: func(*btcec.PublicKey, []net.Addr) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
DisconnectPeer: func(*btcec.PublicKey) error {
|
|
|
|
return nil
|
|
|
|
},
|
2018-12-19 14:54:53 +01:00
|
|
|
Graph: memGraph,
|
|
|
|
Constraints: constraints,
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
2019-03-05 13:58:06 +01:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
agent, err := New(testCfg, initialChans)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create agent: %v", err)
|
|
|
|
}
|
2019-03-05 13:58:06 +01:00
|
|
|
ctx.agent = agent
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// With the autopilot agent and all its dependencies we'll start the
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// primary controller goroutine.
|
|
|
|
if err := agent.Start(); err != nil {
|
|
|
|
t.Fatalf("unable to start agent: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
cleanup := func() {
|
|
|
|
// We must close quit before agent.Stop(), to make sure
|
|
|
|
// ChannelBudget won't block preventing the agent from exiting.
|
|
|
|
close(quit)
|
|
|
|
agent.Stop()
|
|
|
|
}
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
return ctx, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
// respondMoreChans consumes the moreChanArgs element and responds to the agent
|
|
|
|
// with the given moreChansResp.
|
|
|
|
func respondMoreChans(t *testing.T, testCtx *testContext, resp moreChansResp) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// The agent should now query the heuristic.
|
2018-09-02 03:30:24 +02:00
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case <-testCtx.constraints.moreChanArgs:
|
|
|
|
case <-time.After(time.Second * 3):
|
2018-09-02 03:30:24 +02:00
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// We'll send the response.
|
|
|
|
select {
|
|
|
|
case testCtx.constraints.moreChansResps <- resp:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("response wasn't sent in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// respondMoreChans consumes the nodeScoresArgs element and responds to the
|
|
|
|
// agent with the given node scores.
|
|
|
|
func respondNodeScores(t *testing.T, testCtx *testContext,
|
|
|
|
resp map[NodeID]*NodeScore) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Send over an empty list of attachment directives, which should cause
|
|
|
|
// the agent to return to waiting on a new signal.
|
|
|
|
select {
|
|
|
|
case <-testCtx.heuristic.nodeScoresArgs:
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("node scores weren't queried in time")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case testCtx.heuristic.nodeScoresResps <- resp:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("node scores were not sent in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAgentChannelOpenSignal tests that upon receipt of a chanOpenUpdate, then
|
|
|
|
// agent modifies its local state accordingly, and reconsults the heuristic.
|
|
|
|
func TestAgentChannelOpenSignal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// We'll send an initial "no" response to advance the agent past its
|
|
|
|
// initial check.
|
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// Next we'll signal a new channel being opened by the backing LN node,
|
|
|
|
// with a capacity of 1 BTC.
|
|
|
|
newChan := Channel{
|
|
|
|
ChanID: randChanID(),
|
|
|
|
Capacity: btcutil.SatoshiPerBitcoin,
|
|
|
|
}
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnChannelOpen(newChan)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// The agent should now query the heuristic in order to determine its
|
|
|
|
// next action as it local state has now been modified.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// At this point, the local state of the agent should
|
|
|
|
// have also been updated to reflect that the LN node
|
|
|
|
// now has an additional channel with one BTC.
|
|
|
|
if _, ok := testCtx.agent.chanState[newChan.ChanID]; !ok {
|
|
|
|
t.Fatalf("internal channel state wasn't updated")
|
2018-09-02 03:30:24 +02:00
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// There shouldn't be a call to the Select method as we've returned
|
|
|
|
// "false" for NeedMoreChans above.
|
|
|
|
select {
|
|
|
|
|
|
|
|
// If this send success, then Select was erroneously called and the
|
|
|
|
// test should be failed.
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
t.Fatalf("Select was called but shouldn't have been")
|
|
|
|
|
|
|
|
// This is the correct path as Select should've be called.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:39:45 +01:00
|
|
|
// TestAgentHeuristicUpdateSignal tests that upon notification about a
|
|
|
|
// heuristic update, the agent reconsults the heuristic.
|
|
|
|
func TestAgentHeuristicUpdateSignal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// We'll send an initial "no" response to advance the agent past its
|
|
|
|
// initial check.
|
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
|
|
|
|
|
|
|
// Next we'll signal that one of the heuristcs have been updated.
|
|
|
|
testCtx.agent.OnHeuristicUpdate(testCtx.heuristic)
|
|
|
|
|
|
|
|
// The update should trigger the agent to ask for a channel budget.so
|
|
|
|
// we'll respond that there is a budget for opening 1 more channel.
|
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: 1 * btcutil.SatoshiPerBitcoin,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// At this point, the agent should now be querying the heuristic for
|
|
|
|
// scores. We'll respond.
|
|
|
|
pub, err := testCtx.graph.addRandNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := NewNodeID(pub)
|
|
|
|
scores := map[NodeID]*NodeScore{
|
|
|
|
nodeID: {
|
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
respondNodeScores(t, testCtx, scores)
|
|
|
|
|
|
|
|
// Finally, this should result in the agent opening a channel.
|
|
|
|
chanController := testCtx.chanController.(*mockChanController)
|
|
|
|
select {
|
|
|
|
case <-chanController.openChanSignals:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("channel not opened in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 05:56:40 +01:00
|
|
|
// A mockFailingChanController always fails to open a channel.
|
|
|
|
type mockFailingChanController struct {
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:58:36 +02:00
|
|
|
func (m *mockFailingChanController) OpenChannel(target *btcec.PublicKey,
|
|
|
|
amt btcutil.Amount) error {
|
2017-12-05 05:56:40 +01:00
|
|
|
return errors.New("failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockFailingChanController) CloseChannel(chanPoint *wire.OutPoint) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *mockFailingChanController) SpliceIn(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (m *mockFailingChanController) SpliceOut(chanPoint *wire.OutPoint,
|
|
|
|
amt btcutil.Amount) (*Channel, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ChannelController = (*mockFailingChanController)(nil)
|
|
|
|
|
|
|
|
// TestAgentChannelFailureSignal tests that if an autopilot channel fails to
|
|
|
|
// open, the agent is signalled to make a new decision.
|
|
|
|
func TestAgentChannelFailureSignal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.chanController = &mockFailingChanController{}
|
2018-12-19 14:54:53 +01:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
node, err := testCtx.graph.addRandNode()
|
2018-12-19 14:54:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
2017-12-05 05:56:40 +01:00
|
|
|
|
|
|
|
// First ensure the agent will attempt to open a new channel. Return
|
|
|
|
// that we need more channels, and have 5BTC to use.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{1, 5 * btcutil.SatoshiPerBitcoin})
|
2017-12-05 05:56:40 +01:00
|
|
|
|
|
|
|
// At this point, the agent should now be querying the heuristic to
|
2018-02-09 05:06:57 +01:00
|
|
|
// request attachment directives, return a fake so the agent will
|
|
|
|
// attempt to open a channel.
|
2018-12-19 15:24:17 +01:00
|
|
|
var fakeDirective = &NodeScore{
|
|
|
|
NodeID: NewNodeID(node),
|
|
|
|
Score: 0.5,
|
2017-12-05 05:56:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
respondNodeScores(
|
|
|
|
t, testCtx, map[NodeID]*NodeScore{
|
|
|
|
NewNodeID(node): fakeDirective,
|
|
|
|
},
|
|
|
|
)
|
2017-12-05 05:56:40 +01:00
|
|
|
|
|
|
|
// At this point the agent will attempt to create a channel and fail.
|
|
|
|
|
|
|
|
// Now ensure that the controller loop is re-executed.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{1, 5 * btcutil.SatoshiPerBitcoin})
|
|
|
|
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
|
2017-12-05 05:56:40 +01:00
|
|
|
}
|
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// TestAgentChannelCloseSignal ensures that once the agent receives an outside
|
|
|
|
// signal of a channel belonging to the backing LN node being closed, then it
|
|
|
|
// will query the heuristic to make its next decision.
|
|
|
|
func TestAgentChannelCloseSignal(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// We'll start the agent with two channels already being active.
|
|
|
|
initialChans := []Channel{
|
|
|
|
{
|
|
|
|
ChanID: randChanID(),
|
|
|
|
Capacity: btcutil.SatoshiPerBitcoin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChanID: randChanID(),
|
|
|
|
Capacity: btcutil.SatoshiPerBitcoin * 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, initialChans)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// We'll send an initial "no" response to advance the agent past its
|
|
|
|
// initial check.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// Next, we'll close both channels which should force the agent to
|
|
|
|
// re-query the heuristic.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnChannelClose(initialChans[0].ChanID, initialChans[1].ChanID)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// The agent should now query the heuristic in order to determine its
|
|
|
|
// next action as it local state has now been modified.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// At this point, the local state of the agent should
|
|
|
|
// have also been updated to reflect that the LN node
|
|
|
|
// has no existing open channels.
|
|
|
|
if len(testCtx.agent.chanState) != 0 {
|
|
|
|
t.Fatalf("internal channel state wasn't updated")
|
2018-09-02 03:30:24 +02:00
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// There shouldn't be a call to the Select method as we've returned
|
|
|
|
// "false" for NeedMoreChans above.
|
|
|
|
select {
|
|
|
|
|
|
|
|
// If this send success, then Select was erroneously called and the
|
|
|
|
// test should be failed.
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
t.Fatalf("Select was called but shouldn't have been")
|
|
|
|
|
|
|
|
// This is the correct path as Select should've be called.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAgentBalanceUpdateIncrease ensures that once the agent receives an
|
|
|
|
// outside signal concerning a balance update, then it will re-query the
|
|
|
|
// heuristic to determine its next action.
|
|
|
|
func TestAgentBalanceUpdate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// We'll send an initial "no" response to advance the agent past its
|
|
|
|
// initial check.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// Next we'll send a new balance update signal to the agent, adding 5
|
|
|
|
// BTC to the amount of available funds.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.Lock()
|
|
|
|
testCtx.walletBalance += btcutil.SatoshiPerBitcoin * 5
|
|
|
|
testCtx.Unlock()
|
2018-08-29 04:17:14 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnBalanceChange()
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// The agent should now query the heuristic in order to determine its
|
|
|
|
// next action as it local state has now been modified.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// At this point, the local state of the agent should
|
|
|
|
// have also been updated to reflect that the LN node
|
|
|
|
// now has an additional 5BTC available.
|
|
|
|
if testCtx.agent.totalBalance != testCtx.walletBalance {
|
|
|
|
t.Fatalf("expected %v wallet balance "+
|
|
|
|
"instead have %v", testCtx.agent.totalBalance,
|
|
|
|
testCtx.walletBalance)
|
2018-09-02 03:30:24 +02:00
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// There shouldn't be a call to the Select method as we've returned
|
|
|
|
// "false" for NeedMoreChans above.
|
|
|
|
select {
|
|
|
|
|
|
|
|
// If this send success, then Select was erroneously called and the
|
|
|
|
// test should be failed.
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
t.Fatalf("Select was called but shouldn't have been")
|
|
|
|
|
|
|
|
// This is the correct path as Select should've be called.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAgentImmediateAttach tests that if an autopilot agent is created, and it
|
|
|
|
// has enough funds available to create channels, then it does so immediately.
|
|
|
|
func TestAgentImmediateAttach(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2018-02-09 05:06:57 +01:00
|
|
|
const numChans = 5
|
|
|
|
|
2018-12-19 14:54:54 +01:00
|
|
|
// We'll generate 5 mock directives so it can progress within its loop.
|
2018-12-19 15:24:17 +01:00
|
|
|
directives := make(map[NodeID]*NodeScore)
|
2018-12-19 14:54:54 +01:00
|
|
|
nodeKeys := make(map[NodeID]struct{})
|
|
|
|
for i := 0; i < numChans; i++ {
|
2019-03-05 13:58:06 +01:00
|
|
|
pub, err := testCtx.graph.addRandNode()
|
2018-12-19 14:54:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := NewNodeID(pub)
|
2018-12-19 15:24:17 +01:00
|
|
|
directives[nodeID] = &NodeScore{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
2018-12-19 14:54:54 +01:00
|
|
|
}
|
|
|
|
nodeKeys[nodeID] = struct{}{}
|
|
|
|
}
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
// The very first thing the agent should do is query the NeedMoreChans
|
|
|
|
// method on the passed heuristic. So we'll provide it with a response
|
|
|
|
// that will kick off the main loop.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: numChans,
|
|
|
|
amt: 5 * btcutil.SatoshiPerBitcoin,
|
|
|
|
},
|
|
|
|
)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// At this point, the agent should now be querying the heuristic to
|
2018-12-19 14:54:54 +01:00
|
|
|
// requests attachment directives. With our fake directives created,
|
|
|
|
// we'll now send then to the agent as a return value for the Select
|
|
|
|
// function.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondNodeScores(t, testCtx, directives)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
|
|
|
|
// Finally, we should receive 5 calls to the OpenChannel method with
|
|
|
|
// the exact same parameters that we specified within the attachment
|
|
|
|
// directives.
|
2019-03-05 13:58:06 +01:00
|
|
|
chanController := testCtx.chanController.(*mockChanController)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
select {
|
|
|
|
case openChan := <-chanController.openChanSignals:
|
|
|
|
if openChan.amt != btcutil.SatoshiPerBitcoin {
|
|
|
|
t.Fatalf("invalid chan amt: expected %v, got %v",
|
|
|
|
btcutil.SatoshiPerBitcoin, openChan.amt)
|
|
|
|
}
|
2018-08-31 10:02:27 +02:00
|
|
|
nodeID := NewNodeID(openChan.target)
|
|
|
|
_, ok := nodeKeys[nodeID]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected key: %v, not found",
|
|
|
|
nodeID)
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
}
|
2018-08-31 10:02:27 +02:00
|
|
|
delete(nodeKeys, nodeID)
|
2018-11-22 23:18:09 +01:00
|
|
|
|
autopilot: "Look ma no hands!", introducing autopilot mode
This commit introduces the initial implementation of the autopilot
mode. Autopilot is new mode within lnd that enables automatic channel
management. This means that if enabled lnd will attempt to
automatically manage channels according to a set of heuristic defined
within the main configuration for autopilot.Agent instance.
The autopilot.Agent implements a simple closed control loop. It takes
in external signals such as wallet balance updates, new open channel,
and channels that are now closed the updates its internal state. With
each external trigger it will consult the registered
AttachmentHeuristic to decide: if it needs to open any more channels,
and if so how much it should use to open the channels, ultimately
returning a set of recommended AttachmentDirectives. The
autopilot.Agent loop will then take those attempt to establish
connection, and go back in waiting for a new external signal.
With this first implementation the default heuristic is the
ConstrainedPrefAttachment implementation of AttachmentHeuristic. Given
a min and max channel size, a limit on the number of channels, and the
percentage of wallet funds to allocate to channels, it will attempt to
execute a heuristic drive by the Barabási–Albert model model in order
to attempt to drive the global graph towards a scale free topology.
This is commit implements a foundational layer for future simulations,
optimization, and additional heuristics.
2017-08-11 06:14:41 +02:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("channel not opened in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 03:23:52 +02:00
|
|
|
|
2018-08-06 23:02:31 +02:00
|
|
|
// TestAgentPrivateChannels ensure that only requests for private channels are
|
|
|
|
// sent if set.
|
|
|
|
func TestAgentPrivateChannels(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-19 14:54:53 +01:00
|
|
|
|
2018-08-06 23:02:31 +02:00
|
|
|
// The chanController should be initialized such that all of its open
|
|
|
|
// channel requests are for private channels.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.chanController.(*mockChanController).private = true
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2018-08-06 23:02:31 +02:00
|
|
|
const numChans = 5
|
|
|
|
|
2018-12-19 14:54:54 +01:00
|
|
|
// We'll generate 5 mock directives so the pubkeys will be found in the
|
|
|
|
// agent's graph, and it can progress within its loop.
|
2018-12-19 15:24:17 +01:00
|
|
|
directives := make(map[NodeID]*NodeScore)
|
2018-12-19 14:54:54 +01:00
|
|
|
for i := 0; i < numChans; i++ {
|
2019-03-05 13:58:06 +01:00
|
|
|
pub, err := testCtx.graph.addRandNode()
|
2018-12-19 14:54:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
2018-12-19 15:24:17 +01:00
|
|
|
directives[NewNodeID(pub)] = &NodeScore{
|
|
|
|
NodeID: NewNodeID(pub),
|
|
|
|
Score: 0.5,
|
2018-12-19 14:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:02:31 +02:00
|
|
|
// The very first thing the agent should do is query the NeedMoreChans
|
|
|
|
// method on the passed heuristic. So we'll provide it with a response
|
2018-09-02 03:30:24 +02:00
|
|
|
// that will kick off the main loop. We'll send over a response
|
|
|
|
// indicating that it should establish more channels, and give it a
|
|
|
|
// budget of 5 BTC to do so.
|
|
|
|
resp := moreChansResp{
|
2018-12-19 14:54:53 +01:00
|
|
|
numMore: numChans,
|
|
|
|
amt: 5 * btcutil.SatoshiPerBitcoin,
|
2018-09-02 03:30:24 +02:00
|
|
|
}
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, resp)
|
|
|
|
|
2018-08-06 23:02:31 +02:00
|
|
|
// At this point, the agent should now be querying the heuristic to
|
2018-12-19 14:54:54 +01:00
|
|
|
// requests attachment directives. With our fake directives created,
|
|
|
|
// we'll now send then to the agent as a return value for the Select
|
|
|
|
// function.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondNodeScores(t, testCtx, directives)
|
2018-08-06 23:02:31 +02:00
|
|
|
|
|
|
|
// Finally, we should receive 5 calls to the OpenChannel method, each
|
|
|
|
// specifying that it's for a private channel.
|
2019-03-05 13:58:06 +01:00
|
|
|
chanController := testCtx.chanController.(*mockChanController)
|
2018-08-06 23:02:31 +02:00
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
select {
|
|
|
|
case openChan := <-chanController.openChanSignals:
|
|
|
|
if !openChan.private {
|
|
|
|
t.Fatal("expected open channel request to be private")
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("channel not opened in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 03:23:52 +02:00
|
|
|
// TestAgentPendingChannelState ensures that the agent properly factors in its
|
|
|
|
// pending channel state when making decisions w.r.t if it needs more channels
|
|
|
|
// or not, and if so, who is eligible to open new channels to.
|
|
|
|
func TestAgentPendingChannelState(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2018-12-19 14:54:54 +01:00
|
|
|
// We'll only return a single directive for a pre-chosen node.
|
2019-03-05 13:58:06 +01:00
|
|
|
nodeKey, err := testCtx.graph.addRandNode()
|
2018-12-19 14:54:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := NewNodeID(nodeKey)
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeDirective := &NodeScore{
|
2019-03-05 13:58:06 +01:00
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
// Once again, we'll start by telling the agent as part of its first
|
|
|
|
// query, that it needs more channels and has 3 BTC available for
|
|
|
|
// attachment. We'll send over a response indicating that it should
|
|
|
|
// establish more channels, and give it a budget of 1 BTC to do so.
|
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: btcutil.SatoshiPerBitcoin,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
respondNodeScores(t, testCtx,
|
|
|
|
map[NodeID]*NodeScore{
|
|
|
|
nodeID: nodeDirective,
|
|
|
|
},
|
|
|
|
)
|
2017-08-16 03:23:52 +02:00
|
|
|
|
|
|
|
// A request to open the channel should've also been sent.
|
2019-03-05 13:58:06 +01:00
|
|
|
chanController := testCtx.chanController.(*mockChanController)
|
2017-08-16 03:23:52 +02:00
|
|
|
select {
|
|
|
|
case openChan := <-chanController.openChanSignals:
|
2019-03-05 13:58:06 +01:00
|
|
|
chanAmt := testCtx.constraints.MaxChanSize()
|
2018-12-19 15:24:17 +01:00
|
|
|
if openChan.amt != chanAmt {
|
2017-08-16 03:23:52 +02:00
|
|
|
t.Fatalf("invalid chan amt: expected %v, got %v",
|
2018-12-19 15:24:17 +01:00
|
|
|
chanAmt, openChan.amt)
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
if !openChan.target.IsEqual(nodeKey) {
|
|
|
|
t.Fatalf("unexpected key: expected %x, got %x",
|
|
|
|
nodeKey.SerializeCompressed(),
|
|
|
|
openChan.target.SerializeCompressed())
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("channel wasn't opened in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, in order to test that the pending state was properly updated,
|
|
|
|
// we'll trigger a balance update in order to trigger a query to the
|
|
|
|
// heuristic.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.Lock()
|
|
|
|
testCtx.walletBalance += 0.4 * btcutil.SatoshiPerBitcoin
|
|
|
|
testCtx.Unlock()
|
2018-08-29 04:17:14 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnBalanceChange()
|
2017-08-16 03:23:52 +02:00
|
|
|
|
|
|
|
// The heuristic should be queried, and the argument for the set of
|
|
|
|
// channels passed in should include the pending channels that
|
|
|
|
// should've been created above.
|
|
|
|
select {
|
|
|
|
// The request that we get should include a pending channel for the
|
|
|
|
// one that we just created, otherwise the agent isn't properly
|
|
|
|
// updating its internal state.
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.constraints.moreChanArgs:
|
|
|
|
chanAmt := testCtx.constraints.MaxChanSize()
|
2017-08-16 03:23:52 +02:00
|
|
|
if len(req.chans) != 1 {
|
|
|
|
t.Fatalf("should include pending chan in current "+
|
|
|
|
"state, instead have %v chans", len(req.chans))
|
|
|
|
}
|
2018-12-19 15:24:17 +01:00
|
|
|
if req.chans[0].Capacity != chanAmt {
|
2017-08-16 03:23:52 +02:00
|
|
|
t.Fatalf("wrong chan capacity: expected %v, got %v",
|
2018-12-19 15:24:17 +01:00
|
|
|
req.chans[0].Capacity, chanAmt)
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
if req.chans[0].Node != nodeID {
|
|
|
|
t.Fatalf("wrong node ID: expected %x, got %x",
|
2018-08-31 10:02:27 +02:00
|
|
|
nodeID, req.chans[0].Node[:])
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("need more chans wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll send across a response indicating that it *does* need more
|
|
|
|
// channels.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.constraints.moreChansResps <- moreChansResp{1, btcutil.SatoshiPerBitcoin}:
|
2017-08-16 03:23:52 +02:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("need more chans wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The response above should prompt the agent to make a query to the
|
|
|
|
// Select method. The arguments passed should reflect the fact that the
|
|
|
|
// node we have a pending channel to, should be ignored.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
2018-11-22 23:18:09 +01:00
|
|
|
if len(req.chans) == 0 {
|
2017-08-16 03:23:52 +02:00
|
|
|
t.Fatalf("expected to skip %v nodes, instead "+
|
2018-11-22 23:18:09 +01:00
|
|
|
"skipping %v", 1, len(req.chans))
|
2017-08-16 03:23:52 +02:00
|
|
|
}
|
2018-11-22 23:18:09 +01:00
|
|
|
if req.chans[0].Node != nodeID {
|
2017-08-16 03:23:52 +02:00
|
|
|
t.Fatalf("pending node not included in skip arguments")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 03:18:05 +02:00
|
|
|
|
|
|
|
// TestAgentPendingOpenChannel ensures that the agent queries its heuristic once
|
|
|
|
// it detects a channel is pending open. This allows the agent to use its own
|
|
|
|
// change outputs that have yet to confirm for funding transactions.
|
|
|
|
func TestAgentPendingOpenChannel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2018-08-07 03:18:05 +02:00
|
|
|
// We'll send an initial "no" response to advance the agent past its
|
|
|
|
// initial check.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
2018-08-07 03:18:05 +02:00
|
|
|
|
|
|
|
// Next, we'll signal that a new channel has been opened, but it is
|
|
|
|
// still pending.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnChannelPendingOpen()
|
2018-08-07 03:18:05 +02:00
|
|
|
|
|
|
|
// The agent should now query the heuristic in order to determine its
|
|
|
|
// next action as its local state has now been modified.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx, moreChansResp{0, 0})
|
2018-08-07 03:18:05 +02:00
|
|
|
|
|
|
|
// There shouldn't be a call to the Select method as we've returned
|
|
|
|
// "false" for NeedMoreChans above.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
|
2018-08-07 03:18:05 +02:00
|
|
|
t.Fatalf("Select was called but shouldn't have been")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 03:55:03 +02:00
|
|
|
|
|
|
|
// TestAgentOnNodeUpdates tests that the agent will wake up in response to the
|
|
|
|
// OnNodeUpdates signal. This is useful in ensuring that autopilot is always
|
|
|
|
// pulling in the latest graph updates into its decision making. It also
|
|
|
|
// prevents the agent from stalling after an initial attempt that finds no nodes
|
|
|
|
// in the graph.
|
|
|
|
func TestAgentOnNodeUpdates(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-12-10 20:32:05 +01:00
|
|
|
|
2018-08-24 03:55:03 +02:00
|
|
|
// We'll send an initial "yes" response to advance the agent past its
|
|
|
|
// initial check. This will cause it to try to get directives from an
|
|
|
|
// empty graph.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(
|
|
|
|
t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 2,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2018-08-24 03:55:03 +02:00
|
|
|
|
|
|
|
// Send over an empty list of attachment directives, which should cause
|
|
|
|
// the agent to return to waiting on a new signal.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
|
2018-08-24 03:55:03 +02:00
|
|
|
|
|
|
|
// Simulate more nodes being added to the graph by informing the agent
|
|
|
|
// that we have node updates.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnNodeUpdates()
|
2018-08-24 03:55:03 +02:00
|
|
|
|
|
|
|
// In response, the agent should wake up and see if it needs more
|
|
|
|
// channels. Since we haven't done anything, we will send the same
|
|
|
|
// response as before since we are still trying to open channels.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(
|
|
|
|
t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 2,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2018-08-24 03:55:03 +02:00
|
|
|
|
|
|
|
// Again the agent should pull in the next set of attachment directives.
|
|
|
|
// It's not important that this list is also empty, so long as the node
|
|
|
|
// updates signal is causing the agent to make this attempt.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
|
2018-08-24 03:55:03 +02:00
|
|
|
}
|
2018-08-31 10:12:20 +02:00
|
|
|
|
|
|
|
// TestAgentSkipPendingConns asserts that the agent will not try to make
|
|
|
|
// duplicate connection requests to the same node, even if the attachment
|
|
|
|
// heuristic instructs the agent to do so. It also asserts that the agent
|
|
|
|
// stops tracking the pending connection once it finishes. Note that in
|
|
|
|
// practice, a failed connection would be inserted into the skip map passed to
|
|
|
|
// the attachment heuristic, though this does not assert that case.
|
|
|
|
func TestAgentSkipPendingConns(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2018-08-31 10:12:20 +02:00
|
|
|
|
|
|
|
connect := make(chan chan error)
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.cfg.ConnectToPeer = func(*btcec.PublicKey, []net.Addr) (bool, error) {
|
|
|
|
errChan := make(chan error)
|
2018-08-31 10:12:20 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
select {
|
|
|
|
case connect <- errChan:
|
|
|
|
case <-testCtx.quit:
|
|
|
|
return false, errors.New("quit")
|
|
|
|
}
|
2018-08-31 10:12:20 +02:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return false, err
|
|
|
|
case <-testCtx.quit:
|
|
|
|
return false, errors.New("quit")
|
|
|
|
}
|
2018-08-31 10:12:20 +02:00
|
|
|
}
|
2018-12-06 14:24:15 +01:00
|
|
|
|
2018-12-19 14:54:54 +01:00
|
|
|
// We'll only return a single directive for a pre-chosen node.
|
2019-03-05 13:58:06 +01:00
|
|
|
nodeKey, err := testCtx.graph.addRandNode()
|
2018-12-19 14:54:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeID := NewNodeID(nodeKey)
|
|
|
|
nodeDirective := &NodeScore{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also add a second node to the graph, to keep the first one
|
|
|
|
// company.
|
2019-03-05 13:58:06 +01:00
|
|
|
nodeKey2, err := testCtx.graph.addRandNode()
|
2018-12-19 15:24:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
2018-12-19 14:54:54 +01:00
|
|
|
}
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeID2 := NewNodeID(nodeKey2)
|
2018-12-19 14:54:54 +01:00
|
|
|
|
2018-08-31 10:12:20 +02:00
|
|
|
// We'll send an initial "yes" response to advance the agent past its
|
|
|
|
// initial check. This will cause it to try to get directives from the
|
|
|
|
// graph.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 10:12:20 +02:00
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// Both nodes should be part of the arguments.
|
2018-08-31 10:12:20 +02:00
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
2018-12-19 15:24:17 +01:00
|
|
|
if len(req.nodes) != 2 {
|
|
|
|
t.Fatalf("expected %v nodes, instead "+
|
|
|
|
"had %v", 2, len(req.nodes))
|
|
|
|
}
|
|
|
|
if _, ok := req.nodes[nodeID]; !ok {
|
|
|
|
t.Fatalf("node not included in arguments")
|
|
|
|
}
|
|
|
|
if _, ok := req.nodes[nodeID2]; !ok {
|
|
|
|
t.Fatalf("node not included in arguments")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond with a scored directive. We skip node2 for now, implicitly
|
|
|
|
// giving it a zero-score.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{
|
2018-11-22 23:18:09 +01:00
|
|
|
NewNodeID(nodeKey): nodeDirective,
|
|
|
|
}:
|
2018-08-31 10:12:20 +02:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// The agent should attempt connection to the node.
|
2018-08-31 10:12:20 +02:00
|
|
|
var errChan chan error
|
|
|
|
select {
|
|
|
|
case errChan = <-connect:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("agent did not attempt connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal the agent to go again, now that we've tried to connect.
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.OnNodeUpdates()
|
2018-08-31 10:12:20 +02:00
|
|
|
|
|
|
|
// The heuristic again informs the agent that we need more channels.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 10:12:20 +02:00
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// Since the node now has a pending connection, it should be skipped
|
|
|
|
// and not part of the nodes attempting to be scored.
|
2018-08-31 10:12:20 +02:00
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
2018-12-19 15:24:17 +01:00
|
|
|
if len(req.nodes) != 1 {
|
|
|
|
t.Fatalf("expected %v nodes, instead "+
|
|
|
|
"had %v", 1, len(req.nodes))
|
|
|
|
}
|
|
|
|
if _, ok := req.nodes[nodeID2]; !ok {
|
|
|
|
t.Fatalf("node not included in arguments")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond with an emtpty score set.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
|
2018-08-31 10:12:20 +02:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// The agent should not attempt any connection, since no nodes were
|
|
|
|
// scored.
|
2018-08-31 10:12:20 +02:00
|
|
|
select {
|
|
|
|
case <-connect:
|
|
|
|
t.Fatalf("agent should not have attempted connection")
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, timeout the original request, which should still be waiting for
|
|
|
|
// a response.
|
|
|
|
select {
|
|
|
|
case errChan <- fmt.Errorf("connection timeout"):
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("agent did not receive connection timeout")
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:02:01 +01:00
|
|
|
// The agent will now retry since the last connection attempt failed.
|
2018-08-31 10:12:20 +02:00
|
|
|
// The heuristic again informs the agent that we need more channels.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 10:12:20 +02:00
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// The node should now be marked as "failed", which should make it
|
|
|
|
// being skipped during scoring. Again check that it won't be among the
|
|
|
|
// score request.
|
2018-08-31 10:12:20 +02:00
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
2018-12-19 15:24:17 +01:00
|
|
|
if len(req.nodes) != 1 {
|
|
|
|
t.Fatalf("expected %v nodes, instead "+
|
|
|
|
"had %v", 1, len(req.nodes))
|
|
|
|
}
|
|
|
|
if _, ok := req.nodes[nodeID2]; !ok {
|
|
|
|
t.Fatalf("node not included in arguments")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a directive for the second node.
|
|
|
|
nodeDirective2 := &NodeScore{
|
|
|
|
NodeID: nodeID2,
|
|
|
|
Score: 0.5,
|
|
|
|
}
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{
|
2018-12-19 15:24:17 +01:00
|
|
|
nodeID2: nodeDirective2,
|
2018-11-22 23:18:09 +01:00
|
|
|
}:
|
2018-08-31 10:12:20 +02:00
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:24:17 +01:00
|
|
|
// This time, the agent should try the connection to the second node.
|
2018-08-31 10:12:20 +02:00
|
|
|
select {
|
|
|
|
case <-connect:
|
|
|
|
case <-time.After(time.Second * 10):
|
2018-12-19 15:24:17 +01:00
|
|
|
t.Fatalf("agent should have attempted connection")
|
2018-08-31 10:12:20 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-28 12:17:37 +01:00
|
|
|
|
|
|
|
// TestAgentQuitWhenPendingConns tests that we are able to stop the autopilot
|
|
|
|
// agent even though there are pending connections to nodes.
|
|
|
|
func TestAgentQuitWhenPendingConns(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
2019-01-28 12:17:37 +01:00
|
|
|
|
|
|
|
connect := make(chan chan error)
|
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
testCtx.agent.cfg.ConnectToPeer = func(*btcec.PublicKey, []net.Addr) (bool, error) {
|
|
|
|
errChan := make(chan error)
|
2019-01-28 12:17:37 +01:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
select {
|
|
|
|
case connect <- errChan:
|
|
|
|
case <-testCtx.quit:
|
|
|
|
return false, errors.New("quit")
|
|
|
|
}
|
2019-01-28 12:17:37 +01:00
|
|
|
|
2019-03-05 13:58:06 +01:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return false, err
|
|
|
|
case <-testCtx.quit:
|
|
|
|
return false, errors.New("quit")
|
|
|
|
}
|
2019-01-28 12:17:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll only return a single directive for a pre-chosen node.
|
2019-03-05 13:58:06 +01:00
|
|
|
nodeKey, err := testCtx.graph.addRandNode()
|
2019-01-28 12:17:37 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := NewNodeID(nodeKey)
|
|
|
|
nodeDirective := &NodeScore{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll send an initial "yes" response to advance the agent past its
|
|
|
|
// initial check. This will cause it to try to get directives from the
|
|
|
|
// graph.
|
2019-03-05 13:58:06 +01:00
|
|
|
respondMoreChans(t, testCtx,
|
|
|
|
moreChansResp{
|
|
|
|
numMore: 1,
|
|
|
|
amt: testCtx.walletBalance,
|
|
|
|
},
|
|
|
|
)
|
2019-01-28 12:17:37 +01:00
|
|
|
|
|
|
|
// Check the args.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
2019-01-28 12:17:37 +01:00
|
|
|
if len(req.nodes) != 1 {
|
|
|
|
t.Fatalf("expected %v nodes, instead "+
|
|
|
|
"had %v", 1, len(req.nodes))
|
|
|
|
}
|
|
|
|
if _, ok := req.nodes[nodeID]; !ok {
|
|
|
|
t.Fatalf("node not included in arguments")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond with a scored directive.
|
|
|
|
select {
|
2019-03-05 13:58:06 +01:00
|
|
|
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{
|
2019-01-28 12:17:37 +01:00
|
|
|
NewNodeID(nodeKey): nodeDirective,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The agent should attempt connection to the node.
|
|
|
|
select {
|
|
|
|
case <-connect:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
t.Fatalf("agent did not attempt connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we are able to stop the agent, even though there is a
|
|
|
|
// pending connection.
|
|
|
|
stopped := make(chan error)
|
|
|
|
go func() {
|
2019-03-05 13:58:06 +01:00
|
|
|
stopped <- testCtx.agent.Stop()
|
2019-01-28 12:17:37 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-stopped:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error stopping agent: %v", err)
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatalf("unable to stop agent")
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 13:58:31 +01:00
|
|
|
|
|
|
|
// respondWithScores checks that the moreChansRequest contains what we expect,
|
|
|
|
// and responds with the given node scores.
|
|
|
|
func respondWithScores(t *testing.T, testCtx *testContext,
|
|
|
|
channelBudget btcutil.Amount, existingChans, newChans int,
|
|
|
|
nodeScores map[NodeID]*NodeScore) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case testCtx.constraints.moreChansResps <- moreChansResp{
|
|
|
|
numMore: uint32(newChans),
|
|
|
|
amt: channelBudget,
|
|
|
|
}:
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The agent should query for scores using the constraints returned
|
|
|
|
// above. We expect the agent to use the maximum channel size when
|
|
|
|
// opening channels.
|
|
|
|
chanSize := testCtx.constraints.MaxChanSize()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case req := <-testCtx.heuristic.nodeScoresArgs:
|
|
|
|
// All nodes in the graph should be potential channel
|
|
|
|
// candidates.
|
|
|
|
if len(req.nodes) != len(nodeScores) {
|
|
|
|
t.Fatalf("expected %v nodes, instead had %v",
|
|
|
|
len(nodeScores), len(req.nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'existingChans' is already open.
|
|
|
|
if len(req.chans) != existingChans {
|
|
|
|
t.Fatalf("expected %d existing channel, got %v",
|
|
|
|
existingChans, len(req.chans))
|
|
|
|
}
|
|
|
|
if req.amt != chanSize {
|
|
|
|
t.Fatalf("expected channel size of %v, got %v",
|
|
|
|
chanSize, req.amt)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("select wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond with the given scores.
|
|
|
|
select {
|
|
|
|
case testCtx.heuristic.nodeScoresResps <- nodeScores:
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("NodeScores wasn't queried in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkChannelOpens asserts that the channel controller attempts open the
|
|
|
|
// number of channels we expect, and with the exact total allocation.
|
|
|
|
func checkChannelOpens(t *testing.T, testCtx *testContext,
|
|
|
|
allocation btcutil.Amount, numChans int) []NodeID {
|
|
|
|
|
|
|
|
var nodes []NodeID
|
|
|
|
|
|
|
|
// The agent should attempt to open channels, totaling what we expect.
|
|
|
|
var totalAllocation btcutil.Amount
|
|
|
|
chanController := testCtx.chanController.(*mockChanController)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
select {
|
|
|
|
case openChan := <-chanController.openChanSignals:
|
|
|
|
totalAllocation += openChan.amt
|
|
|
|
|
|
|
|
testCtx.Lock()
|
|
|
|
testCtx.walletBalance -= openChan.amt
|
|
|
|
testCtx.Unlock()
|
|
|
|
|
|
|
|
nodes = append(nodes, NewNodeID(openChan.target))
|
|
|
|
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("channel not opened in time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalAllocation != allocation {
|
|
|
|
t.Fatalf("expected agent to open channels totalling %v, "+
|
|
|
|
"instead was %v", allocation, totalAllocation)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, make sure the agent won't try opening more channels.
|
|
|
|
select {
|
|
|
|
case <-chanController.openChanSignals:
|
|
|
|
t.Fatalf("agent unexpectedly opened channel")
|
|
|
|
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAgentChannelSizeAllocation tests that the autopilot agent opens channel
|
|
|
|
// of size that stays within the channel budget and size restrictions.
|
|
|
|
func TestAgentChannelSizeAllocation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Total number of nodes in our mock graph.
|
2019-07-11 13:14:39 +02:00
|
|
|
const numNodes = 20
|
2019-03-05 13:58:31 +01:00
|
|
|
|
|
|
|
testCtx, cleanup := setup(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
nodeScores := make(map[NodeID]*NodeScore)
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
nodeKey, err := testCtx.graph.addRandNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate key: %v", err)
|
|
|
|
}
|
|
|
|
nodeID := NewNodeID(nodeKey)
|
|
|
|
nodeScores[nodeID] = &NodeScore{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Score: 0.5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The agent should now query the heuristic in order to determine its
|
|
|
|
// next action as it local state has now been modified.
|
|
|
|
select {
|
|
|
|
case arg := <-testCtx.constraints.moreChanArgs:
|
|
|
|
if len(arg.chans) != 0 {
|
|
|
|
t.Fatalf("expected agent to have no channels open, "+
|
|
|
|
"had %v", len(arg.chans))
|
|
|
|
}
|
|
|
|
if arg.balance != testCtx.walletBalance {
|
|
|
|
t.Fatalf("expectd agent to have %v balance, had %v",
|
|
|
|
testCtx.walletBalance, arg.balance)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("heuristic wasn't queried in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll return a response telling the agent to open 5 channels, with a
|
|
|
|
// total channel budget of 5 BTC.
|
|
|
|
var channelBudget btcutil.Amount = 5 * btcutil.SatoshiPerBitcoin
|
|
|
|
numExistingChannels := 0
|
|
|
|
numNewChannels := 5
|
|
|
|
respondWithScores(
|
|
|
|
t, testCtx, channelBudget, numExistingChannels,
|
|
|
|
numNewChannels, nodeScores,
|
|
|
|
)
|
|
|
|
|
2019-07-11 13:14:39 +02:00
|
|
|
// We expect the autopilot to have allocated all funds towards
|
|
|
|
// channels.
|
2019-03-05 13:58:31 +01:00
|
|
|
expectedAllocation := testCtx.constraints.MaxChanSize() * btcutil.Amount(numNewChannels)
|
|
|
|
nodes := checkChannelOpens(
|
|
|
|
t, testCtx, expectedAllocation, numNewChannels,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Delete the selected nodes from our set of scores, to avoid scoring
|
|
|
|
// nodes we already have channels to.
|
|
|
|
for _, node := range nodes {
|
|
|
|
delete(nodeScores, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(halseth): this loop is a hack to ensure all the attempted
|
|
|
|
// channels are accounted for. This happens because the agent will
|
|
|
|
// query the ChannelBudget before all the pending channels are added to
|
|
|
|
// the map. Fix by adding them to the pending channels map before
|
|
|
|
// executing directives in goroutines?
|
|
|
|
waitForNumChans := func(expChans int) {
|
|
|
|
t.Helper()
|
|
|
|
|
2019-07-11 13:14:38 +02:00
|
|
|
var (
|
|
|
|
numChans int
|
|
|
|
balance btcutil.Amount
|
|
|
|
)
|
|
|
|
|
2019-03-05 13:58:31 +01:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case arg := <-testCtx.constraints.moreChanArgs:
|
2019-07-11 13:14:38 +02:00
|
|
|
numChans = len(arg.chans)
|
|
|
|
balance = arg.balance
|
|
|
|
|
2019-03-05 13:58:31 +01:00
|
|
|
// As long as the number of existing channels
|
|
|
|
// is below our expected number of channels,
|
2019-07-11 13:14:38 +02:00
|
|
|
// and the balance is not what we expect, we'll
|
|
|
|
// keep responding with "no more channels".
|
|
|
|
if numChans == expChans &&
|
|
|
|
balance == testCtx.walletBalance {
|
|
|
|
break Loop
|
2019-03-05 13:58:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:14:38 +02:00
|
|
|
select {
|
|
|
|
case testCtx.constraints.moreChansResps <- moreChansResp{0, 0}:
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("heuristic wasn't queried " +
|
|
|
|
"in time")
|
2019-03-05 13:58:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(time.Second * 3):
|
2019-07-11 13:14:38 +02:00
|
|
|
t.Fatalf("did not receive expected "+
|
|
|
|
"channels(%d) and balance(%d), "+
|
|
|
|
"instead got %d and %d", expChans,
|
|
|
|
testCtx.walletBalance, numChans,
|
|
|
|
balance)
|
2019-03-05 13:58:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the agent to have 5 channels.
|
|
|
|
waitForNumChans(numNewChannels)
|
|
|
|
|
|
|
|
// Set the channel budget to 1.5 BTC.
|
|
|
|
channelBudget = btcutil.SatoshiPerBitcoin * 3 / 2
|
|
|
|
|
|
|
|
// We'll return a response telling the agent to open 3 channels, with a
|
|
|
|
// total channel budget of 1.5 BTC.
|
|
|
|
numExistingChannels = 5
|
|
|
|
numNewChannels = 3
|
|
|
|
respondWithScores(
|
|
|
|
t, testCtx, channelBudget, numExistingChannels,
|
|
|
|
numNewChannels, nodeScores,
|
|
|
|
)
|
|
|
|
|
|
|
|
// To stay within the budget, we expect the autopilot to open 2
|
|
|
|
// channels.
|
2019-07-11 13:14:39 +02:00
|
|
|
expectedAllocation = channelBudget
|
|
|
|
nodes = checkChannelOpens(t, testCtx, expectedAllocation, 2)
|
|
|
|
numExistingChannels = 7
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
delete(nodeScores, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForNumChans(numExistingChannels)
|
|
|
|
|
|
|
|
// Finally check that we make maximum channels if we are well within
|
|
|
|
// our budget.
|
|
|
|
channelBudget = btcutil.SatoshiPerBitcoin * 5
|
|
|
|
numNewChannels = 2
|
|
|
|
respondWithScores(
|
|
|
|
t, testCtx, channelBudget, numExistingChannels,
|
|
|
|
numNewChannels, nodeScores,
|
|
|
|
)
|
|
|
|
|
|
|
|
// We now expect the autopilot to open 2 channels, and since it has
|
|
|
|
// more than enough balance within the budget, they should both be of
|
|
|
|
// maximum size.
|
|
|
|
expectedAllocation = testCtx.constraints.MaxChanSize() *
|
|
|
|
btcutil.Amount(numNewChannels)
|
|
|
|
|
|
|
|
checkChannelOpens(t, testCtx, expectedAllocation, numNewChannels)
|
2019-03-05 13:58:31 +01:00
|
|
|
}
|