mirror of https://github.com/BTCPrivate/lnd.git
htlcswitch/circuit: add half adds to circuit map
This commit is contained in:
parent
7a14b6bb32
commit
067b261602
|
@ -1,184 +1,229 @@
|
||||||
package htlcswitch
|
package htlcswitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/binary"
|
||||||
"sync"
|
"io"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PaymentCircuit is used by the HTLC switch subsystem to determine the
|
// EmptyCircuitKey is a default value for an outgoing circuit key returned when
|
||||||
// backwards path for the settle/fail HTLC messages. A payment circuit
|
// a circuit's keystone has not been set. Note that this value is invalid for
|
||||||
// will be created once a channel link forwards the HTLC add request and
|
// use as a keystone, since the outgoing channel id can never be equal to
|
||||||
// removed when we receive a settle/fail HTLC message.
|
// sourceHop.
|
||||||
|
var EmptyCircuitKey CircuitKey
|
||||||
|
|
||||||
|
// CircuitKey is a tuple of channel ID and HTLC ID, used to uniquely identify
|
||||||
|
// HTLCs in a circuit. Circuits are identified primarily by the circuit key of
|
||||||
|
// the incoming HTLC. However, a circuit may also be referenced by its outgoing
|
||||||
|
// circuit key after the HTLC has been forwarded via the outgoing link.
|
||||||
|
type CircuitKey = channeldb.CircuitKey
|
||||||
|
|
||||||
|
// PaymentCircuit is used by the switch as placeholder between when the
|
||||||
|
// switch makes a forwarding decision and the outgoing link determines the
|
||||||
|
// proper HTLC ID for the local log. After the outgoing HTLC ID has been
|
||||||
|
// determined, the half circuit will be converted into a full PaymentCircuit.
|
||||||
type PaymentCircuit struct {
|
type PaymentCircuit struct {
|
||||||
|
// AddRef is the forward reference of the Add update in the incoming
|
||||||
|
// link's forwarding package. This value is set on the htlcPacket of the
|
||||||
|
// returned settle/fail so that it can be removed from disk.
|
||||||
|
AddRef channeldb.AddRef
|
||||||
|
|
||||||
|
// Incoming is the circuit key identifying the incoming channel and htlc
|
||||||
|
// index from which this ADD originates.
|
||||||
|
Incoming CircuitKey
|
||||||
|
|
||||||
|
// Outgoing is the circuit key identifying the outgoing channel, and the
|
||||||
|
// HTLC index that was used to forward the ADD. It will be nil if this
|
||||||
|
// circuit's keystone has not been set.
|
||||||
|
Outgoing *CircuitKey
|
||||||
|
|
||||||
// PaymentHash used as unique identifier of payment.
|
// PaymentHash used as unique identifier of payment.
|
||||||
PaymentHash [32]byte
|
PaymentHash [32]byte
|
||||||
|
|
||||||
// IncomingChanID identifies the channel from which add HTLC request
|
// IncomingAmount is the value of the HTLC from the incoming link.
|
||||||
// came and to which settle/fail HTLC request will be returned back.
|
IncomingAmount lnwire.MilliSatoshi
|
||||||
// Once the switch forwards the settle/fail message to the src the
|
|
||||||
// circuit is considered to be completed.
|
|
||||||
IncomingChanID lnwire.ShortChannelID
|
|
||||||
|
|
||||||
// IncomingHTLCID is the ID in the update_add_htlc message we received
|
// OutgoingAmount specifies the value of the HTLC leaving the switch,
|
||||||
// from the incoming channel, which will be included in any settle/fail
|
// either as a payment or forwarded amount.
|
||||||
// messages we send back.
|
OutgoingAmount lnwire.MilliSatoshi
|
||||||
IncomingHTLCID uint64
|
|
||||||
|
|
||||||
// IncomingAmt is the value of the incoming HTLC. If we take this and
|
|
||||||
// subtract it from the OutgoingAmt, then we'll compute the total fee
|
|
||||||
// attached to this payment circuit.
|
|
||||||
IncomingAmt lnwire.MilliSatoshi
|
|
||||||
|
|
||||||
// OutgoingChanID identifies the channel to which we propagate the HTLC
|
|
||||||
// add update and from which we are expecting to receive HTLC
|
|
||||||
// settle/fail request back.
|
|
||||||
OutgoingChanID lnwire.ShortChannelID
|
|
||||||
|
|
||||||
// OutgoingHTLCID is the ID in the update_add_htlc message we sent to
|
|
||||||
// the outgoing channel.
|
|
||||||
OutgoingHTLCID uint64
|
|
||||||
|
|
||||||
// OutgoingAmt is the value of the outgoing HTLC. If we subtract this
|
|
||||||
// from the IncomingAmt, then we'll compute the total fee attached to
|
|
||||||
// this payment circuit.
|
|
||||||
OutgoingAmt lnwire.MilliSatoshi
|
|
||||||
|
|
||||||
// ErrorEncrypter is used to re-encrypt the onion failure before
|
// ErrorEncrypter is used to re-encrypt the onion failure before
|
||||||
// sending it back to the originator of the payment.
|
// sending it back to the originator of the payment.
|
||||||
ErrorEncrypter ErrorEncrypter
|
ErrorEncrypter ErrorEncrypter
|
||||||
|
|
||||||
|
// LoadedFromDisk is set true for any circuits loaded after the circuit
|
||||||
|
// map is reloaded from disk.
|
||||||
|
//
|
||||||
|
// NOTE: This value is determined implicitly during a restart. It is not
|
||||||
|
// persisted, and should never be set outside the circuit map.
|
||||||
|
LoadedFromDisk bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// circuitKey is a channel ID, HTLC ID tuple used as an identifying key for a
|
// HasKeystone returns true if an outgoing link has assigned this circuit's
|
||||||
// payment circuit. The circuit map is keyed with the identifier for the
|
// outgoing circuit key.
|
||||||
// outgoing HTLC
|
func (c *PaymentCircuit) HasKeystone() bool {
|
||||||
type circuitKey struct {
|
return c.Outgoing != nil
|
||||||
chanID lnwire.ShortChannelID
|
|
||||||
htlcID uint64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string representation of the circuitKey.
|
// newPaymentCircuit initializes a payment circuit on the heap using the payment
|
||||||
func (k *circuitKey) String() string {
|
// hash and an in-memory htlc packet.
|
||||||
return fmt.Sprintf("(Chan ID=%s, HTLC ID=%d)", k.chanID, k.htlcID)
|
func newPaymentCircuit(hash *[32]byte, pkt *htlcPacket) *PaymentCircuit {
|
||||||
}
|
var addRef channeldb.AddRef
|
||||||
|
if pkt.sourceRef != nil {
|
||||||
|
addRef = *pkt.sourceRef
|
||||||
|
}
|
||||||
|
|
||||||
// CircuitMap is a data structure that implements thread safe storage of
|
return &PaymentCircuit{
|
||||||
// circuit routing information. The switch consults a circuit map to determine
|
AddRef: addRef,
|
||||||
// where to forward HTLC update messages. Each circuit is stored with its
|
Incoming: CircuitKey{
|
||||||
// outgoing HTLC as the primary key because, each offered HTLC has at most one
|
ChanID: pkt.incomingChanID,
|
||||||
// received HTLC, but there may be multiple offered or received HTLCs with the
|
HtlcID: pkt.incomingHTLCID,
|
||||||
// same payment hash. Circuits are also indexed to provide fast lookups by
|
},
|
||||||
// payment hash.
|
PaymentHash: *hash,
|
||||||
//
|
IncomingAmount: pkt.incomingAmount,
|
||||||
// TODO(andrew.shvv) make it persistent
|
OutgoingAmount: pkt.amount,
|
||||||
type CircuitMap struct {
|
ErrorEncrypter: pkt.obfuscator,
|
||||||
mtx sync.RWMutex
|
|
||||||
circuits map[circuitKey]*PaymentCircuit
|
|
||||||
hashIndex map[[32]byte]map[PaymentCircuit]struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCircuitMap creates a new instance of the CircuitMap.
|
|
||||||
func NewCircuitMap() *CircuitMap {
|
|
||||||
return &CircuitMap{
|
|
||||||
circuits: make(map[circuitKey]*PaymentCircuit),
|
|
||||||
hashIndex: make(map[[32]byte]map[PaymentCircuit]struct{}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookupByHTLC looks up the payment circuit by the outgoing channel and HTLC
|
// makePaymentCircuit initalizes a payment circuit on the stack using the
|
||||||
// IDs. Returns nil if there is no such circuit.
|
// payment hash and an in-memory htlc packet.
|
||||||
func (cm *CircuitMap) LookupByHTLC(chanID lnwire.ShortChannelID, htlcID uint64) *PaymentCircuit {
|
func makePaymentCircuit(hash *[32]byte, pkt *htlcPacket) PaymentCircuit {
|
||||||
cm.mtx.RLock()
|
var addRef channeldb.AddRef
|
||||||
|
if pkt.sourceRef != nil {
|
||||||
key := circuitKey{
|
addRef = *pkt.sourceRef
|
||||||
chanID: chanID,
|
|
||||||
htlcID: htlcID,
|
|
||||||
}
|
}
|
||||||
circuit := cm.circuits[key]
|
|
||||||
|
|
||||||
cm.mtx.RUnlock()
|
return PaymentCircuit{
|
||||||
return circuit
|
AddRef: addRef,
|
||||||
|
Incoming: CircuitKey{
|
||||||
|
ChanID: pkt.incomingChanID,
|
||||||
|
HtlcID: pkt.incomingHTLCID,
|
||||||
|
},
|
||||||
|
PaymentHash: *hash,
|
||||||
|
IncomingAmount: pkt.incomingAmount,
|
||||||
|
OutgoingAmount: pkt.amount,
|
||||||
|
ErrorEncrypter: pkt.obfuscator,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookupByPaymentHash looks up and returns any payment circuits with a given
|
// Encode writes a PaymentCircuit to the provided io.Writer.
|
||||||
// payment hash.
|
func (c *PaymentCircuit) Encode(w io.Writer) error {
|
||||||
func (cm *CircuitMap) LookupByPaymentHash(hash [32]byte) []*PaymentCircuit {
|
if err := c.AddRef.Encode(w); err != nil {
|
||||||
cm.mtx.RLock()
|
return err
|
||||||
|
|
||||||
var circuits []*PaymentCircuit
|
|
||||||
if circuitSet, ok := cm.hashIndex[hash]; ok {
|
|
||||||
circuits = make([]*PaymentCircuit, 0, len(circuitSet))
|
|
||||||
for circuit := range circuitSet {
|
|
||||||
circuits = append(circuits, &circuit)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cm.mtx.RUnlock()
|
if err := c.Incoming.Encode(w); err != nil {
|
||||||
return circuits
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(c.PaymentHash[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var scratch [8]byte
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint64(scratch[:], uint64(c.IncomingAmount))
|
||||||
|
if _, err := w.Write(scratch[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint64(scratch[:], uint64(c.OutgoingAmount))
|
||||||
|
if _, err := w.Write(scratch[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defaults to EncrypterTypeNone.
|
||||||
|
var encrypterType EncrypterType
|
||||||
|
if c.ErrorEncrypter != nil {
|
||||||
|
encrypterType = c.ErrorEncrypter.Type()
|
||||||
|
}
|
||||||
|
|
||||||
|
err := binary.Write(w, binary.BigEndian, encrypterType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip encoding of error encrypter if this half add does not have one.
|
||||||
|
if encrypterType == EncrypterTypeNone {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.ErrorEncrypter.Encode(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds a new active payment circuit to the CircuitMap.
|
// Decode reads a PaymentCircuit from the provided io.Reader.
|
||||||
func (cm *CircuitMap) Add(circuit *PaymentCircuit) error {
|
func (c *PaymentCircuit) Decode(r io.Reader) error {
|
||||||
cm.mtx.Lock()
|
if err := c.AddRef.Decode(r); err != nil {
|
||||||
|
return err
|
||||||
key := circuitKey{
|
|
||||||
chanID: circuit.OutgoingChanID,
|
|
||||||
htlcID: circuit.OutgoingHTLCID,
|
|
||||||
}
|
}
|
||||||
cm.circuits[key] = circuit
|
|
||||||
|
|
||||||
// Add circuit to the hash index.
|
if err := c.Incoming.Decode(r); err != nil {
|
||||||
if _, ok := cm.hashIndex[circuit.PaymentHash]; !ok {
|
return err
|
||||||
cm.hashIndex[circuit.PaymentHash] = make(map[PaymentCircuit]struct{})
|
|
||||||
}
|
}
|
||||||
cm.hashIndex[circuit.PaymentHash][*circuit] = struct{}{}
|
|
||||||
|
|
||||||
cm.mtx.Unlock()
|
if _, err := io.ReadFull(r, c.PaymentHash[:]); err != nil {
|
||||||
return nil
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var scratch [8]byte
|
||||||
|
|
||||||
|
if _, err := io.ReadFull(r, scratch[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.IncomingAmount = lnwire.MilliSatoshi(
|
||||||
|
binary.BigEndian.Uint64(scratch[:]))
|
||||||
|
|
||||||
|
if _, err := io.ReadFull(r, scratch[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.OutgoingAmount = lnwire.MilliSatoshi(
|
||||||
|
binary.BigEndian.Uint64(scratch[:]))
|
||||||
|
|
||||||
|
// Read the encrypter type used for this circuit.
|
||||||
|
var encrypterType EncrypterType
|
||||||
|
err := binary.Read(r, binary.BigEndian, &encrypterType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch encrypterType {
|
||||||
|
case EncrypterTypeNone:
|
||||||
|
// No encrypter was provided, such as when the payment is
|
||||||
|
// locally initiated.
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case EncrypterTypeSphinx:
|
||||||
|
// Sphinx encrypter was used as this is a forwarded HTLC.
|
||||||
|
c.ErrorEncrypter = NewSphinxErrorEncrypter()
|
||||||
|
|
||||||
|
case EncrypterTypeMock:
|
||||||
|
// Test encrypter.
|
||||||
|
c.ErrorEncrypter = NewMockObfuscator()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return UnknownEncrypterType(encrypterType)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.ErrorEncrypter.Decode(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove destroys the target circuit by removing it from the circuit map.
|
// InKey returns the primary identifier for the circuit corresponding to the
|
||||||
func (cm *CircuitMap) Remove(chanID lnwire.ShortChannelID, htlcID uint64) error {
|
// incoming HTLC.
|
||||||
cm.mtx.Lock()
|
func (c *PaymentCircuit) InKey() CircuitKey {
|
||||||
defer cm.mtx.Unlock()
|
return c.Incoming
|
||||||
|
|
||||||
// Look up circuit so that pointer can be matched in the hash index.
|
|
||||||
key := circuitKey{
|
|
||||||
chanID: chanID,
|
|
||||||
htlcID: htlcID,
|
|
||||||
}
|
|
||||||
circuit, found := cm.circuits[key]
|
|
||||||
if !found {
|
|
||||||
return errors.Errorf("Can't find circuit for HTLC %v", key)
|
|
||||||
}
|
|
||||||
delete(cm.circuits, key)
|
|
||||||
|
|
||||||
// Remove circuit from hash index.
|
|
||||||
circuitsWithHash, ok := cm.hashIndex[circuit.PaymentHash]
|
|
||||||
if !ok {
|
|
||||||
return errors.Errorf("Can't find circuit in hash index for HTLC %v",
|
|
||||||
key)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok = circuitsWithHash[*circuit]; !ok {
|
|
||||||
return errors.Errorf("Can't find circuit in hash index for HTLC %v",
|
|
||||||
key)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(circuitsWithHash, *circuit)
|
|
||||||
if len(circuitsWithHash) == 0 {
|
|
||||||
delete(cm.hashIndex, circuit.PaymentHash)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pending returns number of circuits which are waiting for to be completed
|
// OutKey returns the keystone identifying the outgoing link and HTLC ID. If the
|
||||||
// (settle/fail responses to be received).
|
// circuit hasn't been completed, this method returns an EmptyKeystone, which is
|
||||||
func (cm *CircuitMap) pending() int {
|
// an invalid outgoing circuit key. Only call this method if HasKeystone returns
|
||||||
cm.mtx.RLock()
|
// true.
|
||||||
count := len(cm.circuits)
|
func (c *PaymentCircuit) OutKey() CircuitKey {
|
||||||
cm.mtx.RUnlock()
|
if c.Outgoing != nil {
|
||||||
return count
|
return *c.Outgoing
|
||||||
|
}
|
||||||
|
|
||||||
|
return EmptyCircuitKey
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue