2019-03-27 15:20:29 +00:00
|
|
|
package loopdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2022-04-24 20:32:17 +00:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2019-03-27 15:20:29 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
)
|
|
|
|
|
2023-01-05 17:16:05 +00:00
|
|
|
// HtlcKeys is a holder of all keys used when constructing the swap HTLC. Since
|
|
|
|
// it's used for both loop in and loop out swaps it may hold partial information
|
|
|
|
// about the sender or receiver depending on the swap type.
|
|
|
|
type HtlcKeys struct {
|
|
|
|
// SenderScriptKey is the sender's public key that is used in the HTLC,
|
|
|
|
// specifically when constructing the script spend scripts.
|
|
|
|
SenderScriptKey [33]byte
|
|
|
|
|
|
|
|
// SenderInternalPubKey is the sender's internal pubkey that is used in
|
|
|
|
// taproot HTLCs as part of the aggregate internal key.
|
|
|
|
SenderInternalPubKey [33]byte
|
|
|
|
|
|
|
|
// ReceiverScriptKey is the receiver's public key that is used in the
|
|
|
|
// HTLC, specifically when constructing the script spend scripts.
|
|
|
|
ReceiverScriptKey [33]byte
|
|
|
|
|
|
|
|
// ReceiverInternalPubKey is the sender's internal pubkey that is used
|
|
|
|
// in taproot HTLCs as part of the aggregate internal key.
|
|
|
|
ReceiverInternalPubKey [33]byte
|
|
|
|
|
|
|
|
// ClientScriptKeyLocator is the client's key locator for the key used
|
|
|
|
// in the HTLC script spend scripts.
|
|
|
|
ClientScriptKeyLocator keychain.KeyLocator
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:20:29 +00:00
|
|
|
// SwapContract contains the base data that is serialized to persistent storage
|
|
|
|
// for pending swaps.
|
|
|
|
type SwapContract struct {
|
|
|
|
// Preimage is the preimage for the swap.
|
|
|
|
Preimage lntypes.Preimage
|
|
|
|
|
|
|
|
// AmountRequested is the total amount of the swap.
|
|
|
|
AmountRequested btcutil.Amount
|
|
|
|
|
2023-01-05 17:16:05 +00:00
|
|
|
// HtlcKeys holds all keys used in the swap HTLC construction.
|
|
|
|
HtlcKeys HtlcKeys
|
2022-04-24 20:32:17 +00:00
|
|
|
|
2019-03-27 15:20:29 +00:00
|
|
|
// CltvExpiry is the total absolute CLTV expiry of the swap.
|
|
|
|
CltvExpiry int32
|
|
|
|
|
|
|
|
// MaxSwapFee is the maximum we are willing to pay the server for the
|
|
|
|
// swap.
|
|
|
|
MaxSwapFee btcutil.Amount
|
|
|
|
|
|
|
|
// MaxMinerFee is the maximum in on-chain fees that we are willing to
|
|
|
|
// spend.
|
|
|
|
MaxMinerFee btcutil.Amount
|
|
|
|
|
|
|
|
// InitiationHeight is the block height at which the swap was
|
|
|
|
// initiated.
|
|
|
|
InitiationHeight int32
|
|
|
|
|
|
|
|
// InitiationTime is the time at which the swap was initiated.
|
|
|
|
InitiationTime time.Time
|
2020-08-03 08:55:58 +00:00
|
|
|
|
|
|
|
// Label contains an optional label for the swap.
|
|
|
|
Label string
|
2020-07-23 15:28:36 +00:00
|
|
|
|
|
|
|
// ProtocolVersion stores the protocol version when the swap was
|
|
|
|
// created.
|
|
|
|
ProtocolVersion ProtocolVersion
|
2019-03-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 15:43:38 +00:00
|
|
|
// Loop contains fields shared between LoopIn and LoopOut.
|
2019-03-12 15:09:57 +00:00
|
|
|
type Loop struct {
|
|
|
|
Hash lntypes.Hash
|
|
|
|
Events []*LoopEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoopEvent contains the dynamic data of a swap.
|
|
|
|
type LoopEvent struct {
|
2019-05-15 11:55:41 +00:00
|
|
|
SwapStateData
|
2019-03-27 15:20:29 +00:00
|
|
|
|
|
|
|
// Time is the time that this swap had its state changed.
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// State returns the most recent state of this swap.
|
2019-05-15 11:55:41 +00:00
|
|
|
func (s *Loop) State() SwapStateData {
|
2019-03-27 15:20:29 +00:00
|
|
|
lastUpdate := s.LastUpdate()
|
|
|
|
if lastUpdate == nil {
|
2019-05-15 11:55:41 +00:00
|
|
|
return SwapStateData{
|
|
|
|
State: StateInitiated,
|
|
|
|
}
|
2019-03-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 11:55:41 +00:00
|
|
|
return lastUpdate.SwapStateData
|
2019-03-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LastUpdate returns the most recent update of this swap.
|
2019-03-12 15:09:57 +00:00
|
|
|
func (s *Loop) LastUpdate() *LoopEvent {
|
2019-03-27 15:20:29 +00:00
|
|
|
eventCount := len(s.Events)
|
|
|
|
|
|
|
|
if eventCount == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lastEvent := s.Events[eventCount-1]
|
|
|
|
return lastEvent
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
// serializeLoopEvent serializes a state update of a swap. This is used for both
|
|
|
|
// in and out swaps.
|
2019-05-15 11:55:41 +00:00
|
|
|
func serializeLoopEvent(time time.Time, state SwapStateData) (
|
2019-03-27 15:20:29 +00:00
|
|
|
[]byte, error) {
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
if err := binary.Write(&b, byteOrder, time.UnixNano()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:01:27 +00:00
|
|
|
if err := binary.Write(&b, byteOrder, state.State); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(&b, byteOrder, state.Cost.Server); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(&b, byteOrder, state.Cost.Onchain); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(&b, byteOrder, state.Cost.Offchain); err != nil {
|
2019-03-27 15:20:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:09:57 +00:00
|
|
|
// deserializeLoopEvent deserializes a state update of a swap. This is used for
|
|
|
|
// both in and out swaps.
|
|
|
|
func deserializeLoopEvent(value []byte) (*LoopEvent, error) {
|
|
|
|
update := &LoopEvent{}
|
2019-03-27 15:20:29 +00:00
|
|
|
|
|
|
|
r := bytes.NewReader(value)
|
|
|
|
|
|
|
|
var unixNano int64
|
|
|
|
if err := binary.Read(r, byteOrder, &unixNano); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
update.Time = time.Unix(0, unixNano)
|
|
|
|
|
|
|
|
if err := binary.Read(r, byteOrder, &update.State); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:01:27 +00:00
|
|
|
if err := binary.Read(r, byteOrder, &update.Cost.Server); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, byteOrder, &update.Cost.Onchain); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, byteOrder, &update.Cost.Offchain); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:20:29 +00:00
|
|
|
return update, nil
|
|
|
|
}
|