• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.48
/chanacceptor/chainedacceptor.go
1
package chanacceptor
2

3
import (
4
        "sync"
5
        "sync/atomic"
6
)
7

8
// ChainedAcceptor represents a conjunction of ChannelAcceptor results.
9
type ChainedAcceptor struct {
10
        acceptorID uint64 // To be used atomically.
11

12
        // acceptors is a map of ChannelAcceptors that will be evaluated when
13
        // the ChainedAcceptor's Accept method is called.
14
        acceptors    map[uint64]ChannelAcceptor
15
        acceptorsMtx sync.RWMutex
16
}
17

18
// NewChainedAcceptor initializes a ChainedAcceptor.
19
func NewChainedAcceptor() *ChainedAcceptor {
109✔
20
        return &ChainedAcceptor{
109✔
21
                acceptors: make(map[uint64]ChannelAcceptor),
109✔
22
        }
109✔
23
}
109✔
24

25
// AddAcceptor adds a ChannelAcceptor to this ChainedAcceptor.
26
//
27
// NOTE: Part of the MultiplexAcceptor interface.
28
func (c *ChainedAcceptor) AddAcceptor(acceptor ChannelAcceptor) uint64 {
2✔
29
        id := atomic.AddUint64(&c.acceptorID, 1)
2✔
30

2✔
31
        c.acceptorsMtx.Lock()
2✔
32
        c.acceptors[id] = acceptor
2✔
33
        c.acceptorsMtx.Unlock()
2✔
34

2✔
35
        // Return the id so that a caller can call RemoveAcceptor.
2✔
36
        return id
2✔
37
}
2✔
38

39
// RemoveAcceptor removes a ChannelAcceptor from this ChainedAcceptor given
40
// an ID.
41
//
42
// NOTE: Part of the MultiplexAcceptor interface.
43
func (c *ChainedAcceptor) RemoveAcceptor(id uint64) {
2✔
44
        c.acceptorsMtx.Lock()
2✔
45
        delete(c.acceptors, id)
2✔
46
        c.acceptorsMtx.Unlock()
2✔
47
}
2✔
48

49
// numAcceptors returns the number of acceptors contained in the
50
// ChainedAcceptor.
51
func (c *ChainedAcceptor) numAcceptors() int {
6✔
52
        c.acceptorsMtx.RLock()
6✔
53
        defer c.acceptorsMtx.RUnlock()
6✔
54
        return len(c.acceptors)
6✔
55
}
6✔
56

57
// Accept evaluates the results of all ChannelAcceptors in the acceptors map
58
// and returns the conjunction of all these predicates.
59
//
60
// NOTE: Part of the ChannelAcceptor interface.
61
func (c *ChainedAcceptor) Accept(req *ChannelAcceptRequest) *ChannelAcceptResponse {
49✔
62
        c.acceptorsMtx.RLock()
49✔
63
        defer c.acceptorsMtx.RUnlock()
49✔
64

49✔
65
        var finalResp ChannelAcceptResponse
49✔
66

49✔
67
        for _, acceptor := range c.acceptors {
51✔
68
                // Call our acceptor to determine whether we want to accept this
2✔
69
                // channel.
2✔
70
                acceptorResponse := acceptor.Accept(req)
2✔
71

2✔
72
                // If we should reject the channel, we can just exit early. This
2✔
73
                // has the effect of returning the error belonging to our first
2✔
74
                // failed acceptor.
2✔
75
                if acceptorResponse.RejectChannel() {
2✔
76
                        return acceptorResponse
×
77
                }
×
78

79
                // If we have accepted the channel, we need to set the other
80
                // fields that were set in the response. However, since we are
81
                // dealing with multiple responses, we need to make sure that we
82
                // have not received inconsistent values (eg a csv delay of 1
83
                // from one acceptor, and a delay of 120 from another). We
84
                // set each value on our final response if it has not been set
85
                // yet, and allow duplicate sets if the value is the same. If
86
                // we cannot set a field, we return an error response.
87
                var err error
2✔
88
                finalResp, err = mergeResponse(finalResp, *acceptorResponse)
2✔
89
                if err != nil {
2✔
90
                        log.Errorf("response for: %x has inconsistent values: %v",
×
91
                                req.OpenChanMsg.PendingChannelID, err)
×
92

×
93
                        return NewChannelAcceptResponse(
×
94
                                false, errChannelRejected, nil, 0, 0,
×
95
                                0, 0, 0, 0, false,
×
96
                        )
×
97
                }
×
98
        }
99

100
        // If we have gone through all of our acceptors with no objections, we
101
        // can return an acceptor with a nil error.
102
        return &finalResp
49✔
103
}
104

105
// A compile-time constraint to ensure ChainedAcceptor implements the
106
// MultiplexAcceptor interface.
107
var _ MultiplexAcceptor = (*ChainedAcceptor)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc