routing: Move tools inside lnd. Refactor and delete unneeded stuff
Use [33]byte for graph vertex representation. Delete unneeded stuff: 1. DeepEqual for graph comparison 2. EdgePath 3. 2-thread BFS 4. Table transfer messages and neighborhood radius 5. Beacons Refactor: 1. Change ID to Vertex 2. Test use table driven approach 3. Add comments 4. Make graph internal representation private 5. Use wire.OutPoint as EdgeId 6. Decouple routing messages from routing implementation 7. Delete Async methods 8. Delete unneeded channels and priority buffer from manager 9. Delete unneeded interfaces in internal graph realisation 10. Renamed ID to Vertex
This commit is contained in:
1 parent
da3028e10c
commit
327768f4ad
37 files changed
+3152
-308
No files matched your search
@@ -0,0 +1,125 @@
|
||||
// Copyright (c) 2016 Bitfury Group Limited
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
package visualizer
|
||||
|
||||
// Configuration used for graph visualisation.
|
||||
type VisualizerConfig struct {
|
||||
// General options.
|
||||
GlobalCfg *GlobalConfig
|
||||
// Options for node visualisation.
|
||||
NodeCfg *NodeConfig
|
||||
// Options ofr highlighted nodes visualisation.
|
||||
HighlightedNodeCfg *NodeConfig
|
||||
// Options for edges visualisation.
|
||||
EdgeCfg *EdgeConfig
|
||||
// Options for highlighted edges visualisation.
|
||||
HighlightedEdgeCfg *EdgeConfig
|
||||
// Indicate whether shortcuts should be used.
|
||||
// Shortcut of a string is a prefix of a string that is unique
|
||||
// for a given set of strings.
|
||||
EnableShortcut bool
|
||||
}
|
||||
|
||||
var DefaultVisualizerConfig VisualizerConfig = VisualizerConfig{
|
||||
GlobalCfg: &DefaultGlobalConfig,
|
||||
NodeCfg: &DefaultNodeConfig,
|
||||
HighlightedNodeCfg: &DefaultHighlightedNodeConfig,
|
||||
EdgeCfg: &DefaultEdgeConfig,
|
||||
HighlightedEdgeCfg: &DefaultHighlightedEdgeConfig,
|
||||
EnableShortcut: false,
|
||||
}
|
||||
|
||||
type GlobalConfig struct {
|
||||
// Title of an image.
|
||||
Name string
|
||||
Dir bool
|
||||
// Allow multigraph.
|
||||
Strict bool
|
||||
// Background color.
|
||||
BgColor string
|
||||
}
|
||||
|
||||
var DefaultGlobalConfig GlobalConfig = GlobalConfig{
|
||||
Name: `"Routing Table"`,
|
||||
Dir: true,
|
||||
Strict: false, // Allow multigraphs
|
||||
BgColor: "black",
|
||||
}
|
||||
|
||||
type NodeConfig struct {
|
||||
Shape string
|
||||
Style string
|
||||
FontSize string
|
||||
FontColor string
|
||||
Color string
|
||||
FillColor string
|
||||
}
|
||||
|
||||
var DefaultNodeConfig NodeConfig = NodeConfig{
|
||||
Shape: "circle",
|
||||
Style: "filled",
|
||||
FontSize: "12",
|
||||
FontColor: "black",
|
||||
Color: "white",
|
||||
FillColor: "white",
|
||||
}
|
||||
|
||||
var DefaultHighlightedNodeConfig NodeConfig = NodeConfig{
|
||||
Shape: "circle",
|
||||
Style: "filled",
|
||||
FontSize: "12",
|
||||
FontColor: "black",
|
||||
Color: "blue",
|
||||
FillColor: "blue",
|
||||
}
|
||||
|
||||
type EdgeConfig struct {
|
||||
FontSize string
|
||||
FontColor string
|
||||
Scale string
|
||||
Dir string
|
||||
Style string
|
||||
Color string
|
||||
}
|
||||
|
||||
var DefaultEdgeConfig EdgeConfig = EdgeConfig{
|
||||
FontSize: "12",
|
||||
FontColor: "gold",
|
||||
Scale: "2.5",
|
||||
Dir: "none",
|
||||
Style: "solid",
|
||||
Color: "white",
|
||||
}
|
||||
|
||||
var DefaultHighlightedEdgeConfig EdgeConfig = EdgeConfig{
|
||||
FontSize: "12",
|
||||
FontColor: "gold",
|
||||
Scale: "2.5",
|
||||
Dir: "none",
|
||||
Style: "solid",
|
||||
Color: "blue",
|
||||
}
|
||||
|
||||
func SupportedFormatsAsMap() map[string]struct{} {
|
||||
rez := make(map[string]struct{})
|
||||
for _, format := range supportedFormats {
|
||||
rez[format] = struct{}{}
|
||||
}
|
||||
return rez
|
||||
}
|
||||
|
||||
// SupportedFormats contains list of image formats that can be
|
||||
// used for output.
|
||||
func SupportedFormats() []string {
|
||||
return supportedFormats
|
||||
}
|
||||
|
||||
var supportedFormats = []string{
|
||||
"bmp", "canon", "cgimage", "cmap", "cmapx", "cmapx_np", "dot", "eps", "exr",
|
||||
"fig", "gif", "gv", "icns", "ico", "imap", "imap_np", "ismap", "jp2", "jpe",
|
||||
"jpeg", "jpg", "pct", "pdf", "pic", "pict", "plain", "plain-ext", "png",
|
||||
"pov", "ps", "ps2", "psd", "sgi", "svg", "svgz", "tga", "tif", "tiff", "tk",
|
||||
"vml", "vmlz", "xdot", "xdot1.2", "xdot1.4",
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright (c) 2016 Bitfury Group Limited
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
package prefix_tree
|
||||
|
||||
import "errors"
|
||||
|
||||
// Node represents the general interface for node in prefix tree
|
||||
type Node interface {
|
||||
Name() string
|
||||
Len() int
|
||||
Child(nextSymbol string) Node
|
||||
Childs() map[string]Node
|
||||
AddChild(symbol string, child Node) error
|
||||
Parent() Node
|
||||
PreviousSymbol() string
|
||||
IsRoot() bool
|
||||
IsLeaf() bool
|
||||
IsTerminal() bool
|
||||
Terminal()
|
||||
Path() (string, error)
|
||||
}
|
||||
|
||||
type node struct {
|
||||
name string
|
||||
childs map[string]Node
|
||||
parent Node
|
||||
previousSymbol string
|
||||
isTerminal bool
|
||||
}
|
||||
|
||||
// NewNode create new node
|
||||
func NewNode(name string, parent Node, previousSymbol string, isTerminal bool) Node {
|
||||
return &node{
|
||||
name: name,
|
||||
childs: make(map[string]Node),
|
||||
parent: parent,
|
||||
previousSymbol: previousSymbol,
|
||||
isTerminal: isTerminal,
|
||||
}
|
||||
}
|
||||
|
||||
func (nd *node) Name() string {
|
||||
return nd.name
|
||||
}
|
||||
|
||||
func (nd *node) Len() int {
|
||||
return len(nd.childs)
|
||||
}
|
||||
|
||||
func (nd *node) Child(nextSymbol string) Node {
|
||||
if _, ok := nd.childs[nextSymbol]; !ok {
|
||||
return nil
|
||||
}
|
||||
return nd.childs[nextSymbol]
|
||||
}
|
||||
|
||||
func (nd *node) Childs() map[string]Node {
|
||||
return nd.childs
|
||||
}
|
||||
|
||||
func (nd *node) AddChild(symbol string, child Node) error {
|
||||
if _, ok := nd.childs[symbol]; ok {
|
||||
return errors.New("Node already exists")
|
||||
}
|
||||
nd.childs[symbol] = child
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nd *node) Parent() Node {
|
||||
return nd.parent
|
||||
}
|
||||
|
||||
func (nd *node) PreviousSymbol() string {
|
||||
return nd.previousSymbol
|
||||
}
|
||||
|
||||
func (nd *node) IsRoot() bool {
|
||||
return nd.parent == nil
|
||||
}
|
||||
|
||||
func (nd *node) IsLeaf() bool {
|
||||
return len(nd.childs) == 0
|
||||
}
|
||||
|
||||
func (nd *node) IsTerminal() bool {
|
||||
return nd.isTerminal
|
||||
}
|
||||
|
||||
func (nd *node) Terminal() {
|
||||
nd.isTerminal = true
|
||||
}
|
||||
|
||||
func (nd *node) Path() (path string, err error) {
|
||||
var xNode Node = nd
|
||||
for {
|
||||
if xNode.IsRoot() {
|
||||
break
|
||||
}
|
||||
path += xNode.PreviousSymbol()
|
||||
if xNode == xNode.Parent() {
|
||||
return "", CyclicDataError
|
||||
}
|
||||
xNode = xNode.Parent()
|
||||
}
|
||||
return Reverse(path), nil
|
||||
}
|
||||
|
||||
func Reverse(reversePath string) (path string) {
|
||||
length := len(reversePath)
|
||||
for i := length - 1; i >= 0; i-- {
|
||||
path += string(reversePath[i])
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright (c) 2016 Bitfury Group Limited
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
package prefix_tree
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
CyclicDataError = errors.New("Cyclic data")
|
||||
NodeNotFoundError = errors.New("Node not found")
|
||||
NotEnoughData = errors.New("Not enough data")
|
||||
)
|
||||
|
||||
type PrefixTree interface {
|
||||
Add(str string) error
|
||||
Shortcut(fullname string) (string, error)
|
||||
Autocomplete(prefix string) (string, error)
|
||||
Len() int
|
||||
}
|
||||
|
||||
type prefixTree struct {
|
||||
root Node
|
||||
nodes int
|
||||
}
|
||||
|
||||
func NewPrefixTree() PrefixTree {
|
||||
root := NewNode("0", nil, "", false)
|
||||
return &prefixTree{root: root}
|
||||
}
|
||||
|
||||
func (tree *prefixTree) Add(str string) error {
|
||||
var node Node = tree.root
|
||||
for _, symbolRune := range str {
|
||||
symbol := string(byte(symbolRune))
|
||||
child := node.Child(symbol)
|
||||
if child == nil {
|
||||
child = NewNode(strconv.Itoa(tree.Len()), node, symbol, false)
|
||||
err := node.AddChild(symbol, child)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tree.nodes++
|
||||
}
|
||||
node = child
|
||||
}
|
||||
node.Terminal()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Find shortcut for a given string. Shortcut is unique prefix for a given string.
|
||||
// If there is only one string in prefix tree return first symbol
|
||||
func (tree *prefixTree) Shortcut(fullname string) (string, error) {
|
||||
var node Node = tree.root
|
||||
for _, symbolRune := range fullname {
|
||||
symbol := string(byte(symbolRune))
|
||||
child := node.Child(symbol)
|
||||
if child == nil {
|
||||
return "", NodeNotFoundError
|
||||
}
|
||||
node = child
|
||||
}
|
||||
if !node.IsTerminal() {
|
||||
return "", errors.New("Node MUST be terminal")
|
||||
}
|
||||
if !node.IsLeaf() {
|
||||
return fullname, nil
|
||||
}
|
||||
highest := node
|
||||
for {
|
||||
if highest.Parent() == nil || highest.Parent().IsTerminal() || highest.Parent().Len() != 1 {
|
||||
break
|
||||
}
|
||||
if highest == highest.Parent() {
|
||||
return "", CyclicDataError
|
||||
}
|
||||
highest = highest.Parent()
|
||||
}
|
||||
path, err := highest.Path()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if path == "" {
|
||||
return fullname[0:1], nil
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (tree *prefixTree) Autocomplete(prefix string) (string, error) {
|
||||
var node Node = tree.root
|
||||
for _, symbolRune := range prefix {
|
||||
symbol := string(byte(symbolRune))
|
||||
child := node.Child(symbol)
|
||||
if child == nil {
|
||||
return "", NodeNotFoundError
|
||||
}
|
||||
node = child
|
||||
}
|
||||
for !node.IsLeaf() {
|
||||
if node.IsTerminal() || node.Len() != 1 {
|
||||
return "", NotEnoughData
|
||||
}
|
||||
childs := node.Childs()
|
||||
for _, value := range childs {
|
||||
node = value
|
||||
}
|
||||
}
|
||||
path, err := node.Path()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (tree *prefixTree) Len() int {
|
||||
return tree.nodes
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2016 Bitfury Group Limited
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
package prefix_tree
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPrefixTree(t *testing.T) {
|
||||
pt := NewPrefixTree()
|
||||
pt.Add("walk")
|
||||
pt.Add("hello")
|
||||
pt.Add("hi")
|
||||
pt.Add("hell")
|
||||
pt.Add("world")
|
||||
pt.Add("www")
|
||||
|
||||
shortcut, _ := pt.Shortcut("world")
|
||||
if expected := "wo"; shortcut != expected {
|
||||
t.Errorf("expected: %s, actual: %s", expected, shortcut)
|
||||
}
|
||||
|
||||
shortcut, _ = pt.Shortcut("www")
|
||||
if expected := "ww"; shortcut != expected {
|
||||
t.Errorf("expected: %s, actual: %s", expected, shortcut)
|
||||
}
|
||||
|
||||
autocompleted, _ := pt.Autocomplete("wo")
|
||||
if expected := "world"; autocompleted != expected {
|
||||
t.Errorf("expected: %s, actual: %s", expected, autocompleted)
|
||||
}
|
||||
|
||||
autocompleted, _ = pt.Autocomplete("ww")
|
||||
if expected := "www"; autocompleted != expected {
|
||||
t.Errorf("expected: %s, actual: %s", expected, autocompleted)
|
||||
}
|
||||
|
||||
pt.Add("123")
|
||||
pt.Add("456")
|
||||
pt.Add("1234")
|
||||
|
||||
shortcut, _ = pt.Shortcut("123")
|
||||
if expected := "123"; shortcut != expected {
|
||||
t.Errorf("expected: %s, actual: %s", expected, shortcut)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixTreeOneNode(t *testing.T) {
|
||||
pt := NewPrefixTree()
|
||||
pt.Add("123")
|
||||
shortcut, err := pt.Shortcut("123")
|
||||
if err != nil {
|
||||
t.Errorf("error getting shortcut for 123: %v, want: %v", err, nil)
|
||||
}
|
||||
expectedShortcut := "1"
|
||||
if shortcut != expectedShortcut {
|
||||
t.Errorf("expected: %v, actual: %v", expectedShortcut, shortcut)
|
||||
}
|
||||
|
||||
expectedAutocomplete := "123"
|
||||
autocomplete, err := pt.Autocomplete("123")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error getting autocomplete for 123: %v, want: %v", err, nil)
|
||||
}
|
||||
if autocomplete != expectedAutocomplete {
|
||||
t.Errorf("expected: %v, actual: %v", expectedAutocomplete, autocomplete)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// Copyright (c) 2016 Bitfury Group Limited
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
package visualizer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/lightningnetwork/lnd/routing/rt/visualizer/prefix_tree"
|
||||
"github.com/lightningnetwork/lnd/routing/rt/graph"
|
||||
"github.com/awalterschulze/gographviz"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
type visualizer struct {
|
||||
// Graph used for visualisation.
|
||||
G *graph.Graph
|
||||
// Vertexes which should be highlighted.
|
||||
HighlightedNodes []graph.Vertex
|
||||
// Edges which should be highlighted.
|
||||
HighlightedEdges []graph.Edge
|
||||
// Configuration parameters used for visualisation.
|
||||
Config *VisualizerConfig
|
||||
// Function applied to node to obtain its label
|
||||
ApplyToNode func(graph.Vertex) string
|
||||
// Function applied to edge to obtain its label
|
||||
ApplyToEdge func(*graph.ChannelInfo) string
|
||||
// Prefix used for creating shortcuts.
|
||||
pt prefix_tree.PrefixTree
|
||||
graphviz *gographviz.Graph
|
||||
}
|
||||
|
||||
// New creates new visualiser.
|
||||
func New(g *graph.Graph, highlightedNodes []graph.Vertex,
|
||||
highlightedEdges []graph.Edge, config *VisualizerConfig) *visualizer {
|
||||
if config == nil {
|
||||
config = &DefaultVisualizerConfig
|
||||
}
|
||||
return &visualizer{
|
||||
G: g,
|
||||
HighlightedNodes: highlightedNodes,
|
||||
HighlightedEdges: highlightedEdges,
|
||||
Config: config,
|
||||
ApplyToNode: func(v graph.Vertex) string { return hex.EncodeToString(v.ToByte()) },
|
||||
ApplyToEdge: func(info *graph.ChannelInfo) string { return "nil" },
|
||||
pt: prefix_tree.NewPrefixTree(),
|
||||
graphviz: gographviz.NewGraph(),
|
||||
}
|
||||
}
|
||||
|
||||
// EnableShortcut enables/disables shortcuts.
|
||||
// Shortcut is a small unique string used for labeling.
|
||||
func (viz *visualizer) EnableShortcut(value bool) {
|
||||
viz.Config.EnableShortcut = value
|
||||
}
|
||||
|
||||
// BuildPrefixTree builds prefix tree for nodes.
|
||||
// It is needed for shortcuts.
|
||||
func (viz *visualizer) BuildPrefixTree() {
|
||||
for _, node := range viz.G.GetVertexes() {
|
||||
id := viz.ApplyToNode(node)
|
||||
viz.pt.Add(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Draw creates graph representation in Graphviz dot language.
|
||||
func (viz *visualizer) Draw() string {
|
||||
viz.base()
|
||||
viz.drawNodes()
|
||||
viz.drawEdges()
|
||||
return viz.graphviz.String()
|
||||
}
|
||||
|
||||
// Base makes initialization.
|
||||
func (viz *visualizer) base() {
|
||||
viz.graphviz.SetName(viz.Config.GlobalCfg.Name)
|
||||
viz.graphviz.SetDir(viz.Config.GlobalCfg.Dir)
|
||||
viz.graphviz.SetStrict(viz.Config.GlobalCfg.Strict)
|
||||
// TODO(evg): use viz.Add(...) instead viz.Attrs.Add(...)
|
||||
viz.graphviz.Attrs.Add("bgcolor", viz.Config.GlobalCfg.BgColor)
|
||||
}
|
||||
|
||||
func (viz *visualizer) drawNodes() {
|
||||
for _, node := range viz.G.GetVertexes() {
|
||||
if viz.isHighlightedNode(node) {
|
||||
viz.drawNode(node, viz.Config.HighlightedNodeCfg)
|
||||
} else {
|
||||
viz.drawNode(node, viz.Config.NodeCfg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (viz *visualizer) drawNode(node graph.Vertex, cfg *NodeConfig) {
|
||||
id := viz.ApplyToNode(node)
|
||||
if viz.Config.EnableShortcut {
|
||||
// TODO(evg): processing errors
|
||||
id, _ = viz.pt.Shortcut(id)
|
||||
}
|
||||
attrs := gographviz.Attrs{
|
||||
"shape": cfg.Shape,
|
||||
"style": cfg.Style,
|
||||
"fontsize": cfg.FontSize,
|
||||
"fontcolor": cfg.FontColor,
|
||||
"color": cfg.Color,
|
||||
"fillcolor": cfg.FillColor,
|
||||
}
|
||||
viz.graphviz.AddNode(viz.Config.GlobalCfg.Name, id, attrs)
|
||||
}
|
||||
|
||||
func (viz *visualizer) isHighlightedNode(node graph.Vertex) bool {
|
||||
for _, value := range viz.HighlightedNodes {
|
||||
if node.String() == value.String() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (viz *visualizer) drawEdges() {
|
||||
for _, edge := range viz.G.GetUndirectedEdges() {
|
||||
if viz.isHighlightedEdge(edge) {
|
||||
viz.drawEdge(edge, viz.Config.HighlightedEdgeCfg)
|
||||
} else {
|
||||
viz.drawEdge(edge, viz.Config.EdgeCfg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (viz *visualizer) drawEdge(edge graph.Edge, cfg *EdgeConfig) {
|
||||
src := viz.ApplyToNode(edge.Src)
|
||||
tgt := viz.ApplyToNode(edge.Tgt)
|
||||
if viz.Config.EnableShortcut {
|
||||
// TODO(evg): processing errors
|
||||
src, _ = viz.pt.Shortcut(src)
|
||||
tgt, _ = viz.pt.Shortcut(tgt)
|
||||
}
|
||||
attrs := gographviz.Attrs{
|
||||
"fontsize": cfg.FontSize,
|
||||
"fontcolor": cfg.FontColor,
|
||||
"labeldistance": cfg.Scale,
|
||||
"dir": cfg.Dir,
|
||||
"style": cfg.Style,
|
||||
"color": cfg.Color,
|
||||
"label": viz.ApplyToEdge(edge.Info),
|
||||
}
|
||||
viz.graphviz.AddEdge(src, tgt, true, attrs)
|
||||
}
|
||||
|
||||
func (viz *visualizer) isHighlightedEdge(edge graph.Edge) bool {
|
||||
for _, value := range viz.G.GetEdges() {
|
||||
if edge == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Run graphviz command line utility (such as neato).
|
||||
// Used for creation image from textual graph representation.
|
||||
func Run(utility string, TempFile, ImageFile *os.File) error {
|
||||
extension := filepath.Ext(ImageFile.Name())[1:]
|
||||
_, err := exec.Command(utility, "-T"+extension, "-o"+ImageFile.Name(), TempFile.Name()).Output()
|
||||
return err
|
||||
}
|
||||
|
||||
// Opens file in a command line open program.
|
||||
// Used for displaying graphical files.
|
||||
func Open(ImageFile *os.File) error {
|
||||
_, err := exec.Command("open", ImageFile.Name()).Output()
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user