lnd/htlcswitch/held_htlc_set.go
Joost Jager 9c063db698
htlcswitch: add heldHtlcSet
Isolation of the set logic so that it will be easier to add watchdog functionality later.
2022-10-18 18:04:33 +02:00

76 lines
1.7 KiB
Go

package htlcswitch
import (
"errors"
"fmt"
"github.com/lightningnetwork/lnd/channeldb"
)
// heldHtlcSet keeps track of outstanding intercepted forwards. It exposes
// several methods to manipulate the underlying map structure in a consistent
// way.
type heldHtlcSet struct {
set map[channeldb.CircuitKey]InterceptedForward
}
func newHeldHtlcSet() *heldHtlcSet {
return &heldHtlcSet{
set: make(map[channeldb.CircuitKey]InterceptedForward),
}
}
// forEach iterates over all held forwards and calls the given callback for each
// of them.
func (h *heldHtlcSet) forEach(cb func(InterceptedForward)) {
for _, fwd := range h.set {
cb(fwd)
}
}
// popAll calls the callback for each forward and removes them from the set.
func (h *heldHtlcSet) popAll(cb func(InterceptedForward)) {
for _, fwd := range h.set {
cb(fwd)
}
h.set = make(map[channeldb.CircuitKey]InterceptedForward)
}
// pop returns the specified forward and removes it from the set.
func (h *heldHtlcSet) pop(key channeldb.CircuitKey) (InterceptedForward, error) {
intercepted, ok := h.set[key]
if !ok {
return nil, fmt.Errorf("fwd %v not found", key)
}
delete(h.set, key)
return intercepted, nil
}
// exists tests whether the specified forward is part of the set.
func (h *heldHtlcSet) exists(key channeldb.CircuitKey) bool {
_, ok := h.set[key]
return ok
}
// push adds the specified forward to the set. An error is returned if the
// forward exists already.
func (h *heldHtlcSet) push(key channeldb.CircuitKey,
fwd InterceptedForward) error {
if fwd == nil {
return errors.New("nil fwd pushed")
}
if h.exists(key) {
return errors.New("htlc already exists in set")
}
h.set[key] = fwd
return nil
}