github.com/decred/dcrlnd@v0.7.6/lnwallet/errors.go (about) 1 package lnwallet 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/decred/dcrd/chaincfg/chainhash" 8 "github.com/decred/dcrd/dcrutil/v4" 9 "github.com/decred/dcrd/wire" 10 "github.com/decred/dcrlnd/lnwire" 11 ) 12 13 // ReservationError wraps certain errors returned during channel reservation 14 // that can be sent across the wire to the remote peer. Errors not being 15 // ReservationErrors will not be sent to the remote in case of a failed channel 16 // reservation, as they may contain private information. 17 type ReservationError struct { 18 error 19 } 20 21 // A compile time check to ensure ReservationError implements the error 22 // interface. 23 var _ error = (*ReservationError)(nil) 24 25 // ErrZeroCapacity returns an error indicating the funder attempted to put zero 26 // funds into the channel. 27 func ErrZeroCapacity() ReservationError { 28 return ReservationError{ 29 errors.New("zero channel funds"), 30 } 31 } 32 33 // ErrChainMismatch returns an error indicating that the initiator tried to 34 // open a channel for an unknown chain. 35 func ErrChainMismatch(knownChain, 36 unknownChain *chainhash.Hash) ReservationError { 37 return ReservationError{ 38 fmt.Errorf("unknown chain=%v, supported chain=%v", 39 unknownChain, knownChain), 40 } 41 } 42 43 // ErrFunderBalanceDust returns an error indicating the initial balance of the 44 // funder is considered dust at the current commitment fee. 45 func ErrFunderBalanceDust(commitFee, funderBalance, 46 minBalance int64) ReservationError { 47 return ReservationError{ 48 fmt.Errorf("funder balance too small (%v) with fee=%v atoms, "+ 49 "minimum=%v atoms required", funderBalance, 50 commitFee, minBalance), 51 } 52 } 53 54 // ErrCsvDelayTooLarge returns an error indicating that the CSV delay was to 55 // large to be accepted, along with the current max. 56 func ErrCsvDelayTooLarge(remoteDelay, maxDelay uint16) ReservationError { 57 return ReservationError{ 58 fmt.Errorf("CSV delay too large: %v, max is %v", 59 remoteDelay, maxDelay), 60 } 61 } 62 63 // ErrChanReserveTooSmall returns an error indicating that the channel reserve 64 // the remote is requiring is too small to be accepted. 65 func ErrChanReserveTooSmall(reserve, dustLimit dcrutil.Amount) ReservationError { 66 return ReservationError{ 67 fmt.Errorf("channel reserve of %v atoms is too small, min is %v "+ 68 "atoms", int64(reserve), int64(dustLimit)), 69 } 70 } 71 72 // ErrChanReserveTooLarge returns an error indicating that the chan reserve the 73 // remote is requiring, is too large to be accepted. 74 func ErrChanReserveTooLarge(reserve, 75 maxReserve dcrutil.Amount) ReservationError { 76 return ReservationError{ 77 fmt.Errorf("channel reserve is too large: %v atoms, max "+ 78 "is %v atoms", int64(reserve), int64(maxReserve)), 79 } 80 } 81 82 // ErrNonZeroPushAmount is returned by a remote peer that receives a 83 // FundingOpen request for a channel with non-zero push amount while 84 // they have 'rejectpush' enabled. 85 func ErrNonZeroPushAmount() ReservationError { 86 return ReservationError{errors.New("non-zero push amounts are disabled")} 87 } 88 89 // ErrMinHtlcTooLarge returns an error indicating that the MinHTLC value the 90 // remote required is too large to be accepted. 91 func ErrMinHtlcTooLarge(minHtlc, 92 maxMinHtlc lnwire.MilliAtom) ReservationError { 93 return ReservationError{ 94 fmt.Errorf("minimum HTLC value is too large: %v, max is %v", 95 minHtlc, maxMinHtlc), 96 } 97 } 98 99 // ErrMaxHtlcNumTooLarge returns an error indicating that the 'max HTLCs in 100 // flight' value the remote required is too large to be accepted. 101 func ErrMaxHtlcNumTooLarge(maxHtlc, maxMaxHtlc uint16) ReservationError { 102 return ReservationError{ 103 fmt.Errorf("maxHtlcs is too large: %d, max is %d", 104 maxHtlc, maxMaxHtlc), 105 } 106 } 107 108 // ErrMaxHtlcNumTooSmall returns an error indicating that the 'max HTLCs in 109 // flight' value the remote required is too small to be accepted. 110 func ErrMaxHtlcNumTooSmall(maxHtlc, minMaxHtlc uint16) ReservationError { 111 return ReservationError{ 112 fmt.Errorf("maxHtlcs is too small: %d, min is %d", 113 maxHtlc, minMaxHtlc), 114 } 115 } 116 117 // ErrMaxValueInFlightTooSmall returns an error indicating that the 'max HTLC 118 // value in flight' the remote required is too small to be accepted. 119 func ErrMaxValueInFlightTooSmall(maxValInFlight, 120 minMaxValInFlight lnwire.MilliAtom) ReservationError { 121 return ReservationError{ 122 fmt.Errorf("maxValueInFlight too small: %v, min is %v", 123 maxValInFlight, minMaxValInFlight), 124 } 125 } 126 127 // ErrNumConfsTooLarge returns an error indicating that the number of 128 // confirmations required for a channel is too large. 129 func ErrNumConfsTooLarge(numConfs, maxNumConfs uint32) error { 130 return ReservationError{ 131 fmt.Errorf("minimum depth of %d is too large, max is %d", 132 numConfs, maxNumConfs), 133 } 134 } 135 136 // ErrChanTooSmall returns an error indicating that an incoming channel request 137 // was too small. We'll reject any incoming channels if they're below our 138 // configured value for the min channel size we'll accept. 139 func ErrChanTooSmall(chanSize, minChanSize dcrutil.Amount) ReservationError { 140 return ReservationError{ 141 fmt.Errorf("chan size of %v is below min chan size of %v", 142 chanSize, minChanSize), 143 } 144 } 145 146 // ErrChanTooLarge returns an error indicating that an incoming channel request 147 // was too large. We'll reject any incoming channels if they're above our 148 // configured value for the max channel size we'll accept. 149 func ErrChanTooLarge(chanSize, maxChanSize dcrutil.Amount) ReservationError { 150 return ReservationError{ 151 fmt.Errorf("chan size of %v exceeds maximum chan size of %v", 152 chanSize, maxChanSize), 153 } 154 } 155 156 // ErrInvalidDustLimit returns an error indicating that a proposed DustLimit 157 // was rejected. 158 func ErrInvalidDustLimit(dustLimit dcrutil.Amount) ReservationError { 159 return ReservationError{ 160 fmt.Errorf("dust limit %v is invalid", dustLimit), 161 } 162 } 163 164 // ErrHtlcIndexAlreadyFailed is returned when the HTLC index has already been 165 // failed, but has not been committed by our commitment state. 166 type ErrHtlcIndexAlreadyFailed uint64 167 168 // Error returns a message indicating the index that had already been failed. 169 func (e ErrHtlcIndexAlreadyFailed) Error() string { 170 return fmt.Sprintf("HTLC with ID %d has already been failed", e) 171 } 172 173 // ErrHtlcIndexAlreadySettled is returned when the HTLC index has already been 174 // settled, but has not been committed by our commitment state. 175 type ErrHtlcIndexAlreadySettled uint64 176 177 // Error returns a message indicating the index that had already been settled. 178 func (e ErrHtlcIndexAlreadySettled) Error() string { 179 return fmt.Sprintf("HTLC with ID %d has already been settled", e) 180 } 181 182 // ErrInvalidSettlePreimage is returned when trying to settle an HTLC, but the 183 // preimage does not correspond to the payment hash. 184 type ErrInvalidSettlePreimage struct { 185 preimage []byte 186 rhash []byte 187 } 188 189 // Error returns an error message with the offending preimage and intended 190 // payment hash. 191 func (e ErrInvalidSettlePreimage) Error() string { 192 return fmt.Sprintf("Invalid payment preimage %x for hash %x", 193 e.preimage, e.rhash) 194 } 195 196 // ErrUnknownHtlcIndex is returned when locally settling or failing an HTLC, but 197 // the HTLC index is not known to the channel. This typically indicates that the 198 // HTLC was already settled in a prior commitment. 199 type ErrUnknownHtlcIndex struct { 200 chanID lnwire.ShortChannelID 201 index uint64 202 } 203 204 // Error returns an error logging the channel and HTLC index that was unknown. 205 func (e ErrUnknownHtlcIndex) Error() string { 206 return fmt.Sprintf("No HTLC with ID %d in channel %v", 207 e.index, e.chanID) 208 } 209 210 // ErrUtxoAlreadySpent may be returned by BlockChainIO implementations when a 211 // GetUtxo call fails due to the output having been spent. 212 type ErrUtxoAlreadySpent struct { 213 PrevOutPoint wire.OutPoint 214 BlockHeight int32 215 BlockHash chainhash.Hash 216 TxIndex int32 217 SpendingOutPoint wire.OutPoint 218 } 219 220 // Error returns an error string. 221 func (err ErrUtxoAlreadySpent) Error() string { 222 return fmt.Sprintf("UTXO %s already spent by tx %s", 223 err.PrevOutPoint, err.SpendingOutPoint) 224 } 225 226 // Is is part of the error unwrapping contract. 227 func (err ErrUtxoAlreadySpent) Is(target error) bool { 228 var e ErrUtxoAlreadySpent 229 return errors.As(target, &e) 230 }