@ocap/tx-protocols 1.31.0 → 1.31.2
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/bridge-release-crypto.d.mts +39 -0
- package/esm/bridge-release-crypto.mjs +88 -0
- package/esm/bridge-release-window.d.mts +47 -0
- package/esm/bridge-release-window.mjs +62 -0
- package/esm/bridge-reserve.d.mts +24 -0
- package/esm/bridge-reserve.mjs +41 -0
- package/esm/execute.d.mts +5 -3
- package/esm/execute.mjs +593 -21
- package/esm/gas-security-guard.d.mts +7 -2
- package/esm/gas-security-guard.mjs +25 -16
- package/esm/index.d.mts +38 -19
- package/esm/index.mjs +7 -3
- package/esm/pipes/ensure-cost.mjs +36 -10
- package/esm/pipes/write-ledger.d.mts +2 -2
- package/esm/pipes/write-ledger.mjs +6 -3
- package/esm/protocols/account/delegate.mjs +6 -4
- package/esm/protocols/account/migrate.mjs +5 -3
- package/esm/protocols/account/revoke-delegate.mjs +5 -3
- package/esm/protocols/asset/acquire-v2.mjs +3 -1
- package/esm/protocols/asset/acquire-v3.mjs +4 -2
- package/esm/protocols/asset/consume.mjs +3 -1
- package/esm/protocols/asset/create.mjs +4 -2
- package/esm/protocols/asset/mint.mjs +4 -2
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +1 -1
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +1 -1
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +1 -1
- package/esm/protocols/asset/update.mjs +4 -2
- package/esm/protocols/bridge/release.d.mts +34 -0
- package/esm/protocols/bridge/release.mjs +385 -0
- package/esm/protocols/factory/create.mjs +4 -2
- package/esm/protocols/governance/claim-stake.mjs +35 -11
- package/esm/protocols/governance/return-stake.mjs +4 -2
- package/esm/protocols/governance/revoke-ledger.d.mts +34 -0
- package/esm/protocols/governance/revoke-ledger.mjs +179 -0
- package/esm/protocols/governance/revoke-stake.d.mts +7 -2
- package/esm/protocols/governance/revoke-stake.mjs +58 -29
- package/esm/protocols/governance/slash-stake.mjs +14 -4
- package/esm/protocols/governance/stake.mjs +6 -4
- package/esm/protocols/rollup/bridge-create.d.mts +13 -0
- package/esm/protocols/rollup/bridge-create.mjs +206 -0
- package/esm/protocols/rollup/bridge-membership.d.mts +10 -0
- package/esm/protocols/rollup/bridge-membership.mjs +154 -0
- package/esm/protocols/rollup/bridge-migrate.d.mts +10 -0
- package/esm/protocols/rollup/bridge-migrate.mjs +208 -0
- package/esm/protocols/rollup/bridge-v2.d.mts +113 -0
- package/esm/protocols/rollup/bridge-v2.mjs +219 -0
- package/esm/protocols/rollup/claim-reward.mjs +4 -2
- package/esm/protocols/rollup/close.d.mts +2 -1
- package/esm/protocols/rollup/close.mjs +59 -6
- package/esm/protocols/rollup/create-block.mjs +4 -2
- package/esm/protocols/rollup/create.d.mts +3 -0
- package/esm/protocols/rollup/create.mjs +45 -6
- package/esm/protocols/rollup/join.d.mts +2 -1
- package/esm/protocols/rollup/join.mjs +31 -23
- package/esm/protocols/rollup/leave.d.mts +2 -1
- package/esm/protocols/rollup/leave.mjs +32 -24
- package/esm/protocols/rollup/migrate.d.mts +2 -1
- package/esm/protocols/rollup/migrate.mjs +19 -10
- package/esm/protocols/rollup/pause.d.mts +4 -1
- package/esm/protocols/rollup/pause.mjs +50 -6
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +2 -2
- package/esm/protocols/rollup/resume.d.mts +2 -1
- package/esm/protocols/rollup/resume.mjs +26 -6
- package/esm/protocols/rollup/update.d.mts +5 -1
- package/esm/protocols/rollup/update.mjs +56 -9
- package/esm/protocols/token/create.mjs +4 -2
- package/esm/protocols/token/deposit-v2.mjs +5 -3
- package/esm/protocols/token/withdraw-v2.mjs +5 -3
- package/esm/protocols/token-factory/burn.mjs +4 -2
- package/esm/protocols/token-factory/create.mjs +8 -4
- package/esm/protocols/token-factory/mint.mjs +4 -2
- package/esm/protocols/token-factory/update.mjs +20 -5
- package/esm/protocols/trade/exchange-v2.d.mts +5 -1
- package/esm/protocols/trade/exchange-v2.mjs +89 -4
- package/esm/protocols/trade/transfer-v2.mjs +15 -4
- package/esm/protocols/trade/transfer-v3.mjs +5 -3
- package/esm/stateless-prefix.d.mts +16 -0
- package/esm/stateless-prefix.mjs +82 -0
- package/esm/tx-timer.d.mts +1 -1
- package/esm/tx-timer.mjs +1 -1
- package/esm/util.mjs +2 -2
- package/lib/bridge-release-crypto.cjs +93 -0
- package/lib/bridge-release-crypto.d.cts +39 -0
- package/lib/bridge-release-window.cjs +69 -0
- package/lib/bridge-release-window.d.cts +47 -0
- package/lib/bridge-reserve.cjs +45 -0
- package/lib/bridge-reserve.d.cts +24 -0
- package/lib/execute.cjs +594 -19
- package/lib/execute.d.cts +4 -2
- package/lib/gas-security-guard.cjs +25 -16
- package/lib/gas-security-guard.d.cts +7 -2
- package/lib/index.cjs +10 -1
- package/lib/index.d.cts +37 -18
- package/lib/pipes/ensure-cost.cjs +37 -11
- package/lib/pipes/write-ledger.cjs +6 -3
- package/lib/pipes/write-ledger.d.cts +2 -2
- package/lib/protocols/account/delegate.cjs +6 -4
- package/lib/protocols/account/migrate.cjs +5 -3
- package/lib/protocols/account/revoke-delegate.cjs +5 -3
- package/lib/protocols/asset/acquire-v2.cjs +3 -1
- package/lib/protocols/asset/acquire-v3.cjs +4 -2
- package/lib/protocols/asset/consume.cjs +3 -1
- package/lib/protocols/asset/create.cjs +4 -2
- package/lib/protocols/asset/mint.cjs +4 -2
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +1 -1
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +1 -1
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +1 -1
- package/lib/protocols/asset/update.cjs +4 -2
- package/lib/protocols/bridge/release.cjs +390 -0
- package/lib/protocols/bridge/release.d.cts +34 -0
- package/lib/protocols/factory/create.cjs +4 -2
- package/lib/protocols/governance/claim-stake.cjs +35 -11
- package/lib/protocols/governance/return-stake.cjs +4 -2
- package/lib/protocols/governance/revoke-ledger.cjs +184 -0
- package/lib/protocols/governance/revoke-ledger.d.cts +34 -0
- package/lib/protocols/governance/revoke-stake.cjs +58 -29
- package/lib/protocols/governance/revoke-stake.d.cts +7 -2
- package/lib/protocols/governance/slash-stake.cjs +14 -4
- package/lib/protocols/governance/stake.cjs +6 -4
- package/lib/protocols/rollup/bridge-create.cjs +209 -0
- package/lib/protocols/rollup/bridge-create.d.cts +13 -0
- package/lib/protocols/rollup/bridge-membership.cjs +157 -0
- package/lib/protocols/rollup/bridge-membership.d.cts +10 -0
- package/lib/protocols/rollup/bridge-migrate.cjs +212 -0
- package/lib/protocols/rollup/bridge-migrate.d.cts +10 -0
- package/lib/protocols/rollup/bridge-v2.cjs +244 -0
- package/lib/protocols/rollup/bridge-v2.d.cts +113 -0
- package/lib/protocols/rollup/claim-reward.cjs +4 -2
- package/lib/protocols/rollup/close.cjs +59 -6
- package/lib/protocols/rollup/close.d.cts +2 -1
- package/lib/protocols/rollup/create-block.cjs +4 -2
- package/lib/protocols/rollup/create.cjs +45 -6
- package/lib/protocols/rollup/create.d.cts +3 -0
- package/lib/protocols/rollup/join.cjs +31 -23
- package/lib/protocols/rollup/join.d.cts +2 -1
- package/lib/protocols/rollup/leave.cjs +32 -24
- package/lib/protocols/rollup/leave.d.cts +2 -1
- package/lib/protocols/rollup/migrate.cjs +19 -10
- package/lib/protocols/rollup/migrate.d.cts +2 -1
- package/lib/protocols/rollup/pause.cjs +50 -6
- package/lib/protocols/rollup/pause.d.cts +4 -1
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +2 -2
- package/lib/protocols/rollup/resume.cjs +26 -6
- package/lib/protocols/rollup/resume.d.cts +2 -1
- package/lib/protocols/rollup/update.cjs +56 -9
- package/lib/protocols/rollup/update.d.cts +5 -1
- package/lib/protocols/token/create.cjs +4 -2
- package/lib/protocols/token/deposit-v2.cjs +5 -3
- package/lib/protocols/token/withdraw-v2.cjs +5 -3
- package/lib/protocols/token-factory/burn.cjs +4 -2
- package/lib/protocols/token-factory/create.cjs +8 -4
- package/lib/protocols/token-factory/mint.cjs +4 -2
- package/lib/protocols/token-factory/update.cjs +20 -5
- package/lib/protocols/trade/exchange-v2.cjs +90 -3
- package/lib/protocols/trade/exchange-v2.d.cts +5 -1
- package/lib/protocols/trade/transfer-v2.cjs +15 -4
- package/lib/protocols/trade/transfer-v3.cjs +5 -3
- package/lib/stateless-prefix.cjs +88 -0
- package/lib/stateless-prefix.d.cts +16 -0
- package/lib/tx-timer.cjs +1 -1
- package/lib/tx-timer.d.cts +1 -1
- package/lib/util.cjs +2 -2
- package/package.json +16 -16
- package/tools/fixtures.ts +9 -4
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_util = require('../../util.cjs');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
5
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
6
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
7
|
+
let _arcblock_did = require("@arcblock/did");
|
|
8
|
+
let _ocap_state = require("@ocap/state");
|
|
9
|
+
let _ocap_util = require("@ocap/util");
|
|
10
|
+
|
|
11
|
+
//#region src/protocols/rollup/bridge-v2.ts
|
|
12
|
+
/**
|
|
13
|
+
* Shared building blocks of the v2 (on-chain bridge) branches of the rollup governance protocols
|
|
14
|
+
* (bridge/docs/ocap-protocols.md §v2.2). The v1 code paths do not import anything from here except the `ifV1` guard.
|
|
15
|
+
*/
|
|
16
|
+
/** Whether legacy Rollup v1 protocols remain enabled in chain config (`true` when `transaction.rollupV1` is absent). */
|
|
17
|
+
const isRollupV1Enabled = (config) => config?.transaction?.rollupV1?.enabled !== false;
|
|
18
|
+
/**
|
|
19
|
+
* Reads the target rollup before the v1 itx schema runs, so a v2 bridge can skip the v1 stake / fee checks while the v1
|
|
20
|
+
* pipe order (and its error codes) stays exactly as it was. Never fails on missing state (reported later by ExtractState),
|
|
21
|
+
* unless `opts.rejectV1IfDisabled` is set and an existing v1 rollup is targeted while Rollup v1 is sunset.
|
|
22
|
+
*/
|
|
23
|
+
const peekBridge = (rollupKey, opts) => async function PeekBridge(context, next) {
|
|
24
|
+
context.isBridgeV2 = false;
|
|
25
|
+
try {
|
|
26
|
+
const address = context.itx?.rollup;
|
|
27
|
+
if (address) {
|
|
28
|
+
const state = await context.statedb.rollup.get(address, context);
|
|
29
|
+
if (state) {
|
|
30
|
+
context.isBridgeV2 = _ocap_state.rollup.isBridgeV2(state);
|
|
31
|
+
if (!context.isBridgeV2 && opts?.rejectV1IfDisabled && !isRollupV1Enabled(context.config)) return next(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", `${opts.rejectV1IfDisabled} is disabled because Rollup v1 is sunset`));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
return next();
|
|
36
|
+
};
|
|
37
|
+
/** Rejects a non-v2 rollup governance action after `loadBridge` when `transaction.rollupV1.enabled === false`. */
|
|
38
|
+
const rejectRollupV1IfDisabled = (txName) => function RejectRollupV1IfDisabled(context, next) {
|
|
39
|
+
if (!context.isBridgeV2 && !isRollupV1Enabled(context.config)) return next(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", `${txName} is disabled because Rollup v1 is sunset`));
|
|
40
|
+
return next();
|
|
41
|
+
};
|
|
42
|
+
/** Run `pipe` for v1 rollups only (v2 bridges have their own pipes). */
|
|
43
|
+
const ifV1 = (pipe) => (context, next) => context.isBridgeV2 ? next() : pipe(context, next);
|
|
44
|
+
/** Run `pipe` for v2 bridges only. */
|
|
45
|
+
const ifV2 = (pipe) => (context, next) => context.isBridgeV2 ? pipe(context, next) : next();
|
|
46
|
+
const txSecOf = (context) => {
|
|
47
|
+
try {
|
|
48
|
+
return _ocap_state.rollup.toSeconds(context.txTime);
|
|
49
|
+
} catch {
|
|
50
|
+
throw new _ocap_util_lib_error.CustomError("INVALID_TX", "tx time is required for a v2 bridge transaction");
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* After ExtractState: keep the stored row as `rollupStateOrig` and continue with the row as of `txTime`, i.e. with a
|
|
55
|
+
* due owner replacement already applied (owner and seed_validators). Commits go through `commitBridge`.
|
|
56
|
+
*/
|
|
57
|
+
const loadBridge = function LoadBridge(context, next) {
|
|
58
|
+
if (!context.isBridgeV2) return next();
|
|
59
|
+
const txSec = txSecOf(context);
|
|
60
|
+
context.txSec = txSec;
|
|
61
|
+
context.rollupStateOrig = context.rollupState;
|
|
62
|
+
context.rollupState = _ocap_state.rollup.materializeBridgeAt(context.rollupState, txSec);
|
|
63
|
+
context.bridgeBody = _ocap_state.rollup.getBridgeBody(context.rollupState);
|
|
64
|
+
return next();
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Build the `updates` object for `rollup.updateBridge(rollupStateOrig, updates)` from the materialised row, a new body
|
|
68
|
+
* and optional row overrides. The informational mirrors (`validators`, signer counts) are re-derived from the new body.
|
|
69
|
+
*/
|
|
70
|
+
function bridgeUpdates(context, body, rowOverrides = {}) {
|
|
71
|
+
const base = context.rollupState;
|
|
72
|
+
const row = {
|
|
73
|
+
...base,
|
|
74
|
+
...rowOverrides,
|
|
75
|
+
data: {
|
|
76
|
+
...base.data,
|
|
77
|
+
value: body
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const synced = _ocap_state.rollup.syncBridgeView(row, context.txSec);
|
|
81
|
+
return {
|
|
82
|
+
validators: synced.validators,
|
|
83
|
+
seedValidators: synced.seedValidators,
|
|
84
|
+
minSignerCount: synced.minSignerCount,
|
|
85
|
+
maxSignerCount: synced.maxSignerCount,
|
|
86
|
+
data: synced.data,
|
|
87
|
+
paused: synced.paused,
|
|
88
|
+
closed: synced.closed,
|
|
89
|
+
vaultAddress: synced.vaultAddress,
|
|
90
|
+
vaultHistory: synced.vaultHistory
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Governance authority of a v2 bridge. The signers are `tx.from` plus the signers of `tx.signatures` (their signatures
|
|
95
|
+
* are verified by VerifyMultiSigV2 right after this pipe, `tx.from` by the primary signature check). Every signer must
|
|
96
|
+
* be the owner or a validator of the epoch active at `txTime`; delegation is not allowed.
|
|
97
|
+
*/
|
|
98
|
+
const VerifyBridgeAuthority = (rule) => function VerifyBridgeAuthorityPipe(context, next) {
|
|
99
|
+
if (!context.isBridgeV2) return next();
|
|
100
|
+
const r = typeof rule === "function" ? rule(context) : rule;
|
|
101
|
+
const body = context.bridgeBody;
|
|
102
|
+
const { tx } = context;
|
|
103
|
+
if (tx.delegator) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Delegation is not allowed for bridge governance"));
|
|
104
|
+
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(context, "tx.signaturesList");
|
|
105
|
+
if (signatures.some((x) => Boolean(x.delegator))) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Delegation is not allowed for bridge governance"));
|
|
106
|
+
const signerList = signatures.map((x) => x.signer);
|
|
107
|
+
if (new Set(signerList.map(_ocap_state.rollup.lc)).size !== signerList.length) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Duplicate signers are not allowed"));
|
|
108
|
+
const signers = new Set([_ocap_state.rollup.lc(tx.from), ...signerList.map(_ocap_state.rollup.lc)]);
|
|
109
|
+
const owner = _ocap_state.rollup.lc(body.owner);
|
|
110
|
+
const active = _ocap_state.rollup.activeEpochAt(r.quorumBody ? r.quorumBody(context) : body, context.txSec);
|
|
111
|
+
if (!active) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "The bridge has no active epoch yet"));
|
|
112
|
+
const validators = new Set(active.validators);
|
|
113
|
+
for (const s of signers) if (s !== owner && !validators.has(s)) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Signer ${s} is neither the bridge owner nor a validator of the active epoch`));
|
|
114
|
+
if (r.owner === "required" && !signers.has(owner)) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "The bridge owner must sign this transaction"));
|
|
115
|
+
if (r.owner === "alone" && (signers.size !== 1 || !signers.has(owner))) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Only the current bridge owner may sign this transaction, alone"));
|
|
116
|
+
if (r.quorum !== "none") {
|
|
117
|
+
const n = active.validators.length;
|
|
118
|
+
const need = r.quorum === "threshold" ? active.threshold : Math.ceil(2 * n / 3);
|
|
119
|
+
const have = [...signers].filter((s) => validators.has(s)).length;
|
|
120
|
+
if (have < need) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Invalid tx.signatures: expect at least ${need} validator signatures of epoch ${active.epoch}, got ${have}`));
|
|
121
|
+
}
|
|
122
|
+
context.signers = signerList;
|
|
123
|
+
return next();
|
|
124
|
+
};
|
|
125
|
+
/** `keccak256("arc-bridge:gateway:" || uint256 ethChainId || address gateway)` */
|
|
126
|
+
const gatewayIndexKey = (ethChainId, gateway) => (0, _ocap_mcrypto.bridgeEvidenceKey)("arc-bridge:gateway:", [{ uint: ethChainId }, { address: gateway }]);
|
|
127
|
+
/**
|
|
128
|
+
* `keccak256("arc-bridge:token:" || ocapToken)`: one not-final bridge per OCAP token. A closed bridge keeps its token
|
|
129
|
+
* until finality, so `existByToken` (which only knows `closed`) is complemented by this index.
|
|
130
|
+
*/
|
|
131
|
+
const tokenIndexKey = (ocapToken) => (0, _ocap_mcrypto.bridgeEvidenceKey)("arc-bridge:token:", [{ text: (0, _ocap_util.toAddress)(ocapToken) }]);
|
|
132
|
+
/** Tombstone of one signed membership approval, keyed by (bridge, epoch, digest). */
|
|
133
|
+
const membershipTombstoneKey = (bridge, epoch, digestHex) => (0, _ocap_mcrypto.bridgeEvidenceKey)("arc-bridge:membership:", [
|
|
134
|
+
{ uint: epoch },
|
|
135
|
+
{ bytes32: digestHex },
|
|
136
|
+
{ text: bridge }
|
|
137
|
+
]);
|
|
138
|
+
/**
|
|
139
|
+
* Whether an uniqueness index entry still blocks a new bridge at `txSec`. The entry of a closed bridge carries the
|
|
140
|
+
* bridge's `finalAt` and stops blocking once it has passed: release at finality needs no timer.
|
|
141
|
+
*/
|
|
142
|
+
const isBridgeIndex = (entry, kind) => {
|
|
143
|
+
const value = entry?.data?.value;
|
|
144
|
+
return entry?.data?.type === "json" && !!value && typeof value === "object" && value.kind === kind && typeof value.bridge === "string" && value.bridge.length > 0;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* `kind` (when given) restricts the check to genuine index entries written by `create_rollup`: any other evidence row at
|
|
148
|
+
* an `arc-bridge:*` key (a v1 deposit / leave record, a squatted row) is not an index entry, blocks nobody and is
|
|
149
|
+
* overwritten by the next `create_rollup` (index squatting).
|
|
150
|
+
*/
|
|
151
|
+
const indexBlocks = (entry, txSec, kind) => {
|
|
152
|
+
if (!entry) return false;
|
|
153
|
+
if (kind && !isBridgeIndex(entry, kind)) return false;
|
|
154
|
+
const finalAt = entry.data?.value?.finalAt;
|
|
155
|
+
return !(typeof finalAt === "number" && finalAt <= txSec);
|
|
156
|
+
};
|
|
157
|
+
const indexData = (kind, bridge, finalAt) => ({
|
|
158
|
+
type: "json",
|
|
159
|
+
value: {
|
|
160
|
+
kind,
|
|
161
|
+
bridge,
|
|
162
|
+
finalAt
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const createIndexEvidence = (statedb, key, data, context) => statedb.evidence.create(key, _ocap_state.evidence.create({
|
|
166
|
+
hash: key,
|
|
167
|
+
data
|
|
168
|
+
}, context), context);
|
|
169
|
+
const upsertIndexEvidence = async (statedb, key, data, context) => {
|
|
170
|
+
if (await statedb.evidence.get(key, context)) return statedb.evidence.update(key, _ocap_state.evidence.create({
|
|
171
|
+
hash: key,
|
|
172
|
+
data
|
|
173
|
+
}, context), context);
|
|
174
|
+
return createIndexEvidence(statedb, key, data, context);
|
|
175
|
+
};
|
|
176
|
+
/** Decoded `itx.data.value` as a plain object (v2 governance payloads are JSON objects). */
|
|
177
|
+
function readItxData(itx) {
|
|
178
|
+
const value = require_util.decodeAnySafe(itx?.data ?? null)?.value;
|
|
179
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: data must be a JSON object for a v2 bridge");
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
/** Digest of the membership struct for `epoch` (validators already sorted ascending by the epoch rules). */
|
|
183
|
+
function membershipDigest(m) {
|
|
184
|
+
return (0, _ocap_mcrypto.computeBridgeMembershipDigest)({
|
|
185
|
+
ocapChainId: m.ocapChainId,
|
|
186
|
+
bridge: m.bridge,
|
|
187
|
+
epoch: m.epoch.epoch,
|
|
188
|
+
validators: m.epoch.validators,
|
|
189
|
+
threshold: m.epoch.threshold,
|
|
190
|
+
activeFrom: m.epoch.activeFrom,
|
|
191
|
+
voucherValidity: m.epoch.voucherValidity,
|
|
192
|
+
endpointsHash: (0, _ocap_mcrypto.computeBridgeEndpointsHash)(m.endpoints),
|
|
193
|
+
action: m.action
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/** Recover every signature over `digest`; throws on a malformed or non-canonical signature or on duplicate signers. */
|
|
197
|
+
function recoverAll(digest, signatures) {
|
|
198
|
+
const out = [];
|
|
199
|
+
const seen = /* @__PURE__ */ new Set();
|
|
200
|
+
for (const sig of signatures) {
|
|
201
|
+
const rec = (0, _ocap_mcrypto.recoverBridgeSigner)(digest, sig);
|
|
202
|
+
if (!rec) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Invalid BridgeMembership signature");
|
|
203
|
+
if (seen.has(rec.address)) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Duplicate BridgeMembership signer ${rec.address}`);
|
|
204
|
+
seen.add(rec.address);
|
|
205
|
+
out.push({
|
|
206
|
+
address: rec.address,
|
|
207
|
+
pk: (0, _ocap_util.toHex)(rec.publicKey.slice(1))
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
/** v1-only transactions (block creation, rewards, deposit / withdraw through a rollup) are refused for a v2 bridge or when Rollup v1 is sunset. */
|
|
213
|
+
const rejectBridgeV2 = (txName) => function RejectBridgeV2(context, next) {
|
|
214
|
+
if (_ocap_state.rollup.isBridgeV2(context.rollupState)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `${txName} is not supported by a v2 bridge`));
|
|
215
|
+
if (!isRollupV1Enabled(context.config)) return next(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", `${txName} is disabled because Rollup v1 is sunset`));
|
|
216
|
+
return next();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
exports.BRIDGE_MEMBERSHIP_ACTION = _ocap_mcrypto.BRIDGE_MEMBERSHIP_ACTION;
|
|
221
|
+
exports.VerifyBridgeAuthority = VerifyBridgeAuthority;
|
|
222
|
+
exports.bridgeUpdates = bridgeUpdates;
|
|
223
|
+
exports.createIndexEvidence = createIndexEvidence;
|
|
224
|
+
exports.gatewayIndexKey = gatewayIndexKey;
|
|
225
|
+
exports.ifV1 = ifV1;
|
|
226
|
+
exports.ifV2 = ifV2;
|
|
227
|
+
exports.indexBlocks = indexBlocks;
|
|
228
|
+
exports.indexData = indexData;
|
|
229
|
+
exports.isBridgeIndex = isBridgeIndex;
|
|
230
|
+
exports.isEthereumDid = _arcblock_did.isEthereumDid;
|
|
231
|
+
exports.isRollupV1Enabled = isRollupV1Enabled;
|
|
232
|
+
exports.loadBridge = loadBridge;
|
|
233
|
+
exports.membershipDigest = membershipDigest;
|
|
234
|
+
exports.membershipTombstoneKey = membershipTombstoneKey;
|
|
235
|
+
exports.peekBridge = peekBridge;
|
|
236
|
+
exports.readItxData = readItxData;
|
|
237
|
+
exports.recoverAll = recoverAll;
|
|
238
|
+
exports.rejectBridgeV2 = rejectBridgeV2;
|
|
239
|
+
exports.rejectRollupV1IfDisabled = rejectRollupV1IfDisabled;
|
|
240
|
+
exports.reserveIndexKey = _ocap_tx_pipeline.reserveIndexKey;
|
|
241
|
+
exports.toHexDigest = _ocap_mcrypto.toHexDigest;
|
|
242
|
+
exports.tokenIndexKey = tokenIndexKey;
|
|
243
|
+
exports.txSecOf = txSecOf;
|
|
244
|
+
exports.upsertIndexEvidence = upsertIndexEvidence;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { rollup } from "@ocap/state";
|
|
2
|
+
import { PipeFunction, reserveIndexKey } from "@ocap/tx-pipeline";
|
|
3
|
+
import { isEthereumDid } from "@arcblock/did";
|
|
4
|
+
import { BRIDGE_MEMBERSHIP_ACTION, toHexDigest } from "@ocap/mcrypto";
|
|
5
|
+
|
|
6
|
+
//#region src/protocols/rollup/bridge-v2.d.ts
|
|
7
|
+
|
|
8
|
+
type Ctx = any;
|
|
9
|
+
type AnyObj = Record<string, any>;
|
|
10
|
+
/** Whether legacy Rollup v1 protocols remain enabled in chain config (`true` when `transaction.rollupV1` is absent). */
|
|
11
|
+
declare const isRollupV1Enabled: (config: AnyObj | undefined) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Reads the target rollup before the v1 itx schema runs, so a v2 bridge can skip the v1 stake / fee checks while the v1
|
|
14
|
+
* pipe order (and its error codes) stays exactly as it was. Never fails on missing state (reported later by ExtractState),
|
|
15
|
+
* unless `opts.rejectV1IfDisabled` is set and an existing v1 rollup is targeted while Rollup v1 is sunset.
|
|
16
|
+
*/
|
|
17
|
+
declare const peekBridge: (rollupKey: "itx.rollup", opts?: {
|
|
18
|
+
rejectV1IfDisabled?: string;
|
|
19
|
+
}) => PipeFunction<Ctx>;
|
|
20
|
+
/** Rejects a non-v2 rollup governance action after `loadBridge` when `transaction.rollupV1.enabled === false`. */
|
|
21
|
+
declare const rejectRollupV1IfDisabled: (txName: string) => PipeFunction<Ctx>;
|
|
22
|
+
/** Run `pipe` for v1 rollups only (v2 bridges have their own pipes). */
|
|
23
|
+
declare const ifV1: (pipe: PipeFunction<Ctx>) => PipeFunction<Ctx>;
|
|
24
|
+
/** Run `pipe` for v2 bridges only. */
|
|
25
|
+
declare const ifV2: (pipe: PipeFunction<Ctx>) => PipeFunction<Ctx>;
|
|
26
|
+
declare const txSecOf: (context: Ctx) => number;
|
|
27
|
+
/**
|
|
28
|
+
* After ExtractState: keep the stored row as `rollupStateOrig` and continue with the row as of `txTime`, i.e. with a
|
|
29
|
+
* due owner replacement already applied (owner and seed_validators). Commits go through `commitBridge`.
|
|
30
|
+
*/
|
|
31
|
+
declare const loadBridge: PipeFunction<Ctx>;
|
|
32
|
+
/**
|
|
33
|
+
* Build the `updates` object for `rollup.updateBridge(rollupStateOrig, updates)` from the materialised row, a new body
|
|
34
|
+
* and optional row overrides. The informational mirrors (`validators`, signer counts) are re-derived from the new body.
|
|
35
|
+
*/
|
|
36
|
+
declare function bridgeUpdates(context: Ctx, body: AnyObj, rowOverrides?: AnyObj): AnyObj;
|
|
37
|
+
interface AuthorityRule {
|
|
38
|
+
/** the body whose active epoch supplies the quorum (default: the bridge being governed) */
|
|
39
|
+
quorumBody?: (context: Ctx) => rollup.BridgeBodyV2;
|
|
40
|
+
/** `required`: the owner must sign; `alone`: only the owner may sign; `none`: owner not needed */
|
|
41
|
+
owner: 'required' | 'alone' | 'none';
|
|
42
|
+
/** `threshold`: >= threshold of the active epoch; `twoThirds`: >= ceil(2n/3); `none` */
|
|
43
|
+
quorum: 'threshold' | 'twoThirds' | 'none';
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Governance authority of a v2 bridge. The signers are `tx.from` plus the signers of `tx.signatures` (their signatures
|
|
47
|
+
* are verified by VerifyMultiSigV2 right after this pipe, `tx.from` by the primary signature check). Every signer must
|
|
48
|
+
* be the owner or a validator of the epoch active at `txTime`; delegation is not allowed.
|
|
49
|
+
*/
|
|
50
|
+
declare const VerifyBridgeAuthority: (rule: AuthorityRule | ((context: Ctx) => AuthorityRule)) => PipeFunction<Ctx>;
|
|
51
|
+
/** `keccak256("arc-bridge:gateway:" || uint256 ethChainId || address gateway)` */
|
|
52
|
+
declare const gatewayIndexKey: (ethChainId: string, gateway: string) => string;
|
|
53
|
+
/**
|
|
54
|
+
* `keccak256("arc-bridge:token:" || ocapToken)`: one not-final bridge per OCAP token. A closed bridge keeps its token
|
|
55
|
+
* until finality, so `existByToken` (which only knows `closed`) is complemented by this index.
|
|
56
|
+
*/
|
|
57
|
+
declare const tokenIndexKey: (ocapToken: string) => string;
|
|
58
|
+
/** Tombstone of one signed membership approval, keyed by (bridge, epoch, digest). */
|
|
59
|
+
declare const membershipTombstoneKey: (bridge: string, epoch: number, digestHex: string) => string;
|
|
60
|
+
/**
|
|
61
|
+
* Whether an uniqueness index entry still blocks a new bridge at `txSec`. The entry of a closed bridge carries the
|
|
62
|
+
* bridge's `finalAt` and stops blocking once it has passed: release at finality needs no timer.
|
|
63
|
+
*/
|
|
64
|
+
declare const isBridgeIndex: (entry: AnyObj | null | undefined, kind: string) => boolean;
|
|
65
|
+
/**
|
|
66
|
+
* `kind` (when given) restricts the check to genuine index entries written by `create_rollup`: any other evidence row at
|
|
67
|
+
* an `arc-bridge:*` key (a v1 deposit / leave record, a squatted row) is not an index entry, blocks nobody and is
|
|
68
|
+
* overwritten by the next `create_rollup` (index squatting).
|
|
69
|
+
*/
|
|
70
|
+
declare const indexBlocks: (entry: AnyObj | null | undefined, txSec: number, kind?: string) => boolean;
|
|
71
|
+
declare const indexData: (kind: string, bridge: string, finalAt: number | null) => {
|
|
72
|
+
type: string;
|
|
73
|
+
value: {
|
|
74
|
+
kind: string;
|
|
75
|
+
bridge: string;
|
|
76
|
+
finalAt: number | null;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
declare const createIndexEvidence: (statedb: Ctx, key: string, data: AnyObj, context: Ctx) => any;
|
|
80
|
+
declare const upsertIndexEvidence: (statedb: Ctx, key: string, data: AnyObj, context: Ctx) => Promise<any>;
|
|
81
|
+
/** Decoded `itx.data.value` as a plain object (v2 governance payloads are JSON objects). */
|
|
82
|
+
declare function readItxData(itx: AnyObj): AnyObj;
|
|
83
|
+
interface MembershipCheck {
|
|
84
|
+
ocapChainId: string;
|
|
85
|
+
bridge: string;
|
|
86
|
+
epoch: rollup.BridgeEpoch;
|
|
87
|
+
action: number;
|
|
88
|
+
/** endpoint of each validator of `epoch`, same order as `epoch.validators` (bound into the digest) */
|
|
89
|
+
endpoints: string[];
|
|
90
|
+
/** signatures as hex strings */
|
|
91
|
+
signatures: string[];
|
|
92
|
+
}
|
|
93
|
+
interface RecoveredSignature {
|
|
94
|
+
address: string;
|
|
95
|
+
/** 64-byte uncompressed public key as hex, the shape OCAP ethereum wallets use */
|
|
96
|
+
pk: string;
|
|
97
|
+
}
|
|
98
|
+
/** Digest of the membership struct for `epoch` (validators already sorted ascending by the epoch rules). */
|
|
99
|
+
declare function membershipDigest(m: Omit<MembershipCheck, 'signatures'>): Uint8Array;
|
|
100
|
+
/** Recover every signature over `digest`; throws on a malformed or non-canonical signature or on duplicate signers. */
|
|
101
|
+
declare function recoverAll(digest: Uint8Array, signatures: string[]): RecoveredSignature[];
|
|
102
|
+
/** v1-only transactions (block creation, rewards, deposit / withdraw through a rollup) are refused for a v2 bridge or when Rollup v1 is sunset. */
|
|
103
|
+
declare const rejectBridgeV2: (txName: string) => PipeFunction<Ctx>;
|
|
104
|
+
/** Context fields the v2 pipes add; mixed into each protocol's Context type. */
|
|
105
|
+
type BridgeCtxMixin = {
|
|
106
|
+
isBridgeV2?: boolean;
|
|
107
|
+
txSec?: number;
|
|
108
|
+
bridgeBody?: any;
|
|
109
|
+
rollupStateOrig?: any;
|
|
110
|
+
[key: string]: any;
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
export { AuthorityRule, BRIDGE_MEMBERSHIP_ACTION, BridgeCtxMixin, MembershipCheck, RecoveredSignature, VerifyBridgeAuthority, bridgeUpdates, createIndexEvidence, gatewayIndexKey, ifV1, ifV2, indexBlocks, indexData, isBridgeIndex, isEthereumDid, isRollupV1Enabled, loadBridge, membershipDigest, membershipTombstoneKey, peekBridge, readItxData, recoverAll, rejectBridgeV2, rejectRollupV1IfDisabled, reserveIndexKey, toHexDigest, tokenIndexKey, txSecOf, upsertIndexEvidence };
|
|
@@ -3,14 +3,15 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
6
7
|
const require_protocols_rollup_pipes_ensure_validator = require('./pipes/ensure-validator.cjs');
|
|
7
8
|
const require_protocols_rollup_pipes_verify_signers = require('./pipes/verify-signers.cjs');
|
|
8
9
|
const require_protocols_rollup_pipes_verify_status = require('./pipes/verify-status.cjs');
|
|
10
|
+
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
11
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
9
12
|
let _arcblock_did_util = require("@arcblock/did-util");
|
|
10
13
|
let _ocap_state = require("@ocap/state");
|
|
11
|
-
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
12
14
|
let _ocap_util = require("@ocap/util");
|
|
13
|
-
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
14
15
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
15
16
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
16
17
|
let debug = require("debug");
|
|
@@ -66,6 +67,7 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
66
67
|
status: "INVALID_ROLLUP",
|
|
67
68
|
table: "rollup"
|
|
68
69
|
}));
|
|
70
|
+
runner.use(require_protocols_rollup_bridge_v2.rejectBridgeV2("claim_block_reward"));
|
|
69
71
|
runner.use((context, next) => {
|
|
70
72
|
const { itx, rollupState } = context;
|
|
71
73
|
if (_ocap_state.rollup.getRollupTargetType(rollupState) === "EVM" && (0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
|
|
@@ -2,13 +2,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
2
2
|
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
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
5
6
|
const require_protocols_rollup_pipes_ensure_validator = require('./pipes/ensure-validator.cjs');
|
|
6
7
|
const require_protocols_rollup_pipes_verify_signers = require('./pipes/verify-signers.cjs');
|
|
7
8
|
const require_protocols_rollup_pipes_verify_status = require('./pipes/verify-status.cjs');
|
|
8
|
-
let _arcblock_did_util = require("@arcblock/did-util");
|
|
9
|
-
let _ocap_state = require("@ocap/state");
|
|
10
9
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
11
10
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
11
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
12
|
+
let _ocap_state = require("@ocap/state");
|
|
12
13
|
let _arcblock_validator = require("@arcblock/validator");
|
|
13
14
|
|
|
14
15
|
//#region src/protocols/rollup/close.ts
|
|
@@ -28,6 +29,7 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
|
28
29
|
})),
|
|
29
30
|
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
30
31
|
}), (0, _arcblock_validator.vStripUnknown)());
|
|
32
|
+
runner.use(require_protocols_rollup_bridge_v2.peekBridge("itx.rollup"));
|
|
31
33
|
runner.use(({ itx }, next) => {
|
|
32
34
|
const { error } = (0, _arcblock_validator.vValidate)(schema, itx);
|
|
33
35
|
return next(error ? new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`) : void 0);
|
|
@@ -38,14 +40,33 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
38
40
|
status: "INVALID_ROLLUP",
|
|
39
41
|
table: "rollup"
|
|
40
42
|
}));
|
|
41
|
-
runner.use(
|
|
42
|
-
runner.use(
|
|
43
|
+
runner.use(require_protocols_rollup_bridge_v2.loadBridge);
|
|
44
|
+
runner.use(require_protocols_rollup_bridge_v2.ifV1(require_protocols_rollup_pipes_ensure_validator.default(true)));
|
|
45
|
+
runner.use(require_protocols_rollup_bridge_v2.ifV1(require_protocols_rollup_pipes_verify_status.default({
|
|
43
46
|
paused: true,
|
|
44
47
|
closed: false
|
|
45
|
-
}));
|
|
46
|
-
runner.use(
|
|
48
|
+
})));
|
|
49
|
+
runner.use((context, next) => {
|
|
50
|
+
if (!context.isBridgeV2) return next();
|
|
51
|
+
const state = context.rollupState;
|
|
52
|
+
if (state.closed) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "The rollup is closed"));
|
|
53
|
+
if (!_ocap_state.rollup.isQuorumPaused(state)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "A bridge can only be closed while it is under a quorum pause"));
|
|
54
|
+
try {
|
|
55
|
+
const cutoff = require_protocols_rollup_bridge_v2.readItxData(context.itx).depositCutoffEthBlock;
|
|
56
|
+
if (typeof cutoff !== "string" || !/^(0|[1-9][0-9]*)$/.test(cutoff)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.data.depositCutoffEthBlock must be a decimal string");
|
|
57
|
+
context.depositCutoffEthBlock = cutoff;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
return next(err);
|
|
60
|
+
}
|
|
61
|
+
return next();
|
|
62
|
+
});
|
|
63
|
+
runner.use(require_protocols_rollup_bridge_v2.ifV1(require_protocols_rollup_pipes_verify_signers.default({
|
|
47
64
|
signersKey: "tx.signaturesList",
|
|
48
65
|
allowSender: true
|
|
66
|
+
})));
|
|
67
|
+
runner.use(require_protocols_rollup_bridge_v2.VerifyBridgeAuthority({
|
|
68
|
+
owner: "required",
|
|
69
|
+
quorum: "threshold"
|
|
49
70
|
}));
|
|
50
71
|
runner.use(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" }));
|
|
51
72
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
@@ -66,6 +87,10 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
66
87
|
addressKey: "tx.from"
|
|
67
88
|
}));
|
|
68
89
|
runner.use((context, next) => {
|
|
90
|
+
if (context.isBridgeV2) {
|
|
91
|
+
context.stakeAddresses = [];
|
|
92
|
+
return next();
|
|
93
|
+
}
|
|
69
94
|
const { validators, address } = context.rollupState;
|
|
70
95
|
context.stakeAddresses = validators.map((x) => (0, _arcblock_did_util.toStakeAddress)(x.address, address));
|
|
71
96
|
return next();
|
|
@@ -86,6 +111,34 @@ runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
|
86
111
|
runner.use(async (context, next) => {
|
|
87
112
|
const { tx, itx, statedb, senderUpdates, updateVaults, rollupState, senderState } = context;
|
|
88
113
|
const stakeStates = context.stakeStates || [];
|
|
114
|
+
if (context.isBridgeV2) {
|
|
115
|
+
const body = context.bridgeBody;
|
|
116
|
+
const txSec = context.txSec;
|
|
117
|
+
const finalAt = txSec + _ocap_state.rollup.maxVoucherValidity(body) + body.refundDelay;
|
|
118
|
+
const closedBody = {
|
|
119
|
+
...body,
|
|
120
|
+
epochs: body.epochs.filter((e) => e.activeFrom <= txSec),
|
|
121
|
+
closingAt: txSec,
|
|
122
|
+
finalAt,
|
|
123
|
+
depositCutoffEthBlock: context.depositCutoffEthBlock
|
|
124
|
+
};
|
|
125
|
+
const [newSender, newRollup] = await Promise.all([
|
|
126
|
+
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
127
|
+
nonce: tx.nonce,
|
|
128
|
+
...senderUpdates
|
|
129
|
+
}, context), context),
|
|
130
|
+
statedb.rollup.update(itx.rollup, _ocap_state.rollup.updateBridge(context.rollupStateOrig, require_protocols_rollup_bridge_v2.bridgeUpdates(context, closedBody, {
|
|
131
|
+
closed: true,
|
|
132
|
+
paused: true
|
|
133
|
+
}), context), context),
|
|
134
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, require_protocols_rollup_bridge_v2.gatewayIndexKey(body.ethChainId, body.gateway), require_protocols_rollup_bridge_v2.indexData("bridge-gateway", rollupState.address, finalAt), context),
|
|
135
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, require_protocols_rollup_bridge_v2.tokenIndexKey(rollupState.tokenAddress), require_protocols_rollup_bridge_v2.indexData("bridge-token", rollupState.address, finalAt), context)
|
|
136
|
+
]);
|
|
137
|
+
if (updateVaults) await updateVaults();
|
|
138
|
+
context.senderState = newSender;
|
|
139
|
+
context.rollupState = newRollup;
|
|
140
|
+
return next();
|
|
141
|
+
}
|
|
89
142
|
const [newSenderState, newRollupState, newStakeStates] = await Promise.all([
|
|
90
143
|
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
91
144
|
nonce: tx.nonce,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BridgeCtxMixin } from "./bridge-v2.cjs";
|
|
1
2
|
import { IReadyContext, IStakeState, IWithRollup, IWithSender, IWithSigners, TCloseRollupTx } from "@ocap/types";
|
|
2
3
|
import { Runner } from "@ocap/tx-pipeline";
|
|
3
4
|
|
|
@@ -8,7 +9,7 @@ import { Runner } from "@ocap/tx-pipeline";
|
|
|
8
9
|
* Phase: IReadyContext (after TakeStateSnapshot)
|
|
9
10
|
* Mixins: IWithRollup (required), IWithSender (required), IWithSigners (required)
|
|
10
11
|
*/
|
|
11
|
-
type Context = IReadyContext<TCloseRollupTx> & IWithRollup & IWithSender & IWithSigners & {
|
|
12
|
+
type Context = IReadyContext<TCloseRollupTx> & IWithRollup & IWithSender & IWithSigners & BridgeCtxMixin & {
|
|
12
13
|
/** Stake addresses for all validators */
|
|
13
14
|
stakeAddresses?: string[];
|
|
14
15
|
/** Stake states for all validators */
|
|
@@ -3,15 +3,16 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
6
7
|
const require_protocols_rollup_pipes_ensure_validator = require('./pipes/ensure-validator.cjs');
|
|
7
8
|
const require_protocols_rollup_pipes_verify_signers = require('./pipes/verify-signers.cjs');
|
|
8
9
|
const require_protocols_rollup_pipes_verify_status = require('./pipes/verify-status.cjs');
|
|
9
10
|
const require_protocols_rollup_pipes_verify_evidence = require('./pipes/verify-evidence.cjs');
|
|
10
|
-
let _arcblock_did_util = require("@arcblock/did-util");
|
|
11
|
-
let _ocap_state = require("@ocap/state");
|
|
12
11
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
13
12
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
14
13
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
14
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
15
|
+
let _ocap_state = require("@ocap/state");
|
|
15
16
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
16
17
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
17
18
|
let lodash_pick = require("lodash/pick");
|
|
@@ -72,6 +73,7 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
72
73
|
status: "INVALID_ROLLUP",
|
|
73
74
|
table: "rollup"
|
|
74
75
|
}));
|
|
76
|
+
runner.use(require_protocols_rollup_bridge_v2.rejectBridgeV2("create_rollup_block"));
|
|
75
77
|
runner.use(require_protocols_rollup_pipes_ensure_validator.default(false));
|
|
76
78
|
runner.use(require_protocols_rollup_pipes_verify_status.default({ closed: false }));
|
|
77
79
|
runner.use((context, next) => {
|
|
@@ -3,12 +3,15 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
let _ocap_state = require("@ocap/state");
|
|
6
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
7
|
+
const require_protocols_rollup_bridge_create = require('./bridge-create.cjs');
|
|
9
8
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
10
9
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
11
10
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
11
|
+
let _arcblock_did = require("@arcblock/did");
|
|
12
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
13
|
+
let _ocap_state = require("@ocap/state");
|
|
14
|
+
let _ocap_util = require("@ocap/util");
|
|
12
15
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
13
16
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
14
17
|
let debug = require("debug");
|
|
@@ -26,6 +29,17 @@ runner.use((context, next) => {
|
|
|
26
29
|
context.formattedItx = (0, _ocap_message.formatMessage)("CreateRollupTx", itx);
|
|
27
30
|
const decodedData = require_util.decodeAnySafe(itx.data ?? null);
|
|
28
31
|
context.rollupData = decodedData;
|
|
32
|
+
context.isBridgeV2 = _ocap_state.rollup.isBridgeV2Data(decodedData);
|
|
33
|
+
if (context.isBridgeV2) {
|
|
34
|
+
try {
|
|
35
|
+
context.bridgeInput = _ocap_state.rollup.parseBridgeCreateData(decodedData.value);
|
|
36
|
+
_ocap_state.rollup.assertV1FieldsUnused(context.formattedItx, "INVALID_ROLLUP_PROPS");
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return next(new _ocap_util_lib_error.CustomError("INVALID_TX", err.message));
|
|
39
|
+
}
|
|
40
|
+
return next();
|
|
41
|
+
}
|
|
42
|
+
if (!require_protocols_rollup_bridge_v2.isRollupV1Enabled(context.config)) return next(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", "create_rollup (v1) is disabled because Rollup v1 is sunset"));
|
|
29
43
|
try {
|
|
30
44
|
_ocap_state.rollup.create({
|
|
31
45
|
...context.formattedItx,
|
|
@@ -59,7 +73,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([
|
|
|
59
73
|
const context = ctx;
|
|
60
74
|
const targetType = _ocap_state.rollup.getRollupTargetType({
|
|
61
75
|
...context.formattedItx,
|
|
62
|
-
data: context.rollupData
|
|
76
|
+
data: context.isBridgeV2 ? null : context.rollupData
|
|
63
77
|
});
|
|
64
78
|
const seedValidators = context.formattedItx?.seedValidators || [];
|
|
65
79
|
return targetType === "EVM" ? seedValidators.every(({ address }) => (0, _arcblock_did.isEthereumDid)(address)) : true;
|
|
@@ -75,6 +89,15 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([
|
|
|
75
89
|
itx.address = "";
|
|
76
90
|
context.rollupData = itx.data;
|
|
77
91
|
context.seedValidators = (0, _ocap_util_lib_get_list_field.getListField)(itx, "seedValidators").map((x) => x.address);
|
|
92
|
+
if (context.isBridgeV2) {
|
|
93
|
+
const rest = { ...itx.data.value || {} };
|
|
94
|
+
delete rest.ethOwnerAttestation;
|
|
95
|
+
delete rest.consents;
|
|
96
|
+
itx.data = {
|
|
97
|
+
...itx.data,
|
|
98
|
+
value: rest
|
|
99
|
+
};
|
|
100
|
+
}
|
|
78
101
|
return (0, _arcblock_did_util.toRollupAddress)(itx, context.txEncoding) === context.itx.address;
|
|
79
102
|
}
|
|
80
103
|
}
|
|
@@ -125,9 +148,19 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
|
|
|
125
148
|
return (0, lodash_isEmpty.default)(ctx.tokenState?.foreignToken) === false;
|
|
126
149
|
}
|
|
127
150
|
}]));
|
|
151
|
+
runner.use((context, next) => {
|
|
152
|
+
const { tx, itx, config, tokenState } = context;
|
|
153
|
+
const isOfficialToken = !!config?.token?.address && (0, _ocap_util.isSameDid)(itx.tokenAddress, config.token.address);
|
|
154
|
+
const authority = isOfficialToken ? config?.moderator?.address : tokenState?.issuer;
|
|
155
|
+
const creators = [tx.from, tx.delegator].filter(Boolean);
|
|
156
|
+
if (!authority || !creators.some((x) => (0, _ocap_util.isSameDid)(x, authority))) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", isOfficialToken ? "Only the chain moderator can create a rollup for the official token" : "Only the token issuer can create a rollup for the token"));
|
|
157
|
+
return next();
|
|
158
|
+
});
|
|
128
159
|
runner.use(async (context, next) => {
|
|
129
160
|
const { statedb, itx } = context;
|
|
130
161
|
if (await statedb.rollup.existByToken(itx.tokenAddress, context)) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "Only 1 active rollup can be created for the token"));
|
|
162
|
+
const entry = await statedb.evidence.get(require_protocols_rollup_bridge_v2.tokenIndexKey(itx.tokenAddress), context);
|
|
163
|
+
if (entry && require_protocols_rollup_bridge_v2.indexBlocks(entry, context.txTime ? _ocap_state.rollup.toSeconds(context.txTime) : Number.MAX_SAFE_INTEGER, "bridge-token")) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "Only 1 bridge that is not final can exist for the token"));
|
|
131
164
|
return next();
|
|
132
165
|
});
|
|
133
166
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
@@ -143,14 +176,20 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
|
|
|
143
176
|
return (0, lodash_isEmpty.default)(ctx.rollupState);
|
|
144
177
|
}
|
|
145
178
|
}]));
|
|
146
|
-
runner.use(
|
|
179
|
+
runner.use(require_protocols_rollup_bridge_create.verifyBridgeCreate);
|
|
180
|
+
runner.use(require_pipes_ensure_gas.default((context) => context.isBridgeV2 ? {
|
|
181
|
+
create: 4,
|
|
182
|
+
update: 2,
|
|
183
|
+
payment: 0
|
|
184
|
+
} : {
|
|
147
185
|
create: 1,
|
|
148
186
|
update: 2,
|
|
149
187
|
payment: 0
|
|
150
|
-
}))
|
|
188
|
+
}));
|
|
151
189
|
runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
|
|
152
190
|
runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
153
191
|
runner.use(async (context, next) => {
|
|
192
|
+
if (context.isBridgeV2) return require_protocols_rollup_bridge_create.createBridgeStep(context, next);
|
|
154
193
|
const { tx, formattedItx, rollupData, statedb, senderState, delegatorState, delegationState, senderUpdates, updateVaults } = context;
|
|
155
194
|
const issuer = delegatorState ? delegatorState.address : senderState.address;
|
|
156
195
|
const [newSenderState, newRollupState, newDelegationState] = await Promise.all([
|
|
@@ -25,6 +25,9 @@ type Context = IReadyContext<TCreateRollupTx> & IWithSender & Partial<IWithToken
|
|
|
25
25
|
rollupState?: IRollupState | null;
|
|
26
26
|
/** Whether delegation state changed */
|
|
27
27
|
isDelegationChanged?: boolean;
|
|
28
|
+
/** v2 (on-chain bridge) create */
|
|
29
|
+
isBridgeV2?: boolean;
|
|
30
|
+
bridgeInput?: unknown;
|
|
28
31
|
};
|
|
29
32
|
declare const runner: Runner<Context>;
|
|
30
33
|
//#endregion
|