mirror of
https://github.com/lightninglabs/loop
synced 2024-11-04 06:00:21 +00:00
loopin+loopout: move htlc out of swapKit
This commit moves htlc out of swapkit in preparation of adding separate p2wsh and np2wsh htlcs to loop-in swaps.
This commit is contained in:
parent
7a44eec36f
commit
b2b0bad951
42
loopin.go
42
loopin.go
@ -48,8 +48,12 @@ var (
|
||||
type loopInSwap struct {
|
||||
swapKit
|
||||
|
||||
executeConfig
|
||||
|
||||
loopdb.LoopInContract
|
||||
|
||||
htlc *swap.Htlc
|
||||
|
||||
timeoutAddr btcutil.Address
|
||||
}
|
||||
|
||||
@ -148,19 +152,25 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
|
||||
},
|
||||
}
|
||||
|
||||
swapKit, err := newSwapKit(
|
||||
swapKit := newSwapKit(
|
||||
swapHash, swap.TypeIn, cfg, &contract.SwapContract,
|
||||
swap.HtlcNP2WSH,
|
||||
)
|
||||
|
||||
swapKit.lastUpdateTime = initiationTime
|
||||
|
||||
// Create the htlc.
|
||||
htlc, err := swapKit.getHtlc(swap.HtlcNP2WSH)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
swapKit.lastUpdateTime = initiationTime
|
||||
// Log htlc address for debugging.
|
||||
swapKit.log.Infof("Htlc address: %v", htlc.Address)
|
||||
|
||||
swap := &loopInSwap{
|
||||
LoopInContract: contract,
|
||||
swapKit: *swapKit,
|
||||
htlc: htlc,
|
||||
}
|
||||
|
||||
// Persist the data before exiting this function, so that the caller can
|
||||
@ -182,17 +192,23 @@ func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig,
|
||||
|
||||
log.Infof("Resuming loop in swap %v", hash)
|
||||
|
||||
swapKit, err := newSwapKit(
|
||||
swapKit := newSwapKit(
|
||||
hash, swap.TypeIn, cfg, &pend.Contract.SwapContract,
|
||||
swap.HtlcNP2WSH,
|
||||
)
|
||||
|
||||
// Create the htlc.
|
||||
htlc, err := swapKit.getHtlc(swap.HtlcNP2WSH)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Log htlc address for debugging.
|
||||
swapKit.log.Infof("Htlc address: %v", htlc.Address)
|
||||
|
||||
swap := &loopInSwap{
|
||||
LoopInContract: *pend.Contract,
|
||||
swapKit: *swapKit,
|
||||
htlc: htlc,
|
||||
}
|
||||
|
||||
lastUpdate := pend.LastUpdate()
|
||||
@ -222,6 +238,22 @@ func validateLoopInContract(lnd *lndclient.LndServices,
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendUpdate reports an update to the swap state.
|
||||
func (s *loopInSwap) sendUpdate(ctx context.Context) error {
|
||||
info := s.swapInfo()
|
||||
s.log.Infof("Loop in swap state: %v", info.State)
|
||||
|
||||
info.HtlcAddress = s.htlc.Address
|
||||
|
||||
select {
|
||||
case s.statusChan <- *info:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// execute starts/resumes the swap. It is a thin wrapper around executeSwap to
|
||||
// conveniently handle the error case.
|
||||
func (s *loopInSwap) execute(mainCtx context.Context,
|
||||
|
43
loopout.go
43
loopout.go
@ -52,6 +52,10 @@ type loopOutSwap struct {
|
||||
|
||||
loopdb.LoopOutContract
|
||||
|
||||
executeConfig
|
||||
|
||||
htlc *swap.Htlc
|
||||
|
||||
swapPaymentChan chan lndclient.PaymentResult
|
||||
prePaymentChan chan lndclient.PaymentResult
|
||||
}
|
||||
@ -134,19 +138,25 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
|
||||
},
|
||||
}
|
||||
|
||||
swapKit, err := newSwapKit(
|
||||
swapKit := newSwapKit(
|
||||
swapHash, swap.TypeOut, cfg, &contract.SwapContract,
|
||||
swap.HtlcP2WSH,
|
||||
)
|
||||
|
||||
swapKit.lastUpdateTime = initiationTime
|
||||
|
||||
// Create the htlc.
|
||||
htlc, err := swapKit.getHtlc(swap.HtlcP2WSH)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
swapKit.lastUpdateTime = initiationTime
|
||||
// Log htlc address for debugging.
|
||||
swapKit.log.Infof("Htlc address: %v", htlc.Address)
|
||||
|
||||
swap := &loopOutSwap{
|
||||
LoopOutContract: contract,
|
||||
swapKit: *swapKit,
|
||||
htlc: htlc,
|
||||
}
|
||||
|
||||
// Persist the data before exiting this function, so that the caller
|
||||
@ -168,17 +178,24 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
|
||||
|
||||
log.Infof("Resuming loop out swap %v", hash)
|
||||
|
||||
swapKit, err := newSwapKit(
|
||||
swapKit := newSwapKit(
|
||||
hash, swap.TypeOut, cfg, &pend.Contract.SwapContract,
|
||||
swap.HtlcP2WSH,
|
||||
)
|
||||
|
||||
// Create the htlc.
|
||||
htlc, err := swapKit.getHtlc(swap.HtlcP2WSH)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Log htlc address for debugging.
|
||||
swapKit.log.Infof("Htlc address: %v", htlc.Address)
|
||||
|
||||
// Create the swap.
|
||||
swap := &loopOutSwap{
|
||||
LoopOutContract: *pend.Contract,
|
||||
swapKit: *swapKit,
|
||||
htlc: htlc,
|
||||
}
|
||||
|
||||
lastUpdate := pend.LastUpdate()
|
||||
@ -192,6 +209,22 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
|
||||
return swap, nil
|
||||
}
|
||||
|
||||
// sendUpdate reports an update to the swap state.
|
||||
func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
|
||||
info := s.swapInfo()
|
||||
s.log.Infof("Loop out swap state: %v", info.State)
|
||||
|
||||
info.HtlcAddress = s.htlc.Address
|
||||
|
||||
select {
|
||||
case s.statusChan <- *info:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// execute starts/resumes the swap. It is a thin wrapper around
|
||||
// executeAndFinalize to conveniently handle the error case.
|
||||
func (s *loopOutSwap) execute(mainCtx context.Context,
|
||||
|
58
swap.go
58
swap.go
@ -11,7 +11,6 @@ import (
|
||||
)
|
||||
|
||||
type swapKit struct {
|
||||
htlc *swap.Htlc
|
||||
hash lntypes.Hash
|
||||
|
||||
height int32
|
||||
@ -19,51 +18,49 @@ type swapKit struct {
|
||||
log *swap.PrefixLog
|
||||
|
||||
lastUpdateTime time.Time
|
||||
cost loopdb.SwapCost
|
||||
state loopdb.SwapState
|
||||
executeConfig
|
||||
swapConfig
|
||||
|
||||
cost loopdb.SwapCost
|
||||
|
||||
state loopdb.SwapState
|
||||
|
||||
contract *loopdb.SwapContract
|
||||
|
||||
swapType swap.Type
|
||||
|
||||
swapConfig
|
||||
}
|
||||
|
||||
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
|
||||
contract *loopdb.SwapContract, outputType swap.HtlcOutputType) (
|
||||
*swapKit, error) {
|
||||
|
||||
// Compose expected on-chain swap script
|
||||
htlc, err := swap.NewHtlc(
|
||||
contract.CltvExpiry, contract.SenderKey,
|
||||
contract.ReceiverKey, hash, outputType,
|
||||
cfg.lnd.ChainParams,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contract *loopdb.SwapContract) *swapKit {
|
||||
|
||||
log := &swap.PrefixLog{
|
||||
Hash: hash,
|
||||
Logger: log,
|
||||
}
|
||||
|
||||
// Log htlc address for debugging.
|
||||
log.Infof("Htlc address: %v", htlc.Address)
|
||||
|
||||
return &swapKit{
|
||||
swapConfig: *cfg,
|
||||
hash: hash,
|
||||
log: log,
|
||||
htlc: htlc,
|
||||
state: loopdb.StateInitiated,
|
||||
contract: contract,
|
||||
swapType: swapType,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// sendUpdate reports an update to the swap state.
|
||||
func (s *swapKit) sendUpdate(ctx context.Context) error {
|
||||
info := &SwapInfo{
|
||||
// getHtlc composes and returns the on-chain swap script.
|
||||
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
|
||||
return swap.NewHtlc(
|
||||
s.contract.CltvExpiry, s.contract.SenderKey,
|
||||
s.contract.ReceiverKey, s.hash, outputType,
|
||||
s.swapConfig.lnd.ChainParams,
|
||||
)
|
||||
}
|
||||
|
||||
// swapInfo constructs and returns a filled SwapInfo from
|
||||
// the swapKit.
|
||||
func (s *swapKit) swapInfo() *SwapInfo {
|
||||
return &SwapInfo{
|
||||
SwapContract: *s.contract,
|
||||
SwapHash: s.hash,
|
||||
SwapType: s.swapType,
|
||||
@ -72,18 +69,7 @@ func (s *swapKit) sendUpdate(ctx context.Context) error {
|
||||
State: s.state,
|
||||
Cost: s.cost,
|
||||
},
|
||||
HtlcAddress: s.htlc.Address,
|
||||
}
|
||||
|
||||
s.log.Infof("state %v", info.State)
|
||||
|
||||
select {
|
||||
case s.statusChan <- *info:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type genericSwap interface {
|
||||
|
Loading…
Reference in New Issue
Block a user