@ocap/tx-pipeline 1.31.5 → 1.31.6
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-keys.d.mts +17 -1
- package/esm/bridge-release-keys.mjs +35 -20
- package/esm/bridge-reserves.d.mts +48 -9
- package/esm/bridge-reserves.mjs +66 -16
- package/esm/helpers/bridge-withdraw.d.mts +22 -38
- package/esm/helpers/bridge-withdraw.mjs +57 -77
- package/esm/helpers/replay-guard.d.mts +29 -12
- package/esm/helpers/replay-guard.mjs +68 -22
- package/esm/index.d.mts +3 -3
- package/esm/index.mjs +3 -3
- package/esm/legacy-protocol-sunset.d.mts +35 -21
- package/esm/legacy-protocol-sunset.mjs +124 -70
- package/esm/legacy-rollup-archive.d.mts +44 -7
- package/esm/legacy-rollup-archive.mjs +78 -13
- package/esm/library.d.mts +3 -3
- package/esm/library.mjs +3 -3
- package/esm/pipes/bridge-release-gate.d.mts +3 -2
- package/esm/pipes/bridge-release-gate.mjs +90 -35
- package/esm/pipes/bridge-reserve-v2-gate.d.mts +2 -0
- package/esm/pipes/bridge-reserve-v2-gate.mjs +31 -13
- package/esm/pipes/decode-tx.mjs +1 -1
- package/esm/pipes/post-execution-invariant-gate.mjs +5 -4
- package/esm/pipes/pre-flight-waf.mjs +6 -3
- package/esm/pipes/verify-multisig-v2.mjs +1 -1
- package/esm/pipes/verify-multisig.mjs +1 -1
- package/esm/pipes/verify-tx-size.mjs +8 -2
- package/esm/pipes/verify-tx.mjs +1 -1
- package/esm/transaction-block.mjs +11 -0
- package/lib/bridge-release-keys.cjs +35 -18
- package/lib/bridge-release-keys.d.cts +17 -1
- package/lib/bridge-reserves.cjs +73 -18
- package/lib/bridge-reserves.d.cts +48 -9
- package/lib/helpers/bridge-withdraw.cjs +60 -80
- package/lib/helpers/bridge-withdraw.d.cts +22 -38
- package/lib/helpers/replay-guard.cjs +64 -17
- package/lib/helpers/replay-guard.d.cts +29 -12
- package/lib/index.cjs +2 -2
- package/lib/index.d.cts +3 -3
- package/lib/legacy-protocol-sunset.cjs +122 -69
- package/lib/legacy-protocol-sunset.d.cts +35 -21
- package/lib/legacy-rollup-archive.cjs +82 -14
- package/lib/legacy-rollup-archive.d.cts +44 -7
- package/lib/library.cjs +1 -2
- package/lib/library.d.cts +3 -3
- package/lib/pipes/bridge-release-gate.cjs +89 -33
- package/lib/pipes/bridge-release-gate.d.cts +3 -2
- package/lib/pipes/bridge-reserve-v2-gate.cjs +30 -12
- package/lib/pipes/bridge-reserve-v2-gate.d.cts +2 -0
- package/lib/pipes/decode-tx.cjs +1 -1
- package/lib/pipes/post-execution-invariant-gate.cjs +3 -2
- package/lib/pipes/pre-flight-waf.cjs +6 -3
- package/lib/pipes/verify-multisig-v2.cjs +1 -1
- package/lib/pipes/verify-multisig.cjs +1 -1
- package/lib/pipes/verify-tx-size.cjs +8 -2
- package/lib/pipes/verify-tx.cjs +1 -1
- package/lib/transaction-block.cjs +11 -0
- package/package.json +10 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BRIDGE_RECONCILE_TX_TYPES,
|
|
1
|
+
import { BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_V2_TX_TYPE, bareAddress, isNamedBridgeRelease } from "../bridge-reserves.mjs";
|
|
2
2
|
import { BN } from "@ocap/util";
|
|
3
3
|
import { toBridgeReserveAddress } from "@arcblock/did-util";
|
|
4
4
|
import isEqual from "lodash/isEqual.js";
|
|
@@ -11,11 +11,13 @@ import isEqual from "lodash/isEqual.js";
|
|
|
11
11
|
* bridge a reserve belongs to comes from the `reserve -> bridge` index, resolved once per transaction by
|
|
12
12
|
* `prepareBridgeReserveGate` before this synchronous check runs (`bridge-reserves.ts`); the address is never trusted.
|
|
13
13
|
*
|
|
14
|
-
* - R1 debits:
|
|
15
|
-
*
|
|
16
|
-
* `transaction.bridge.enabled` must be on for a
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* - R1 debits: a release of THAT bridge (the reserve of `itx.rollup`, or of its `migratedTo` chain for a REFUND): a
|
|
15
|
+
* `deposit_token_v2` with `kind ∈ {1,2,3}` (`isBridgeRelease`, #1692); a v1 deposit (`kind == 0`) never debits a reserve. Also `migrate_rollup`,
|
|
16
|
+
* and the reconcile hook (a named, empty list today). The master switch `transaction.bridge.enabled` must be on for a
|
|
17
|
+
* release.
|
|
18
|
+
* - R2 credits: the bridge branch of `withdraw_token_v2` (the withdraw, #1691) into the reserve of the live, unpaused,
|
|
19
|
+
* not-closing v2 bridge `itx.rollup` names and no other, with the master switch on, in the bridge token only;
|
|
20
|
+
* `create_rollup` funding the reserve of the bridge it creates;
|
|
19
21
|
* `migrate_rollup` filling the successor's reserve. A role-25 address that maps to no bridge accepts nothing.
|
|
20
22
|
* - R3: a reserve account is only materialised by `create_rollup`, only token balances ever change, nothing writes the
|
|
21
23
|
* `balance` table directly, and no record of another table gains a reference to a reserve.
|
|
@@ -38,13 +40,25 @@ const migratedToOf = (row) => {
|
|
|
38
40
|
* `assertBridgeReservesV2Protected`.
|
|
39
41
|
*/
|
|
40
42
|
const LEGACY_BALANCE_FIELDS = new Set(["balance", "balanceNum"]);
|
|
41
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Account columns whose stored default is zero (statedb/sqlite 001-genesis). Since #1640 the sqlite statedb returns
|
|
45
|
+
* the whole stored row from create / update, so every new account carries them; at zero they are "not set", the same
|
|
46
|
+
* as an account row that does not have them at all (memory statedb). Any other value is state and still counts (#1647).
|
|
47
|
+
*/
|
|
48
|
+
const ZERO_DEFAULT_FIELDS = new Set([
|
|
49
|
+
"gasBalance",
|
|
50
|
+
"numTxs",
|
|
51
|
+
"numAssets"
|
|
52
|
+
]);
|
|
53
|
+
const isZeroDefault = (k, v) => ZERO_DEFAULT_FIELDS.has(k) && (v === 0 || v === "0");
|
|
54
|
+
/** Account fields other than balances; empty values, zero column defaults and the context are ignored. */
|
|
42
55
|
const stableFields = (account) => {
|
|
43
56
|
const out = {};
|
|
44
57
|
for (const [k, v] of Object.entries(account || {})) {
|
|
45
58
|
if (LEGACY_BALANCE_FIELDS.has(k)) continue;
|
|
46
59
|
if (k === "tokens" || k === "context" || k === "meta" || k === "$loki" || k.startsWith("_")) continue;
|
|
47
60
|
if (v === void 0 || v === null || v === "" || Array.isArray(v) && v.length === 0) continue;
|
|
61
|
+
if (isZeroDefault(k, v)) continue;
|
|
48
62
|
out[k] = v;
|
|
49
63
|
}
|
|
50
64
|
return out;
|
|
@@ -162,12 +176,15 @@ function assertBridgeReservesV2Protected(input) {
|
|
|
162
176
|
const afterRollups = after.get("rollup") ?? /* @__PURE__ */ new Map();
|
|
163
177
|
const beforeRollups = before.get("rollup") ?? /* @__PURE__ */ new Map();
|
|
164
178
|
const reserveOfBridge = (bridge) => norm(toBridgeReserveAddress(bridge));
|
|
179
|
+
const isRelease = isNamedBridgeRelease(txType, input.itx);
|
|
180
|
+
const mayDebit = BRIDGE_RESERVE_DEBIT_TX_TYPES.includes(txType) || isRelease;
|
|
181
|
+
const debitTypes = `${BRIDGE_RESERVE_DEBIT_TX_TYPES.join(", ")}, deposit_token_v2 (bridge kinds)`;
|
|
165
182
|
for (const c of changes) {
|
|
166
|
-
if (c.debits.length > 0 && !
|
|
183
|
+
if (c.debits.length > 0 && !mayDebit) throw createGateError("BRIDGE_RESERVE_DEBIT_FORBIDDEN", `Post-Execution Invariant Gate: bridge reserve ${c.key} was debited in a ${txType || "unknown"} tx (allowed: ${debitTypes})`);
|
|
167
184
|
if (c.credits.length > 0 && !BRIDGE_RESERVE_CREDIT_TX_TYPES.includes(txType)) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: bridge reserve ${c.key} was credited in a ${txType || "unknown"} tx (allowed: ${BRIDGE_RESERVE_CREDIT_TX_TYPES.join(", ")})`);
|
|
168
185
|
if (!c.before && txType !== "create_rollup") throw createGateError("BRIDGE_RESERVE_STATE_VIOLATION", `Post-Execution Invariant Gate: bridge reserve ${c.key} can only be materialised by create_rollup, not by a ${txType || "unknown"} tx`);
|
|
169
186
|
if (c.before && !isEqual(stableFields(c.before), stableFields(c.after))) throw createGateError("BRIDGE_RESERVE_STATE_VIOLATION", `Post-Execution Invariant Gate: bridge reserve ${c.key} may only change token balances`);
|
|
170
|
-
if (!
|
|
187
|
+
if (!mayDebit && !BRIDGE_RESERVE_CREDIT_TX_TYPES.includes(txType)) throw createGateError("BRIDGE_RESERVE_STATE_VIOLATION", `Post-Execution Invariant Gate: bridge reserve ${c.key} can not be written by a ${txType || "unknown"} tx`);
|
|
171
188
|
}
|
|
172
189
|
const legacy = (rec, field) => {
|
|
173
190
|
const v = rec?.[field];
|
|
@@ -177,19 +194,21 @@ function assertBridgeReservesV2Protected(input) {
|
|
|
177
194
|
const resolution = input.resolution ?? null;
|
|
178
195
|
const bridgeOf = (key) => resolution?.reserves.get(bareAddress(key)) ?? null;
|
|
179
196
|
if (txType !== "create_rollup" && txType !== "migrate_rollup") {
|
|
180
|
-
const ledgerRows =
|
|
197
|
+
const ledgerRows = isRelease && Number(input.releaseKind) === 2 ? new Set([...input.resolution?.release?.debitable.values() ?? []].map((b) => bareAddress(b.address))) : /* @__PURE__ */ new Set();
|
|
181
198
|
for (const [key, row] of afterRollups.entries()) {
|
|
182
199
|
const prior = beforeRollups.get(key) ?? null;
|
|
183
200
|
if (ledgerRows.has(bareAddress(key))) continue;
|
|
184
201
|
if (isBridgeV2Row(row) && !isEqual(prior, row)) throw createGateError("BRIDGE_RESERVE_STATE_VIOLATION", `Post-Execution Invariant Gate: a ${txType || "unknown"} tx can not move a bridge reserve and write the bridge ${key} in the same tx`);
|
|
185
202
|
}
|
|
186
203
|
}
|
|
187
|
-
if (txType ===
|
|
204
|
+
if (txType === BRIDGE_WITHDRAW_V2_TX_TYPE) {
|
|
188
205
|
for (const c of changes) {
|
|
189
206
|
if (c.debits.length > 0 || c.credits.length === 0) throw createGateError("BRIDGE_RESERVE_DEBIT_FORBIDDEN", `Post-Execution Invariant Gate: a withdraw only credits a bridge reserve (${c.key})`);
|
|
190
207
|
if (!input.enabled) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: bridge reserve ${c.key} can not be credited while transaction.bridge.enabled is off`);
|
|
191
208
|
const bridge = bridgeOf(c.key);
|
|
192
209
|
if (!bridge) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: ${c.key} has the bridge reserve role but is not the reserve of any v2 bridge`);
|
|
210
|
+
const named = resolution?.withdraw ?? null;
|
|
211
|
+
if (!named || bareAddress(named.address) !== bareAddress(bridge.address)) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: a withdraw_token_v2 of ${named?.address ?? "(no v2 bridge)"} can not credit reserve ${c.key}, which belongs to bridge ${bridge.address}`);
|
|
193
212
|
if (!bridge.acceptingWithdraws) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: bridge ${bridge.address} does not accept withdraws (paused: ${bridge.paused}, closing: ${bridge.closing}, final: ${bridge.final}, migrated: ${bridge.migrated})`);
|
|
194
213
|
const token = norm(bridge.token);
|
|
195
214
|
for (const tok of c.credits) if (!token || tok !== token) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: bridge reserve ${c.key} can only be credited with the bridge token ${token}, not ${tok}`);
|
|
@@ -201,8 +220,7 @@ function assertBridgeReservesV2Protected(input) {
|
|
|
201
220
|
}
|
|
202
221
|
return;
|
|
203
222
|
}
|
|
204
|
-
if (
|
|
205
|
-
const isRelease = txType === BRIDGE_RELEASE_TX_TYPE;
|
|
223
|
+
if (isRelease || BRIDGE_RECONCILE_TX_TYPES.includes(txType)) {
|
|
206
224
|
if (changes.some((c) => c.credits.length > 0 || c.debits.length === 0)) throw createGateError("BRIDGE_RESERVE_CREDIT_FORBIDDEN", `Post-Execution Invariant Gate: a ${txType} tx only debits a bridge reserve`);
|
|
207
225
|
if (isRelease && !input.enabled) throw createGateError("BRIDGE_RESERVE_DEBIT_FORBIDDEN", "Post-Execution Invariant Gate: a bridge reserve can not be debited while transaction.bridge.enabled is off");
|
|
208
226
|
if (isRelease && changes.length !== 1) throw createGateError("BRIDGE_RESERVE_DEBIT_FORBIDDEN", `Post-Execution Invariant Gate: a release debits exactly one bridge reserve, got ${changes.length}`);
|
package/esm/pipes/decode-tx.mjs
CHANGED
|
@@ -4,8 +4,8 @@ import { assertWireByteCeiling } from "./pre-flight-waf.mjs";
|
|
|
4
4
|
import { toTxHash } from "@ocap/mcrypto";
|
|
5
5
|
import { fromBase64, toUint8Array } from "@ocap/util";
|
|
6
6
|
import { CustomError } from "@ocap/util/lib/error";
|
|
7
|
-
import { decodeAny, encodeAny, findFormatProblem } from "@ocap/message";
|
|
8
7
|
import { getListField, setListField } from "@ocap/util/lib/get-list-field";
|
|
8
|
+
import { decodeAny, encodeAny, findFormatProblem } from "@ocap/message";
|
|
9
9
|
import { CANONICAL_SELF_DESCRIBE_PREFIX, decodeTx, encodeTx } from "@ocap/message/cbor";
|
|
10
10
|
import { decodeTx as decodeTx$1, encodeTx as encodeTx$1 } from "@ocap/message/protobuf";
|
|
11
11
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { stripTxTypePrefix } from "../bridge-reserves.mjs";
|
|
2
|
-
import {
|
|
1
|
+
import { isBridgeRelease, stripTxTypePrefix } from "../bridge-reserves.mjs";
|
|
2
|
+
import { checkBridgeWithdrawV2, isBridgeWithdrawFailure } from "../helpers/bridge-withdraw.mjs";
|
|
3
3
|
import { assertBridgeReleaseProtected } from "./bridge-release-gate.mjs";
|
|
4
4
|
import { assertBridgeReservesV2Protected } from "./bridge-reserve-v2-gate.mjs";
|
|
5
5
|
import { types } from "@ocap/mcrypto";
|
|
@@ -530,8 +530,9 @@ function inspectPostExecutionInvariants(context) {
|
|
|
530
530
|
enabled: !!(context.config?.transaction)?.bridge?.enabled,
|
|
531
531
|
resolution: context._bridgeReserveGate ?? null,
|
|
532
532
|
releaseKind: context.itx?.kind,
|
|
533
|
+
itx: context.itx ?? null,
|
|
533
534
|
checkWithdraw: (bridge, credited) => {
|
|
534
|
-
const withdraw =
|
|
535
|
+
const withdraw = checkBridgeWithdrawV2(context, bridge);
|
|
535
536
|
if (isBridgeWithdrawFailure(withdraw)) return `${withdraw.code}: ${withdraw.message}`;
|
|
536
537
|
if (!credited.eq(new BN(withdraw.amount))) return `the reserve grew by ${credited.toString(10)}, not by the withdrawn amount ${withdraw.amount}`;
|
|
537
538
|
return null;
|
|
@@ -809,7 +810,7 @@ function inspectPostExecutionInvariants(context) {
|
|
|
809
810
|
const isCloseRollupTx = txType === "fg:t:close_rollup" || txType === "close_rollup";
|
|
810
811
|
const isLeaveRollupTx = txType === "fg:t:leave_rollup" || txType === "leave_rollup";
|
|
811
812
|
const isSlashStakeTx = txType === "fg:t:slash_stake" || txType === "slash_stake";
|
|
812
|
-
const isRollupDepositTx = txType === "fg:t:deposit_token_v2" || txType === "deposit_token_v2";
|
|
813
|
+
const isRollupDepositTx = (txType === "fg:t:deposit_token_v2" || txType === "deposit_token_v2") && !isBridgeRelease(txType, context.itx) && context.isBridgeV2 !== true;
|
|
813
814
|
const isClaimBlockRewardTx = txType === "fg:t:claim_block_reward" || txType === "claim_block_reward";
|
|
814
815
|
const isStakeOfRetiredToken = (stakeRec) => {
|
|
815
816
|
for (const afterFac of afterFactories.values()) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { txResourceKey } from "../bridge-reserves.mjs";
|
|
1
2
|
import { ENCRYPTED_NFT_SCHEMA_VERSION, validateEncryptedAssetEnvelope } from "../encrypted-nft-envelope.mjs";
|
|
2
3
|
import DecodeItx from "./decode-itx.mjs";
|
|
3
4
|
import runner_default from "../runner.mjs";
|
|
@@ -8,9 +9,9 @@ import VerifyTx from "./verify-tx.mjs";
|
|
|
8
9
|
import VerifyTxSize from "./verify-tx-size.mjs";
|
|
9
10
|
import DecodeTx from "./decode-tx.mjs";
|
|
10
11
|
import { CustomError } from "@ocap/util/lib/error";
|
|
11
|
-
import { createMessage, decodeAny, encodeAny, fromTypeUrl, toTypeUrl } from "@ocap/message";
|
|
12
12
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
13
13
|
import get from "lodash/get.js";
|
|
14
|
+
import { createMessage, decodeAny, encodeAny, fromTypeUrl, toTypeUrl } from "@ocap/message";
|
|
14
15
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
15
16
|
|
|
16
17
|
//#region src/pipes/pre-flight-waf.ts
|
|
@@ -518,6 +519,8 @@ function scanItxPayloadAnomalies(itx, txType, config, rawTxItx, txSignatures) {
|
|
|
518
519
|
if (totalNestedAssets > maxListSize * 2) throw new CustomError("INVALID_TX_SIZE", `Total nested asset entries (${totalNestedAssets}) exceeds safe envelope`);
|
|
519
520
|
const itxSignatures = getListField(itxObj, "signatures");
|
|
520
521
|
if (Array.isArray(itxSignatures) && itxSignatures.length > maxMultisig) throw new CustomError("INVALID_TX", `Too many signatures in itx: maximum allowed is ${maxMultisig}`);
|
|
522
|
+
const itxValidatorSignatures = getListField(itxObj, "validatorSignatures");
|
|
523
|
+
if (Array.isArray(itxValidatorSignatures) && itxValidatorSignatures.length > maxMultisig) throw new CustomError("INVALID_TX", `Too many signatures in itx: maximum allowed is ${maxMultisig}`);
|
|
521
524
|
const txsList = getListField(itxObj, "txs");
|
|
522
525
|
const maxTxsLimit = Math.max(maxListSize, MAX_ROLLUP_TXS_LIST_SIZE);
|
|
523
526
|
if (Array.isArray(txsList) && txsList.length > maxTxsLimit) throw new CustomError("INVALID_TX_SIZE", `Size of txs can not exceeded ${maxTxsLimit} items`);
|
|
@@ -602,7 +605,7 @@ function tryApplyPreFlightVerifiedSnapshot(context) {
|
|
|
602
605
|
]) : void 0) || snap.maxTxSizeForType !== (snap.txType ? get(config, [
|
|
603
606
|
"transaction",
|
|
604
607
|
"maxTxSize",
|
|
605
|
-
snap.txType
|
|
608
|
+
txResourceKey(snap.txType, snap.itx)
|
|
606
609
|
], defaultSizeLimit) : defaultSizeLimit) || snap.maxExtraSize !== get(config, "transaction.maxExtraSize") || snap.maxMultisig !== get(config, "transaction.maxMultisig") || snap.maxListSize !== get(config, "transaction.maxListSize") || snap.maxAssetSize !== get(config, "transaction.maxAssetSize") || snap.isSupportedTx !== Boolean(Array.isArray(supportedTxs) && supportedTxs.includes(snap.txType)) || snap.isMultiSignV2Tx !== Boolean(Array.isArray(multiSignV2Txs) && multiSignV2Txs.includes(snap.txType))) return false;
|
|
607
610
|
const extra = context.extra;
|
|
608
611
|
if (snap.txExtraSerialized !== serializeExtraField(extra?.txExtra) || snap.gasStakeHeadersSerialized !== serializeExtraField(extra?.gasStakeHeaders) || snap.coSignatureSerialized !== serializeExtraField(extra?.coSignature)) return false;
|
|
@@ -758,7 +761,7 @@ async function inspectTransactionPreFlight(context) {
|
|
|
758
761
|
maxTxSizeForType: get(config, [
|
|
759
762
|
"transaction",
|
|
760
763
|
"maxTxSize",
|
|
761
|
-
preFlightCtx.txType
|
|
764
|
+
txResourceKey(preFlightCtx.txType, preFlightCtx.itx)
|
|
762
765
|
], defaultSizeLimit),
|
|
763
766
|
maxExtraSize: get(config, "transaction.maxExtraSize"),
|
|
764
767
|
maxMultisig: get(config, "transaction.maxMultisig"),
|
|
@@ -5,9 +5,9 @@ import { encodeTxByEncoding } from "../helpers/encode-tx-by-context.mjs";
|
|
|
5
5
|
import { types } from "@ocap/mcrypto";
|
|
6
6
|
import { CustomError } from "@ocap/util/lib/error";
|
|
7
7
|
import { isFromPublicKey, toTypeInfo } from "@arcblock/did";
|
|
8
|
-
import { decodeAny } from "@ocap/message";
|
|
9
8
|
import { getListField, setListField } from "@ocap/util/lib/get-list-field";
|
|
10
9
|
import get from "lodash/get.js";
|
|
10
|
+
import { decodeAny } from "@ocap/message";
|
|
11
11
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
12
12
|
import omit from "lodash/omit.js";
|
|
13
13
|
import Debug from "debug";
|
|
@@ -4,9 +4,9 @@ import { encodeTxByEncoding } from "../helpers/encode-tx-by-context.mjs";
|
|
|
4
4
|
import { types } from "@ocap/mcrypto";
|
|
5
5
|
import { CustomError } from "@ocap/util/lib/error";
|
|
6
6
|
import { isFromPublicKey, toTypeInfo } from "@arcblock/did";
|
|
7
|
-
import { decodeAny } from "@ocap/message";
|
|
8
7
|
import { deleteListField, getListField, setListField } from "@ocap/util/lib/get-list-field";
|
|
9
8
|
import get from "lodash/get.js";
|
|
9
|
+
import { decodeAny } from "@ocap/message";
|
|
10
10
|
import cloneDeep from "lodash/cloneDeep.js";
|
|
11
11
|
import { fromPublicKey } from "@ocap/wallet";
|
|
12
12
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { txResourceKey } from "../bridge-reserves.mjs";
|
|
1
2
|
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
-
import { toTypeUrl } from "@ocap/message";
|
|
3
3
|
import get from "lodash/get.js";
|
|
4
|
+
import { toTypeUrl } from "@ocap/message";
|
|
4
5
|
|
|
5
6
|
//#region src/pipes/verify-tx-size.ts
|
|
6
7
|
const DEFAULT_MAX_EXTRA_SIZE = 8 * 1024;
|
|
@@ -36,7 +37,12 @@ function VerifyTxSize(context, next) {
|
|
|
36
37
|
context._baseTxSize = baseTxSize;
|
|
37
38
|
const defaultSizeLimit = get(config, "transaction.maxTxSize.default");
|
|
38
39
|
const rawItx = tx.itx;
|
|
39
|
-
const
|
|
40
|
+
const typeKey = txType || rawItx?.typeUrl || (rawItx?.type ? toTypeUrl(rawItx.type) : void 0);
|
|
41
|
+
const sizeLimit = get(config, [
|
|
42
|
+
"transaction",
|
|
43
|
+
"maxTxSize",
|
|
44
|
+
typeKey ? txResourceKey(typeKey, context.itx) : typeKey
|
|
45
|
+
], defaultSizeLimit);
|
|
40
46
|
if (baseTxSize > sizeLimit) return next(new CustomError("INVALID_TX_SIZE", `Transaction size too large for ${txType}: expect ${sizeLimit}, got ${baseTxSize}`));
|
|
41
47
|
const maxExtraSize = get(config, "transaction.maxExtraSize", DEFAULT_MAX_EXTRA_SIZE);
|
|
42
48
|
if (totalExtraBytes > maxExtraSize || gasStakeHeadersBytes > MAX_GAS_STAKE_HEADERS_SIZE || coSignatureBytes > MAX_CO_SIGNATURE_SIZE) {
|
package/esm/pipes/verify-tx.mjs
CHANGED
|
@@ -4,9 +4,9 @@ import { BN } from "@ocap/util";
|
|
|
4
4
|
import { CustomError } from "@ocap/util/lib/error";
|
|
5
5
|
import { isFromPublicKey, toTypeInfo } from "@arcblock/did";
|
|
6
6
|
import { account } from "@ocap/state";
|
|
7
|
-
import { toTypeUrl } from "@ocap/message";
|
|
8
7
|
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
9
8
|
import get from "lodash/get.js";
|
|
9
|
+
import { toTypeUrl } from "@ocap/message";
|
|
10
10
|
import isEmpty from "lodash/isEmpty.js";
|
|
11
11
|
|
|
12
12
|
//#region src/pipes/verify-tx.ts
|
|
@@ -2022,6 +2022,17 @@ async function executeCrossChainPurchaseBlock(input, options) {
|
|
|
2022
2022
|
if (input.enforceZeroSum === false) throw new CustomError("INVALID_CROSS_CHAIN_PURCHASE", "Zero-sum conservation cannot be disabled in cross-chain purchase blocks");
|
|
2023
2023
|
const proposer = deposit.proposer || input.relayer;
|
|
2024
2024
|
assertSafeKey(proposer, "deposit.proposer");
|
|
2025
|
+
let depositRollupRow;
|
|
2026
|
+
try {
|
|
2027
|
+
const rollupTable = options.statedb?.rollup;
|
|
2028
|
+
if (typeof rollupTable?.get !== "function") throw new Error("the statedb has no rollup table");
|
|
2029
|
+
const canonical = String(deposit.rollup).trim().replace(/^did:abt:/, "");
|
|
2030
|
+
const rows = await Promise.all([...new Set([deposit.rollup, canonical])].map((key) => rollupTable.get(key)));
|
|
2031
|
+
depositRollupRow = rows.find((row) => row?.data?.value?.version === 2) ?? rows[0];
|
|
2032
|
+
} catch (err) {
|
|
2033
|
+
throw new CustomError("INVALID_CROSS_CHAIN_PURCHASE", `Can not read deposit.rollup ${deposit.rollup}: ${String(err?.message || err)}`);
|
|
2034
|
+
}
|
|
2035
|
+
if (depositRollupRow?.data?.value?.version === 2) throw new CustomError("FORBIDDEN", `deposit.rollup ${deposit.rollup} is a v2 bridge: a bridge deposit can not be a step of a cross-chain purchase block`);
|
|
2025
2036
|
const combinedOfacList = /* @__PURE__ */ new Set();
|
|
2026
2037
|
if (Array.isArray(input.ofacBlocklist)) for (const addr of input.ofacBlocklist) combinedOfacList.add(addr);
|
|
2027
2038
|
if (options.ofacBlocklist) for (const addr of options.ofacBlocklist) combinedOfacList.add(addr);
|
|
@@ -3,43 +3,58 @@ let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
|
3
3
|
|
|
4
4
|
//#region src/bridge-release-keys.ts
|
|
5
5
|
/**
|
|
6
|
-
* Evidence-table keys owned by `
|
|
6
|
+
* Evidence-table keys owned by the bridge release, `deposit_token_v2` kind 1/2/3 (#1466,
|
|
7
|
+
* bridge/docs/ocap-protocols.md §3 checks 3 and 7, §v2.4).
|
|
7
8
|
*
|
|
8
9
|
* Both keys carry the bridge address, so several bridges on one chain never share a tombstone or a bucket. Every
|
|
9
10
|
* preimage is fixed-width words followed by the bridge DID as the trailing text (`bridgeEvidenceKey`), so two
|
|
10
11
|
* different tuples can never produce the same bytes:
|
|
11
12
|
*
|
|
12
|
-
* - tombstone: `keccak256("arc-bridge:release:" || uint kind || bytes32 evidence || uint ordinal || bridge)`;
|
|
13
|
-
* - bucket: `keccak256("arc-bridge:release-bucket:" || uint ring || uint slot || bridge)`, `ring` 0 =
|
|
13
|
+
* - tombstone: `"rt:" + keccak256("arc-bridge:release:" || uint kind || bytes32 evidence || uint ordinal || bridge)[0..63)`;
|
|
14
|
+
* - bucket: `"rb:" + keccak256("arc-bridge:release-bucket:" || uint ring || uint slot || bridge)[0..63)`, `ring` 0 =
|
|
15
|
+
* deposit, 1 = refund.
|
|
16
|
+
*
|
|
17
|
+
* #1680: the keys are SELF-IDENTIFYING (`@ocap/mcrypto` `bridgeReleaseTombstoneKey` / `bridgeReleaseBucketKey`, the one
|
|
18
|
+
* derivation the bridge runtime uses too). Legacy rollup v1 protocols write evidence rows at a caller-chosen
|
|
19
|
+
* `patterns.txHash` key; no such key is ever in the `rt:` / `rb:` space, so a v1 tx can not pre-write a tombstone (a
|
|
20
|
+
* faked replay) or a bucket (a bricked ring). The gate refuses every tx type but a bridge release any write in that
|
|
21
|
+
* space, by key.
|
|
14
22
|
*
|
|
15
23
|
* A bucket row stores `"<ring>:<slot>:<bridge>:<hourIndex>:<amountInBaseUnits>"`. The row names its own bridge, ring and
|
|
16
|
-
* slot, so the post-execution gate can tell
|
|
17
|
-
* `release_bridge_token` writing one) without a global list of bridges: it recomputes the key from the row's own fields.
|
|
24
|
+
* slot, so the post-execution gate can tell which bucket a release wrote: it recomputes the key from the row's own fields.
|
|
18
25
|
*
|
|
19
26
|
* `bridge` is always the canonical bridge address (no `did:abt:` prefix), exactly as `itx.bridge` must be spelled.
|
|
20
27
|
*/
|
|
21
28
|
const RELEASE_RING_SLOTS = 24;
|
|
22
29
|
const RELEASE_RINGS = ["deposit", "refund"];
|
|
23
|
-
const TOMBSTONE_PREFIX = "arc-bridge:release:";
|
|
24
|
-
const BUCKET_PREFIX = "arc-bridge:release-bucket:";
|
|
25
30
|
const ringIndex = (ring) => RELEASE_RINGS.indexOf(ring);
|
|
26
31
|
/** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
|
|
27
32
|
function getReleaseTombstoneKey(bridge, kind, evidence, ordinal) {
|
|
28
|
-
return (0, _ocap_mcrypto.
|
|
29
|
-
{ uint: kind },
|
|
30
|
-
{ bytes32: String(evidence).toLowerCase() },
|
|
31
|
-
{ uint: ordinal },
|
|
32
|
-
{ text: bridge }
|
|
33
|
-
]);
|
|
33
|
+
return (0, _ocap_mcrypto.bridgeReleaseTombstoneKey)(bridge, kind, String(evidence).toLowerCase(), ordinal);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* `data` of the rows the legacy rollup v1 protocols write at a CALLER-CHOSEN key (`itx.evidence.hash`, any
|
|
37
|
+
* `patterns.txHash`): `join_rollup`, `leave_rollup`, `deposit_token_v2` (v1) and `claim_block_reward`. Those are the only
|
|
38
|
+
* writers that can name a keccak `0x` bridge key (#1680); every other evidence key is derived (unsigned-tx, abuse-slash)
|
|
39
|
+
* or must be an existing tx (`claim_stake`).
|
|
40
|
+
*/
|
|
41
|
+
const V1_EVIDENCE_DATA = new Set([
|
|
42
|
+
"rollup-join",
|
|
43
|
+
"rollup-leave",
|
|
44
|
+
"rollup-deposit",
|
|
45
|
+
"rollup-claim-block-reward"
|
|
46
|
+
]);
|
|
47
|
+
/**
|
|
48
|
+
* Whether `row` is a row a legacy v1 tx planted (its `data` is exactly one of `V1_EVIDENCE_DATA`). Such a row at a
|
|
49
|
+
* keccak `0x` bridge key (membership tombstone, gateway / token / reserve index) blocks nothing and is replaced by the
|
|
50
|
+
* bridge record. Any OTHER existing row at a bridge key blocks (fail closed): a bridge tx never overwrites a row it can
|
|
51
|
+
* not identify.
|
|
52
|
+
*/
|
|
53
|
+
const isV1EvidenceRow = (row) => !!row && typeof row.data === "string" && V1_EVIDENCE_DATA.has(row.data);
|
|
35
54
|
/** Key of slot `slot` (0..23) of `ring` of `bridge`. */
|
|
36
55
|
function getReleaseBucketKey(bridge, ring, slot) {
|
|
37
56
|
if (!RELEASE_RINGS.includes(ring) || !Number.isInteger(slot) || slot < 0 || slot >= RELEASE_RING_SLOTS) throw new Error(`Invalid release bucket: ${ring}:${slot}`);
|
|
38
|
-
return (0, _ocap_mcrypto.
|
|
39
|
-
{ uint: ringIndex(ring) },
|
|
40
|
-
{ uint: slot },
|
|
41
|
-
{ text: bridge }
|
|
42
|
-
]);
|
|
57
|
+
return (0, _ocap_mcrypto.bridgeReleaseBucketKey)(bridge, ringIndex(ring), slot);
|
|
43
58
|
}
|
|
44
59
|
/** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
|
|
45
60
|
const formatBucketData = (bridge, ring, slot, window) => `${ring}:${slot}:${bridge}:${window}`;
|
|
@@ -81,8 +96,10 @@ function identifyBucketRow(row) {
|
|
|
81
96
|
//#endregion
|
|
82
97
|
exports.RELEASE_RINGS = RELEASE_RINGS;
|
|
83
98
|
exports.RELEASE_RING_SLOTS = RELEASE_RING_SLOTS;
|
|
99
|
+
exports.V1_EVIDENCE_DATA = V1_EVIDENCE_DATA;
|
|
84
100
|
exports.formatBucketData = formatBucketData;
|
|
85
101
|
exports.getReleaseBucketKey = getReleaseBucketKey;
|
|
86
102
|
exports.getReleaseTombstoneKey = getReleaseTombstoneKey;
|
|
87
103
|
exports.identifyBucketRow = identifyBucketRow;
|
|
104
|
+
exports.isV1EvidenceRow = isV1EvidenceRow;
|
|
88
105
|
exports.parseBucketData = parseBucketData;
|
|
@@ -4,6 +4,22 @@ declare const RELEASE_RINGS: readonly ["deposit", "refund"];
|
|
|
4
4
|
type ReleaseRing = (typeof RELEASE_RINGS)[number];
|
|
5
5
|
/** Tombstone key of a release. `evidence` is a lowercase `0x` bytes32 (the itx schema enforces the spelling). */
|
|
6
6
|
declare function getReleaseTombstoneKey(bridge: string, kind: number, evidence: string, ordinal: number): string;
|
|
7
|
+
/**
|
|
8
|
+
* `data` of the rows the legacy rollup v1 protocols write at a CALLER-CHOSEN key (`itx.evidence.hash`, any
|
|
9
|
+
* `patterns.txHash`): `join_rollup`, `leave_rollup`, `deposit_token_v2` (v1) and `claim_block_reward`. Those are the only
|
|
10
|
+
* writers that can name a keccak `0x` bridge key (#1680); every other evidence key is derived (unsigned-tx, abuse-slash)
|
|
11
|
+
* or must be an existing tx (`claim_stake`).
|
|
12
|
+
*/
|
|
13
|
+
declare const V1_EVIDENCE_DATA: ReadonlySet<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Whether `row` is a row a legacy v1 tx planted (its `data` is exactly one of `V1_EVIDENCE_DATA`). Such a row at a
|
|
16
|
+
* keccak `0x` bridge key (membership tombstone, gateway / token / reserve index) blocks nothing and is replaced by the
|
|
17
|
+
* bridge record. Any OTHER existing row at a bridge key blocks (fail closed): a bridge tx never overwrites a row it can
|
|
18
|
+
* not identify.
|
|
19
|
+
*/
|
|
20
|
+
declare const isV1EvidenceRow: (row: {
|
|
21
|
+
data?: unknown;
|
|
22
|
+
} | null | undefined) => boolean;
|
|
7
23
|
/** Key of slot `slot` (0..23) of `ring` of `bridge`. */
|
|
8
24
|
declare function getReleaseBucketKey(bridge: string, ring: ReleaseRing, slot: number): string;
|
|
9
25
|
/** Row data of a bucket: names the bridge, ring and slot it belongs to, then `<hourIndex>:<amount>`. */
|
|
@@ -30,4 +46,4 @@ declare function identifyBucketRow(row: {
|
|
|
30
46
|
slot: number;
|
|
31
47
|
} | null;
|
|
32
48
|
//#endregion
|
|
33
|
-
export { ParsedBucketData, RELEASE_RINGS, RELEASE_RING_SLOTS, ReleaseRing, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, parseBucketData };
|
|
49
|
+
export { ParsedBucketData, RELEASE_RINGS, RELEASE_RING_SLOTS, ReleaseRing, V1_EVIDENCE_DATA, formatBucketData, getReleaseBucketKey, getReleaseTombstoneKey, identifyBucketRow, isV1EvidenceRow, parseBucketData };
|
package/lib/bridge-reserves.cjs
CHANGED
|
@@ -21,32 +21,75 @@ let _ocap_state = require("@ocap/state");
|
|
|
21
21
|
*
|
|
22
22
|
* Reads go to `context.statedb` and are cached on the context for the lifetime of one transaction.
|
|
23
23
|
*/
|
|
24
|
-
const BRIDGE_RELEASE_TX_TYPE = "release_bridge_token";
|
|
25
|
-
const BRIDGE_WITHDRAW_TX_TYPE = "transfer_v2";
|
|
26
24
|
/**
|
|
27
|
-
*
|
|
28
|
-
* `
|
|
25
|
+
* The withdraw (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4): a `withdraw_token_v2` whose
|
|
26
|
+
* `itx.rollup` is a v2 bridge.
|
|
27
|
+
*/
|
|
28
|
+
const BRIDGE_WITHDRAW_V2_TX_TYPE = "withdraw_token_v2";
|
|
29
|
+
/**
|
|
30
|
+
* The release (#1692, bridge/docs/deposit-withdraw-v2-unification.md §2.3, §2.5): a `deposit_token_v2` with
|
|
31
|
+
* `kind ∈ {1,2,3}` (DEPOSIT, REFUND, DEPOSIT_B), the only tx that debits a bridge reserve for a user.
|
|
32
|
+
*/
|
|
33
|
+
const BRIDGE_RELEASE_V2_TX_TYPE = "deposit_token_v2";
|
|
34
|
+
/** The `kind` values of a bridge release (`OcapRelease.kind`); 0 is a legacy (v1 rollup) deposit. */
|
|
35
|
+
const BRIDGE_RELEASE_KINDS = [
|
|
36
|
+
1,
|
|
37
|
+
2,
|
|
38
|
+
3
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* maxTxSize / maxGasOps key of the release (§2.7 item 3). Not a tx type: it never appears in `supportedTxs`.
|
|
42
|
+
* It passes the config key pattern `fg:t:[_a-z0-9]+`.
|
|
43
|
+
*/
|
|
44
|
+
const BRIDGE_RELEASE_V2_RESOURCE_KEY = "fg:t:deposit_token_v2_bridge";
|
|
45
|
+
/**
|
|
46
|
+
* Tx types that may DEBIT a reserve (R1), besides a bridge release (`isNamedBridgeRelease`, which may only debit the
|
|
47
|
+
* reserve of its own bridge, or of the `migratedTo` chain of a migrated bridge): `migrate_rollup` moves a whole reserve
|
|
48
|
+
* to its successor's.
|
|
29
49
|
* The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
|
|
30
50
|
* in the gate has to change for it to be allowed to debit a mapped reserve.
|
|
31
51
|
*/
|
|
32
52
|
const BRIDGE_RECONCILE_TX_TYPES = [];
|
|
33
|
-
const BRIDGE_RESERVE_DEBIT_TX_TYPES = [
|
|
34
|
-
BRIDGE_RELEASE_TX_TYPE,
|
|
35
|
-
"migrate_rollup",
|
|
36
|
-
...BRIDGE_RECONCILE_TX_TYPES
|
|
37
|
-
];
|
|
53
|
+
const BRIDGE_RESERVE_DEBIT_TX_TYPES = ["migrate_rollup", ...BRIDGE_RECONCILE_TX_TYPES];
|
|
38
54
|
/**
|
|
39
|
-
* Tx types that may CREDIT a reserve (R2). `
|
|
40
|
-
* bridge; `create_rollup` funds the reserve of the bridge it creates;
|
|
55
|
+
* Tx types that may CREDIT a reserve (R2). `withdraw_token_v2` (bridge branch) is the withdraw and
|
|
56
|
+
* only into a live, unpaused, not-closing bridge; `create_rollup` funds the reserve of the bridge it creates;
|
|
57
|
+
* `migrate_rollup` fills the successor's reserve.
|
|
41
58
|
*/
|
|
42
59
|
const BRIDGE_RESERVE_CREDIT_TX_TYPES = [
|
|
43
|
-
|
|
60
|
+
BRIDGE_WITHDRAW_V2_TX_TYPE,
|
|
44
61
|
"create_rollup",
|
|
45
62
|
"migrate_rollup"
|
|
46
63
|
];
|
|
47
64
|
/** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
|
|
48
65
|
const BRIDGE_MAX_MIGRATION_HOPS = 16;
|
|
49
66
|
const stripTxTypePrefix = (txType) => String(txType || "").replace(/^fg:t:/, "");
|
|
67
|
+
/**
|
|
68
|
+
* `isBridgeRelease(txType, itx)` (§2.3, one definition for the whole design): `txType` is `deposit_token_v2` and
|
|
69
|
+
* `itx.kind ∈ {1,2,3}`. Stateless (the kind is in the itx); the protocol also requires the row `itx.rollup` names to be a
|
|
70
|
+
* v2 bridge (both keys must match). Strict: a kind that is not one of the three numbers is not a bridge release.
|
|
71
|
+
*/
|
|
72
|
+
function isBridgeRelease(txType, itx) {
|
|
73
|
+
if (stripTxTypePrefix(txType) !== BRIDGE_RELEASE_V2_TX_TYPE) return false;
|
|
74
|
+
const kind = itx?.kind;
|
|
75
|
+
return typeof kind === "number" && BRIDGE_RELEASE_KINDS.includes(kind);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* For the gate: an `isBridgeRelease` that also NAMES its bridge (`itx.rollup` a non-empty string). Narrower, so fail
|
|
79
|
+
* closed: a bridge kind without a bridge is no release at all (the protocol refuses it before any write), and it owns
|
|
80
|
+
* nothing in the release key space and may debit no reserve.
|
|
81
|
+
*/
|
|
82
|
+
function isNamedBridgeRelease(txType, itx) {
|
|
83
|
+
const rollupField = itx?.rollup;
|
|
84
|
+
return isBridgeRelease(txType, itx) && typeof rollupField === "string" && rollupField.length > 0;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The `transaction.maxTxSize` / `maxGasOps` key a tx is measured by: `fg:t:deposit_token_v2_bridge` for a bridge release,
|
|
88
|
+
* the tx type itself otherwise (so every other tx, and every v1 deposit, keeps its key and limit).
|
|
89
|
+
*/
|
|
90
|
+
function txResourceKey(txType, itx) {
|
|
91
|
+
return isBridgeRelease(txType, itx) ? BRIDGE_RELEASE_V2_RESOURCE_KEY : txType;
|
|
92
|
+
}
|
|
50
93
|
/** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
|
|
51
94
|
const bareAddress = (address) => String(address ?? "").trim().replace(/^did:abt:/, "");
|
|
52
95
|
/** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
|
|
@@ -203,7 +246,8 @@ async function prepareBridgeReserveGate(context) {
|
|
|
203
246
|
});
|
|
204
247
|
const input = {
|
|
205
248
|
reserves: /* @__PURE__ */ new Map(),
|
|
206
|
-
release: null
|
|
249
|
+
release: null,
|
|
250
|
+
withdraw: null
|
|
207
251
|
};
|
|
208
252
|
Object.defineProperty(context, "_bridgeReserveGate", {
|
|
209
253
|
value: input,
|
|
@@ -223,7 +267,8 @@ async function prepareBridgeReserveGate(context) {
|
|
|
223
267
|
for (const k of tracker.writtenKeys.get(table) ?? []) consider(k);
|
|
224
268
|
}
|
|
225
269
|
for (const k of tracker.directBalanceWrites ?? []) consider(k);
|
|
226
|
-
const
|
|
270
|
+
const txType = stripTxTypePrefix(context.txType);
|
|
271
|
+
const isRelease = isNamedBridgeRelease(txType, context.itx);
|
|
227
272
|
if (candidates.size === 0 && !isRelease) return;
|
|
228
273
|
await Promise.all([...candidates].map(async (key) => {
|
|
229
274
|
try {
|
|
@@ -233,25 +278,35 @@ async function prepareBridgeReserveGate(context) {
|
|
|
233
278
|
}
|
|
234
279
|
}));
|
|
235
280
|
if (isRelease) try {
|
|
236
|
-
input.release = await resolveBridgeForRelease(context, context.itx);
|
|
281
|
+
input.release = await resolveBridgeForRelease(context, { bridge: context.itx.rollup });
|
|
237
282
|
} catch {
|
|
238
283
|
input.release = null;
|
|
239
284
|
}
|
|
285
|
+
if (txType === BRIDGE_WITHDRAW_V2_TX_TYPE && candidates.size > 0) try {
|
|
286
|
+
input.withdraw = await resolveBridge(context, context.itx?.rollup);
|
|
287
|
+
} catch {
|
|
288
|
+
input.withdraw = null;
|
|
289
|
+
}
|
|
240
290
|
}
|
|
241
291
|
|
|
242
292
|
//#endregion
|
|
243
293
|
exports.BRIDGE_MAX_MIGRATION_HOPS = BRIDGE_MAX_MIGRATION_HOPS;
|
|
244
294
|
exports.BRIDGE_RECONCILE_TX_TYPES = BRIDGE_RECONCILE_TX_TYPES;
|
|
245
|
-
exports.
|
|
295
|
+
exports.BRIDGE_RELEASE_KINDS = BRIDGE_RELEASE_KINDS;
|
|
296
|
+
exports.BRIDGE_RELEASE_V2_RESOURCE_KEY = BRIDGE_RELEASE_V2_RESOURCE_KEY;
|
|
297
|
+
exports.BRIDGE_RELEASE_V2_TX_TYPE = BRIDGE_RELEASE_V2_TX_TYPE;
|
|
246
298
|
exports.BRIDGE_RESERVE_CREDIT_TX_TYPES = BRIDGE_RESERVE_CREDIT_TX_TYPES;
|
|
247
299
|
exports.BRIDGE_RESERVE_DEBIT_TX_TYPES = BRIDGE_RESERVE_DEBIT_TX_TYPES;
|
|
248
|
-
exports.
|
|
300
|
+
exports.BRIDGE_WITHDRAW_V2_TX_TYPE = BRIDGE_WITHDRAW_V2_TX_TYPE;
|
|
249
301
|
exports.bareAddress = bareAddress;
|
|
250
302
|
exports.describeBridge = describeBridge;
|
|
303
|
+
exports.isBridgeRelease = isBridgeRelease;
|
|
251
304
|
exports.isBridgeReserveRole = isBridgeReserveRole;
|
|
305
|
+
exports.isNamedBridgeRelease = isNamedBridgeRelease;
|
|
252
306
|
exports.prepareBridgeReserveGate = prepareBridgeReserveGate;
|
|
253
307
|
exports.reserveIndexKey = reserveIndexKey;
|
|
254
308
|
exports.resolveBridge = resolveBridge;
|
|
255
309
|
exports.resolveBridgeForRelease = resolveBridgeForRelease;
|
|
256
310
|
exports.resolveBridgeForReserve = resolveBridgeForReserve;
|
|
257
|
-
exports.stripTxTypePrefix = stripTxTypePrefix;
|
|
311
|
+
exports.stripTxTypePrefix = stripTxTypePrefix;
|
|
312
|
+
exports.txResourceKey = txResourceKey;
|
|
@@ -3,24 +3,58 @@ import { rollup } from "@ocap/state";
|
|
|
3
3
|
//#region src/bridge-reserves.d.ts
|
|
4
4
|
type Ctx = any;
|
|
5
5
|
type AnyRow = Record<string, any>;
|
|
6
|
-
declare const BRIDGE_RELEASE_TX_TYPE = "release_bridge_token";
|
|
7
|
-
declare const BRIDGE_WITHDRAW_TX_TYPE = "transfer_v2";
|
|
8
6
|
/**
|
|
9
|
-
*
|
|
10
|
-
* `
|
|
7
|
+
* The withdraw (#1691, bridge/docs/deposit-withdraw-v2-unification.md §2.4): a `withdraw_token_v2` whose
|
|
8
|
+
* `itx.rollup` is a v2 bridge.
|
|
9
|
+
*/
|
|
10
|
+
declare const BRIDGE_WITHDRAW_V2_TX_TYPE = "withdraw_token_v2";
|
|
11
|
+
/**
|
|
12
|
+
* The release (#1692, bridge/docs/deposit-withdraw-v2-unification.md §2.3, §2.5): a `deposit_token_v2` with
|
|
13
|
+
* `kind ∈ {1,2,3}` (DEPOSIT, REFUND, DEPOSIT_B), the only tx that debits a bridge reserve for a user.
|
|
14
|
+
*/
|
|
15
|
+
declare const BRIDGE_RELEASE_V2_TX_TYPE = "deposit_token_v2";
|
|
16
|
+
/** The `kind` values of a bridge release (`OcapRelease.kind`); 0 is a legacy (v1 rollup) deposit. */
|
|
17
|
+
declare const BRIDGE_RELEASE_KINDS: readonly number[];
|
|
18
|
+
/**
|
|
19
|
+
* maxTxSize / maxGasOps key of the release (§2.7 item 3). Not a tx type: it never appears in `supportedTxs`.
|
|
20
|
+
* It passes the config key pattern `fg:t:[_a-z0-9]+`.
|
|
21
|
+
*/
|
|
22
|
+
declare const BRIDGE_RELEASE_V2_RESOURCE_KEY = "fg:t:deposit_token_v2_bridge";
|
|
23
|
+
/**
|
|
24
|
+
* Tx types that may DEBIT a reserve (R1), besides a bridge release (`isNamedBridgeRelease`, which may only debit the
|
|
25
|
+
* reserve of its own bridge, or of the `migratedTo` chain of a migrated bridge): `migrate_rollup` moves a whole reserve
|
|
26
|
+
* to its successor's.
|
|
11
27
|
* The reconcile transaction of §v2.6 is added to {@link BRIDGE_RECONCILE_TX_TYPES} when it is designed; nothing else
|
|
12
28
|
* in the gate has to change for it to be allowed to debit a mapped reserve.
|
|
13
29
|
*/
|
|
14
30
|
declare const BRIDGE_RECONCILE_TX_TYPES: readonly string[];
|
|
15
31
|
declare const BRIDGE_RESERVE_DEBIT_TX_TYPES: readonly string[];
|
|
16
32
|
/**
|
|
17
|
-
* Tx types that may CREDIT a reserve (R2). `
|
|
18
|
-
* bridge; `create_rollup` funds the reserve of the bridge it creates;
|
|
33
|
+
* Tx types that may CREDIT a reserve (R2). `withdraw_token_v2` (bridge branch) is the withdraw and
|
|
34
|
+
* only into a live, unpaused, not-closing bridge; `create_rollup` funds the reserve of the bridge it creates;
|
|
35
|
+
* `migrate_rollup` fills the successor's reserve.
|
|
19
36
|
*/
|
|
20
37
|
declare const BRIDGE_RESERVE_CREDIT_TX_TYPES: readonly string[];
|
|
21
38
|
/** Longest `migratedTo` chain followed for a refund (a loop or a pathological chain stops the walk, fail closed). */
|
|
22
39
|
declare const BRIDGE_MAX_MIGRATION_HOPS = 16;
|
|
23
40
|
declare const stripTxTypePrefix: (txType: unknown) => string;
|
|
41
|
+
/**
|
|
42
|
+
* `isBridgeRelease(txType, itx)` (§2.3, one definition for the whole design): `txType` is `deposit_token_v2` and
|
|
43
|
+
* `itx.kind ∈ {1,2,3}`. Stateless (the kind is in the itx); the protocol also requires the row `itx.rollup` names to be a
|
|
44
|
+
* v2 bridge (both keys must match). Strict: a kind that is not one of the three numbers is not a bridge release.
|
|
45
|
+
*/
|
|
46
|
+
declare function isBridgeRelease(txType: unknown, itx: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* For the gate: an `isBridgeRelease` that also NAMES its bridge (`itx.rollup` a non-empty string). Narrower, so fail
|
|
49
|
+
* closed: a bridge kind without a bridge is no release at all (the protocol refuses it before any write), and it owns
|
|
50
|
+
* nothing in the release key space and may debit no reserve.
|
|
51
|
+
*/
|
|
52
|
+
declare function isNamedBridgeRelease(txType: unknown, itx: unknown): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The `transaction.maxTxSize` / `maxGasOps` key a tx is measured by: `fg:t:deposit_token_v2_bridge` for a bridge release,
|
|
55
|
+
* the tx type itself otherwise (so every other tx, and every v1 deposit, keeps its key and limit).
|
|
56
|
+
*/
|
|
57
|
+
declare function txResourceKey(txType: string, itx: unknown): string;
|
|
24
58
|
/** Strip a `did:abt:` prefix and whitespace: the form used in the evidence index and in rows. */
|
|
25
59
|
declare const bareAddress: (address: unknown) => string;
|
|
26
60
|
/** Whether `address` has the DID role of a bridge reserve. Syntactic only: see the file header. */
|
|
@@ -64,7 +98,7 @@ declare function resolveBridge(context: Ctx, bridgeAddress: unknown): Promise<Re
|
|
|
64
98
|
* address as one that accepts no credit and allows no debit.
|
|
65
99
|
*/
|
|
66
100
|
declare function resolveBridgeForReserve(context: Ctx, address: unknown): Promise<ResolvedBridge | null>;
|
|
67
|
-
/** What a
|
|
101
|
+
/** What a bridge release (or a reconcile) is allowed to debit. */
|
|
68
102
|
interface ReleaseResolution {
|
|
69
103
|
/** the bridge named by `itx.bridge` */
|
|
70
104
|
bridge: ResolvedBridge;
|
|
@@ -92,8 +126,13 @@ declare function resolveBridgeForRelease(context: Ctx, itx: {
|
|
|
92
126
|
interface BridgeReserveResolution {
|
|
93
127
|
/** reserve (bare address) -> its bridge, `null` when unmapped */
|
|
94
128
|
reserves: Map<string, ResolvedBridge | null>;
|
|
95
|
-
/** present for
|
|
129
|
+
/** present for a release whose bridge (`itx.rollup`) resolves */
|
|
96
130
|
release: ReleaseResolution | null;
|
|
131
|
+
/**
|
|
132
|
+
* `withdraw_token_v2` that touched a reserve: the v2 bridge `itx.rollup` names (pre-tx row), `null` when it names no
|
|
133
|
+
* v2 bridge (a v1 rollup, nothing, a failed read). Only the reserve of this bridge may be credited.
|
|
134
|
+
*/
|
|
135
|
+
withdraw?: ResolvedBridge | null;
|
|
97
136
|
}
|
|
98
137
|
/**
|
|
99
138
|
* Resolve, once per transaction, everything the synchronous post-execution gate needs about bridge reserves. Run it
|
|
@@ -104,4 +143,4 @@ interface BridgeReserveResolution {
|
|
|
104
143
|
*/
|
|
105
144
|
declare function prepareBridgeReserveGate(context: Ctx): Promise<void>;
|
|
106
145
|
//#endregion
|
|
107
|
-
export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES,
|
|
146
|
+
export { BRIDGE_MAX_MIGRATION_HOPS, BRIDGE_RECONCILE_TX_TYPES, BRIDGE_RELEASE_KINDS, BRIDGE_RELEASE_V2_RESOURCE_KEY, BRIDGE_RELEASE_V2_TX_TYPE, BRIDGE_RESERVE_CREDIT_TX_TYPES, BRIDGE_RESERVE_DEBIT_TX_TYPES, BRIDGE_WITHDRAW_V2_TX_TYPE, BridgeReserveResolution, ReleaseResolution, ResolvedBridge, bareAddress, describeBridge, isBridgeRelease, isBridgeReserveRole, isNamedBridgeRelease, prepareBridgeReserveGate, reserveIndexKey, resolveBridge, resolveBridgeForRelease, resolveBridgeForReserve, stripTxTypePrefix, txResourceKey };
|