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