forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircuit.go
230 lines (188 loc) · 6.45 KB
/
circuit.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package htlcswitch
import (
"encoding/binary"
"io"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnwire"
)
// EmptyCircuitKey is a default value for an outgoing circuit key returned when
// a circuit's keystone has not been set. Note that this value is invalid for
// use as a keystone, since the outgoing channel id can never be equal to
// sourceHop.
var EmptyCircuitKey CircuitKey
// CircuitKey is a tuple of channel ID and HTLC ID, used to uniquely identify
// HTLCs in a circuit. Circuits are identified primarily by the circuit key of
// the incoming HTLC. However, a circuit may also be referenced by its outgoing
// circuit key after the HTLC has been forwarded via the outgoing link.
type CircuitKey = channeldb.CircuitKey
// PaymentCircuit is used by the switch as placeholder between when the
// switch makes a forwarding decision and the outgoing link determines the
// proper HTLC ID for the local log. After the outgoing HTLC ID has been
// determined, the half circuit will be converted into a full PaymentCircuit.
type PaymentCircuit struct {
// AddRef is the forward reference of the Add update in the incoming
// link's forwarding package. This value is set on the htlcPacket of the
// returned settle/fail so that it can be removed from disk.
AddRef channeldb.AddRef
// Incoming is the circuit key identifying the incoming channel and htlc
// index from which this ADD originates.
Incoming CircuitKey
// Outgoing is the circuit key identifying the outgoing channel, and the
// HTLC index that was used to forward the ADD. It will be nil if this
// circuit's keystone has not been set.
Outgoing *CircuitKey
// PaymentHash used as unique identifier of payment.
PaymentHash [32]byte
// IncomingAmount is the value of the HTLC from the incoming link.
IncomingAmount lnwire.MilliSatoshi
// OutgoingAmount specifies the value of the HTLC leaving the switch,
// either as a payment or forwarded amount.
OutgoingAmount lnwire.MilliSatoshi
// ErrorEncrypter is used to re-encrypt the onion failure before
// sending it back to the originator of the payment.
ErrorEncrypter hop.ErrorEncrypter
// LoadedFromDisk is set true for any circuits loaded after the circuit
// map is reloaded from disk.
//
// NOTE: This value is determined implicitly during a restart. It is not
// persisted, and should never be set outside the circuit map.
LoadedFromDisk bool
}
// HasKeystone returns true if an outgoing link has assigned this circuit's
// outgoing circuit key.
func (c *PaymentCircuit) HasKeystone() bool {
return c.Outgoing != nil
}
// newPaymentCircuit initializes a payment circuit on the heap using the payment
// hash and an in-memory htlc packet.
func newPaymentCircuit(hash *[32]byte, pkt *htlcPacket) *PaymentCircuit {
var addRef channeldb.AddRef
if pkt.sourceRef != nil {
addRef = *pkt.sourceRef
}
return &PaymentCircuit{
AddRef: addRef,
Incoming: CircuitKey{
ChanID: pkt.incomingChanID,
HtlcID: pkt.incomingHTLCID,
},
PaymentHash: *hash,
IncomingAmount: pkt.incomingAmount,
OutgoingAmount: pkt.amount,
ErrorEncrypter: pkt.obfuscator,
}
}
// makePaymentCircuit initializes a payment circuit on the stack using the
// payment hash and an in-memory htlc packet.
func makePaymentCircuit(hash *[32]byte, pkt *htlcPacket) PaymentCircuit {
var addRef channeldb.AddRef
if pkt.sourceRef != nil {
addRef = *pkt.sourceRef
}
return PaymentCircuit{
AddRef: addRef,
Incoming: CircuitKey{
ChanID: pkt.incomingChanID,
HtlcID: pkt.incomingHTLCID,
},
PaymentHash: *hash,
IncomingAmount: pkt.incomingAmount,
OutgoingAmount: pkt.amount,
ErrorEncrypter: pkt.obfuscator,
}
}
// Encode writes a PaymentCircuit to the provided io.Writer.
func (c *PaymentCircuit) Encode(w io.Writer) error {
if err := c.AddRef.Encode(w); err != nil {
return err
}
if err := c.Incoming.Encode(w); err != nil {
return err
}
if _, err := w.Write(c.PaymentHash[:]); err != nil {
return err
}
var scratch [8]byte
binary.BigEndian.PutUint64(scratch[:], uint64(c.IncomingAmount))
if _, err := w.Write(scratch[:]); err != nil {
return err
}
binary.BigEndian.PutUint64(scratch[:], uint64(c.OutgoingAmount))
if _, err := w.Write(scratch[:]); err != nil {
return err
}
// Defaults to EncrypterTypeNone.
var encrypterType hop.EncrypterType
if c.ErrorEncrypter != nil {
encrypterType = c.ErrorEncrypter.Type()
}
err := binary.Write(w, binary.BigEndian, encrypterType)
if err != nil {
return err
}
// Skip encoding of error encrypter if this half add does not have one.
if encrypterType == hop.EncrypterTypeNone {
return nil
}
return c.ErrorEncrypter.Encode(w)
}
// Decode reads a PaymentCircuit from the provided io.Reader.
func (c *PaymentCircuit) Decode(r io.Reader) error {
if err := c.AddRef.Decode(r); err != nil {
return err
}
if err := c.Incoming.Decode(r); err != nil {
return err
}
if _, err := io.ReadFull(r, c.PaymentHash[:]); err != nil {
return err
}
var scratch [8]byte
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return err
}
c.IncomingAmount = lnwire.MilliSatoshi(
binary.BigEndian.Uint64(scratch[:]))
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return err
}
c.OutgoingAmount = lnwire.MilliSatoshi(
binary.BigEndian.Uint64(scratch[:]))
// Read the encrypter type used for this circuit.
var encrypterType hop.EncrypterType
err := binary.Read(r, binary.BigEndian, &encrypterType)
if err != nil {
return err
}
switch encrypterType {
case hop.EncrypterTypeNone:
// No encrypter was provided, such as when the payment is
// locally initiated.
return nil
case hop.EncrypterTypeSphinx:
// Sphinx encrypter was used as this is a forwarded HTLC.
c.ErrorEncrypter = hop.NewSphinxErrorEncrypter()
case hop.EncrypterTypeMock:
// Test encrypter.
c.ErrorEncrypter = NewMockObfuscator()
default:
return UnknownEncrypterType(encrypterType)
}
return c.ErrorEncrypter.Decode(r)
}
// InKey returns the primary identifier for the circuit corresponding to the
// incoming HTLC.
func (c *PaymentCircuit) InKey() CircuitKey {
return c.Incoming
}
// OutKey returns the keystone identifying the outgoing link and HTLC ID. If the
// circuit hasn't been completed, this method returns an EmptyKeystone, which is
// an invalid outgoing circuit key. Only call this method if HasKeystone returns
// true.
func (c *PaymentCircuit) OutKey() CircuitKey {
if c.Outgoing != nil {
return *c.Outgoing
}
return EmptyCircuitKey
}