forked from lightninglabs/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.go
114 lines (90 loc) · 2.45 KB
/
swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package loop
import (
"context"
"time"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lntypes"
)
type swapKit struct {
hash lntypes.Hash
height int32
log *swap.PrefixLog
lastUpdateTime time.Time
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) *swapKit {
log := &swap.PrefixLog{
Hash: hash,
Logger: log,
}
return &swapKit{
swapConfig: *cfg,
hash: hash,
log: log,
state: loopdb.StateInitiated,
contract: contract,
swapType: swapType,
}
}
// GetHtlcScriptVersion returns the correct HTLC script version for the passed
// protocol version.
func GetHtlcScriptVersion(
protocolVersion loopdb.ProtocolVersion) swap.ScriptVersion {
if protocolVersion != loopdb.ProtocolVersionUnrecorded &&
protocolVersion >= loopdb.ProtocolVersionHtlcV2 {
// Use HTLC v2 script only if we know the swap was initiated
// with a client that supports HTLC v2. Unrecorded protocol
// version implies that there was no protocol version stored
// along side a serialized swap that we're resuming in which
// case the swap was initiated with HTLC v1 script.
return swap.HtlcV2
}
return swap.HtlcV1
}
// getHtlc composes and returns the on-chain swap script.
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
return swap.NewHtlc(
GetHtlcScriptVersion(s.contract.ProtocolVersion),
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,
LastUpdate: s.lastUpdateTime,
SwapStateData: loopdb.SwapStateData{
State: s.state,
Cost: s.cost,
},
}
}
type genericSwap interface {
execute(mainCtx context.Context, cfg *executeConfig,
height int32) error
}
type swapConfig struct {
lnd *lndclient.LndServices
store loopdb.SwapStore
server swapServerClient
}
func newSwapConfig(lnd *lndclient.LndServices, store loopdb.SwapStore,
server swapServerClient) *swapConfig {
return &swapConfig{
lnd: lnd,
store: store,
server: server,
}
}