Merge pull request #1910 from halseth/fundingmanager-state-machine

Fundingmanager state machine cleanup
This commit is contained in:
Olaoluwa Osuntokun 2019-09-25 16:31:51 -07:00 committed by GitHub
commit b1d122773e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 489 additions and 522 deletions

File diff suppressed because it is too large Load diff

View file

@ -244,7 +244,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
chainNotifier := &mockNotifier{
oneConfChannel: 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)
@ -398,7 +398,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
f.cfg.NotifyWhenOnline = func(peer [33]byte,
connectedChan chan<- lnpeer.Peer) {
connectedChan <- testNode
connectedChan <- testNode.remotePeer
}
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) {
t.Helper()
select {
case <-msgChan:
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) {
t.Helper()
var msg lnwire.Message
select {
case msg = <-msgChan:
@ -748,6 +752,8 @@ func assertFundingMsgSent(t *testing.T, msgChan chan lnwire.Message,
func assertNumPendingReservations(t *testing.T, node *testNode,
peerPubKey *btcec.PublicKey, expectedNum int) {
t.Helper()
serializedPubKey := newSerializedKey(peerPubKey)
actualNum := len(node.fundingMgr.activeReservations[serializedPubKey])
if actualNum == expectedNum {
@ -760,6 +766,8 @@ func assertNumPendingReservations(t *testing.T, node *testNode,
}
func assertNumPendingChannelsBecomes(t *testing.T, node *testNode, expectedNum int) {
t.Helper()
var numPendingChans int
for i := 0; i < testPollNumTries; i++ {
// 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) {
t.Helper()
var numPendingChans int
for i := 0; i < 5; i++ {
// 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,
fundingOutPoint *wire.OutPoint, expectedState channelOpeningState) {
t.Helper()
var state channelOpeningState
var err error
@ -839,18 +850,24 @@ func assertDatabaseState(t *testing.T, node *testNode,
func assertMarkedOpen(t *testing.T, alice, bob *testNode,
fundingOutPoint *wire.OutPoint) {
t.Helper()
assertDatabaseState(t, alice, fundingOutPoint, markedOpen)
assertDatabaseState(t, bob, fundingOutPoint, markedOpen)
}
func assertFundingLockedSent(t *testing.T, alice, bob *testNode,
fundingOutPoint *wire.OutPoint) {
t.Helper()
assertDatabaseState(t, alice, fundingOutPoint, fundingLockedSent)
assertDatabaseState(t, bob, fundingOutPoint, fundingLockedSent)
}
func assertAddedToRouterGraph(t *testing.T, alice, bob *testNode,
fundingOutPoint *wire.OutPoint) {
t.Helper()
assertDatabaseState(t, alice, 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) {
t.Helper()
// After the FundingLocked message is sent and six confirmations have
// been reached, the channel will be announced to the greater network
// 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,
fundingOutPoint *wire.OutPoint) {
t.Helper()
assertErrChannelNotFound(t, alice, fundingOutPoint)
assertErrChannelNotFound(t, bob, fundingOutPoint)
@ -1020,6 +1040,7 @@ func assertNoChannelState(t *testing.T, alice, bob *testNode,
func assertErrChannelNotFound(t *testing.T, node *testNode,
fundingOutPoint *wire.OutPoint) {
t.Helper()
var state channelOpeningState
var err error
@ -1043,6 +1064,8 @@ func assertErrChannelNotFound(t *testing.T, node *testNode,
}
func assertHandleFundingLocked(t *testing.T, alice, bob *testNode) {
t.Helper()
// They should both send the new channel state to their peer.
select {
case c := <-alice.newChannels:
@ -1725,7 +1748,7 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
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{
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf - 1,
}
@ -1734,12 +1757,12 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
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, bob, 1)
// Increase both Alice and Bob to maxWaitNumBlocksFundingConf height
// Increase both Alice and Bob to maxWaitNumBlocksFundingConf height.
alice.mockNotifier.epochChan <- &chainntnfs.BlockEpoch{
Height: fundingBroadcastHeight + maxWaitNumBlocksFundingConf,
}
@ -1748,13 +1771,13 @@ func TestFundingManagerFundingNotTimeoutInitiator(t *testing.T) {
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)
// Bob should have sent an Error message to Alice.
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)
}