diff --git a/fundingmanager.go b/fundingmanager.go index 7c2b43c4..8010d40f 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -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{ diff --git a/lnwire/neighbor_ack.go b/lnwire/neighbor_ack.go index d1fa13fe..8f6ea453 100644 --- a/lnwire/neighbor_ack.go +++ b/lnwire/neighbor_ack.go @@ -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 { diff --git a/lnwire/neighbor_ack_test.go b/lnwire/neighbor_ack_test.go new file mode 100644 index 00000000..6e921d72 --- /dev/null +++ b/lnwire/neighbor_ack_test.go @@ -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") + } +} + diff --git a/lnwire/neighbor_hello.go b/lnwire/neighbor_hello.go index 01645c12..53a57a2a 100644 --- a/lnwire/neighbor_hello.go +++ b/lnwire/neighbor_hello.go @@ -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) diff --git a/lnwire/neighbor_hello_test.go b/lnwire/neighbor_hello_test.go index 25285979..b2549c2e 100644 --- a/lnwire/neighbor_hello_test.go +++ b/lnwire/neighbor_hello_test.go @@ -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") } } diff --git a/lnwire/neighbor_rst.go b/lnwire/neighbor_rst.go index f04421e4..3ecf46e0 100644 --- a/lnwire/neighbor_rst.go +++ b/lnwire/neighbor_rst.go @@ -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 { diff --git a/lnwire/neighbor_upd.go b/lnwire/neighbor_upd.go index 59ff781a..17d35203 100644 --- a/lnwire/neighbor_upd.go +++ b/lnwire/neighbor_upd.go @@ -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) diff --git a/lnwire/routing_messages.go b/lnwire/routing_messages.go deleted file mode 100644 index be67f98c..00000000 --- a/lnwire/routing_messages.go +++ /dev/null @@ -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 -} diff --git a/lnwire/routing_table_request.go b/lnwire/routing_table_request.go index 4780d89f..69f5bc6f 100644 --- a/lnwire/routing_table_request.go +++ b/lnwire/routing_table_request.go @@ -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 { diff --git a/lnwire/routing_table_transfer.go b/lnwire/routing_table_transfer.go index b4138dce..b06a54bd 100644 --- a/lnwire/routing_table_transfer.go +++ b/lnwire/routing_table_transfer.go @@ -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 { diff --git a/peer.go b/peer.go index a5f73ceb..a37fdf90 100644 --- a/peer.go +++ b/peer.go @@ -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 { diff --git a/server.go b/server.go index e8c591e2..4fab56c0 100644 --- a/server.go +++ b/server.go @@ -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) }