multi: initial integration of routing module
This commit integrates BitFury's current routing functionality into lnd. The primary ochestration point for the routing sub-system in the routingMgr. The routingMgr manages all persistent and volatile state related to routing within the network. Newly opened channels, either when the initiator or responder are inserted into the routing table once the channel is fully open. Once new links are inserted the routingMgr can then perform path selection in order to locate an "optimal" path to a target destination.
This commit is contained in:
1 parent
fc16159a37
commit
f8c851769f
16 files changed
+467
-1
No files matched your search
@@ -44,6 +44,14 @@ const (
|
||||
CmdCommitSignature = uint32(2000)
|
||||
CmdCommitRevocation = uint32(2010)
|
||||
|
||||
// Commands for routing
|
||||
CmdNeighborHelloMessage = uint32(3000)
|
||||
CmdNeighborUpdMessage = uint32(3010)
|
||||
CmdNeighborAckMessage = uint32(3020)
|
||||
CmdNeighborRstMessage = uint32(3030)
|
||||
CmdRoutingTableRequestMessage = uint32(3040)
|
||||
CmdRoutingTableTransferMessage = uint32(3050)
|
||||
|
||||
// Commands for reporting protocol errors.
|
||||
CmdErrorGeneric = uint32(4000)
|
||||
)
|
||||
@@ -94,6 +102,18 @@ func makeEmptyMessage(command uint32) (Message, error) {
|
||||
msg = &CommitRevocation{}
|
||||
case CmdErrorGeneric:
|
||||
msg = &ErrorGeneric{}
|
||||
case CmdNeighborHelloMessage:
|
||||
msg = &NeighborHelloMessage{}
|
||||
case CmdNeighborUpdMessage:
|
||||
msg = &NeighborUpdMessage{}
|
||||
case CmdNeighborAckMessage:
|
||||
msg = &NeighborAckMessage{}
|
||||
case CmdNeighborRstMessage:
|
||||
msg = &NeighborRstMessage{}
|
||||
case CmdRoutingTableRequestMessage:
|
||||
msg = &RoutingTableRequestMessage{}
|
||||
case CmdRoutingTableTransferMessage:
|
||||
msg = &RoutingTableTransferMessage{}
|
||||
default:
|
||||
return nil, fmt.Errorf("unhandled command [%d]", command)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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 lnwire
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type NeighborAckMessage struct {
|
||||
RoutingMessageBase
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) String() string {
|
||||
return fmt.Sprintf("NeighborAckMessage{%v %v}", msg.SenderID, msg.ReceiverID)
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) Command() uint32{
|
||||
return CmdNeighborAckMessage
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) Encode(w io.Writer, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) Decode(r io.Reader, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) MaxPayloadLength(uint32) uint32{
|
||||
return 0
|
||||
}
|
||||
|
||||
func (msg *NeighborAckMessage) Validate() error{
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Message = (*NeighborAckMessage)(nil)
|
||||
@@ -0,0 +1,52 @@
|
||||
// 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 lnwire
|
||||
|
||||
import (
|
||||
"io"
|
||||
"github.com/BitfuryLightning/tools/rt"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type NeighborHelloMessage struct{
|
||||
RoutingMessageBase
|
||||
RT *rt.RoutingTable
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) Decode(r io.Reader, pver uint32) error{
|
||||
decoder := gob.NewDecoder(r)
|
||||
rt1 := rt.NewRoutingTable()
|
||||
err := decoder.Decode(rt1.G)
|
||||
msg.RT = rt1
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) Encode(w io.Writer, pver uint32) error{
|
||||
encoder := gob.NewEncoder(w)
|
||||
err := encoder.Encode(msg.RT.G)
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) Command() uint32{
|
||||
return CmdNeighborHelloMessage
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) MaxPayloadLength(uint32) uint32{
|
||||
// TODO: Insert some estimations
|
||||
return 1000000
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) Validate() error{
|
||||
// TODO: Add validation
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborHelloMessage) String() string{
|
||||
return fmt.Sprintf("NeighborHelloMessage{%v %v %v}", msg.SenderID, msg.ReceiverID, msg.RT)
|
||||
}
|
||||
|
||||
|
||||
var _ Message = (*NeighborHelloMessage)(nil)
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 lnwire
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"github.com/BitfuryLightning/tools/rt"
|
||||
"github.com/BitfuryLightning/tools/rt/graph"
|
||||
)
|
||||
|
||||
func TestNeighborHelloMessageEncodeDecode(t *testing.T){
|
||||
Id1 := graph.NewID(1)
|
||||
Id2 := graph.NewID(2)
|
||||
rt1 := rt.NewRoutingTable()
|
||||
rt1.AddChannel(Id1, Id2, graph.NewEdgeID("1"), &rt.ChannelInfo{1, 1})
|
||||
b := new(bytes.Buffer)
|
||||
msg1 := NeighborHelloMessage{RT:rt1}
|
||||
err := msg1.Encode(b, 0)
|
||||
if err != nil{
|
||||
t.Fatalf("Can't encode message ", err)
|
||||
}
|
||||
msg2 := new(NeighborHelloMessage)
|
||||
err = msg2.Decode(b, 0)
|
||||
if err != nil{
|
||||
t.Fatalf("Can't decode message ", err)
|
||||
}
|
||||
if msg2.RT == nil{
|
||||
t.Fatal("After decoding RT should not be nil")
|
||||
}
|
||||
if !msg2.RT.HasChannel(Id1, Id2, nil){
|
||||
t.Errorf("msg2.RT.HasChannel(Id1, Id2) = false, want true")
|
||||
}
|
||||
if !msg2.RT.HasChannel(Id2, Id1, nil){
|
||||
t.Errorf("msg2.RT.HasChannel(Id2, Id1) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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 lnwire
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type NeighborRstMessage struct {
|
||||
RoutingMessageBase
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) String() string {
|
||||
return fmt.Sprintf("NeighborRstMessage{%v %v}", msg.SenderID, msg.ReceiverID)
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) Command() uint32{
|
||||
return CmdNeighborRstMessage
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) Encode(w io.Writer, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) Decode(r io.Reader, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) MaxPayloadLength(uint32) uint32{
|
||||
return 0
|
||||
}
|
||||
|
||||
func (msg *NeighborRstMessage) Validate() error{
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Message = (*NeighborRstMessage)(nil)
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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 lnwire
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/BitfuryLightning/tools/rt"
|
||||
"encoding/gob"
|
||||
"io"
|
||||
)
|
||||
|
||||
type NeighborUpdMessage struct {
|
||||
RoutingMessageBase
|
||||
DiffBuff *rt.DifferenceBuffer
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) Decode(r io.Reader, pver uint32) error{
|
||||
decoder := gob.NewDecoder(r)
|
||||
diffBuff := new(rt.DifferenceBuffer)
|
||||
err := decoder.Decode(diffBuff)
|
||||
msg.DiffBuff = diffBuff
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) Encode(w io.Writer, pver uint32) error{
|
||||
encoder := gob.NewEncoder(w)
|
||||
err := encoder.Encode(msg.DiffBuff)
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) Command() uint32{
|
||||
return CmdNeighborUpdMessage
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) MaxPayloadLength(uint32) uint32{
|
||||
// TODO: Insert some estimations
|
||||
return 1000000
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) Validate() error{
|
||||
// TODO: Add validation
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *NeighborUpdMessage) String() string {
|
||||
return fmt.Sprintf("NeighborUpdMessage{%v %v %v}", msg.SenderID, msg.ReceiverID, *msg.DiffBuff)
|
||||
}
|
||||
|
||||
var _ Message = (*NeighborUpdMessage)(nil)
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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 lnwire
|
||||
|
||||
import (
|
||||
"github.com/BitfuryLightning/tools/rt/graph"
|
||||
)
|
||||
|
||||
type RoutingMessageBase struct {
|
||||
SenderID graph.ID
|
||||
ReceiverID graph.ID
|
||||
}
|
||||
|
||||
func (msg RoutingMessageBase) GetReceiverID() graph.ID{
|
||||
return msg.ReceiverID
|
||||
}
|
||||
|
||||
func (msg RoutingMessageBase) GetSenderID() graph.ID{
|
||||
return msg.SenderID
|
||||
}
|
||||
|
||||
// Interface for all routing messages. All messages have sender and receiver
|
||||
type RoutingMessage interface {
|
||||
GetSenderID() graph.ID
|
||||
GetReceiverID() graph.ID
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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 lnwire
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type RoutingTableRequestMessage struct {
|
||||
RoutingMessageBase
|
||||
}
|
||||
|
||||
func (msg *RoutingTableRequestMessage) String() string {
|
||||
return fmt.Sprintf("RoutingTableRequestMessage{%v %v}", msg.SenderID, msg.ReceiverID)
|
||||
}
|
||||
|
||||
|
||||
func (msg *RoutingTableRequestMessage) Command() uint32{
|
||||
return CmdRoutingTableRequestMessage
|
||||
}
|
||||
|
||||
func (msg *RoutingTableRequestMessage) Encode(w io.Writer, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *RoutingTableRequestMessage) Decode(r io.Reader, pver uint32) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg *RoutingTableRequestMessage) MaxPayloadLength(uint32) uint32{
|
||||
return 0
|
||||
}
|
||||
|
||||
func (msg *RoutingTableRequestMessage) Validate() error{
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Message = (*RoutingTableRequestMessage)(nil)
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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 lnwire
|
||||
|
||||
import (
|
||||
"io"
|
||||
"github.com/BitfuryLightning/tools/rt"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type RoutingTableTransferMessage struct {
|
||||
RoutingMessageBase
|
||||
RT *rt.RoutingTable
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) String() string {
|
||||
return fmt.Sprintf("RoutingTableTransferMessage{%v %v %v}", msg.SenderID, msg.ReceiverID, msg.RT)
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) Decode(r io.Reader, pver uint32) error{
|
||||
decoder := gob.NewDecoder(r)
|
||||
rt1 := rt.NewRoutingTable()
|
||||
err := decoder.Decode(rt1.G)
|
||||
msg.RT = rt1
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) Encode(w io.Writer, pver uint32) error{
|
||||
encoder := gob.NewEncoder(w)
|
||||
err := encoder.Encode(msg.RT.G)
|
||||
return err
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) Command() uint32{
|
||||
return CmdRoutingTableTransferMessage
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) MaxPayloadLength(uint32) uint32{
|
||||
// TODO: Insert some estimations
|
||||
return 1000000
|
||||
}
|
||||
|
||||
func (msg *RoutingTableTransferMessage) Validate() error{
|
||||
// TODO: Add validation
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Message = (*RoutingTableTransferMessage)(nil)
|
||||
Reference in new issue
Block a user