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

lightningnetwork / lnd / 13566028875

27 Feb 2025 12:09PM UTC coverage: 49.396% (-9.4%) from 58.748%
13566028875

Pull #9555

github

ellemouton
graph/db: populate the graph cache in Start instead of during construction

In this commit, we move the graph cache population logic out of the
ChannelGraph constructor and into its Start method instead.
Pull Request #9555: graph: extract cache from CRUD [6]

34 of 54 new or added lines in 4 files covered. (62.96%)

27464 existing lines in 436 files now uncovered.

101095 of 204664 relevant lines covered (49.4%)

1.54 hits per line

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

65.88
/zpay32/amountunits.go
1
package zpay32
2

3
import (
4
        "fmt"
5
        "strconv"
6

7
        "github.com/lightningnetwork/lnd/lnwire"
8
)
9

10
var (
11
        // toMSat is a map from a unit to a function that converts an amount
12
        // of that unit to millisatoshis.
13
        toMSat = map[byte]func(uint64) (lnwire.MilliSatoshi, error){
14
                'm': mBtcToMSat,
15
                'u': uBtcToMSat,
16
                'n': nBtcToMSat,
17
                'p': pBtcToMSat,
18
        }
19

20
        // fromMSat is a map from a unit to a function that converts an amount
21
        // in millisatoshis to an amount of that unit.
22
        fromMSat = map[byte]func(lnwire.MilliSatoshi) (uint64, error){
23
                'm': mSatToMBtc,
24
                'u': mSatToUBtc,
25
                'n': mSatToNBtc,
26
                'p': mSatToPBtc,
27
        }
28
)
29

30
// mBtcToMSat converts the given amount in milliBTC to millisatoshis.
31
func mBtcToMSat(m uint64) (lnwire.MilliSatoshi, error) {
3✔
32
        return lnwire.MilliSatoshi(m) * 100000000, nil
3✔
33
}
3✔
34

35
// uBtcToMSat converts the given amount in microBTC to millisatoshis.
36
func uBtcToMSat(u uint64) (lnwire.MilliSatoshi, error) {
3✔
37
        return lnwire.MilliSatoshi(u * 100000), nil
3✔
38
}
3✔
39

40
// nBtcToMSat converts the given amount in nanoBTC to millisatoshis.
41
func nBtcToMSat(n uint64) (lnwire.MilliSatoshi, error) {
3✔
42
        return lnwire.MilliSatoshi(n * 100), nil
3✔
43
}
3✔
44

45
// pBtcToMSat converts the given amount in picoBTC to millisatoshis.
UNCOV
46
func pBtcToMSat(p uint64) (lnwire.MilliSatoshi, error) {
×
UNCOV
47
        if p < 10 {
×
UNCOV
48
                return 0, fmt.Errorf("minimum amount is 10p")
×
UNCOV
49
        }
×
UNCOV
50
        if p%10 != 0 {
×
UNCOV
51
                return 0, fmt.Errorf("amount %d pBTC not expressible in msat",
×
UNCOV
52
                        p)
×
UNCOV
53
        }
×
UNCOV
54
        return lnwire.MilliSatoshi(p / 10), nil
×
55
}
56

57
// mSatToMBtc converts the given amount in millisatoshis to milliBTC.
58
func mSatToMBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
59
        if msat%100000000 != 0 {
6✔
60
                return 0, fmt.Errorf("%d msat not expressible "+
3✔
61
                        "in mBTC", msat)
3✔
62
        }
3✔
63
        return uint64(msat / 100000000), nil
3✔
64
}
65

66
// mSatToUBtc converts the given amount in millisatoshis to microBTC.
67
func mSatToUBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
68
        if msat%100000 != 0 {
6✔
69
                return 0, fmt.Errorf("%d msat not expressible "+
3✔
70
                        "in uBTC", msat)
3✔
71
        }
3✔
72
        return uint64(msat / 100000), nil
3✔
73
}
74

75
// mSatToNBtc converts the given amount in millisatoshis to nanoBTC.
76
func mSatToNBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
77
        if msat%100 != 0 {
3✔
UNCOV
78
                return 0, fmt.Errorf("%d msat not expressible in nBTC", msat)
×
UNCOV
79
        }
×
80
        return uint64(msat / 100), nil
3✔
81
}
82

83
// mSatToPBtc converts the given amount in millisatoshis to picoBTC.
84
func mSatToPBtc(msat lnwire.MilliSatoshi) (uint64, error) {
3✔
85
        return uint64(msat * 10), nil
3✔
86
}
3✔
87

88
// decodeAmount returns the amount encoded by the provided string in
89
// millisatoshi.
90
func decodeAmount(amount string) (lnwire.MilliSatoshi, error) {
3✔
91
        if len(amount) < 1 {
3✔
UNCOV
92
                return 0, fmt.Errorf("amount must be non-empty")
×
UNCOV
93
        }
×
94

95
        // If last character is a digit, then the amount can just be
96
        // interpreted as BTC.
97
        char := amount[len(amount)-1]
3✔
98
        digit := char - '0'
3✔
99
        if digit >= 0 && digit <= 9 {
3✔
UNCOV
100
                btc, err := strconv.ParseUint(amount, 10, 64)
×
UNCOV
101
                if err != nil {
×
UNCOV
102
                        return 0, err
×
UNCOV
103
                }
×
UNCOV
104
                return lnwire.MilliSatoshi(btc) * mSatPerBtc, nil
×
105
        }
106

107
        // If not a digit, it must be part of the known units.
108
        conv, ok := toMSat[char]
3✔
109
        if !ok {
3✔
UNCOV
110
                return 0, fmt.Errorf("unknown multiplier %c", char)
×
UNCOV
111
        }
×
112

113
        // Known unit.
114
        num := amount[:len(amount)-1]
3✔
115
        if len(num) < 1 {
3✔
UNCOV
116
                return 0, fmt.Errorf("number must be non-empty")
×
UNCOV
117
        }
×
118

119
        am, err := strconv.ParseUint(num, 10, 64)
3✔
120
        if err != nil {
3✔
UNCOV
121
                return 0, err
×
UNCOV
122
        }
×
123

124
        return conv(am)
3✔
125
}
126

127
// encodeAmount encodes the provided millisatoshi amount using as few characters
128
// as possible.
129
func encodeAmount(msat lnwire.MilliSatoshi) (string, error) {
3✔
130
        // If possible to express in BTC, that will always be the shortest
3✔
131
        // representation.
3✔
132
        if msat%mSatPerBtc == 0 {
3✔
UNCOV
133
                return strconv.FormatInt(int64(msat/mSatPerBtc), 10), nil
×
UNCOV
134
        }
×
135

136
        // Should always be expressible in pico BTC.
137
        pico, err := fromMSat['p'](msat)
3✔
138
        if err != nil {
3✔
139
                return "", fmt.Errorf("unable to express %d msat as pBTC: %w",
×
140
                        msat, err)
×
141
        }
×
142
        shortened := strconv.FormatUint(pico, 10) + "p"
3✔
143
        for unit, conv := range fromMSat {
6✔
144
                am, err := conv(msat)
3✔
145
                if err != nil {
6✔
146
                        // Not expressible using this unit.
3✔
147
                        continue
3✔
148
                }
149

150
                // Save the shortest found representation.
151
                str := strconv.FormatUint(am, 10) + string(unit)
3✔
152
                if len(str) < len(shortened) {
6✔
153
                        shortened = str
3✔
154
                }
3✔
155
        }
156

157
        return shortened, nil
3✔
158
}
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