lnwire: add support for Features in NodeAnnouncement

Add support for Features in NodeAnnouncment according to spec.
This commit is contained in:
bryanvu authored and Olaoluwa Osuntokun committed 2017-03-29 12:03:43 -07:00
1 parent 3087bec4db
commit 085b7333cb
10 files changed
+145 -32

No files matched your search

+5
View File
@@ -75,4 +75,9 @@ var (
green: 255,
blue: 255,
}
someFeature = featureName("somefeature")
someFeatures = NewFeatureVector([]Feature{
{someFeature, OptionalFlag},
})
)
+7
View File
@@ -97,6 +97,9 @@ type NodeAnnouncement struct {
// Alias is used to customize their node's appearance in maps and graphs
Alias Alias
// Features is the list of protocol features this node supports.
Features *FeatureVector
// Address includes two specification fields: 'ipv6' and 'port' on which
// the node is accepting incoming connections.
Addresses []net.Addr
@@ -127,6 +130,7 @@ func (a *NodeAnnouncement) Decode(r io.Reader, pver uint32) error {
&a.RGBColor,
&a.Alias,
&a.Addresses,
&a.Features,
)
}
@@ -141,6 +145,7 @@ func (a *NodeAnnouncement) Encode(w io.Writer, pver uint32) error {
a.RGBColor,
a.Alias,
a.Addresses,
a.Features,
)
}
@@ -162,6 +167,7 @@ func (a *NodeAnnouncement) MaxPayloadLength(pver uint32) uint32 {
// NodeID - 33 bytes
// RGBColor - 3 bytes
// Alias - 32 bytes
// Features - variable
// NumAddresses - 2 bytes
// AddressDescriptor - 1 byte
// Ipv4 - 4 bytes (optional)
@@ -183,6 +189,7 @@ func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
a.RGBColor,
a.Alias,
a.Addresses,
a.Features,
)
if err != nil {
return nil, err
+16 -8
View File
@@ -10,31 +10,37 @@ import (
)
func TestNodeAnnouncementEncodeDecode(t *testing.T) {
cua := &NodeAnnouncement{
na := &NodeAnnouncement{
Signature: someSig,
Timestamp: maxUint32,
NodeID: pubKey,
RGBColor: someRGB,
Alias: someAlias,
Addresses: someAddresses,
Features: someFeatures,
}
// Next encode the NA message into an empty bytes buffer.
var b bytes.Buffer
if err := cua.Encode(&b, 0); err != nil {
if err := na.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode NodeAnnouncement: %v", err)
}
// Deserialize the encoded NA message into a new empty struct.
cua2 := &NodeAnnouncement{}
if err := cua2.Decode(&b, 0); err != nil {
na2 := &NodeAnnouncement{}
if err := na2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode NodeAnnouncement: %v", err)
}
// We do not encode the feature map in feature vector, for that reason
// the node announcement messages will differ. Set feature map with nil
// in order to use deep equal function.
na.Features.featuresMap = nil
// Assert equality of the two instances.
if !reflect.DeepEqual(cua, cua2) {
if !reflect.DeepEqual(na, na2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
cua, cua2)
na, na2)
}
}
@@ -52,6 +58,7 @@ func TestNodeAnnouncementValidation(t *testing.T) {
NodeID: nodePubKey,
RGBColor: someRGB,
Alias: someAlias,
Features: someFeatures,
}
dataToSign, _ := na.DataToSign()
@@ -73,6 +80,7 @@ func TestNodeAnnouncementPayloadLength(t *testing.T) {
RGBColor: someRGB,
Alias: someAlias,
Addresses: someAddresses,
Features: someFeatures,
}
var b bytes.Buffer
@@ -81,9 +89,9 @@ func TestNodeAnnouncementPayloadLength(t *testing.T) {
}
serializedLength := uint32(b.Len())
if serializedLength != 164 {
if serializedLength != 167 {
t.Fatalf("payload length estimate is incorrect: expected %v "+
"got %v", 164, serializedLength)
"got %v", 167, serializedLength)
}
if na.MaxPayloadLength(0) != 8192 {