@ocap/tx-protocols 1.19.6 → 1.19.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/lib/index.js CHANGED
@@ -3,7 +3,6 @@ const states = require('@ocap/state');
3
3
  const transferV2 = require('./protocols/trade/transfer-v2');
4
4
  const transferV3 = require('./protocols/trade/transfer-v3');
5
5
  const exchangeV2 = require('./protocols/trade/exchange-v2');
6
- const declare = require('./protocols/account/declare');
7
6
  const migrate = require('./protocols/account/migrate');
8
7
  const delegate = require('./protocols/account/delegate');
9
8
  const revokeDelegate = require('./protocols/account/revoke-delegate');
@@ -44,7 +43,6 @@ const createExecutor = ({ filter, runAsLambda }) => {
44
43
  exchangeV2,
45
44
 
46
45
  // account
47
- declare,
48
46
  accountMigrate: migrate,
49
47
  delegate,
50
48
  revokeDelegate,
@@ -188,7 +188,11 @@ runner.use(
188
188
  // update receiver
189
189
  receiverState
190
190
  ? Promise.resolve(receiverState)
191
- : statedb.account.create(receiver, account.create({ address: receiver }, context), context),
191
+ : statedb.account.create(
192
+ receiver,
193
+ account.create({ address: receiver, tokens: { [context.config.token.address]: '0' } }, context),
194
+ context
195
+ ),
192
196
 
193
197
  delegationState
194
198
  ? statedb.delegation.update(itx.address, delegation.update(delegationState, { ...itx, from: sender, ops: merged }, context), context) // prettier-ignore
@@ -41,16 +41,21 @@ runner.use(
41
41
  );
42
42
 
43
43
  // Ensure sender exist
44
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })); // prettier-ignore
44
+ runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' })); // prettier-ignore
45
45
  runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
46
46
 
47
47
  // Ensure tx fee and gas
48
48
  runner.use(
49
49
  EnsureTxGas((context) => {
50
- return { create: 1, update: getRelatedAddresses(context.senderState).length, payment: 0 };
50
+ return {
51
+ create: context.senderState ? 1 : 2,
52
+ update: context.senderState ? getRelatedAddresses(context.senderState).length : 0,
53
+ payment: 0,
54
+ };
51
55
  })
52
56
  );
53
57
  runner.use(EnsureTxCost({ attachSenderChanges: true, throwOnInsufficientFund: false }));
58
+ runner.use(pipes.VerifyGasPayer());
54
59
 
55
60
  // Save context snapshot before updating states
56
61
  runner.use(pipes.TakeStateSnapshot());
@@ -62,6 +67,8 @@ runner.use(
62
67
 
63
68
  const sender = context.senderState;
64
69
  const receiver = await statedb.account.get(itx.address, context);
70
+ const senderTokens = sender?.tokens || { [context.config.token.address]: '0' };
71
+ const senderAddress = sender?.address || tx.from;
65
72
 
66
73
  // Ensure receiver does not exist
67
74
  if (receiver) {
@@ -73,11 +80,11 @@ runner.use(
73
80
  itx.address,
74
81
  account.create(
75
82
  {
76
- ...sender, // copy all old account data to new account
83
+ ...(sender || { tokens: senderTokens }), // copy all old account data to new account
77
84
  ...senderUpdates,
78
85
  address: itx.address,
79
86
  pk: itx.pk,
80
- migratedFrom: uniq(flatten([sender.address, ...sender.migratedFrom])),
87
+ migratedFrom: sender ? uniq(flatten([sender.address, ...sender.migratedFrom])) : [senderAddress],
81
88
  },
82
89
  context
83
90
  ),
@@ -86,18 +93,23 @@ runner.use(
86
93
  debug('new account', newAccount);
87
94
 
88
95
  // Update old accounts
89
- const addresses = getRelatedAddresses(sender);
96
+ const addresses = sender ? getRelatedAddresses(sender) : [senderAddress];
90
97
  const states = await Promise.all(
91
98
  addresses.map(async (address) => {
92
99
  let state = null;
93
- if (address === sender.address) {
94
- state = account.update(sender, { migratedTo: itx.address, nonce: tx.nonce }, context);
100
+ if (address === senderAddress) {
101
+ state = account.updateOrCreate(
102
+ sender,
103
+ { address, tokens: senderTokens, migratedTo: itx.address, nonce: tx.nonce },
104
+ context
105
+ );
106
+ await statedb.account.updateOrCreate(sender, state, context);
95
107
  } else {
96
108
  const old = await statedb.account.get(address, { traceMigration: false, txn: context.txn });
97
109
  state = account.update(old, { migratedTo: itx.address }, context);
110
+ await statedb.account.update(address, state, context);
98
111
  }
99
112
 
100
- await statedb.account.update(address, state, context);
101
113
  return state;
102
114
  })
103
115
  );
@@ -107,7 +119,7 @@ runner.use(
107
119
 
108
120
  // Update context
109
121
  context.receiverState = newAccount;
110
- context.senderState = states.find((x) => x.address === sender.address);
122
+ context.senderState = states.find((x) => x.address === senderAddress);
111
123
 
112
124
  return next();
113
125
  },
@@ -178,6 +178,7 @@ runner.use(
178
178
  })
179
179
  );
180
180
  runner.use(EnsureTxCost({ attachSenderChanges: true }));
181
+ runner.use(pipes.VerifyGasPayer());
181
182
 
182
183
  // Save context snapshot before updating states
183
184
  runner.use(pipes.TakeStateSnapshot());
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.19.6",
6
+ "version": "1.19.8",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -21,18 +21,18 @@
21
21
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@arcblock/did": "1.19.6",
25
- "@arcblock/did-util": "1.19.6",
26
- "@arcblock/jwt": "1.19.6",
27
- "@arcblock/validator": "1.19.6",
28
- "@ocap/asset": "1.19.6",
29
- "@ocap/mcrypto": "1.19.6",
30
- "@ocap/merkle-tree": "1.19.6",
31
- "@ocap/message": "1.19.6",
32
- "@ocap/state": "1.19.6",
33
- "@ocap/tx-pipeline": "1.19.6",
34
- "@ocap/util": "1.19.6",
35
- "@ocap/wallet": "1.19.6",
24
+ "@arcblock/did": "1.19.8",
25
+ "@arcblock/did-util": "1.19.8",
26
+ "@arcblock/jwt": "1.19.8",
27
+ "@arcblock/validator": "1.19.8",
28
+ "@ocap/asset": "1.19.8",
29
+ "@ocap/mcrypto": "1.19.8",
30
+ "@ocap/merkle-tree": "1.19.8",
31
+ "@ocap/message": "1.19.8",
32
+ "@ocap/state": "1.19.8",
33
+ "@ocap/tx-pipeline": "1.19.8",
34
+ "@ocap/util": "1.19.8",
35
+ "@ocap/wallet": "1.19.8",
36
36
  "debug": "^4.3.6",
37
37
  "deep-diff": "^1.0.2",
38
38
  "empty-value": "^1.0.1",
@@ -47,5 +47,5 @@
47
47
  "jest": "^29.7.0",
48
48
  "start-server-and-test": "^1.14.0"
49
49
  },
50
- "gitHead": "a884f23e66c8b498f2bf8d1efbbb3b0d024f4a9d"
50
+ "gitHead": "c64c55f04a40357501cc3ecf4e5e7531aee15ffe"
51
51
  }
@@ -1,61 +0,0 @@
1
- const { CustomError: Error } = require('@ocap/util/lib/error');
2
- const { Joi } = require('@arcblock/validator');
3
- const { Runner, pipes } = require('@ocap/tx-pipeline');
4
- const { account } = require('@ocap/state');
5
- const { toBase58 } = require('@ocap/util');
6
-
7
- const runner = new Runner();
8
-
9
- runner.use(pipes.VerifyMultiSig(0));
10
-
11
- // verify itx
12
- const schema = Joi.object({
13
- issuer: Joi.DID().prefix().optional().allow(''),
14
- moniker: Joi.string()
15
- .regex(/^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/)
16
- .required(),
17
- data: Joi.any().optional(),
18
- }).options({ stripUnknown: true, noDefaults: false });
19
- runner.use(({ itx }, next) => {
20
- const { error } = schema.validate(itx);
21
- if (error) {
22
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
23
- }
24
- return next();
25
- });
26
-
27
- // Ensure sender does not exist
28
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState' }));
29
- runner.use(pipes.VerifySender({ state: 'senderState' }));
30
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
31
-
32
- // The issuer must exist in the ledger
33
- runner.use(pipes.ExtractState({ from: 'itx.issuer', to: 'issuerState', status: 'INVALID_TX' }));
34
-
35
- // Save context snapshot before updating states
36
- runner.use(pipes.TakeStateSnapshot());
37
-
38
- // Create account state
39
- runner.use(
40
- async (context, next) => {
41
- const { tx, itx, statedb } = context;
42
- const tokens = { [context.config.token.address]: '0' };
43
-
44
- const [senderState] = await Promise.all([
45
- statedb.account.create(
46
- tx.from,
47
- account.create({ address: tx.from, pk: toBase58(tx.pk), nonce: tx.nonce, tokens, ...itx }, context),
48
- context
49
- ),
50
- ]);
51
-
52
- context.senderState = senderState;
53
-
54
- next();
55
- },
56
- { persistError: true }
57
- );
58
-
59
- runner.use(pipes.VerifyStateDiff());
60
-
61
- module.exports = runner;