• 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

74.6
/channeldb/peers.go
1
package channeldb
2

3
import (
4
        "bytes"
5
        "errors"
6
        "fmt"
7
        "time"
8

9
        "github.com/lightningnetwork/lnd/kvdb"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
)
12

13
var (
14
        // peersBucket is the name of a top level bucket in which we store
15
        // information about our peers. Information for different peers is
16
        // stored in buckets keyed by their public key.
17
        //
18
        //
19
        // peers-bucket
20
        //      |
21
        //      |-- <peer-pubkey>
22
        //      |        |--flap-count-key: <ts><flap count>
23
        //      |
24
        //      |-- <peer-pubkey>
25
        //      |        |--flap-count-key: <ts><flap count>
26
        peersBucket = []byte("peers-bucket")
27

28
        // flapCountKey is a key used in the peer pubkey sub-bucket that stores
29
        // the timestamp of a peer's last flap count and its all time flap
30
        // count.
31
        flapCountKey = []byte("flap-count")
32
)
33

34
var (
35
        // ErrNoPeerBucket is returned when we try to read entries for a peer
36
        // that is not tracked.
37
        ErrNoPeerBucket = errors.New("peer bucket not found")
38
)
39

40
// FlapCount contains information about a peer's flap count.
41
type FlapCount struct {
42
        // Count provides the total flap count for a peer.
43
        Count uint32
44

45
        // LastFlap is the timestamp of the last flap recorded for a peer.
46
        LastFlap time.Time
47
}
48

49
// WriteFlapCounts writes the flap count for a set of peers to disk, creating a
50
// bucket for the peer's pubkey if necessary. Note that this function overwrites
51
// the current value.
52
func (d *DB) WriteFlapCounts(flapCounts map[route.Vertex]*FlapCount) error {
1✔
53
        // Exit early if there are no updates.
1✔
54
        if len(flapCounts) == 0 {
1✔
55
                log.Debugf("No flap counts to write, skipped db update")
×
56
                return nil
×
57
        }
×
58

59
        return kvdb.Update(d, func(tx kvdb.RwTx) error {
2✔
60
                // Run through our set of flap counts and record them for
1✔
61
                // each peer, creating a bucket for the peer pubkey if required.
1✔
62
                for peer, flapCount := range flapCounts {
3✔
63
                        peers := tx.ReadWriteBucket(peersBucket)
2✔
64

2✔
65
                        peerBucket, err := peers.CreateBucketIfNotExists(
2✔
66
                                peer[:],
2✔
67
                        )
2✔
68
                        if err != nil {
2✔
69
                                return err
×
70
                        }
×
71

72
                        var b bytes.Buffer
2✔
73
                        err = serializeTime(&b, flapCount.LastFlap)
2✔
74
                        if err != nil {
2✔
75
                                return err
×
76
                        }
×
77

78
                        if err = WriteElement(&b, flapCount.Count); err != nil {
2✔
79
                                return err
×
80
                        }
×
81

82
                        err = peerBucket.Put(flapCountKey, b.Bytes())
2✔
83
                        if err != nil {
2✔
84
                                return err
×
85
                        }
×
86
                }
87

88
                return nil
1✔
89
        }, func() {})
1✔
90
}
91

92
// ReadFlapCount attempts to read the flap count for a peer, failing if the
93
// peer is not found or we do not have flap count stored.
94
func (d *DB) ReadFlapCount(pubkey route.Vertex) (*FlapCount, error) {
3✔
95
        var flapCount FlapCount
3✔
96

3✔
97
        if err := kvdb.View(d, func(tx kvdb.RTx) error {
6✔
98
                peers := tx.ReadBucket(peersBucket)
3✔
99

3✔
100
                peerBucket := peers.NestedReadBucket(pubkey[:])
3✔
101
                if peerBucket == nil {
4✔
102
                        return ErrNoPeerBucket
1✔
103
                }
1✔
104

105
                flapBytes := peerBucket.Get(flapCountKey)
2✔
106
                if flapBytes == nil {
2✔
107
                        return fmt.Errorf("flap count not recorded for: %v",
×
108
                                pubkey)
×
109
                }
×
110

111
                var (
2✔
112
                        err error
2✔
113
                        r   = bytes.NewReader(flapBytes)
2✔
114
                )
2✔
115

2✔
116
                flapCount.LastFlap, err = deserializeTime(r)
2✔
117
                if err != nil {
2✔
118
                        return err
×
119
                }
×
120

121
                return ReadElements(r, &flapCount.Count)
2✔
122
        }, func() {
3✔
123
                flapCount = FlapCount{}
3✔
124
        }); err != nil {
4✔
125
                return nil, err
1✔
126
        }
1✔
127

128
        return &flapCount, nil
2✔
129
}
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