forked from lightningnetwork/lnd
/
channel_update_announcement.go
166 lines (136 loc) · 4.29 KB
/
channel_update_announcement.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
package lnwire
import (
"bytes"
"errors"
"io"
"github.com/roasbeef/btcd/btcec"
)
// ChannelUpdateAnnouncement message is used after channel has been initially
// announced. Each side independently announces its fees and minimum expiry for
// HTLCs and other parameters. Also this message is used to redeclare initially
// setted channel parameters.
type ChannelUpdateAnnouncement struct {
// Signature is used to validate the announced data and prove the
// ownership of node id.
Signature *btcec.Signature
// ShortChannelID is the unique description of the funding transaction.
ShortChannelID ShortChannelID
// Timestamp allows ordering in the case of multiple announcements.
// We should ignore the message if timestamp is not greater than
// the last-received.
Timestamp uint32
// Flags least-significant bit must be set to 0 if the creating node
// corresponds to the first node in the previously sent channel
// announcement and 1 otherwise.
Flags uint16
// TimeLockDelta is the minimum number of blocks this node requires to
// be added to the expiry of HTLCs. This is a security parameter
// determined by the node operator. This value represents the required
// gap between the time locks of the incoming and outgoing HTLC's set
// to this node.
TimeLockDelta uint16
// HtlcMinimumMsat is the minimum HTLC value which will be accepted.
HtlcMinimumMsat uint32
// FeeBaseMstat...
FeeBaseMsat uint32
// FeeProportionalMillionths...
FeeProportionalMillionths uint32
}
// A compile time check to ensure ChannelUpdateAnnouncement implements the
// lnwire.Message interface.
var _ Message = (*ChannelUpdateAnnouncement)(nil)
// Validate performs any necessary sanity checks to ensure all fields present
// on the ChannelUpdateAnnouncement are valid.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdateAnnouncement) Validate() error {
// NOTE: As far as we don't have the node id (public key) in this
// message, we can't validate the signature on this stage, it should
// be validated latter - in discovery service handler.
if a.TimeLockDelta == 0 {
return errors.New("expiry should be greater then zero")
}
return nil
}
// Decode deserializes a serialized ChannelUpdateAnnouncement stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdateAnnouncement) Decode(r io.Reader, pver uint32) error {
return readElements(r,
&a.Signature,
&a.ShortChannelID,
&a.Timestamp,
&a.Flags,
&a.TimeLockDelta,
&a.HtlcMinimumMsat,
&a.FeeBaseMsat,
&a.FeeProportionalMillionths,
)
}
// Encode serializes the target ChannelUpdateAnnouncement into the passed
// io.Writer observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdateAnnouncement) Encode(w io.Writer, pver uint32) error {
return writeElements(w,
a.Signature,
a.ShortChannelID,
a.Timestamp,
a.Flags,
a.TimeLockDelta,
a.HtlcMinimumMsat,
a.FeeBaseMsat,
a.FeeProportionalMillionths,
)
}
// Command returns the integer uniquely identifying this message type on the
// wire.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdateAnnouncement) Command() uint32 {
return CmdChannelUpdateAnnouncement
}
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdateAnnouncement) MaxPayloadLength(pver uint32) uint32 {
var length uint32
// Signature - 64 bytes
length += 64
// ShortChannelID - 8 bytes
length += 8
// Timestamp - 4 bytes
length += 4
// Flags - 2 bytes
length += 2
// Expiry - 2 bytes
length += 2
// HtlcMinimumMstat - 4 bytes
length += 4
// FeeBaseMstat - 4 bytes
length += 4
// FeeProportionalMillionths - 4 bytes
length += 4
return length
}
// DataToSign is used to retrieve part of the announcement message which
// should be signed.
func (a *ChannelUpdateAnnouncement) DataToSign() ([]byte, error) {
// We should not include the signatures itself.
var w bytes.Buffer
err := writeElements(&w,
a.ShortChannelID,
a.Timestamp,
a.Flags,
a.TimeLockDelta,
a.HtlcMinimumMsat,
a.FeeBaseMsat,
a.FeeProportionalMillionths,
)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}