-
Notifications
You must be signed in to change notification settings - Fork 6
/
htlc_set.go
124 lines (94 loc) · 2.14 KB
/
htlc_set.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
115
116
117
118
119
120
121
122
123
124
package lnmux
import (
"github.com/bottlepay/lnmux/types"
"github.com/lightningnetwork/lnd/lntypes"
)
type htlcSet interface {
addHtlc(key types.HtlcKey, amt int64)
getHtlcMap() map[types.HtlcKey]int64
accepted(key types.HtlcKey) bool
totalSetAmt() int64
isComplete() *SetID
deleteAll(cb func(types.HtlcKey))
deleteHtlc(key types.HtlcKey)
hash() lntypes.Hash
paymentAddr() [32]byte
value() int64
preimage() lntypes.Preimage
}
type htlcSetImpl struct {
params htlcSetParameters
htlcs map[types.HtlcKey]int64
parent *htlcSetsImpl
}
func newHtlcSetImpl(parent *htlcSetsImpl,
params htlcSetParameters) *htlcSetImpl {
return &htlcSetImpl{
params: params,
htlcs: make(map[types.HtlcKey]int64),
parent: parent,
}
}
func (h *htlcSetImpl) deleteAll(cb func(types.HtlcKey)) {
for key := range h.htlcs {
cb(key)
}
h.parent.delete(h.params.preimage.Hash())
}
func (h *htlcSetImpl) totalSetAmt() int64 {
var total int64
for _, amt := range h.htlcs {
total += amt
}
return total
}
func (h *htlcSetImpl) isComplete() *SetID {
if h.totalSetAmt() != h.params.value {
return nil
}
var keys []types.HtlcKey
for htlc := range h.htlcs {
keys = append(keys, htlc)
}
hash := newSetID(keys)
return &hash
}
func (h *htlcSetImpl) hash() lntypes.Hash {
return h.params.preimage.Hash()
}
func (h *htlcSetImpl) paymentAddr() [32]byte {
return h.params.paymentAddr
}
func (h *htlcSetImpl) value() int64 {
return h.params.value
}
func (h *htlcSetImpl) preimage() lntypes.Preimage {
return h.params.preimage
}
func (h *htlcSetImpl) getHtlcMap() map[types.HtlcKey]int64 {
htlcMap := make(map[types.HtlcKey]int64)
for key, amt := range h.htlcs {
htlcMap[key] = amt
}
return htlcMap
}
func (h *htlcSetImpl) accepted(key types.HtlcKey) bool {
_, ok := h.htlcs[key]
return ok
}
func (h *htlcSetImpl) deleteHtlc(key types.HtlcKey) {
_, ok := h.htlcs[key]
if !ok {
panic("htlc not found")
}
delete(h.htlcs, key)
if len(h.htlcs) == 0 {
h.parent.delete(h.params.preimage.Hash())
}
}
func (h *htlcSetImpl) addHtlc(key types.HtlcKey, amt int64) {
if _, ok := h.htlcs[key]; ok {
panic("htlc already exists")
}
h.htlcs[key] = amt
}