@ocap/tx-protocols 1.13.66 → 1.13.70

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/lib/execute.js CHANGED
@@ -29,7 +29,7 @@ module.exports = ({ filter, runAsLambda }) => {
29
29
  // Since bloom-filters may give false positive, we need to check for statedb if replay detected
30
30
  const exist = await context.statedb.tx.get(context.txHash, context);
31
31
  if (exist) {
32
- return next(new Error('FORBIDDEN', 'Can not execute duplicate transaction'));
32
+ return next(new Error('DUPLICATE_TX', 'Can not execute duplicate transaction'));
33
33
  }
34
34
  }
35
35
 
@@ -82,7 +82,7 @@ runner.use(
82
82
  );
83
83
 
84
84
  // 5. verify sender & signer & owner
85
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })); // prettier-ignore
85
+ runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' })); // prettier-ignore
86
86
  runner.use(pipes.ExtractState({ from: 'senders', to: 'signerStates', status: 'INVALID_SIGNER_STATE', table: 'account' })); // prettier-ignore
87
87
  runner.use(pipes.ExtractState({ from: 'itx.owner', to: 'ownerState', status: 'OK', table: 'account' }));
88
88
  runner.use(pipes.VerifyAccountMigration({ signerKey: 'signerStates', senderKey: 'senderState' }));
@@ -152,7 +152,7 @@ runner.use(
152
152
  );
153
153
  });
154
154
 
155
- const isAlsoSigner = !!signerUpdates[senderState.address];
155
+ const isAlsoSigner = !!signerUpdates[tx.from];
156
156
  const owner = ownerState ? ownerState.address : itx.owner;
157
157
 
158
158
  // Factory updates
@@ -166,12 +166,12 @@ runner.use(
166
166
 
167
167
  const [newSenderState, newReceiverState, assetState, newSignerStates, newFactoryState, newAssetStates] =
168
168
  await Promise.all([
169
- // Update sender state
170
- statedb.account.update(
171
- senderState.address,
172
- account.update(
169
+ // update sender
170
+ statedb.account.updateOrCreate(
171
+ senderState,
172
+ account.updateOrCreate(
173
173
  senderState,
174
- Object.assign({ nonce: tx.nonce, pk: tx.pk }, signerUpdates[senderState.address] || {}),
174
+ Object.assign({ address: tx.from, nonce: tx.nonce, pk: tx.pk }, signerUpdates[tx.from] || {}),
175
175
  context
176
176
  ),
177
177
  context
@@ -192,7 +192,7 @@ runner.use(
192
192
  // Update signer state
193
193
  Promise.all(
194
194
  signerStates
195
- .filter((x) => x.address !== senderState.address)
195
+ .filter((x) => x.address !== tx.from)
196
196
  .map((x) =>
197
197
  statedb.account.update(x.address, account.update(x, signerUpdates[x.address], context), context)
198
198
  )
@@ -77,7 +77,7 @@ runner.use(
77
77
  // For security consideration, user can not create fake assets from a factory
78
78
  // https://github.com/ArcBlock/asset-chain/issues/97
79
79
  {
80
- error: 'FORBIDDEN',
80
+ error: 'CREATE_FROM_FACTORY',
81
81
  message: 'You can only get asset from factory from acquire or mint',
82
82
  fn: ({ itx, factoryState }) => !(itx.parent && factoryState),
83
83
  persist: true,
@@ -31,7 +31,7 @@ runner.use(pipes.ExtractState({ from: 'itx.owner', to: 'ownerState', status: 'OK
31
31
  runner.use(
32
32
  pipes.VerifyInfo([
33
33
  {
34
- error: 'FORBIDDEN',
34
+ error: 'OWNER_ONLY_OPERATION',
35
35
  message: 'Only factory owner is allowed to mint',
36
36
  fn: ({ factoryState, senderState }) => factoryState.owner === senderState.address,
37
37
  persist: true,
@@ -9,5 +9,5 @@ module.exports = ({ factoryState }, next) => {
9
9
  return next();
10
10
  }
11
11
 
12
- return next(new Error('FORBIDDEN', 'This request will exceed factory mint limit'));
12
+ return next(new Error('EXCEED_MINT_LIMIT', 'This request will exceed factory mint limit'));
13
13
  };
@@ -72,7 +72,7 @@ runner.use(pipes.ExtractState({ from: 'itx.address', to: 'factoryState', status:
72
72
  runner.use(
73
73
  pipes.VerifyInfo([
74
74
  {
75
- error: 'FORBIDDEN',
75
+ error: 'DUPLICATE_FACTORY',
76
76
  message: 'This asset factory already exist on chain',
77
77
  fn: (context) => isEmpty(context.factoryState),
78
78
  },
@@ -31,7 +31,7 @@ runner.use(pipes.ExtractState({ from: 'itx.address', to: 'stakeState', status: '
31
31
  runner.use(
32
32
  pipes.VerifyInfo([
33
33
  {
34
- error: 'FORBIDDEN',
34
+ error: 'SENDER_NOT_MATCH',
35
35
  message: 'You are not allowed to claim stake from this address',
36
36
  fn: ({ tx, stakeState }) => tx.from === stakeState.sender,
37
37
  },
@@ -43,7 +43,7 @@ runner.use(pipes.ExtractState({ from: 'itx.evidence.hash', to: 'evidenceState',
43
43
  runner.use(
44
44
  pipes.VerifyInfo([
45
45
  {
46
- error: 'FORBIDDEN',
46
+ error: 'ALREADY_CLAIMED',
47
47
  message: 'Revoke evidence already seen on this chain',
48
48
  fn: ({ evidenceState }) => !evidenceState,
49
49
  },
@@ -70,7 +70,7 @@ runner.use(
70
70
  fn: ({ txState, itx }) => txState.tx.itxJson.address === itx.address,
71
71
  },
72
72
  {
73
- error: 'FORBIDDEN',
73
+ error: 'WITHIN_WAITING_PERIOD',
74
74
  message: 'You can not claim stake before waiting period ends',
75
75
  fn: ({ txState, stakeState, txTime }) => {
76
76
  const end = +new Date(txState.time) + (stakeState.revokeWaitingPeriod || 0) * 1000;
@@ -49,12 +49,12 @@ runner.use(
49
49
  runner.use(
50
50
  pipes.VerifyInfo([
51
51
  {
52
- error: 'FORBIDDEN',
52
+ error: 'SENDER_NOT_MATCH',
53
53
  message: 'You are not allowed to revoke stake from this address',
54
54
  fn: ({ tx, stakeState }) => tx.from === stakeState.sender,
55
55
  },
56
56
  {
57
- error: 'FORBIDDEN',
57
+ error: 'STAKE_NOT_REVOCABLE',
58
58
  message: 'This stake address is not revocable',
59
59
  fn: ({ stakeState }) => stakeState.revocable,
60
60
  },
@@ -64,9 +64,9 @@ runner.use(pipes.VerifyMultiSigV2({ signersKey: 'senders' }));
64
64
  runner.use(pipes.ExtractState({ from: 'itx.address', to: 'stakeState', status: 'OK', table: 'stake' }));
65
65
 
66
66
  // 5. verify sender & signer & receiver
67
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE' }));
68
- runner.use(pipes.ExtractState({ from: 'senders', to: 'signerStates', status: 'INVALID_SIGNER_STATE' }));
69
- runner.use(pipes.ExtractState({ from: 'itx.receiver', to: 'receiverState', status: 'INVALID_RECEIVER_STATE' }));
67
+ runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' }));
68
+ runner.use(pipes.ExtractState({ from: 'senders', to: 'signerStates', status: 'INVALID_SIGNER_STATE', table: 'account' })); // prettier-ignore
69
+ runner.use(pipes.ExtractState({ from: 'itx.receiver', to: 'receiverState', status: 'INVALID_RECEIVER_STATE' })); // can by any type
70
70
  runner.use(pipes.VerifyAccountMigration({ signerKey: 'signerStates', senderKey: 'senderState' }));
71
71
 
72
72
  // 6. verify token state and balance
@@ -115,15 +115,15 @@ runner.use(
115
115
  stakeUpdates.assets.push(...assetsList);
116
116
  });
117
117
 
118
- const isAlsoSigner = !!signerUpdates[senderState.address];
118
+ const isAlsoSigner = !!signerUpdates[tx.from];
119
119
 
120
120
  const [newSenderState, newSignerStates, newStakeState, newAssetStates] = await Promise.all([
121
121
  // Update sender state
122
- statedb.account.update(
123
- senderState.address,
124
- account.update(
122
+ statedb.account.updateOrCreate(
123
+ senderState,
124
+ account.updateOrCreate(
125
125
  senderState,
126
- Object.assign({ nonce: tx.nonce, pk: tx.pk }, signerUpdates[senderState.address] || {}),
126
+ Object.assign({ address: tx.from, nonce: tx.nonce, pk: tx.pk }, signerUpdates[tx.from] || {}),
127
127
  context
128
128
  ),
129
129
  context
@@ -132,7 +132,7 @@ runner.use(
132
132
  // Update signer state
133
133
  Promise.all(
134
134
  signerStates
135
- .filter((x) => x.address !== senderState.address)
135
+ .filter((x) => x.address !== tx.from)
136
136
  .map((x) => statedb.account.update(x.address, account.update(x, signerUpdates[x.address], context), context))
137
137
  ),
138
138
 
@@ -264,7 +264,10 @@ runner.use(
264
264
  ),
265
265
  ]);
266
266
 
267
- context.stakeStates = newStakeStates;
267
+ // to avoid this to be incorrectly indexed
268
+ delete context.stakeState;
269
+
270
+ context.stakeStates = newStakeStates.filter((x) => updates.stake[x.address]);
268
271
  context.accountStates = newAccountStates;
269
272
  context.blockState = newBlockState;
270
273
 
@@ -252,70 +252,74 @@ runner.use(
252
252
  const { tx, itx, statedb, rollupState, senderState, stakeStates, txStates, formattedItx } = context;
253
253
  const senderStates = context.senderStates || [];
254
254
 
255
- const [newSenderState, newStakeStates, newSenderStates, newRollupState, rollupBlockState] = await Promise.all([
256
- // update sender(proposer) account
257
- statedb.account.update(
258
- senderState.address,
259
- account.update(senderState, Object.assign({ nonce: tx.nonce }), context),
260
- context
261
- ),
262
-
263
- // update stake states
264
- Promise.all(
265
- stakeStates.map((x) =>
266
- context.stakeUpdates[x.address]
267
- ? statedb.stake.update(
268
- x.address,
269
- stake.update(x, applyTokenChange(x, context.stakeUpdates[x.address]), context),
270
- context
271
- )
272
- : Promise.resolve(x)
273
- )
274
- ),
275
-
276
- // update tx owner states: those may get refund
277
- Promise.all(
278
- senderStates.map((x) =>
279
- statedb.account.update(
280
- x.address,
281
- account.update(x, applyTokenChange(x, context.accountUpdates[x.address]), context),
282
- context
255
+ const [newSenderState, newStakeStates, newSenderStates, newRollupState, rollupBlockState, newTxStates] =
256
+ await Promise.all([
257
+ // update sender(proposer) account
258
+ statedb.account.update(
259
+ senderState.address,
260
+ account.update(senderState, Object.assign({ nonce: tx.nonce }), context),
261
+ context
262
+ ),
263
+
264
+ // update stake states
265
+ Promise.all(
266
+ stakeStates.map((x) =>
267
+ context.stakeUpdates[x.address]
268
+ ? statedb.stake.update(
269
+ x.address,
270
+ stake.update(x, applyTokenChange(x, context.stakeUpdates[x.address]), context),
271
+ context
272
+ )
273
+ : Promise.resolve(x)
283
274
  )
284
- )
285
- ),
286
-
287
- // Update rollup state
288
- statedb.rollup.update(
289
- rollupState.address,
290
- rollup.update(rollupState, { blockHeight: itx.height, blockHash: itx.hash }, context),
291
- context
292
- ),
293
-
294
- // Create rollup block
295
- statedb.rollupBlock.create(
296
- itx.hash,
297
- rollupBlock.create(
298
- {
299
- ...formattedItx,
300
- ...['rewardAmount', 'mintedAmount', 'burnedAmount'].reduce((acc, x) => {
301
- acc[x] = context[x].toString(10);
302
- return acc;
303
- }, {}),
304
- },
275
+ ),
276
+
277
+ // update tx owner states: those may get refund
278
+ Promise.all(
279
+ senderStates.map((x) =>
280
+ statedb.account.update(
281
+ x.address,
282
+ account.update(x, applyTokenChange(x, context.accountUpdates[x.address]), context),
283
+ context
284
+ )
285
+ )
286
+ ),
287
+
288
+ // Update rollup state
289
+ statedb.rollup.update(
290
+ rollupState.address,
291
+ rollup.update(rollupState, { blockHeight: itx.height, blockHash: itx.hash }, context),
292
+ context
293
+ ),
294
+
295
+ // Create rollup block
296
+ statedb.rollupBlock.create(
297
+ itx.hash,
298
+ rollupBlock.create(
299
+ {
300
+ ...formattedItx,
301
+ ...['rewardAmount', 'mintedAmount', 'burnedAmount'].reduce((acc, x) => {
302
+ acc[x] = context[x].toString(10);
303
+ return acc;
304
+ }, {}),
305
+ },
306
+ context
307
+ ),
305
308
  context
306
309
  ),
307
- context
308
- ),
309
310
 
310
- // Update tx states: mark as finalized
311
- Promise.all(txStates.map((x) => statedb.tx.update(x.hash, Tx.update(x, { finalized: true, tx: x.tx }), context))),
312
- ]);
311
+ // Update tx states: mark as finalized
312
+ Promise.all(
313
+ txStates.map((x) => statedb.tx.update(x.hash, Tx.update(x, { finalized: true, tx: x.tx }), context))
314
+ ),
315
+ ]);
313
316
 
314
317
  context.senderState = newSenderState;
315
- context.stakeStates = newStakeStates;
318
+ context.stakeStates = newStakeStates.filter((x) => context.stakeUpdates[x.address]);
316
319
  context.newSenderStates = newSenderStates;
317
320
  context.rollupState = newRollupState;
318
321
  context.rollupBlockState = rollupBlockState;
322
+ context.txStates = newTxStates;
319
323
 
320
324
  context.updatedAccounts = [...Object.values(context.stakeUpdates), ...Object.values(context.accountUpdates)];
321
325
 
@@ -88,7 +88,7 @@ runner.use(async (context, next) => {
88
88
  const { statedb, itx } = context;
89
89
  const exist = await statedb.rollup.existByToken(itx.tokenAddress, context);
90
90
  if (exist) {
91
- return next(new Error('FORBIDDEN', 'Only 1 rollup can be created for the token'));
91
+ return next(new Error('DUPLICATE_ROLLUP', 'Only 1 rollup can be created for the token'));
92
92
  }
93
93
 
94
94
  return next();
@@ -99,7 +99,7 @@ runner.use(pipes.ExtractState({ from: 'itx.address', to: 'rollupState', status:
99
99
  runner.use(
100
100
  pipes.VerifyInfo([
101
101
  {
102
- error: 'FORBIDDEN',
102
+ error: 'DUPLICATE_ROLLUP',
103
103
  message: 'This rollup already exist on chain',
104
104
  fn: (context) => isEmpty(context.rollupState),
105
105
  },
@@ -10,8 +10,8 @@ module.exports = async (context, next) => {
10
10
  const vaultState = await statedb.account.get(config.vaults.txFee, context);
11
11
 
12
12
  if (!txFee) {
13
- context.senderUpdates = { tokens: {} };
14
- context.vaultUpdates = { tokens: {} };
13
+ context.senderUpdates = {};
14
+ context.vaultUpdates = {};
15
15
  context.vaultState = vaultState;
16
16
  context.updatedAccounts = [];
17
17
  return next();
@@ -66,7 +66,7 @@ runner.use(pipes.ExtractState({ from: 'itx.address', to: 'tokenState', table: 't
66
66
  runner.use(
67
67
  pipes.VerifyInfo([
68
68
  {
69
- error: 'FORBIDDEN',
69
+ error: 'DUPLICATE_TOKEN',
70
70
  message: 'Token address already exists on chain',
71
71
  fn: (context) => isEmpty(context.tokenState),
72
72
  },
@@ -77,12 +77,12 @@ runner.use(
77
77
  runner.use(async (context, next) => {
78
78
  const { symbol } = context.config.token;
79
79
  if (symbol.toLowerCase() === context.itx.symbol.toLowerCase()) {
80
- return next(new Error('FORBIDDEN', `Token symbol can not be ${symbol}`));
80
+ return next(new Error('DUPLICATE_SYMBOL', `Token symbol can not be ${symbol}`));
81
81
  }
82
82
 
83
83
  const exist = await context.statedb.token.existBySymbol(context.itx.symbol, context);
84
84
  if (exist) {
85
- return next(new Error('FORBIDDEN', 'Token symbol already exists'));
85
+ return next(new Error('DUPLICATE_SYMBOL', 'Token symbol already exists'));
86
86
  }
87
87
 
88
88
  return next();
@@ -97,6 +97,7 @@ runner.use(
97
97
  const data = decodeAnySafe(itx.data);
98
98
 
99
99
  // We are definitely creating a different token, so it is safe to set tokens to initial supply
100
+ senderUpdates.tokens = senderUpdates.tokens || {};
100
101
  senderUpdates.tokens[itx.address] = itx.initialSupply;
101
102
 
102
103
  const [newSenderState, tokenState, newVaultState] = await Promise.all([
package/lib/util.js CHANGED
@@ -54,7 +54,7 @@ const decodeAnySafe = (encoded) => {
54
54
 
55
55
  const applyTokenUpdates = (tokens, state, operator) => {
56
56
  if (['add', 'sub'].includes(operator) === false) {
57
- throw new Error('FORBIDDEN', `Invalid operator when applyTokenUpdates: ${operator}`);
57
+ throw new Error('UNEXPECTED_OPERATOR', `Invalid operator when applyTokenUpdates: ${operator}`);
58
58
  }
59
59
 
60
60
  if (!state) {
@@ -69,7 +69,7 @@ const applyTokenUpdates = (tokens, state, operator) => {
69
69
  const balance = new BN(oldTokens[address] || 0);
70
70
  const newBalance = balance[operator](requirement);
71
71
  if (newBalance.lt(ZERO)) {
72
- throw new Error('FORBIDDEN', `Negative token balance when applyTokenUpdates for ${address}`);
72
+ throw new Error('NEGATIVE_TOKEN_BALANCE', `Negative token balance when applyTokenUpdates for ${address}`);
73
73
  }
74
74
  newTokens[address] = newBalance.toString(10);
75
75
  }
@@ -102,16 +102,16 @@ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
102
102
  const minFeeAmount = new BN(minFee);
103
103
 
104
104
  if (feeRate < 0) {
105
- throw new Error('FORBIDDEN', 'Unexpected negative feeRate when getTxFee, abort!');
105
+ throw new Error('NEGATIVE_FEE_RATE', 'Unexpected negative feeRate when getTxFee, abort!');
106
106
  }
107
107
  if (userAmount.lt(ZERO)) {
108
- throw new Error('FORBIDDEN', 'Unexpected negative amount when getTxFee, abort!');
108
+ throw new Error('NEGATIVE_AMOUNT', 'Unexpected negative amount when getTxFee, abort!');
109
109
  }
110
110
  if (maxFeeAmount.lt(ZERO)) {
111
- throw new Error('FORBIDDEN', 'Unexpected negative maxFee when getTxFee, abort!');
111
+ throw new Error('NEGATIVE_MAX_FEE', 'Unexpected negative maxFee when getTxFee, abort!');
112
112
  }
113
113
  if (minFeeAmount.lt(ZERO)) {
114
- throw new Error('FORBIDDEN', 'Unexpected negative minFee when getTxFee, abort!');
114
+ throw new Error('NEGATIVE_MIN_FEE', 'Unexpected negative minFee when getTxFee, abort!');
115
115
  }
116
116
 
117
117
  // total fee
@@ -144,11 +144,11 @@ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
144
144
  const splitTxFee = ({ total, shares = {}, stringify = true }) => {
145
145
  const totalAmount = new BN(total);
146
146
  if (totalAmount.lt(ZERO)) {
147
- throw new Error('FORBIDDEN', 'Unexpected negative total when splitTxFee, abort!');
147
+ throw new Error('NEGATIVE_TOTAL_AMOUNT', 'Unexpected negative total when splitTxFee, abort!');
148
148
  }
149
149
  Object.keys(shares).forEach((key) => {
150
150
  if (shares[key] < 0) {
151
- throw new Error('FORBIDDEN', `Unexpected negative shares[${key}] when splitTxFee, abort!`);
151
+ throw new Error('NEGATIVE_FEE_SHARE', `Unexpected negative shares[${key}] when splitTxFee, abort!`);
152
152
  }
153
153
  });
154
154
 
@@ -217,7 +217,7 @@ const ensureBlockReward = (rollupState, minReward, txStates) => {
217
217
  }
218
218
 
219
219
  if (actualFee.lt(ZERO)) {
220
- throw new Error('FORBIDDEN', 'Got negative actualFee for tx, abort!');
220
+ throw new Error('NEGATIVE_ACTUAL_FEE', 'Got negative actualFee for tx, abort!');
221
221
  }
222
222
 
223
223
  // If the actualFee is less than the maxFee, user will have a refund
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.13.66",
6
+ "version": "1.13.70",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,17 +19,17 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@arcblock/did": "1.13.66",
23
- "@arcblock/did-util": "1.13.66",
24
- "@ocap/asset": "1.13.66",
25
- "@ocap/mcrypto": "1.13.66",
26
- "@ocap/merkle-tree": "1.13.66",
27
- "@ocap/message": "1.13.66",
28
- "@ocap/state": "1.13.66",
29
- "@ocap/tx-pipeline": "1.13.66",
30
- "@ocap/util": "1.13.66",
31
- "@ocap/validator": "1.13.66",
32
- "@ocap/wallet": "1.13.66",
22
+ "@arcblock/did": "1.13.70",
23
+ "@arcblock/did-util": "1.13.70",
24
+ "@ocap/asset": "1.13.70",
25
+ "@ocap/mcrypto": "1.13.70",
26
+ "@ocap/merkle-tree": "1.13.70",
27
+ "@ocap/message": "1.13.70",
28
+ "@ocap/state": "1.13.70",
29
+ "@ocap/tx-pipeline": "1.13.70",
30
+ "@ocap/util": "1.13.70",
31
+ "@ocap/validator": "1.13.70",
32
+ "@ocap/wallet": "1.13.70",
33
33
  "debug": "^4.3.2",
34
34
  "empty-value": "^1.0.1",
35
35
  "lodash": "^4.17.21",
@@ -42,5 +42,5 @@
42
42
  "devDependencies": {
43
43
  "jest": "^27.3.1"
44
44
  },
45
- "gitHead": "1395a8dccee398dd0aed47c2b54a2c6e350d8621"
45
+ "gitHead": "17d673049138356f4cce9ca3d59e2f531b479c2d"
46
46
  }