invoices.go 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. package channeldb
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "time"
  10. "github.com/lightningnetwork/lnd/channeldb/models"
  11. "github.com/lightningnetwork/lnd/htlcswitch/hop"
  12. invpkg "github.com/lightningnetwork/lnd/invoices"
  13. "github.com/lightningnetwork/lnd/kvdb"
  14. "github.com/lightningnetwork/lnd/lntypes"
  15. "github.com/lightningnetwork/lnd/lnwire"
  16. "github.com/lightningnetwork/lnd/record"
  17. "github.com/lightningnetwork/lnd/tlv"
  18. )
  19. var (
  20. // invoiceBucket is the name of the bucket within the database that
  21. // stores all data related to invoices no matter their final state.
  22. // Within the invoice bucket, each invoice is keyed by its invoice ID
  23. // which is a monotonically increasing uint32.
  24. invoiceBucket = []byte("invoices")
  25. // paymentHashIndexBucket is the name of the sub-bucket within the
  26. // invoiceBucket which indexes all invoices by their payment hash. The
  27. // payment hash is the sha256 of the invoice's payment preimage. This
  28. // index is used to detect duplicates, and also to provide a fast path
  29. // for looking up incoming HTLCs to determine if we're able to settle
  30. // them fully.
  31. //
  32. // maps: payHash => invoiceKey
  33. invoiceIndexBucket = []byte("paymenthashes")
  34. // payAddrIndexBucket is the name of the top-level bucket that maps
  35. // payment addresses to their invoice number. This can be used
  36. // to efficiently query or update non-legacy invoices. Note that legacy
  37. // invoices will not be included in this index since they all have the
  38. // same, all-zero payment address, however all newly generated invoices
  39. // will end up in this index.
  40. //
  41. // maps: payAddr => invoiceKey
  42. payAddrIndexBucket = []byte("pay-addr-index")
  43. // setIDIndexBucket is the name of the top-level bucket that maps set
  44. // ids to their invoice number. This can be used to efficiently query or
  45. // update AMP invoice. Note that legacy or MPP invoices will not be
  46. // included in this index, since their HTLCs do not have a set id.
  47. //
  48. // maps: setID => invoiceKey
  49. setIDIndexBucket = []byte("set-id-index")
  50. // numInvoicesKey is the name of key which houses the auto-incrementing
  51. // invoice ID which is essentially used as a primary key. With each
  52. // invoice inserted, the primary key is incremented by one. This key is
  53. // stored within the invoiceIndexBucket. Within the invoiceBucket
  54. // invoices are uniquely identified by the invoice ID.
  55. numInvoicesKey = []byte("nik")
  56. // addIndexBucket is an index bucket that we'll use to create a
  57. // monotonically increasing set of add indexes. Each time we add a new
  58. // invoice, this sequence number will be incremented and then populated
  59. // within the new invoice.
  60. //
  61. // In addition to this sequence number, we map:
  62. //
  63. // addIndexNo => invoiceKey
  64. addIndexBucket = []byte("invoice-add-index")
  65. // settleIndexBucket is an index bucket that we'll use to create a
  66. // monotonically increasing integer for tracking a "settle index". Each
  67. // time an invoice is settled, this sequence number will be incremented
  68. // as populate within the newly settled invoice.
  69. //
  70. // In addition to this sequence number, we map:
  71. //
  72. // settleIndexNo => invoiceKey
  73. settleIndexBucket = []byte("invoice-settle-index")
  74. )
  75. const (
  76. // A set of tlv type definitions used to serialize invoice htlcs to the
  77. // database.
  78. //
  79. // NOTE: A migration should be added whenever this list changes. This
  80. // prevents against the database being rolled back to an older
  81. // format where the surrounding logic might assume a different set of
  82. // fields are known.
  83. chanIDType tlv.Type = 1
  84. htlcIDType tlv.Type = 3
  85. amtType tlv.Type = 5
  86. acceptHeightType tlv.Type = 7
  87. acceptTimeType tlv.Type = 9
  88. resolveTimeType tlv.Type = 11
  89. expiryHeightType tlv.Type = 13
  90. htlcStateType tlv.Type = 15
  91. mppTotalAmtType tlv.Type = 17
  92. htlcAMPType tlv.Type = 19
  93. htlcHashType tlv.Type = 21
  94. htlcPreimageType tlv.Type = 23
  95. // A set of tlv type definitions used to serialize invoice bodiees.
  96. //
  97. // NOTE: A migration should be added whenever this list changes. This
  98. // prevents against the database being rolled back to an older
  99. // format where the surrounding logic might assume a different set of
  100. // fields are known.
  101. memoType tlv.Type = 0
  102. payReqType tlv.Type = 1
  103. createTimeType tlv.Type = 2
  104. settleTimeType tlv.Type = 3
  105. addIndexType tlv.Type = 4
  106. settleIndexType tlv.Type = 5
  107. preimageType tlv.Type = 6
  108. valueType tlv.Type = 7
  109. cltvDeltaType tlv.Type = 8
  110. expiryType tlv.Type = 9
  111. paymentAddrType tlv.Type = 10
  112. featuresType tlv.Type = 11
  113. invStateType tlv.Type = 12
  114. amtPaidType tlv.Type = 13
  115. hodlInvoiceType tlv.Type = 14
  116. invoiceAmpStateType tlv.Type = 15
  117. // A set of tlv type definitions used to serialize the invoice AMP
  118. // state along-side the main invoice body.
  119. ampStateSetIDType tlv.Type = 0
  120. ampStateHtlcStateType tlv.Type = 1
  121. ampStateSettleIndexType tlv.Type = 2
  122. ampStateSettleDateType tlv.Type = 3
  123. ampStateCircuitKeysType tlv.Type = 4
  124. ampStateAmtPaidType tlv.Type = 5
  125. )
  126. // AddInvoice inserts the targeted invoice into the database. If the invoice has
  127. // *any* payment hashes which already exists within the database, then the
  128. // insertion will be aborted and rejected due to the strict policy banning any
  129. // duplicate payment hashes. A side effect of this function is that it sets
  130. // AddIndex on newInvoice.
  131. func (d *DB) AddInvoice(_ context.Context, newInvoice *invpkg.Invoice,
  132. paymentHash lntypes.Hash) (uint64, error) {
  133. if err := invpkg.ValidateInvoice(newInvoice, paymentHash); err != nil {
  134. return 0, err
  135. }
  136. var invoiceAddIndex uint64
  137. err := kvdb.Update(d, func(tx kvdb.RwTx) error {
  138. invoices, err := tx.CreateTopLevelBucket(invoiceBucket)
  139. if err != nil {
  140. return err
  141. }
  142. invoiceIndex, err := invoices.CreateBucketIfNotExists(
  143. invoiceIndexBucket,
  144. )
  145. if err != nil {
  146. return err
  147. }
  148. addIndex, err := invoices.CreateBucketIfNotExists(
  149. addIndexBucket,
  150. )
  151. if err != nil {
  152. return err
  153. }
  154. // Ensure that an invoice an identical payment hash doesn't
  155. // already exist within the index.
  156. if invoiceIndex.Get(paymentHash[:]) != nil {
  157. return invpkg.ErrDuplicateInvoice
  158. }
  159. // Check that we aren't inserting an invoice with a duplicate
  160. // payment address. The all-zeros payment address is
  161. // special-cased to support legacy keysend invoices which don't
  162. // assign one. This is safe since later we also will avoid
  163. // indexing them and avoid collisions.
  164. payAddrIndex := tx.ReadWriteBucket(payAddrIndexBucket)
  165. if newInvoice.Terms.PaymentAddr != invpkg.BlankPayAddr {
  166. paymentAddr := newInvoice.Terms.PaymentAddr[:]
  167. if payAddrIndex.Get(paymentAddr) != nil {
  168. return invpkg.ErrDuplicatePayAddr
  169. }
  170. }
  171. // If the current running payment ID counter hasn't yet been
  172. // created, then create it now.
  173. var invoiceNum uint32
  174. invoiceCounter := invoiceIndex.Get(numInvoicesKey)
  175. if invoiceCounter == nil {
  176. var scratch [4]byte
  177. byteOrder.PutUint32(scratch[:], invoiceNum)
  178. err := invoiceIndex.Put(numInvoicesKey, scratch[:])
  179. if err != nil {
  180. return err
  181. }
  182. } else {
  183. invoiceNum = byteOrder.Uint32(invoiceCounter)
  184. }
  185. newIndex, err := putInvoice(
  186. invoices, invoiceIndex, payAddrIndex, addIndex,
  187. newInvoice, invoiceNum, paymentHash,
  188. )
  189. if err != nil {
  190. return err
  191. }
  192. invoiceAddIndex = newIndex
  193. return nil
  194. }, func() {
  195. invoiceAddIndex = 0
  196. })
  197. if err != nil {
  198. return 0, err
  199. }
  200. return invoiceAddIndex, err
  201. }
  202. // InvoicesAddedSince can be used by callers to seek into the event time series
  203. // of all the invoices added in the database. The specified sinceAddIndex
  204. // should be the highest add index that the caller knows of. This method will
  205. // return all invoices with an add index greater than the specified
  206. // sinceAddIndex.
  207. //
  208. // NOTE: The index starts from 1, as a result. We enforce that specifying a
  209. // value below the starting index value is a noop.
  210. func (d *DB) InvoicesAddedSince(_ context.Context, sinceAddIndex uint64) (
  211. []invpkg.Invoice, error) {
  212. var newInvoices []invpkg.Invoice
  213. // If an index of zero was specified, then in order to maintain
  214. // backwards compat, we won't send out any new invoices.
  215. if sinceAddIndex == 0 {
  216. return newInvoices, nil
  217. }
  218. var startIndex [8]byte
  219. byteOrder.PutUint64(startIndex[:], sinceAddIndex)
  220. err := kvdb.View(d, func(tx kvdb.RTx) error {
  221. invoices := tx.ReadBucket(invoiceBucket)
  222. if invoices == nil {
  223. return nil
  224. }
  225. addIndex := invoices.NestedReadBucket(addIndexBucket)
  226. if addIndex == nil {
  227. return nil
  228. }
  229. // We'll now run through each entry in the add index starting
  230. // at our starting index. We'll continue until we reach the
  231. // very end of the current key space.
  232. invoiceCursor := addIndex.ReadCursor()
  233. // We'll seek to the starting index, then manually advance the
  234. // cursor in order to skip the entry with the since add index.
  235. invoiceCursor.Seek(startIndex[:])
  236. addSeqNo, invoiceKey := invoiceCursor.Next()
  237. for ; addSeqNo != nil && bytes.Compare(addSeqNo, startIndex[:]) > 0; addSeqNo, invoiceKey = invoiceCursor.Next() {
  238. // For each key found, we'll look up the actual
  239. // invoice, then accumulate it into our return value.
  240. invoice, err := fetchInvoice(invoiceKey, invoices)
  241. if err != nil {
  242. return err
  243. }
  244. newInvoices = append(newInvoices, invoice)
  245. }
  246. return nil
  247. }, func() {
  248. newInvoices = nil
  249. })
  250. if err != nil {
  251. return nil, err
  252. }
  253. return newInvoices, nil
  254. }
  255. // LookupInvoice attempts to look up an invoice according to its 32 byte
  256. // payment hash. If an invoice which can settle the HTLC identified by the
  257. // passed payment hash isn't found, then an error is returned. Otherwise, the
  258. // full invoice is returned. Before setting the incoming HTLC, the values
  259. // SHOULD be checked to ensure the payer meets the agreed upon contractual
  260. // terms of the payment.
  261. func (d *DB) LookupInvoice(_ context.Context, ref invpkg.InvoiceRef) (
  262. invpkg.Invoice, error) {
  263. var invoice invpkg.Invoice
  264. err := kvdb.View(d, func(tx kvdb.RTx) error {
  265. invoices := tx.ReadBucket(invoiceBucket)
  266. if invoices == nil {
  267. return invpkg.ErrNoInvoicesCreated
  268. }
  269. invoiceIndex := invoices.NestedReadBucket(invoiceIndexBucket)
  270. if invoiceIndex == nil {
  271. return invpkg.ErrNoInvoicesCreated
  272. }
  273. payAddrIndex := tx.ReadBucket(payAddrIndexBucket)
  274. setIDIndex := tx.ReadBucket(setIDIndexBucket)
  275. // Retrieve the invoice number for this invoice using
  276. // the provided invoice reference.
  277. invoiceNum, err := fetchInvoiceNumByRef(
  278. invoiceIndex, payAddrIndex, setIDIndex, ref,
  279. )
  280. if err != nil {
  281. return err
  282. }
  283. var setID *invpkg.SetID
  284. switch {
  285. // If this is a payment address ref, and the blank modified was
  286. // specified, then we'll use the zero set ID to indicate that
  287. // we won't want any HTLCs returned.
  288. case ref.PayAddr() != nil &&
  289. ref.Modifier() == invpkg.HtlcSetBlankModifier:
  290. var zeroSetID invpkg.SetID
  291. setID = &zeroSetID
  292. // If this is a set ID ref, and the htlc set only modified was
  293. // specified, then we'll pass through the specified setID so
  294. // only that will be returned.
  295. case ref.SetID() != nil &&
  296. ref.Modifier() == invpkg.HtlcSetOnlyModifier:
  297. setID = (*invpkg.SetID)(ref.SetID())
  298. }
  299. // An invoice was found, retrieve the remainder of the invoice
  300. // body.
  301. i, err := fetchInvoice(invoiceNum, invoices, setID)
  302. if err != nil {
  303. return err
  304. }
  305. invoice = i
  306. return nil
  307. }, func() {})
  308. if err != nil {
  309. return invoice, err
  310. }
  311. return invoice, nil
  312. }
  313. // fetchInvoiceNumByRef retrieve the invoice number for the provided invoice
  314. // reference. The payment address will be treated as the primary key, falling
  315. // back to the payment hash if nothing is found for the payment address. An
  316. // error is returned if the invoice is not found.
  317. func fetchInvoiceNumByRef(invoiceIndex, payAddrIndex, setIDIndex kvdb.RBucket,
  318. ref invpkg.InvoiceRef) ([]byte, error) {
  319. // If the set id is present, we only consult the set id index for this
  320. // invoice. This type of query is only used to facilitate user-facing
  321. // requests to lookup, settle or cancel an AMP invoice.
  322. setID := ref.SetID()
  323. if setID != nil {
  324. invoiceNumBySetID := setIDIndex.Get(setID[:])
  325. if invoiceNumBySetID == nil {
  326. return nil, invpkg.ErrInvoiceNotFound
  327. }
  328. return invoiceNumBySetID, nil
  329. }
  330. payHash := ref.PayHash()
  331. payAddr := ref.PayAddr()
  332. getInvoiceNumByHash := func() []byte {
  333. if payHash != nil {
  334. return invoiceIndex.Get(payHash[:])
  335. }
  336. return nil
  337. }
  338. getInvoiceNumByAddr := func() []byte {
  339. if payAddr != nil {
  340. // Only allow lookups for payment address if it is not a
  341. // blank payment address, which is a special-cased value
  342. // for legacy keysend invoices.
  343. if *payAddr != invpkg.BlankPayAddr {
  344. return payAddrIndex.Get(payAddr[:])
  345. }
  346. }
  347. return nil
  348. }
  349. invoiceNumByHash := getInvoiceNumByHash()
  350. invoiceNumByAddr := getInvoiceNumByAddr()
  351. switch {
  352. // If payment address and payment hash both reference an existing
  353. // invoice, ensure they reference the _same_ invoice.
  354. case invoiceNumByAddr != nil && invoiceNumByHash != nil:
  355. if !bytes.Equal(invoiceNumByAddr, invoiceNumByHash) {
  356. return nil, invpkg.ErrInvRefEquivocation
  357. }
  358. return invoiceNumByAddr, nil
  359. // Return invoices by payment addr only.
  360. //
  361. // NOTE: We constrain this lookup to only apply if the invoice ref does
  362. // not contain a payment hash. Legacy and MPP payments depend on the
  363. // payment hash index to enforce that the HTLCs payment hash matches the
  364. // payment hash for the invoice, without this check we would
  365. // inadvertently assume the invoice contains the correct preimage for
  366. // the HTLC, which we only enforce via the lookup by the invoice index.
  367. case invoiceNumByAddr != nil && payHash == nil:
  368. return invoiceNumByAddr, nil
  369. // If we were only able to reference the invoice by hash, return the
  370. // corresponding invoice number. This can happen when no payment address
  371. // was provided, or if it didn't match anything in our records.
  372. case invoiceNumByHash != nil:
  373. return invoiceNumByHash, nil
  374. // Otherwise we don't know of the target invoice.
  375. default:
  376. return nil, invpkg.ErrInvoiceNotFound
  377. }
  378. }
  379. // FetchPendingInvoices returns all invoices that have not yet been settled or
  380. // canceled. The returned map is keyed by the payment hash of each respective
  381. // invoice.
  382. func (d *DB) FetchPendingInvoices(_ context.Context) (
  383. map[lntypes.Hash]invpkg.Invoice, error) {
  384. result := make(map[lntypes.Hash]invpkg.Invoice)
  385. err := kvdb.View(d, func(tx kvdb.RTx) error {
  386. invoices := tx.ReadBucket(invoiceBucket)
  387. if invoices == nil {
  388. return nil
  389. }
  390. invoiceIndex := invoices.NestedReadBucket(invoiceIndexBucket)
  391. if invoiceIndex == nil {
  392. // Mask the error if there's no invoice
  393. // index as that simply means there are no
  394. // invoices added yet to the DB. In this case
  395. // we simply return an empty list.
  396. return nil
  397. }
  398. return invoiceIndex.ForEach(func(k, v []byte) error {
  399. // Skip the special numInvoicesKey as that does not
  400. // point to a valid invoice.
  401. if bytes.Equal(k, numInvoicesKey) {
  402. return nil
  403. }
  404. // Skip sub-buckets.
  405. if v == nil {
  406. return nil
  407. }
  408. invoice, err := fetchInvoice(v, invoices)
  409. if err != nil {
  410. return err
  411. }
  412. if invoice.IsPending() {
  413. var paymentHash lntypes.Hash
  414. copy(paymentHash[:], k)
  415. result[paymentHash] = invoice
  416. }
  417. return nil
  418. })
  419. }, func() {
  420. result = make(map[lntypes.Hash]invpkg.Invoice)
  421. })
  422. if err != nil {
  423. return nil, err
  424. }
  425. return result, nil
  426. }
  427. // QueryInvoices allows a caller to query the invoice database for invoices
  428. // within the specified add index range.
  429. func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
  430. invpkg.InvoiceSlice, error) {
  431. var resp invpkg.InvoiceSlice
  432. err := kvdb.View(d, func(tx kvdb.RTx) error {
  433. // If the bucket wasn't found, then there aren't any invoices
  434. // within the database yet, so we can simply exit.
  435. invoices := tx.ReadBucket(invoiceBucket)
  436. if invoices == nil {
  437. return invpkg.ErrNoInvoicesCreated
  438. }
  439. // Get the add index bucket which we will use to iterate through
  440. // our indexed invoices.
  441. invoiceAddIndex := invoices.NestedReadBucket(addIndexBucket)
  442. if invoiceAddIndex == nil {
  443. return invpkg.ErrNoInvoicesCreated
  444. }
  445. // Create a paginator which reads from our add index bucket with
  446. // the parameters provided by the invoice query.
  447. paginator := newPaginator(
  448. invoiceAddIndex.ReadCursor(), q.Reversed, q.IndexOffset,
  449. q.NumMaxInvoices,
  450. )
  451. // accumulateInvoices looks up an invoice based on the index we
  452. // are given, adds it to our set of invoices if it has the right
  453. // characteristics for our query and returns the number of items
  454. // we have added to our set of invoices.
  455. accumulateInvoices := func(_, indexValue []byte) (bool, error) {
  456. invoice, err := fetchInvoice(indexValue, invoices)
  457. if err != nil {
  458. return false, err
  459. }
  460. // Skip any settled or canceled invoices if the caller
  461. // is only interested in pending ones.
  462. if q.PendingOnly && !invoice.IsPending() {
  463. return false, nil
  464. }
  465. // Get the creation time in Unix seconds, this always
  466. // rounds down the nanoseconds to full seconds.
  467. createTime := invoice.CreationDate.Unix()
  468. // Skip any invoices that were created before the
  469. // specified time.
  470. if createTime < q.CreationDateStart {
  471. return false, nil
  472. }
  473. // Skip any invoices that were created after the
  474. // specified time.
  475. if q.CreationDateEnd != 0 &&
  476. createTime > q.CreationDateEnd {
  477. return false, nil
  478. }
  479. // At this point, we've exhausted the offset, so we'll
  480. // begin collecting invoices found within the range.
  481. resp.Invoices = append(resp.Invoices, invoice)
  482. return true, nil
  483. }
  484. // Query our paginator using accumulateInvoices to build up a
  485. // set of invoices.
  486. if err := paginator.query(accumulateInvoices); err != nil {
  487. return err
  488. }
  489. // If we iterated through the add index in reverse order, then
  490. // we'll need to reverse the slice of invoices to return them in
  491. // forward order.
  492. if q.Reversed {
  493. numInvoices := len(resp.Invoices)
  494. for i := 0; i < numInvoices/2; i++ {
  495. reverse := numInvoices - i - 1
  496. resp.Invoices[i], resp.Invoices[reverse] =
  497. resp.Invoices[reverse], resp.Invoices[i]
  498. }
  499. }
  500. return nil
  501. }, func() {
  502. resp = invpkg.InvoiceSlice{
  503. InvoiceQuery: q,
  504. }
  505. })
  506. if err != nil && !errors.Is(err, invpkg.ErrNoInvoicesCreated) {
  507. return resp, err
  508. }
  509. // Finally, record the indexes of the first and last invoices returned
  510. // so that the caller can resume from this point later on.
  511. if len(resp.Invoices) > 0 {
  512. resp.FirstIndexOffset = resp.Invoices[0].AddIndex
  513. lastIdx := len(resp.Invoices) - 1
  514. resp.LastIndexOffset = resp.Invoices[lastIdx].AddIndex
  515. }
  516. return resp, nil
  517. }
  518. // UpdateInvoice attempts to update an invoice corresponding to the passed
  519. // payment hash. If an invoice matching the passed payment hash doesn't exist
  520. // within the database, then the action will fail with a "not found" error.
  521. //
  522. // The update is performed inside the same database transaction that fetches the
  523. // invoice and is therefore atomic. The fields to update are controlled by the
  524. // supplied callback. When updating an invoice, the update itself happens
  525. // in-memory on a copy of the invoice. Once it is written successfully to the
  526. // database, the in-memory copy is returned to the caller.
  527. func (d *DB) UpdateInvoice(_ context.Context, ref invpkg.InvoiceRef,
  528. setIDHint *invpkg.SetID, callback invpkg.InvoiceUpdateCallback) (
  529. *invpkg.Invoice, error) {
  530. var updatedInvoice *invpkg.Invoice
  531. err := kvdb.Update(d, func(tx kvdb.RwTx) error {
  532. invoices, err := tx.CreateTopLevelBucket(invoiceBucket)
  533. if err != nil {
  534. return err
  535. }
  536. invoiceIndex, err := invoices.CreateBucketIfNotExists(
  537. invoiceIndexBucket,
  538. )
  539. if err != nil {
  540. return err
  541. }
  542. settleIndex, err := invoices.CreateBucketIfNotExists(
  543. settleIndexBucket,
  544. )
  545. if err != nil {
  546. return err
  547. }
  548. payAddrIndex := tx.ReadBucket(payAddrIndexBucket)
  549. setIDIndex := tx.ReadWriteBucket(setIDIndexBucket)
  550. // Retrieve the invoice number for this invoice using the
  551. // provided invoice reference.
  552. invoiceNum, err := fetchInvoiceNumByRef(
  553. invoiceIndex, payAddrIndex, setIDIndex, ref,
  554. )
  555. if err != nil {
  556. return err
  557. }
  558. // If the set ID hint is non-nil, then we'll use that to filter
  559. // out the HTLCs for AMP invoice so we don't need to read them
  560. // all out to satisfy the invoice callback below. If it's nil,
  561. // then we pass in the zero set ID which means no HTLCs will be
  562. // read out.
  563. var invSetID invpkg.SetID
  564. if setIDHint != nil {
  565. invSetID = *setIDHint
  566. }
  567. invoice, err := fetchInvoice(invoiceNum, invoices, &invSetID)
  568. if err != nil {
  569. return err
  570. }
  571. now := d.clock.Now()
  572. updater := &kvInvoiceUpdater{
  573. db: d,
  574. invoicesBucket: invoices,
  575. settleIndexBucket: settleIndex,
  576. setIDIndexBucket: setIDIndex,
  577. updateTime: now,
  578. invoiceNum: invoiceNum,
  579. invoice: &invoice,
  580. updatedAmpHtlcs: make(ampHTLCsMap),
  581. settledSetIDs: make(map[invpkg.SetID]struct{}),
  582. }
  583. payHash := ref.PayHash()
  584. updatedInvoice, err = invpkg.UpdateInvoice(
  585. payHash, updater.invoice, now, callback, updater,
  586. )
  587. return err
  588. }, func() {
  589. updatedInvoice = nil
  590. })
  591. return updatedInvoice, err
  592. }
  593. // ampHTLCsMap is a map of AMP HTLCs affected by an invoice update.
  594. type ampHTLCsMap map[invpkg.SetID]map[models.CircuitKey]*invpkg.InvoiceHTLC
  595. // kvInvoiceUpdater is an implementation of the InvoiceUpdater interface that
  596. // is used with the kv implementation of the invoice database. Note that this
  597. // updater is not concurrency safe and synchronizaton is expected to be handled
  598. // on the DB level.
  599. type kvInvoiceUpdater struct {
  600. db *DB
  601. invoicesBucket kvdb.RwBucket
  602. settleIndexBucket kvdb.RwBucket
  603. setIDIndexBucket kvdb.RwBucket
  604. // updateTime is the timestamp for the update.
  605. updateTime time.Time
  606. // invoiceNum is a legacy key similar to the add index that is used
  607. // only in the kv implementation.
  608. invoiceNum []byte
  609. // invoice is the invoice that we're updating. As a side effect of the
  610. // update this invoice will be mutated.
  611. invoice *invpkg.Invoice
  612. // updatedAmpHtlcs holds the set of AMP HTLCs that were added or
  613. // cancelled as part of this update.
  614. updatedAmpHtlcs ampHTLCsMap
  615. // settledSetIDs holds the set IDs that are settled with this update.
  616. settledSetIDs map[invpkg.SetID]struct{}
  617. }
  618. // NOTE: this method does nothing in the k/v implementation of InvoiceUpdater.
  619. func (k *kvInvoiceUpdater) AddHtlc(_ models.CircuitKey,
  620. _ *invpkg.InvoiceHTLC) error {
  621. return nil
  622. }
  623. // NOTE: this method does nothing in the k/v implementation of InvoiceUpdater.
  624. func (k *kvInvoiceUpdater) ResolveHtlc(_ models.CircuitKey, _ invpkg.HtlcState,
  625. _ time.Time) error {
  626. return nil
  627. }
  628. // NOTE: this method does nothing in the k/v implementation of InvoiceUpdater.
  629. func (k *kvInvoiceUpdater) AddAmpHtlcPreimage(_ [32]byte, _ models.CircuitKey,
  630. _ lntypes.Preimage) error {
  631. return nil
  632. }
  633. // NOTE: this method does nothing in the k/v implementation of InvoiceUpdater.
  634. func (k *kvInvoiceUpdater) UpdateInvoiceState(_ invpkg.ContractState,
  635. _ *lntypes.Preimage) error {
  636. return nil
  637. }
  638. // NOTE: this method does nothing in the k/v implementation of InvoiceUpdater.
  639. func (k *kvInvoiceUpdater) UpdateInvoiceAmtPaid(_ lnwire.MilliSatoshi) error {
  640. return nil
  641. }
  642. // UpdateAmpState updates the state of the AMP invoice identified by the setID.
  643. func (k *kvInvoiceUpdater) UpdateAmpState(setID [32]byte,
  644. state invpkg.InvoiceStateAMP, circuitKey models.CircuitKey) error {
  645. if _, ok := k.updatedAmpHtlcs[setID]; !ok {
  646. switch state.State {
  647. case invpkg.HtlcStateAccepted:
  648. // If we're just now creating the HTLCs for this set
  649. // then we'll also pull in the existing HTLCs that are
  650. // part of this set, so we can write them all to disk
  651. // together (same value)
  652. k.updatedAmpHtlcs[setID] = k.invoice.HTLCSet(
  653. &setID, invpkg.HtlcStateAccepted,
  654. )
  655. case invpkg.HtlcStateCanceled:
  656. // Only HTLCs in the accepted state, can be cancelled,
  657. // but we also want to merge that with HTLCs that may be
  658. // canceled as well since it can be cancelled one by
  659. // one.
  660. k.updatedAmpHtlcs[setID] = k.invoice.HTLCSet(
  661. &setID, invpkg.HtlcStateAccepted,
  662. )
  663. cancelledHtlcs := k.invoice.HTLCSet(
  664. &setID, invpkg.HtlcStateCanceled,
  665. )
  666. for htlcKey, htlc := range cancelledHtlcs {
  667. k.updatedAmpHtlcs[setID][htlcKey] = htlc
  668. }
  669. case invpkg.HtlcStateSettled:
  670. k.updatedAmpHtlcs[setID] = make(
  671. map[models.CircuitKey]*invpkg.InvoiceHTLC,
  672. )
  673. }
  674. }
  675. if state.State == invpkg.HtlcStateSettled {
  676. // Add the set ID to the set that was settled in this invoice
  677. // update. We'll use this later to update the settle index.
  678. k.settledSetIDs[setID] = struct{}{}
  679. }
  680. k.updatedAmpHtlcs[setID][circuitKey] = k.invoice.Htlcs[circuitKey]
  681. return nil
  682. }
  683. // Finalize finalizes the update before it is written to the database.
  684. func (k *kvInvoiceUpdater) Finalize(updateType invpkg.UpdateType) error {
  685. switch updateType {
  686. case invpkg.AddHTLCsUpdate:
  687. return k.storeAddHtlcsUpdate()
  688. case invpkg.CancelHTLCsUpdate:
  689. return k.storeCancelHtlcsUpdate()
  690. case invpkg.SettleHodlInvoiceUpdate:
  691. return k.storeSettleHodlInvoiceUpdate()
  692. case invpkg.CancelInvoiceUpdate:
  693. return k.serializeAndStoreInvoice()
  694. }
  695. return fmt.Errorf("unknown update type: %v", updateType)
  696. }
  697. // storeCancelHtlcsUpdate updates the invoice in the database after cancelling a
  698. // set of HTLCs.
  699. func (k *kvInvoiceUpdater) storeCancelHtlcsUpdate() error {
  700. err := k.serializeAndStoreInvoice()
  701. if err != nil {
  702. return err
  703. }
  704. // If this is an AMP invoice, then we'll actually store the rest
  705. // of the HTLCs in-line with the invoice, using the invoice ID
  706. // as a prefix, and the AMP key as a suffix: invoiceNum ||
  707. // setID.
  708. if k.invoice.IsAMP() {
  709. return k.updateAMPInvoices()
  710. }
  711. return nil
  712. }
  713. // storeAddHtlcsUpdate updates the invoice in the database after adding a set of
  714. // HTLCs.
  715. func (k *kvInvoiceUpdater) storeAddHtlcsUpdate() error {
  716. invoiceIsAMP := k.invoice.IsAMP()
  717. for htlcSetID := range k.updatedAmpHtlcs {
  718. // Check if this SetID already exist.
  719. setIDInvNum := k.setIDIndexBucket.Get(htlcSetID[:])
  720. if setIDInvNum == nil {
  721. err := k.setIDIndexBucket.Put(
  722. htlcSetID[:], k.invoiceNum,
  723. )
  724. if err != nil {
  725. return err
  726. }
  727. } else if !bytes.Equal(setIDInvNum, k.invoiceNum) {
  728. return invpkg.ErrDuplicateSetID{
  729. SetID: htlcSetID,
  730. }
  731. }
  732. }
  733. // If this is a non-AMP invoice, then the state can eventually go to
  734. // ContractSettled, so we pass in nil value as part of
  735. // setSettleMetaFields.
  736. if !invoiceIsAMP && k.invoice.State == invpkg.ContractSettled {
  737. err := k.setSettleMetaFields(nil)
  738. if err != nil {
  739. return err
  740. }
  741. }
  742. // As we don't update the settle index above for AMP invoices, we'll do
  743. // it here for each sub-AMP invoice that was settled.
  744. for settledSetID := range k.settledSetIDs {
  745. settledSetID := settledSetID
  746. err := k.setSettleMetaFields(&settledSetID)
  747. if err != nil {
  748. return err
  749. }
  750. }
  751. err := k.serializeAndStoreInvoice()
  752. if err != nil {
  753. return err
  754. }
  755. // If this is an AMP invoice, then we'll actually store the rest of the
  756. // HTLCs in-line with the invoice, using the invoice ID as a prefix,
  757. // and the AMP key as a suffix: invoiceNum || setID.
  758. if invoiceIsAMP {
  759. return k.updateAMPInvoices()
  760. }
  761. return nil
  762. }
  763. // storeSettleHodlInvoiceUpdate updates the invoice in the database after
  764. // settling a hodl invoice.
  765. func (k *kvInvoiceUpdater) storeSettleHodlInvoiceUpdate() error {
  766. err := k.setSettleMetaFields(nil)
  767. if err != nil {
  768. return err
  769. }
  770. return k.serializeAndStoreInvoice()
  771. }
  772. // setSettleMetaFields updates the metadata associated with settlement of an
  773. // invoice. If a non-nil setID is passed in, then the value will be append to
  774. // the invoice number as well, in order to allow us to detect repeated payments
  775. // to the same AMP invoices "across time".
  776. func (k *kvInvoiceUpdater) setSettleMetaFields(setID *invpkg.SetID) error {
  777. // Now that we know the invoice hasn't already been settled, we'll
  778. // update the settle index so we can place this settle event in the
  779. // proper location within our time series.
  780. nextSettleSeqNo, err := k.settleIndexBucket.NextSequence()
  781. if err != nil {
  782. return err
  783. }
  784. // Make a new byte array on the stack that can potentially store the 4
  785. // byte invoice number along w/ the 32 byte set ID. We capture valueLen
  786. // here which is the number of bytes copied so we can only store the 4
  787. // bytes if this is a non-AMP invoice.
  788. var indexKey [invoiceSetIDKeyLen]byte
  789. valueLen := copy(indexKey[:], k.invoiceNum)
  790. if setID != nil {
  791. valueLen += copy(indexKey[valueLen:], setID[:])
  792. }
  793. var seqNoBytes [8]byte
  794. byteOrder.PutUint64(seqNoBytes[:], nextSettleSeqNo)
  795. err = k.settleIndexBucket.Put(seqNoBytes[:], indexKey[:valueLen])
  796. if err != nil {
  797. return err
  798. }
  799. // If the setID is nil, then this means that this is a non-AMP settle,
  800. // so we'll update the invoice settle index directly.
  801. if setID == nil {
  802. k.invoice.SettleDate = k.updateTime
  803. k.invoice.SettleIndex = nextSettleSeqNo
  804. } else {
  805. // If the set ID isn't blank, we'll update the AMP state map
  806. // which tracks when each of the setIDs associated with a given
  807. // AMP invoice are settled.
  808. ampState := k.invoice.AMPState[*setID]
  809. ampState.SettleDate = k.updateTime
  810. ampState.SettleIndex = nextSettleSeqNo
  811. k.invoice.AMPState[*setID] = ampState
  812. }
  813. return nil
  814. }
  815. // updateAMPInvoices updates the set of AMP invoices in-place. For AMP, rather
  816. // then continually write the invoices to the end of the invoice value, we
  817. // instead write the invoices into a new key preifx that follows the main
  818. // invoice number. This ensures that we don't need to continually decode a
  819. // potentially massive HTLC set, and also allows us to quickly find the HLTCs
  820. // associated with a particular HTLC set.
  821. func (k *kvInvoiceUpdater) updateAMPInvoices() error {
  822. for setID, htlcSet := range k.updatedAmpHtlcs {
  823. // First write out the set of HTLCs including all the relevant
  824. // TLV values.
  825. var b bytes.Buffer
  826. if err := serializeHtlcs(&b, htlcSet); err != nil {
  827. return err
  828. }
  829. // Next store each HTLC in-line, using a prefix based off the
  830. // invoice number.
  831. invoiceSetIDKey := makeInvoiceSetIDKey(k.invoiceNum, setID[:])
  832. err := k.invoicesBucket.Put(invoiceSetIDKey[:], b.Bytes())
  833. if err != nil {
  834. return err
  835. }
  836. }
  837. return nil
  838. }
  839. // serializeAndStoreInvoice is a helper function used to store invoices.
  840. func (k *kvInvoiceUpdater) serializeAndStoreInvoice() error {
  841. var buf bytes.Buffer
  842. if err := serializeInvoice(&buf, k.invoice); err != nil {
  843. return err
  844. }
  845. return k.invoicesBucket.Put(k.invoiceNum, buf.Bytes())
  846. }
  847. // InvoicesSettledSince can be used by callers to catch up any settled invoices
  848. // they missed within the settled invoice time series. We'll return all known
  849. // settled invoice that have a settle index higher than the passed
  850. // sinceSettleIndex.
  851. //
  852. // NOTE: The index starts from 1, as a result. We enforce that specifying a
  853. // value below the starting index value is a noop.
  854. func (d *DB) InvoicesSettledSince(_ context.Context, sinceSettleIndex uint64) (
  855. []invpkg.Invoice, error) {
  856. var settledInvoices []invpkg.Invoice
  857. // If an index of zero was specified, then in order to maintain
  858. // backwards compat, we won't send out any new invoices.
  859. if sinceSettleIndex == 0 {
  860. return settledInvoices, nil
  861. }
  862. var startIndex [8]byte
  863. byteOrder.PutUint64(startIndex[:], sinceSettleIndex)
  864. err := kvdb.View(d, func(tx kvdb.RTx) error {
  865. invoices := tx.ReadBucket(invoiceBucket)
  866. if invoices == nil {
  867. return nil
  868. }
  869. settleIndex := invoices.NestedReadBucket(settleIndexBucket)
  870. if settleIndex == nil {
  871. return nil
  872. }
  873. // We'll now run through each entry in the add index starting
  874. // at our starting index. We'll continue until we reach the
  875. // very end of the current key space.
  876. invoiceCursor := settleIndex.ReadCursor()
  877. // We'll seek to the starting index, then manually advance the
  878. // cursor in order to skip the entry with the since add index.
  879. invoiceCursor.Seek(startIndex[:])
  880. seqNo, indexValue := invoiceCursor.Next()
  881. for ; seqNo != nil && bytes.Compare(seqNo, startIndex[:]) > 0; seqNo, indexValue = invoiceCursor.Next() {
  882. // Depending on the length of the index value, this may
  883. // or may not be an AMP invoice, so we'll extract the
  884. // invoice value into two components: the invoice num,
  885. // and the setID (may not be there).
  886. var (
  887. invoiceKey [4]byte
  888. setID *invpkg.SetID
  889. )
  890. valueLen := copy(invoiceKey[:], indexValue)
  891. if len(indexValue) == invoiceSetIDKeyLen {
  892. setID = new(invpkg.SetID)
  893. copy(setID[:], indexValue[valueLen:])
  894. }
  895. // For each key found, we'll look up the actual
  896. // invoice, then accumulate it into our return value.
  897. invoice, err := fetchInvoice(
  898. invoiceKey[:], invoices, setID,
  899. )
  900. if err != nil {
  901. return err
  902. }
  903. settledInvoices = append(settledInvoices, invoice)
  904. }
  905. return nil
  906. }, func() {
  907. settledInvoices = nil
  908. })
  909. if err != nil {
  910. return nil, err
  911. }
  912. return settledInvoices, nil
  913. }
  914. func putInvoice(invoices, invoiceIndex, payAddrIndex, addIndex kvdb.RwBucket,
  915. i *invpkg.Invoice, invoiceNum uint32, paymentHash lntypes.Hash) (
  916. uint64, error) {
  917. // Create the invoice key which is just the big-endian representation
  918. // of the invoice number.
  919. var invoiceKey [4]byte
  920. byteOrder.PutUint32(invoiceKey[:], invoiceNum)
  921. // Increment the num invoice counter index so the next invoice bares
  922. // the proper ID.
  923. var scratch [4]byte
  924. invoiceCounter := invoiceNum + 1
  925. byteOrder.PutUint32(scratch[:], invoiceCounter)
  926. if err := invoiceIndex.Put(numInvoicesKey, scratch[:]); err != nil {
  927. return 0, err
  928. }
  929. // Add the payment hash to the invoice index. This will let us quickly
  930. // identify if we can settle an incoming payment, and also to possibly
  931. // allow a single invoice to have multiple payment installations.
  932. err := invoiceIndex.Put(paymentHash[:], invoiceKey[:])
  933. if err != nil {
  934. return 0, err
  935. }
  936. // Add the invoice to the payment address index, but only if the invoice
  937. // has a non-zero payment address. The all-zero payment address is still
  938. // in use by legacy keysend, so we special-case here to avoid
  939. // collisions.
  940. if i.Terms.PaymentAddr != invpkg.BlankPayAddr {
  941. err = payAddrIndex.Put(i.Terms.PaymentAddr[:], invoiceKey[:])
  942. if err != nil {
  943. return 0, err
  944. }
  945. }
  946. // Next, we'll obtain the next add invoice index (sequence
  947. // number), so we can properly place this invoice within this
  948. // event stream.
  949. nextAddSeqNo, err := addIndex.NextSequence()
  950. if err != nil {
  951. return 0, err
  952. }
  953. // With the next sequence obtained, we'll updating the event series in
  954. // the add index bucket to map this current add counter to the index of
  955. // this new invoice.
  956. var seqNoBytes [8]byte
  957. byteOrder.PutUint64(seqNoBytes[:], nextAddSeqNo)
  958. if err := addIndex.Put(seqNoBytes[:], invoiceKey[:]); err != nil {
  959. return 0, err
  960. }
  961. i.AddIndex = nextAddSeqNo
  962. // Finally, serialize the invoice itself to be written to the disk.
  963. var buf bytes.Buffer
  964. if err := serializeInvoice(&buf, i); err != nil {
  965. return 0, err
  966. }
  967. if err := invoices.Put(invoiceKey[:], buf.Bytes()); err != nil {
  968. return 0, err
  969. }
  970. return nextAddSeqNo, nil
  971. }
  972. // recordSize returns the amount of bytes this TLV record will occupy when
  973. // encoded.
  974. func ampRecordSize(a *invpkg.AMPInvoiceState) func() uint64 {
  975. var (
  976. b bytes.Buffer
  977. buf [8]byte
  978. )
  979. // We know that encoding works since the tests pass in the build this
  980. // file is checked into, so we'll simplify things and simply encode it
  981. // ourselves then report the total amount of bytes used.
  982. if err := ampStateEncoder(&b, a, &buf); err != nil {
  983. // This should never error out, but we log it just in case it
  984. // does.
  985. log.Errorf("encoding the amp invoice state failed: %v", err)
  986. }
  987. return func() uint64 {
  988. return uint64(len(b.Bytes()))
  989. }
  990. }
  991. // serializeInvoice serializes an invoice to a writer.
  992. //
  993. // Note: this function is in use for a migration. Before making changes that
  994. // would modify the on disk format, make a copy of the original code and store
  995. // it with the migration.
  996. func serializeInvoice(w io.Writer, i *invpkg.Invoice) error {
  997. creationDateBytes, err := i.CreationDate.MarshalBinary()
  998. if err != nil {
  999. return err
  1000. }
  1001. settleDateBytes, err := i.SettleDate.MarshalBinary()
  1002. if err != nil {
  1003. return err
  1004. }
  1005. var fb bytes.Buffer
  1006. err = i.Terms.Features.EncodeBase256(&fb)
  1007. if err != nil {
  1008. return err
  1009. }
  1010. featureBytes := fb.Bytes()
  1011. preimage := [32]byte(invpkg.UnknownPreimage)
  1012. if i.Terms.PaymentPreimage != nil {
  1013. preimage = *i.Terms.PaymentPreimage
  1014. if preimage == invpkg.UnknownPreimage {
  1015. return errors.New("cannot use all-zeroes preimage")
  1016. }
  1017. }
  1018. value := uint64(i.Terms.Value)
  1019. cltvDelta := uint32(i.Terms.FinalCltvDelta)
  1020. expiry := uint64(i.Terms.Expiry)
  1021. amtPaid := uint64(i.AmtPaid)
  1022. state := uint8(i.State)
  1023. var hodlInvoice uint8
  1024. if i.HodlInvoice {
  1025. hodlInvoice = 1
  1026. }
  1027. tlvStream, err := tlv.NewStream(
  1028. // Memo and payreq.
  1029. tlv.MakePrimitiveRecord(memoType, &i.Memo),
  1030. tlv.MakePrimitiveRecord(payReqType, &i.PaymentRequest),
  1031. // Add/settle metadata.
  1032. tlv.MakePrimitiveRecord(createTimeType, &creationDateBytes),
  1033. tlv.MakePrimitiveRecord(settleTimeType, &settleDateBytes),
  1034. tlv.MakePrimitiveRecord(addIndexType, &i.AddIndex),
  1035. tlv.MakePrimitiveRecord(settleIndexType, &i.SettleIndex),
  1036. // Terms.
  1037. tlv.MakePrimitiveRecord(preimageType, &preimage),
  1038. tlv.MakePrimitiveRecord(valueType, &value),
  1039. tlv.MakePrimitiveRecord(cltvDeltaType, &cltvDelta),
  1040. tlv.MakePrimitiveRecord(expiryType, &expiry),
  1041. tlv.MakePrimitiveRecord(paymentAddrType, &i.Terms.PaymentAddr),
  1042. tlv.MakePrimitiveRecord(featuresType, &featureBytes),
  1043. // Invoice state.
  1044. tlv.MakePrimitiveRecord(invStateType, &state),
  1045. tlv.MakePrimitiveRecord(amtPaidType, &amtPaid),
  1046. tlv.MakePrimitiveRecord(hodlInvoiceType, &hodlInvoice),
  1047. // Invoice AMP state.
  1048. tlv.MakeDynamicRecord(
  1049. invoiceAmpStateType, &i.AMPState,
  1050. ampRecordSize(&i.AMPState),
  1051. ampStateEncoder, ampStateDecoder,
  1052. ),
  1053. )
  1054. if err != nil {
  1055. return err
  1056. }
  1057. var b bytes.Buffer
  1058. if err = tlvStream.Encode(&b); err != nil {
  1059. return err
  1060. }
  1061. err = binary.Write(w, byteOrder, uint64(b.Len()))
  1062. if err != nil {
  1063. return err
  1064. }
  1065. if _, err = w.Write(b.Bytes()); err != nil {
  1066. return err
  1067. }
  1068. // Only if this is a _non_ AMP invoice do we serialize the HTLCs
  1069. // in-line with the rest of the invoice.
  1070. if i.IsAMP() {
  1071. return nil
  1072. }
  1073. return serializeHtlcs(w, i.Htlcs)
  1074. }
  1075. // serializeHtlcs serializes a map containing circuit keys and invoice htlcs to
  1076. // a writer.
  1077. func serializeHtlcs(w io.Writer,
  1078. htlcs map[models.CircuitKey]*invpkg.InvoiceHTLC) error {
  1079. for key, htlc := range htlcs {
  1080. // Encode the htlc in a tlv stream.
  1081. chanID := key.ChanID.ToUint64()
  1082. amt := uint64(htlc.Amt)
  1083. mppTotalAmt := uint64(htlc.MppTotalAmt)
  1084. acceptTime := putNanoTime(htlc.AcceptTime)
  1085. resolveTime := putNanoTime(htlc.ResolveTime)
  1086. state := uint8(htlc.State)
  1087. var records []tlv.Record
  1088. records = append(records,
  1089. tlv.MakePrimitiveRecord(chanIDType, &chanID),
  1090. tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
  1091. tlv.MakePrimitiveRecord(amtType, &amt),
  1092. tlv.MakePrimitiveRecord(
  1093. acceptHeightType, &htlc.AcceptHeight,
  1094. ),
  1095. tlv.MakePrimitiveRecord(acceptTimeType, &acceptTime),
  1096. tlv.MakePrimitiveRecord(resolveTimeType, &resolveTime),
  1097. tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
  1098. tlv.MakePrimitiveRecord(htlcStateType, &state),
  1099. tlv.MakePrimitiveRecord(mppTotalAmtType, &mppTotalAmt),
  1100. )
  1101. if htlc.AMP != nil {
  1102. setIDRecord := tlv.MakeDynamicRecord(
  1103. htlcAMPType, &htlc.AMP.Record,
  1104. htlc.AMP.Record.PayloadSize,
  1105. record.AMPEncoder, record.AMPDecoder,
  1106. )
  1107. records = append(records, setIDRecord)
  1108. hash32 := [32]byte(htlc.AMP.Hash)
  1109. hashRecord := tlv.MakePrimitiveRecord(
  1110. htlcHashType, &hash32,
  1111. )
  1112. records = append(records, hashRecord)
  1113. if htlc.AMP.Preimage != nil {
  1114. preimage32 := [32]byte(*htlc.AMP.Preimage)
  1115. preimageRecord := tlv.MakePrimitiveRecord(
  1116. htlcPreimageType, &preimage32,
  1117. )
  1118. records = append(records, preimageRecord)
  1119. }
  1120. }
  1121. // Convert the custom records to tlv.Record types that are ready
  1122. // for serialization.
  1123. customRecords := tlv.MapToRecords(htlc.CustomRecords)
  1124. // Append the custom records. Their ids are in the experimental
  1125. // range and sorted, so there is no need to sort again.
  1126. records = append(records, customRecords...)
  1127. tlvStream, err := tlv.NewStream(records...)
  1128. if err != nil {
  1129. return err
  1130. }
  1131. var b bytes.Buffer
  1132. if err := tlvStream.Encode(&b); err != nil {
  1133. return err
  1134. }
  1135. // Write the length of the tlv stream followed by the stream
  1136. // bytes.
  1137. err = binary.Write(w, byteOrder, uint64(b.Len()))
  1138. if err != nil {
  1139. return err
  1140. }
  1141. if _, err := w.Write(b.Bytes()); err != nil {
  1142. return err
  1143. }
  1144. }
  1145. return nil
  1146. }
  1147. // putNanoTime returns the unix nano time for the passed timestamp. A zero-value
  1148. // timestamp will be mapped to 0, since calling UnixNano in that case is
  1149. // undefined.
  1150. func putNanoTime(t time.Time) uint64 {
  1151. if t.IsZero() {
  1152. return 0
  1153. }
  1154. return uint64(t.UnixNano())
  1155. }
  1156. // getNanoTime returns a timestamp for the given number of nano seconds. If zero
  1157. // is provided, an zero-value time stamp is returned.
  1158. func getNanoTime(ns uint64) time.Time {
  1159. if ns == 0 {
  1160. return time.Time{}
  1161. }
  1162. return time.Unix(0, int64(ns))
  1163. }
  1164. // fetchFilteredAmpInvoices retrieves only a select set of AMP invoices
  1165. // identified by the setID value.
  1166. func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
  1167. setIDs ...*invpkg.SetID) (map[models.CircuitKey]*invpkg.InvoiceHTLC,
  1168. error) {
  1169. htlcs := make(map[models.CircuitKey]*invpkg.InvoiceHTLC)
  1170. for _, setID := range setIDs {
  1171. invoiceSetIDKey := makeInvoiceSetIDKey(invoiceNum, setID[:])
  1172. htlcSetBytes := invoiceBucket.Get(invoiceSetIDKey[:])
  1173. if htlcSetBytes == nil {
  1174. // A set ID was passed in, but we don't have this
  1175. // stored yet, meaning that the setID is being added
  1176. // for the first time.
  1177. return htlcs, invpkg.ErrInvoiceNotFound
  1178. }
  1179. htlcSetReader := bytes.NewReader(htlcSetBytes)
  1180. htlcsBySetID, err := deserializeHtlcs(htlcSetReader)
  1181. if err != nil {
  1182. return nil, err
  1183. }
  1184. for key, htlc := range htlcsBySetID {
  1185. htlcs[key] = htlc
  1186. }
  1187. }
  1188. return htlcs, nil
  1189. }
  1190. // forEachAMPInvoice is a helper function that attempts to iterate over each of
  1191. // the HTLC sets (based on their set ID) for the given AMP invoice identified
  1192. // by its invoiceNum. The callback closure is called for each key within the
  1193. // prefix range.
  1194. func forEachAMPInvoice(invoiceBucket kvdb.RBucket, invoiceNum []byte,
  1195. callback func(key, htlcSet []byte) error) error {
  1196. invoiceCursor := invoiceBucket.ReadCursor()
  1197. // Seek to the first key that includes the invoice data itself.
  1198. invoiceCursor.Seek(invoiceNum)
  1199. // Advance to the very first key _after_ the invoice data, as this is
  1200. // where we'll encounter our first HTLC (if any are present).
  1201. cursorKey, htlcSet := invoiceCursor.Next()
  1202. // If at this point, the cursor key doesn't match the invoice num
  1203. // prefix, then we know that this HTLC doesn't have any set ID HTLCs
  1204. // associated with it.
  1205. if !bytes.HasPrefix(cursorKey, invoiceNum) {
  1206. return nil
  1207. }
  1208. // Otherwise continue to iterate until we no longer match the prefix,
  1209. // executing the call back at each step.
  1210. for ; cursorKey != nil && bytes.HasPrefix(cursorKey, invoiceNum); cursorKey, htlcSet = invoiceCursor.Next() {
  1211. err := callback(cursorKey, htlcSet)
  1212. if err != nil {
  1213. return err
  1214. }
  1215. }
  1216. return nil
  1217. }
  1218. // fetchAmpSubInvoices attempts to use the invoiceNum as a prefix within the
  1219. // AMP bucket to find all the individual HTLCs (by setID) associated with a
  1220. // given invoice. If a list of set IDs are specified, then only HTLCs
  1221. // associated with that setID will be retrieved.
  1222. func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
  1223. setIDs ...*invpkg.SetID) (map[models.CircuitKey]*invpkg.InvoiceHTLC,
  1224. error) {
  1225. // If a set of setIDs was specified, then we can skip the cursor and
  1226. // just read out exactly what we need.
  1227. if len(setIDs) != 0 && setIDs[0] != nil {
  1228. return fetchFilteredAmpInvoices(
  1229. invoiceBucket, invoiceNum, setIDs...,
  1230. )
  1231. }
  1232. // Otherwise, iterate over all the htlc sets that are prefixed beside
  1233. // this invoice in the main invoice bucket.
  1234. htlcs := make(map[models.CircuitKey]*invpkg.InvoiceHTLC)
  1235. err := forEachAMPInvoice(invoiceBucket, invoiceNum,
  1236. func(key, htlcSet []byte) error {
  1237. htlcSetReader := bytes.NewReader(htlcSet)
  1238. htlcsBySetID, err := deserializeHtlcs(htlcSetReader)
  1239. if err != nil {
  1240. return err
  1241. }
  1242. for key, htlc := range htlcsBySetID {
  1243. htlcs[key] = htlc
  1244. }
  1245. return nil
  1246. },
  1247. )
  1248. if err != nil {
  1249. return nil, err
  1250. }
  1251. return htlcs, nil
  1252. }
  1253. // fetchInvoice attempts to read out the relevant state for the invoice as
  1254. // specified by the invoice number. If the setID fields are set, then only the
  1255. // HTLC information pertaining to those set IDs is returned.
  1256. func fetchInvoice(invoiceNum []byte, invoices kvdb.RBucket,
  1257. setIDs ...*invpkg.SetID) (invpkg.Invoice, error) {
  1258. invoiceBytes := invoices.Get(invoiceNum)
  1259. if invoiceBytes == nil {
  1260. return invpkg.Invoice{}, invpkg.ErrInvoiceNotFound
  1261. }
  1262. invoiceReader := bytes.NewReader(invoiceBytes)
  1263. invoice, err := deserializeInvoice(invoiceReader)
  1264. if err != nil {
  1265. return invpkg.Invoice{}, err
  1266. }
  1267. // If this is an AMP invoice we'll also attempt to read out the set of
  1268. // HTLCs that were paid to prior set IDs, if needed.
  1269. if !invoice.IsAMP() {
  1270. return invoice, nil
  1271. }
  1272. if shouldFetchAMPHTLCs(invoice, setIDs) {
  1273. invoice.Htlcs, err = fetchAmpSubInvoices(
  1274. invoices, invoiceNum, setIDs...,
  1275. )
  1276. // TODO(positiveblue): we should fail when we are not able to
  1277. // fetch all the HTLCs for an AMP invoice. Multiple tests in
  1278. // the invoice and channeldb package break if we return this
  1279. // error. We need to update them when we migrate this logic to
  1280. // the sql implementation.
  1281. if err != nil {
  1282. log.Errorf("unable to fetch amp htlcs for inv "+
  1283. "%v and setIDs %v: %w", invoiceNum, setIDs, err)
  1284. }
  1285. }
  1286. return invoice, nil
  1287. }
  1288. // shouldFetchAMPHTLCs returns true if we need to fetch the set of HTLCs that
  1289. // were paid to the relevant set IDs.
  1290. func shouldFetchAMPHTLCs(invoice invpkg.Invoice, setIDs []*invpkg.SetID) bool {
  1291. // For AMP invoice that already have HTLCs populated (created before
  1292. // recurring invoices), then we don't need to read from the prefix
  1293. // keyed section of the bucket.
  1294. if len(invoice.Htlcs) != 0 {
  1295. return false
  1296. }
  1297. // If the "zero" setID was specified, then this means that no HTLC data
  1298. // should be returned alongside of it.
  1299. if len(setIDs) != 0 && setIDs[0] != nil &&
  1300. *setIDs[0] == invpkg.BlankPayAddr {
  1301. return false
  1302. }
  1303. return true
  1304. }
  1305. // fetchInvoiceStateAMP retrieves the state of all the relevant sub-invoice for
  1306. // an AMP invoice. This methods only decode the relevant state vs the entire
  1307. // invoice.
  1308. func fetchInvoiceStateAMP(invoiceNum []byte,
  1309. invoices kvdb.RBucket) (invpkg.AMPInvoiceState, error) {
  1310. // Fetch the raw invoice bytes.
  1311. invoiceBytes := invoices.Get(invoiceNum)
  1312. if invoiceBytes == nil {
  1313. return nil, invpkg.ErrInvoiceNotFound
  1314. }
  1315. r := bytes.NewReader(invoiceBytes)
  1316. var bodyLen int64
  1317. err := binary.Read(r, byteOrder, &bodyLen)
  1318. if err != nil {
  1319. return nil, err
  1320. }
  1321. // Next, we'll make a new TLV stream that only attempts to decode the
  1322. // bytes we actually need.
  1323. ampState := make(invpkg.AMPInvoiceState)
  1324. tlvStream, err := tlv.NewStream(
  1325. // Invoice AMP state.
  1326. tlv.MakeDynamicRecord(
  1327. invoiceAmpStateType, &ampState, nil,
  1328. ampStateEncoder, ampStateDecoder,
  1329. ),
  1330. )
  1331. if err != nil {
  1332. return nil, err
  1333. }
  1334. invoiceReader := io.LimitReader(r, bodyLen)
  1335. if err = tlvStream.Decode(invoiceReader); err != nil {
  1336. return nil, err
  1337. }
  1338. return ampState, nil
  1339. }
  1340. func deserializeInvoice(r io.Reader) (invpkg.Invoice, error) {
  1341. var (
  1342. preimageBytes [32]byte
  1343. value uint64
  1344. cltvDelta uint32
  1345. expiry uint64
  1346. amtPaid uint64
  1347. state uint8
  1348. hodlInvoice uint8
  1349. creationDateBytes []byte
  1350. settleDateBytes []byte
  1351. featureBytes []byte
  1352. )
  1353. var i invpkg.Invoice
  1354. i.AMPState = make(invpkg.AMPInvoiceState)
  1355. tlvStream, err := tlv.NewStream(
  1356. // Memo and payreq.
  1357. tlv.MakePrimitiveRecord(memoType, &i.Memo),
  1358. tlv.MakePrimitiveRecord(payReqType, &i.PaymentRequest),
  1359. // Add/settle metadata.
  1360. tlv.MakePrimitiveRecord(createTimeType, &creationDateBytes),
  1361. tlv.MakePrimitiveRecord(settleTimeType, &settleDateBytes),
  1362. tlv.MakePrimitiveRecord(addIndexType, &i.AddIndex),
  1363. tlv.MakePrimitiveRecord(settleIndexType, &i.SettleIndex),
  1364. // Terms.
  1365. tlv.MakePrimitiveRecord(preimageType, &preimageBytes),
  1366. tlv.MakePrimitiveRecord(valueType, &value),
  1367. tlv.MakePrimitiveRecord(cltvDeltaType, &cltvDelta),
  1368. tlv.MakePrimitiveRecord(expiryType, &expiry),
  1369. tlv.MakePrimitiveRecord(paymentAddrType, &i.Terms.PaymentAddr),
  1370. tlv.MakePrimitiveRecord(featuresType, &featureBytes),
  1371. // Invoice state.
  1372. tlv.MakePrimitiveRecord(invStateType, &state),
  1373. tlv.MakePrimitiveRecord(amtPaidType, &amtPaid),
  1374. tlv.MakePrimitiveRecord(hodlInvoiceType, &hodlInvoice),
  1375. // Invoice AMP state.
  1376. tlv.MakeDynamicRecord(
  1377. invoiceAmpStateType, &i.AMPState, nil,
  1378. ampStateEncoder, ampStateDecoder,
  1379. ),
  1380. )
  1381. if err != nil {
  1382. return i, err
  1383. }
  1384. var bodyLen int64
  1385. err = binary.Read(r, byteOrder, &bodyLen)
  1386. if err != nil {
  1387. return i, err
  1388. }
  1389. lr := io.LimitReader(r, bodyLen)
  1390. if err = tlvStream.Decode(lr); err != nil {
  1391. return i, err
  1392. }
  1393. preimage := lntypes.Preimage(preimageBytes)
  1394. if preimage != invpkg.UnknownPreimage {
  1395. i.Terms.PaymentPreimage = &preimage
  1396. }
  1397. i.Terms.Value = lnwire.MilliSatoshi(value)
  1398. i.Terms.FinalCltvDelta = int32(cltvDelta)
  1399. i.Terms.Expiry = time.Duration(expiry)
  1400. i.AmtPaid = lnwire.MilliSatoshi(amtPaid)
  1401. i.State = invpkg.ContractState(state)
  1402. if hodlInvoice != 0 {
  1403. i.HodlInvoice = true
  1404. }
  1405. err = i.CreationDate.UnmarshalBinary(creationDateBytes)
  1406. if err != nil {
  1407. return i, err
  1408. }
  1409. err = i.SettleDate.UnmarshalBinary(settleDateBytes)
  1410. if err != nil {
  1411. return i, err
  1412. }
  1413. rawFeatures := lnwire.NewRawFeatureVector()
  1414. err = rawFeatures.DecodeBase256(
  1415. bytes.NewReader(featureBytes), len(featureBytes),
  1416. )
  1417. if err != nil {
  1418. return i, err
  1419. }
  1420. i.Terms.Features = lnwire.NewFeatureVector(
  1421. rawFeatures, lnwire.Features,
  1422. )
  1423. i.Htlcs, err = deserializeHtlcs(r)
  1424. return i, err
  1425. }
  1426. func encodeCircuitKeys(w io.Writer, val interface{}, buf *[8]byte) error {
  1427. if v, ok := val.(*map[models.CircuitKey]struct{}); ok {
  1428. // We encode the set of circuit keys as a varint length prefix.
  1429. // followed by a series of fixed sized uint8 integers.
  1430. numKeys := uint64(len(*v))
  1431. if err := tlv.WriteVarInt(w, numKeys, buf); err != nil {
  1432. return err
  1433. }
  1434. for key := range *v {
  1435. scidInt := key.ChanID.ToUint64()
  1436. if err := tlv.EUint64(w, &scidInt, buf); err != nil {
  1437. return err
  1438. }
  1439. if err := tlv.EUint64(w, &key.HtlcID, buf); err != nil {
  1440. return err
  1441. }
  1442. }
  1443. return nil
  1444. }
  1445. return tlv.NewTypeForEncodingErr(val, "*map[CircuitKey]struct{}")
  1446. }
  1447. func decodeCircuitKeys(r io.Reader, val interface{}, buf *[8]byte,
  1448. l uint64) error {
  1449. if v, ok := val.(*map[models.CircuitKey]struct{}); ok {
  1450. // First, we'll read out the varint that encodes the number of
  1451. // circuit keys encoded.
  1452. numKeys, err := tlv.ReadVarInt(r, buf)
  1453. if err != nil {
  1454. return err
  1455. }
  1456. // Now that we know how many keys to expect, iterate reading
  1457. // each one until we're done.
  1458. for i := uint64(0); i < numKeys; i++ {
  1459. var (
  1460. key models.CircuitKey
  1461. scid uint64
  1462. )
  1463. if err := tlv.DUint64(r, &scid, buf, 8); err != nil {
  1464. return err
  1465. }
  1466. key.ChanID = lnwire.NewShortChanIDFromInt(scid)
  1467. err := tlv.DUint64(r, &key.HtlcID, buf, 8)
  1468. if err != nil {
  1469. return err
  1470. }
  1471. (*v)[key] = struct{}{}
  1472. }
  1473. return nil
  1474. }
  1475. return tlv.NewTypeForDecodingErr(val, "*map[CircuitKey]struct{}", l, l)
  1476. }
  1477. // ampStateEncoder is a custom TLV encoder for the AMPInvoiceState record.
  1478. func ampStateEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
  1479. if v, ok := val.(*invpkg.AMPInvoiceState); ok {
  1480. // We'll encode the AMP state as a series of KV pairs on the
  1481. // wire with a length prefix.
  1482. numRecords := uint64(len(*v))
  1483. // First, we'll write out the number of records as a var int.
  1484. if err := tlv.WriteVarInt(w, numRecords, buf); err != nil {
  1485. return err
  1486. }
  1487. // With that written out, we'll now encode the entries
  1488. // themselves as a sub-TLV record, which includes its _own_
  1489. // inner length prefix.
  1490. for setID, ampState := range *v {
  1491. setID := [32]byte(setID)
  1492. ampState := ampState
  1493. htlcState := uint8(ampState.State)
  1494. settleDate := ampState.SettleDate
  1495. settleDateBytes, err := settleDate.MarshalBinary()
  1496. if err != nil {
  1497. return err
  1498. }
  1499. amtPaid := uint64(ampState.AmtPaid)
  1500. var ampStateTlvBytes bytes.Buffer
  1501. tlvStream, err := tlv.NewStream(
  1502. tlv.MakePrimitiveRecord(
  1503. ampStateSetIDType, &setID,
  1504. ),
  1505. tlv.MakePrimitiveRecord(
  1506. ampStateHtlcStateType, &htlcState,
  1507. ),
  1508. tlv.MakePrimitiveRecord(
  1509. ampStateSettleIndexType,
  1510. &ampState.SettleIndex,
  1511. ),
  1512. tlv.MakePrimitiveRecord(
  1513. ampStateSettleDateType,
  1514. &settleDateBytes,
  1515. ),
  1516. tlv.MakeDynamicRecord(
  1517. ampStateCircuitKeysType,
  1518. &ampState.InvoiceKeys,
  1519. func() uint64 {
  1520. // The record takes 8 bytes to
  1521. // encode the set of circuits,
  1522. // 8 bytes for the scid for the
  1523. // key, and 8 bytes for the HTLC
  1524. // index.
  1525. keys := ampState.InvoiceKeys
  1526. numKeys := uint64(len(keys))
  1527. size := tlv.VarIntSize(numKeys)
  1528. dataSize := (numKeys * 16)
  1529. return size + dataSize
  1530. },
  1531. encodeCircuitKeys, decodeCircuitKeys,
  1532. ),
  1533. tlv.MakePrimitiveRecord(
  1534. ampStateAmtPaidType, &amtPaid,
  1535. ),
  1536. )
  1537. if err != nil {
  1538. return err
  1539. }
  1540. err = tlvStream.Encode(&ampStateTlvBytes)
  1541. if err != nil {
  1542. return err
  1543. }
  1544. // We encode the record with a varint length followed by
  1545. // the _raw_ TLV bytes.
  1546. tlvLen := uint64(len(ampStateTlvBytes.Bytes()))
  1547. if err := tlv.WriteVarInt(w, tlvLen, buf); err != nil {
  1548. return err
  1549. }
  1550. _, err = w.Write(ampStateTlvBytes.Bytes())
  1551. if err != nil {
  1552. return err
  1553. }
  1554. }
  1555. return nil
  1556. }
  1557. return tlv.NewTypeForEncodingErr(val, "channeldb.AMPInvoiceState")
  1558. }
  1559. // ampStateDecoder is a custom TLV decoder for the AMPInvoiceState record.
  1560. func ampStateDecoder(r io.Reader, val interface{}, buf *[8]byte,
  1561. l uint64) error {
  1562. if v, ok := val.(*invpkg.AMPInvoiceState); ok {
  1563. // First, we'll decode the varint that encodes how many set IDs
  1564. // are encoded within the greater map.
  1565. numRecords, err := tlv.ReadVarInt(r, buf)
  1566. if err != nil {
  1567. return err
  1568. }
  1569. // Now that we know how many records we'll need to read, we can
  1570. // iterate and read them all out in series.
  1571. for i := uint64(0); i < numRecords; i++ {
  1572. // Read out the varint that encodes the size of this
  1573. // inner TLV record.
  1574. stateRecordSize, err := tlv.ReadVarInt(r, buf)
  1575. if err != nil {
  1576. return err
  1577. }
  1578. // Using this information, we'll create a new limited
  1579. // reader that'll return an EOF once the end has been
  1580. // reached so the stream stops consuming bytes.
  1581. innerTlvReader := io.LimitedReader{
  1582. R: r,
  1583. N: int64(stateRecordSize),
  1584. }
  1585. var (
  1586. setID [32]byte
  1587. htlcState uint8
  1588. settleIndex uint64
  1589. settleDateBytes []byte
  1590. invoiceKeys = make(
  1591. map[models.CircuitKey]struct{},
  1592. )
  1593. amtPaid uint64
  1594. )
  1595. tlvStream, err := tlv.NewStream(
  1596. tlv.MakePrimitiveRecord(
  1597. ampStateSetIDType, &setID,
  1598. ),
  1599. tlv.MakePrimitiveRecord(
  1600. ampStateHtlcStateType, &htlcState,
  1601. ),
  1602. tlv.MakePrimitiveRecord(
  1603. ampStateSettleIndexType, &settleIndex,
  1604. ),
  1605. tlv.MakePrimitiveRecord(
  1606. ampStateSettleDateType,
  1607. &settleDateBytes,
  1608. ),
  1609. tlv.MakeDynamicRecord(
  1610. ampStateCircuitKeysType,
  1611. &invoiceKeys, nil,
  1612. encodeCircuitKeys, decodeCircuitKeys,
  1613. ),
  1614. tlv.MakePrimitiveRecord(
  1615. ampStateAmtPaidType, &amtPaid,
  1616. ),
  1617. )
  1618. if err != nil {
  1619. return err
  1620. }
  1621. err = tlvStream.Decode(&innerTlvReader)
  1622. if err != nil {
  1623. return err
  1624. }
  1625. var settleDate time.Time
  1626. err = settleDate.UnmarshalBinary(settleDateBytes)
  1627. if err != nil {
  1628. return err
  1629. }
  1630. (*v)[setID] = invpkg.InvoiceStateAMP{
  1631. State: invpkg.HtlcState(htlcState),
  1632. SettleIndex: settleIndex,
  1633. SettleDate: settleDate,
  1634. InvoiceKeys: invoiceKeys,
  1635. AmtPaid: lnwire.MilliSatoshi(amtPaid),
  1636. }
  1637. }
  1638. return nil
  1639. }
  1640. return tlv.NewTypeForDecodingErr(
  1641. val, "channeldb.AMPInvoiceState", l, l,
  1642. )
  1643. }
  1644. // deserializeHtlcs reads a list of invoice htlcs from a reader and returns it
  1645. // as a map.
  1646. func deserializeHtlcs(r io.Reader) (map[models.CircuitKey]*invpkg.InvoiceHTLC,
  1647. error) {
  1648. htlcs := make(map[models.CircuitKey]*invpkg.InvoiceHTLC)
  1649. for {
  1650. // Read the length of the tlv stream for this htlc.
  1651. var streamLen int64
  1652. if err := binary.Read(r, byteOrder, &streamLen); err != nil {
  1653. if err == io.EOF {
  1654. break
  1655. }
  1656. return nil, err
  1657. }
  1658. // Limit the reader so that it stops at the end of this htlc's
  1659. // stream.
  1660. htlcReader := io.LimitReader(r, streamLen)
  1661. // Decode the contents into the htlc fields.
  1662. var (
  1663. htlc invpkg.InvoiceHTLC
  1664. key models.CircuitKey
  1665. chanID uint64
  1666. state uint8
  1667. acceptTime, resolveTime uint64
  1668. amt, mppTotalAmt uint64
  1669. amp = &record.AMP{}
  1670. hash32 = &[32]byte{}
  1671. preimage32 = &[32]byte{}
  1672. )
  1673. tlvStream, err := tlv.NewStream(
  1674. tlv.MakePrimitiveRecord(chanIDType, &chanID),
  1675. tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
  1676. tlv.MakePrimitiveRecord(amtType, &amt),
  1677. tlv.MakePrimitiveRecord(
  1678. acceptHeightType, &htlc.AcceptHeight,
  1679. ),
  1680. tlv.MakePrimitiveRecord(acceptTimeType, &acceptTime),
  1681. tlv.MakePrimitiveRecord(resolveTimeType, &resolveTime),
  1682. tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
  1683. tlv.MakePrimitiveRecord(htlcStateType, &state),
  1684. tlv.MakePrimitiveRecord(mppTotalAmtType, &mppTotalAmt),
  1685. tlv.MakeDynamicRecord(
  1686. htlcAMPType, amp, amp.PayloadSize,
  1687. record.AMPEncoder, record.AMPDecoder,
  1688. ),
  1689. tlv.MakePrimitiveRecord(htlcHashType, hash32),
  1690. tlv.MakePrimitiveRecord(htlcPreimageType, preimage32),
  1691. )
  1692. if err != nil {
  1693. return nil, err
  1694. }
  1695. parsedTypes, err := tlvStream.DecodeWithParsedTypes(htlcReader)
  1696. if err != nil {
  1697. return nil, err
  1698. }
  1699. if _, ok := parsedTypes[htlcAMPType]; !ok {
  1700. amp = nil
  1701. }
  1702. var preimage *lntypes.Preimage
  1703. if _, ok := parsedTypes[htlcPreimageType]; ok {
  1704. pimg := lntypes.Preimage(*preimage32)
  1705. preimage = &pimg
  1706. }
  1707. var hash *lntypes.Hash
  1708. if _, ok := parsedTypes[htlcHashType]; ok {
  1709. h := lntypes.Hash(*hash32)
  1710. hash = &h
  1711. }
  1712. key.ChanID = lnwire.NewShortChanIDFromInt(chanID)
  1713. htlc.AcceptTime = getNanoTime(acceptTime)
  1714. htlc.ResolveTime = getNanoTime(resolveTime)
  1715. htlc.State = invpkg.HtlcState(state)
  1716. htlc.Amt = lnwire.MilliSatoshi(amt)
  1717. htlc.MppTotalAmt = lnwire.MilliSatoshi(mppTotalAmt)
  1718. if amp != nil && hash != nil {
  1719. htlc.AMP = &invpkg.InvoiceHtlcAMPData{
  1720. Record: *amp,
  1721. Hash: *hash,
  1722. Preimage: preimage,
  1723. }
  1724. }
  1725. // Reconstruct the custom records fields from the parsed types
  1726. // map return from the tlv parser.
  1727. htlc.CustomRecords = hop.NewCustomRecords(parsedTypes)
  1728. htlcs[key] = &htlc
  1729. }
  1730. return htlcs, nil
  1731. }
  1732. // invoiceSetIDKeyLen is the length of the key that's used to store the
  1733. // individual HTLCs prefixed by their ID along side the main invoice within the
  1734. // invoiceBytes. We use 4 bytes for the invoice number, and 32 bytes for the
  1735. // set ID.
  1736. const invoiceSetIDKeyLen = 4 + 32
  1737. // makeInvoiceSetIDKey returns the prefix key, based on the set ID and invoice
  1738. // number where the HTLCs for this setID will be stored udner.
  1739. func makeInvoiceSetIDKey(invoiceNum, setID []byte) [invoiceSetIDKeyLen]byte {
  1740. // Construct the prefix key we need to obtain the invoice information:
  1741. // invoiceNum || setID.
  1742. var invoiceSetIDKey [invoiceSetIDKeyLen]byte
  1743. copy(invoiceSetIDKey[:], invoiceNum)
  1744. copy(invoiceSetIDKey[len(invoiceNum):], setID)
  1745. return invoiceSetIDKey
  1746. }
  1747. // delAMPInvoices attempts to delete all the "sub" invoices associated with a
  1748. // greater AMP invoices. We do this by deleting the set of keys that share the
  1749. // invoice number as a prefix.
  1750. func delAMPInvoices(invoiceNum []byte, invoiceBucket kvdb.RwBucket) error {
  1751. // Since it isn't safe to delete using an active cursor, we'll use the
  1752. // cursor simply to collect the set of keys we need to delete, _then_
  1753. // delete them in another pass.
  1754. var keysToDel [][]byte
  1755. err := forEachAMPInvoice(
  1756. invoiceBucket, invoiceNum,
  1757. func(cursorKey, v []byte) error {
  1758. keysToDel = append(keysToDel, cursorKey)
  1759. return nil
  1760. },
  1761. )
  1762. if err != nil {
  1763. return err
  1764. }
  1765. // In this next phase, we'll then delete all the relevant invoices.
  1766. for _, keyToDel := range keysToDel {
  1767. if err := invoiceBucket.Delete(keyToDel); err != nil {
  1768. return err
  1769. }
  1770. }
  1771. return nil
  1772. }
  1773. // delAMPSettleIndex removes all the entries in the settle index associated
  1774. // with a given AMP invoice.
  1775. func delAMPSettleIndex(invoiceNum []byte, invoices,
  1776. settleIndex kvdb.RwBucket) error {
  1777. // First, we need to grab the AMP invoice state to see if there's
  1778. // anything that we even need to delete.
  1779. ampState, err := fetchInvoiceStateAMP(invoiceNum, invoices)
  1780. if err != nil {
  1781. return err
  1782. }
  1783. // If there's no AMP state at all (non-AMP invoice), then we can return
  1784. // early.
  1785. if len(ampState) == 0 {
  1786. return nil
  1787. }
  1788. // Otherwise, we'll need to iterate and delete each settle index within
  1789. // the set of returned entries.
  1790. var settleIndexKey [8]byte
  1791. for _, subState := range ampState {
  1792. byteOrder.PutUint64(
  1793. settleIndexKey[:], subState.SettleIndex,
  1794. )
  1795. if err := settleIndex.Delete(settleIndexKey[:]); err != nil {
  1796. return err
  1797. }
  1798. }
  1799. return nil
  1800. }
  1801. // DeleteCanceledInvoices deletes all canceled invoices from the database.
  1802. func (d *DB) DeleteCanceledInvoices(_ context.Context) error {
  1803. return kvdb.Update(d, func(tx kvdb.RwTx) error {
  1804. invoices := tx.ReadWriteBucket(invoiceBucket)
  1805. if invoices == nil {
  1806. return nil
  1807. }
  1808. invoiceIndex := invoices.NestedReadWriteBucket(
  1809. invoiceIndexBucket,
  1810. )
  1811. if invoiceIndex == nil {
  1812. return nil
  1813. }
  1814. invoiceAddIndex := invoices.NestedReadWriteBucket(
  1815. addIndexBucket,
  1816. )
  1817. if invoiceAddIndex == nil {
  1818. return nil
  1819. }
  1820. payAddrIndex := tx.ReadWriteBucket(payAddrIndexBucket)
  1821. return invoiceIndex.ForEach(func(k, v []byte) error {
  1822. // Skip the special numInvoicesKey as that does not
  1823. // point to a valid invoice.
  1824. if bytes.Equal(k, numInvoicesKey) {
  1825. return nil
  1826. }
  1827. // Skip sub-buckets.
  1828. if v == nil {
  1829. return nil
  1830. }
  1831. invoice, err := fetchInvoice(v, invoices)
  1832. if err != nil {
  1833. return err
  1834. }
  1835. if invoice.State != invpkg.ContractCanceled {
  1836. return nil
  1837. }
  1838. // Delete the payment hash from the invoice index.
  1839. err = invoiceIndex.Delete(k)
  1840. if err != nil {
  1841. return err
  1842. }
  1843. // Delete payment address index reference if there's a
  1844. // valid payment address.
  1845. if invoice.Terms.PaymentAddr != invpkg.BlankPayAddr {
  1846. // To ensure consistency check that the already
  1847. // fetched invoice key matches the one in the
  1848. // payment address index.
  1849. key := payAddrIndex.Get(
  1850. invoice.Terms.PaymentAddr[:],
  1851. )
  1852. if bytes.Equal(key, k) {
  1853. // Delete from the payment address
  1854. // index.
  1855. if err := payAddrIndex.Delete(
  1856. invoice.Terms.PaymentAddr[:],
  1857. ); err != nil {
  1858. return err
  1859. }
  1860. }
  1861. }
  1862. // Remove from the add index.
  1863. var addIndexKey [8]byte
  1864. byteOrder.PutUint64(addIndexKey[:], invoice.AddIndex)
  1865. err = invoiceAddIndex.Delete(addIndexKey[:])
  1866. if err != nil {
  1867. return err
  1868. }
  1869. // Note that we don't need to delete the invoice from
  1870. // the settle index as it is not added until the
  1871. // invoice is settled.
  1872. // Now remove all sub invoices.
  1873. err = delAMPInvoices(k, invoices)
  1874. if err != nil {
  1875. return err
  1876. }
  1877. // Finally remove the serialized invoice from the
  1878. // invoice bucket.
  1879. return invoices.Delete(k)
  1880. })
  1881. }, func() {})
  1882. }
  1883. // DeleteInvoice attempts to delete the passed invoices from the database in
  1884. // one transaction. The passed delete references hold all keys required to
  1885. // delete the invoices without also needing to deserialize them.
  1886. func (d *DB) DeleteInvoice(_ context.Context,
  1887. invoicesToDelete []invpkg.InvoiceDeleteRef) error {
  1888. err := kvdb.Update(d, func(tx kvdb.RwTx) error {
  1889. invoices := tx.ReadWriteBucket(invoiceBucket)
  1890. if invoices == nil {
  1891. return invpkg.ErrNoInvoicesCreated
  1892. }
  1893. invoiceIndex := invoices.NestedReadWriteBucket(
  1894. invoiceIndexBucket,
  1895. )
  1896. if invoiceIndex == nil {
  1897. return invpkg.ErrNoInvoicesCreated
  1898. }
  1899. invoiceAddIndex := invoices.NestedReadWriteBucket(
  1900. addIndexBucket,
  1901. )
  1902. if invoiceAddIndex == nil {
  1903. return invpkg.ErrNoInvoicesCreated
  1904. }
  1905. // settleIndex can be nil, as the bucket is created lazily
  1906. // when the first invoice is settled.
  1907. settleIndex := invoices.NestedReadWriteBucket(settleIndexBucket)
  1908. payAddrIndex := tx.ReadWriteBucket(payAddrIndexBucket)
  1909. for _, ref := range invoicesToDelete {
  1910. // Fetch the invoice key for using it to check for
  1911. // consistency and also to delete from the invoice
  1912. // index.
  1913. invoiceKey := invoiceIndex.Get(ref.PayHash[:])
  1914. if invoiceKey == nil {
  1915. return invpkg.ErrInvoiceNotFound
  1916. }
  1917. err := invoiceIndex.Delete(ref.PayHash[:])
  1918. if err != nil {
  1919. return err
  1920. }
  1921. // Delete payment address index reference if there's a
  1922. // valid payment address passed.
  1923. if ref.PayAddr != nil {
  1924. // To ensure consistency check that the already
  1925. // fetched invoice key matches the one in the
  1926. // payment address index.
  1927. key := payAddrIndex.Get(ref.PayAddr[:])
  1928. if bytes.Equal(key, invoiceKey) {
  1929. // Delete from the payment address
  1930. // index. Note that since the payment
  1931. // address index has been introduced
  1932. // with an empty migration it may be
  1933. // possible that the index doesn't have
  1934. // an entry for this invoice.
  1935. // ref: https://github.com/lightningnetwork/lnd/pull/4285/commits/cbf71b5452fa1d3036a43309e490787c5f7f08dc#r426368127
  1936. if err := payAddrIndex.Delete(
  1937. ref.PayAddr[:],
  1938. ); err != nil {
  1939. return err
  1940. }
  1941. }
  1942. }
  1943. var addIndexKey [8]byte
  1944. byteOrder.PutUint64(addIndexKey[:], ref.AddIndex)
  1945. // To ensure consistency check that the key stored in
  1946. // the add index also matches the previously fetched
  1947. // invoice key.
  1948. key := invoiceAddIndex.Get(addIndexKey[:])
  1949. if !bytes.Equal(key, invoiceKey) {
  1950. return fmt.Errorf("unknown invoice " +
  1951. "in add index")
  1952. }
  1953. // Remove from the add index.
  1954. err = invoiceAddIndex.Delete(addIndexKey[:])
  1955. if err != nil {
  1956. return err
  1957. }
  1958. // Remove from the settle index if available and
  1959. // if the invoice is settled.
  1960. if settleIndex != nil && ref.SettleIndex > 0 {
  1961. var settleIndexKey [8]byte
  1962. byteOrder.PutUint64(
  1963. settleIndexKey[:], ref.SettleIndex,
  1964. )
  1965. // To ensure consistency check that the already
  1966. // fetched invoice key matches the one in the
  1967. // settle index
  1968. key := settleIndex.Get(settleIndexKey[:])
  1969. if !bytes.Equal(key, invoiceKey) {
  1970. return fmt.Errorf("unknown invoice " +
  1971. "in settle index")
  1972. }
  1973. err = settleIndex.Delete(settleIndexKey[:])
  1974. if err != nil {
  1975. return err
  1976. }
  1977. }
  1978. // In addition to deleting the main invoice state, if
  1979. // this is an AMP invoice, then we'll also need to
  1980. // delete the set HTLC set stored as a key prefix. For
  1981. // non-AMP invoices, this'll be a noop.
  1982. err = delAMPSettleIndex(
  1983. invoiceKey, invoices, settleIndex,
  1984. )
  1985. if err != nil {
  1986. return err
  1987. }
  1988. err = delAMPInvoices(invoiceKey, invoices)
  1989. if err != nil {
  1990. return err
  1991. }
  1992. // Finally remove the serialized invoice from the
  1993. // invoice bucket.
  1994. err = invoices.Delete(invoiceKey)
  1995. if err != nil {
  1996. return err
  1997. }
  1998. }
  1999. return nil
  2000. }, func() {})
  2001. return err
  2002. }