@ocap/tx-protocols 1.20.2 → 1.20.3

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/execute.js +39 -29
  3. package/package.json +26 -24
package/README.md CHANGED
@@ -5,7 +5,7 @@ Defines the interface of OCAP query resolver.
5
5
  ## Usage
6
6
 
7
7
  ```shell
8
- yarn add @ocap/tx-pipeline
8
+ pnpm install @ocap/tx-pipeline
9
9
  ```
10
10
 
11
11
  Then:
package/lib/execute.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable consistent-return */
2
2
  const get = require('lodash/get');
3
3
  const pick = require('lodash/pick');
4
+ const merge = require('lodash/merge');
4
5
  const camelCase = require('lodash/camelCase');
5
6
  const { CustomError: Error } = require('@ocap/util/lib/error');
6
7
  const { Runner, pipes } = require('@ocap/tx-pipeline');
@@ -159,54 +160,60 @@ module.exports = ({ filter, runAsLambda }) => {
159
160
  const startTime = Date.now();
160
161
 
161
162
  try {
162
- await runAsLambda(async (txn) => {
163
- // create a new context each time in case we are retrying
164
- ctx = pick(context, ['txBase64', 'statedb', 'indexdb', 'config', 'states', 'filter', 'extra', 'logger']);
165
- ctx.txn = txn;
163
+ const txState = await runAsLambda(
164
+ async (txn) => {
165
+ // create a new context each time in case we are retrying
166
+ ctx = pick(context, ['txBase64', 'statedb', 'indexdb', 'config', 'states', 'filter', 'extra', 'logger']);
167
+ ctx.txn = txn;
166
168
 
167
- await execute(ctx, protocols, true);
169
+ await execute(ctx, protocols, true);
168
170
 
169
- const txState = context.states.tx.create(ctx, 'OK');
170
- await context.statedb.tx.create(txState.hash, txState, ctx);
171
+ const state = context.states.tx.create(ctx, 'OK');
172
+ await context.statedb.tx.create(state.hash, state, ctx);
171
173
 
172
- // update indexdb
173
- flushEvents(ctx, { txState });
174
+ return state;
175
+ },
176
+ { cleanWorkingSet: true, verifyHash: true }
177
+ );
174
178
 
175
- ctx.logger?.info('Tx finalized', {
176
- txHash: ctx.txHash,
177
- txState,
178
- txStatus: 'OK',
179
- duration: Date.now() - startTime,
180
- });
179
+ // update indexdb after statedb commit
180
+ flushEvents(ctx, { txState });
181
+
182
+ ctx.logger?.info('Tx finalized', {
183
+ txHash: ctx.txHash,
184
+ txState,
185
+ txStatus: 'OK',
186
+ duration: Date.now() - startTime,
181
187
  });
182
188
  } catch (error) {
183
189
  const txStatus = error.code || 'INTERNAL';
184
- let txState = context.states.tx.create(ctx, txStatus, false);
190
+ let txState = ctx.tx ? context.states.tx.create(ctx, txStatus, false) : null;
185
191
 
186
192
  ctx.logger?.error('Failed to execute transaction', { error, txHash: ctx.txHash, txState });
187
193
 
188
- if (shouldPersistTx(error)) {
194
+ if (txState && shouldPersistTx(error)) {
189
195
  try {
190
- await runAsLambda(async (txn) => {
196
+ txState = await runAsLambda(async (txn) => {
191
197
  ctx = Object.assign({}, ctx, { txn, cacheStates: null });
192
198
 
193
199
  await ensureGasFeePaid(ctx);
194
200
 
195
201
  // Recreate tx to pick up gas related fields
196
- txState = context.states.tx.create(ctx, txStatus);
202
+ const state = context.states.tx.create(ctx, txStatus);
203
+ await ctx.statedb.tx.create(state.hash, state, ctx);
197
204
 
198
- await ctx.statedb.tx.create(txState.hash, txState, ctx);
205
+ return state;
206
+ });
199
207
 
200
- // update indexdb
201
- flushEvents(ctx, { txState });
208
+ // update indexdb after statedb commit
209
+ flushEvents(ctx, { txState });
202
210
 
203
- ctx.logger?.info('Tx finalized', {
204
- txHash: ctx.txHash,
205
- txState,
206
- txStatus,
207
- error,
208
- duration: Date.now() - startTime,
209
- });
211
+ ctx.logger?.info('Tx finalized', {
212
+ txHash: ctx.txHash,
213
+ txState,
214
+ txStatus,
215
+ error,
216
+ duration: Date.now() - startTime,
210
217
  });
211
218
  } catch (err) {
212
219
  ctx.logger?.error('Failed to save invalid transaction to statedb', {
@@ -221,6 +228,9 @@ module.exports = ({ filter, runAsLambda }) => {
221
228
  }
222
229
 
223
230
  throw error;
231
+ } finally {
232
+ // Merge new ctx to original context
233
+ merge(context, ctx);
224
234
  }
225
235
 
226
236
  return ctx;
package/package.json CHANGED
@@ -3,41 +3,36 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.20.2",
6
+ "version": "1.20.3",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
10
10
  "lib"
11
11
  ],
12
- "scripts": {
13
- "lint": "eslint tests lib",
14
- "lint:fix": "eslint --fix tests lib",
15
- "start": "node tools/start-chain.js",
16
- "test": "jest --forceExit --detectOpenHandles",
17
- "test:ci": "jest --forceExit --detectOpenHandles --coverage",
18
- "coverage": "start-server-and-test start http://127.0.0.1:4001 test:ci"
19
- },
20
12
  "keywords": [],
21
13
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
22
14
  "license": "MIT",
23
15
  "dependencies": {
24
- "@arcblock/did": "1.20.2",
25
- "@arcblock/did-util": "1.20.2",
26
- "@arcblock/jwt": "1.20.2",
27
- "@arcblock/validator": "1.20.2",
28
- "@ocap/asset": "1.20.2",
29
- "@ocap/mcrypto": "1.20.2",
30
- "@ocap/merkle-tree": "1.20.2",
31
- "@ocap/message": "1.20.2",
32
- "@ocap/state": "1.20.2",
33
- "@ocap/tx-pipeline": "1.20.2",
34
- "@ocap/util": "1.20.2",
35
- "@ocap/wallet": "1.20.2",
36
16
  "debug": "^4.3.6",
37
17
  "deep-diff": "^1.0.2",
38
18
  "empty-value": "^1.0.1",
39
19
  "lodash": "^4.17.21",
40
- "url-join": "^4.0.1"
20
+ "url-join": "^4.0.1",
21
+ "@arcblock/did": "1.20.3",
22
+ "@arcblock/did-util": "1.20.3",
23
+ "@arcblock/jwt": "1.20.3",
24
+ "@arcblock/validator": "1.20.3",
25
+ "@ocap/asset": "1.20.3",
26
+ "@ocap/mcrypto": "1.20.3",
27
+ "@ocap/merkle-tree": "1.20.3",
28
+ "@ocap/message": "1.20.3",
29
+ "@ocap/state": "1.20.3",
30
+ "@ocap/util": "1.20.3",
31
+ "@ocap/wallet": "1.20.3",
32
+ "@ocap/tx-pipeline": "1.20.3",
33
+ "@ocap/e2e-test": "1.20.3",
34
+ "@ocap/client": "1.20.3",
35
+ "@ocap/statedb-memory": "1.20.3"
41
36
  },
42
37
  "resolutions": {
43
38
  "bn.js": "5.2.1",
@@ -47,5 +42,12 @@
47
42
  "jest": "^29.7.0",
48
43
  "start-server-and-test": "^1.14.0"
49
44
  },
50
- "gitHead": "70ddd9b43070605879fed7dff50a9f19f5325ea8"
51
- }
45
+ "scripts": {
46
+ "lint": "eslint tests lib",
47
+ "lint:fix": "eslint --fix tests lib",
48
+ "start": "node tools/start-chain.js",
49
+ "test": "jest --forceExit --detectOpenHandles",
50
+ "test:ci": "jest --forceExit --detectOpenHandles --coverage",
51
+ "coverage": "start-server-and-test start http://127.0.0.1:4001 test:ci"
52
+ }
53
+ }