From dddf0bc887559957299bc1e12977b093acbb3fea Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 27 Apr 2021 16:51:27 +0200 Subject: [PATCH] htlcswitch: concurrent onion decoding --- go.mod | 2 +- go.sum | 4 ++-- htlcswitch/hop/iterator.go | 18 ++++++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index f4295faa..556a78c5 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( github.com/kkdai/bstream v0.0.0-20181106074824-b3251f7901ec github.com/lightninglabs/neutrino v0.12.1 github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display - github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea + github.com/lightningnetwork/lightning-onion v1.0.2-0.20210520211913-522b799e65b1 github.com/lightningnetwork/lnd/cert v1.0.3 github.com/lightningnetwork/lnd/clock v1.0.1 github.com/lightningnetwork/lnd/healthcheck v1.0.0 diff --git a/go.sum b/go.sum index 88aad54f..681a79c5 100644 --- a/go.sum +++ b/go.sum @@ -211,8 +211,8 @@ github.com/lightninglabs/neutrino v0.12.1 h1:9umzk5kKNc/l3bAyak8ClSRP1qSulnjc6kp github.com/lightninglabs/neutrino v0.12.1/go.mod h1:GlKninWpRBbL7b8G0oQ36/8downfnFwKsr0hbRA6E/E= github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display h1:RZJ8H4ueU/aQ9pFtx5wqsuD3B/DezrewJeVwDKKYY8E= github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display/go.mod h1:2oKOBU042GKFHrdbgGiKax4xVrFiZu51lhacUZQ9MnE= -github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea h1:oCj48NQ8u7Vz+MmzHqt0db6mxcFZo3Ho7M5gCJauY/k= -github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4= +github.com/lightningnetwork/lightning-onion v1.0.2-0.20210520211913-522b799e65b1 h1:h1BsjPzWea790mAXISoiT/qr0JRcixTCDNLmjsDThSw= +github.com/lightningnetwork/lightning-onion v1.0.2-0.20210520211913-522b799e65b1/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY= github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6/go.mod h1:8Vg/LTOO0KYa/vlHWJ6XZAevPQThGH5sufO0Hrou/lA= diff --git a/htlcswitch/hop/iterator.go b/htlcswitch/hop/iterator.go index afae12f4..00929334 100644 --- a/htlcswitch/hop/iterator.go +++ b/htlcswitch/hop/iterator.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "sync" "github.com/btcsuite/btcd/btcec" sphinx "github.com/lightningnetwork/lightning-onion" @@ -293,12 +294,21 @@ func (p *OnionProcessor) DecodeHopIterators(id []byte, } } - for i, req := range reqs { - onionPkt := &onionPkts[i] - resp := &resps[i] + // Execute cpu-heavy onion decoding in parallel. + var wg sync.WaitGroup + for i := range reqs { + wg.Add(1) + go func(seqNum uint16) { + defer wg.Done() - resp.FailCode = decode(uint16(i), onionPkt, req) + onionPkt := &onionPkts[seqNum] + + resps[seqNum].FailCode = decode( + seqNum, onionPkt, reqs[seqNum], + ) + }(uint16(i)) } + wg.Wait() // With that batch created, we will now attempt to write the shared // secrets to disk. This operation will returns the set of indices that