@ocap/tx-protocols 1.29.7 → 1.29.8

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/esm/execute.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TxTimer } from "./tx-timer.mjs";
1
2
  import { CustomError } from "@ocap/util/lib/error";
2
3
  import { Blacklist } from "@ocap/state";
3
4
  import { IAccountState, IGasContext } from "@ocap/types";
@@ -31,6 +32,8 @@ interface IExecutionContext extends IGasContext {
31
32
  ledgerSequence?: number;
32
33
  /** State commit hash set by onCommit callback (Dolt only) */
33
34
  stateCommitHash?: string;
35
+ /** Transaction execution timer */
36
+ timer?: TxTimer;
34
37
  }
35
38
  /** Protocol runner interface */
36
39
  interface IProtocolRunner {
package/esm/execute.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import ensure_cost_default from "./pipes/ensure-cost.mjs";
2
2
  import ensure_gas_default from "./pipes/ensure-gas.mjs";
3
3
  import { WriteLedger } from "./pipes/write-ledger.mjs";
4
+ import { TxTimer } from "./tx-timer.mjs";
4
5
  import { Runner, pipes } from "@ocap/tx-pipeline";
5
6
  import { CustomError } from "@ocap/util/lib/error";
6
7
  import camelCase from "lodash/camelCase.js";
@@ -101,6 +102,7 @@ const createExecute = ({ filter, runAsLambda }) => {
101
102
  const protocol = protocols[getTxName(context.txType)];
102
103
  if (!protocol) return reject(new CustomError("UNSUPPORTED_TX", `Unsupported tx type ${context.txType}`));
103
104
  protocol.run(context, async (error) => {
105
+ context.timer?.mark("protocol");
104
106
  if (isRetrySupported) {
105
107
  if (error) return reject(error);
106
108
  return resolve(context);
@@ -114,7 +116,9 @@ const createExecute = ({ filter, runAsLambda }) => {
114
116
  txState = states.tx.create(context, txStatus);
115
117
  }
116
118
  await context.statedb.tx.create(txState.hash, txState, context);
119
+ context.timer?.mark("commit");
117
120
  flushEvents(context, { txState });
121
+ context.timer?.mark("indexdb");
118
122
  } catch (e) {
119
123
  context.logger?.error("Failed to save transaction to statedb", {
120
124
  error: e,
@@ -128,7 +132,8 @@ const createExecute = ({ filter, runAsLambda }) => {
128
132
  txHash: context.txHash,
129
133
  txStatus,
130
134
  txState,
131
- error
135
+ error,
136
+ timing: context.timer?.summary()
132
137
  });
133
138
  if (error) return reject(error);
134
139
  return resolve(context);
@@ -137,7 +142,7 @@ const createExecute = ({ filter, runAsLambda }) => {
137
142
  });
138
143
  if (typeof runAsLambda === "function") return async (context, protocols) => {
139
144
  let ctx = context;
140
- const startTime = Date.now();
145
+ context.timer = new TxTimer();
141
146
  try {
142
147
  const txState = await runAsLambda(async (txn) => {
143
148
  ctx = pick(context, [
@@ -150,7 +155,8 @@ const createExecute = ({ filter, runAsLambda }) => {
150
155
  "filter",
151
156
  "extra",
152
157
  "logger",
153
- "ledgerSequence"
158
+ "ledgerSequence",
159
+ "timer"
154
160
  ]);
155
161
  ctx.txn = txn;
156
162
  await execute(ctx, protocols, true);
@@ -174,7 +180,9 @@ const createExecute = ({ filter, runAsLambda }) => {
174
180
  }
175
181
  }
176
182
  });
183
+ ctx.timer?.mark("commit");
177
184
  flushEvents(ctx, { txState });
185
+ ctx.timer?.mark("indexdb");
178
186
  ctx.ledger?.createCheckpointIfNeeded()?.catch((err) => {
179
187
  ctx.logger?.error("Background checkpoint creation failed", {
180
188
  txHash: ctx.txHash,
@@ -185,7 +193,7 @@ const createExecute = ({ filter, runAsLambda }) => {
185
193
  txHash: ctx.txHash,
186
194
  txState,
187
195
  txStatus: "OK",
188
- duration: Date.now() - startTime
196
+ timing: ctx.timer?.summary()
189
197
  });
190
198
  } catch (error) {
191
199
  const err = error;
@@ -226,7 +234,9 @@ const createExecute = ({ filter, runAsLambda }) => {
226
234
  }
227
235
  }
228
236
  });
237
+ ctx.timer?.mark("commit");
229
238
  flushEvents(ctx, { txState });
239
+ ctx.timer?.mark("indexdb");
230
240
  ctx.ledger?.createCheckpointIfNeeded()?.catch((err$1) => {
231
241
  ctx.logger?.error("Background checkpoint creation failed", {
232
242
  txHash: ctx.txHash,
@@ -238,7 +248,7 @@ const createExecute = ({ filter, runAsLambda }) => {
238
248
  txState,
239
249
  txStatus,
240
250
  error,
241
- duration: Date.now() - startTime
251
+ timing: ctx.timer?.summary()
242
252
  });
243
253
  } catch (innerErr) {
244
254
  ctx.logger?.error("Failed to save invalid transaction to statedb", {
@@ -258,6 +268,7 @@ const createExecute = ({ filter, runAsLambda }) => {
258
268
  return ctx;
259
269
  };
260
270
  return async (context, protocols) => {
271
+ context.timer = new TxTimer();
261
272
  const result = await execute(context, protocols);
262
273
  if (result.ledger && result.txHash && result.ledgerSequence && result.txBase64) try {
263
274
  await result.ledger.finalizeEntry(result.txHash, "", result.ledgerSequence, result.txBase64);
@@ -267,6 +278,7 @@ const createExecute = ({ filter, runAsLambda }) => {
267
278
  error: err
268
279
  });
269
280
  }
281
+ result.timer?.mark("finalize");
270
282
  result.ledger?.createCheckpointIfNeeded()?.catch((err) => {
271
283
  result.logger?.error("Background checkpoint creation failed", {
272
284
  txHash: result.txHash,
@@ -47,6 +47,10 @@ interface IWriteLedgerContext {
47
47
  };
48
48
  [key: string]: unknown;
49
49
  };
50
+ /** Transaction execution timer */
51
+ timer?: {
52
+ mark(label: string): void;
53
+ };
50
54
  }
51
55
  /**
52
56
  * Error codes for ledger write failures
@@ -70,7 +70,9 @@ const WriteLedger = async (context, next) => {
70
70
  timestamp: txTime ? new Date(txTime).getTime() : Date.now(),
71
71
  txExtra
72
72
  };
73
+ context.timer?.mark("verify");
73
74
  const entry = await ledger.append(txInput);
75
+ context.timer?.mark("ledger");
74
76
  context.ledgerSequence = entry.sequence;
75
77
  debug("Transaction written to ledger", {
76
78
  txHash,
@@ -0,0 +1,17 @@
1
+ //#region src/tx-timer.d.ts
2
+ /**
3
+ * Lightweight transaction execution timer.
4
+ *
5
+ * Records named marks at key stages and computes per-stage durations.
6
+ * Overhead per mark() call is a single Date.now() invocation (< 1 µs).
7
+ */
8
+ declare class TxTimer {
9
+ private marks;
10
+ private origin;
11
+ /** Record a named timestamp. */
12
+ mark(label: string): void;
13
+ /** Compute per-stage durations (ms) between consecutive marks. */
14
+ summary(): Record<string, number>;
15
+ }
16
+ //#endregion
17
+ export { TxTimer };
@@ -0,0 +1,31 @@
1
+ //#region src/tx-timer.ts
2
+ /**
3
+ * Lightweight transaction execution timer.
4
+ *
5
+ * Records named marks at key stages and computes per-stage durations.
6
+ * Overhead per mark() call is a single Date.now() invocation (< 1 µs).
7
+ */
8
+ var TxTimer = class {
9
+ constructor() {
10
+ this.marks = [];
11
+ this.origin = Date.now();
12
+ }
13
+ /** Record a named timestamp. */
14
+ mark(label) {
15
+ this.marks.push([label, Date.now()]);
16
+ }
17
+ /** Compute per-stage durations (ms) between consecutive marks. */
18
+ summary() {
19
+ const result = {};
20
+ let prev = this.origin;
21
+ for (const [label, time] of this.marks) {
22
+ result[label] = time - prev;
23
+ prev = time;
24
+ }
25
+ result.total = prev - this.origin;
26
+ return result;
27
+ }
28
+ };
29
+
30
+ //#endregion
31
+ export { TxTimer };
package/lib/execute.cjs CHANGED
@@ -3,6 +3,7 @@ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
3
3
  const require_pipes_ensure_cost = require('./pipes/ensure-cost.cjs');
4
4
  const require_pipes_ensure_gas = require('./pipes/ensure-gas.cjs');
5
5
  const require_pipes_write_ledger = require('./pipes/write-ledger.cjs');
6
+ const require_tx_timer = require('./tx-timer.cjs');
6
7
  let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
7
8
  let _ocap_util_lib_error = require("@ocap/util/lib/error");
8
9
  let lodash_camelCase = require("lodash/camelCase");
@@ -107,6 +108,7 @@ const createExecute = ({ filter, runAsLambda }) => {
107
108
  const protocol = protocols[getTxName(context.txType)];
108
109
  if (!protocol) return reject(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", `Unsupported tx type ${context.txType}`));
109
110
  protocol.run(context, async (error) => {
111
+ context.timer?.mark("protocol");
110
112
  if (isRetrySupported) {
111
113
  if (error) return reject(error);
112
114
  return resolve(context);
@@ -120,7 +122,9 @@ const createExecute = ({ filter, runAsLambda }) => {
120
122
  txState = states.tx.create(context, txStatus);
121
123
  }
122
124
  await context.statedb.tx.create(txState.hash, txState, context);
125
+ context.timer?.mark("commit");
123
126
  flushEvents(context, { txState });
127
+ context.timer?.mark("indexdb");
124
128
  } catch (e) {
125
129
  context.logger?.error("Failed to save transaction to statedb", {
126
130
  error: e,
@@ -134,7 +138,8 @@ const createExecute = ({ filter, runAsLambda }) => {
134
138
  txHash: context.txHash,
135
139
  txStatus,
136
140
  txState,
137
- error
141
+ error,
142
+ timing: context.timer?.summary()
138
143
  });
139
144
  if (error) return reject(error);
140
145
  return resolve(context);
@@ -143,7 +148,7 @@ const createExecute = ({ filter, runAsLambda }) => {
143
148
  });
144
149
  if (typeof runAsLambda === "function") return async (context, protocols) => {
145
150
  let ctx = context;
146
- const startTime = Date.now();
151
+ context.timer = new require_tx_timer.TxTimer();
147
152
  try {
148
153
  const txState = await runAsLambda(async (txn) => {
149
154
  ctx = (0, lodash_pick.default)(context, [
@@ -156,7 +161,8 @@ const createExecute = ({ filter, runAsLambda }) => {
156
161
  "filter",
157
162
  "extra",
158
163
  "logger",
159
- "ledgerSequence"
164
+ "ledgerSequence",
165
+ "timer"
160
166
  ]);
161
167
  ctx.txn = txn;
162
168
  await execute(ctx, protocols, true);
@@ -180,7 +186,9 @@ const createExecute = ({ filter, runAsLambda }) => {
180
186
  }
181
187
  }
182
188
  });
189
+ ctx.timer?.mark("commit");
183
190
  flushEvents(ctx, { txState });
191
+ ctx.timer?.mark("indexdb");
184
192
  ctx.ledger?.createCheckpointIfNeeded()?.catch((err) => {
185
193
  ctx.logger?.error("Background checkpoint creation failed", {
186
194
  txHash: ctx.txHash,
@@ -191,7 +199,7 @@ const createExecute = ({ filter, runAsLambda }) => {
191
199
  txHash: ctx.txHash,
192
200
  txState,
193
201
  txStatus: "OK",
194
- duration: Date.now() - startTime
202
+ timing: ctx.timer?.summary()
195
203
  });
196
204
  } catch (error) {
197
205
  const err = error;
@@ -232,7 +240,9 @@ const createExecute = ({ filter, runAsLambda }) => {
232
240
  }
233
241
  }
234
242
  });
243
+ ctx.timer?.mark("commit");
235
244
  flushEvents(ctx, { txState });
245
+ ctx.timer?.mark("indexdb");
236
246
  ctx.ledger?.createCheckpointIfNeeded()?.catch((err$1) => {
237
247
  ctx.logger?.error("Background checkpoint creation failed", {
238
248
  txHash: ctx.txHash,
@@ -244,7 +254,7 @@ const createExecute = ({ filter, runAsLambda }) => {
244
254
  txState,
245
255
  txStatus,
246
256
  error,
247
- duration: Date.now() - startTime
257
+ timing: ctx.timer?.summary()
248
258
  });
249
259
  } catch (innerErr) {
250
260
  ctx.logger?.error("Failed to save invalid transaction to statedb", {
@@ -264,6 +274,7 @@ const createExecute = ({ filter, runAsLambda }) => {
264
274
  return ctx;
265
275
  };
266
276
  return async (context, protocols) => {
277
+ context.timer = new require_tx_timer.TxTimer();
267
278
  const result = await execute(context, protocols);
268
279
  if (result.ledger && result.txHash && result.ledgerSequence && result.txBase64) try {
269
280
  await result.ledger.finalizeEntry(result.txHash, "", result.ledgerSequence, result.txBase64);
@@ -273,6 +284,7 @@ const createExecute = ({ filter, runAsLambda }) => {
273
284
  error: err
274
285
  });
275
286
  }
287
+ result.timer?.mark("finalize");
276
288
  result.ledger?.createCheckpointIfNeeded()?.catch((err) => {
277
289
  result.logger?.error("Background checkpoint creation failed", {
278
290
  txHash: result.txHash,
package/lib/execute.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TxTimer } from "./tx-timer.cjs";
1
2
  import { IAccountState, IGasContext } from "@ocap/types";
2
3
  import { CustomError } from "@ocap/util/lib/error";
3
4
  import { Blacklist } from "@ocap/state";
@@ -31,6 +32,8 @@ interface IExecutionContext extends IGasContext {
31
32
  ledgerSequence?: number;
32
33
  /** State commit hash set by onCommit callback (Dolt only) */
33
34
  stateCommitHash?: string;
35
+ /** Transaction execution timer */
36
+ timer?: TxTimer;
34
37
  }
35
38
  /** Protocol runner interface */
36
39
  interface IProtocolRunner {
@@ -73,7 +73,9 @@ const WriteLedger = async (context, next) => {
73
73
  timestamp: txTime ? new Date(txTime).getTime() : Date.now(),
74
74
  txExtra
75
75
  };
76
+ context.timer?.mark("verify");
76
77
  const entry = await ledger.append(txInput);
78
+ context.timer?.mark("ledger");
77
79
  context.ledgerSequence = entry.sequence;
78
80
  debug$1("Transaction written to ledger", {
79
81
  txHash,
@@ -47,6 +47,10 @@ interface IWriteLedgerContext {
47
47
  };
48
48
  [key: string]: unknown;
49
49
  };
50
+ /** Transaction execution timer */
51
+ timer?: {
52
+ mark(label: string): void;
53
+ };
50
54
  }
51
55
  /**
52
56
  * Error codes for ledger write failures
@@ -0,0 +1,32 @@
1
+
2
+ //#region src/tx-timer.ts
3
+ /**
4
+ * Lightweight transaction execution timer.
5
+ *
6
+ * Records named marks at key stages and computes per-stage durations.
7
+ * Overhead per mark() call is a single Date.now() invocation (< 1 µs).
8
+ */
9
+ var TxTimer = class {
10
+ constructor() {
11
+ this.marks = [];
12
+ this.origin = Date.now();
13
+ }
14
+ /** Record a named timestamp. */
15
+ mark(label) {
16
+ this.marks.push([label, Date.now()]);
17
+ }
18
+ /** Compute per-stage durations (ms) between consecutive marks. */
19
+ summary() {
20
+ const result = {};
21
+ let prev = this.origin;
22
+ for (const [label, time] of this.marks) {
23
+ result[label] = time - prev;
24
+ prev = time;
25
+ }
26
+ result.total = prev - this.origin;
27
+ return result;
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ exports.TxTimer = TxTimer;
@@ -0,0 +1,17 @@
1
+ //#region src/tx-timer.d.ts
2
+ /**
3
+ * Lightweight transaction execution timer.
4
+ *
5
+ * Records named marks at key stages and computes per-stage durations.
6
+ * Overhead per mark() call is a single Date.now() invocation (< 1 µs).
7
+ */
8
+ declare class TxTimer {
9
+ private marks;
10
+ private origin;
11
+ /** Record a named timestamp. */
12
+ mark(label: string): void;
13
+ /** Compute per-stage durations (ms) between consecutive marks. */
14
+ summary(): Record<string, number>;
15
+ }
16
+ //#endregion
17
+ export { TxTimer };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.29.7",
6
+ "version": "1.29.8",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -46,22 +46,22 @@
46
46
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
47
47
  "license": "MIT",
48
48
  "dependencies": {
49
- "@arcblock/did": "1.29.7",
50
- "@arcblock/did-util": "1.29.7",
51
- "@arcblock/jwt": "1.29.7",
52
- "@arcblock/validator": "1.29.7",
53
- "@arcblock/vc": "1.29.7",
49
+ "@arcblock/did": "1.29.8",
50
+ "@arcblock/did-util": "1.29.8",
51
+ "@arcblock/jwt": "1.29.8",
52
+ "@arcblock/validator": "1.29.8",
53
+ "@arcblock/vc": "1.29.8",
54
54
  "@blocklet/xss": "^0.3.7",
55
- "@ocap/asset": "1.29.7",
56
- "@ocap/client": "1.29.7",
57
- "@ocap/mcrypto": "1.29.7",
58
- "@ocap/merkle-tree": "1.29.7",
59
- "@ocap/message": "1.29.7",
60
- "@ocap/state": "1.29.7",
61
- "@ocap/tx-pipeline": "1.29.7",
62
- "@ocap/types": "1.29.7",
63
- "@ocap/util": "1.29.7",
64
- "@ocap/wallet": "1.29.7",
55
+ "@ocap/asset": "1.29.8",
56
+ "@ocap/client": "1.29.8",
57
+ "@ocap/mcrypto": "1.29.8",
58
+ "@ocap/merkle-tree": "1.29.8",
59
+ "@ocap/message": "1.29.8",
60
+ "@ocap/state": "1.29.8",
61
+ "@ocap/tx-pipeline": "1.29.8",
62
+ "@ocap/types": "1.29.8",
63
+ "@ocap/util": "1.29.8",
64
+ "@ocap/wallet": "1.29.8",
65
65
  "debug": "^4.4.3",
66
66
  "deep-diff": "^1.0.2",
67
67
  "lodash": "^4.17.23",
@@ -72,8 +72,8 @@
72
72
  "elliptic": "6.5.3"
73
73
  },
74
74
  "devDependencies": {
75
- "@ocap/e2e-test": "1.29.7",
76
- "@ocap/statedb-memory": "1.29.7",
75
+ "@ocap/e2e-test": "1.29.8",
76
+ "@ocap/statedb-memory": "1.29.8",
77
77
  "@types/debug": "^4.1.12",
78
78
  "@types/lodash": "^4.17.16",
79
79
  "start-server-and-test": "^1.14.0"