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

lightningnetwork / lnd / 13026011979

29 Jan 2025 06:43AM UTC coverage: 58.787% (+0.01%) from 58.777%
13026011979

Pull #9432

github

NishantBansal2003
docs: add release notes.

Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
Pull Request #9432: multi: add upfront-shutdown-address to lnd.conf.

30 of 38 new or added lines in 4 files covered. (78.95%)

53 existing lines in 15 files now uncovered.

136068 of 231460 relevant lines covered (58.79%)

19262.11 hits per line

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

78.95
/chanacceptor/chainedacceptor.go
1
package chanacceptor
2

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

7
        "github.com/btcsuite/btcd/chaincfg"
8
        "github.com/lightningnetwork/lnd/lnwallet/chancloser"
9
)
10

11
// ChainedAcceptor represents a conjunction of ChannelAcceptor results.
12
type ChainedAcceptor struct {
13
        acceptorID uint64 // To be used atomically.
14

15
        // An address to enforce payout of our funds to on cooperative close.
16
        closeAddress string
17

18
        // params are our current chain params.
19
        params *chaincfg.Params
20

21
        // acceptors is a map of ChannelAcceptors that will be evaluated when
22
        // the ChainedAcceptor's Accept method is called.
23
        acceptors    map[uint64]ChannelAcceptor
24
        acceptorsMtx sync.RWMutex
25
}
26

27
// NewChainedAcceptor initializes a ChainedAcceptor.
28
func NewChainedAcceptor() *ChainedAcceptor {
108✔
29
        return &ChainedAcceptor{
108✔
30
                acceptors: make(map[uint64]ChannelAcceptor),
108✔
31
        }
108✔
32
}
108✔
33

34
// NewChainedAcceptorWithOpts initializes a ChainedAcceptor with the given
35
// closeAddress and params.
36
func NewChainedAcceptorWithOpts(
37
        closeAddress string,
38
        params *chaincfg.Params,
39
) *ChainedAcceptor {
6✔
40

6✔
41
        return &ChainedAcceptor{
6✔
42
                acceptors:    make(map[uint64]ChannelAcceptor),
6✔
43
                closeAddress: closeAddress,
6✔
44
                params:       params,
6✔
45
        }
6✔
46
}
6✔
47

48
// AddAcceptor adds a ChannelAcceptor to this ChainedAcceptor.
49
//
50
// NOTE: Part of the MultiplexAcceptor interface.
51
func (c *ChainedAcceptor) AddAcceptor(acceptor ChannelAcceptor) uint64 {
7✔
52
        id := atomic.AddUint64(&c.acceptorID, 1)
7✔
53

7✔
54
        c.acceptorsMtx.Lock()
7✔
55
        c.acceptors[id] = acceptor
7✔
56
        c.acceptorsMtx.Unlock()
7✔
57

7✔
58
        // Return the id so that a caller can call RemoveAcceptor.
7✔
59
        return id
7✔
60
}
7✔
61

62
// RemoveAcceptor removes a ChannelAcceptor from this ChainedAcceptor given
63
// an ID.
64
//
65
// NOTE: Part of the MultiplexAcceptor interface.
66
func (c *ChainedAcceptor) RemoveAcceptor(id uint64) {
7✔
67
        c.acceptorsMtx.Lock()
7✔
68
        delete(c.acceptors, id)
7✔
69
        c.acceptorsMtx.Unlock()
7✔
70
}
7✔
71

72
// numAcceptors returns the number of acceptors contained in the
73
// ChainedAcceptor.
74
func (c *ChainedAcceptor) numAcceptors() int {
9✔
75
        c.acceptorsMtx.RLock()
9✔
76
        defer c.acceptorsMtx.RUnlock()
9✔
77
        return len(c.acceptors)
9✔
78
}
9✔
79

80
// Accept evaluates the results of all ChannelAcceptors in the acceptors map
81
// and returns the conjunction of all these predicates.
82
//
83
// NOTE: Part of the ChannelAcceptor interface.
84
func (c *ChainedAcceptor) Accept(req *ChannelAcceptRequest) *ChannelAcceptResponse {
57✔
85
        c.acceptorsMtx.RLock()
57✔
86
        defer c.acceptorsMtx.RUnlock()
57✔
87

57✔
88
        var finalResp ChannelAcceptResponse
57✔
89

57✔
90
        for _, acceptor := range c.acceptors {
64✔
91
                // Call our acceptor to determine whether we want to accept this
7✔
92
                // channel.
7✔
93
                acceptorResponse := acceptor.Accept(req)
7✔
94

7✔
95
                // If we should reject the channel, we can just exit early. This
7✔
96
                // has the effect of returning the error belonging to our first
7✔
97
                // failed acceptor.
7✔
98
                if acceptorResponse.RejectChannel() {
10✔
99
                        return acceptorResponse
3✔
100
                }
3✔
101

102
                // If we have accepted the channel, we need to set the other
103
                // fields that were set in the response. However, since we are
104
                // dealing with multiple responses, we need to make sure that we
105
                // have not received inconsistent values (eg a csv delay of 1
106
                // from one acceptor, and a delay of 120 from another). We
107
                // set each value on our final response if it has not been set
108
                // yet, and allow duplicate sets if the value is the same. If
109
                // we cannot set a field, we return an error response.
110
                var err error
7✔
111
                finalResp, err = mergeResponse(finalResp, *acceptorResponse)
7✔
112
                if err != nil {
7✔
113
                        log.Errorf("response for: %x has inconsistent values: %v",
×
114
                                req.OpenChanMsg.PendingChannelID, err)
×
115

×
116
                        return NewChannelAcceptResponse(
×
117
                                false, errChannelRejected, nil, 0, 0,
×
118
                                0, 0, 0, 0, false,
×
119
                        )
×
120
                }
×
121
        }
122

123
        // Attempt to parse the upfront shutdown address provided.
124
        if len(finalResp.UpfrontShutdown) == 0 && len(c.closeAddress) != 0 {
67✔
125
                upfront, err := chancloser.ParseUpfrontShutdownAddress(
10✔
126
                        c.closeAddress, c.params,
10✔
127
                )
10✔
128
                if err != nil {
10✔
NEW
129
                        log.Errorf("Could not parse upfront shutdown for "+
×
NEW
130
                                "%x: %v", req.OpenChanMsg.PendingChannelID, err)
×
NEW
131

×
NEW
132
                        return NewChannelAcceptResponse(
×
NEW
133
                                false, errChannelRejected, nil, 0, 0,
×
NEW
134
                                0, 0, 0, 0, false,
×
NEW
135
                        )
×
NEW
136
                }
×
137
                finalResp.UpfrontShutdown = upfront
10✔
138
        }
139

140
        // If we have gone through all of our acceptors with no objections, we
141
        // can return an acceptor with a nil error.
142
        return &finalResp
57✔
143
}
144

145
// A compile-time constraint to ensure ChainedAcceptor implements the
146
// MultiplexAcceptor interface.
147
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