@ocap/tx-protocols 1.28.9 → 1.29.0
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 +53 -0
- package/esm/execute.mjs +225 -0
- package/esm/index.d.mts +95 -0
- package/esm/index.mjs +100 -0
- package/esm/pipes/ensure-cost.d.mts +18 -0
- package/esm/pipes/ensure-cost.mjs +136 -0
- package/esm/pipes/ensure-gas.d.mts +14 -0
- package/esm/pipes/ensure-gas.mjs +38 -0
- package/esm/protocols/account/delegate.d.mts +4 -0
- package/esm/protocols/account/delegate.mjs +195 -0
- package/esm/protocols/account/migrate.d.mts +4 -0
- package/esm/protocols/account/migrate.mjs +130 -0
- package/esm/protocols/account/revoke-delegate.d.mts +4 -0
- package/esm/protocols/account/revoke-delegate.mjs +102 -0
- package/esm/protocols/asset/acquire-v2.d.mts +9 -0
- package/esm/protocols/asset/acquire-v2.mjs +213 -0
- package/esm/protocols/asset/acquire-v3.d.mts +17 -0
- package/esm/protocols/asset/acquire-v3.mjs +265 -0
- package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
- package/esm/protocols/asset/calls/transfer.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer.mjs +32 -0
- package/esm/protocols/asset/consume.d.mts +4 -0
- package/esm/protocols/asset/consume.mjs +92 -0
- package/esm/protocols/asset/create.d.mts +4 -0
- package/esm/protocols/asset/create.mjs +136 -0
- package/esm/protocols/asset/mint.d.mts +4 -0
- package/esm/protocols/asset/mint.mjs +156 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
- package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
- package/esm/protocols/asset/update.d.mts +4 -0
- package/esm/protocols/asset/update.mjs +111 -0
- package/esm/protocols/factory/create.d.mts +29 -0
- package/esm/protocols/factory/create.mjs +155 -0
- package/esm/protocols/governance/claim-stake.d.mts +27 -0
- package/esm/protocols/governance/claim-stake.mjs +220 -0
- package/esm/protocols/governance/return-stake.d.mts +27 -0
- package/esm/protocols/governance/return-stake.mjs +211 -0
- package/esm/protocols/governance/revoke-stake.d.mts +27 -0
- package/esm/protocols/governance/revoke-stake.mjs +178 -0
- package/esm/protocols/governance/slash-stake.d.mts +17 -0
- package/esm/protocols/governance/slash-stake.mjs +213 -0
- package/esm/protocols/governance/stake.d.mts +15 -0
- package/esm/protocols/governance/stake.mjs +270 -0
- package/esm/protocols/rollup/claim-reward.d.mts +11 -0
- package/esm/protocols/rollup/claim-reward.mjs +322 -0
- package/esm/protocols/rollup/close.d.mts +4 -0
- package/esm/protocols/rollup/close.mjs +105 -0
- package/esm/protocols/rollup/create-block.d.mts +11 -0
- package/esm/protocols/rollup/create-block.mjs +303 -0
- package/esm/protocols/rollup/create.d.mts +4 -0
- package/esm/protocols/rollup/create.mjs +164 -0
- package/esm/protocols/rollup/join.d.mts +4 -0
- package/esm/protocols/rollup/join.mjs +152 -0
- package/esm/protocols/rollup/leave.d.mts +4 -0
- package/esm/protocols/rollup/leave.mjs +137 -0
- package/esm/protocols/rollup/migrate.d.mts +4 -0
- package/esm/protocols/rollup/migrate.mjs +85 -0
- package/esm/protocols/rollup/pause.d.mts +4 -0
- package/esm/protocols/rollup/pause.mjs +76 -0
- package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
- package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
- package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
- package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
- package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
- package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
- package/esm/protocols/rollup/resume.d.mts +4 -0
- package/esm/protocols/rollup/resume.mjs +79 -0
- package/esm/protocols/rollup/update.d.mts +4 -0
- package/esm/protocols/rollup/update.mjs +111 -0
- package/esm/protocols/token/create.d.mts +4 -0
- package/esm/protocols/token/create.mjs +150 -0
- package/esm/protocols/token/deposit-v2.d.mts +11 -0
- package/esm/protocols/token/deposit-v2.mjs +216 -0
- package/esm/protocols/token/withdraw-v2.d.mts +9 -0
- package/esm/protocols/token/withdraw-v2.mjs +222 -0
- package/esm/protocols/token-factory/burn.d.mts +15 -0
- package/esm/protocols/token-factory/burn.mjs +233 -0
- package/esm/protocols/token-factory/create.d.mts +4 -0
- package/esm/protocols/token-factory/create.mjs +254 -0
- package/esm/protocols/token-factory/mint.d.mts +15 -0
- package/esm/protocols/token-factory/mint.mjs +234 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
- package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
- package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
- package/esm/protocols/token-factory/update.d.mts +10 -0
- package/esm/protocols/token-factory/update.mjs +152 -0
- package/esm/protocols/trade/exchange-v2.d.mts +9 -0
- package/esm/protocols/trade/exchange-v2.mjs +239 -0
- package/esm/protocols/trade/transfer-v2.d.mts +9 -0
- package/esm/protocols/trade/transfer-v2.mjs +226 -0
- package/esm/protocols/trade/transfer-v3.d.mts +17 -0
- package/esm/protocols/trade/transfer-v3.mjs +270 -0
- package/esm/util.d.mts +141 -0
- package/esm/util.mjs +278 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/execute.cjs +231 -0
- package/lib/execute.d.cts +53 -0
- package/lib/index.cjs +105 -0
- package/lib/index.d.cts +95 -0
- package/lib/pipes/ensure-cost.cjs +141 -0
- package/lib/pipes/ensure-cost.d.cts +18 -0
- package/lib/pipes/ensure-gas.cjs +41 -0
- package/lib/pipes/ensure-gas.d.cts +14 -0
- package/lib/protocols/account/delegate.cjs +201 -0
- package/lib/protocols/account/delegate.d.cts +4 -0
- package/lib/protocols/account/migrate.cjs +135 -0
- package/lib/protocols/account/migrate.d.cts +4 -0
- package/lib/protocols/account/revoke-delegate.cjs +107 -0
- package/lib/protocols/account/revoke-delegate.d.cts +4 -0
- package/lib/protocols/asset/acquire-v2.cjs +216 -0
- package/lib/protocols/asset/acquire-v2.d.cts +9 -0
- package/lib/protocols/asset/acquire-v3.cjs +269 -0
- package/lib/protocols/asset/acquire-v3.d.cts +17 -0
- package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
- package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
- package/lib/protocols/asset/calls/transfer.cjs +35 -0
- package/lib/protocols/asset/calls/transfer.d.cts +4 -0
- package/lib/protocols/asset/consume.cjs +95 -0
- package/lib/protocols/asset/consume.d.cts +4 -0
- package/lib/protocols/asset/create.cjs +140 -0
- package/lib/protocols/asset/create.d.cts +4 -0
- package/lib/protocols/asset/mint.cjs +159 -0
- package/lib/protocols/asset/mint.d.cts +4 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
- package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
- package/lib/protocols/asset/update.cjs +114 -0
- package/lib/protocols/asset/update.d.cts +4 -0
- package/lib/protocols/factory/create.cjs +161 -0
- package/lib/protocols/factory/create.d.cts +29 -0
- package/lib/protocols/governance/claim-stake.cjs +223 -0
- package/lib/protocols/governance/claim-stake.d.cts +27 -0
- package/lib/protocols/governance/return-stake.cjs +215 -0
- package/lib/protocols/governance/return-stake.d.cts +27 -0
- package/lib/protocols/governance/revoke-stake.cjs +182 -0
- package/lib/protocols/governance/revoke-stake.d.cts +27 -0
- package/lib/protocols/governance/slash-stake.cjs +217 -0
- package/lib/protocols/governance/slash-stake.d.cts +17 -0
- package/lib/protocols/governance/stake.cjs +275 -0
- package/lib/protocols/governance/stake.d.cts +15 -0
- package/lib/protocols/rollup/claim-reward.cjs +328 -0
- package/lib/protocols/rollup/claim-reward.d.cts +11 -0
- package/lib/protocols/rollup/close.cjs +107 -0
- package/lib/protocols/rollup/close.d.cts +4 -0
- package/lib/protocols/rollup/create-block.cjs +309 -0
- package/lib/protocols/rollup/create-block.d.cts +11 -0
- package/lib/protocols/rollup/create.cjs +169 -0
- package/lib/protocols/rollup/create.d.cts +4 -0
- package/lib/protocols/rollup/join.cjs +157 -0
- package/lib/protocols/rollup/join.d.cts +4 -0
- package/lib/protocols/rollup/leave.cjs +141 -0
- package/lib/protocols/rollup/leave.d.cts +4 -0
- package/lib/protocols/rollup/migrate.cjs +87 -0
- package/lib/protocols/rollup/migrate.d.cts +4 -0
- package/lib/protocols/rollup/pause.cjs +78 -0
- package/lib/protocols/rollup/pause.d.cts +4 -0
- package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
- package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
- package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
- package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
- package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
- package/lib/protocols/rollup/resume.cjs +81 -0
- package/lib/protocols/rollup/resume.d.cts +4 -0
- package/lib/protocols/rollup/update.cjs +114 -0
- package/lib/protocols/rollup/update.d.cts +4 -0
- package/lib/protocols/token/create.cjs +156 -0
- package/lib/protocols/token/create.d.cts +4 -0
- package/lib/protocols/token/deposit-v2.cjs +219 -0
- package/lib/protocols/token/deposit-v2.d.cts +11 -0
- package/lib/protocols/token/withdraw-v2.cjs +225 -0
- package/lib/protocols/token/withdraw-v2.d.cts +9 -0
- package/lib/protocols/token-factory/burn.cjs +236 -0
- package/lib/protocols/token-factory/burn.d.cts +15 -0
- package/lib/protocols/token-factory/create.cjs +260 -0
- package/lib/protocols/token-factory/create.d.cts +4 -0
- package/lib/protocols/token-factory/mint.cjs +237 -0
- package/lib/protocols/token-factory/mint.d.cts +15 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
- package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
- package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
- package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
- package/lib/protocols/token-factory/update.cjs +155 -0
- package/lib/protocols/token-factory/update.d.cts +10 -0
- package/lib/protocols/trade/exchange-v2.cjs +243 -0
- package/lib/protocols/trade/exchange-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v2.cjs +229 -0
- package/lib/protocols/trade/transfer-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v3.cjs +274 -0
- package/lib/protocols/trade/transfer-v3.d.cts +17 -0
- package/lib/util.cjs +296 -0
- package/lib/util.d.cts +141 -0
- package/package.json +49 -22
- package/tools/fixtures.ts +564 -0
- package/lib/execute.js +0 -254
- package/lib/index.js +0 -117
- package/lib/pipes/ensure-cost.js +0 -193
- package/lib/pipes/ensure-gas.js +0 -48
- package/lib/protocols/account/delegate.js +0 -223
- package/lib/protocols/account/migrate.js +0 -153
- package/lib/protocols/account/revoke-delegate.js +0 -110
- package/lib/protocols/asset/acquire-v2.js +0 -262
- package/lib/protocols/asset/acquire-v3.js +0 -330
- package/lib/protocols/asset/calls/README.md +0 -5
- package/lib/protocols/asset/calls/transfer-token.js +0 -36
- package/lib/protocols/asset/calls/transfer.js +0 -28
- package/lib/protocols/asset/consume.js +0 -105
- package/lib/protocols/asset/create.js +0 -151
- package/lib/protocols/asset/mint.js +0 -199
- package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
- package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
- package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
- package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
- package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
- package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
- package/lib/protocols/asset/update.js +0 -131
- package/lib/protocols/factory/create.js +0 -191
- package/lib/protocols/governance/claim-stake.js +0 -266
- package/lib/protocols/governance/return-stake.js +0 -248
- package/lib/protocols/governance/revoke-stake.js +0 -172
- package/lib/protocols/governance/slash-stake.js +0 -271
- package/lib/protocols/governance/stake.js +0 -303
- package/lib/protocols/rollup/claim-reward.js +0 -342
- package/lib/protocols/rollup/close.js +0 -104
- package/lib/protocols/rollup/create-block.js +0 -413
- package/lib/protocols/rollup/create.js +0 -197
- package/lib/protocols/rollup/join.js +0 -182
- package/lib/protocols/rollup/leave.js +0 -145
- package/lib/protocols/rollup/migrate.js +0 -85
- package/lib/protocols/rollup/pause.js +0 -75
- package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
- package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
- package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
- package/lib/protocols/rollup/pipes/verify-status.js +0 -30
- package/lib/protocols/rollup/resume.js +0 -75
- package/lib/protocols/rollup/update.js +0 -122
- package/lib/protocols/token/create.js +0 -199
- package/lib/protocols/token/deposit-v2.js +0 -290
- package/lib/protocols/token/withdraw-v2.js +0 -305
- package/lib/protocols/token-factory/burn.js +0 -371
- package/lib/protocols/token-factory/create.js +0 -342
- package/lib/protocols/token-factory/mint.js +0 -385
- package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
- package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
- package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
- package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
- package/lib/protocols/token-factory/update.js +0 -208
- package/lib/protocols/trade/exchange-v2.js +0 -239
- package/lib/protocols/trade/transfer-v2.js +0 -233
- package/lib/protocols/trade/transfer-v3.js +0 -333
- package/lib/util.js +0 -442
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { applyTokenChange, ensureBlockReward, getRewardLocker } from "../../util.mjs";
|
|
2
|
+
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
|
+
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
+
import ensure_validator_default from "./pipes/ensure-validator.mjs";
|
|
5
|
+
import verify_signers_default from "./pipes/verify-signers.mjs";
|
|
6
|
+
import verify_status_default from "./pipes/verify-status.mjs";
|
|
7
|
+
import verify_evidence_default from "./pipes/verify-evidence.mjs";
|
|
8
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
9
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
10
|
+
import pick from "lodash/pick.js";
|
|
11
|
+
import { toStakeAddress } from "@arcblock/did-util";
|
|
12
|
+
import { account, rollup, rollupBlock, stake, tx } from "@ocap/state";
|
|
13
|
+
import Debug from "debug";
|
|
14
|
+
import { formatMessage } from "@ocap/message";
|
|
15
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
16
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
17
|
+
import { Joi, patterns, schemas } from "@arcblock/validator";
|
|
18
|
+
import MerkleTree from "@ocap/merkle-tree";
|
|
19
|
+
|
|
20
|
+
//#region src/protocols/rollup/create-block.ts
|
|
21
|
+
const debug = Debug("@ocap/tx-protocols:create-rollup-block");
|
|
22
|
+
const runner = new Runner("create_rollup_block");
|
|
23
|
+
const schema = Joi.object({
|
|
24
|
+
hash: Joi.string().regex(patterns.txHash).required(),
|
|
25
|
+
height: Joi.number().integer().greater(0).required(),
|
|
26
|
+
merkleRoot: Joi.string().regex(patterns.txHash).required(),
|
|
27
|
+
previousHash: Joi.string().when("height", {
|
|
28
|
+
is: 1,
|
|
29
|
+
then: Joi.string().optional().allow(null).allow(""),
|
|
30
|
+
otherwise: Joi.string().regex(patterns.txHash).required()
|
|
31
|
+
}),
|
|
32
|
+
txsHash: Joi.string().regex(patterns.txHash).required(),
|
|
33
|
+
txsList: Joi.array().items(Joi.string().regex(patterns.txHash).required()).min(1).unique().required(),
|
|
34
|
+
proposer: Joi.DID().prefix().wallet("ethereum").required(),
|
|
35
|
+
signaturesList: schemas.multiSig.min(1).required(),
|
|
36
|
+
rollup: Joi.DID().prefix().role("ROLE_ROLLUP").required(),
|
|
37
|
+
minReward: Joi.BN().min(0).required(),
|
|
38
|
+
governance: Joi.boolean().default(false),
|
|
39
|
+
data: Joi.any().optional().allow(null)
|
|
40
|
+
}).options({
|
|
41
|
+
stripUnknown: true,
|
|
42
|
+
noDefaults: false
|
|
43
|
+
});
|
|
44
|
+
runner.use((context, next) => {
|
|
45
|
+
const { tx: tx$1, itx } = context;
|
|
46
|
+
context.formattedItx = formatMessage("CreateRollupBlockTx", itx);
|
|
47
|
+
const { error } = schema.validate(itx);
|
|
48
|
+
if (error) return next(new CustomError("INVALID_TX", error.message));
|
|
49
|
+
if (tx$1.from !== itx.proposer) return next(new CustomError("INVALID_TX", "itx.proposer must be same with tx.from"));
|
|
50
|
+
if (getListField(tx$1, "signatures").some((x) => x.signer === itx.proposer) === false) return next(new CustomError("INVALID_TX", "itx.proposer must exist in signatures"));
|
|
51
|
+
const txsList = getListField(itx, "txs");
|
|
52
|
+
if (MerkleTree.getListHash(txsList) !== itx.txsHash) return next(new CustomError("INVALID_TX", "itx.txsHash does not match with itx.txs"));
|
|
53
|
+
if (MerkleTree.getBlockHash({
|
|
54
|
+
...pick(itx, [
|
|
55
|
+
"height",
|
|
56
|
+
"merkleRoot",
|
|
57
|
+
"txsHash"
|
|
58
|
+
]),
|
|
59
|
+
previousHash: itx.height === 1 ? "" : itx.previousHash
|
|
60
|
+
}) !== itx.hash) return next(new CustomError("INVALID_TX", "itx.hash invalid"));
|
|
61
|
+
return next();
|
|
62
|
+
});
|
|
63
|
+
runner.use(pipes.ExtractState({
|
|
64
|
+
from: "itx.rollup",
|
|
65
|
+
to: "rollupState",
|
|
66
|
+
status: "INVALID_ROLLUP",
|
|
67
|
+
table: "rollup"
|
|
68
|
+
}));
|
|
69
|
+
runner.use(ensure_validator_default(false));
|
|
70
|
+
runner.use(verify_status_default({ closed: false }));
|
|
71
|
+
runner.use((context, next) => {
|
|
72
|
+
const { itx, rollupState } = context;
|
|
73
|
+
if (itx.height !== (rollupState?.blockHeight || 0) + 1) return next(new CustomError("INVALID_TX", `Expect new block height to be ${(rollupState?.blockHeight || 0) + 1}, got ${itx.height}`));
|
|
74
|
+
const itxTxsList = getListField(itx, "txs");
|
|
75
|
+
if (itx.governance) {
|
|
76
|
+
if (itx.minReward !== "0") return next(new CustomError("INVALID_TX", "itx.minReward should be 0 for governance block"));
|
|
77
|
+
if (itxTxsList.length !== 1) return next(new CustomError("INVALID_TX", "itx.txs should contain only 1 tx for governance block"));
|
|
78
|
+
} else {
|
|
79
|
+
if (rollupState?.paused) return next(new CustomError("INVALID_TX", "Normal block is only allowed when rollup is not paused"));
|
|
80
|
+
const blockSize = itxTxsList.length;
|
|
81
|
+
if (blockSize < (rollupState?.minBlockSize || 0)) return next(new CustomError("INVALID_TX", `Expect at least ${rollupState?.minBlockSize || 0} tx in the block, got ${blockSize}`));
|
|
82
|
+
if (blockSize > (rollupState?.maxBlockSize || 0)) return next(new CustomError("INVALID_TX", `Expect at most ${rollupState?.maxBlockSize || 0} tx in the block, got ${blockSize}`));
|
|
83
|
+
}
|
|
84
|
+
return next();
|
|
85
|
+
});
|
|
86
|
+
runner.use(verify_signers_default({
|
|
87
|
+
signersKey: "itx.signaturesList",
|
|
88
|
+
allowSender: true
|
|
89
|
+
}));
|
|
90
|
+
runner.use(verify_evidence_default({
|
|
91
|
+
evidenceKey: "itx.hash",
|
|
92
|
+
signaturesKey: "itx.signaturesList",
|
|
93
|
+
verifyMethod: "ethVerify"
|
|
94
|
+
}));
|
|
95
|
+
runner.use(verify_signers_default({
|
|
96
|
+
signersKey: "tx.signaturesList",
|
|
97
|
+
allowSender: true
|
|
98
|
+
}));
|
|
99
|
+
runner.use(pipes.VerifyMultiSigV2({ signersKey: "signers" }));
|
|
100
|
+
runner.use(pipes.ExtractState({
|
|
101
|
+
from: "tx.from",
|
|
102
|
+
to: "senderState",
|
|
103
|
+
status: "INVALID_SENDER_STATE",
|
|
104
|
+
table: "account"
|
|
105
|
+
}));
|
|
106
|
+
runner.use(pipes.ExtractState({
|
|
107
|
+
from: "signers",
|
|
108
|
+
to: "signerStates",
|
|
109
|
+
status: "INVALID_SIGNER_STATE",
|
|
110
|
+
table: "account"
|
|
111
|
+
}));
|
|
112
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
113
|
+
signerKey: "signerStates",
|
|
114
|
+
stateKey: "senderState",
|
|
115
|
+
addressKey: "tx.from"
|
|
116
|
+
}));
|
|
117
|
+
runner.use(pipes.ExtractState({
|
|
118
|
+
from: "itx.previousHash",
|
|
119
|
+
to: "previousBlock",
|
|
120
|
+
status: "OK",
|
|
121
|
+
table: "rollupBlock"
|
|
122
|
+
}));
|
|
123
|
+
runner.use((context, next) => {
|
|
124
|
+
const { txTime, itx, previousBlock, rollupState } = context;
|
|
125
|
+
if (itx.height > 1) {
|
|
126
|
+
if (!previousBlock) return next(new CustomError("INVALID_TX", `Rollup block with height ${itx.height} does not exist`));
|
|
127
|
+
if (itx.height !== previousBlock.height + 1) return next(new CustomError("INVALID_TX", `Unexpected previous block height, expected ${itx.height - 1}, got ${previousBlock.height}`));
|
|
128
|
+
if (itx.governance === false) {
|
|
129
|
+
const minBlockTime = +new Date(previousBlock.context.genesisTime) + (rollupState?.minBlockInterval || 0) * 1e3;
|
|
130
|
+
if (+new Date(txTime) < minBlockTime) return next(new CustomError("INVALID_TX", `Block does not comply with minBlockInterval: ${new Date(minBlockTime).toISOString()}`));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return next();
|
|
134
|
+
});
|
|
135
|
+
runner.use(pipes.ExtractState({
|
|
136
|
+
from: "itx.hash",
|
|
137
|
+
to: "newBlock",
|
|
138
|
+
status: "OK",
|
|
139
|
+
table: "rollupBlock"
|
|
140
|
+
}));
|
|
141
|
+
runner.use((context, next) => {
|
|
142
|
+
if (context.newBlock) return next(new CustomError("INVALID_TX", `Rollup block with height ${context.itx.height} already exists`));
|
|
143
|
+
return next();
|
|
144
|
+
});
|
|
145
|
+
runner.use(pipes.ExtractState({
|
|
146
|
+
from: "rollupState.tokenAddress",
|
|
147
|
+
to: "tokenState",
|
|
148
|
+
status: "INVALID_TOKEN",
|
|
149
|
+
table: "token"
|
|
150
|
+
}));
|
|
151
|
+
runner.use(pipes.ExtractState({
|
|
152
|
+
from: "itx.txsList",
|
|
153
|
+
to: "txStates",
|
|
154
|
+
status: "INVALID_TX",
|
|
155
|
+
table: "tx"
|
|
156
|
+
}));
|
|
157
|
+
runner.use(({ itx, txStates }, next) => {
|
|
158
|
+
const normalTxTypes = ["deposit_token_v2", "withdraw_token_v2"];
|
|
159
|
+
const governanceTxTypes = [
|
|
160
|
+
"pause_rollup",
|
|
161
|
+
"resume_rollup",
|
|
162
|
+
"migrate_rollup"
|
|
163
|
+
];
|
|
164
|
+
const txList = txStates || [];
|
|
165
|
+
if (txList.some((x) => x.finalized)) return next(new CustomError("INVALID_TX", "Should not include finalized tx in later block"));
|
|
166
|
+
if (txList.some((x) => x.code !== "OK")) return next(new CustomError("INVALID_TX", "Rollup block can only include successful executed transactions"));
|
|
167
|
+
if (itx.governance) {
|
|
168
|
+
if (txList.some((x) => governanceTxTypes.includes(x.type) === false)) return next(new CustomError("INVALID_TX", `Governance block can only include governance txs: ${governanceTxTypes.join(",")}`));
|
|
169
|
+
} else if (txList.some((x) => normalTxTypes.includes(x.type) === false)) return next(new CustomError("INVALID_TX", `Rollup block can only include normal txs: ${normalTxTypes.join(",")}`));
|
|
170
|
+
if (txList.some((x) => {
|
|
171
|
+
return (x.tx?.itxJson)?.rollup !== itx.rollup;
|
|
172
|
+
})) return next(new CustomError("INVALID_TX", `Rollup block can only include tx from rollup ${itx.rollup}`));
|
|
173
|
+
if (MerkleTree.getBlockMerkleTree(txList.map((x) => {
|
|
174
|
+
const txItxJson = x.tx?.itxJson;
|
|
175
|
+
if (x.type === "withdraw_token_v2") return {
|
|
176
|
+
hash: x.hash,
|
|
177
|
+
to: txItxJson?.to,
|
|
178
|
+
amount: txItxJson?.token?.value
|
|
179
|
+
};
|
|
180
|
+
if (x.type === "pause_rollup") return {
|
|
181
|
+
hash: x.hash,
|
|
182
|
+
to: MerkleTree.BLACK_HOLE,
|
|
183
|
+
amount: "0"
|
|
184
|
+
};
|
|
185
|
+
if (x.type === "resume_rollup") return {
|
|
186
|
+
hash: x.hash,
|
|
187
|
+
to: MerkleTree.BLACK_HOLE,
|
|
188
|
+
amount: "0"
|
|
189
|
+
};
|
|
190
|
+
if (x.type === "migrate_rollup") return {
|
|
191
|
+
hash: x.hash,
|
|
192
|
+
to: txItxJson?.to,
|
|
193
|
+
amount: "0"
|
|
194
|
+
};
|
|
195
|
+
return null;
|
|
196
|
+
}).filter(Boolean)).getHexRoot() !== itx.merkleRoot) return next(new CustomError("INVALID_TX", "Invalid rollup block merkle root"));
|
|
197
|
+
return next();
|
|
198
|
+
});
|
|
199
|
+
runner.use((context, next) => {
|
|
200
|
+
const { itx, txStates } = context;
|
|
201
|
+
const txList = txStates || [];
|
|
202
|
+
const depositProposerStakeAddr = txList.filter((x) => x.type === "deposit_token_v2").map((x) => {
|
|
203
|
+
const txItxJson = x.tx?.itxJson;
|
|
204
|
+
return toStakeAddress(txItxJson.proposer, itx.rollup);
|
|
205
|
+
});
|
|
206
|
+
const withdrawLockerStakeAddr = txList.filter((x) => x.type === "withdraw_token_v2").map((x) => toStakeAddress(x.tx.from, itx.rollup));
|
|
207
|
+
context.stakeAddresses = [
|
|
208
|
+
...depositProposerStakeAddr,
|
|
209
|
+
...withdrawLockerStakeAddr,
|
|
210
|
+
getRewardLocker(itx.rollup)
|
|
211
|
+
];
|
|
212
|
+
return next();
|
|
213
|
+
});
|
|
214
|
+
runner.use(pipes.ExtractState({
|
|
215
|
+
from: "stakeAddresses",
|
|
216
|
+
to: "stakeStates",
|
|
217
|
+
status: "INVALID_STAKE_STATE",
|
|
218
|
+
table: "stake"
|
|
219
|
+
}));
|
|
220
|
+
runner.use((context, next) => {
|
|
221
|
+
const { itx, txStates, rollupState } = context;
|
|
222
|
+
try {
|
|
223
|
+
const txStatesForReward = txStates || [];
|
|
224
|
+
const result = ensureBlockReward(rollupState, itx.minReward, txStatesForReward);
|
|
225
|
+
context.senders = Object.keys(result.accountUpdates);
|
|
226
|
+
Object.assign(context, result);
|
|
227
|
+
return next();
|
|
228
|
+
} catch (err) {
|
|
229
|
+
return next(err);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
runner.use(pipes.ExtractState({
|
|
233
|
+
from: "senders",
|
|
234
|
+
to: "senderStates",
|
|
235
|
+
status: "INVALID_SENDER_STATE",
|
|
236
|
+
table: "account"
|
|
237
|
+
}));
|
|
238
|
+
runner.use(ensure_gas_default((context) => {
|
|
239
|
+
const result = {
|
|
240
|
+
create: 1,
|
|
241
|
+
update: 2,
|
|
242
|
+
payment: 0
|
|
243
|
+
};
|
|
244
|
+
result.update += (context.stakeStates || []).filter((x) => context.stakeUpdates?.[x.address]).length;
|
|
245
|
+
result.update += (context.txStates || []).length;
|
|
246
|
+
result.update += (context.senderStates || []).length;
|
|
247
|
+
return result;
|
|
248
|
+
}));
|
|
249
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
250
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
251
|
+
runner.use(async (context, next) => {
|
|
252
|
+
const { tx: tx$1, itx, statedb, rollupState, senderState, stakeStates, txStates, formattedItx, senderUpdates, updateVaults } = context;
|
|
253
|
+
const senderStatesList = context.senderStates || [];
|
|
254
|
+
const stakeStatesList = stakeStates || [];
|
|
255
|
+
const txStatesList = txStates || [];
|
|
256
|
+
const stakeUpdatesMap = context.stakeUpdates || {};
|
|
257
|
+
const accountUpdatesMap = context.accountUpdates || {};
|
|
258
|
+
const [newSenderState, newStakeStates, newSenderStates, newRollupState, rollupBlockState, newTxStates] = await Promise.all([
|
|
259
|
+
statedb.account.update(senderState.address, account.update(senderState, {
|
|
260
|
+
nonce: tx$1.nonce,
|
|
261
|
+
...senderUpdates
|
|
262
|
+
}, context), context),
|
|
263
|
+
Promise.all(stakeStatesList.map((x) => stakeUpdatesMap[x.address] ? statedb.stake.update(x.address, stake.update(x, applyTokenChange(x, stakeUpdatesMap[x.address]), context), context) : Promise.resolve(x))),
|
|
264
|
+
Promise.all(senderStatesList.map((x) => statedb.account.update(x.address, account.update(x, applyTokenChange(x, accountUpdatesMap[x.address]), context), context))),
|
|
265
|
+
statedb.rollup.update(rollupState.address, rollup.update(rollupState, {
|
|
266
|
+
blockHeight: itx.height,
|
|
267
|
+
blockHash: itx.hash
|
|
268
|
+
}, context), context),
|
|
269
|
+
statedb.rollupBlock.create(itx.hash, rollupBlock.create({
|
|
270
|
+
...formattedItx,
|
|
271
|
+
...[
|
|
272
|
+
"rewardAmount",
|
|
273
|
+
"mintedAmount",
|
|
274
|
+
"burnedAmount"
|
|
275
|
+
].reduce((acc, x) => {
|
|
276
|
+
const val = context[x];
|
|
277
|
+
acc[x] = val ? val.toString(10) : "0";
|
|
278
|
+
return acc;
|
|
279
|
+
}, {})
|
|
280
|
+
}, context), context),
|
|
281
|
+
Promise.all(txStatesList.map((x) => statedb.tx.update(x.hash, tx.update(x, { finalized: true }), context)))
|
|
282
|
+
]);
|
|
283
|
+
if (updateVaults) await updateVaults();
|
|
284
|
+
context.senderState = newSenderState;
|
|
285
|
+
context.stakeStates = newStakeStates.filter((x) => stakeUpdatesMap[x.address]);
|
|
286
|
+
context.newSenderStates = newSenderStates;
|
|
287
|
+
context.rollupState = newRollupState;
|
|
288
|
+
context.createdRollupBlock = rollupBlockState;
|
|
289
|
+
context.txStates = newTxStates;
|
|
290
|
+
context.updatedAccounts = context.updatedAccounts || [];
|
|
291
|
+
context.updatedAccounts.push(...cloneDeep(Object.values(stakeUpdatesMap)));
|
|
292
|
+
context.updatedAccounts.push(...cloneDeep(Object.values(accountUpdatesMap)));
|
|
293
|
+
debug("create-rollup-block", {
|
|
294
|
+
itx,
|
|
295
|
+
updatedAccounts: context.updatedAccounts
|
|
296
|
+
});
|
|
297
|
+
next();
|
|
298
|
+
}, { persistError: true });
|
|
299
|
+
runner.use(pipes.VerifyStateDiff());
|
|
300
|
+
var create_block_default = runner;
|
|
301
|
+
|
|
302
|
+
//#endregion
|
|
303
|
+
export { create_block_default as default };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
|
|
2
|
+
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
|
+
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
5
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
6
|
+
import { isEthereumDid, isFromPublicKey } from "@arcblock/did";
|
|
7
|
+
import { toRollupAddress } from "@arcblock/did-util";
|
|
8
|
+
import { account, delegation, rollup } from "@ocap/state";
|
|
9
|
+
import Debug from "debug";
|
|
10
|
+
import { formatMessage } from "@ocap/message";
|
|
11
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
12
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
13
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
14
|
+
|
|
15
|
+
//#region src/protocols/rollup/create.ts
|
|
16
|
+
const debug = Debug("@ocap/tx-protocols:create-rollup");
|
|
17
|
+
const runner = new Runner("create_rollup");
|
|
18
|
+
runner.use(pipes.VerifyMultiSig(0));
|
|
19
|
+
runner.use((context, next) => {
|
|
20
|
+
const { itx } = context;
|
|
21
|
+
context.formattedItx = formatMessage("CreateRollupTx", itx);
|
|
22
|
+
try {
|
|
23
|
+
rollup.create(context.formattedItx, context);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
return next(new CustomError("INVALID_TX", err.message));
|
|
26
|
+
}
|
|
27
|
+
return next();
|
|
28
|
+
});
|
|
29
|
+
runner.use(pipes.VerifyInfo([
|
|
30
|
+
{
|
|
31
|
+
error: "INVALID_TX",
|
|
32
|
+
message: "Sum of itx.proposerFeeShare and itx.publisherFeeShare must be less than 10000",
|
|
33
|
+
fn: (ctx) => {
|
|
34
|
+
const context = ctx;
|
|
35
|
+
return context.itx.proposerFeeShare + context.itx.publisherFeeShare < 1e4;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
error: "INVALID_TX",
|
|
40
|
+
message: "Seed validator pk and address not match",
|
|
41
|
+
fn: (ctx) => {
|
|
42
|
+
return (ctx.formattedItx?.seedValidators || []).every(({ pk, address }) => isFromPublicKey(address, pk));
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
error: "INVALID_TX",
|
|
47
|
+
message: "Seed validator must use ethereum compatible address",
|
|
48
|
+
fn: (ctx) => {
|
|
49
|
+
return (ctx.formattedItx?.seedValidators || []).every(({ address }) => isEthereumDid(address));
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
error: "INVALID_TX",
|
|
54
|
+
message: "Rollup address is not valid",
|
|
55
|
+
fn: (ctx) => {
|
|
56
|
+
const context = ctx;
|
|
57
|
+
const itx = cloneDeep(context.itx);
|
|
58
|
+
itx.data = decodeAnySafe(itx.data);
|
|
59
|
+
itx.address = "";
|
|
60
|
+
context.rollupData = itx.data;
|
|
61
|
+
context.seedValidators = getListField(itx, "seedValidators").map((x) => x.address);
|
|
62
|
+
return toRollupAddress(itx) === context.itx.address;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
]));
|
|
66
|
+
runner.use(pipes.ExtractState({
|
|
67
|
+
from: "tx.from",
|
|
68
|
+
to: "senderState",
|
|
69
|
+
status: "INVALID_SENDER_STATE",
|
|
70
|
+
table: "account"
|
|
71
|
+
}));
|
|
72
|
+
runner.use(pipes.ExtractState({
|
|
73
|
+
from: "seedValidators",
|
|
74
|
+
to: "validatorStates",
|
|
75
|
+
status: "INVALID_VALIDATOR_STATE",
|
|
76
|
+
table: "account"
|
|
77
|
+
}));
|
|
78
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
79
|
+
signerKey: "validatorStates",
|
|
80
|
+
stateKey: "senderState",
|
|
81
|
+
addressKey: "tx.from"
|
|
82
|
+
}));
|
|
83
|
+
runner.use(pipes.ExtractState({
|
|
84
|
+
from: "tx.delegator",
|
|
85
|
+
to: "delegatorState",
|
|
86
|
+
status: "OK",
|
|
87
|
+
table: "account"
|
|
88
|
+
}));
|
|
89
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
90
|
+
stateKey: "delegatorState",
|
|
91
|
+
addressKey: "tx.delegator"
|
|
92
|
+
}));
|
|
93
|
+
runner.use(pipes.VerifyDelegation({
|
|
94
|
+
type: "signature",
|
|
95
|
+
signerKey: "senderState",
|
|
96
|
+
delegatorKey: "delegatorState",
|
|
97
|
+
getRequirements: getDelegationRequirements
|
|
98
|
+
}));
|
|
99
|
+
runner.use(pipes.ExtractState({
|
|
100
|
+
from: "itx.tokenAddress",
|
|
101
|
+
to: "tokenState",
|
|
102
|
+
status: "INVALID_TOKEN",
|
|
103
|
+
table: "token"
|
|
104
|
+
}));
|
|
105
|
+
runner.use(pipes.VerifyInfo([{
|
|
106
|
+
error: "INVALID_TOKEN",
|
|
107
|
+
message: "Token does not have foreignToken attached",
|
|
108
|
+
fn: (ctx) => {
|
|
109
|
+
return isEmpty(ctx.tokenState?.foreignToken) === false;
|
|
110
|
+
}
|
|
111
|
+
}]));
|
|
112
|
+
runner.use(async (context, next) => {
|
|
113
|
+
const { statedb, itx } = context;
|
|
114
|
+
if (await statedb.rollup.existByToken(itx.tokenAddress, context)) return next(new CustomError("DUPLICATE_ROLLUP", "Only 1 active rollup can be created for the token"));
|
|
115
|
+
return next();
|
|
116
|
+
});
|
|
117
|
+
runner.use(pipes.ExtractState({
|
|
118
|
+
from: "itx.address",
|
|
119
|
+
to: "rollupState",
|
|
120
|
+
status: "OK",
|
|
121
|
+
table: "rollup"
|
|
122
|
+
}));
|
|
123
|
+
runner.use(pipes.VerifyInfo([{
|
|
124
|
+
error: "DUPLICATE_ROLLUP",
|
|
125
|
+
message: "This rollup already exist on chain",
|
|
126
|
+
fn: (ctx) => {
|
|
127
|
+
return isEmpty(ctx.rollupState);
|
|
128
|
+
}
|
|
129
|
+
}]));
|
|
130
|
+
runner.use(ensure_gas_default(() => ({
|
|
131
|
+
create: 1,
|
|
132
|
+
update: 2,
|
|
133
|
+
payment: 0
|
|
134
|
+
})));
|
|
135
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
136
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
137
|
+
runner.use(async (context, next) => {
|
|
138
|
+
const { tx: tx$1, formattedItx, rollupData, statedb, senderState, delegatorState, delegationState, senderUpdates, updateVaults } = context;
|
|
139
|
+
const issuer = delegatorState ? delegatorState.address : senderState.address;
|
|
140
|
+
const [newSenderState, newRollupState, newDelegationState] = await Promise.all([
|
|
141
|
+
statedb.account.update(senderState.address, account.update(senderState, {
|
|
142
|
+
nonce: tx$1.nonce,
|
|
143
|
+
...senderUpdates
|
|
144
|
+
}, context), context),
|
|
145
|
+
statedb.rollup.create(formattedItx.address, rollup.create({
|
|
146
|
+
...formattedItx,
|
|
147
|
+
issuer,
|
|
148
|
+
validators: formattedItx.seedValidators || [],
|
|
149
|
+
data: rollupData
|
|
150
|
+
}, context), context),
|
|
151
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : Promise.resolve(delegationState)
|
|
152
|
+
]);
|
|
153
|
+
if (updateVaults) await updateVaults();
|
|
154
|
+
context.senderState = newSenderState;
|
|
155
|
+
context.rollupState = newRollupState;
|
|
156
|
+
context.delegationState = newDelegationState;
|
|
157
|
+
debug("create-rollup", newRollupState);
|
|
158
|
+
next();
|
|
159
|
+
}, { persistError: true });
|
|
160
|
+
runner.use(pipes.VerifyStateDiff());
|
|
161
|
+
var create_default = runner;
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
export { create_default as default };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
2
|
+
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
3
|
+
import verify_signers_default from "./pipes/verify-signers.mjs";
|
|
4
|
+
import verify_status_default from "./pipes/verify-status.mjs";
|
|
5
|
+
import verify_evidence_default from "./pipes/verify-evidence.mjs";
|
|
6
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
7
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
8
|
+
import { isEthereumDid } from "@arcblock/did";
|
|
9
|
+
import { toStakeAddress } from "@arcblock/did-util";
|
|
10
|
+
import { account, evidence, rollup, stake } from "@ocap/state";
|
|
11
|
+
import { BN, toHex } from "@ocap/util";
|
|
12
|
+
import Debug from "debug";
|
|
13
|
+
import { Joi, patterns, schemas } from "@arcblock/validator";
|
|
14
|
+
import MerkleTree from "@ocap/merkle-tree";
|
|
15
|
+
import joinUrl from "url-join";
|
|
16
|
+
|
|
17
|
+
//#region src/protocols/rollup/join.ts
|
|
18
|
+
const debug = Debug("@ocap/tx-protocols:join-rollup");
|
|
19
|
+
const runner = new Runner("join_rollup");
|
|
20
|
+
const schema = Joi.object({
|
|
21
|
+
rollup: Joi.DID().prefix().role("ROLE_ROLLUP").required(),
|
|
22
|
+
endpoint: Joi.string().uri({ scheme: [/https?/] }).required(),
|
|
23
|
+
evidence: Joi.object({ hash: Joi.string().regex(patterns.txHash).required() }).required(),
|
|
24
|
+
signaturesList: schemas.multiSig.min(1).required(),
|
|
25
|
+
data: Joi.any().optional().allow(null)
|
|
26
|
+
}).options({
|
|
27
|
+
stripUnknown: true,
|
|
28
|
+
noDefaults: false
|
|
29
|
+
});
|
|
30
|
+
runner.use(({ itx }, next) => {
|
|
31
|
+
const { error } = schema.validate(itx);
|
|
32
|
+
return next(error ? new CustomError("INVALID_TX", `Invalid itx: ${error.message}`) : void 0);
|
|
33
|
+
});
|
|
34
|
+
runner.use(pipes.ExtractState({
|
|
35
|
+
from: "itx.rollup",
|
|
36
|
+
to: "rollupState",
|
|
37
|
+
status: "INVALID_ROLLUP",
|
|
38
|
+
table: "rollup"
|
|
39
|
+
}));
|
|
40
|
+
runner.use(verify_status_default({ paused: false }));
|
|
41
|
+
runner.use((context, next) => {
|
|
42
|
+
const { tx: tx$1, itx, rollupState } = context;
|
|
43
|
+
const validator = {
|
|
44
|
+
pk: toHex(tx$1.pk),
|
|
45
|
+
address: tx$1.from,
|
|
46
|
+
endpoint: itx.endpoint
|
|
47
|
+
};
|
|
48
|
+
if (isEthereumDid(tx$1.from) === false) return next(new CustomError("INVALID_TX", "Invalid tx.from: only ethereum compatible address can join rollup"));
|
|
49
|
+
if (rollupState?.validators.some((x) => x.address === validator.address)) return next(new CustomError("INVALID_TX", `Address ${validator.address} already exist in validators`));
|
|
50
|
+
if (rollupState?.validators.some((x) => joinUrl(x.endpoint, "/") === joinUrl(validator.endpoint, "/"))) return next(new CustomError("INVALID_TX", `Endpoint ${validator.endpoint} already exist in validators`));
|
|
51
|
+
context.newValidator = validator;
|
|
52
|
+
context.validatorsHash = MerkleTree.getListHash([validator.address]);
|
|
53
|
+
return next();
|
|
54
|
+
});
|
|
55
|
+
runner.use(verify_signers_default({
|
|
56
|
+
signersKey: "itx.signaturesList",
|
|
57
|
+
allowSender: false
|
|
58
|
+
}));
|
|
59
|
+
runner.use(verify_evidence_default({
|
|
60
|
+
evidenceKey: "validatorsHash",
|
|
61
|
+
signaturesKey: "itx.signaturesList",
|
|
62
|
+
verifyMethod: "ethVerify"
|
|
63
|
+
}));
|
|
64
|
+
runner.use(pipes.ExtractState({
|
|
65
|
+
from: "itx.evidence.hash",
|
|
66
|
+
to: "evidenceState",
|
|
67
|
+
status: "OK",
|
|
68
|
+
table: "evidence"
|
|
69
|
+
}));
|
|
70
|
+
runner.use(({ evidenceState }, next) => {
|
|
71
|
+
if (evidenceState) return next(new CustomError("INVALID_TX", "Join evidence already seen on this chain"));
|
|
72
|
+
return next();
|
|
73
|
+
});
|
|
74
|
+
runner.use(verify_signers_default({
|
|
75
|
+
signersKey: "tx.signaturesList",
|
|
76
|
+
allowSender: false
|
|
77
|
+
}));
|
|
78
|
+
runner.use(pipes.VerifyMultiSigV2({ signersKey: "signers" }));
|
|
79
|
+
runner.use((context, next) => {
|
|
80
|
+
const { tx: tx$1, itx } = context;
|
|
81
|
+
context.stakeAddress = toStakeAddress(tx$1.from, itx.rollup);
|
|
82
|
+
return next();
|
|
83
|
+
});
|
|
84
|
+
runner.use(pipes.ExtractState({
|
|
85
|
+
from: "stakeAddress",
|
|
86
|
+
to: "stakeState",
|
|
87
|
+
status: "INVALID_STAKE_STATE",
|
|
88
|
+
table: "stake"
|
|
89
|
+
}));
|
|
90
|
+
runner.use((context, next) => {
|
|
91
|
+
const { stakeState, rollupState } = context;
|
|
92
|
+
if (stakeState?.revocable === false) return next(new CustomError("INVALID_STAKE_STATE", `Staking already locked: ${stakeState.address}`));
|
|
93
|
+
const actualStake = new BN(stakeState?.tokens[rollupState?.tokenAddress || ""] || 0);
|
|
94
|
+
const requiredStake = new BN(rollupState?.minStakeAmount || 0);
|
|
95
|
+
if (actualStake.lt(requiredStake)) return next(new CustomError("INVALID_STAKE_STATE", `Staking does not match min stake amount: ${requiredStake}`));
|
|
96
|
+
return next();
|
|
97
|
+
});
|
|
98
|
+
runner.use(pipes.ExtractState({
|
|
99
|
+
from: "tx.from",
|
|
100
|
+
to: "senderState",
|
|
101
|
+
status: "INVALID_SENDER_STATE",
|
|
102
|
+
table: "account"
|
|
103
|
+
}));
|
|
104
|
+
runner.use(pipes.ExtractState({
|
|
105
|
+
from: "signers",
|
|
106
|
+
to: "signerStates",
|
|
107
|
+
status: "INVALID_SIGNER_STATE",
|
|
108
|
+
table: "account"
|
|
109
|
+
}));
|
|
110
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
111
|
+
signerKey: "signerStates",
|
|
112
|
+
stateKey: "senderState",
|
|
113
|
+
addressKey: "tx.from"
|
|
114
|
+
}));
|
|
115
|
+
runner.use(ensure_gas_default(() => ({
|
|
116
|
+
create: 1,
|
|
117
|
+
update: 3,
|
|
118
|
+
payment: 0
|
|
119
|
+
})));
|
|
120
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
121
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
122
|
+
runner.use(async (context, next) => {
|
|
123
|
+
const { tx: tx$1, itx, rollupState, statedb, senderState, stakeState, newValidator, stakeAddress, senderUpdates, updateVaults } = context;
|
|
124
|
+
const newValidators = rollupState.validators.concat([newValidator]);
|
|
125
|
+
const [newSenderState, newRollupState, newStakeState, newEvidenceState] = await Promise.all([
|
|
126
|
+
statedb.account.update(senderState.address, account.update(senderState, {
|
|
127
|
+
nonce: tx$1.nonce,
|
|
128
|
+
...senderUpdates
|
|
129
|
+
}, context), context),
|
|
130
|
+
statedb.rollup.update(rollupState.address, rollup.update(rollupState, { validators: newValidators }, context), context),
|
|
131
|
+
statedb.stake.update(stakeAddress, stake.update(stakeState, {
|
|
132
|
+
revocable: false,
|
|
133
|
+
revokeWaitingPeriod: rollupState.leaveWaitingPeriod
|
|
134
|
+
}, context), context),
|
|
135
|
+
statedb.evidence.create(itx.evidence.hash, evidence.create({
|
|
136
|
+
hash: itx.evidence.hash,
|
|
137
|
+
data: "rollup-join"
|
|
138
|
+
}, context), context)
|
|
139
|
+
]);
|
|
140
|
+
if (updateVaults) await updateVaults();
|
|
141
|
+
context.senderState = newSenderState;
|
|
142
|
+
context.rollupState = newRollupState;
|
|
143
|
+
context.stakeState = newStakeState;
|
|
144
|
+
context.evidenceState = newEvidenceState;
|
|
145
|
+
debug("join-rollup", newValidators);
|
|
146
|
+
next();
|
|
147
|
+
}, { persistError: true });
|
|
148
|
+
runner.use(pipes.VerifyStateDiff());
|
|
149
|
+
var join_default = runner;
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { join_default as default };
|