htlcswitch/link_test: extract adding link to switch

This commit is contained in:
Johan T. Halseth 2018-05-09 16:49:48 +02:00
parent 3b2fd32523
commit 9a47494517
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -1414,12 +1414,12 @@ func (m *mockPeer) Disconnect(reason error) {
var _ Peer = (*mockPeer)(nil) var _ Peer = (*mockPeer)(nil)
func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) ( func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
ChannelLink, *lnwallet.LightningChannel, chan time.Time, func(), ChannelLink, *lnwallet.LightningChannel, chan time.Time, func() error,
chanRestoreFunc, error) { func(), chanRestoreFunc, error) {
var chanIDBytes [8]byte var chanIDBytes [8]byte
if _, err := io.ReadFull(rand.Reader, chanIDBytes[:]); err != nil { if _, err := io.ReadFull(rand.Reader, chanIDBytes[:]); err != nil {
return nil, nil, nil, nil, nil, err return nil, nil, nil, nil, nil, nil, err
} }
chanID := lnwire.NewShortChanIDFromInt( chanID := lnwire.NewShortChanIDFromInt(
@ -1430,7 +1430,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
chanReserve, chanReserve, chanID, chanReserve, chanReserve, chanID,
) )
if err != nil { if err != nil {
return nil, nil, nil, nil, nil, err return nil, nil, nil, nil, nil, nil, err
} }
var ( var (
@ -1461,7 +1461,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
aliceDb := aliceChannel.State().Db aliceDb := aliceChannel.State().Db
aliceSwitch, err := initSwitchWithDB(aliceDb) aliceSwitch, err := initSwitchWithDB(aliceDb)
if err != nil { if err != nil {
return nil, nil, nil, nil, nil, err return nil, nil, nil, nil, nil, nil, err
} }
t := make(chan time.Time) t := make(chan time.Time)
@ -1494,8 +1494,8 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
const startingHeight = 100 const startingHeight = 100
aliceLink := NewChannelLink(aliceCfg, aliceChannel, startingHeight) aliceLink := NewChannelLink(aliceCfg, aliceChannel, startingHeight)
if err := aliceSwitch.AddLink(aliceLink); err != nil { start := func() error {
return nil, nil, nil, nil, nil, err return aliceSwitch.AddLink(aliceLink)
} }
go func() { go func() {
for { for {
@ -1514,7 +1514,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
defer bobChannel.Stop() defer bobChannel.Stop()
} }
return aliceLink, bobChannel, t, cleanUp, restore, nil return aliceLink, bobChannel, t, start, cleanUp, restore, nil
} }
func assertLinkBandwidth(t *testing.T, link ChannelLink, func assertLinkBandwidth(t *testing.T, link ChannelLink,
@ -1686,13 +1686,17 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
// We'll start the test by creating a single instance of // We'll start the test by creating a single instance of
const chanAmt = btcutil.SatoshiPerBitcoin * 5 const chanAmt = btcutil.SatoshiPerBitcoin * 5
aliceLink, bobChannel, tmr, cleanUp, _, err := aliceLink, bobChannel, tmr, start, cleanUp, _, err :=
newSingleLinkTestHarness(chanAmt, 0) newSingleLinkTestHarness(chanAmt, 0)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
var ( var (
carolChanID = lnwire.NewShortChanIDFromInt(3) carolChanID = lnwire.NewShortChanIDFromInt(3)
mockBlob [lnwire.OnionPacketSize]byte mockBlob [lnwire.OnionPacketSize]byte
@ -2104,13 +2108,17 @@ func TestChannelLinkBandwidthConsistencyOverflow(t *testing.T) {
var mockBlob [lnwire.OnionPacketSize]byte var mockBlob [lnwire.OnionPacketSize]byte
const chanAmt = btcutil.SatoshiPerBitcoin * 5 const chanAmt = btcutil.SatoshiPerBitcoin * 5
aliceLink, bobChannel, batchTick, cleanUp, _, err := aliceLink, bobChannel, batchTick, start, cleanUp, _, err :=
newSingleLinkTestHarness(chanAmt, 0) newSingleLinkTestHarness(chanAmt, 0)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
var ( var (
coreLink = aliceLink.(*channelLink) coreLink = aliceLink.(*channelLink)
defaultCommitFee = coreLink.channel.StateSnapshot().CommitFee defaultCommitFee = coreLink.channel.StateSnapshot().CommitFee
@ -2353,13 +2361,17 @@ func TestChannelLinkTrimCircuitsPending(t *testing.T) {
// We'll start by creating a new link with our chanAmt (5 BTC). We will // We'll start by creating a new link with our chanAmt (5 BTC). We will
// only be testing Alice's behavior, so the reference to Bob's channel // only be testing Alice's behavior, so the reference to Bob's channel
// state is unnecessary. // state is unnecessary.
aliceLink, _, batchTicker, cleanUp, restore, err := aliceLink, _, batchTicker, start, cleanUp, restore, err :=
newSingleLinkTestHarness(chanAmt, 0) newSingleLinkTestHarness(chanAmt, 0)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
alice := newPersistentLinkHarness(t, aliceLink, batchTicker, restore) alice := newPersistentLinkHarness(t, aliceLink, batchTicker, restore)
// Compute the static fees that will be used to determine the // Compute the static fees that will be used to determine the
@ -2619,13 +2631,17 @@ func TestChannelLinkTrimCircuitsNoCommit(t *testing.T) {
// We'll start by creating a new link with our chanAmt (5 BTC). We will // We'll start by creating a new link with our chanAmt (5 BTC). We will
// only be testing Alice's behavior, so the reference to Bob's channel // only be testing Alice's behavior, so the reference to Bob's channel
// state is unnecessary. // state is unnecessary.
aliceLink, _, batchTicker, cleanUp, restore, err := aliceLink, _, batchTicker, start, cleanUp, restore, err :=
newSingleLinkTestHarness(chanAmt, 0) newSingleLinkTestHarness(chanAmt, 0)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
alice := newPersistentLinkHarness(t, aliceLink, batchTicker, restore) alice := newPersistentLinkHarness(t, aliceLink, batchTicker, restore)
// We'll put Alice into hodl.Commit mode, such that the circuits for any // We'll put Alice into hodl.Commit mode, such that the circuits for any
@ -2876,13 +2892,17 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) {
// channel reserve. // channel reserve.
const chanAmt = btcutil.SatoshiPerBitcoin * 5 const chanAmt = btcutil.SatoshiPerBitcoin * 5
const chanReserve = btcutil.SatoshiPerBitcoin * 1 const chanReserve = btcutil.SatoshiPerBitcoin * 1
aliceLink, bobChannel, batchTimer, cleanUp, _, err := aliceLink, bobChannel, batchTimer, start, cleanUp, _, err :=
newSingleLinkTestHarness(chanAmt, chanReserve) newSingleLinkTestHarness(chanAmt, chanReserve)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
var ( var (
mockBlob [lnwire.OnionPacketSize]byte mockBlob [lnwire.OnionPacketSize]byte
coreLink = aliceLink.(*channelLink) coreLink = aliceLink.(*channelLink)
@ -2991,13 +3011,17 @@ func TestChannelLinkBandwidthChanReserve(t *testing.T) {
// should therefore be 0. // should therefore be 0.
const bobChanAmt = btcutil.SatoshiPerBitcoin * 1 const bobChanAmt = btcutil.SatoshiPerBitcoin * 1
const bobChanReserve = btcutil.SatoshiPerBitcoin * 1.5 const bobChanReserve = btcutil.SatoshiPerBitcoin * 1.5
bobLink, _, _, bobCleanUp, _, err := bobLink, _, _, start, bobCleanUp, _, err :=
newSingleLinkTestHarness(bobChanAmt, bobChanReserve) newSingleLinkTestHarness(bobChanAmt, bobChanReserve)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer bobCleanUp() defer bobCleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
// Make sure bandwidth is reported as 0. // Make sure bandwidth is reported as 0.
assertLinkBandwidth(t, bobLink, 0) assertLinkBandwidth(t, bobLink, 0)
} }
@ -4070,13 +4094,17 @@ func TestChannelLinkNoMoreUpdates(t *testing.T) {
const chanAmt = btcutil.SatoshiPerBitcoin * 5 const chanAmt = btcutil.SatoshiPerBitcoin * 5
const chanReserve = btcutil.SatoshiPerBitcoin * 1 const chanReserve = btcutil.SatoshiPerBitcoin * 1
aliceLink, bobChannel, _, cleanUp, _, err := aliceLink, bobChannel, _, start, cleanUp, _, err :=
newSingleLinkTestHarness(chanAmt, chanReserve) newSingleLinkTestHarness(chanAmt, chanReserve)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
var ( var (
coreLink = aliceLink.(*channelLink) coreLink = aliceLink.(*channelLink)
aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs
@ -4158,13 +4186,17 @@ func TestChannelLinkWaitForRevocation(t *testing.T) {
const chanAmt = btcutil.SatoshiPerBitcoin * 5 const chanAmt = btcutil.SatoshiPerBitcoin * 5
const chanReserve = btcutil.SatoshiPerBitcoin * 1 const chanReserve = btcutil.SatoshiPerBitcoin * 1
aliceLink, bobChannel, _, cleanUp, _, err := aliceLink, bobChannel, _, start, cleanUp, _, err :=
newSingleLinkTestHarness(chanAmt, chanReserve) newSingleLinkTestHarness(chanAmt, chanReserve)
if err != nil { if err != nil {
t.Fatalf("unable to create link: %v", err) t.Fatalf("unable to create link: %v", err)
} }
defer cleanUp() defer cleanUp()
if err := start(); err != nil {
t.Fatalf("unable to start test harness: %v", err)
}
var ( var (
coreLink = aliceLink.(*channelLink) coreLink = aliceLink.(*channelLink)
aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs aliceMsgs = coreLink.cfg.Peer.(*mockPeer).sentMsgs