@integraledger/profile-facilitator 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/store.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type SettleAnswer } from "./answers.js";
1
+ import { type OperatorLog, type SettleAnswer } from "./answers.js";
2
2
  /** The table, and the index the sweep reads: the rows whose answer is final, by `until`. */
3
3
  export declare const SCHEMA = "CREATE TABLE IF NOT EXISTS settlement (\n network text NOT NULL,\n id text NOT NULL,\n answer jsonb,\n until timestamptz NOT NULL,\n since bigint,\n unread bigint[] NOT NULL DEFAULT '{}',\n PRIMARY KEY (network, id)\n);\nCREATE INDEX IF NOT EXISTS settlement_final_until ON settlement (until) WHERE answer IS NOT NULL AND answer->>'errorReason' IS DISTINCT FROM 'settlement_pending'";
4
4
  /** The bound on acquiring a connection, on each query as the client waits for it, and on each statement in the server. */
@@ -30,8 +30,6 @@ export interface SettlementStore {
30
30
  claim(network: string, id: string, until: Date, since?: number): Promise<boolean>;
31
31
  /** Writes the answer unless the row already holds a final one. True when this call wrote it. */
32
32
  answer(network: string, id: string, answer: SettleAnswer): Promise<boolean>;
33
- /** Removes a claim that has no answer: the claimant submitted nothing. True when a row was removed. */
34
- release(network: string, id: string): Promise<boolean>;
35
33
  /**
36
34
  * Moves `since` on to `to`, never back, after a scan that read every finalized block from `from` up to `to` except
37
35
  * `unread`, and read `resolved`, blocks of the stored `unread` it read again. The stored `unread` gains `unread`,
@@ -47,7 +45,11 @@ export interface SettlementStore {
47
45
  drop(): Promise<void>;
48
46
  close(): Promise<void>;
49
47
  }
50
- export declare function openStore(url: string): Promise<SettlementStore>;
48
+ /**
49
+ * Opens the pool and creates the table. `log` receives an error the pool reports for an idle connection, and a sweep
50
+ * that fails; the sweep runs again a minute later.
51
+ */
52
+ export declare function openStore(url: string, log?: OperatorLog): Promise<SettlementStore>;
51
53
  /**
52
54
  * The part of `settleWaitMs` kept for writing the answer once the wait for the chain ends: a second, or a quarter of a
53
55
  * shorter wait. The node calls and the waits end that much before the deadline, so the answer is stored, then given.
package/dist/store.js CHANGED
@@ -1,15 +1,17 @@
1
1
  /**
2
2
  * The deduplication table: one row per settled payment, keyed by network and transaction id, holding the answer the
3
- * first `/settle` gave. A row is claimed by one conditional insert, so concurrent settles of one payment submit once.
4
- * A final answer is never replaced; a pending one is replaced by what a later read of the chain shows. A row whose
5
- * answer is final is kept until `KEEP_AFTER_UNTIL_MS` after its validity window has ended, so a repeat in that time
6
- * reads the stored answer and the chain before anything is verified again. A row whose answer is not final (a claim
7
- * with no answer yet, or `settlement_pending`) is kept until a read of the chain makes it final, so a repeat never
8
- * finds its id missing and never answers that nothing was broadcast. Every call to the database is bounded by
9
- * `STORE_TIMEOUT_MS`.
3
+ * first `/settle` gave. A row is claimed by one conditional insert, so concurrent settles of one payment submit once,
4
+ * and a claim is never removed: a submission the node refuses, or that cannot have reached it, is stored as the row's
5
+ * answer. A final answer is never replaced; a pending one is replaced by what a later read of the chain shows, or by
6
+ * the stored answer to a broadcast or submission the node refused or never received. A row whose answer is final is
7
+ * kept until `KEEP_AFTER_UNTIL_MS` after its validity window has ended, so a repeat in that time reads the stored
8
+ * answer before the expiration check and the node reads are made again. A row whose answer is not final (a claim with
9
+ * no answer yet, or `settlement_pending`) is kept until a read of the chain, or the answer to a refused or unsent
10
+ * broadcast or submission, makes it final, so a repeat never finds its id missing. Every call to the database is
11
+ * bounded by `STORE_TIMEOUT_MS`.
10
12
  */
11
13
  import pg from "pg";
12
- import { isPending } from "./answers.js";
14
+ import { isPending, operatorLine } from "./answers.js";
13
15
  import { within } from "./http.js";
14
16
  /** A row's answer is final when it is written and is not `settlement_pending`. */
15
17
  const FINAL = "answer IS NOT NULL AND answer->>'errorReason' IS DISTINCT FROM 'settlement_pending'";
@@ -29,7 +31,11 @@ export const STORE_TIMEOUT_MS = 5_000;
29
31
  /** How long a row whose answer is final is kept after `until`. */
30
32
  export const KEEP_AFTER_UNTIL_MS = 24 * 60 * 60 * 1000;
31
33
  const DROP_EVERY_MS = 60_000;
32
- export async function openStore(url) {
34
+ /**
35
+ * Opens the pool and creates the table. `log` receives an error the pool reports for an idle connection, and a sweep
36
+ * that fails; the sweep runs again a minute later.
37
+ */
38
+ export async function openStore(url, log = operatorLine) {
33
39
  const pool = new pg.Pool({
34
40
  connectionString: url,
35
41
  max: 10,
@@ -37,7 +43,7 @@ export async function openStore(url) {
37
43
  query_timeout: STORE_TIMEOUT_MS,
38
44
  statement_timeout: STORE_TIMEOUT_MS,
39
45
  });
40
- pool.on("error", () => { });
46
+ pool.on("error", (e) => log({ event: "store-connection-error" }, e));
41
47
  await pool.query(SCHEMA);
42
48
  const store = {
43
49
  async read(network, id) {
@@ -60,10 +66,6 @@ export async function openStore(url) {
60
66
  AND (answer IS NULL OR answer->>'errorReason' = 'settlement_pending')${isPending(answer) ? " AND answer IS NULL" : ""}`, [network, id, JSON.stringify(answer)]);
61
67
  return r.rowCount === 1;
62
68
  },
63
- async release(network, id) {
64
- const r = await pool.query("DELETE FROM settlement WHERE network = $1 AND id = $2 AND answer IS NULL", [network, id]);
65
- return r.rowCount === 1;
66
- },
67
69
  async advance(network, id, scan) {
68
70
  await pool.query(`UPDATE settlement SET since = GREATEST(since, $3),
69
71
  unread = ARRAY(
@@ -83,7 +85,7 @@ export async function openStore(url) {
83
85
  },
84
86
  };
85
87
  const timer = setInterval(() => {
86
- store.drop().catch(() => { });
88
+ store.drop().catch((e) => log({ event: "store-call-failed", call: "sweep" }, e));
87
89
  }, DROP_EVERY_MS);
88
90
  timer.unref();
89
91
  return store;
package/dist/tron.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { type FacilitatorRequest, type SettleAnswer, type VerifyAnswer } from "./answers.js";
1
+ import { type FacilitatorRequest, type OperatorLog, type SettleAnswer, type VerifyAnswer } from "./answers.js";
2
2
  import { type SettlementStore } from "./store.js";
3
3
  export interface TronNode {
4
4
  network: `tron:${number}`;
5
5
  fullNode: string;
6
6
  solidityNode: string;
7
7
  }
8
- export declare function createTron(nodes: readonly TronNode[], s: SettlementStore, settleWaitMs: number): {
8
+ export declare function createTron(nodes: readonly TronNode[], s: SettlementStore, settleWaitMs: number, log: OperatorLog): {
9
9
  verify: (r: FacilitatorRequest) => Promise<VerifyAnswer>;
10
10
  settle: (r: FacilitatorRequest) => Promise<SettleAnswer>;
11
11
  networks: string[];