Repository navigation
feat(peering): deliver paid peering invoices and gate feeds #1088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ferryx349
wants to merge
11
commits into
main
Choose a base branch
from
feat/paid-peering-2-delivery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,689
−65
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1d5c473
feat(peering): add paid peering billing ledger
Ferryx349 e45cd65
fix(peering): add missing peering payment repository to ledger PR
Ferryx349 2d3671c
fix(peering): make failed settlements retryable and preserve paid amo…
Ferryx349 416a358
Merge branch 'main' into feat/paid-peering-1-ledger
Ferryx349 82c7b6f
Merge branch 'main' into feat/paid-peering-1-ledger
Ferryx349 ebf01e6
fix(peering): harden retry scan, expiry fallback and notification swi…
Ferryx349 a6d50fd
test(peering): cover the paid peering ledger in integration
Ferryx349 e1d08e1
feat(peering): deliver paid peering invoices and gate feeds
Ferryx349 1a27e19
fix(peering): harden invoice delivery and access gating
Ferryx349 62d63f7
fix(peering): make paid feed expiry lifecycle-safe
Ferryx349 3275588
fix(peering): preserve renewals and free feeds on expiry
Ferryx349 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat(peering): deliver paid peering invoices and gate peer feeds | ||
|
|
||
| Configured peers can request their BOLT11 invoice as a signed `kind 402` event | ||
| and pay it through Relay B's existing processor. Feed `REQ`, `COUNT` and NIP-77 | ||
| requests are refused with a payment-required reason until a paid period is | ||
| active. Expired invoices are reissued after a cooldown, and a missed webhook is | ||
| rechecked before access is denied. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat(peering): add the paid peering billing ledger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| exports.up = async function (knex) { | ||
| await knex.schema.alterTable('invoices', (table) => { | ||
| table.enum('purpose', ['admission', 'subscription', 'peering']).nullable() | ||
| table.bigint('term_amount_msats').unsigned().nullable() | ||
| table.integer('term_period_seconds').nullable() | ||
| table.index(['purpose', 'status'], 'idx_invoices_purpose_status') | ||
| }) | ||
|
|
||
| // Existing rows predate the purpose column. A plan means a subscription; no plan means the | ||
| // admission-fee flow. Peering invoices always set purpose explicitly. | ||
| await knex.raw(` | ||
| UPDATE invoices | ||
| SET purpose = CASE WHEN plan_id IS NULL THEN 'admission' ELSE 'subscription' END | ||
| WHERE purpose IS NULL | ||
| `) | ||
|
|
||
| await knex.raw('ALTER TABLE invoices ALTER COLUMN purpose SET DEFAULT \'admission\'') | ||
| await knex.raw('ALTER TABLE invoices ALTER COLUMN purpose SET NOT NULL') | ||
|
|
||
| // confirm_invoice() credits the payer's user balance. A peering invoice is paid | ||
| // by another relay, not by a user, so skip the balance credit for that purpose. | ||
| await knex.raw(` | ||
| CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE) | ||
| RETURNS INTEGER | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| payee BYTEA; | ||
| confirmed_date TIMESTAMP WITHOUT TIME ZONE; | ||
| invoice_unit TEXT; | ||
| invoice_purpose TEXT; | ||
| BEGIN | ||
| PERFORM ASSERT_SERIALIZED(); | ||
|
|
||
| SELECT "pubkey", "confirmed_at", "unit", "purpose" | ||
| INTO payee, confirmed_date, invoice_unit, invoice_purpose | ||
| FROM "invoices" | ||
| WHERE id = invoice_id; | ||
|
|
||
| IF confirmed_date IS NULL THEN | ||
| UPDATE invoices | ||
| SET | ||
| "confirmed_at" = confirmation_date, | ||
| "amount_paid" = amount_received, | ||
| "updated_at" = now_utc() | ||
| WHERE id = invoice_id; | ||
|
|
||
| IF invoice_purpose <> 'peering' THEN | ||
| IF invoice_unit = 'sats' THEN | ||
| UPDATE users SET balance = balance + amount_received * 1000 WHERE "pubkey" = payee; | ||
| ELSIF invoice_unit = 'msats' THEN | ||
| UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee; | ||
| ELSIF invoice_unit = 'btc' THEN | ||
| UPDATE users SET balance = balance + amount_received * 100000000 * 1000 WHERE "pubkey" = payee; | ||
| END IF; | ||
| END IF; | ||
| END IF; | ||
| RETURN 0; | ||
| END; | ||
| $$;`) | ||
|
|
||
| await knex.schema.createTable('peering_payments', (table) => { | ||
| // A peer can both buy from and sell to this relay, so direction is part of the key. | ||
| table.binary('peer_pubkey').notNullable() | ||
| table.enum('direction', ['sell', 'buy']).notNullable().defaultTo('sell') | ||
| table | ||
| .enum('status', ['unpaid', 'invoiced', 'active', 'renewal_pending', 'lapsed']) | ||
| .notNullable() | ||
| .defaultTo('unpaid') | ||
| table.text('current_invoice_id').nullable() | ||
| table.text('renewal_invoice_id').nullable() | ||
| table.timestamp('period_start', { useTz: true }).nullable() | ||
| table.timestamp('period_end', { useTz: true }).nullable() | ||
| table.bigint('term_amount_msats').unsigned().nullable() | ||
| table.integer('term_period_seconds').nullable() | ||
| table.text('last_settled_invoice_id').nullable() | ||
| table.timestamp('last_settled_at', { useTz: true }).nullable() | ||
| table.timestamp('last_checked_at', { useTz: true }).nullable() | ||
| table.text('last_error').nullable() | ||
| table.timestamp('created_at', { useTz: true }).notNullable().defaultTo(knex.fn.now()) | ||
| table.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(knex.fn.now()) | ||
|
|
||
| table.primary(['peer_pubkey', 'direction']) | ||
| table.index(['status', 'period_end'], 'idx_peering_payments_status_period_end') | ||
| }) | ||
| } | ||
|
|
||
| exports.down = async function (knex) { | ||
| await knex.schema.dropTable('peering_payments') | ||
|
|
||
| // Restore the previous confirm_invoice() before the purpose column disappears. | ||
| await knex.raw(` | ||
| CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE) | ||
| RETURNS INTEGER | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| payee BYTEA; | ||
| confirmed_date TIMESTAMP WITHOUT TIME ZONE; | ||
| invoice_unit TEXT; | ||
| BEGIN | ||
| PERFORM ASSERT_SERIALIZED(); | ||
|
|
||
| SELECT "pubkey", "confirmed_at", "unit" INTO payee, confirmed_date, invoice_unit FROM "invoices" WHERE id = invoice_id; | ||
| IF confirmed_date IS NULL THEN | ||
| UPDATE invoices | ||
| SET | ||
| "confirmed_at" = confirmation_date, | ||
| "amount_paid" = amount_received, | ||
| "updated_at" = now_utc() | ||
| WHERE id = invoice_id; | ||
| IF invoice_unit = 'sats' THEN | ||
| UPDATE users SET balance = balance + amount_received * 1000 WHERE "pubkey" = payee; | ||
| ELSIF invoice_unit = 'msats' THEN | ||
| UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee; | ||
| ELSIF invoice_unit = 'btc' THEN | ||
| UPDATE users SET balance = balance + amount_received * 100000000 * 1000 WHERE "pubkey" = payee; | ||
| END IF; | ||
| END IF; | ||
| RETURN 0; | ||
| END; | ||
| $$;`) | ||
|
|
||
| await knex.schema.alterTable('invoices', (table) => { | ||
| table.dropIndex(['purpose', 'status'], 'idx_invoices_purpose_status') | ||
| table.dropColumn('term_amount_msats') | ||
| table.dropColumn('term_period_seconds') | ||
| table.dropColumn('purpose') | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
peering.access.lapsedswitch cannot send an alert. Nothing enqueuesPEERING_ACCESS_LAPSEDor moves an ended peering period toLAPSED. Access checks deny the expired period, but operators who enable this alert never hear that the peer lost access.Connect the ended-period scan to a one-time state change and notification.
Knowledge Base Used: Notifications and maintenance