mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-28 22:50:58 +02:00
htlcswitch: add heldHtlcSet
Isolation of the set logic so that it will be easier to add watchdog functionality later.
This commit is contained in:
75
htlcswitch/held_htlc_set.go
Normal file
75
htlcswitch/held_htlc_set.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user