Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions channeldb/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ var (

// ErrNodeAliasNotFound is returned when alias for node can't be found.
ErrNodeAliasNotFound = fmt.Errorf("alias for node not found")

// ErrUnknownAddressType is returned when a node's addressType is not
// an expected value.
ErrUnknownAddressType = fmt.Errorf("address type cannot be resolved")
)
128 changes: 109 additions & 19 deletions channeldb/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/boltdb/bolt"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire"
Expand Down Expand Up @@ -114,6 +115,16 @@ type ChannelGraph struct {
// * LRU cache for edges?
}

// addressType specifies the network protocol and version that should be used
// when connecting to a node at a particular address.
type addressType uint8

const (
tcp4Addr addressType = 0
tcp6Addr addressType = 1
onionAddr addressType = 2
)

// ForEachChannel iterates through all the channel edges stored within the
// graph and invokes the passed callback for each edge. The callback takes two
// edges as since this is a directed graph, both the in/out edges are visited.
Expand Down Expand Up @@ -801,7 +812,7 @@ type LightningNode struct {
LastUpdate time.Time

// Address is the TCP address this node is reachable over.
Address *net.TCPAddr
Addresses []net.Addr

// PubKey is the node's long-term identity public key. This key will be
// used to authenticated any advertisements/updates sent by the node.
Expand All @@ -820,6 +831,9 @@ type LightningNode struct {
// TODO(roasbeef): hook into serialization once full verification is in
AuthSig *btcec.Signature

// Features is the list of protocol features supported by this node.
Features *lnwire.FeatureVector

db *DB

// TODO(roasbeef): discovery will need storage to keep it's last IP
Expand Down Expand Up @@ -1265,7 +1279,7 @@ func (c *ChannelGraph) NewChannelEdgePolicy() *ChannelEdgePolicy {

func putLightningNode(nodeBucket *bolt.Bucket, aliasBucket *bolt.Bucket, node *LightningNode) error {
var (
scratch [8]byte
scratch [16]byte
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it was changed to 16?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndrewSamokhvalov thanks for the review and the comments! Good catch here. I made a change that required the additional bytes, and then backed that out and left this in. Fixed now.

b bytes.Buffer
)

Expand All @@ -1276,13 +1290,8 @@ func putLightningNode(nodeBucket *bolt.Bucket, aliasBucket *bolt.Bucket, node *L
}

updateUnix := uint64(node.LastUpdate.Unix())
byteOrder.PutUint64(scratch[:], updateUnix)
if _, err := b.Write(scratch[:]); err != nil {
return err
}

addrString := node.Address.String()
if err := wire.WriteVarString(&b, 0, addrString); err != nil {
byteOrder.PutUint64(scratch[:8], updateUnix)
if _, err := b.Write(scratch[:8]); err != nil {
return err
}

Expand All @@ -1304,6 +1313,45 @@ func putLightningNode(nodeBucket *bolt.Bucket, aliasBucket *bolt.Bucket, node *L
return err
}

if err := node.Features.Encode(&b); err != nil {
return err
}

numAddresses := uint16(len(node.Addresses))
byteOrder.PutUint16(scratch[:2], numAddresses)
if _, err := b.Write(scratch[:2]); err != nil {
return err
}

for _, address := range node.Addresses {
if address.Network() == "tcp" {
if address.(*net.TCPAddr).IP.To4() != nil {
scratch[0] = uint8(tcp4Addr)
if _, err := b.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:4], address.(*net.TCPAddr).IP.To4())
if _, err := b.Write(scratch[:4]); err != nil {
return err
}
} else {
scratch[0] = uint8(tcp6Addr)
if _, err := b.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:], address.(*net.TCPAddr).IP.To16())
if _, err := b.Write(scratch[:]); err != nil {
return err
}
}
byteOrder.PutUint16(scratch[:2],
uint16(address.(*net.TCPAddr).Port))
if _, err := b.Write(scratch[:2]); err != nil {
return err
}
}
}

return nodeBucket.Put(nodePub, b.Bytes())
}

Expand All @@ -1321,28 +1369,20 @@ func fetchLightningNode(nodeBucket *bolt.Bucket,

func deserializeLightningNode(r io.Reader) (*LightningNode, error) {
node := &LightningNode{}

var scratch [8]byte

if _, err := r.Read(scratch[:]); err != nil {
return nil, err
}

unix := int64(byteOrder.Uint64(scratch[:]))
node.LastUpdate = time.Unix(unix, 0)

addrString, err := wire.ReadVarString(r, 0)
if err != nil {
return nil, err
}
node.Address, err = net.ResolveTCPAddr("tcp", addrString)
if err != nil {
return nil, err
}

var pub [33]byte
if _, err := r.Read(pub[:]); err != nil {
return nil, err
}
var err error
node.PubKey, err = btcec.ParsePubKey(pub[:], btcec.S256())
if err != nil {
return nil, err
Expand All @@ -1363,6 +1403,56 @@ func deserializeLightningNode(r io.Reader) (*LightningNode, error) {
return nil, err
}

node.Features, err = lnwire.NewFeatureVectorFromReader(r)
if err != nil {
return nil, err
}

if _, err := r.Read(scratch[:2]); err != nil {
return nil, err
}
numAddresses := int(byteOrder.Uint16(scratch[:2]))

var addresses []net.Addr
for i := 0; i < numAddresses; i++ {
var address net.Addr
if _, err := r.Read(scratch[:1]); err != nil {
return nil, err
}

switch addressType(scratch[0]) {
case tcp4Addr:
addr := &net.TCPAddr{}
var ip [4]byte
if _, err := r.Read(ip[:]); err != nil {
return nil, err
}
addr.IP = (net.IP)(ip[:])
if _, err := r.Read(scratch[:2]); err != nil {
return nil, err
}
addr.Port = int(byteOrder.Uint16(scratch[:2]))
address = addr
case tcp6Addr:
addr := &net.TCPAddr{}
var ip [16]byte
if _, err := r.Read(ip[:]); err != nil {
return nil, err
}
addr.IP = (net.IP)(ip[:])
if _, err := r.Read(scratch[:2]); err != nil {
return nil, err
}
addr.Port = int(byteOrder.Uint16(scratch[:2]))
address = addr
default:
return nil, ErrUnknownAddressType
}

addresses = append(addresses, address)
}
node.Addresses = addresses

return node, nil
}

Expand Down
Loading