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

lightningnetwork / lnd / 13727082033

07 Mar 2025 06:37PM UTC coverage: 58.289% (-10.3%) from 68.615%
13727082033

push

github

web-flow
Merge pull request #9581 from yyforyongyu/fix-TestReconnectSucceed

tor: fix `TestReconnectSucceed`

94454 of 162044 relevant lines covered (58.29%)

1.81 hits per line

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

70.9
/lnrpc/marshall_utils.go
1
package lnrpc
2

3
import (
4
        "encoding/hex"
5
        "errors"
6
        "fmt"
7

8
        "github.com/btcsuite/btcd/btcutil"
9
        "github.com/btcsuite/btcd/chaincfg"
10
        "github.com/btcsuite/btcd/txscript"
11
        "github.com/btcsuite/btcd/wire"
12
        "github.com/btcsuite/btcwallet/wallet"
13
        "github.com/lightningnetwork/lnd/aliasmgr"
14
        "github.com/lightningnetwork/lnd/fn/v2"
15
        "github.com/lightningnetwork/lnd/lnwallet"
16
        "github.com/lightningnetwork/lnd/lnwire"
17
        "golang.org/x/exp/maps"
18
)
19

20
var (
21
        // ErrSatMsatMutualExclusive is returned when both a sat and an msat
22
        // amount are set.
23
        ErrSatMsatMutualExclusive = errors.New(
24
                "sat and msat arguments are mutually exclusive",
25
        )
26

27
        // ErrNegativeAmt is returned when a negative amount is specified.
28
        ErrNegativeAmt = errors.New("amount cannot be negative")
29
)
30

31
// CalculateFeeLimit returns the fee limit in millisatoshis. If a percentage
32
// based fee limit has been requested, we'll factor in the ratio provided with
33
// the amount of the payment.
34
func CalculateFeeLimit(feeLimit *FeeLimit,
35
        amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
3✔
36

3✔
37
        switch feeLimit.GetLimit().(type) {
3✔
38
        case *FeeLimit_Fixed:
3✔
39
                return lnwire.NewMSatFromSatoshis(
3✔
40
                        btcutil.Amount(feeLimit.GetFixed()),
3✔
41
                )
3✔
42

43
        case *FeeLimit_FixedMsat:
×
44
                return lnwire.MilliSatoshi(feeLimit.GetFixedMsat())
×
45

46
        case *FeeLimit_Percent:
3✔
47
                return amount * lnwire.MilliSatoshi(feeLimit.GetPercent()) / 100
3✔
48

49
        default:
3✔
50
                // Fall back to a sane default value that is based on the amount
3✔
51
                // itself.
3✔
52
                return lnwallet.DefaultRoutingFeeLimitForAmount(amount)
3✔
53
        }
54
}
55

56
// UnmarshallAmt returns a strong msat type for a sat/msat pair of rpc fields.
57
func UnmarshallAmt(amtSat, amtMsat int64) (lnwire.MilliSatoshi, error) {
3✔
58
        if amtSat != 0 && amtMsat != 0 {
3✔
59
                return 0, ErrSatMsatMutualExclusive
×
60
        }
×
61

62
        if amtSat < 0 || amtMsat < 0 {
3✔
63
                return 0, ErrNegativeAmt
×
64
        }
×
65

66
        if amtSat != 0 {
6✔
67
                return lnwire.NewMSatFromSatoshis(btcutil.Amount(amtSat)), nil
3✔
68
        }
3✔
69

70
        return lnwire.MilliSatoshi(amtMsat), nil
3✔
71
}
72

73
// ParseConfs validates the minimum and maximum confirmation arguments of a
74
// ListUnspent request.
75
func ParseConfs(min, max int32) (int32, int32, error) {
3✔
76
        switch {
3✔
77
        // Ensure that the user didn't attempt to specify a negative number of
78
        // confirmations, as that isn't possible.
79
        case min < 0:
×
80
                return 0, 0, fmt.Errorf("min confirmations must be >= 0")
×
81

82
        // We'll also ensure that the min number of confs is strictly less than
83
        // or equal to the max number of confs for sanity.
84
        case min > max:
×
85
                return 0, 0, fmt.Errorf("max confirmations must be >= min " +
×
86
                        "confirmations")
×
87

88
        default:
3✔
89
                return min, max, nil
3✔
90
        }
91
}
92

93
// MarshalUtxos translates a []*lnwallet.Utxo into a []*lnrpc.Utxo.
94
func MarshalUtxos(utxos []*lnwallet.Utxo, activeNetParams *chaincfg.Params) (
95
        []*Utxo, error) {
3✔
96

3✔
97
        res := make([]*Utxo, 0, len(utxos))
3✔
98
        for _, utxo := range utxos {
6✔
99
                // Translate lnwallet address type to the proper gRPC proto
3✔
100
                // address type.
3✔
101
                var addrType AddressType
3✔
102
                switch utxo.AddressType {
3✔
103
                case lnwallet.WitnessPubKey:
3✔
104
                        addrType = AddressType_WITNESS_PUBKEY_HASH
3✔
105

106
                case lnwallet.NestedWitnessPubKey:
3✔
107
                        addrType = AddressType_NESTED_PUBKEY_HASH
3✔
108

109
                case lnwallet.TaprootPubkey:
3✔
110
                        addrType = AddressType_TAPROOT_PUBKEY
3✔
111

112
                case lnwallet.UnknownAddressType:
×
113
                        continue
×
114

115
                default:
×
116
                        return nil, fmt.Errorf("invalid utxo address type")
×
117
                }
118

119
                // Now that we know we have a proper mapping to an address,
120
                // we'll convert the regular outpoint to an lnrpc variant.
121
                outpoint := MarshalOutPoint(&utxo.OutPoint)
3✔
122

3✔
123
                utxoResp := Utxo{
3✔
124
                        AddressType:   addrType,
3✔
125
                        AmountSat:     int64(utxo.Value),
3✔
126
                        PkScript:      hex.EncodeToString(utxo.PkScript),
3✔
127
                        Outpoint:      outpoint,
3✔
128
                        Confirmations: utxo.Confirmations,
3✔
129
                }
3✔
130

3✔
131
                // Finally, we'll attempt to extract the raw address from the
3✔
132
                // script so we can display a human friendly address to the end
3✔
133
                // user.
3✔
134
                _, outAddresses, _, err := txscript.ExtractPkScriptAddrs(
3✔
135
                        utxo.PkScript, activeNetParams,
3✔
136
                )
3✔
137
                if err != nil {
3✔
138
                        return nil, err
×
139
                }
×
140

141
                // If we can't properly locate a single address, then this was
142
                // an error in our mapping, and we'll return an error back to
143
                // the user.
144
                if len(outAddresses) != 1 {
3✔
145
                        return nil, fmt.Errorf("an output was unexpectedly " +
×
146
                                "multisig")
×
147
                }
×
148
                utxoResp.Address = outAddresses[0].String()
3✔
149

3✔
150
                res = append(res, &utxoResp)
3✔
151
        }
152

153
        return res, nil
3✔
154
}
155

156
// MarshallOutputType translates a txscript.ScriptClass into a
157
// lnrpc.OutputScriptType.
158
func MarshallOutputType(o txscript.ScriptClass) OutputScriptType {
3✔
159
        // Translate txscript ScriptClass type to the proper gRPC proto
3✔
160
        // output script type.
3✔
161
        switch o {
3✔
162
        case txscript.ScriptHashTy:
×
163
                return OutputScriptType_SCRIPT_TYPE_SCRIPT_HASH
×
164
        case txscript.WitnessV0PubKeyHashTy:
3✔
165
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V0_PUBKEY_HASH
3✔
166
        case txscript.WitnessV0ScriptHashTy:
3✔
167
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V0_SCRIPT_HASH
3✔
168
        case txscript.PubKeyTy:
×
169
                return OutputScriptType_SCRIPT_TYPE_PUBKEY
×
170
        case txscript.MultiSigTy:
×
171
                return OutputScriptType_SCRIPT_TYPE_MULTISIG
×
172
        case txscript.NullDataTy:
×
173
                return OutputScriptType_SCRIPT_TYPE_NULLDATA
×
174
        case txscript.NonStandardTy:
×
175
                return OutputScriptType_SCRIPT_TYPE_NON_STANDARD
×
176
        case txscript.WitnessUnknownTy:
×
177
                return OutputScriptType_SCRIPT_TYPE_WITNESS_UNKNOWN
×
178
        case txscript.WitnessV1TaprootTy:
3✔
179
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V1_TAPROOT
3✔
180
        default:
3✔
181
                return OutputScriptType_SCRIPT_TYPE_PUBKEY_HASH
3✔
182
        }
183
}
184

185
// MarshalOutPoint converts a wire.OutPoint to its proto counterpart.
186
func MarshalOutPoint(op *wire.OutPoint) *OutPoint {
3✔
187
        return &OutPoint{
3✔
188
                TxidBytes:   op.Hash[:],
3✔
189
                TxidStr:     op.Hash.String(),
3✔
190
                OutputIndex: op.Index,
3✔
191
        }
3✔
192
}
3✔
193

194
// UnmarshallCoinSelectionStrategy converts a lnrpc.CoinSelectionStrategy proto
195
// type to its wallet.CoinSelectionStrategy counterpart type, considering
196
// a global default strategy if necessary.
197
//
198
// The globalStrategy parameter specifies the default coin selection strategy
199
// to use if the strategy is set to STRATEGY_USE_GLOBAL_CONFIG. This allows
200
// flexibility in defining a default strategy at a global level.
201
func UnmarshallCoinSelectionStrategy(strategy CoinSelectionStrategy,
202
        globalStrategy wallet.CoinSelectionStrategy) (
203
        wallet.CoinSelectionStrategy, error) {
3✔
204

3✔
205
        switch strategy {
3✔
206
        case CoinSelectionStrategy_STRATEGY_USE_GLOBAL_CONFIG:
3✔
207
                return globalStrategy, nil
3✔
208

209
        case CoinSelectionStrategy_STRATEGY_LARGEST:
×
210
                return wallet.CoinSelectionLargest, nil
×
211

212
        case CoinSelectionStrategy_STRATEGY_RANDOM:
×
213
                return wallet.CoinSelectionRandom, nil
×
214

215
        default:
×
216
                return nil, fmt.Errorf("unknown coin selection strategy "+
×
217
                        "%v", strategy)
×
218
        }
219
}
220

221
// MarshalAliasMap converts a ScidAliasMap to its proto counterpart. This is
222
// used in various RPCs that handle scid alias mappings.
223
func MarshalAliasMap(scidMap aliasmgr.ScidAliasMap) []*AliasMap {
3✔
224
        return fn.Map(
3✔
225
                maps.Keys(scidMap),
3✔
226
                func(base lnwire.ShortChannelID) *AliasMap {
6✔
227
                        return &AliasMap{
3✔
228
                                BaseScid: base.ToUint64(),
3✔
229
                                Aliases: fn.Map(
3✔
230
                                        scidMap[base],
3✔
231
                                        func(a lnwire.ShortChannelID) uint64 {
6✔
232
                                                return a.ToUint64()
3✔
233
                                        },
3✔
234
                                ),
235
                        }
236
                },
237
        )
238
}
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