/
htlc_sets.go
67 lines (53 loc) · 1.28 KB
/
htlc_sets.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
package lnmux
import (
"github.com/bottlepay/lnmux/types"
"github.com/lightningnetwork/lnd/lntypes"
)
type htlcSets interface {
get(hash lntypes.Hash) (htlcSet, bool)
forEach(cb func(htlcSet))
add(set *htlcSetParameters, htlcKey types.HtlcKey, htlcAmt int64) htlcSet
}
type htlcSetsImpl struct {
sets map[lntypes.Hash]*htlcSetImpl
}
func newHtlcSets() *htlcSetsImpl {
return &htlcSetsImpl{
sets: make(map[lntypes.Hash]*htlcSetImpl),
}
}
type htlcSetParameters struct {
preimage lntypes.Preimage
value int64
paymentAddr [32]byte
}
// add adds a new htlc set and the first accepted htlc for that set.
func (h *htlcSetsImpl) add(set *htlcSetParameters, htlcKey types.HtlcKey,
htlcAmt int64) htlcSet {
hash := set.preimage.Hash()
if _, ok := h.sets[hash]; ok {
panic("set already exists")
}
htlcSet := newHtlcSetImpl(h, *set)
htlcSet.addHtlc(htlcKey, htlcAmt)
h.sets[hash] = htlcSet
return htlcSet
}
func (h *htlcSetsImpl) get(hash lntypes.Hash) (htlcSet, bool) {
set, ok := h.sets[hash]
if !ok {
return nil, false
}
return set, ok
}
func (h *htlcSetsImpl) forEach(cb func(htlcSet)) {
for _, set := range h.sets {
cb(set)
}
}
func (h *htlcSetsImpl) delete(hash lntypes.Hash) {
if _, ok := h.sets[hash]; !ok {
panic("set not found")
}
delete(h.sets, hash)
}