mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-07 18:42:42 +02:00
Merge pull request #1910 from halseth/fundingmanager-state-machine
Fundingmanager state machine cleanup
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -244,7 +244,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
|
|||||||
chainNotifier := &mockNotifier{
|
chainNotifier := &mockNotifier{
|
||||||
oneConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
oneConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
||||||
sixConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
sixConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
|
||||||
epochChan: make(chan *chainntnfs.BlockEpoch, 1),
|
epochChan: make(chan *chainntnfs.BlockEpoch, 2),
|
||||||
}
|
}
|
||||||
|
|
||||||
sentMessages := make(chan lnwire.Message)
|
sentMessages := make(chan lnwire.Message)
|
||||||
@@ -398,7 +398,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
|
|||||||
f.cfg.NotifyWhenOnline = func(peer [33]byte,
|
f.cfg.NotifyWhenOnline = func(peer [33]byte,
|
||||||
connectedChan chan<- lnpeer.Peer) {
|
connectedChan chan<- lnpeer.Peer) {
|
||||||
|
|
||||||
connectedChan <- testNode
|
connectedChan <- testNode.remotePeer
|
||||||
}
|
}
|
||||||
|
|
||||||
return testNode, nil
|
return testNode, nil
|
||||||
@@ -679,6 +679,8 @@ func fundChannel(t *testing.T, alice, bob *testNode, localFundingAmt,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertErrorNotSent(t *testing.T, msgChan chan lnwire.Message) {
|
func assertErrorNotSent(t *testing.T, msgChan chan lnwire.Message) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-msgChan:
|
case <-msgChan:
|
||||||
t.Fatalf("error sent unexpectedly")
|
t.Fatalf("error sent unexpectedly")
|
||||||
@@ -688,6 +690,8 @@ func assertErrorNotSent(t *testing.T, msgChan chan lnwire.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertErrorSent(t *testing.T, msgChan chan lnwire.Message) {
|
func assertErrorSent(t *testing.T, msgChan chan lnwire.Message) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
var msg lnwire.Message
|
var msg lnwire.Message
|
||||||
select {
|
select {
|
||||||
case msg = <-msgChan:
|
case msg = <-msgChan:
|
||||||
@@ -748,6 +752,8 @@ func assertFundingMsgSent(t *testing.T, msgChan chan lnwire.Message,
|
|||||||
|
|
||||||
func assertNumPendingReservations(t *testing.T, node *testNode,
|
func assertNumPendingReservations(t *testing.T, node *testNode,
|
||||||
peerPubKey *btcec.PublicKey, expectedNum int) {
|
peerPubKey *btcec.PublicKey, expectedNum int) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
serializedPubKey := newSerializedKey(peerPubKey)
|
serializedPubKey := newSerializedKey(peerPubKey)
|
||||||
actualNum := len(node.fundingMgr.activeReservations[serializedPubKey])
|
actualNum := len(node.fundingMgr.activeReservations[serializedPubKey])
|
||||||
if actualNum == expectedNum {
|
if actualNum == expectedNum {
|
||||||
@@ -760,6 +766,8 @@ func assertNumPendingReservations(t *testing.T, node *testNode,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertNumPendingChannelsBecomes(t *testing.T, node *testNode, expectedNum int) {
|
func assertNumPendingChannelsBecomes(t *testing.T, node *testNode, expectedNum int) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
var numPendingChans int
|
var numPendingChans int
|
||||||
for i := 0; i < testPollNumTries; i++ {
|
for i := 0; i < testPollNumTries; i++ {
|
||||||
// If this is not the first try, sleep before retrying.
|
// If this is not the first try, sleep before retrying.
|
||||||
@@ -784,6 +792,8 @@ func assertNumPendingChannelsBecomes(t *testing.T, node *testNode, expectedNum i
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertNumPendingChannelsRemains(t *testing.T, node *testNode, expectedNum int) {
|
func assertNumPendingChannelsRemains(t *testing.T, node *testNode, expectedNum int) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
var numPendingChans int
|
var numPendingChans int
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
// If this is not the first try, sleep before retrying.
|
// If this is not the first try, sleep before retrying.
|
||||||
@@ -807,6 +817,7 @@ func assertNumPendingChannelsRemains(t *testing.T, node *testNode, expectedNum i
|
|||||||
|
|
||||||
func assertDatabaseState(t *testing.T, node *testNode,
|
func assertDatabaseState(t *testing.T, node *testNode,
|
||||||
fundingOutPoint *wire.OutPoint, expectedState channelOpeningState) {
|
fundingOutPoint *wire.OutPoint, expectedState channelOpeningState) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
var state channelOpeningState
|
var state channelOpeningState
|
||||||
var err error
|
var err error
|
||||||
@@ -839,18 +850,24 @@ func assertDatabaseState(t *testing.T, node *testNode,
|
|||||||
|
|
||||||
func assertMarkedOpen(t *testing.T, alice, bob *testNode,
|
func assertMarkedOpen(t *testing.T, alice, bob *testNode,
|
||||||
fundingOutPoint *wire.OutPoint) {
|
fundingOutPoint *wire.OutPoint) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
|
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
|
||||||
assertDatabaseState(t, bob, fundingOutPoint, markedOpen)
|
assertDatabaseState(t, bob, fundingOutPoint, markedOpen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertFundingLockedSent(t *testing.T, alice, bob *testNode,
|
func assertFundingLockedSent(t *testing.T, alice, bob *testNode,
|
||||||
fundingOutPoint *wire.OutPoint) {
|
fundingOutPoint *wire.OutPoint) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
|
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
|
||||||
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
|
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertAddedToRouterGraph(t *testing.T, alice, bob *testNode,
|
func assertAddedToRouterGraph(t *testing.T, alice, bob *testNode,
|
||||||
fundingOutPoint *wire.OutPoint) {
|
fundingOutPoint *wire.OutPoint) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
assertDatabaseState(t, alice, fundingOutPoint, addedToRouterGraph)
|
assertDatabaseState(t, alice, fundingOutPoint, addedToRouterGraph)
|
||||||
assertDatabaseState(t, bob, fundingOutPoint, addedToRouterGraph)
|
assertDatabaseState(t, bob, fundingOutPoint, addedToRouterGraph)
|
||||||
}
|
}
|
||||||
@@ -957,6 +974,8 @@ func assertChannelAnnouncements(t *testing.T, alice, bob *testNode,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertAnnouncementSignatures(t *testing.T, alice, bob *testNode) {
|
func assertAnnouncementSignatures(t *testing.T, alice, bob *testNode) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
// After the FundingLocked message is sent and six confirmations have
|
// After the FundingLocked message is sent and six confirmations have
|
||||||
// been reached, the channel will be announced to the greater network
|
// been reached, the channel will be announced to the greater network
|
||||||
// by having the nodes exchange announcement signatures.
|
// by having the nodes exchange announcement signatures.
|
||||||
@@ -1013,6 +1032,7 @@ func waitForOpenUpdate(t *testing.T, updateChan chan *lnrpc.OpenStatusUpdate) {
|
|||||||
|
|
||||||
func assertNoChannelState(t *testing.T, alice, bob *testNode,
|
func assertNoChannelState(t *testing.T, alice, bob *testNode,
|
||||||
fundingOutPoint *wire.OutPoint) {
|
fundingOutPoint *wire.OutPoint) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
assertErrChannelNotFound(t, alice, fundingOutPoint)
|
assertErrChannelNotFound(t, alice, fundingOutPoint)
|
||||||
assertErrChannelNotFound(t, bob, fundingOutPoint)
|
assertErrChannelNotFound(t, bob, fundingOutPoint)
|
||||||
@@ -1020,6 +1040,7 @@ func assertNoChannelState(t *testing.T, alice, bob *testNode,
|
|||||||
|
|
||||||
func assertErrChannelNotFound(t *testing.T, node *testNode,
|
func assertErrChannelNotFound(t *testing.T, node *testNode,
|
||||||
fundingOutPoint *wire.OutPoint) {
|
fundingOutPoint *wire.OutPoint) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
var state channelOpeningState
|
var state channelOpeningState
|
||||||
var err error
|
var err error
|
||||||
@@ -1043,6 +1064,8 @@ func assertErrChannelNotFound(t *testing.T, node *testNode,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertHandleFundingLocked(t *testing.T, alice, bob *testNode) {
|
func assertHandleFundingLocked(t *testing.T, alice, bob *testNode) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
// They should both send the new channel state to their peer.
|
// They should both send the new channel state to their peer.
|
||||||
select {
|
select {
|
||||||
case c := <-alice.newChannels:
|
case c := <-alice.newChannels:
|
||||||
@@ -1725,7 +1748,7 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
|
|||||||
t.Fatalf("alice did not publish funding tx")
|
t.Fatalf("alice did not publish funding tx")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increase the height to 1 minus the maxWaitNumBlocksFundingConf height
|
// Increase the height to 1 minus the maxWaitNumBlocksFundingConf height.
|
||||||
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
||||||
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
||||||
}
|
}
|
||||||
@@ -1734,12 +1757,12 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
|
|||||||
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assert both and Alice and Bob still have 1 pending channels
|
// Assert both and Alice and Bob still have 1 pending channels.
|
||||||
assertNumPendingChannelsRemains(t, alice, 1)
|
assertNumPendingChannelsRemains(t, alice, 1)
|
||||||
|
|
||||||
assertNumPendingChannelsRemains(t, bob, 1)
|
assertNumPendingChannelsRemains(t, bob, 1)
|
||||||
|
|
||||||
// Increase both Alice and Bob to maxWaitNumBlocksFundingConf height
|
// Increase both Alice and Bob to maxWaitNumBlocksFundingConf height.
|
||||||
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
|
||||||
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
||||||
}
|
}
|
||||||
@@ -1748,13 +1771,13 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
|
|||||||
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since Alice was the initiator, the channel should not have timed out
|
// Since Alice was the initiator, the channel should not have timed out.
|
||||||
assertNumPendingChannelsRemains(t, alice, 1)
|
assertNumPendingChannelsRemains(t, alice, 1)
|
||||||
|
|
||||||
// Bob should have sent an Error message to Alice.
|
// Bob should have sent an Error message to Alice.
|
||||||
assertErrorSent(t, bob.msgChan)
|
assertErrorSent(t, bob.msgChan)
|
||||||
|
|
||||||
// Since Bob was not the initiator, the channel should timeout
|
// Since Bob was not the initiator, the channel should timeout.
|
||||||
assertNumPendingChannelsBecomes(t, bob, 0)
|
assertNumPendingChannelsBecomes(t, bob, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user