Skip to content

Commit 1a5b5c5

Browse files
committed
lntypes: Add a ChannelParty type.
This commit introduces a ChannelParty type to LND. It is useful for consolidating all references to the duality between the local and remote nodes. This is currently handled by having named struct rows or named boolean parameters, named either "local" or "remote". This change alleviates the programmer from having to decide which node should be bound to `true` or `false`. In an upcoming commit we will change callsites to use this.
1 parent 16d80f5 commit 1a5b5c5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

lntypes/channel_party.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package lntypes
2+
3+
import "fmt"
4+
5+
// ChannelParty is a type used to have an unambiguous description of which node
6+
// is being referred to. This eliminates the need to describe as "local" or
7+
// "remote" using bool.
8+
type ChannelParty uint8
9+
10+
const (
11+
// Local is a ChannelParty constructor that is used to refer to the
12+
// node that is running.
13+
Local ChannelParty = iota
14+
15+
// Remote is a ChannelParty constructor that is used to refer to the
16+
// node on the other end of the peer connection.
17+
Remote
18+
)
19+
20+
// String provides a string representation of ChannelParty (useful for logging).
21+
func (p ChannelParty) String() string {
22+
switch p {
23+
case Local:
24+
return "Local"
25+
case Remote:
26+
return "Remote"
27+
default:
28+
panic(fmt.Sprintf("invalid ChannelParty value: %d", p))
29+
}
30+
}
31+
32+
// CounterParty inverts the role of the ChannelParty.
33+
func (p ChannelParty) CounterParty() ChannelParty {
34+
switch p {
35+
case Local:
36+
return Remote
37+
case Remote:
38+
return Local
39+
default:
40+
panic(fmt.Sprintf("invalid ChannelParty value: %v", p))
41+
}
42+
}
43+
44+
// IsLocal returns true if the ChannelParty is Local.
45+
func (p ChannelParty) IsLocal() bool {
46+
return p == Local
47+
}
48+
49+
// IsRemote returns true if the ChannelParty is Remote.
50+
func (p ChannelParty) IsRemote() bool {
51+
return p == Remote
52+
}

0 commit comments

Comments
 (0)