htlcswitch: extract onion decode function

This commit is contained in:
Joost Jager 2021-04-27 16:43:06 +02:00
parent dee6a1a29b
commit 94b6f8af5b

@ -251,9 +251,8 @@ func (p *OnionProcessor) DecodeHopIterators(id []byte,
tx := p.router.BeginTxn(id, batchSize) tx := p.router.BeginTxn(id, batchSize)
for i, req := range reqs { decode := func(seqNum uint16, onionPkt *sphinx.OnionPacket,
onionPkt := &onionPkts[i] req DecodeHopIteratorRequest) lnwire.FailCode {
resp := &resps[i]
err := onionPkt.Decode(req.OnionReader) err := onionPkt.Decode(req.OnionReader)
switch err { switch err {
@ -261,45 +260,46 @@ func (p *OnionProcessor) DecodeHopIterators(id []byte,
// success // success
case sphinx.ErrInvalidOnionVersion: case sphinx.ErrInvalidOnionVersion:
resp.FailCode = lnwire.CodeInvalidOnionVersion return lnwire.CodeInvalidOnionVersion
continue
case sphinx.ErrInvalidOnionKey: case sphinx.ErrInvalidOnionKey:
resp.FailCode = lnwire.CodeInvalidOnionKey return lnwire.CodeInvalidOnionKey
continue
default: default:
log.Errorf("unable to decode onion packet: %v", err) log.Errorf("unable to decode onion packet: %v", err)
resp.FailCode = lnwire.CodeInvalidOnionKey return lnwire.CodeInvalidOnionKey
continue
} }
err = tx.ProcessOnionPacket( err = tx.ProcessOnionPacket(
uint16(i), onionPkt, req.RHash, req.IncomingCltv, seqNum, onionPkt, req.RHash, req.IncomingCltv,
) )
switch err { switch err {
case nil: case nil:
// success // success
return lnwire.CodeNone
case sphinx.ErrInvalidOnionVersion: case sphinx.ErrInvalidOnionVersion:
resp.FailCode = lnwire.CodeInvalidOnionVersion return lnwire.CodeInvalidOnionVersion
continue
case sphinx.ErrInvalidOnionHMAC: case sphinx.ErrInvalidOnionHMAC:
resp.FailCode = lnwire.CodeInvalidOnionHmac return lnwire.CodeInvalidOnionHmac
continue
case sphinx.ErrInvalidOnionKey: case sphinx.ErrInvalidOnionKey:
resp.FailCode = lnwire.CodeInvalidOnionKey return lnwire.CodeInvalidOnionKey
continue
default: default:
log.Errorf("unable to process onion packet: %v", err) log.Errorf("unable to process onion packet: %v", err)
resp.FailCode = lnwire.CodeInvalidOnionKey return lnwire.CodeInvalidOnionKey
continue
} }
} }
for i, req := range reqs {
onionPkt := &onionPkts[i]
resp := &resps[i]
resp.FailCode = decode(uint16(i), onionPkt, req)
}
// With that batch created, we will now attempt to write the shared // With that batch created, we will now attempt to write the shared
// secrets to disk. This operation will returns the set of indices that // secrets to disk. This operation will returns the set of indices that
// were detected as replays, and the computed sphinx packets for all // were detected as replays, and the computed sphinx packets for all