Skip to content

Commit 4cf7555

Browse files
committed
lnwallet: move commitChain to its own file.
1 parent 3e7866d commit 4cf7555

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

lnwallet/channel.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package lnwallet
22

33
import (
44
"bytes"
5-
"container/list"
65
"crypto/sha256"
76
"errors"
87
"fmt"
@@ -768,61 +767,6 @@ func (lc *LightningChannel) diskCommitToMemCommit(isLocal bool,
768767
return commit, nil
769768
}
770769

771-
// commitmentChain represents a chain of unrevoked commitments. The tail of the
772-
// chain is the latest fully signed, yet unrevoked commitment. Two chains are
773-
// tracked, one for the local node, and another for the remote node. New
774-
// commitments we create locally extend the remote node's chain, and vice
775-
// versa. Commitment chains are allowed to grow to a bounded length, after
776-
// which the tail needs to be "dropped" before new commitments can be received.
777-
// The tail is "dropped" when the owner of the chain sends a revocation for the
778-
// previous tail.
779-
type commitmentChain struct {
780-
// commitments is a linked list of commitments to new states. New
781-
// commitments are added to the end of the chain with increase height.
782-
// Once a commitment transaction is revoked, the tail is incremented,
783-
// freeing up the revocation window for new commitments.
784-
commitments *list.List
785-
}
786-
787-
// newCommitmentChain creates a new commitment chain.
788-
func newCommitmentChain() *commitmentChain {
789-
return &commitmentChain{
790-
commitments: list.New(),
791-
}
792-
}
793-
794-
// addCommitment extends the commitment chain by a single commitment. This
795-
// added commitment represents a state update proposed by either party. Once
796-
// the commitment prior to this commitment is revoked, the commitment becomes
797-
// the new defacto state within the channel.
798-
func (s *commitmentChain) addCommitment(c *commitment) {
799-
s.commitments.PushBack(c)
800-
}
801-
802-
// advanceTail reduces the length of the commitment chain by one. The tail of
803-
// the chain should be advanced once a revocation for the lowest unrevoked
804-
// commitment in the chain is received.
805-
func (s *commitmentChain) advanceTail() {
806-
s.commitments.Remove(s.commitments.Front())
807-
}
808-
809-
// tip returns the latest commitment added to the chain.
810-
func (s *commitmentChain) tip() *commitment {
811-
return s.commitments.Back().Value.(*commitment)
812-
}
813-
814-
// tail returns the lowest unrevoked commitment transaction in the chain.
815-
func (s *commitmentChain) tail() *commitment {
816-
return s.commitments.Front().Value.(*commitment)
817-
}
818-
819-
// hasUnackedCommitment returns true if the commitment chain has more than one
820-
// entry. The tail of the commitment chain has been ACKed by revoking all prior
821-
// commitments, but any subsequent commitments have not yet been ACKed.
822-
func (s *commitmentChain) hasUnackedCommitment() bool {
823-
return s.commitments.Front() != s.commitments.Back()
824-
}
825-
826770
// LightningChannel implements the state machine which corresponds to the
827771
// current commitment protocol wire spec. The state machine implemented allows
828772
// for asynchronous fully desynchronized, batched+pipelined updates to

lnwallet/commitment_chain.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package lnwallet
2+
3+
import "container/list"
4+
5+
// commitmentChain represents a chain of unrevoked commitments. The tail of the
6+
// chain is the latest fully signed, yet unrevoked commitment. Two chains are
7+
// tracked, one for the local node, and another for the remote node. New
8+
// commitments we create locally extend the remote node's chain, and vice
9+
// versa. Commitment chains are allowed to grow to a bounded length, after
10+
// which the tail needs to be "dropped" before new commitments can be received.
11+
// The tail is "dropped" when the owner of the chain sends a revocation for the
12+
// previous tail.
13+
type commitmentChain struct {
14+
// commitments is a linked list of commitments to new states. New
15+
// commitments are added to the end of the chain with increase height.
16+
// Once a commitment transaction is revoked, the tail is incremented,
17+
// freeing up the revocation window for new commitments.
18+
commitments *list.List
19+
}
20+
21+
// newCommitmentChain creates a new commitment chain.
22+
func newCommitmentChain() *commitmentChain {
23+
return &commitmentChain{
24+
commitments: list.New(),
25+
}
26+
}
27+
28+
// addCommitment extends the commitment chain by a single commitment. This
29+
// added commitment represents a state update proposed by either party. Once
30+
// the commitment prior to this commitment is revoked, the commitment becomes
31+
// the new defacto state within the channel.
32+
func (s *commitmentChain) addCommitment(c *commitment) {
33+
s.commitments.PushBack(c)
34+
}
35+
36+
// advanceTail reduces the length of the commitment chain by one. The tail of
37+
// the chain should be advanced once a revocation for the lowest unrevoked
38+
// commitment in the chain is received.
39+
func (s *commitmentChain) advanceTail() {
40+
s.commitments.Remove(s.commitments.Front())
41+
}
42+
43+
// tip returns the latest commitment added to the chain.
44+
func (s *commitmentChain) tip() *commitment {
45+
return s.commitments.Back().Value.(*commitment)
46+
}
47+
48+
// tail returns the lowest unrevoked commitment transaction in the chain.
49+
func (s *commitmentChain) tail() *commitment {
50+
return s.commitments.Front().Value.(*commitment)
51+
}
52+
53+
// hasUnackedCommitment returns true if the commitment chain has more than one
54+
// entry. The tail of the commitment chain has been ACKed by revoking all prior
55+
// commitments, but any subsequent commitments have not yet been ACKed.
56+
func (s *commitmentChain) hasUnackedCommitment() bool {
57+
return s.commitments.Front() != s.commitments.Back()
58+
}

0 commit comments

Comments
 (0)