@ocap/tx-protocols 1.31.1 → 1.31.3
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
|
@@ -3,14 +3,15 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
+
const require_stateless_prefix = require('../../stateless-prefix.cjs');
|
|
6
7
|
const require_protocols_governance_revoke_ledger = require('./revoke-ledger.cjs');
|
|
7
|
-
let _arcblock_did = require("@arcblock/did");
|
|
8
8
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
9
|
-
let _ocap_state = require("@ocap/state");
|
|
10
9
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
11
|
-
let _ocap_util = require("@ocap/util");
|
|
12
10
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
13
11
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
12
|
+
let _arcblock_did = require("@arcblock/did");
|
|
13
|
+
let _ocap_state = require("@ocap/state");
|
|
14
|
+
let _ocap_util = require("@ocap/util");
|
|
14
15
|
let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
|
|
15
16
|
let debug = require("debug");
|
|
16
17
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
@@ -58,6 +59,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyListSize({ listKey: [
|
|
|
58
59
|
"tokens",
|
|
59
60
|
"assets"
|
|
60
61
|
] }));
|
|
62
|
+
require_stateless_prefix.markStatelessPrefix(runner);
|
|
61
63
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
62
64
|
from: "tx.from",
|
|
63
65
|
to: "senderState",
|
|
@@ -3,14 +3,15 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
-
|
|
7
|
-
let _arcblock_did_util = require("@arcblock/did-util");
|
|
6
|
+
const require_stateless_prefix = require('../../stateless-prefix.cjs');
|
|
8
7
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
9
|
-
let _ocap_state = require("@ocap/state");
|
|
10
8
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
11
|
-
let _ocap_util = require("@ocap/util");
|
|
12
9
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
13
10
|
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
11
|
+
let _arcblock_did = require("@arcblock/did");
|
|
12
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
13
|
+
let _ocap_state = require("@ocap/state");
|
|
14
|
+
let _ocap_util = require("@ocap/util");
|
|
14
15
|
let lodash_pick = require("lodash/pick");
|
|
15
16
|
lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
|
|
16
17
|
let debug = require("debug");
|
|
@@ -99,6 +100,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyListSize({ listKey: [
|
|
|
99
100
|
"tokens",
|
|
100
101
|
"assets"
|
|
101
102
|
] }));
|
|
103
|
+
require_stateless_prefix.markStatelessPrefix(runner);
|
|
102
104
|
runner.use(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "senders" }));
|
|
103
105
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
104
106
|
from: "itx.address",
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
|
+
let _arcblock_did = require("@arcblock/did");
|
|
6
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
7
|
+
let _ocap_state = require("@ocap/state");
|
|
8
|
+
let _ocap_util = require("@ocap/util");
|
|
9
|
+
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
10
|
+
|
|
11
|
+
//#region src/protocols/rollup/bridge-create.ts
|
|
12
|
+
/**
|
|
13
|
+
* `create_rollup` for a v2 bridge (bridge/docs/ocap-protocols.md §v2.2, row `create_rollup`).
|
|
14
|
+
*/
|
|
15
|
+
const isHttpUrl = (s) => {
|
|
16
|
+
try {
|
|
17
|
+
return typeof s === "string" && ["http:", "https:"].includes(new URL(s).protocol);
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Everything the v1 pipes do not cover for a v2 create. Runs after the token-authority check (#1411) and the
|
|
24
|
+
* one-bridge-per-token check, so it only sees a creator that already controls the token.
|
|
25
|
+
*/
|
|
26
|
+
async function verifyBridgeCreate(context, next) {
|
|
27
|
+
if (!context.isBridgeV2) return next();
|
|
28
|
+
const { tx, itx, config, tokenState, statedb } = context;
|
|
29
|
+
const input = context.bridgeInput;
|
|
30
|
+
const txSec = require_protocols_rollup_bridge_v2.txSecOf(context);
|
|
31
|
+
const authority = !!config?.token?.address && (0, _ocap_util.isSameDid)(itx.tokenAddress, config.token.address) ? config?.moderator?.address : tokenState?.issuer;
|
|
32
|
+
const owner = [tx.from, tx.delegator].filter(Boolean).find((x) => (0, _ocap_util.isSameDid)(x, authority));
|
|
33
|
+
if (!owner) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", "Only the token owner can create a bridge for the token"));
|
|
34
|
+
if (!(0, _arcblock_did.isEthereumDid)(owner)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "The bridge owner must use an ethereum compatible address"));
|
|
35
|
+
const seeds = context.seedValidators;
|
|
36
|
+
if (!seeds || seeds.length !== 1 || !_ocap_state.rollup.sameAddress(seeds[0], owner)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "seed_validators of a v2 bridge must be the bridge owner alone"));
|
|
37
|
+
if (!_ocap_state.rollup.isValidRollupContractAddress(itx.contractAddress, "EVM")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "contractAddress must be an ethereum token address"));
|
|
38
|
+
if (itx.vaultAddress && !_ocap_state.rollup.isValidRollupVaultAddress(itx.vaultAddress, "EVM")) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "vaultAddress must be an ethereum address"));
|
|
39
|
+
if (!(itx.minBlockConfirmation >= 1 && itx.minBlockConfirmation <= 100)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "minBlockConfirmation must be between 1 and 100"));
|
|
40
|
+
const ethToken = String(itx.contractAddress).toLowerCase();
|
|
41
|
+
const { body } = input;
|
|
42
|
+
const epoch1 = body.epochs[0];
|
|
43
|
+
const consents = input.consents;
|
|
44
|
+
if (consents.length !== epoch1.validators.length) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "Every initial validator must sign a BridgeMembership consent"));
|
|
45
|
+
const endpointOf = {};
|
|
46
|
+
for (const c of consents) {
|
|
47
|
+
const addr = String(c?.address || "").toLowerCase();
|
|
48
|
+
if (!epoch1.validators.includes(addr)) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Consent signer ${addr} is not an initial validator`));
|
|
49
|
+
if (endpointOf[addr] !== void 0) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Duplicate consent of ${addr}`));
|
|
50
|
+
if (!isHttpUrl(c.endpoint)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Consent of ${addr} needs an http(s) endpoint`));
|
|
51
|
+
endpointOf[addr] = c.endpoint;
|
|
52
|
+
}
|
|
53
|
+
const gatewayKey = require_protocols_rollup_bridge_v2.gatewayIndexKey(body.ethChainId, body.gateway);
|
|
54
|
+
const tokenKey = require_protocols_rollup_bridge_v2.tokenIndexKey(itx.tokenAddress);
|
|
55
|
+
const reserve = (0, _arcblock_did_util.toBridgeReserveAddress)(itx.address);
|
|
56
|
+
const reserveKey = (0, _ocap_tx_pipeline.reserveIndexKey)(reserve);
|
|
57
|
+
const [gatewayEntry, tokenEntry, reserveEntry, reserveAccount, validatorAccounts] = await Promise.all([
|
|
58
|
+
statedb.evidence.get(gatewayKey, context),
|
|
59
|
+
statedb.evidence.get(tokenKey, context),
|
|
60
|
+
statedb.evidence.get(reserveKey, context),
|
|
61
|
+
statedb.account.get(reserve, context),
|
|
62
|
+
Promise.all(epoch1.validators.map((a) => statedb.account.get(_ocap_state.account.ensureChecksumAddress(a), context)))
|
|
63
|
+
]);
|
|
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
|
+
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 (require_protocols_rollup_bridge_v2.isBridgeIndex(reserveEntry, "bridge-reserve") || reserveAccount) return next(new _ocap_util_lib_error.CustomError("DUPLICATE_ROLLUP", "The reserve of this bridge already exists"));
|
|
67
|
+
const missing = epoch1.validators.find((_, i) => !validatorAccounts[i]);
|
|
68
|
+
if (missing) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Initial validator ${missing} has no account on this chain`));
|
|
69
|
+
let attestationSigner;
|
|
70
|
+
try {
|
|
71
|
+
attestationSigner = (0, _ocap_mcrypto.recoverBridgeSigner)((0, _ocap_mcrypto.computeBridgeOwnerAttestationDigest)({
|
|
72
|
+
ocapChainId: config.chainId,
|
|
73
|
+
salt: body.salt,
|
|
74
|
+
ocapToken: itx.tokenAddress,
|
|
75
|
+
ethChainId: body.ethChainId,
|
|
76
|
+
gateway: body.gateway,
|
|
77
|
+
ethToken
|
|
78
|
+
}), body.ethOwnerAttestation.signature)?.address;
|
|
79
|
+
} catch (err) {
|
|
80
|
+
return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Invalid ethOwnerAttestation: ${err.message}`));
|
|
81
|
+
}
|
|
82
|
+
if (!attestationSigner || attestationSigner !== body.ethOwnerAttestation.address) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "ethOwnerAttestation signature does not match its address"));
|
|
83
|
+
const members = {};
|
|
84
|
+
try {
|
|
85
|
+
const recovered = require_protocols_rollup_bridge_v2.recoverAll(require_protocols_rollup_bridge_v2.membershipDigest({
|
|
86
|
+
ocapChainId: config.chainId,
|
|
87
|
+
bridge: itx.address,
|
|
88
|
+
epoch: epoch1,
|
|
89
|
+
action: _ocap_mcrypto.BRIDGE_MEMBERSHIP_ACTION.CREATE,
|
|
90
|
+
endpoints: epoch1.validators.map((a) => endpointOf[a])
|
|
91
|
+
}), consents.map((c) => c?.signature));
|
|
92
|
+
consents.forEach((c, i) => {
|
|
93
|
+
const addr = String(c.address).toLowerCase();
|
|
94
|
+
if (addr !== recovered[i].address) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Consent of ${c.address} was not signed by that address`);
|
|
95
|
+
members[addr] = {
|
|
96
|
+
address: _ocap_state.account.ensureChecksumAddress(c.address),
|
|
97
|
+
pk: recovered[i].pk,
|
|
98
|
+
endpoint: c.endpoint
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return next(err);
|
|
103
|
+
}
|
|
104
|
+
context.bridge = {
|
|
105
|
+
owner,
|
|
106
|
+
reserve,
|
|
107
|
+
members,
|
|
108
|
+
ethToken,
|
|
109
|
+
keys: {
|
|
110
|
+
gatewayKey,
|
|
111
|
+
tokenKey,
|
|
112
|
+
reserveKey
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
return next();
|
|
116
|
+
}
|
|
117
|
+
/** The state writes of a v2 create: row, reserve account (funded zero-sum from tx.from), and the three index entries. */
|
|
118
|
+
async function createBridgeStep(context, next) {
|
|
119
|
+
const { tx, itx, formattedItx, statedb, senderState, delegationState, senderUpdates, updateVaults, bridge, bridgeInput } = context;
|
|
120
|
+
const input = bridgeInput;
|
|
121
|
+
const txSec = require_protocols_rollup_bridge_v2.txSecOf(context);
|
|
122
|
+
const tokenAddress = itx.tokenAddress;
|
|
123
|
+
const amount = new _ocap_util.BN(input.initialReserve);
|
|
124
|
+
const baseTokens = senderUpdates?.tokens ?? senderState.tokens ?? {};
|
|
125
|
+
const have = new _ocap_util.BN(baseTokens[tokenAddress] || 0);
|
|
126
|
+
if (have.lt(amount)) return next(new _ocap_util_lib_error.CustomError("INSUFFICIENT_FUND", "Insufficient fund in tx.from to pay the initial reserve"));
|
|
127
|
+
const senderTokens = amount.gtn(0) ? {
|
|
128
|
+
...baseTokens,
|
|
129
|
+
[tokenAddress]: have.sub(amount).toString(10)
|
|
130
|
+
} : baseTokens;
|
|
131
|
+
const body = {
|
|
132
|
+
...input.body,
|
|
133
|
+
owner: bridge.owner,
|
|
134
|
+
reserve: bridge.reserve,
|
|
135
|
+
members: bridge.members,
|
|
136
|
+
pausers: input.body.pausers.length ? input.body.pausers : [bridge.owner],
|
|
137
|
+
lonePause: null,
|
|
138
|
+
lastLonePause: {},
|
|
139
|
+
pendingOwner: null,
|
|
140
|
+
pendingMigration: null,
|
|
141
|
+
closingAt: null,
|
|
142
|
+
depositCutoffEthBlock: null,
|
|
143
|
+
finalAt: null,
|
|
144
|
+
migratedTo: null,
|
|
145
|
+
migratedAmount: null
|
|
146
|
+
};
|
|
147
|
+
const epoch1 = body.epochs[0];
|
|
148
|
+
const attrs = _ocap_state.rollup.syncBridgeView({
|
|
149
|
+
address: formattedItx.address,
|
|
150
|
+
tokenAddress,
|
|
151
|
+
vaultAddress: formattedItx.vaultAddress,
|
|
152
|
+
contractAddress: formattedItx.contractAddress,
|
|
153
|
+
seedValidators: formattedItx.seedValidators,
|
|
154
|
+
validators: [],
|
|
155
|
+
minSignerCount: epoch1.threshold,
|
|
156
|
+
maxSignerCount: epoch1.validators.length,
|
|
157
|
+
minBlockConfirmation: formattedItx.minBlockConfirmation,
|
|
158
|
+
issuer: bridge.owner,
|
|
159
|
+
data: {
|
|
160
|
+
type: "json",
|
|
161
|
+
value: body
|
|
162
|
+
}
|
|
163
|
+
}, txSec);
|
|
164
|
+
const rollupState = _ocap_state.rollup.create(attrs, context);
|
|
165
|
+
const reserveState = _ocap_state.account.create({
|
|
166
|
+
address: bridge.reserve,
|
|
167
|
+
tokens: amount.gtn(0) ? { [tokenAddress]: amount.toString(10) } : {}
|
|
168
|
+
}, context);
|
|
169
|
+
const bridgeAddress = rollupState.address;
|
|
170
|
+
const [newSenderState, newRollupState, newDelegationState, newReserveState] = await Promise.all([
|
|
171
|
+
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
172
|
+
nonce: tx.nonce,
|
|
173
|
+
...senderUpdates,
|
|
174
|
+
tokens: senderTokens
|
|
175
|
+
}, context), context),
|
|
176
|
+
statedb.rollup.create(bridgeAddress, rollupState, context),
|
|
177
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, _ocap_state.delegation.update(delegationState, {}, context), context) : Promise.resolve(delegationState),
|
|
178
|
+
statedb.account.create(bridge.reserve, reserveState, context)
|
|
179
|
+
]);
|
|
180
|
+
await Promise.all([
|
|
181
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, bridge.keys.gatewayKey, require_protocols_rollup_bridge_v2.indexData("bridge-gateway", bridgeAddress, null), context),
|
|
182
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, bridge.keys.tokenKey, require_protocols_rollup_bridge_v2.indexData("bridge-token", bridgeAddress, null), context),
|
|
183
|
+
require_protocols_rollup_bridge_v2.upsertIndexEvidence(statedb, bridge.keys.reserveKey, require_protocols_rollup_bridge_v2.indexData("bridge-reserve", bridgeAddress, null), context)
|
|
184
|
+
]);
|
|
185
|
+
if (updateVaults) await updateVaults();
|
|
186
|
+
if (amount.gtn(0)) {
|
|
187
|
+
context.updatedAccounts = context.updatedAccounts || [];
|
|
188
|
+
context.updatedAccounts.push({
|
|
189
|
+
address: senderState.address,
|
|
190
|
+
token: tokenAddress,
|
|
191
|
+
delta: `-${amount.toString(10)}`,
|
|
192
|
+
action: "transfer"
|
|
193
|
+
}, {
|
|
194
|
+
address: bridge.reserve,
|
|
195
|
+
token: tokenAddress,
|
|
196
|
+
delta: amount.toString(10),
|
|
197
|
+
action: "transfer"
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
context.senderState = newSenderState;
|
|
201
|
+
context.rollupState = newRollupState;
|
|
202
|
+
context.delegationState = newDelegationState;
|
|
203
|
+
context.reserveState = newReserveState;
|
|
204
|
+
return next();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
exports.createBridgeStep = createBridgeStep;
|
|
209
|
+
exports.verifyBridgeCreate = verifyBridgeCreate;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PipeNext } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/protocols/rollup/bridge-create.d.ts
|
|
4
|
+
type Ctx = any;
|
|
5
|
+
/**
|
|
6
|
+
* Everything the v1 pipes do not cover for a v2 create. Runs after the token-authority check (#1411) and the
|
|
7
|
+
* one-bridge-per-token check, so it only sees a creator that already controls the token.
|
|
8
|
+
*/
|
|
9
|
+
declare function verifyBridgeCreate(context: Ctx, next: PipeNext): Promise<void>;
|
|
10
|
+
/** The state writes of a v2 create: row, reserve account (funded zero-sum from tx.from), and the three index entries. */
|
|
11
|
+
declare function createBridgeStep(context: Ctx, next: PipeNext): Promise<void>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createBridgeStep, verifyBridgeCreate };
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
5
|
+
let _arcblock_did = require("@arcblock/did");
|
|
6
|
+
let _ocap_state = require("@ocap/state");
|
|
7
|
+
let _arcblock_validator = require("@arcblock/validator");
|
|
8
|
+
|
|
9
|
+
//#region src/protocols/rollup/bridge-membership.ts
|
|
10
|
+
/**
|
|
11
|
+
* `join_rollup` / `leave_rollup` for a v2 bridge (bridge/docs/ocap-protocols.md §v2.2, row `join_rollup / leave_rollup`).
|
|
12
|
+
*
|
|
13
|
+
* A membership change creates a NEW EPOCH. It is authorised by EIP-712 `BridgeMembership` signatures of the owner and a
|
|
14
|
+
* quorum of the epoch active at txTime (and, for a join, of the joiner itself), carried in `itx.data.signatures`. The
|
|
15
|
+
* transaction sender is irrelevant: anyone may submit the approved change. There is no stake and no global
|
|
16
|
+
* `validatorsHash` evidence, so one operator can serve several bridges and leave and rejoin.
|
|
17
|
+
*/
|
|
18
|
+
const isHttpUrl = (s) => {
|
|
19
|
+
try {
|
|
20
|
+
return typeof s === "string" && ["http:", "https:"].includes(new URL(s).protocol);
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const itxSchemaV2 = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
26
|
+
rollup: (0, _arcblock_validator.vDID)({
|
|
27
|
+
prefix: true,
|
|
28
|
+
role: "ROLE_ROLLUP"
|
|
29
|
+
}),
|
|
30
|
+
endpoint: _arcblock_validator.v.optional(_arcblock_validator.v.string()),
|
|
31
|
+
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
32
|
+
}), (0, _arcblock_validator.vStripUnknown)());
|
|
33
|
+
const membershipSchemaV2 = (action) => function MembershipSchemaV2(context, next) {
|
|
34
|
+
if (!context.isBridgeV2) return next();
|
|
35
|
+
const { error } = (0, _arcblock_validator.vValidate)(itxSchemaV2, context.itx);
|
|
36
|
+
if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
37
|
+
if (action === "join" && !isHttpUrl(context.itx.endpoint)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: endpoint must be a valid HTTP/HTTPS URL"));
|
|
38
|
+
return next();
|
|
39
|
+
};
|
|
40
|
+
function parseMembershipData(itx) {
|
|
41
|
+
const d = require_protocols_rollup_bridge_v2.readItxData(itx);
|
|
42
|
+
for (const k of Object.keys(d)) if (![
|
|
43
|
+
"validator",
|
|
44
|
+
"threshold",
|
|
45
|
+
"activeFrom",
|
|
46
|
+
"voucherValidity",
|
|
47
|
+
"signatures"
|
|
48
|
+
].includes(k)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", `Unknown key in membership data: ${k}`);
|
|
49
|
+
if (typeof d.validator !== "string" || !(0, _arcblock_did.isEthereumDid)(d.validator)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "data.validator must be an ethereum address");
|
|
50
|
+
for (const k of [
|
|
51
|
+
"threshold",
|
|
52
|
+
"activeFrom",
|
|
53
|
+
"voucherValidity"
|
|
54
|
+
]) if (!Number.isSafeInteger(d[k]) || d[k] < 0) throw new _ocap_util_lib_error.CustomError("INVALID_TX", `data.${k} must be a non-negative integer`);
|
|
55
|
+
if (!Array.isArray(d.signatures) || d.signatures.length === 0 || d.signatures.length > 18) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "data.signatures must be a non-empty array of BridgeMembership signatures");
|
|
56
|
+
return d;
|
|
57
|
+
}
|
|
58
|
+
const membershipChecks = (action) => async function MembershipChecks(context, next) {
|
|
59
|
+
if (!context.isBridgeV2) return next();
|
|
60
|
+
const { itx, statedb, config } = context;
|
|
61
|
+
const state = context.rollupState;
|
|
62
|
+
const body = context.bridgeBody;
|
|
63
|
+
const txSec = context.txSec;
|
|
64
|
+
try {
|
|
65
|
+
_ocap_state.rollup.assertV1FieldsUnused(itx, "INVALID_ROLLUP_UPDATE");
|
|
66
|
+
if (state.closed || body.closingAt !== null) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "Membership is frozen after the bridge is closed");
|
|
67
|
+
const data = parseMembershipData(itx);
|
|
68
|
+
const plan = _ocap_state.rollup.planMembership(body, {
|
|
69
|
+
action,
|
|
70
|
+
validator: data.validator,
|
|
71
|
+
threshold: data.threshold,
|
|
72
|
+
activeFrom: data.activeFrom,
|
|
73
|
+
voucherValidity: data.voucherValidity
|
|
74
|
+
}, txSec);
|
|
75
|
+
if (action === "join" && !isHttpUrl(itx.endpoint)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.endpoint must be an http(s) URL");
|
|
76
|
+
if (action === "join" && !_ocap_state.rollup.sameAddress(context.tx.from, data.validator)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "tx.from must be the joining validator");
|
|
77
|
+
const maxSignatures = plan.signingEpoch.validators.length + 2;
|
|
78
|
+
if (data.signatures.length > maxSignatures) throw new _ocap_util_lib_error.CustomError("INVALID_TX", `data.signatures may hold at most ${maxSignatures} approvals`);
|
|
79
|
+
const joinerKey = _ocap_state.rollup.lc(data.validator);
|
|
80
|
+
const endpoints = plan.epoch.validators.map((a) => action === "join" && a === joinerKey ? itx.endpoint : body.members[a].endpoint);
|
|
81
|
+
const digest = require_protocols_rollup_bridge_v2.membershipDigest({
|
|
82
|
+
ocapChainId: config.chainId,
|
|
83
|
+
bridge: state.address,
|
|
84
|
+
epoch: plan.epoch,
|
|
85
|
+
action: action === "join" ? _ocap_mcrypto.BRIDGE_MEMBERSHIP_ACTION.JOIN : _ocap_mcrypto.BRIDGE_MEMBERSHIP_ACTION.LEAVE,
|
|
86
|
+
endpoints
|
|
87
|
+
});
|
|
88
|
+
const digestHex = `0x${Buffer.from(digest).toString("hex")}`;
|
|
89
|
+
const tombstoneKey = require_protocols_rollup_bridge_v2.membershipTombstoneKey(state.address, plan.epoch.epoch, digestHex);
|
|
90
|
+
if (await statedb.evidence.get(tombstoneKey, context)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "This membership approval was already used on this chain");
|
|
91
|
+
const recovered = require_protocols_rollup_bridge_v2.recoverAll(digest, data.signatures);
|
|
92
|
+
const signers = new Map(recovered.map((r) => [r.address, r]));
|
|
93
|
+
const owner = _ocap_state.rollup.lc(body.owner);
|
|
94
|
+
const quorum = new Set(plan.signingEpoch.validators);
|
|
95
|
+
const joiner = _ocap_state.rollup.lc(data.validator);
|
|
96
|
+
for (const address of signers.keys()) if (!(address === owner || quorum.has(address) || action === "join" && address === joiner)) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Signer ${address} is not the owner, a validator or the joiner`);
|
|
97
|
+
if (!signers.has(owner)) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "The bridge owner must sign the membership change");
|
|
98
|
+
const have = [...signers.keys()].filter((a) => quorum.has(a)).length;
|
|
99
|
+
if (have < plan.signingEpoch.threshold) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Expect at least ${plan.signingEpoch.threshold} validator signatures of epoch ${plan.signingEpoch.epoch}, got ${have}`);
|
|
100
|
+
const members = { ...body.members };
|
|
101
|
+
if (action === "join") {
|
|
102
|
+
const j = signers.get(joiner);
|
|
103
|
+
if (!j) throw new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", "The joining validator must sign the membership change");
|
|
104
|
+
members[joiner] = {
|
|
105
|
+
address: _ocap_state.account.ensureChecksumAddress(data.validator),
|
|
106
|
+
pk: j.pk,
|
|
107
|
+
endpoint: itx.endpoint
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
context.membership = {
|
|
111
|
+
plan,
|
|
112
|
+
tombstoneKey,
|
|
113
|
+
members
|
|
114
|
+
};
|
|
115
|
+
context.signers = [];
|
|
116
|
+
return next();
|
|
117
|
+
} catch (err) {
|
|
118
|
+
const e = err;
|
|
119
|
+
return next(String(e.code || "").startsWith("INVALID_ROLLUP") ? new _ocap_util_lib_error.CustomError("INVALID_TX", e.message) : e);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const membershipStep = (action) => async function MembershipStep(context, next) {
|
|
123
|
+
const { tx, statedb, senderState, senderUpdates, updateVaults } = context;
|
|
124
|
+
const body = context.bridgeBody;
|
|
125
|
+
const { plan, tombstoneKey, members } = context.membership;
|
|
126
|
+
const nextBody = {
|
|
127
|
+
...body,
|
|
128
|
+
epochs: [...body.epochs, plan.epoch],
|
|
129
|
+
members
|
|
130
|
+
};
|
|
131
|
+
const [newSenderState, newRollupState] = await Promise.all([
|
|
132
|
+
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
133
|
+
nonce: tx.nonce,
|
|
134
|
+
...senderUpdates
|
|
135
|
+
}, context), context),
|
|
136
|
+
statedb.rollup.update(context.rollupStateOrig.address, _ocap_state.rollup.updateBridge(context.rollupStateOrig, require_protocols_rollup_bridge_v2.bridgeUpdates(context, nextBody), context), context),
|
|
137
|
+
statedb.evidence.create(tombstoneKey, _ocap_state.evidence.create({
|
|
138
|
+
hash: tombstoneKey,
|
|
139
|
+
data: {
|
|
140
|
+
type: "json",
|
|
141
|
+
value: {
|
|
142
|
+
kind: `bridge-${action}`,
|
|
143
|
+
epoch: plan.epoch.epoch
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}, context), context)
|
|
147
|
+
]);
|
|
148
|
+
if (updateVaults) await updateVaults();
|
|
149
|
+
context.senderState = newSenderState;
|
|
150
|
+
context.rollupState = newRollupState;
|
|
151
|
+
return next();
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
//#endregion
|
|
155
|
+
exports.membershipChecks = membershipChecks;
|
|
156
|
+
exports.membershipSchemaV2 = membershipSchemaV2;
|
|
157
|
+
exports.membershipStep = membershipStep;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PipeFunction, PipeNext } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/protocols/rollup/bridge-membership.d.ts
|
|
4
|
+
type Ctx = any;
|
|
5
|
+
type Action = 'join' | 'leave';
|
|
6
|
+
declare const membershipSchemaV2: (action: Action) => PipeFunction<Ctx>;
|
|
7
|
+
declare const membershipChecks: (action: Action) => PipeFunction<Ctx>;
|
|
8
|
+
declare const membershipStep: (action: Action) => (context: Ctx, next: PipeNext) => Promise<void>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { membershipChecks, membershipSchemaV2, membershipStep };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_protocols_rollup_bridge_v2 = require('./bridge-v2.cjs');
|
|
3
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
4
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
5
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
6
|
+
let _ocap_state = require("@ocap/state");
|
|
7
|
+
let _ocap_util = require("@ocap/util");
|
|
8
|
+
let _arcblock_validator = require("@arcblock/validator");
|
|
9
|
+
|
|
10
|
+
//#region src/protocols/rollup/bridge-migrate.ts
|
|
11
|
+
/**
|
|
12
|
+
* `migrate_rollup` for a v2 bridge (bridge/docs/ocap-protocols.md §v2.2, row `migrate_rollup`).
|
|
13
|
+
*
|
|
14
|
+
* - `type = vault`: a new Ethereum Vault, owner + quorum.
|
|
15
|
+
* - `type = reserve`: moves the whole residual reserve of a FINAL bridge to a successor bridge. Announced first, executed
|
|
16
|
+
* after 30 days, vetoable in between by the token authority of #1411 alone. `data.action` selects the step.
|
|
17
|
+
* - `type = contract` is rejected: the gateway / Ethereum token of a bridge is immutable.
|
|
18
|
+
*/
|
|
19
|
+
const schemaV2 = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
20
|
+
rollup: (0, _arcblock_validator.vDID)({
|
|
21
|
+
prefix: true,
|
|
22
|
+
role: "ROLE_ROLLUP"
|
|
23
|
+
}),
|
|
24
|
+
to: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1)),
|
|
25
|
+
type: _arcblock_validator.v.picklist(["vault", "reserve"]),
|
|
26
|
+
message: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.trim(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.maxLength(512)),
|
|
27
|
+
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
28
|
+
}), (0, _arcblock_validator.vStripUnknown)());
|
|
29
|
+
const migrateSchemaV2 = function MigrateSchemaV2(context, next) {
|
|
30
|
+
if (!context.isBridgeV2) return next();
|
|
31
|
+
const { error } = (0, _arcblock_validator.vValidate)(schemaV2, context.itx);
|
|
32
|
+
if (error) {
|
|
33
|
+
const hint = context.itx?.type === "contract" ? " (the gateway and the ethereum token of a bridge are immutable)" : "";
|
|
34
|
+
return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}${hint}`));
|
|
35
|
+
}
|
|
36
|
+
return next();
|
|
37
|
+
};
|
|
38
|
+
const TOKEN_AUTHORITY_ERROR = "Only the token authority can cancel a reserve migration";
|
|
39
|
+
/** The token authority of #1411: the moderator for the official token, the token issuer otherwise. */
|
|
40
|
+
function tokenAuthority(context, tokenState) {
|
|
41
|
+
const { config } = context;
|
|
42
|
+
return !!config?.token?.address && (0, _ocap_util.isSameDid)(context.rollupState.tokenAddress, config.token.address) ? config?.moderator?.address : tokenState?.issuer;
|
|
43
|
+
}
|
|
44
|
+
async function migrateBridgeChecks(context, next) {
|
|
45
|
+
if (!context.isBridgeV2) return next();
|
|
46
|
+
const { itx, statedb } = context;
|
|
47
|
+
const state = context.rollupState;
|
|
48
|
+
const body = context.bridgeBody;
|
|
49
|
+
const txSec = context.txSec;
|
|
50
|
+
try {
|
|
51
|
+
if (itx.type === "vault") {
|
|
52
|
+
if (state.closed) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The rollup is closed");
|
|
53
|
+
if (!_ocap_state.rollup.isBridgePausedAt(state, txSec)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The rollup is not paused");
|
|
54
|
+
if (!itx.to || !_ocap_state.rollup.isValidRollupVaultAddress(itx.to, "EVM")) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: \"to\" Expected wallet type to be \"ethereum\"");
|
|
55
|
+
const same = (a) => _ocap_state.rollup.lc(a) === _ocap_state.rollup.lc(itx.to);
|
|
56
|
+
if (state.vaultAddress && same(state.vaultAddress)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "can not migrate vault to self");
|
|
57
|
+
if ((state.vaultHistory || []).some(same)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "can not migrate vault to history vaults");
|
|
58
|
+
context.migratePlan = { kind: "vault" };
|
|
59
|
+
return next();
|
|
60
|
+
}
|
|
61
|
+
const action = (itx.data ? require_protocols_rollup_bridge_v2.readItxData(itx) : {}).action;
|
|
62
|
+
if (![
|
|
63
|
+
"announce",
|
|
64
|
+
"execute",
|
|
65
|
+
"cancel"
|
|
66
|
+
].includes(action)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.data.action must be announce, execute or cancel");
|
|
67
|
+
if (action === "cancel") {
|
|
68
|
+
if (!body.pendingMigration) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "There is no pending reserve migration to cancel");
|
|
69
|
+
context.tokenAuthority = tokenAuthority(context, await statedb.token.get(state.tokenAddress, context));
|
|
70
|
+
context.migratePlan = { kind: "cancel" };
|
|
71
|
+
return next();
|
|
72
|
+
}
|
|
73
|
+
if (!_ocap_state.rollup.isBridgeFinalAt(body, txSec)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The source bridge is not final yet");
|
|
74
|
+
if (body.migratedTo !== null) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The reserve of this bridge was already migrated");
|
|
75
|
+
if (action === "announce" && body.pendingMigration) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "A reserve migration is already pending");
|
|
76
|
+
if (action === "execute") {
|
|
77
|
+
const p = body.pendingMigration;
|
|
78
|
+
if (!p) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "No reserve migration was announced");
|
|
79
|
+
if (_ocap_state.rollup.lc(p.to) !== _ocap_state.rollup.lc(itx.to)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The target differs from the announced migration");
|
|
80
|
+
if (txSec < p.effectiveAt) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The reserve migration window has not elapsed yet");
|
|
81
|
+
}
|
|
82
|
+
const target = await statedb.rollup.get(itx.to, context);
|
|
83
|
+
if (!target || !_ocap_state.rollup.isBridgeV2(target)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The migration target must be a v2 bridge");
|
|
84
|
+
const tbody = _ocap_state.rollup.getBridgeBody(target);
|
|
85
|
+
if (_ocap_state.rollup.sameAddress(target.address, state.address)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "Can not migrate a reserve to its own bridge");
|
|
86
|
+
if (target.closed) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The migration target is closed");
|
|
87
|
+
if (target.tokenAddress !== state.tokenAddress) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The migration target bridges a different OCAP token");
|
|
88
|
+
if (tbody.ethChainId !== body.ethChainId || _ocap_state.rollup.lc(target.contractAddress) !== _ocap_state.rollup.lc(state.contractAddress)) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The migration target bridges a different ethereum token");
|
|
89
|
+
if (_ocap_state.rollup.toSeconds(target.context?.genesisTime) < body.finalAt) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The migration target must be created after the source bridge is final");
|
|
90
|
+
context.migrateTarget = target;
|
|
91
|
+
context.migratePlan = { kind: action };
|
|
92
|
+
if (action === "execute") {
|
|
93
|
+
const [sourceState, targetState] = await Promise.all([statedb.account.get((0, _arcblock_did_util.toBridgeReserveAddress)(state.address), context), statedb.account.get((0, _arcblock_did_util.toBridgeReserveAddress)(target.address), context)]);
|
|
94
|
+
if (!sourceState || !targetState) throw new _ocap_util_lib_error.CustomError("INVALID_TX", "The reserve account of the source or the target bridge does not exist");
|
|
95
|
+
context.sourceReserveState = sourceState;
|
|
96
|
+
context.targetReserveState = targetState;
|
|
97
|
+
}
|
|
98
|
+
return next();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
return next(err);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const migrateBridgeAuth = function MigrateBridgeAuth(context, next) {
|
|
104
|
+
if (!context.isBridgeV2) return next();
|
|
105
|
+
const kind = context.migratePlan?.kind;
|
|
106
|
+
if (kind === "cancel") {
|
|
107
|
+
const { tx } = context;
|
|
108
|
+
const authority = context.tokenAuthority;
|
|
109
|
+
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(context, "tx.signaturesList");
|
|
110
|
+
if (!authority || tx.delegator || signatures.length > 0 || !(0, _ocap_util.isSameDid)(tx.from, authority)) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", TOKEN_AUTHORITY_ERROR));
|
|
111
|
+
context.signers = [];
|
|
112
|
+
return next();
|
|
113
|
+
}
|
|
114
|
+
if (kind === "vault") return require_protocols_rollup_bridge_v2.VerifyBridgeAuthority({
|
|
115
|
+
owner: "required",
|
|
116
|
+
quorum: "threshold"
|
|
117
|
+
})(context, next);
|
|
118
|
+
return require_protocols_rollup_bridge_v2.VerifyBridgeAuthority({
|
|
119
|
+
owner: "required",
|
|
120
|
+
quorum: "threshold",
|
|
121
|
+
quorumBody: (ctx) => _ocap_state.rollup.getBridgeBody(ctx.migrateTarget)
|
|
122
|
+
})(context, next);
|
|
123
|
+
};
|
|
124
|
+
async function migrateBridgeStep(context, next) {
|
|
125
|
+
const { tx, itx, statedb, rollupState, senderState, senderUpdates, updateVaults } = context;
|
|
126
|
+
const body = context.bridgeBody;
|
|
127
|
+
const txSec = context.txSec;
|
|
128
|
+
const kind = context.migratePlan.kind;
|
|
129
|
+
let nextBody = body;
|
|
130
|
+
let rowOverrides = {};
|
|
131
|
+
const accountWrites = [];
|
|
132
|
+
const updated = [];
|
|
133
|
+
if (kind === "vault") rowOverrides = {
|
|
134
|
+
vaultAddress: itx.to,
|
|
135
|
+
vaultHistory: rollupState.vaultAddress ? [...rollupState.vaultHistory || [], rollupState.vaultAddress] : [...rollupState.vaultHistory || []]
|
|
136
|
+
};
|
|
137
|
+
else if (kind === "announce") nextBody = {
|
|
138
|
+
...body,
|
|
139
|
+
pendingMigration: {
|
|
140
|
+
to: context.migrateTarget.address,
|
|
141
|
+
announcedAt: txSec,
|
|
142
|
+
effectiveAt: txSec + _ocap_state.rollup.BRIDGE_RESERVE_MIGRATION_WINDOW
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
else if (kind === "cancel") nextBody = {
|
|
146
|
+
...body,
|
|
147
|
+
pendingMigration: null
|
|
148
|
+
};
|
|
149
|
+
else if (kind === "execute") {
|
|
150
|
+
const target = context.migrateTarget;
|
|
151
|
+
const sourceReserve = (0, _arcblock_did_util.toBridgeReserveAddress)(rollupState.address);
|
|
152
|
+
const targetReserve = (0, _arcblock_did_util.toBridgeReserveAddress)(target.address);
|
|
153
|
+
const sourceState = context.sourceReserveState;
|
|
154
|
+
const targetState = context.targetReserveState;
|
|
155
|
+
const moved = {};
|
|
156
|
+
const nextTarget = { ...targetState.tokens || {} };
|
|
157
|
+
for (const [token, value] of Object.entries(sourceState.tokens || {})) {
|
|
158
|
+
if (new _ocap_util.BN(value).lten(0)) continue;
|
|
159
|
+
moved[token] = value;
|
|
160
|
+
nextTarget[token] = new _ocap_util.BN(nextTarget[token] || 0).add(new _ocap_util.BN(value)).toString(10);
|
|
161
|
+
updated.push({
|
|
162
|
+
address: sourceReserve,
|
|
163
|
+
token,
|
|
164
|
+
delta: `-${value}`,
|
|
165
|
+
action: "transfer"
|
|
166
|
+
});
|
|
167
|
+
updated.push({
|
|
168
|
+
address: targetReserve,
|
|
169
|
+
token,
|
|
170
|
+
delta: value,
|
|
171
|
+
action: "transfer"
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (Object.keys(moved).length > 0) accountWrites.push(statedb.account.update(sourceReserve, _ocap_state.account.update(sourceState, { tokens: {} }, context), context), statedb.account.update(targetReserve, _ocap_state.account.update(targetState, { tokens: nextTarget }, context), context));
|
|
175
|
+
context.reserveMigration = {
|
|
176
|
+
sourceReserve,
|
|
177
|
+
targetReserve
|
|
178
|
+
};
|
|
179
|
+
nextBody = {
|
|
180
|
+
...body,
|
|
181
|
+
migratedTo: target.address,
|
|
182
|
+
pendingMigration: null,
|
|
183
|
+
migratedAmount: moved[rollupState.tokenAddress] ?? "0"
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const [newSenderState, newRollupState, ...reserveStates] = await Promise.all([
|
|
187
|
+
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
188
|
+
nonce: tx.nonce,
|
|
189
|
+
...senderUpdates
|
|
190
|
+
}, context), context),
|
|
191
|
+
statedb.rollup.update(itx.rollup, _ocap_state.rollup.updateBridge(context.rollupStateOrig, require_protocols_rollup_bridge_v2.bridgeUpdates(context, nextBody, rowOverrides), context), context),
|
|
192
|
+
...accountWrites
|
|
193
|
+
]);
|
|
194
|
+
if (reserveStates.length === 2) {
|
|
195
|
+
context.sourceReserveState = reserveStates[0];
|
|
196
|
+
context.targetReserveState = reserveStates[1];
|
|
197
|
+
}
|
|
198
|
+
if (updateVaults) await updateVaults();
|
|
199
|
+
if (updated.length) {
|
|
200
|
+
context.updatedAccounts = context.updatedAccounts || [];
|
|
201
|
+
context.updatedAccounts.push(...updated);
|
|
202
|
+
}
|
|
203
|
+
context.senderState = newSenderState;
|
|
204
|
+
context.rollupState = newRollupState;
|
|
205
|
+
return next();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
exports.migrateBridgeAuth = migrateBridgeAuth;
|
|
210
|
+
exports.migrateBridgeChecks = migrateBridgeChecks;
|
|
211
|
+
exports.migrateBridgeStep = migrateBridgeStep;
|
|
212
|
+
exports.migrateSchemaV2 = migrateSchemaV2;
|