multi: Update lnd to use new feature vector API.

This commit is contained in:
Jim Posen 2017-10-11 11:37:54 -07:00 committed by Olaoluwa Osuntokun
parent af49752d4d
commit 9fd77a6e40
16 changed files with 49 additions and 63 deletions

View File

@ -145,8 +145,9 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
IP: bytes.Repeat([]byte("a"), 16), IP: bytes.Repeat([]byte("a"), 16),
}, },
}, },
Features: lnwire.NewFeatureVector(nil), Features: lnwire.NewFeatureVector(nil,
AuthSig: testSig, lnwire.GlobalFeatures),
AuthSig: testSig,
} }
if err := d.db.AddLightningNode(graphNode); err != nil { if err := d.db.AddLightningNode(graphNode); err != nil {
return nil, err return nil, err
@ -170,7 +171,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
IP: bytes.Repeat([]byte("a"), 16), IP: bytes.Repeat([]byte("a"), 16),
}, },
}, },
Features: lnwire.NewFeatureVector(nil), Features: lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures),
AuthSig: testSig, AuthSig: testSig,
} }
if err := d.db.AddLightningNode(dbNode); err != nil { if err := d.db.AddLightningNode(dbNode); err != nil {

View File

@ -1592,10 +1592,12 @@ func deserializeLightningNode(r io.Reader) (*LightningNode, error) {
return nil, err return nil, err
} }
node.Features, err = lnwire.NewFeatureVectorFromReader(r) fv := lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures)
err = fv.Decode(r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
node.Features = fv
if _, err := r.Read(scratch[:2]); err != nil { if _, err := r.Read(scratch[:2]); err != nil {
return nil, err return nil, err

View File

@ -37,7 +37,7 @@ var (
_, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10) _, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10)
_, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10) _, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10)
testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures)
) )
func createTestVertex(db *DB) (*LightningNode, error) { func createTestVertex(db *DB) (*LightningNode, error) {

View File

@ -647,6 +647,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
} }
} }
features := lnwire.NewFeatureVector(msg.Features, lnwire.GlobalFeatures)
node := &channeldb.LightningNode{ node := &channeldb.LightningNode{
HaveNodeAnnouncement: true, HaveNodeAnnouncement: true,
LastUpdate: time.Unix(int64(msg.Timestamp), 0), LastUpdate: time.Unix(int64(msg.Timestamp), 0),
@ -654,7 +655,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(nMsg *networkMsg) []l
PubKey: msg.NodeID, PubKey: msg.NodeID,
Alias: msg.Alias.String(), Alias: msg.Alias.String(),
AuthSig: msg.Signature, AuthSig: msg.Signature,
Features: msg.Features, Features: features,
} }
if err := d.cfg.Router.AddNode(node); err != nil { if err := d.cfg.Router.AddNode(node); err != nil {
@ -1178,7 +1179,7 @@ func (d *AuthenticatedGossiper) synchronizeWithNode(syncReq *syncRequest) error
Addresses: node.Addresses, Addresses: node.Addresses,
NodeID: node.PubKey, NodeID: node.PubKey,
Alias: alias, Alias: alias,
Features: node.Features, Features: node.Features.RawFeatureVector,
} }
announceMessages = append(announceMessages, ann) announceMessages = append(announceMessages, ann)
@ -1258,7 +1259,7 @@ func (d *AuthenticatedGossiper) updateChannel(info *channeldb.ChannelEdgeInfo,
NodeID2: info.NodeKey2, NodeID2: info.NodeKey2,
ChainHash: info.ChainHash, ChainHash: info.ChainHash,
BitcoinKey1: info.BitcoinKey1, BitcoinKey1: info.BitcoinKey1,
Features: lnwire.NewFeatureVector([]lnwire.Feature{}), Features: lnwire.NewRawFeatureVector(),
BitcoinKey2: info.BitcoinKey2, BitcoinKey2: info.BitcoinKey2,
} }
} }

View File

@ -31,7 +31,7 @@ var (
testAddr = &net.TCPAddr{IP: (net.IP)([]byte{0xA, 0x0, 0x0, 0x1}), testAddr = &net.TCPAddr{IP: (net.IP)([]byte{0xA, 0x0, 0x0, 0x1}),
Port: 9000} Port: 9000}
testAddrs = []net.Addr{testAddr} testAddrs = []net.Addr{testAddr}
testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) testFeatures = lnwire.NewRawFeatureVector()
testSig = &btcec.Signature{ testSig = &btcec.Signature{
R: new(big.Int), R: new(big.Int),
S: new(big.Int), S: new(big.Int),

View File

@ -32,7 +32,7 @@ func createChanAnnouncement(chanProof *channeldb.ChannelAuthProof,
NodeID2: chanInfo.NodeKey2, NodeID2: chanInfo.NodeKey2,
ChainHash: chanInfo.ChainHash, ChainHash: chanInfo.ChainHash,
BitcoinKey1: chanInfo.BitcoinKey1, BitcoinKey1: chanInfo.BitcoinKey1,
Features: lnwire.NewFeatureVector([]lnwire.Feature{}), Features: lnwire.NewRawFeatureVector(),
BitcoinKey2: chanInfo.BitcoinKey2, BitcoinKey2: chanInfo.BitcoinKey2,
} }

View File

@ -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,
},
})

View File

@ -1738,7 +1738,7 @@ func (f *fundingManager) newChanAnnouncement(localPubKey, remotePubKey *btcec.Pu
// within the blockchain. // within the blockchain.
chanAnn := &lnwire.ChannelAnnouncement{ chanAnn := &lnwire.ChannelAnnouncement{
ShortChannelID: shortChanID, ShortChannelID: shortChanID,
Features: lnwire.NewFeatureVector([]lnwire.Feature{}), Features: lnwire.NewRawFeatureVector(),
ChainHash: chainHash, ChainHash: chainHash,
} }

View File

@ -29,7 +29,7 @@ type ChannelAnnouncement struct {
// by the target node. This field can be used to signal the type of the // 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 // channel, or modifications to the fields that would normally follow
// this vector. // this vector.
Features *FeatureVector Features *RawFeatureVector
// ChainHash denotes the target chain that this channel was opened // ChainHash denotes the target chain that this channel was opened
// within. This value should be the genesis hash of the target chain. // within. This value should be the genesis hash of the target chain.

View File

@ -170,10 +170,14 @@ type FeatureVector struct {
} }
// NewFeatureVector constructs a new FeatureVector from a raw feature vector and // 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, func NewFeatureVector(featureVector *RawFeatureVector,
featureNames map[FeatureBit]string) *FeatureVector { featureNames map[FeatureBit]string) *FeatureVector {
if featureVector == nil {
featureVector = NewRawFeatureVector()
}
return &FeatureVector{ return &FeatureVector{
RawFeatureVector: featureVector, RawFeatureVector: featureVector,
featureNames: featureNames, featureNames: featureNames,

View File

@ -9,15 +9,15 @@ import "io"
type Init struct { type Init struct {
// GlobalFeatures is feature vector which affects HTLCs and thus are // GlobalFeatures is feature vector which affects HTLCs and thus are
// also advertised to other nodes. // also advertised to other nodes.
GlobalFeatures *FeatureVector GlobalFeatures *RawFeatureVector
// LocalFeatures is feature vector which only affect the protocol // LocalFeatures is feature vector which only affect the protocol
// between two nodes. // between two nodes.
LocalFeatures *FeatureVector LocalFeatures *RawFeatureVector
} }
// NewInitMessage creates new instance of init message object. // NewInitMessage creates new instance of init message object.
func NewInitMessage(gf, lf *FeatureVector) *Init { func NewInitMessage(gf *RawFeatureVector, lf *RawFeatureVector) *Init {
return &Init{ return &Init{
GlobalFeatures: gf, GlobalFeatures: gf,
LocalFeatures: lf, LocalFeatures: lf,

View File

@ -215,7 +215,7 @@ func writeElement(w io.Writer, element interface{}) error {
if err := wire.WriteVarBytes(w, 0, e); err != nil { if err := wire.WriteVarBytes(w, 0, e); err != nil {
return err return err
} }
case *FeatureVector: case *RawFeatureVector:
if e == nil { if e == nil {
return fmt.Errorf("cannot write nil feature vector") return fmt.Errorf("cannot write nil feature vector")
} }
@ -435,8 +435,9 @@ func readElement(r io.Reader, element interface{}) error {
return err return err
} }
*e = pubKey *e = pubKey
case **FeatureVector: case **RawFeatureVector:
f, err := NewFeatureVectorFromReader(r) f := NewRawFeatureVector()
err = f.Decode(r)
if err != nil { if err != nil {
return err return err
} }

View File

@ -51,16 +51,14 @@ func randPubKey() (*btcec.PublicKey, error) {
return priv.PubKey(), nil return priv.PubKey(), nil
} }
func randFeatureVector(r *rand.Rand) *FeatureVector { func randRawFeatureVector(r *rand.Rand) *RawFeatureVector {
numFeatures := r.Int31n(10000) featureVec := NewRawFeatureVector()
features := make([]Feature, numFeatures) for i := 0; i < 10000; i++ {
for i := int32(0); i < numFeatures; i++ { if r.Int31n(2) == 0 {
features[i] = Feature{ featureVec.Set(FeatureBit(i))
Flag: featureFlag(rand.Int31n(2) + 1),
} }
} }
return featureVec
return NewFeatureVector(features)
} }
func TestMaxOutPointIndex(t *testing.T) { func TestMaxOutPointIndex(t *testing.T) {
@ -139,11 +137,9 @@ func TestLightningWireProtocol(t *testing.T) {
customTypeGen := map[MessageType]func([]reflect.Value, *rand.Rand){ customTypeGen := map[MessageType]func([]reflect.Value, *rand.Rand){
MsgInit: func(v []reflect.Value, r *rand.Rand) { MsgInit: func(v []reflect.Value, r *rand.Rand) {
req := NewInitMessage( req := NewInitMessage(
randFeatureVector(r), randRawFeatureVector(r),
randFeatureVector(r), randRawFeatureVector(r),
) )
req.GlobalFeatures.featuresMap = nil
req.LocalFeatures.featuresMap = nil
v[0] = reflect.ValueOf(*req) v[0] = reflect.ValueOf(*req)
}, },
@ -351,9 +347,8 @@ func TestLightningWireProtocol(t *testing.T) {
MsgChannelAnnouncement: func(v []reflect.Value, r *rand.Rand) { MsgChannelAnnouncement: func(v []reflect.Value, r *rand.Rand) {
req := ChannelAnnouncement{ req := ChannelAnnouncement{
ShortChannelID: NewShortChanIDFromInt(uint64(r.Int63())), ShortChannelID: NewShortChanIDFromInt(uint64(r.Int63())),
Features: randFeatureVector(r), Features: randRawFeatureVector(r),
} }
req.Features.featuresMap = nil
req.NodeSig1 = testSig req.NodeSig1 = testSig
req.NodeSig2 = testSig req.NodeSig2 = testSig
req.BitcoinSig1 = testSig req.BitcoinSig1 = testSig
@ -396,7 +391,7 @@ func TestLightningWireProtocol(t *testing.T) {
req := NodeAnnouncement{ req := NodeAnnouncement{
Signature: testSig, Signature: testSig,
Features: randFeatureVector(r), Features: randRawFeatureVector(r),
Timestamp: uint32(r.Int31()), Timestamp: uint32(r.Int31()),
Alias: a, Alias: a,
RGBColor: RGB{ RGBColor: RGB{
@ -407,7 +402,6 @@ func TestLightningWireProtocol(t *testing.T) {
// TODO(roasbeef): proper gen rand addrs // TODO(roasbeef): proper gen rand addrs
Addresses: testAddrs, Addresses: testAddrs,
} }
req.Features.featuresMap = nil
var err error var err error
req.NodeID, err = randPubKey() req.NodeID, err = randPubKey()

View File

@ -59,7 +59,7 @@ type NodeAnnouncement struct {
Signature *btcec.Signature Signature *btcec.Signature
// Features is the list of protocol features this node supports. // Features is the list of protocol features this node supports.
Features *FeatureVector Features *RawFeatureVector
// Timestamp allows ordering in the case of multiple announcements. // Timestamp allows ordering in the case of multiple announcements.
Timestamp uint32 Timestamp uint32

View File

@ -26,7 +26,7 @@ var (
Port: 9000} Port: 9000}
testAddrs = []net.Addr{testAddr} testAddrs = []net.Addr{testAddr}
testFeatures = lnwire.NewFeatureVector([]lnwire.Feature{}) testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures)
testHash = [32]byte{ testHash = [32]byte{
0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab, 0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,

View File

@ -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() serializedPubKey := privKey.PubKey().SerializeCompressed()
s := &server{ s := &server{
chanDB: chanDB, chanDB: chanDB,
@ -159,8 +162,10 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
outboundPeers: make(map[string]*peer), outboundPeers: make(map[string]*peer),
peerConnectedListeners: make(map[string][]chan<- struct{}), peerConnectedListeners: make(map[string][]chan<- struct{}),
globalFeatures: globalFeatures, globalFeatures: lnwire.NewFeatureVector(globalFeatures,
localFeatures: localFeatures, lnwire.GlobalFeatures),
localFeatures: lnwire.NewFeatureVector(localFeatures,
lnwire.LocalFeatures),
quit: make(chan struct{}), quit: make(chan struct{}),
} }
@ -234,7 +239,7 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
Addresses: selfAddrs, Addresses: selfAddrs,
PubKey: privKey.PubKey(), PubKey: privKey.PubKey(),
Alias: alias.String(), Alias: alias.String(),
Features: globalFeatures, Features: s.globalFeatures,
} }
// If our information has changed since our last boot, then we'll // 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, Addresses: selfNode.Addresses,
NodeID: selfNode.PubKey, NodeID: selfNode.PubKey,
Alias: alias, Alias: alias,
Features: selfNode.Features, Features: selfNode.Features.RawFeatureVector,
} }
selfNode.AuthSig, err = discovery.SignAnnouncement(s.nodeSigner, selfNode.AuthSig, err = discovery.SignAnnouncement(s.nodeSigner,
s.identityPriv.PubKey(), nodeAnn, s.identityPriv.PubKey(), nodeAnn,