2015-12-26 21:35:15 +03:00
|
|
|
package channeldb
|
2016-10-26 00:04:42 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-06-15 05:47:00 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2016-10-26 00:04:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-01-13 08:01:50 +03:00
|
|
|
// nodeInfoBucket stores metadata pertaining to nodes that we've had
|
2016-10-26 00:04:42 +03:00
|
|
|
// direct channel-based correspondence with. This bucket allows one to
|
|
|
|
// query for all open channels pertaining to the node by exploring each
|
|
|
|
// node's sub-bucket within the openChanBucket.
|
|
|
|
nodeInfoBucket = []byte("nib")
|
|
|
|
)
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// LinkNode stores metadata related to node's that we have/had a direct
|
2016-10-26 00:04:42 +03:00
|
|
|
// channel open with. Information such as the Bitcoin network the node
|
|
|
|
// advertised, and its identity public key are also stored. Additionally, this
|
|
|
|
// struct and the bucket its stored within have store data similar to that of
|
2018-02-07 06:11:11 +03:00
|
|
|
// Bitcoin's addrmanager. The TCP address information stored within the struct
|
2016-10-26 00:04:42 +03:00
|
|
|
// can be used to establish persistent connections will all channel
|
2017-01-13 08:01:50 +03:00
|
|
|
// counterparties on daemon startup.
|
2016-10-26 00:04:42 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): also add current OnionKey plus rotation schedule?
|
|
|
|
// TODO(roasbeef): add bitfield for supported services
|
|
|
|
// * possibly add a wire.NetAddress type, type
|
|
|
|
type LinkNode struct {
|
|
|
|
// Network indicates the Bitcoin network that the LinkNode advertises
|
|
|
|
// for incoming channel creation.
|
|
|
|
Network wire.BitcoinNet
|
|
|
|
|
|
|
|
// IdentityPub is the node's current identity public key. Any
|
|
|
|
// channel/topology related information received by this node MUST be
|
|
|
|
// signed by this public key.
|
|
|
|
IdentityPub *btcec.PublicKey
|
|
|
|
|
|
|
|
// LastSeen tracks the last time this node was seen within the network.
|
|
|
|
// A node should be marked as seen if the daemon either is able to
|
|
|
|
// establish an outgoing connection to the node or receives a new
|
|
|
|
// incoming connection from the node. This timestamp (stored in unix
|
|
|
|
// epoch) may be used within a heuristic which aims to determine when a
|
|
|
|
// channel should be unilaterally closed due to inactivity.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): replace with block hash/height?
|
2016-10-27 00:53:10 +03:00
|
|
|
// * possibly add a time-value metric into the heuristic?
|
2016-10-26 00:04:42 +03:00
|
|
|
LastSeen time.Time
|
|
|
|
|
|
|
|
// Addresses is a list of IP address in which either we were able to
|
|
|
|
// reach the node over in the past, OR we received an incoming
|
|
|
|
// authenticated connection for the stored identity public key.
|
2018-02-03 09:24:43 +03:00
|
|
|
Addresses []net.Addr
|
2016-10-26 00:04:42 +03:00
|
|
|
|
|
|
|
db *DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLinkNode creates a new LinkNode from the provided parameters, which is
|
|
|
|
// backed by an instance of channeldb.
|
|
|
|
func (db *DB) NewLinkNode(bitNet wire.BitcoinNet, pub *btcec.PublicKey,
|
2018-02-03 09:24:43 +03:00
|
|
|
addr net.Addr) *LinkNode {
|
2016-10-26 00:04:42 +03:00
|
|
|
|
|
|
|
return &LinkNode{
|
|
|
|
Network: bitNet,
|
|
|
|
IdentityPub: pub,
|
|
|
|
LastSeen: time.Now(),
|
2018-02-03 09:24:43 +03:00
|
|
|
Addresses: []net.Addr{addr},
|
2016-10-26 00:04:42 +03:00
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLastSeen updates the last time this node was directly encountered on
|
|
|
|
// the Lightning Network.
|
|
|
|
func (l *LinkNode) UpdateLastSeen(lastSeen time.Time) error {
|
|
|
|
l.LastSeen = lastSeen
|
|
|
|
|
|
|
|
return l.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddress appends the specified TCP address to the list of known addresses
|
|
|
|
// this node is/was known to be reachable at.
|
2018-05-02 09:33:17 +03:00
|
|
|
func (l *LinkNode) AddAddress(addr net.Addr) error {
|
2016-10-27 00:32:42 +03:00
|
|
|
for _, a := range l.Addresses {
|
|
|
|
if a.String() == addr.String() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 00:04:42 +03:00
|
|
|
l.Addresses = append(l.Addresses, addr)
|
|
|
|
|
|
|
|
return l.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync performs a full database sync which writes the current up-to-date data
|
|
|
|
// within the struct to the database.
|
|
|
|
func (l *LinkNode) Sync() error {
|
|
|
|
|
|
|
|
// Finally update the database by storing the link node and updating
|
|
|
|
// any relevant indexes.
|
2016-11-28 05:32:45 +03:00
|
|
|
return l.db.Update(func(tx *bolt.Tx) error {
|
2016-10-26 00:04:42 +03:00
|
|
|
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
|
2016-12-14 07:48:49 +03:00
|
|
|
if nodeMetaBucket == nil {
|
|
|
|
return ErrLinkNodesNotFound
|
2016-10-26 00:04:42 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 00:36:30 +03:00
|
|
|
return putLinkNode(nodeMetaBucket, l)
|
2016-10-26 00:04:42 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-27 00:36:30 +03:00
|
|
|
// putLinkNode serializes then writes the encoded version of the passed link
|
|
|
|
// node into the nodeMetaBucket. This function is provided in order to allow
|
|
|
|
// the ability to re-use a database transaction across many operations.
|
|
|
|
func putLinkNode(nodeMetaBucket *bolt.Bucket, l *LinkNode) error {
|
|
|
|
// First serialize the LinkNode into its raw-bytes encoding.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := serializeLinkNode(&b, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// Finally insert the link-node into the node metadata bucket keyed
|
2016-10-27 00:36:30 +03:00
|
|
|
// according to the its pubkey serialized in compressed form.
|
|
|
|
nodePub := l.IdentityPub.SerializeCompressed()
|
|
|
|
return nodeMetaBucket.Put(nodePub, b.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-06-12 00:47:15 +03:00
|
|
|
// DeleteLinkNode removes the link node with the given identity from the
|
|
|
|
// database.
|
|
|
|
func (d *DB) DeleteLinkNode(identity *btcec.PublicKey) error {
|
|
|
|
return d.Update(func(tx *bolt.Tx) error {
|
|
|
|
return d.deleteLinkNode(tx, identity)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) deleteLinkNode(tx *bolt.Tx, identity *btcec.PublicKey) error {
|
|
|
|
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
|
|
|
|
if nodeMetaBucket == nil {
|
|
|
|
return ErrLinkNodesNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey := identity.SerializeCompressed()
|
|
|
|
return nodeMetaBucket.Delete(pubKey)
|
|
|
|
}
|
|
|
|
|
2016-10-26 00:04:42 +03:00
|
|
|
// FetchLinkNode attempts to lookup the data for a LinkNode based on a target
|
|
|
|
// identity public key. If a particular LinkNode for the passed identity public
|
|
|
|
// key cannot be found, then ErrNodeNotFound if returned.
|
|
|
|
func (db *DB) FetchLinkNode(identity *btcec.PublicKey) (*LinkNode, error) {
|
|
|
|
var (
|
|
|
|
node *LinkNode
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2016-11-28 05:32:45 +03:00
|
|
|
err = db.View(func(tx *bolt.Tx) error {
|
2017-01-13 08:01:50 +03:00
|
|
|
// First fetch the bucket for storing node metadata, bailing
|
2016-10-26 00:04:42 +03:00
|
|
|
// out early if it hasn't been created yet.
|
|
|
|
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
|
2016-12-14 07:48:49 +03:00
|
|
|
if nodeMetaBucket == nil {
|
|
|
|
return ErrLinkNodesNotFound
|
2016-10-26 00:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a link node for that particular public key cannot be
|
2018-04-18 05:02:04 +03:00
|
|
|
// located, then exit early with an ErrNodeNotFound.
|
2016-10-26 00:04:42 +03:00
|
|
|
pubKey := identity.SerializeCompressed()
|
|
|
|
nodeBytes := nodeMetaBucket.Get(pubKey)
|
|
|
|
if nodeBytes == nil {
|
|
|
|
return ErrNodeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, decode an allocate a fresh LinkNode object to be
|
|
|
|
// returned to the caller.
|
|
|
|
nodeReader := bytes.NewReader(nodeBytes)
|
|
|
|
node, err = deserializeLinkNode(nodeReader)
|
2017-02-23 21:59:50 +03:00
|
|
|
return err
|
2016-10-26 00:04:42 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2018-06-15 05:47:00 +03:00
|
|
|
// FetchAllLinkNodes starts a new database transaction to fetch all nodes with
|
|
|
|
// whom we have active channels with.
|
2016-10-26 00:04:42 +03:00
|
|
|
func (db *DB) FetchAllLinkNodes() ([]*LinkNode, error) {
|
|
|
|
var linkNodes []*LinkNode
|
2016-11-28 05:32:45 +03:00
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
2018-06-15 05:47:00 +03:00
|
|
|
nodes, err := db.fetchAllLinkNodes(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-26 00:04:42 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 05:47:00 +03:00
|
|
|
linkNodes = nodes
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-14 07:48:49 +03:00
|
|
|
|
2018-06-15 05:47:00 +03:00
|
|
|
return linkNodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchAllLinkNodes uses an existing database transaction to fetch all nodes
|
|
|
|
// with whom we have active channels with.
|
|
|
|
func (db *DB) fetchAllLinkNodes(tx *bolt.Tx) ([]*LinkNode, error) {
|
|
|
|
nodeMetaBucket := tx.Bucket(nodeInfoBucket)
|
|
|
|
if nodeMetaBucket == nil {
|
|
|
|
return nil, ErrLinkNodesNotFound
|
|
|
|
}
|
2016-10-26 00:04:42 +03:00
|
|
|
|
2018-06-15 05:47:00 +03:00
|
|
|
var linkNodes []*LinkNode
|
|
|
|
err := nodeMetaBucket.ForEach(func(k, v []byte) error {
|
|
|
|
if v == nil {
|
2016-10-26 00:04:42 +03:00
|
|
|
return nil
|
2018-06-15 05:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeReader := bytes.NewReader(v)
|
|
|
|
linkNode, err := deserializeLinkNode(nodeReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
linkNodes = append(linkNodes, linkNode)
|
|
|
|
return nil
|
2016-10-26 00:04:42 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return linkNodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeLinkNode(w io.Writer, l *LinkNode) error {
|
|
|
|
var buf [8]byte
|
|
|
|
|
|
|
|
byteOrder.PutUint32(buf[:4], uint32(l.Network))
|
|
|
|
if _, err := w.Write(buf[:4]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedID := l.IdentityPub.SerializeCompressed()
|
|
|
|
if _, err := w.Write(serializedID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
seenUnix := uint64(l.LastSeen.Unix())
|
|
|
|
byteOrder.PutUint64(buf[:], seenUnix)
|
|
|
|
if _, err := w.Write(buf[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
numAddrs := uint32(len(l.Addresses))
|
|
|
|
byteOrder.PutUint32(buf[:4], numAddrs)
|
|
|
|
if _, err := w.Write(buf[:4]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range l.Addresses {
|
2018-01-30 03:00:23 +03:00
|
|
|
if err := serializeAddr(w, addr); err != nil {
|
2016-10-26 00:04:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeLinkNode(r io.Reader) (*LinkNode, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
buf [8]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
node := &LinkNode{}
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(r, buf[:4]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.Network = wire.BitcoinNet(byteOrder.Uint32(buf[:4]))
|
|
|
|
|
|
|
|
var pub [33]byte
|
|
|
|
if _, err := io.ReadFull(r, pub[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.IdentityPub, err = btcec.ParsePubKey(pub[:], btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(r, buf[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node.LastSeen = time.Unix(int64(byteOrder.Uint64(buf[:])), 0)
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(r, buf[:4]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
numAddrs := byteOrder.Uint32(buf[:4])
|
|
|
|
|
2018-02-03 09:24:43 +03:00
|
|
|
node.Addresses = make([]net.Addr, numAddrs)
|
2016-10-26 00:04:42 +03:00
|
|
|
for i := uint32(0); i < numAddrs; i++ {
|
2018-01-30 03:00:23 +03:00
|
|
|
addr, err := deserializeAddr(r)
|
2016-10-26 00:04:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-03 09:24:43 +03:00
|
|
|
node.Addresses[i] = addr
|
2016-10-26 00:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|