-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommitmentSpec.scala
174 lines (155 loc) · 5.35 KB
/
CommitmentSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package scoin.ln.transactions
import scoin._
import scoin.ln._
import scoin.ln.transactions.Transactions.{
CommitmentFormat,
ZeroFeeHtlcTxAnchorOutputsCommitmentFormat
}
sealed trait CommitmentOutput
object CommitmentOutput {
case object ToLocal extends CommitmentOutput
case object ToRemote extends CommitmentOutput
case object ToLocalAnchor extends CommitmentOutput
case object ToRemoteAnchor extends CommitmentOutput
case class InHtlc(incomingHtlc: IncomingHtlc) extends CommitmentOutput
case class OutHtlc(outgoingHtlc: OutgoingHtlc) extends CommitmentOutput
}
sealed trait DirectedHtlc {
val add: UpdateAddHtlc
def opposite: DirectedHtlc = this match {
case IncomingHtlc(_) => OutgoingHtlc(add)
case OutgoingHtlc(_) => IncomingHtlc(add)
}
def direction: String = this match {
case IncomingHtlc(_) => "IN"
case OutgoingHtlc(_) => "OUT"
}
}
object DirectedHtlc {
def incoming: PartialFunction[DirectedHtlc, UpdateAddHtlc] = {
case h: IncomingHtlc => h.add
}
def outgoing: PartialFunction[DirectedHtlc, UpdateAddHtlc] = {
case h: OutgoingHtlc => h.add
}
}
case class IncomingHtlc(add: UpdateAddHtlc) extends DirectedHtlc
case class OutgoingHtlc(add: UpdateAddHtlc) extends DirectedHtlc
final case class CommitmentSpec(
htlcs: Set[DirectedHtlc],
commitTxFeerate: FeeratePerKw,
toLocal: MilliSatoshi,
toRemote: MilliSatoshi
) {
def htlcTxFeerate(commitmentFormat: CommitmentFormat): FeeratePerKw =
commitmentFormat match {
case ZeroFeeHtlcTxAnchorOutputsCommitmentFormat =>
FeeratePerKw(Satoshi(0))
case _ => commitTxFeerate
}
def findIncomingHtlcById(id: Long): Option[IncomingHtlc] =
htlcs.collectFirst { case htlc: IncomingHtlc if htlc.add.id == id => htlc }
def findOutgoingHtlcById(id: Long): Option[OutgoingHtlc] =
htlcs.collectFirst { case htlc: OutgoingHtlc if htlc.add.id == id => htlc }
}
object CommitmentSpec {
def removeHtlc(changes: List[UpdateMessage], id: Long): List[UpdateMessage] =
changes.filterNot {
case u: UpdateAddHtlc => u.id == id
case _ => false
}
def addHtlc(
spec: CommitmentSpec,
directedHtlc: DirectedHtlc
): CommitmentSpec = {
directedHtlc match {
case OutgoingHtlc(add) =>
spec.copy(
toLocal = spec.toLocal - add.amountMsat,
htlcs = spec.htlcs + directedHtlc
)
case IncomingHtlc(add) =>
spec.copy(
toRemote = spec.toRemote - add.amountMsat,
htlcs = spec.htlcs + directedHtlc
)
}
}
def fulfillIncomingHtlc(
spec: CommitmentSpec,
htlcId: Long
): CommitmentSpec = {
spec.findIncomingHtlcById(htlcId) match {
case Some(htlc) =>
spec.copy(
toLocal = spec.toLocal + htlc.add.amountMsat,
htlcs = spec.htlcs - htlc
)
case None => throw new RuntimeException(s"cannot find htlc id=$htlcId")
}
}
def fulfillOutgoingHtlc(
spec: CommitmentSpec,
htlcId: Long
): CommitmentSpec = {
spec.findOutgoingHtlcById(htlcId) match {
case Some(htlc) =>
spec.copy(
toRemote = spec.toRemote + htlc.add.amountMsat,
htlcs = spec.htlcs - htlc
)
case None => throw new RuntimeException(s"cannot find htlc id=$htlcId")
}
}
def failIncomingHtlc(spec: CommitmentSpec, htlcId: Long): CommitmentSpec = {
spec.findIncomingHtlcById(htlcId) match {
case Some(htlc) =>
spec.copy(
toRemote = spec.toRemote + htlc.add.amountMsat,
htlcs = spec.htlcs - htlc
)
case None => throw new RuntimeException(s"cannot find htlc id=$htlcId")
}
}
def failOutgoingHtlc(spec: CommitmentSpec, htlcId: Long): CommitmentSpec = {
spec.findOutgoingHtlcById(htlcId) match {
case Some(htlc) =>
spec.copy(
toLocal = spec.toLocal + htlc.add.amountMsat,
htlcs = spec.htlcs - htlc
)
case None => throw new RuntimeException(s"cannot find htlc id=$htlcId")
}
}
def reduce(
localCommitSpec: CommitmentSpec,
localChanges: List[UpdateMessage],
remoteChanges: List[UpdateMessage]
): CommitmentSpec = {
val spec1 = localChanges.foldLeft(localCommitSpec) {
case (spec, u: UpdateAddHtlc) => addHtlc(spec, OutgoingHtlc(u))
case (spec, _) => spec
}
val spec2 = remoteChanges.foldLeft(spec1) {
case (spec, u: UpdateAddHtlc) => addHtlc(spec, IncomingHtlc(u))
case (spec, _) => spec
}
val spec3 = localChanges.foldLeft(spec2) {
case (spec, u: UpdateFulfillHtlc) => fulfillIncomingHtlc(spec, u.id)
case (spec, u: UpdateFailHtlc) => failIncomingHtlc(spec, u.id)
case (spec, u: UpdateFailMalformedHtlc) => failIncomingHtlc(spec, u.id)
case (spec, _) => spec
}
val spec4 = remoteChanges.foldLeft(spec3) {
case (spec, u: UpdateFulfillHtlc) => fulfillOutgoingHtlc(spec, u.id)
case (spec, u: UpdateFailHtlc) => failOutgoingHtlc(spec, u.id)
case (spec, u: UpdateFailMalformedHtlc) => failOutgoingHtlc(spec, u.id)
case (spec, _) => spec
}
val spec5 = (localChanges ++ remoteChanges).foldLeft(spec4) {
case (spec, u: UpdateFee) => spec.copy(commitTxFeerate = u.feeratePerKw)
case (spec, _) => spec
}
spec5
}
}