Merge pull request #5248 from joostjager/concurrent-onion
htlcswitch: concurrent onion decoding
This commit is contained in:
3 files changed
+31
-21
No files matched your search
+28
-18
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||
@@ -251,9 +252,8 @@ func (p *OnionProcessor) DecodeHopIterators(id []byte,
|
||||
|
||||
tx := p.router.BeginTxn(id, batchSize)
|
||||
|
||||
for i, req := range reqs {
|
||||
onionPkt := &onionPkts[i]
|
||||
resp := &resps[i]
|
||||
decode := func(seqNum uint16, onionPkt *sphinx.OnionPacket,
|
||||
req DecodeHopIteratorRequest) lnwire.FailCode {
|
||||
|
||||
err := onionPkt.Decode(req.OnionReader)
|
||||
switch err {
|
||||
@@ -261,45 +261,55 @@ func (p *OnionProcessor) DecodeHopIterators(id []byte,
|
||||
// success
|
||||
|
||||
case sphinx.ErrInvalidOnionVersion:
|
||||
resp.FailCode = lnwire.CodeInvalidOnionVersion
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionVersion
|
||||
|
||||
case sphinx.ErrInvalidOnionKey:
|
||||
resp.FailCode = lnwire.CodeInvalidOnionKey
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionKey
|
||||
|
||||
default:
|
||||
log.Errorf("unable to decode onion packet: %v", err)
|
||||
resp.FailCode = lnwire.CodeInvalidOnionKey
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionKey
|
||||
}
|
||||
|
||||
err = tx.ProcessOnionPacket(
|
||||
uint16(i), onionPkt, req.RHash, req.IncomingCltv,
|
||||
seqNum, onionPkt, req.RHash, req.IncomingCltv,
|
||||
)
|
||||
switch err {
|
||||
case nil:
|
||||
// success
|
||||
return lnwire.CodeNone
|
||||
|
||||
case sphinx.ErrInvalidOnionVersion:
|
||||
resp.FailCode = lnwire.CodeInvalidOnionVersion
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionVersion
|
||||
|
||||
case sphinx.ErrInvalidOnionHMAC:
|
||||
resp.FailCode = lnwire.CodeInvalidOnionHmac
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionHmac
|
||||
|
||||
case sphinx.ErrInvalidOnionKey:
|
||||
resp.FailCode = lnwire.CodeInvalidOnionKey
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionKey
|
||||
|
||||
default:
|
||||
log.Errorf("unable to process onion packet: %v", err)
|
||||
resp.FailCode = lnwire.CodeInvalidOnionKey
|
||||
continue
|
||||
return lnwire.CodeInvalidOnionKey
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
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
|
||||
// were detected as replays, and the computed sphinx packets for all
|
||||
|
||||
Reference in new issue
Block a user