From eda95e6607af0ea330590db308bf1f6b352d33a6 Mon Sep 17 00:00:00 2001 From: positiveblue Date: Mon, 29 May 2023 19:05:46 -0700 Subject: [PATCH] sqldb: add AMP invoices SQL schema Schema for AMP invocies. AMP invoices can be paid multiple times and each payment to an AMP invoice is identified by a `set_id`. The A in AMP stands for `Atomic`. All the htlcs belonging to the same AMP payment (share the same set_id) will be resolved at the same time with the same result: settled/canceled. AMP invoices do not have an "invoice preimage". Instead, each htcl has its own hash/preimage. When a new htlc is added the hash for that htlc is attached to it. When all the htlcs of a set_id have been received we are able to compute the preimage for each one of them. --- .../migrations/000002_amp_invoices.down.sql | 8 +++ .../migrations/000002_amp_invoices.up.sql | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 sqldb/sqlc/migrations/000002_amp_invoices.down.sql create mode 100644 sqldb/sqlc/migrations/000002_amp_invoices.up.sql diff --git a/sqldb/sqlc/migrations/000002_amp_invoices.down.sql b/sqldb/sqlc/migrations/000002_amp_invoices.down.sql new file mode 100644 index 000000000..0167bdeb2 --- /dev/null +++ b/sqldb/sqlc/migrations/000002_amp_invoices.down.sql @@ -0,0 +1,8 @@ +DROP INDEX IF EXISTS amp_htlcs_htlc_id_idx; +DROP INDEX IF EXISTS amp_htlcs_invoice_id_idx; +DROP INDEX IF EXISTS amp_htlcs_set_id_idx; +DROP TABLE IF EXISTS amp_invoice_htlcs; + +DROP INDEX IF EXISTS amp_invoice_payments_invoice_id_idx; +DROP TABLE IF EXISTS amp_invoice_payments; + diff --git a/sqldb/sqlc/migrations/000002_amp_invoices.up.sql b/sqldb/sqlc/migrations/000002_amp_invoices.up.sql new file mode 100644 index 000000000..a4daa8c30 --- /dev/null +++ b/sqldb/sqlc/migrations/000002_amp_invoices.up.sql @@ -0,0 +1,54 @@ +-- amp_invoices_payments +CREATE TABLE IF NOT EXISTS amp_invoice_payments ( + -- The set id identifying the payment. + set_id BLOB PRIMARY KEY, + + -- The state of this amp payment. This matches the state for all the htlcs + -- belonging to this set id. The A in AMP stands for Atomic. + state SMALLINT NOT NULL, + + -- Timestamp of when the first htlc for this payment was accepted. + created_at TIMESTAMP NOT NULL, + + -- If settled, the invoice payment related to this set id. + settled_index INTEGER REFERENCES invoice_payments(id), + + -- The invoice id this set id is related to. + invoice_id INTEGER NOT NULL REFERENCES invoices(id) +); + +CREATE INDEX IF NOT EXISTS amp_invoice_payments_invoice_id_idx ON amp_invoice_payments(invoice_id); + +-- amp_invoice_htlcs contains the complementary information for an htlc related +-- to an AMP invoice. +CREATE TABLE IF NOT EXISTS amp_invoice_htlcs ( + -- The set id identifying the payment this htlc belongs to. + set_id BLOB NOT NULL REFERENCES amp_invoice_payments(set_id), + + -- The id of the htlc this entry blongs to. + htlc_id BIGINT NOT NULL REFERENCES invoice_htlcs(id), + + -- The invoice id this entry is related to. + invoice_id INTEGER NOT NULL REFERENCES invoices(id), + + -- The root share for this amp htlc. + root_share BLOB NOT NULL, + + -- The child index for this amp htlc. + child_index BIGINT NOT NULL, + + -- The htlc-level payment hash. An AMP htlc will carry a different payment + -- hash from the invoice it might be satisfying. They are needed to ensure + -- that we reconstruct the preimage correctly. + hash BLOB NOT NULL, + + -- The HTLC-level preimage that satisfies the AMP htlc's Hash. + -- The preimage will be derived either from secret share reconstruction of + -- the shares in the AMP payload. + preimage BLOB +); + +CREATE INDEX IF NOT EXISTS amp_htlcs_set_id_idx ON amp_invoice_htlcs(set_id); +CREATE INDEX IF NOT EXISTS amp_htlcs_invoice_id_idx ON amp_invoice_htlcs(invoice_id); +CREATE INDEX IF NOT EXISTS amp_htlcs_htlc_id_idx ON amp_invoice_htlcs(htlc_id); +