mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 09:48:19 +01:00
channeldb/invoices: optimize deserializeHtlcs
Instead of allocating a byte slice to read in each htlc's raw TLV stream, use a LimitReader to truncate the stream to the proper length.
This commit is contained in:
parent
4c872c438b
commit
6cabea563e
1 changed files with 5 additions and 7 deletions
|
@ -1132,7 +1132,7 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Read the length of the tlv stream for this htlc.
|
// Read the length of the tlv stream for this htlc.
|
||||||
var streamLen uint64
|
var streamLen int64
|
||||||
if err := binary.Read(r, byteOrder, &streamLen); err != nil {
|
if err := binary.Read(r, byteOrder, &streamLen); err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
|
@ -1141,11 +1141,9 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
streamBytes := make([]byte, streamLen)
|
// Limit the reader so that it stops at the end of this htlc's
|
||||||
if _, err := r.Read(streamBytes); err != nil {
|
// stream.
|
||||||
return nil, err
|
htlcReader := io.LimitReader(r, streamLen)
|
||||||
}
|
|
||||||
streamReader := bytes.NewReader(streamBytes)
|
|
||||||
|
|
||||||
// Decode the contents into the htlc fields.
|
// Decode the contents into the htlc fields.
|
||||||
var (
|
var (
|
||||||
|
@ -1172,7 +1170,7 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tlvStream.Decode(streamReader); err != nil {
|
if err := tlvStream.Decode(htlcReader); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue