lnwire+peer: clamp pong bytes, make ping handler more effcieint

This was not properly enforced and would be a spec violation on the
peer's end. Also re-use a pong buffer to save on heap allocations if
there are a lot of peers. The pong buffer is only read from, so this
is concurrent safe.
This commit is contained in:
Olaoluwa Osuntokun committed 2022-01-13 15:01:18 -08:00
1 parent ae16f2b631
commit 3481286ea0
6 files changed
+55 -7

No files matched your search

+8 -4
View File
@@ -327,6 +327,12 @@ type Config struct {
// from the peer.
HandleCustomMessage func(peer [33]byte, msg *lnwire.Custom) error
// PongBuf is a slice we'll reuse instead of allocating memory on the
// heap. Since only reads will occur and no writes, there is no need
// for any synchronization primitives. As a result, it's safe to share
// this across multiple Peer struct instances.
PongBuf []byte
// Quit is the server's quit channel. If this is closed, we halt operation.
Quit chan struct{}
}
@@ -1394,10 +1400,8 @@ out:
// Next, we'll send over the amount of specified pong
// bytes.
//
// TODO(roasbeef): read out from pong scratch instead?
pongBytes := make([]byte, msg.NumPongBytes)
p.queueMsg(lnwire.NewPong(pongBytes), nil)
pong := lnwire.NewPong(p.cfg.PongBuf[0:msg.NumPongBytes])
p.queueMsg(pong, nil)
case *lnwire.OpenChannel,
*lnwire.AcceptChannel,
+2
View File
@@ -999,6 +999,7 @@ func TestStaticRemoteDowngrade(t *testing.T) {
Features: test.features,
Conn: mockConn,
WritePool: writePool,
PongBuf: make([]byte, lnwire.MaxPongBytes),
},
}
@@ -1103,6 +1104,7 @@ func TestPeerCustomMessage(t *testing.T) {
}
return nil
},
PongBuf: make([]byte, lnwire.MaxPongBytes),
})
// Set up the init sequence.