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

lightningnetwork / lnd / 12182588486

05 Dec 2024 03:11PM UTC coverage: 48.249% (-9.7%) from 57.911%
12182588486

Pull #9260

github

yyforyongyu
workflows: increase num of tranches to 16

Keep the SQL, etcd, bitcoin rpcpolling builds and non-ubuntu builds at 8
since they are less stable.
Pull Request #9260: Beat itest [3/3]: fix all itest flakes

5 of 6 new or added lines in 2 files covered. (83.33%)

27912 existing lines in 427 files now uncovered.

97420 of 201909 relevant lines covered (48.25%)

0.51 hits per line

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

18.18
/blockcache/blockcache.go
1
package blockcache
2

3
import (
4
        "github.com/btcsuite/btcd/btcutil"
5
        "github.com/btcsuite/btcd/chaincfg/chainhash"
6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/lightninglabs/neutrino"
8
        "github.com/lightninglabs/neutrino/cache"
9
        "github.com/lightninglabs/neutrino/cache/lru"
10
        "github.com/lightningnetwork/lnd/lntypes"
11
        "github.com/lightningnetwork/lnd/multimutex"
12
)
13

14
// BlockCache is an lru cache for blocks.
15
type BlockCache struct {
16
        Cache     *lru.Cache[wire.InvVect, *neutrino.CacheableBlock]
17
        HashMutex *multimutex.Mutex[lntypes.Hash]
18
}
19

20
// NewBlockCache creates a new BlockCache with the given maximum capacity.
21
func NewBlockCache(capacity uint64) *BlockCache {
1✔
22
        return &BlockCache{
1✔
23
                Cache: lru.NewCache[wire.InvVect, *neutrino.CacheableBlock](
1✔
24
                        capacity,
1✔
25
                ),
1✔
26
                HashMutex: multimutex.NewMutex[lntypes.Hash](),
1✔
27
        }
1✔
28
}
1✔
29

30
// GetBlock first checks to see if the BlockCache already contains the block
31
// with the given hash. If it does then the block is fetched from the cache and
32
// returned. Otherwise the getBlockImpl function is used in order to fetch the
33
// new block and then it is stored in the block cache and returned.
34
func (bc *BlockCache) GetBlock(hash *chainhash.Hash,
35
        getBlockImpl func(hash *chainhash.Hash) (*wire.MsgBlock,
UNCOV
36
                error)) (*wire.MsgBlock, error) {
×
UNCOV
37

×
UNCOV
38
        bc.HashMutex.Lock(lntypes.Hash(*hash))
×
UNCOV
39
        defer bc.HashMutex.Unlock(lntypes.Hash(*hash))
×
UNCOV
40

×
UNCOV
41
        // Create an inv vector for getting the block.
×
UNCOV
42
        inv := wire.NewInvVect(wire.InvTypeWitnessBlock, hash)
×
UNCOV
43

×
UNCOV
44
        // Check if the block corresponding to the given hash is already
×
UNCOV
45
        // stored in the blockCache and return it if it is.
×
UNCOV
46
        cacheBlock, err := bc.Cache.Get(*inv)
×
UNCOV
47
        if err != nil && err != cache.ErrElementNotFound {
×
48
                return nil, err
×
49
        }
×
UNCOV
50
        if cacheBlock != nil {
×
UNCOV
51
                return cacheBlock.MsgBlock(), nil
×
UNCOV
52
        }
×
53

54
        // Fetch the block from the chain backends.
UNCOV
55
        msgBlock, err := getBlockImpl(hash)
×
UNCOV
56
        if err != nil {
×
57
                return nil, err
×
58
        }
×
59

60
        // Make a copy of the block so it won't escape to the heap.
UNCOV
61
        msgBlockCopy := msgBlock.Copy()
×
UNCOV
62
        block := btcutil.NewBlock(msgBlockCopy)
×
UNCOV
63

×
UNCOV
64
        // Add the new block to blockCache. If the Cache is at its maximum
×
UNCOV
65
        // capacity then the LFU item will be evicted in favour of this new
×
UNCOV
66
        // block.
×
UNCOV
67
        _, err = bc.Cache.Put(
×
UNCOV
68
                *inv, &neutrino.CacheableBlock{
×
UNCOV
69
                        Block: block,
×
UNCOV
70
                },
×
UNCOV
71
        )
×
UNCOV
72
        if err != nil {
×
73
                return nil, err
×
74
        }
×
75

UNCOV
76
        return msgBlockCopy, nil
×
77
}
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