2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
"github.com/lightninglabs/loop/swap"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type swapKit struct {
|
2019-03-07 04:32:24 +00:00
|
|
|
htlc *swap.Htlc
|
2019-03-06 20:13:50 +00:00
|
|
|
hash lntypes.Hash
|
|
|
|
|
|
|
|
height int32
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
log *SwapLog
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
lastUpdateTime time.Time
|
|
|
|
cost SwapCost
|
2019-03-07 04:32:24 +00:00
|
|
|
state loopdb.SwapState
|
2019-03-06 20:13:50 +00:00
|
|
|
executeConfig
|
|
|
|
swapConfig
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
contract *loopdb.SwapContract
|
|
|
|
swapType Type
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig,
|
|
|
|
contract *loopdb.SwapContract) (*swapKit, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Compose expected on-chain swap script
|
2019-03-07 04:32:24 +00:00
|
|
|
htlc, err := swap.NewHtlc(
|
2019-03-06 20:13:50 +00:00
|
|
|
contract.CltvExpiry, contract.SenderKey,
|
|
|
|
contract.ReceiverKey, hash,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log htlc address for debugging.
|
|
|
|
htlcAddress, err := htlc.Address(cfg.lnd.ChainParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
log := &SwapLog{
|
2019-03-06 20:13:50 +00:00
|
|
|
Hash: hash,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Htlc address: %v", htlcAddress)
|
|
|
|
|
|
|
|
return &swapKit{
|
|
|
|
swapConfig: *cfg,
|
|
|
|
hash: hash,
|
|
|
|
log: log,
|
|
|
|
htlc: htlc,
|
2019-03-07 04:32:24 +00:00
|
|
|
state: loopdb.StateInitiated,
|
2019-03-06 20:13:50 +00:00
|
|
|
contract: contract,
|
|
|
|
swapType: swapType,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendUpdate reports an update to the swap state.
|
|
|
|
func (s *swapKit) sendUpdate(ctx context.Context) error {
|
|
|
|
info := &SwapInfo{
|
|
|
|
SwapContract: *s.contract,
|
|
|
|
SwapHash: s.hash,
|
|
|
|
SwapType: s.swapType,
|
|
|
|
LastUpdate: s.lastUpdateTime,
|
|
|
|
State: s.state,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Infof("state %v", info.State)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.statusChan <- *info:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type genericSwap interface {
|
|
|
|
execute(mainCtx context.Context, cfg *executeConfig,
|
|
|
|
height int32) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type swapConfig struct {
|
|
|
|
lnd *lndclient.LndServices
|
2019-03-07 04:32:24 +00:00
|
|
|
store loopdb.SwapStore
|
2019-03-06 20:13:50 +00:00
|
|
|
server swapServerClient
|
|
|
|
}
|