From 9fd77a6e40baf935364feaecd50d28378cb6b006 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Wed, 11 Oct 2017 11:37:54 -0700 Subject: [PATCH] multi: Update lnd to use new feature vector API. --- autopilot/graph.go | 7 ++++--- channeldb/graph.go | 4 +++- channeldb/graph_test.go | 2 +- discovery/gossiper.go | 7 ++++--- discovery/gossiper_test.go | 2 +- discovery/utils.go | 2 +- features.go | 22 ---------------------- fundingmanager.go | 2 +- lnwire/channel_announcement.go | 2 +- lnwire/features.go | 6 +++++- lnwire/init_message.go | 6 +++--- lnwire/lnwire.go | 7 ++++--- lnwire/lnwire_test.go | 26 ++++++++++---------------- lnwire/node_announcement.go | 2 +- routing/notifications_test.go | 2 +- server.go | 13 +++++++++---- 16 files changed, 49 insertions(+), 63 deletions(-) delete mode 100644 features.go diff --git a/autopilot/graph.go b/autopilot/graph.go index 1e5a7515..b2dcfaf4 100644 --- a/autopilot/graph.go +++ b/autopilot/graph.go @@ -145,8 +145,9 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey, IP: bytes.Repeat([]byte("a"), 16), }, }, - Features: lnwire.NewFeatureVector(nil), - AuthSig: testSig, + Features: lnwire.NewFeatureVector(nil, + lnwire.GlobalFeatures), + AuthSig: testSig, } if err := d.db.AddLightningNode(graphNode); err != nil { return nil, err @@ -170,7 +171,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey, IP: bytes.Repeat([]byte("a"), 16), }, }, - Features: lnwire.NewFeatureVector(nil), + Features: lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures), AuthSig: testSig, } if err := d.db.AddLightningNode(dbNode); err != nil { diff --git a/channeldb/graph.go b/channeldb/graph.go index 11cd14eb..d9f21ef1 100644 --- a/channeldb/graph.go +++ b/channeldb/graph.go @@ -1592,10 +1592,12 @@ func deserializeLightningNode(r io.Reader) (*LightningNode, error) { return nil, err } - node.Features, err = lnwire.NewFeatureVectorFromReader(r) + fv := lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures) + err = fv.Decode(r) if err != nil { return nil, err } + node.Features = fv if _, err := r.Read(scratch[:2]); err != nil { return nil, err diff --git a/channeldb/graph_test.go b/channeldb/graph_test.go index a971b322..587ade7d 100644 --- a/channeldb/graph_test.go +++ b/channeldb/graph_test.go @@ -37,7 +37,7 @@ var ( _, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10) _, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10) - testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) + testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures) ) func createTestVertex(db *DB) (*LightningNode, error) { diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 3f1770fd..59f9e6e2 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -647,6 +647,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l } } + features := lnwire.NewFeatureVector(msg.Features, lnwire.GlobalFeatures) node := &channeldb.LightningNode{ HaveNodeAnnouncement: true, LastUpdate: time.Unix(int64(msg.Timestamp), 0), @@ -654,7 +655,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l PubKey: msg.NodeID, Alias: msg.Alias.String(), AuthSig: msg.Signature, - Features: msg.Features, + Features: features, } if err := d.cfg.Router.AddNode(node); err != nil { @@ -1178,7 +1179,7 @@ func (d *AuthenticatedGossiper) synchronizeWithNode(syncReq *syncRequest) error Addresses: node.Addresses, NodeID: node.PubKey, Alias: alias, - Features: node.Features, + Features: node.Features.RawFeatureVector, } announceMessages = append(announceMessages, ann) @@ -1258,7 +1259,7 @@ func (d *AuthenticatedGossiper) updateChannel(info *channeldb.ChannelEdgeInfo, NodeID2: info.NodeKey2, ChainHash: info.ChainHash, BitcoinKey1: info.BitcoinKey1, - Features: lnwire.NewFeatureVector([]lnwire.Feature{}), + Features: lnwire.NewRawFeatureVector(), BitcoinKey2: info.BitcoinKey2, } } diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index 2f1ec8bc..0f256362 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -31,7 +31,7 @@ var ( testAddr = &net.TCPAddr{IP: (net.IP)([]byte{0xA, 0x0, 0x0, 0x1}), Port: 9000} testAddrs = []net.Addr{testAddr} - testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) + testFeatures = lnwire.NewRawFeatureVector() testSig = &btcec.Signature{ R: new(big.Int), S: new(big.Int), diff --git a/discovery/utils.go b/discovery/utils.go index e649fac1..80b889dd 100644 --- a/discovery/utils.go +++ b/discovery/utils.go @@ -32,7 +32,7 @@ func createChanAnnouncement(chanProof *channeldb.ChannelAuthProof, NodeID2: chanInfo.NodeKey2, ChainHash: chanInfo.ChainHash, BitcoinKey1: chanInfo.BitcoinKey1, - Features: lnwire.NewFeatureVector([]lnwire.Feature{}), + Features: lnwire.NewRawFeatureVector(), BitcoinKey2: chanInfo.BitcoinKey2, } diff --git a/features.go b/features.go deleted file mode 100644 index 4233538f..00000000 --- a/features.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import "github.com/lightningnetwork/lnd/lnwire" - -// globalFeatures feature vector which affects HTLCs and thus are also -// advertised to other nodes. -var globalFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) - -// localFeatures is an feature vector which represent the features which -// only affect the protocol between these two nodes. -// -// TODO(roasbeef): update to only have one, add a dummy vector? -var localFeatures = lnwire.NewFeatureVector([]lnwire.Feature{ - { - Name: "filler", - Flag: lnwire.OptionalFlag, - }, - { - Name: "announce-graph", - Flag: lnwire.OptionalFlag, - }, -}) diff --git a/fundingmanager.go b/fundingmanager.go index 310b6052..c3e09500 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -1738,7 +1738,7 @@ func (f *fundingManager) newChanAnnouncement(localPubKey, remotePubKey *btcec.Pu // within the blockchain. chanAnn := &lnwire.ChannelAnnouncement{ ShortChannelID: shortChanID, - Features: lnwire.NewFeatureVector([]lnwire.Feature{}), + Features: lnwire.NewRawFeatureVector(), ChainHash: chainHash, } diff --git a/lnwire/channel_announcement.go b/lnwire/channel_announcement.go index b426ab33..29dbb39a 100644 --- a/lnwire/channel_announcement.go +++ b/lnwire/channel_announcement.go @@ -29,7 +29,7 @@ type ChannelAnnouncement struct { // by the target node. This field can be used to signal the type of the // channel, or modifications to the fields that would normally follow // this vector. - Features *FeatureVector + Features *RawFeatureVector // ChainHash denotes the target chain that this channel was opened // within. This value should be the genesis hash of the target chain. diff --git a/lnwire/features.go b/lnwire/features.go index 4b054178..bd53bed1 100644 --- a/lnwire/features.go +++ b/lnwire/features.go @@ -170,10 +170,14 @@ type FeatureVector struct { } // NewFeatureVector constructs a new FeatureVector from a raw feature vector and -// mapping of feature definitions. +// mapping of feature definitions. If the feature vector argument is nil, a new +// one will be constructed with no enabled features. func NewFeatureVector(featureVector *RawFeatureVector, featureNames map[FeatureBit]string) *FeatureVector { + if featureVector == nil { + featureVector = NewRawFeatureVector() + } return &FeatureVector{ RawFeatureVector: featureVector, featureNames: featureNames, diff --git a/lnwire/init_message.go b/lnwire/init_message.go index 471d64d9..e1146077 100644 --- a/lnwire/init_message.go +++ b/lnwire/init_message.go @@ -9,15 +9,15 @@ import "io" type Init struct { // GlobalFeatures is feature vector which affects HTLCs and thus are // also advertised to other nodes. - GlobalFeatures *FeatureVector + GlobalFeatures *RawFeatureVector // LocalFeatures is feature vector which only affect the protocol // between two nodes. - LocalFeatures *FeatureVector + LocalFeatures *RawFeatureVector } // NewInitMessage creates new instance of init message object. -func NewInitMessage(gf, lf *FeatureVector) *Init { +func NewInitMessage(gf *RawFeatureVector, lf *RawFeatureVector) *Init { return &Init{ GlobalFeatures: gf, LocalFeatures: lf, diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index 5631890e..b47b0d5a 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -215,7 +215,7 @@ func writeElement(w io.Writer, element interface{}) error { if err := wire.WriteVarBytes(w, 0, e); err != nil { return err } - case *FeatureVector: + case *RawFeatureVector: if e == nil { return fmt.Errorf("cannot write nil feature vector") } @@ -435,8 +435,9 @@ func readElement(r io.Reader, element interface{}) error { return err } *e = pubKey - case **FeatureVector: - f, err := NewFeatureVectorFromReader(r) + case **RawFeatureVector: + f := NewRawFeatureVector() + err = f.Decode(r) if err != nil { return err } diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index d3b6bf2b..7636297d 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -51,16 +51,14 @@ func randPubKey() (*btcec.PublicKey, error) { return priv.PubKey(), nil } -func randFeatureVector(r *rand.Rand) *FeatureVector { - numFeatures := r.Int31n(10000) - features := make([]Feature, numFeatures) - for i := int32(0); i < numFeatures; i++ { - features[i] = Feature{ - Flag: featureFlag(rand.Int31n(2) + 1), +func randRawFeatureVector(r *rand.Rand) *RawFeatureVector { + featureVec := NewRawFeatureVector() + for i := 0; i < 10000; i++ { + if r.Int31n(2) == 0 { + featureVec.Set(FeatureBit(i)) } } - - return NewFeatureVector(features) + return featureVec } func TestMaxOutPointIndex(t *testing.T) { @@ -139,11 +137,9 @@ func TestLightningWireProtocol(t *testing.T) { customTypeGen := map[MessageType]func([]reflect.Value, *rand.Rand){ MsgInit: func(v []reflect.Value, r *rand.Rand) { req := NewInitMessage( - randFeatureVector(r), - randFeatureVector(r), + randRawFeatureVector(r), + randRawFeatureVector(r), ) - req.GlobalFeatures.featuresMap = nil - req.LocalFeatures.featuresMap = nil v[0] = reflect.ValueOf(*req) }, @@ -351,9 +347,8 @@ func TestLightningWireProtocol(t *testing.T) { MsgChannelAnnouncement: func(v []reflect.Value, r *rand.Rand) { req := ChannelAnnouncement{ ShortChannelID: NewShortChanIDFromInt(uint64(r.Int63())), - Features: randFeatureVector(r), + Features: randRawFeatureVector(r), } - req.Features.featuresMap = nil req.NodeSig1 = testSig req.NodeSig2 = testSig req.BitcoinSig1 = testSig @@ -396,7 +391,7 @@ func TestLightningWireProtocol(t *testing.T) { req := NodeAnnouncement{ Signature: testSig, - Features: randFeatureVector(r), + Features: randRawFeatureVector(r), Timestamp: uint32(r.Int31()), Alias: a, RGBColor: RGB{ @@ -407,7 +402,6 @@ func TestLightningWireProtocol(t *testing.T) { // TODO(roasbeef): proper gen rand addrs Addresses: testAddrs, } - req.Features.featuresMap = nil var err error req.NodeID, err = randPubKey() diff --git a/lnwire/node_announcement.go b/lnwire/node_announcement.go index 3ee1ada2..7cc13972 100644 --- a/lnwire/node_announcement.go +++ b/lnwire/node_announcement.go @@ -59,7 +59,7 @@ type NodeAnnouncement struct { Signature *btcec.Signature // Features is the list of protocol features this node supports. - Features *FeatureVector + Features *RawFeatureVector // Timestamp allows ordering in the case of multiple announcements. Timestamp uint32 diff --git a/routing/notifications_test.go b/routing/notifications_test.go index 73ff25d0..e93bae46 100644 --- a/routing/notifications_test.go +++ b/routing/notifications_test.go @@ -26,7 +26,7 @@ var ( Port: 9000} testAddrs = []net.Addr{testAddr} - testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) + testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures) testHash = [32]byte{ 0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab, diff --git a/server.go b/server.go index 14340141..eb64ec2c 100644 --- a/server.go +++ b/server.go @@ -131,6 +131,9 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, } } + globalFeatures := lnwire.NewRawFeatureVector() + localFeatures := lnwire.NewRawFeatureVector(lnwire.InitialRoutingSync) + serializedPubKey := privKey.PubKey().SerializeCompressed() s := &server{ chanDB: chanDB, @@ -159,8 +162,10 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, outboundPeers: make(map[string]*peer), peerConnectedListeners: make(map[string][]chan<- struct{}), - globalFeatures: globalFeatures, - localFeatures: localFeatures, + globalFeatures: lnwire.NewFeatureVector(globalFeatures, + lnwire.GlobalFeatures), + localFeatures: lnwire.NewFeatureVector(localFeatures, + lnwire.LocalFeatures), quit: make(chan struct{}), } @@ -234,7 +239,7 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, Addresses: selfAddrs, PubKey: privKey.PubKey(), Alias: alias.String(), - Features: globalFeatures, + Features: s.globalFeatures, } // If our information has changed since our last boot, then we'll @@ -247,7 +252,7 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, Addresses: selfNode.Addresses, NodeID: selfNode.PubKey, Alias: alias, - Features: selfNode.Features, + Features: selfNode.Features.RawFeatureVector, } selfNode.AuthSig, err = discovery.SignAnnouncement(s.nodeSigner, s.identityPriv.PubKey(), nodeAnn,