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

lightningnetwork / lnd / 15567023278

10 Jun 2025 06:10PM UTC coverage: 68.503% (+10.2%) from 58.331%
15567023278

Pull #9923

github

web-flow
Merge 6fb90c88c into 32592dbd2
Pull Request #9923: graph/db: only fetch required info for graph cache population

70 of 83 new or added lines in 6 files covered. (84.34%)

24 existing lines in 6 files now uncovered.

134420 of 196224 relevant lines covered (68.5%)

22306.35 hits per line

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

0.0
/channeldb/migration_01_to_11/zpay32/amountunits.go
1
package zpay32
2

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

7
        lnwire "github.com/lightningnetwork/lnd/channeldb/migration/lnwire21"
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) {
×
32
        return lnwire.MilliSatoshi(m) * 100000000, nil
×
33
}
×
34

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

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

45
// pBtcToMSat converts the given amount in picoBTC to millisatoshis.
46
func pBtcToMSat(p uint64) (lnwire.MilliSatoshi, error) {
×
47
        if p < 10 {
×
48
                return 0, fmt.Errorf("minimum amount is 10p")
×
49
        }
×
50
        if p%10 != 0 {
×
51
                return 0, fmt.Errorf("amount %d pBTC not expressible in msat",
×
52
                        p)
×
53
        }
×
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) {
×
59
        if msat%100000000 != 0 {
×
60
                return 0, fmt.Errorf("%d msat not expressible "+
×
61
                        "in mBTC", msat)
×
62
        }
×
63
        return uint64(msat / 100000000), nil
×
64
}
65

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

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

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

88
// decodeAmount returns the amount encoded by the provided string in
89
// millisatoshi.
90
func decodeAmount(amount string) (lnwire.MilliSatoshi, error) {
×
91
        if len(amount) < 1 {
×
92
                return 0, fmt.Errorf("amount must be non-empty")
×
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]
×
98
        digit := char - '0'
×
99
        if digit >= 0 && digit <= 9 {
×
100
                btc, err := strconv.ParseUint(amount, 10, 64)
×
101
                if err != nil {
×
102
                        return 0, err
×
103
                }
×
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]
×
109
        if !ok {
×
110
                return 0, fmt.Errorf("unknown multiplier %c", char)
×
111
        }
×
112

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

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

124
        return conv(am)
×
125
}
126

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

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

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

157
        return shortened, nil
×
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