@ocap/tx-protocols 1.31.5 → 1.31.7
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.mjs +1 -1
- package/esm/execute.mjs +24 -5
- package/esm/gas-security-guard.d.mts +2 -8
- package/esm/gas-security-guard.mjs +16 -1
- package/esm/index.d.mts +16 -3
- package/esm/index.mjs +0 -2
- package/esm/pipes/ensure-cost.mjs +3 -0
- package/esm/pipes/ensure-gas.d.mts +2 -1
- package/esm/pipes/write-ledger.mjs +2 -1
- package/esm/protocols/bridge/release-checks.d.mts +69 -0
- package/esm/protocols/bridge/{release.mjs → release-checks.mjs} +101 -100
- package/esm/protocols/rollup/bridge-create.mjs +2 -2
- package/esm/protocols/rollup/bridge-membership.mjs +10 -12
- package/esm/protocols/rollup/bridge-v2.d.mts +15 -5
- package/esm/protocols/rollup/bridge-v2.mjs +33 -8
- package/esm/protocols/token/deposit-v2.mjs +126 -25
- package/esm/protocols/token/withdraw-v2.d.mts +13 -1
- package/esm/protocols/token/withdraw-v2.mjs +158 -25
- package/esm/protocols/trade/transfer-v2.mjs +3 -8
- package/lib/bridge-release-crypto.cjs +1 -1
- package/lib/execute.cjs +24 -5
- package/lib/gas-security-guard.cjs +16 -1
- package/lib/gas-security-guard.d.cts +2 -8
- package/lib/index.cjs +0 -2
- package/lib/index.d.cts +16 -3
- package/lib/pipes/ensure-cost.cjs +3 -0
- package/lib/pipes/ensure-gas.d.cts +2 -1
- package/lib/pipes/write-ledger.cjs +2 -1
- package/lib/protocols/bridge/{release.cjs → release-checks.cjs} +115 -101
- package/lib/protocols/bridge/release-checks.d.cts +69 -0
- package/lib/protocols/rollup/bridge-create.cjs +1 -1
- package/lib/protocols/rollup/bridge-membership.cjs +9 -10
- package/lib/protocols/rollup/bridge-v2.cjs +32 -5
- package/lib/protocols/rollup/bridge-v2.d.cts +15 -5
- package/lib/protocols/token/deposit-v2.cjs +125 -24
- package/lib/protocols/token/withdraw-v2.cjs +155 -22
- package/lib/protocols/token/withdraw-v2.d.cts +13 -1
- package/lib/protocols/trade/transfer-v2.cjs +2 -7
- package/package.json +16 -16
- package/esm/protocols/bridge/release.d.mts +0 -34
- package/lib/protocols/bridge/release.d.cts +0 -34
|
@@ -1,31 +1,30 @@
|
|
|
1
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
1
|
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
3
2
|
const require_bridge_release_crypto = require('../../bridge-release-crypto.cjs');
|
|
4
3
|
const require_bridge_release_window = require('../../bridge-release-window.cjs');
|
|
5
4
|
const require_util = require('../../util.cjs');
|
|
6
|
-
|
|
7
|
-
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
8
|
-
const require_stateless_prefix = require('../../stateless-prefix.cjs');
|
|
5
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
9
6
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
10
7
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
11
8
|
let _ocap_state = require("@ocap/state");
|
|
12
9
|
let _ocap_util = require("@ocap/util");
|
|
13
|
-
let _arcblock_validator = require("@arcblock/validator");
|
|
14
10
|
|
|
15
|
-
//#region src/protocols/bridge/release.ts
|
|
11
|
+
//#region src/protocols/bridge/release-checks.ts
|
|
16
12
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
13
|
+
* The checks of a bridge release (bridge/docs/ocap-protocols.md §3 checks 1-10, §v2.2, §v2.4): the bridge branch of
|
|
14
|
+
* `deposit_token_v2` with `kind ∈ {1,2,3}` on a v2 bridge (`protocols/token/deposit-v2.ts`,
|
|
15
|
+
* bridge/docs/deposit-withdraw-v2-unification.md §2.5), the only form a release can take.
|
|
20
16
|
*
|
|
21
|
-
* The
|
|
22
|
-
*
|
|
17
|
+
* The runner validates the itx shape and maps it onto one {@link ReleaseView} (`context.releaseItx`); every pipe below
|
|
18
|
+
* reads that view only. The tombstone and bucket keys are `getReleaseTombstoneKey` / `getReleaseBucketKey`.
|
|
23
19
|
*
|
|
24
|
-
* Lifecycle per kind (§v2.2 `pause_rollup` / `close_rollup
|
|
20
|
+
* Lifecycle per kind (§v2.2 `pause_rollup` / `close_rollup`):
|
|
25
21
|
* - not closing: a pause of any kind (lone or quorum) refuses every kind, REFUND included;
|
|
26
22
|
* - closing (`close_rollup` has run; the bridge is quorum-paused for good): DEPOSIT / DEPOSIT_B are accepted until
|
|
27
23
|
* `finalAt` (validators must release the backlog before finality), REFUND is accepted for as long as it is due,
|
|
28
24
|
* also after finality, and also from the reserve of a successor once the funds were migrated.
|
|
25
|
+
*
|
|
26
|
+
* The sender-state pipes, the snapshot and the state-diff check stay in each runner file (the #960 structural test reads
|
|
27
|
+
* them from the protocol source).
|
|
29
28
|
*/
|
|
30
29
|
const RELEASE_KIND = {
|
|
31
30
|
DEPOSIT: 1,
|
|
@@ -37,33 +36,13 @@ const MAX_MIGRATION_HOPS = 8;
|
|
|
37
36
|
const UINT256_MAX = (1n << 256n) - 1n;
|
|
38
37
|
const invalid = (message) => new _ocap_util_lib_error.CustomError("INVALID_TX", message);
|
|
39
38
|
const forbidden = (message) => new _ocap_util_lib_error.CustomError("FORBIDDEN", message);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
RELEASE_KIND.DEPOSIT,
|
|
45
|
-
RELEASE_KIND.REFUND,
|
|
46
|
-
RELEASE_KIND.DEPOSIT_B
|
|
47
|
-
]),
|
|
48
|
-
bridge: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.maxLength(128)),
|
|
49
|
-
evidence: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^0x[0-9a-f]{64}$/)),
|
|
50
|
-
ordinal: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0)),
|
|
51
|
-
to: _arcblock_validator.schemas.tokenHolder,
|
|
52
|
-
amount: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^[1-9]\d{0,77}$/)),
|
|
53
|
-
ethChainId: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(/^[1-9]\d{0,18}$/)),
|
|
54
|
-
epoch: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.integer(), _arcblock_validator.v.minValue(0), _arcblock_validator.v.maxValue(Number.MAX_SAFE_INTEGER)),
|
|
55
|
-
ethSender: _arcblock_validator.v.optional(_arcblock_validator.v.string(), ""),
|
|
56
|
-
claimSignature: _arcblock_validator.v.optional(_arcblock_validator.v.string(), ""),
|
|
57
|
-
signaturesList: _arcblock_validator.v.pipe(_arcblock_validator.v.array(_arcblock_validator.v.string()), _arcblock_validator.v.maxLength(16)),
|
|
58
|
-
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
59
|
-
}), (0, _arcblock_validator.vStripUnknown)());
|
|
60
|
-
runner.use((context, next) => {
|
|
61
|
-
const { error } = (0, _arcblock_validator.vValidate)(schema, context.itx);
|
|
62
|
-
if (error) return next(invalid(`Invalid itx: ${error.message}`));
|
|
63
|
-
next();
|
|
39
|
+
/** The pipes below are named (structural tests and debugging find pipes by name). */
|
|
40
|
+
const named = (name, fn) => Object.defineProperty(fn, "name", {
|
|
41
|
+
value: name,
|
|
42
|
+
configurable: true
|
|
64
43
|
});
|
|
65
|
-
|
|
66
|
-
const
|
|
44
|
+
const checkReleaseFields = named("CheckReleaseFields", (context, next) => {
|
|
45
|
+
const itx = context.releaseItx;
|
|
67
46
|
if (itx.ordinal !== 0) return next(invalid("Release ordinal must be 0 in v1"));
|
|
68
47
|
if (BigInt(itx.amount) > UINT256_MAX) return next(invalid("Release amount exceeds uint256"));
|
|
69
48
|
if (itx.kind === RELEASE_KIND.DEPOSIT_B) {
|
|
@@ -72,24 +51,28 @@ runner.use((context, next) => {
|
|
|
72
51
|
} else if (itx.ethSender !== "" || itx.claimSignature !== "") return next(invalid("ethSender and claimSignature are only valid for DEPOSIT_B"));
|
|
73
52
|
next();
|
|
74
53
|
});
|
|
75
|
-
|
|
76
|
-
runner.use((context, next) => {
|
|
54
|
+
const rejectReleaseDelegator = named("RejectReleaseDelegator", (context, next) => {
|
|
77
55
|
if (context.tx.delegator) return next(invalid("A release can not be delegated"));
|
|
78
56
|
next();
|
|
79
57
|
});
|
|
80
|
-
|
|
58
|
+
const resolveReleaseTime = named("ResolveReleaseTime", (context, next) => {
|
|
81
59
|
const { txTime } = context;
|
|
82
60
|
const ms = typeof txTime === "string" ? new Date(txTime).getTime() : NaN;
|
|
83
61
|
if (!Number.isFinite(ms) || ms <= 0) return next(invalid("Release requires a valid tx time"));
|
|
84
62
|
context.releaseTimeMs = ms;
|
|
85
63
|
next();
|
|
86
64
|
});
|
|
87
|
-
|
|
88
|
-
|
|
65
|
+
/**
|
|
66
|
+
* 1. master switch, the bridge the release names, its lifecycle for this kind.
|
|
67
|
+
* `opts.token`: the release names the token (`itx.token.address`), which must be the bridge token (fail closed).
|
|
68
|
+
*/
|
|
69
|
+
const resolveReleaseBridge = (opts) => named("ResolveReleaseBridge", async (context, next) => {
|
|
70
|
+
const { config } = context;
|
|
71
|
+
const itx = context.releaseItx;
|
|
89
72
|
if (!config?.transaction?.bridge?.enabled) return next(forbidden("Bridges are not enabled on this chain"));
|
|
90
73
|
let release;
|
|
91
74
|
try {
|
|
92
|
-
release = await (0, _ocap_tx_pipeline.resolveBridgeForRelease)(context, itx);
|
|
75
|
+
release = await (0, _ocap_tx_pipeline.resolveBridgeForRelease)(context, { bridge: itx.bridge });
|
|
93
76
|
} catch (err) {
|
|
94
77
|
return next(err);
|
|
95
78
|
}
|
|
@@ -106,17 +89,22 @@ runner.use(async (context, next) => {
|
|
|
106
89
|
if (!signing) return next(invalid("The reserve that holds the funds of this bridge could not be resolved"));
|
|
107
90
|
if (signing !== bridge && !signing.closing && signing.paused) return next(forbidden(`Bridge ${signing.address}, which holds the funds, is paused`));
|
|
108
91
|
context.signingBridge = signing;
|
|
109
|
-
if (
|
|
92
|
+
if (opts.token) {
|
|
93
|
+
const token = opts.token(context);
|
|
94
|
+
if (typeof token !== "string" || token !== bridge.token) return next(invalid(`Release token ${String(token)} is not the token of bridge ${bridge.address}`));
|
|
95
|
+
}
|
|
110
96
|
next();
|
|
111
97
|
});
|
|
112
|
-
|
|
113
|
-
const {
|
|
98
|
+
const checkReleaseMinimum = named("CheckReleaseMinimum", (context, next) => {
|
|
99
|
+
const { release, releaseAmount } = context;
|
|
100
|
+
const itx = context.releaseItx;
|
|
114
101
|
const min = BigInt(release.bridge.body.minDeposit);
|
|
115
102
|
if (itx.kind !== RELEASE_KIND.REFUND && releaseAmount < min) return next(invalid(`Release amount is below the minimum deposit ${min}`));
|
|
116
103
|
next();
|
|
117
104
|
});
|
|
118
|
-
|
|
119
|
-
const { config
|
|
105
|
+
const checkReleaseRecipient = named("CheckReleaseRecipient", (context, next) => {
|
|
106
|
+
const { config } = context;
|
|
107
|
+
const itx = context.releaseItx;
|
|
120
108
|
const to = (0, _ocap_tx_pipeline.normalizeEntityKey)(itx.to);
|
|
121
109
|
if (typeof itx.to !== "string" || itx.to.startsWith("did:abt:") || _ocap_state.account.ensureChecksumAddress(itx.to) !== itx.to) return next(invalid("Release recipient is not in canonical form (bare address, EIP-55 checksum for 0x)"));
|
|
122
110
|
if ((0, _ocap_tx_pipeline.isBridgeReserveRole)(itx.to)) return next(invalid("A release can not pay a bridge reserve"));
|
|
@@ -130,15 +118,17 @@ runner.use((context, next) => {
|
|
|
130
118
|
].filter((x) => typeof x === "string" && !!x).map((x) => (0, _ocap_tx_pipeline.normalizeEntityKey)(x)).includes(to)) return next(invalid("A release can not pay a vault"));
|
|
131
119
|
next();
|
|
132
120
|
});
|
|
133
|
-
|
|
134
|
-
const {
|
|
121
|
+
const checkReleaseTombstone = named("CheckReleaseTombstone", async (context, next) => {
|
|
122
|
+
const { statedb, release } = context;
|
|
123
|
+
const itx = context.releaseItx;
|
|
135
124
|
const key = (0, _ocap_tx_pipeline.getReleaseTombstoneKey)(release.bridge.address, itx.kind, itx.evidence, itx.ordinal);
|
|
136
125
|
context.tombstoneKey = key;
|
|
137
126
|
if (await statedb.evidence.get(key, context)) return next(invalid("Release already executed for this evidence"));
|
|
138
127
|
next();
|
|
139
128
|
});
|
|
140
|
-
|
|
141
|
-
const {
|
|
129
|
+
const verifyReleaseSignatures = named("VerifyReleaseSignatures", (context, next) => {
|
|
130
|
+
const { release, releaseTimeMs } = context;
|
|
131
|
+
const itx = context.releaseItx;
|
|
142
132
|
const bridge = release.bridge;
|
|
143
133
|
const body = context.signingBridge.body;
|
|
144
134
|
const epochs = body.epochs;
|
|
@@ -178,8 +168,8 @@ runner.use((context, next) => {
|
|
|
178
168
|
}
|
|
179
169
|
next();
|
|
180
170
|
});
|
|
181
|
-
|
|
182
|
-
const
|
|
171
|
+
const verifyReleaseClaim = named("VerifyReleaseClaim", (context, next) => {
|
|
172
|
+
const itx = context.releaseItx;
|
|
183
173
|
if (itx.kind !== RELEASE_KIND.DEPOSIT_B) return next();
|
|
184
174
|
const body = context.signingBridge.body;
|
|
185
175
|
const signer = require_bridge_release_crypto.recoverEthSigner(require_bridge_release_crypto.getDepositClaimDigest({
|
|
@@ -211,25 +201,42 @@ async function resolveMigrated(context, address) {
|
|
|
211
201
|
current = migratedTo[0];
|
|
212
202
|
}
|
|
213
203
|
}
|
|
214
|
-
|
|
215
|
-
|
|
204
|
+
const CANONICAL_DECIMAL = /^(0|[1-9]\d{0,77})$/;
|
|
205
|
+
const isEmptyOrZero = (value) => value === void 0 || value === null || value === "" || value === "0";
|
|
206
|
+
const isEmptyData = (value) => {
|
|
207
|
+
if (value === void 0 || value === null || value === "") return true;
|
|
208
|
+
if (typeof value !== "object") return false;
|
|
209
|
+
const { typeUrl, type, value: inner } = value;
|
|
210
|
+
const empty = (x) => x === void 0 || x === null || x === "";
|
|
211
|
+
return empty(typeUrl) && empty(type) && (empty(inner) || inner instanceof Uint8Array && inner.length === 0);
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* The amount a stored withdraw moved into the reserve of `bridge`, or a rejection message. The only refundable withdraw
|
|
215
|
+
* is the bridge branch of `withdraw_token_v2` naming THIS bridge in `itx.rollup` (whether or not it migrated since).
|
|
216
|
+
*/
|
|
217
|
+
function refundableAmount(withdrawTx, bridge) {
|
|
218
|
+
const itxJson = withdrawTx.itxJson || {};
|
|
219
|
+
const token = (0, _ocap_tx_pipeline.normalizeEntityKey)(bridge.token);
|
|
220
|
+
if (typeof itxJson.rollup !== "string" || itxJson.rollup !== bridge.address) return { error: "Refund evidence is not a withdraw_token_v2 from this bridge" };
|
|
221
|
+
if (withdrawTx.delegator) return { error: "A delegated withdraw can not be refunded" };
|
|
222
|
+
if (!(itxJson.proposer === void 0 || itxJson.proposer === null || itxJson.proposer === "") || !isEmptyOrZero(itxJson.maxFee) || !isEmptyOrZero(itxJson.actualFee) || !(0, _ocap_mcrypto.parseStrictEthAddress)(itxJson.to).ok || !isEmptyData(itxJson.data)) return { error: "Refund evidence is not a bridge withdraw_token_v2 (proposer, fees, recipient or data)" };
|
|
223
|
+
const entry = itxJson.token || {};
|
|
224
|
+
if ((0, _ocap_tx_pipeline.normalizeEntityKey)(entry.address) !== token) return { error: "Refund evidence moved a token other than the bridge token" };
|
|
225
|
+
if (typeof entry.value !== "string" || !CANONICAL_DECIMAL.test(entry.value)) return { error: "Refund evidence has no canonical withdraw amount" };
|
|
226
|
+
return { amount: BigInt(entry.value) };
|
|
227
|
+
}
|
|
228
|
+
const checkReleaseRefund = named("CheckReleaseRefund", async (context, next) => {
|
|
229
|
+
const { release, statedb, releaseTimeMs, releaseAmount } = context;
|
|
230
|
+
const itx = context.releaseItx;
|
|
216
231
|
if (itx.kind !== RELEASE_KIND.REFUND) return next();
|
|
217
232
|
const bridge = release.bridge;
|
|
218
233
|
const body = bridge.body;
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
if (!withdraw || withdraw.code !== "OK" || withdraw.type !== "transfer_v2") return next(invalid("Refund evidence is not a successful transfer_v2"));
|
|
234
|
+
const withdraw = await statedb.tx.get(itx.evidence.replace(/^0x/i, "").toUpperCase(), context);
|
|
235
|
+
if (!withdraw || withdraw.code !== "OK" || withdraw.type !== "withdraw_token_v2") return next(invalid("Refund evidence is not a successful bridge withdraw_token_v2"));
|
|
222
236
|
const withdrawTx = withdraw.tx || {};
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
225
|
-
if (
|
|
226
|
-
if ((itxJson.assets || []).length > 0) return next(invalid("Refund evidence moved assets"));
|
|
227
|
-
let paid = new _ocap_util.BN(itxJson.value || "0");
|
|
228
|
-
for (const entry of itxJson.tokens || []) {
|
|
229
|
-
if ((0, _ocap_tx_pipeline.normalizeEntityKey)(entry.address) !== token) return next(invalid("Refund evidence moved a token other than the bridge token"));
|
|
230
|
-
paid = paid.add(new _ocap_util.BN(entry.value));
|
|
231
|
-
}
|
|
232
|
-
if (BigInt(paid.toString(10)) !== releaseAmount) return next(invalid("Refund amount does not match the withdraw"));
|
|
237
|
+
const paid = refundableAmount(withdrawTx, bridge);
|
|
238
|
+
if ("error" in paid) return next(invalid(paid.error));
|
|
239
|
+
if (paid.amount !== releaseAmount) return next(invalid("Refund amount does not match the withdraw"));
|
|
233
240
|
const withdrawMs = new Date(withdraw.time).getTime();
|
|
234
241
|
if (!Number.isFinite(withdrawMs)) return next(invalid("Refund evidence has no valid time"));
|
|
235
242
|
const withdrawSec = Math.floor(withdrawMs / 1e3);
|
|
@@ -256,14 +263,15 @@ runner.use(async (context, next) => {
|
|
|
256
263
|
context.refundLedger = ledger;
|
|
257
264
|
next();
|
|
258
265
|
});
|
|
259
|
-
|
|
260
|
-
const state = await context.statedb.account.get(context.
|
|
266
|
+
const checkReleaseRecipientState = named("CheckReleaseRecipientState", async (context, next) => {
|
|
267
|
+
const state = await context.statedb.account.get(context.releaseItx.to, rawRead(context));
|
|
261
268
|
if (state?.migratedTo?.length) return next(invalid("Release recipient has been migrated"));
|
|
262
269
|
context.receiverState = state ?? void 0;
|
|
263
270
|
next();
|
|
264
271
|
});
|
|
265
|
-
|
|
266
|
-
const {
|
|
272
|
+
const checkReleaseCap = named("CheckReleaseCap", async (context, next) => {
|
|
273
|
+
const { release, statedb, releaseTimeMs, releaseAmount } = context;
|
|
274
|
+
const itx = context.releaseItx;
|
|
267
275
|
const bridge = release.bridge;
|
|
268
276
|
const ring = itx.kind === RELEASE_KIND.REFUND ? "refund" : "deposit";
|
|
269
277
|
const cap = BigInt(ring === "refund" ? bridge.body.refundDailyCap : bridge.body.releaseDailyCap);
|
|
@@ -291,9 +299,9 @@ runner.use(async (context, next) => {
|
|
|
291
299
|
};
|
|
292
300
|
next();
|
|
293
301
|
});
|
|
294
|
-
|
|
295
|
-
const {
|
|
296
|
-
const reserveAddress =
|
|
302
|
+
const checkReleaseReserve = named("CheckReleaseReserve", async (context, next) => {
|
|
303
|
+
const { release, statedb, releaseAmount } = context;
|
|
304
|
+
const reserveAddress = context.releaseItx.kind === RELEASE_KIND.REFUND ? release.fundsReserve : release.reserve;
|
|
297
305
|
const reserveState = await statedb.account.get(reserveAddress, context);
|
|
298
306
|
if (!reserveState) return next(invalid("The bridge reserve account does not exist"));
|
|
299
307
|
const token = release.debitable.get(reserveAddress).token;
|
|
@@ -302,29 +310,23 @@ runner.use(async (context, next) => {
|
|
|
302
310
|
context.releaseToken = token;
|
|
303
311
|
next();
|
|
304
312
|
});
|
|
305
|
-
|
|
313
|
+
/** Gas operations of a release (for `EnsureTxGas`). */
|
|
314
|
+
const releaseGasEstimate = (context) => {
|
|
306
315
|
const ledger = (context.refundLedger || []).length;
|
|
307
316
|
return {
|
|
308
317
|
create: 1 + (context.receiverState ? 0 : 1) + (context.releaseRing?.exists ? 0 : 1),
|
|
309
318
|
update: 3 + (context.releaseRing?.exists ? 1 : 0) + ledger,
|
|
310
319
|
payment: 0
|
|
311
320
|
};
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
addressKey: "tx.from"
|
|
322
|
-
}));
|
|
323
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "receiverState"] }));
|
|
324
|
-
runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
|
|
325
|
-
runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
326
|
-
runner.use(async (context, next) => {
|
|
327
|
-
const { itx, tx, statedb, senderState, receiverState, reserveState, senderChange, releaseAmount, releaseRing, releaseToken, tombstoneKey, updateVaults, refundLedger } = context;
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* The state writes of a release: the tombstone (create only: a second release of the same evidence was refused above,
|
|
324
|
+
* and `create` fails on an existing key), the one bucket it uses, the refund ledger of the migrated bridges a REFUND
|
|
325
|
+
* draws through, the reserve debit, the recipient credit and the sender (nonce, gas).
|
|
326
|
+
*/
|
|
327
|
+
const writeRelease = named("WriteRelease", async (context, next) => {
|
|
328
|
+
const { tx, statedb, senderState, receiverState, reserveState, senderChange, releaseAmount, releaseRing, releaseToken, tombstoneKey, updateVaults, refundLedger } = context;
|
|
329
|
+
const itx = context.releaseItx;
|
|
328
330
|
const amount = new _ocap_util.BN(releaseAmount.toString(10));
|
|
329
331
|
const selfRelease = (0, _ocap_tx_pipeline.normalizeEntityKey)(senderState.address) === (0, _ocap_tx_pipeline.normalizeEntityKey)(itx.to);
|
|
330
332
|
await statedb.evidence.create(tombstoneKey, _ocap_state.evidence.create({
|
|
@@ -338,7 +340,7 @@ runner.use(async (context, next) => {
|
|
|
338
340
|
}, context), context);
|
|
339
341
|
for (const entry of refundLedger || []) {
|
|
340
342
|
const body = entry.state.data.value;
|
|
341
|
-
const
|
|
343
|
+
const updated = {
|
|
342
344
|
...entry.state,
|
|
343
345
|
data: {
|
|
344
346
|
...entry.state.data,
|
|
@@ -348,7 +350,7 @@ runner.use(async (context, next) => {
|
|
|
348
350
|
}
|
|
349
351
|
}
|
|
350
352
|
};
|
|
351
|
-
await statedb.rollup.update(entry.address, _ocap_state.rollup.updateBridge(entry.state,
|
|
353
|
+
await statedb.rollup.update(entry.address, _ocap_state.rollup.updateBridge(entry.state, updated, context), context);
|
|
352
354
|
}
|
|
353
355
|
const reserveTokens = { ...reserveState.tokens || {} };
|
|
354
356
|
reserveTokens[releaseToken] = new _ocap_util.BN(reserveTokens[releaseToken] || "0").sub(amount).toString(10);
|
|
@@ -379,12 +381,24 @@ runner.use(async (context, next) => {
|
|
|
379
381
|
context.bridgeToken = releaseToken;
|
|
380
382
|
if (updateVaults) await updateVaults();
|
|
381
383
|
next();
|
|
382
|
-
}
|
|
383
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyStateDiff());
|
|
384
|
-
var release_default = runner;
|
|
384
|
+
});
|
|
385
385
|
|
|
386
386
|
//#endregion
|
|
387
387
|
exports.MAX_MIGRATION_HOPS = MAX_MIGRATION_HOPS;
|
|
388
388
|
exports.RELEASE_KIND = RELEASE_KIND;
|
|
389
|
-
exports.
|
|
390
|
-
exports.
|
|
389
|
+
exports.checkReleaseCap = checkReleaseCap;
|
|
390
|
+
exports.checkReleaseFields = checkReleaseFields;
|
|
391
|
+
exports.checkReleaseMinimum = checkReleaseMinimum;
|
|
392
|
+
exports.checkReleaseRecipient = checkReleaseRecipient;
|
|
393
|
+
exports.checkReleaseRecipientState = checkReleaseRecipientState;
|
|
394
|
+
exports.checkReleaseRefund = checkReleaseRefund;
|
|
395
|
+
exports.checkReleaseReserve = checkReleaseReserve;
|
|
396
|
+
exports.checkReleaseTombstone = checkReleaseTombstone;
|
|
397
|
+
exports.rejectReleaseDelegator = rejectReleaseDelegator;
|
|
398
|
+
exports.releaseGasEstimate = releaseGasEstimate;
|
|
399
|
+
exports.resolveMigrated = resolveMigrated;
|
|
400
|
+
exports.resolveReleaseBridge = resolveReleaseBridge;
|
|
401
|
+
exports.resolveReleaseTime = resolveReleaseTime;
|
|
402
|
+
exports.verifyReleaseClaim = verifyReleaseClaim;
|
|
403
|
+
exports.verifyReleaseSignatures = verifyReleaseSignatures;
|
|
404
|
+
exports.writeRelease = writeRelease;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PipeFunction, ReleaseResolution } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/protocols/bridge/release-checks.d.ts
|
|
4
|
+
declare const RELEASE_KIND: {
|
|
5
|
+
readonly DEPOSIT: 1;
|
|
6
|
+
readonly REFUND: 2;
|
|
7
|
+
readonly DEPOSIT_B: 3;
|
|
8
|
+
};
|
|
9
|
+
/** `resolveMigrated` follows at most this many `migratedTo` links of an ACCOUNT (check 6) */
|
|
10
|
+
declare const MAX_MIGRATION_HOPS = 8;
|
|
11
|
+
/** What a release says. */
|
|
12
|
+
interface ReleaseView {
|
|
13
|
+
kind: number;
|
|
14
|
+
/** the canonical bridge address: `itx.rollup` */
|
|
15
|
+
bridge: string;
|
|
16
|
+
/** lowercase `0x` bytes32: `itx.evidence.hash` */
|
|
17
|
+
evidence: string;
|
|
18
|
+
ordinal: number;
|
|
19
|
+
to: string;
|
|
20
|
+
/** base units, canonical decimal: `itx.token.value` */
|
|
21
|
+
amount: string;
|
|
22
|
+
epoch: number;
|
|
23
|
+
ethSender: string;
|
|
24
|
+
claimSignature: string;
|
|
25
|
+
/** `itx.validatorSignaturesList` */
|
|
26
|
+
signaturesList: string[];
|
|
27
|
+
}
|
|
28
|
+
type ReleaseContext = Record<string, any> & {
|
|
29
|
+
releaseItx: ReleaseView;
|
|
30
|
+
/** what `resolveBridgeForRelease` found: the bridge, its reserve and the reserves a refund may draw on */
|
|
31
|
+
release?: ReleaseResolution;
|
|
32
|
+
releaseAmount?: bigint;
|
|
33
|
+
releaseTimeMs?: number;
|
|
34
|
+
};
|
|
35
|
+
type Pipe = PipeFunction<any>;
|
|
36
|
+
declare const checkReleaseFields: Pipe;
|
|
37
|
+
declare const rejectReleaseDelegator: Pipe;
|
|
38
|
+
declare const resolveReleaseTime: Pipe;
|
|
39
|
+
/**
|
|
40
|
+
* 1. master switch, the bridge the release names, its lifecycle for this kind.
|
|
41
|
+
* `opts.token`: the release names the token (`itx.token.address`), which must be the bridge token (fail closed).
|
|
42
|
+
*/
|
|
43
|
+
declare const resolveReleaseBridge: (opts: {
|
|
44
|
+
token?: (context: ReleaseContext) => unknown;
|
|
45
|
+
}) => Pipe;
|
|
46
|
+
declare const checkReleaseMinimum: Pipe;
|
|
47
|
+
declare const checkReleaseRecipient: Pipe;
|
|
48
|
+
declare const checkReleaseTombstone: Pipe;
|
|
49
|
+
declare const verifyReleaseSignatures: Pipe;
|
|
50
|
+
declare const verifyReleaseClaim: Pipe;
|
|
51
|
+
declare function resolveMigrated(context: ReleaseContext, address: string): Promise<string | null>;
|
|
52
|
+
declare const checkReleaseRefund: Pipe;
|
|
53
|
+
declare const checkReleaseRecipientState: Pipe;
|
|
54
|
+
declare const checkReleaseCap: Pipe;
|
|
55
|
+
declare const checkReleaseReserve: Pipe;
|
|
56
|
+
/** Gas operations of a release (for `EnsureTxGas`). */
|
|
57
|
+
declare const releaseGasEstimate: (context: ReleaseContext) => {
|
|
58
|
+
create: number;
|
|
59
|
+
update: any;
|
|
60
|
+
payment: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* The state writes of a release: the tombstone (create only: a second release of the same evidence was refused above,
|
|
64
|
+
* and `create` fails on an existing key), the one bucket it uses, the refund ledger of the migrated bridges a REFUND
|
|
65
|
+
* draws through, the reserve debit, the recipient credit and the sender (nonce, gas).
|
|
66
|
+
*/
|
|
67
|
+
declare const writeRelease: Pipe;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { MAX_MIGRATION_HOPS, RELEASE_KIND, ReleaseContext, ReleaseView, checkReleaseCap, checkReleaseFields, checkReleaseMinimum, checkReleaseRecipient, checkReleaseRecipientState, checkReleaseRefund, checkReleaseReserve, checkReleaseTombstone, rejectReleaseDelegator, releaseGasEstimate, resolveMigrated, resolveReleaseBridge, resolveReleaseTime, verifyReleaseClaim, verifyReleaseSignatures, writeRelease };
|
|
@@ -63,7 +63,7 @@ async function verifyBridgeCreate(context, next) {
|
|
|
63
63
|
]);
|
|
64
64
|
if (require_protocols_rollup_bridge_v2.indexBlocks(gatewayEntry, txSec, "bridge-gateway")) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "A bridge that is not final already uses this ethereum gateway"));
|
|
65
65
|
if (require_protocols_rollup_bridge_v2.indexBlocks(tokenEntry, txSec, "bridge-token")) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "Only 1 bridge that is not final can exist for the token"));
|
|
66
|
-
if (
|
|
66
|
+
if (reserveEntry && !(0, _ocap_tx_pipeline.isV1EvidenceRow)(reserveEntry) || reserveAccount) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "The reserve of this bridge already exists"));
|
|
67
67
|
const missing = epoch1.validators.find((_, i) => !validatorAccounts[i]);
|
|
68
68
|
if (missing) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Initial validator ${missing} has no account on this chain`));
|
|
69
69
|
let attestationSigner;
|
|
@@ -5,6 +5,7 @@ let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
|
5
5
|
let _arcblock_did = require("@arcblock/did");
|
|
6
6
|
let _ocap_state = require("@ocap/state");
|
|
7
7
|
let _arcblock_validator = require("@arcblock/validator");
|
|
8
|
+
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
8
9
|
|
|
9
10
|
//#region src/protocols/rollup/bridge-membership.ts
|
|
10
11
|
/**
|
|
@@ -87,7 +88,8 @@ const membershipChecks = (action) => async function MembershipChecks(context, ne
|
|
|
87
88
|
});
|
|
88
89
|
const digestHex = `0x${Buffer.from(digest).toString("hex")}`;
|
|
89
90
|
const tombstoneKey = require_protocols_rollup_bridge_v2.membershipTombstoneKey(state.address, plan.epoch.epoch, digestHex);
|
|
90
|
-
|
|
91
|
+
const tombstoneRow = await statedb.evidence.get(tombstoneKey, context);
|
|
92
|
+
if (tombstoneRow && !(0, _ocap_tx_pipeline.isV1EvidenceRow)(tombstoneRow)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "This membership approval was already used on this chain");
|
|
91
93
|
const recovered = require_protocols_rollup_bridge_v2.recoverAll(digest, data.signatures);
|
|
92
94
|
const signers = new Map(recovered.map((r) => [r.address, r]));
|
|
93
95
|
const owner = _ocap_state.rollup.lc(body.owner);
|
|
@@ -134,16 +136,13 @@ const membershipStep = (action) => async function MembershipStep(context, next)
|
|
|
134
136
|
...senderUpdates
|
|
135
137
|
}, context), context),
|
|
136
138
|
statedb.rollup.update(context.rollupStateOrig.address, _ocap_state.rollup.updateBridge(context.rollupStateOrig, require_protocols_rollup_bridge_v2.bridgeUpdates(context, nextBody), context), context),
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
kind: `bridge-${action}`,
|
|
143
|
-
epoch: plan.epoch.epoch
|
|
144
|
-
}
|
|
139
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, tombstoneKey, {
|
|
140
|
+
type: "json",
|
|
141
|
+
value: {
|
|
142
|
+
kind: `bridge-${action}`,
|
|
143
|
+
epoch: plan.epoch.epoch
|
|
145
144
|
}
|
|
146
|
-
}, context)
|
|
145
|
+
}, context)
|
|
147
146
|
]);
|
|
148
147
|
if (updateVaults) await updateVaults();
|
|
149
148
|
context.senderState = newSenderState;
|
|
@@ -34,6 +34,30 @@ const peekBridge = (rollupKey, opts) => async function PeekBridge(context, next)
|
|
|
34
34
|
} catch {}
|
|
35
35
|
return next();
|
|
36
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* Branch selection of `deposit_token_v2` / `withdraw_token_v2` (#1691, #1692, bridge/docs/deposit-withdraw-v2-unification.md
|
|
39
|
+
* §2.3): by the version of the row `itx.rollup` names, read BEFORE the v1 itx schema. A v2 bridge takes the bridge branch;
|
|
40
|
+
* anything else (a v1 rollup, no row, an address that is not even a DID) takes the v1 branch, whose pipes run unchanged and
|
|
41
|
+
* in the original order. Unlike the governance `peekBridge`, a missing statedb or a failed read is an error and never a
|
|
42
|
+
* silent fallback to v1, which would judge a bridge tx by v1 rules (a statedb-less run would otherwise refuse every bridge
|
|
43
|
+
* deposit through the v1 checks: a permanent DoS).
|
|
44
|
+
*/
|
|
45
|
+
const peekBridgeFailClosed = (txName, pipeName) => Object.defineProperty(async (context, next) => {
|
|
46
|
+
context.isBridgeV2 = false;
|
|
47
|
+
const address = context.itx?.rollup;
|
|
48
|
+
if (typeof address !== "string" || !address || !(0, _arcblock_did.isValid)(address.replace(/^did:abt:/, ""))) return next();
|
|
49
|
+
let row;
|
|
50
|
+
try {
|
|
51
|
+
row = await context.statedb.rollup.get(address, context);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return next(new _ocap_util_lib_error.CustomError("INTERNAL", `${txName} can not read rollup ${address}: ${String(err?.message || err)}`));
|
|
54
|
+
}
|
|
55
|
+
context.isBridgeV2 = !!row && _ocap_state.rollup.isBridgeV2(row);
|
|
56
|
+
return next();
|
|
57
|
+
}, "name", {
|
|
58
|
+
value: pipeName,
|
|
59
|
+
configurable: true
|
|
60
|
+
});
|
|
37
61
|
/** Rejects a non-v2 rollup governance action after `loadBridge` when `transaction.rollupV1.enabled === false`. */
|
|
38
62
|
const rejectRollupV1IfDisabled = (txName) => function RejectRollupV1IfDisabled(context, next) {
|
|
39
63
|
if (!context.isBridgeV2 && !isRollupV1Enabled(context.config)) return next(new _ocap_util_lib_error.CustomError("UNSUPPORTED_TX", `${txName} is disabled because Rollup v1 is sunset`));
|
|
@@ -144,13 +168,14 @@ const isBridgeIndex = (entry, kind) => {
|
|
|
144
168
|
return entry?.data?.type === "json" && !!value && typeof value === "object" && value.kind === kind && typeof value.bridge === "string" && value.bridge.length > 0;
|
|
145
169
|
};
|
|
146
170
|
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
171
|
+
* A row a legacy v1 tx planted at the key (`isV1EvidenceRow`: a v1 join / leave / deposit / claim record) is not an
|
|
172
|
+
* index entry, blocks nobody and is overwritten by the next `create_rollup` (index squatting, #1505 L2, #1680). With
|
|
173
|
+
* `kind`, any other row that is not a genuine index entry of that kind blocks (fail closed, #1680): it is never
|
|
174
|
+
* overwritten.
|
|
150
175
|
*/
|
|
151
176
|
const indexBlocks = (entry, txSec, kind) => {
|
|
152
|
-
if (!entry) return false;
|
|
153
|
-
if (kind && !isBridgeIndex(entry, kind)) return
|
|
177
|
+
if (!entry || (0, _ocap_tx_pipeline.isV1EvidenceRow)(entry)) return false;
|
|
178
|
+
if (kind && !isBridgeIndex(entry, kind)) return true;
|
|
154
179
|
const finalAt = entry.data?.value?.finalAt;
|
|
155
180
|
return !(typeof finalAt === "number" && finalAt <= txSec);
|
|
156
181
|
};
|
|
@@ -229,10 +254,12 @@ exports.indexData = indexData;
|
|
|
229
254
|
exports.isBridgeIndex = isBridgeIndex;
|
|
230
255
|
exports.isEthereumDid = _arcblock_did.isEthereumDid;
|
|
231
256
|
exports.isRollupV1Enabled = isRollupV1Enabled;
|
|
257
|
+
exports.isV1EvidenceRow = _ocap_tx_pipeline.isV1EvidenceRow;
|
|
232
258
|
exports.loadBridge = loadBridge;
|
|
233
259
|
exports.membershipDigest = membershipDigest;
|
|
234
260
|
exports.membershipTombstoneKey = membershipTombstoneKey;
|
|
235
261
|
exports.peekBridge = peekBridge;
|
|
262
|
+
exports.peekBridgeFailClosed = peekBridgeFailClosed;
|
|
236
263
|
exports.readItxData = readItxData;
|
|
237
264
|
exports.recoverAll = recoverAll;
|
|
238
265
|
exports.rejectBridgeV2 = rejectBridgeV2;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { rollup } from "@ocap/state";
|
|
2
|
-
import { PipeFunction, reserveIndexKey } from "@ocap/tx-pipeline";
|
|
2
|
+
import { PipeFunction, isV1EvidenceRow, reserveIndexKey } from "@ocap/tx-pipeline";
|
|
3
3
|
import { isEthereumDid } from "@arcblock/did";
|
|
4
4
|
import { BRIDGE_MEMBERSHIP_ACTION, toHexDigest } from "@ocap/mcrypto";
|
|
5
5
|
|
|
@@ -17,6 +17,15 @@ declare const isRollupV1Enabled: (config: AnyObj | undefined) => boolean;
|
|
|
17
17
|
declare const peekBridge: (rollupKey: "itx.rollup", opts?: {
|
|
18
18
|
rejectV1IfDisabled?: string;
|
|
19
19
|
}) => PipeFunction<Ctx>;
|
|
20
|
+
/**
|
|
21
|
+
* Branch selection of `deposit_token_v2` / `withdraw_token_v2` (#1691, #1692, bridge/docs/deposit-withdraw-v2-unification.md
|
|
22
|
+
* §2.3): by the version of the row `itx.rollup` names, read BEFORE the v1 itx schema. A v2 bridge takes the bridge branch;
|
|
23
|
+
* anything else (a v1 rollup, no row, an address that is not even a DID) takes the v1 branch, whose pipes run unchanged and
|
|
24
|
+
* in the original order. Unlike the governance `peekBridge`, a missing statedb or a failed read is an error and never a
|
|
25
|
+
* silent fallback to v1, which would judge a bridge tx by v1 rules (a statedb-less run would otherwise refuse every bridge
|
|
26
|
+
* deposit through the v1 checks: a permanent DoS).
|
|
27
|
+
*/
|
|
28
|
+
declare const peekBridgeFailClosed: (txName: string, pipeName: string) => PipeFunction<Ctx>;
|
|
20
29
|
/** Rejects a non-v2 rollup governance action after `loadBridge` when `transaction.rollupV1.enabled === false`. */
|
|
21
30
|
declare const rejectRollupV1IfDisabled: (txName: string) => PipeFunction<Ctx>;
|
|
22
31
|
/** Run `pipe` for v1 rollups only (v2 bridges have their own pipes). */
|
|
@@ -63,9 +72,10 @@ declare const membershipTombstoneKey: (bridge: string, epoch: number, digestHex:
|
|
|
63
72
|
*/
|
|
64
73
|
declare const isBridgeIndex: (entry: AnyObj | null | undefined, kind: string) => boolean;
|
|
65
74
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
75
|
+
* A row a legacy v1 tx planted at the key (`isV1EvidenceRow`: a v1 join / leave / deposit / claim record) is not an
|
|
76
|
+
* index entry, blocks nobody and is overwritten by the next `create_rollup` (index squatting, #1505 L2, #1680). With
|
|
77
|
+
* `kind`, any other row that is not a genuine index entry of that kind blocks (fail closed, #1680): it is never
|
|
78
|
+
* overwritten.
|
|
69
79
|
*/
|
|
70
80
|
declare const indexBlocks: (entry: AnyObj | null | undefined, txSec: number, kind?: string) => boolean;
|
|
71
81
|
declare const indexData: (kind: string, bridge: string, finalAt: number | null) => {
|
|
@@ -110,4 +120,4 @@ type BridgeCtxMixin = {
|
|
|
110
120
|
[key: string]: any;
|
|
111
121
|
};
|
|
112
122
|
//#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 };
|
|
123
|
+
export { AuthorityRule, BRIDGE_MEMBERSHIP_ACTION, BridgeCtxMixin, MembershipCheck, RecoveredSignature, VerifyBridgeAuthority, bridgeUpdates, createIndexEvidence, gatewayIndexKey, ifV1, ifV2, indexBlocks, indexData, isBridgeIndex, isEthereumDid, isRollupV1Enabled, isV1EvidenceRow, loadBridge, membershipDigest, membershipTombstoneKey, peekBridge, peekBridgeFailClosed, readItxData, recoverAll, rejectBridgeV2, rejectRollupV1IfDisabled, reserveIndexKey, toHexDigest, tokenIndexKey, txSecOf, upsertIndexEvidence };
|