routing: Fix bugs with not sending routing messages

LIGHT-138, LIGHT-141. Due to some issues in sending/receiving parts of lnd,
messages with zero length are not sent. So added some mock content to
NeighborAck. Moved sender/receiver from routing message to wrap message
which contains lnwire routing message.
This commit is contained in:
BitfuryLightning 2016-08-23 15:41:41 -04:00
parent b5f07ede46
commit d8bceb16f9
12 changed files with 100 additions and 62 deletions

View File

@ -585,10 +585,9 @@ func (f *fundingManager) handleFundingSignComplete(fmsg *fundingSignCompleteMsg)
// finding.
chanInfo := openChan.StateSnapshot()
capacity := float64(chanInfo.Capacity)
fmsg.peer.server.routingMgr.AddChannel(
graph.NewID(fmsg.peer.server.lightningID),
fmsg.peer.server.routingMgr.OpenChannel(
graph.NewID(chanInfo.RemoteID),
graph.NewEdgeID(fundingPoint.Hash.String()),
graph.NewEdgeID(fundingPoint.String()),
&rt.ChannelInfo{
Cpt: capacity,
},
@ -657,8 +656,7 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
// Notify the L3 routing manager of the newly active channel link.
capacity := float64(resCtx.reservation.OurContribution().FundingAmount +
resCtx.reservation.TheirContribution().FundingAmount)
fmsg.peer.server.routingMgr.AddChannel(
graph.NewID(fmsg.peer.server.lightningID),
fmsg.peer.server.routingMgr.OpenChannel(
graph.NewID([32]byte(fmsg.peer.lightningID)),
graph.NewEdgeID(resCtx.reservation.FundingOutpoint().String()),
&rt.ChannelInfo{

View File

@ -10,11 +10,10 @@ import (
)
type NeighborAckMessage struct {
RoutingMessageBase
}
func (msg *NeighborAckMessage) String() string {
return fmt.Sprintf("NeighborAckMessage{%v %v}", msg.SenderID, msg.ReceiverID)
return fmt.Sprintf("NeighborAckMessage{}",)
}
func (msg *NeighborAckMessage) Command() uint32 {
@ -22,6 +21,8 @@ func (msg *NeighborAckMessage) Command() uint32 {
}
func (msg *NeighborAckMessage) Encode(w io.Writer, pver uint32) error {
// Transmission function work incorrect with empty messages so write some random string to make message not empty
w.Write([]byte("NeighborAckMessage"))
return nil
}
@ -30,7 +31,9 @@ func (msg *NeighborAckMessage) Decode(r io.Reader, pver uint32) error {
}
func (msg *NeighborAckMessage) MaxPayloadLength(uint32) uint32 {
return 0
// Some random number. Transmission functions work bad if it is 0
return 100
}
func (msg *NeighborAckMessage) Validate() error {

View File

@ -0,0 +1,44 @@
// 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 (
"bytes"
"testing"
"github.com/roasbeef/btcd/wire"
)
func TestNeighborAckMessageEncodeDecode(t *testing.T) {
b := new(bytes.Buffer)
msg1 := NeighborAckMessage{}
err := msg1.Encode(b, 0)
if err != nil {
t.Fatalf("Can't encode message ", err)
}
msg2 := new(NeighborAckMessage)
err = msg2.Decode(b, 0)
if err != nil {
t.Fatalf("Can't decode message ", err)
}
}
func TestNeighborAckMessageReadWrite(t *testing.T){
b := new(bytes.Buffer)
msg1 := &NeighborAckMessage{}
_, err := WriteMessage(b, msg1, 0, wire.SimNet)
if err != nil {
t.Fatalf("Can't write message %v", err)
}
_, msg2, _, err := ReadMessage(b, 0, wire.SimNet)
if err != nil {
t.Fatalf("Can't read message %v", err)
}
_, ok := msg2.(*NeighborAckMessage)
if !ok {
t.Fatalf("Can't convert to *NeighborAckMessage")
}
}

View File

@ -12,7 +12,6 @@ import (
)
type NeighborHelloMessage struct {
RoutingMessageBase
RT *rt.RoutingTable
}
@ -42,7 +41,7 @@ func (msg *NeighborHelloMessage) Validate() error {
}
func (msg *NeighborHelloMessage) String() string {
return fmt.Sprintf("NeighborHelloMessage{%v %v %v}", msg.SenderID, msg.ReceiverID, msg.RT)
return fmt.Sprintf("NeighborHelloMessage{%v}", msg.RT)
}
var _ Message = (*NeighborHelloMessage)(nil)

View File

@ -10,6 +10,7 @@ import (
"github.com/BitfuryLightning/tools/rt"
"github.com/BitfuryLightning/tools/rt/graph"
"github.com/roasbeef/btcd/wire"
)
func TestNeighborHelloMessageEncodeDecode(t *testing.T) {
@ -31,10 +32,40 @@ func TestNeighborHelloMessageEncodeDecode(t *testing.T) {
if msg2.RT == nil {
t.Fatal("After decoding RT should not be nil")
}
if !msg2.RT.HasChannel(Id1, Id2, nil) {
if !msg2.RT.HasChannel(Id1, Id2, graph.NewEdgeID("1")) {
t.Errorf("msg2.RT.HasChannel(Id1, Id2) = false, want true")
}
if !msg2.RT.HasChannel(Id2, Id1, nil) {
if !msg2.RT.HasChannel(Id2, Id1, graph.NewEdgeID("1")) {
t.Errorf("msg2.RT.HasChannel(Id2, Id1) = false, want true")
}
}
func TestNeighborHelloMessageReadWrite(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 := WriteMessage(b, msg1, 0, wire.SimNet)
if err != nil {
t.Fatalf("Can't write message %v", err)
}
_, msg2, _, err := ReadMessage(b, 0, wire.SimNet)
if err != nil {
t.Fatalf("Can't read message %v", err)
}
msg2c, ok := msg2.(*NeighborHelloMessage)
if !ok {
t.Fatalf("Can't convert to *NeighborHelloMessage")
}
if msg2c.RT == nil {
t.Fatal("After decoding RT should not be nil")
}
if !msg2c.RT.HasChannel(Id1, Id2, graph.NewEdgeID("1")) {
t.Errorf("msg2.RT.HasChannel(Id1, Id2) = false, want true")
}
if !msg2c.RT.HasChannel(Id2, Id1, graph.NewEdgeID("1")) {
t.Errorf("msg2.RT.HasChannel(Id2, Id1) = false, want true")
}
}

View File

@ -10,11 +10,10 @@ import (
)
type NeighborRstMessage struct {
RoutingMessageBase
}
func (msg *NeighborRstMessage) String() string {
return fmt.Sprintf("NeighborRstMessage{%v %v}", msg.SenderID, msg.ReceiverID)
return fmt.Sprintf("NeighborRstMessage{}")
}
func (msg *NeighborRstMessage) Command() uint32 {

View File

@ -13,7 +13,6 @@ import (
)
type NeighborUpdMessage struct {
RoutingMessageBase
DiffBuff *rt.DifferenceBuffer
}
@ -46,7 +45,7 @@ func (msg *NeighborUpdMessage) Validate() error {
}
func (msg *NeighborUpdMessage) String() string {
return fmt.Sprintf("NeighborUpdMessage{%v %v %v}", msg.SenderID, msg.ReceiverID, *msg.DiffBuff)
return fmt.Sprintf("NeighborUpdMessage{%v}", *msg.DiffBuff)
}
var _ Message = (*NeighborUpdMessage)(nil)

View File

@ -1,35 +0,0 @@
// 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"
)
// RoutingMessageBase is the base struct for all routing messages within the
// lnwire package.
type RoutingMessageBase struct {
// SenderID is the ID of the sender of the routing message.
SenderID graph.ID
// ReceiverID is the ID of the receiver of the routig message.
ReceiverID graph.ID
}
// GetReceiverID returns the ID of the receiver of routing message.
func (msg RoutingMessageBase) GetReceiverID() graph.ID {
return msg.ReceiverID
}
// GetSenderID returns the ID of the sender of the routing message.
func (msg RoutingMessageBase) GetSenderID() graph.ID {
return msg.SenderID
}
// RoutingMessageBase is a shared interface for all routing messages.
type RoutingMessage interface {
GetSenderID() graph.ID
GetReceiverID() graph.ID
}

View File

@ -10,11 +10,10 @@ import (
)
type RoutingTableRequestMessage struct {
RoutingMessageBase
}
func (msg *RoutingTableRequestMessage) String() string {
return fmt.Sprintf("RoutingTableRequestMessage{%v %v}", msg.SenderID, msg.ReceiverID)
return fmt.Sprintf("RoutingTableRequestMessage{}")
}
func (msg *RoutingTableRequestMessage) Command() uint32 {

View File

@ -13,12 +13,11 @@ import (
)
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)
return fmt.Sprintf("RoutingTableTransferMessage{%v %v %v}", msg.RT)
}
func (msg *RoutingTableTransferMessage) Decode(r io.Reader, pver uint32) error {

View File

@ -19,6 +19,7 @@ import (
"github.com/roasbeef/btcd/txscript"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
"github.com/BitfuryLightning/tools/rt/graph"
)
var (
@ -385,9 +386,8 @@ out:
*lnwire.NeighborUpdMessage,
*lnwire.RoutingTableRequestMessage,
*lnwire.RoutingTableTransferMessage:
// TODO(mkl): determine sender and receiver of message
p.server.routingMgr.ChIn <- msg
// Convert to base routing message and set sender and receiver
p.server.routingMgr.ReceiveRoutingMessage(msg, graph.NewID(([32]byte)(p.lightningID)))
}
if isChanUpate {

View File

@ -13,7 +13,6 @@ import (
"github.com/lightningnetwork/lnd/lndc"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcutil"
@ -284,8 +283,11 @@ out:
s.handleOpenChanReq(msg)
}
case msg := <-s.routingMgr.ChOut:
msg1 := msg.(lnwire.RoutingMessage)
receiverID := msg1.GetReceiverID().ToByte32()
msg1 := msg.(*routing.RoutingMessage)
if msg1.ReceiverID == nil{
peerLog.Critical("msg1.GetReceiverID() == nil")
}
receiverID := msg1.ReceiverID.ToByte32()
var targetPeer *peer
for _, peer := range s.peers { // TODO: threadsafe api
// We found the the target
@ -297,7 +299,7 @@ out:
if targetPeer != nil {
fndgLog.Info("Peer found. Sending message")
done := make(chan struct{}, 1)
targetPeer.queueMsg(msg.(lnwire.Message), done)
targetPeer.queueMsg(msg1.Msg, done)
} else {
srvrLog.Errorf("Can't find peer to send message %v", receiverID)
}