@ocap/tx-protocols 1.30.26 → 1.31.0
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/execute.d.mts +7 -0
- package/esm/execute.mjs +196 -71
- package/esm/gas-security-guard.d.mts +374 -0
- package/esm/gas-security-guard.mjs +1101 -0
- package/esm/index.d.mts +35 -5
- package/esm/index.mjs +3 -1
- package/esm/pipes/ensure-cost.mjs +314 -58
- package/esm/protocols/account/delegate.mjs +64 -16
- package/esm/protocols/account/migrate.d.mts +3 -0
- package/esm/protocols/account/migrate.mjs +235 -35
- package/esm/protocols/account/revoke-delegate.d.mts +3 -3
- package/esm/protocols/account/revoke-delegate.mjs +70 -38
- package/esm/protocols/asset/acquire-v2.mjs +29 -12
- package/esm/protocols/asset/acquire-v3.mjs +78 -22
- package/esm/protocols/asset/calls/transfer-token.mjs +2 -1
- package/esm/protocols/asset/calls/transfer.mjs +1 -0
- package/esm/protocols/asset/consume.mjs +12 -4
- package/esm/protocols/asset/create.mjs +18 -5
- package/esm/protocols/asset/mint.mjs +23 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +21 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +115 -1
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +14 -8
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +9 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +21 -5
- package/esm/protocols/asset/update.d.mts +13 -2
- package/esm/protocols/asset/update.mjs +133 -14
- package/esm/protocols/factory/create.mjs +14 -6
- package/esm/protocols/governance/claim-stake.mjs +92 -10
- package/esm/protocols/governance/return-stake.mjs +44 -12
- package/esm/protocols/governance/revoke-stake.mjs +17 -5
- package/esm/protocols/governance/slash-stake.mjs +64 -25
- package/esm/protocols/governance/stake.mjs +63 -13
- package/esm/protocols/rollup/claim-reward.mjs +39 -20
- package/esm/protocols/rollup/close.mjs +1 -1
- package/esm/protocols/rollup/create-block.mjs +8 -7
- package/esm/protocols/rollup/create.mjs +17 -6
- package/esm/protocols/rollup/join.mjs +3 -3
- package/esm/protocols/rollup/leave.mjs +1 -1
- package/esm/protocols/rollup/migrate.mjs +7 -4
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +12 -4
- package/esm/protocols/rollup/update.mjs +6 -1
- package/esm/protocols/token/create.mjs +23 -12
- package/esm/protocols/token/deposit-v2.d.mts +5 -1
- package/esm/protocols/token/deposit-v2.mjs +81 -29
- package/esm/protocols/token/withdraw-v2.mjs +33 -16
- package/esm/protocols/token-factory/burn.mjs +43 -10
- package/esm/protocols/token-factory/create.mjs +23 -14
- package/esm/protocols/token-factory/mint.mjs +54 -21
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +1 -1
- package/esm/protocols/token-factory/update.mjs +16 -6
- package/esm/protocols/trade/exchange-v2.d.mts +4 -0
- package/esm/protocols/trade/exchange-v2.mjs +78 -22
- package/esm/protocols/trade/transfer-v2.mjs +44 -13
- package/esm/protocols/trade/transfer-v3.mjs +72 -31
- package/esm/util.d.mts +3 -1
- package/esm/util.mjs +26 -17
- package/lib/execute.cjs +193 -67
- package/lib/execute.d.cts +7 -0
- package/lib/gas-security-guard.cjs +1140 -0
- package/lib/gas-security-guard.d.cts +374 -0
- package/lib/index.cjs +122 -1
- package/lib/index.d.cts +36 -6
- package/lib/pipes/ensure-cost.cjs +313 -57
- package/lib/protocols/account/delegate.cjs +64 -16
- package/lib/protocols/account/migrate.cjs +235 -35
- package/lib/protocols/account/migrate.d.cts +3 -0
- package/lib/protocols/account/revoke-delegate.cjs +71 -39
- package/lib/protocols/account/revoke-delegate.d.cts +3 -3
- package/lib/protocols/asset/acquire-v2.cjs +28 -11
- package/lib/protocols/asset/acquire-v3.cjs +77 -20
- package/lib/protocols/asset/calls/transfer-token.cjs +3 -2
- package/lib/protocols/asset/calls/transfer.cjs +1 -0
- package/lib/protocols/asset/consume.cjs +11 -3
- package/lib/protocols/asset/create.cjs +19 -6
- package/lib/protocols/asset/mint.cjs +23 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +117 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +21 -1
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +14 -9
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +9 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +21 -5
- package/lib/protocols/asset/update.cjs +131 -12
- package/lib/protocols/asset/update.d.cts +13 -2
- package/lib/protocols/factory/create.cjs +14 -6
- package/lib/protocols/governance/claim-stake.cjs +92 -9
- package/lib/protocols/governance/claim-stake.d.cts +1 -1
- package/lib/protocols/governance/return-stake.cjs +44 -11
- package/lib/protocols/governance/return-stake.d.cts +1 -1
- package/lib/protocols/governance/revoke-stake.cjs +17 -5
- package/lib/protocols/governance/revoke-stake.d.cts +1 -1
- package/lib/protocols/governance/slash-stake.cjs +64 -24
- package/lib/protocols/governance/slash-stake.d.cts +1 -1
- package/lib/protocols/governance/stake.cjs +65 -14
- package/lib/protocols/rollup/claim-reward.cjs +39 -20
- package/lib/protocols/rollup/close.cjs +1 -1
- package/lib/protocols/rollup/create-block.cjs +9 -8
- package/lib/protocols/rollup/create-block.d.cts +1 -1
- package/lib/protocols/rollup/create.cjs +18 -7
- package/lib/protocols/rollup/join.cjs +3 -3
- package/lib/protocols/rollup/leave.cjs +1 -1
- package/lib/protocols/rollup/migrate.cjs +7 -4
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +11 -3
- package/lib/protocols/rollup/update.cjs +6 -1
- package/lib/protocols/token/create.cjs +24 -13
- package/lib/protocols/token/deposit-v2.cjs +80 -28
- package/lib/protocols/token/deposit-v2.d.cts +5 -1
- package/lib/protocols/token/withdraw-v2.cjs +32 -15
- package/lib/protocols/token-factory/burn.cjs +42 -9
- package/lib/protocols/token-factory/create.cjs +24 -15
- package/lib/protocols/token-factory/mint.cjs +53 -20
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +1 -1
- package/lib/protocols/token-factory/update.cjs +16 -6
- package/lib/protocols/trade/exchange-v2.cjs +76 -20
- package/lib/protocols/trade/exchange-v2.d.cts +4 -0
- package/lib/protocols/trade/transfer-v2.cjs +45 -14
- package/lib/protocols/trade/transfer-v3.cjs +72 -30
- package/lib/util.cjs +26 -17
- package/lib/util.d.cts +4 -2
- package/package.json +16 -16
package/esm/execute.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ITxGasQuotaTelemetry, ITxSecurityWarning } from "./gas-security-guard.mjs";
|
|
1
2
|
import { TxTimer } from "./tx-timer.mjs";
|
|
2
3
|
import { Blacklist } from "@ocap/state";
|
|
3
4
|
import { CustomError } from "@ocap/util/lib/error";
|
|
@@ -36,6 +37,12 @@ interface IExecutionContext extends IGasContext {
|
|
|
36
37
|
timer?: TxTimer;
|
|
37
38
|
/** Per-invocation identifier for in-flight deduplication across retries */
|
|
38
39
|
executionId?: symbol;
|
|
40
|
+
/** True when charging gas on a failed persisted transaction */
|
|
41
|
+
isFailedTxGasCharge?: boolean;
|
|
42
|
+
/** Machine-readable gas quota & burst escalation telemetry */
|
|
43
|
+
gasQuotaTelemetry?: ITxGasQuotaTelemetry;
|
|
44
|
+
/** Structured progressive strike warning */
|
|
45
|
+
securityWarning?: ITxSecurityWarning;
|
|
39
46
|
}
|
|
40
47
|
/** Protocol runner interface */
|
|
41
48
|
interface IProtocolRunner {
|
package/esm/execute.mjs
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
+
import { classifyErrorStrikeCategory, recordActorSecurityStrike, resolveVerifiedStrikeActors, restoreActorSecurityRecords, snapshotActorSecurityRecords } from "./gas-security-guard.mjs";
|
|
1
2
|
import ensure_cost_default from "./pipes/ensure-cost.mjs";
|
|
2
3
|
import ensure_gas_default from "./pipes/ensure-gas.mjs";
|
|
3
4
|
import { WriteLedger } from "./pipes/write-ledger.mjs";
|
|
4
5
|
import { TxTimer } from "./tx-timer.mjs";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { toTypeInfo } from "@arcblock/did";
|
|
7
|
+
import { toDelegateAddress } from "@arcblock/did-util";
|
|
8
|
+
import { toTxHash, types } from "@ocap/mcrypto";
|
|
9
|
+
import { account, evidence, parseNonceBn } from "@ocap/state";
|
|
10
|
+
import { Runner, inspectPostExecutionInvariants, installStateWriteInterceptor, pipes, rollbackTrackedStateWrites, verifyImplicitAssetOwnership } from "@ocap/tx-pipeline";
|
|
11
|
+
import { BN, isSameDid } from "@ocap/util";
|
|
9
12
|
import { CustomError } from "@ocap/util/lib/error";
|
|
13
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
14
|
+
import { getRelatedAddresses } from "@ocap/util/lib/get-related-addr";
|
|
10
15
|
import camelCase from "lodash/camelCase.js";
|
|
16
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
11
17
|
import get from "lodash/get.js";
|
|
12
18
|
import merge from "lodash/merge.js";
|
|
13
19
|
import pick from "lodash/pick.js";
|
|
@@ -39,7 +45,33 @@ const flushEvents = (context, extra = {}) => {
|
|
|
39
45
|
});
|
|
40
46
|
context.events = [];
|
|
41
47
|
};
|
|
42
|
-
const
|
|
48
|
+
const SINGLE_SIG_DELEGATION_TX_TYPES = new Set([
|
|
49
|
+
"fg:t:acquire_asset_v2",
|
|
50
|
+
"fg:t:acquire_asset_v3",
|
|
51
|
+
"fg:t:create_asset",
|
|
52
|
+
"fg:t:update_asset",
|
|
53
|
+
"fg:t:create_factory",
|
|
54
|
+
"fg:t:create_rollup",
|
|
55
|
+
"fg:t:create_token_factory",
|
|
56
|
+
"fg:t:update_token_factory",
|
|
57
|
+
"fg:t:create_token",
|
|
58
|
+
"fg:t:exchange_v2",
|
|
59
|
+
"fg:t:transfer",
|
|
60
|
+
"fg:t:transfer_v2",
|
|
61
|
+
"fg:t:transfer_v3"
|
|
62
|
+
]);
|
|
63
|
+
const MULTISIG_DELEGATION_TX_TYPES = new Set([
|
|
64
|
+
"fg:t:acquire_asset_v3",
|
|
65
|
+
"fg:t:exchange_v2",
|
|
66
|
+
"fg:t:transfer_v3"
|
|
67
|
+
]);
|
|
68
|
+
const shouldPersistTx = (err, context) => {
|
|
69
|
+
if (!err) return true;
|
|
70
|
+
if (!get(err, "props.persist")) return false;
|
|
71
|
+
if (context?.tx?.delegator && !context.delegationState && !context.isImplicitDelegation) return false;
|
|
72
|
+
if ((getListField(context?.tx, "signatures") || []).some((s) => Boolean(s?.delegator)) && !context.isMultiSigDelegationVerified && !context.delegationStates) return false;
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
43
75
|
const toUnsignedEvidenceKey = (unsignedTxHash) => toTxHash(Buffer.from(`unsigned-tx:${unsignedTxHash}`));
|
|
44
76
|
const UNSIGNED_TX_EVIDENCE_DATA_PREFIX = "unsigned-tx:";
|
|
45
77
|
const hasEvidenceStore = (statedb) => Boolean(statedb && typeof statedb.evidence?.get === "function" && typeof statedb.evidence?.create === "function");
|
|
@@ -56,18 +88,11 @@ const getOrCreateSyncState = (target) => {
|
|
|
56
88
|
}
|
|
57
89
|
return state;
|
|
58
90
|
};
|
|
59
|
-
/**
|
|
60
|
-
* Determine whether `unsignedTxHash` deduplication in `statedb.evidence` is active
|
|
61
|
-
* for the current execution context.
|
|
62
|
-
*/
|
|
63
|
-
const isUnsignedDedupActive = async (context) => {
|
|
64
|
-
if (!context.unsignedTxHash) return false;
|
|
65
|
-
return isDedupHeightActive(context);
|
|
66
|
-
};
|
|
67
91
|
const createExecute = ({ filter, runAsLambda }) => {
|
|
68
92
|
const fallbackSyncTarget = {};
|
|
69
93
|
const getSyncState = (context) => {
|
|
70
|
-
|
|
94
|
+
const rawDb = (context?.statedb)?._underlyingStateDb || context?.statedb;
|
|
95
|
+
return getOrCreateSyncState((rawDb && typeof rawDb === "object" ? rawDb : null) || (context?.ledger && typeof context.ledger === "object" ? context.ledger : null) || fallbackSyncTarget);
|
|
71
96
|
};
|
|
72
97
|
const withPreWriteLock = async (context, fn) => {
|
|
73
98
|
const syncState = getSyncState(context);
|
|
@@ -104,7 +129,6 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
104
129
|
};
|
|
105
130
|
const recordUnsignedTxInStateDb = async (context, txHash) => {
|
|
106
131
|
if (!context.unsignedTxHash || !hasEvidenceStore(context.statedb)) return;
|
|
107
|
-
if (!await isUnsignedDedupActive(context)) return;
|
|
108
132
|
const unsignedKey = toUnsignedEvidenceKey(context.unsignedTxHash);
|
|
109
133
|
if (!await context.statedb.evidence.get(unsignedKey, { txn: context.txn })) {
|
|
110
134
|
const evState = evidence.create({
|
|
@@ -118,9 +142,9 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
118
142
|
}
|
|
119
143
|
};
|
|
120
144
|
/**
|
|
121
|
-
*
|
|
145
|
+
* Sender nonce precheck, run under the pre-write lock BEFORE WriteLedger.
|
|
122
146
|
*
|
|
123
|
-
* Every persisted pre
|
|
147
|
+
* Every persisted pre-#1127 tx has `nonce <= sender.nonce` once the offline migration
|
|
124
148
|
* restored each account's max historical nonce. Rejecting stale nonces here (instead of
|
|
125
149
|
* relying on each protocol's `account.update` after WriteLedger) means a signature-malleated
|
|
126
150
|
* twin of ANY historical tx is refused without appending a ledger row and without depending
|
|
@@ -131,10 +155,6 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
131
155
|
* decides as before.
|
|
132
156
|
* - Sender migrated (statedb resolves `tx.from` to its successor): reject like
|
|
133
157
|
* VerifyAccountMigration, but before the ledger write.
|
|
134
|
-
*
|
|
135
|
-
* Activation boundary: only runs when the tx is already active at the expected sequence. A tx
|
|
136
|
-
* that is pre-activation here but lands on the activation sequence after WriteLedger skips this
|
|
137
|
-
* precheck once; the post-write nonce/dedup checks and the protocol's account.update still apply.
|
|
138
158
|
*/
|
|
139
159
|
const assertSenderNonceFresh = async (context) => {
|
|
140
160
|
const from = context.tx?.from;
|
|
@@ -147,12 +167,57 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
147
167
|
const currentNonceBn = parseNonceBn(senderState.nonce) ?? new BN(0);
|
|
148
168
|
if (!incomingNonceBn || incomingNonceBn.lte(currentNonceBn)) throw new CustomError("INVALID_NONCE", "nonce must be greater in newer transactions");
|
|
149
169
|
};
|
|
170
|
+
/**
|
|
171
|
+
* Pre-ledger delegation authorization check, run under the pre-write lock BEFORE WriteLedger.
|
|
172
|
+
*
|
|
173
|
+
* Unauthenticated delegated transactions (`tx.delegator` or multisig `signatures[].delegator`
|
|
174
|
+
* without valid on-chain delegation or implicit asset ownership) are rejected with
|
|
175
|
+
* `persist: false` so they cannot charge gas or advance nonces on the target account.
|
|
176
|
+
* Rejecting them before `WriteLedger` prevents unauthenticated actors from appending
|
|
177
|
+
* orphaned, unpersisted entries to the immutable ledger.
|
|
178
|
+
*/
|
|
179
|
+
const assertDelegatorPairAuthorized = async (context, signerAddress, delegatorAddress, noDelegationMsg) => {
|
|
180
|
+
if (!signerAddress || !delegatorAddress) return;
|
|
181
|
+
const statedb = context.statedb;
|
|
182
|
+
const accountStore = statedb?.account;
|
|
183
|
+
if (typeof accountStore?.get !== "function") return;
|
|
184
|
+
const delegatorState = await accountStore.get(delegatorAddress, context);
|
|
185
|
+
if (delegatorState && (!isSameDid(delegatorState.address, delegatorAddress) || Array.isArray(delegatorState.migratedTo) && delegatorState.migratedTo.length > 0)) throw new CustomError("ACCOUNT_MIGRATED", `Delegator account ${delegatorAddress} is migrated`, { persist: false });
|
|
186
|
+
if (toTypeInfo(signerAddress).role === types.RoleType.ROLE_ASSET) {
|
|
187
|
+
if (typeof statedb?.asset?.get !== "function") return;
|
|
188
|
+
let isOwner = false;
|
|
189
|
+
try {
|
|
190
|
+
isOwner = await verifyImplicitAssetOwnership(signerAddress, delegatorState || { address: delegatorAddress }, context, delegatorAddress);
|
|
191
|
+
} catch (err) {
|
|
192
|
+
if (err && typeof err === "object") err.props = {
|
|
193
|
+
...err.props || {},
|
|
194
|
+
persist: false
|
|
195
|
+
};
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
if (!isOwner) throw new CustomError("INVALID_DELEGATION", `Account ${delegatorState?.address || delegatorAddress} is not the owner of asset ${signerAddress}`, { persist: false });
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (typeof statedb?.delegation?.get !== "function") return;
|
|
202
|
+
const signerState = await accountStore.get(signerAddress, context);
|
|
203
|
+
if (signerState && (!isSameDid(signerState.address, signerAddress) || Array.isArray(signerState.migratedTo) && signerState.migratedTo.length > 0)) throw new CustomError("ACCOUNT_MIGRATED", `account ${signerAddress} is migrated`, { persist: false });
|
|
204
|
+
if (!signerState || !delegatorState) throw new CustomError("INVALID_DELEGATION", noDelegationMsg, { persist: false });
|
|
205
|
+
const related = getRelatedAddresses(delegatorState);
|
|
206
|
+
if (!(await Promise.all(related.map((x) => statedb.delegation.get(toDelegateAddress(signerState.address, x), context)))).some(Boolean)) throw new CustomError("INVALID_DELEGATION", noDelegationMsg, { persist: false });
|
|
207
|
+
};
|
|
208
|
+
const assertDelegationAuthorized = async (context) => {
|
|
209
|
+
const tx$1 = context.tx;
|
|
210
|
+
if (tx$1?.delegator) {
|
|
211
|
+
if (context.txType && !SINGLE_SIG_DELEGATION_TX_TYPES.has(context.txType)) throw new CustomError("INVALID_DELEGATION", "Delegation is not supported for this transaction", { persist: false });
|
|
212
|
+
await assertDelegatorPairAuthorized(context, tx$1.from, tx$1.delegator, "No delegation found between sender and delegator");
|
|
213
|
+
}
|
|
214
|
+
const signatures = getListField(tx$1, "signatures") || [];
|
|
215
|
+
for (const sig of signatures) if (sig?.delegator) {
|
|
216
|
+
if (context.txType && !MULTISIG_DELEGATION_TX_TYPES.has(context.txType)) throw new CustomError("INVALID_DELEGATION", "Delegation is not supported for this transaction", { persist: false });
|
|
217
|
+
await assertDelegatorPairAuthorized(context, sig.signer, sig.delegator, "No delegation found between signer and delegator");
|
|
218
|
+
}
|
|
219
|
+
};
|
|
150
220
|
const pre = new Runner("pre-execute");
|
|
151
|
-
pre.use(async (context, next) => {
|
|
152
|
-
const dedupHeight = resolveDedupActivationHeight(context);
|
|
153
|
-
if (dedupHeight > 0 && dedupHeight < PENDING_DEDUP_ACTIVATION_HEIGHT) await resolveEffectiveHeight(context);
|
|
154
|
-
return next();
|
|
155
|
-
});
|
|
156
221
|
pre.use(pipes.DecodeTx);
|
|
157
222
|
pre.use(pipes.VerifyBlocked({
|
|
158
223
|
filter,
|
|
@@ -169,57 +234,40 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
169
234
|
if (filter.has(context.txHash)) {
|
|
170
235
|
if (await context.statedb.tx.get(context.txHash, context)) return next(new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction"));
|
|
171
236
|
}
|
|
172
|
-
if (
|
|
173
|
-
const inFlightUnsignedOwner =
|
|
237
|
+
if (context.unsignedTxHash) {
|
|
238
|
+
const inFlightUnsignedOwner = syncState.inFlightUnsignedTxHashes.get(context.unsignedTxHash);
|
|
174
239
|
if (inFlightUnsignedOwner && inFlightUnsignedOwner !== execId || await hasCommittedUnsignedTx(context)) return next(new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction"));
|
|
175
240
|
}
|
|
176
241
|
filter.add(context.txHash);
|
|
177
242
|
return next();
|
|
178
243
|
});
|
|
179
244
|
pre.use(pipes.VerifyTxSize);
|
|
245
|
+
pre.use(pipes.PreFlightWaf);
|
|
180
246
|
pre.use(pipes.VerifySignature);
|
|
247
|
+
pre.use((context, next) => {
|
|
248
|
+
context.isSignatureVerified = true;
|
|
249
|
+
return next();
|
|
250
|
+
});
|
|
181
251
|
pre.use(async (context, next) => {
|
|
182
252
|
try {
|
|
183
253
|
await withPreWriteLock(context, async () => {
|
|
184
254
|
const syncState = getSyncState(context);
|
|
185
|
-
if (context.height === void 0 && !context.ledgerSequence) {
|
|
186
|
-
delete context.expectedLedgerSequence;
|
|
187
|
-
delete context.dedupActive;
|
|
188
|
-
const dedupHeight = resolveDedupActivationHeight(context);
|
|
189
|
-
if (dedupHeight > 0 && dedupHeight < PENDING_DEDUP_ACTIVATION_HEIGHT) await resolveEffectiveHeight(context);
|
|
190
|
-
}
|
|
191
|
-
const isActiveNow = await isUnsignedDedupActive(context);
|
|
192
|
-
context.dedupActive = isActiveNow;
|
|
193
255
|
const execId = context.executionId || Symbol("tx-exec");
|
|
194
256
|
context.executionId = execId;
|
|
195
257
|
if (context.txHash) {
|
|
196
258
|
const existingTxOwner = syncState.inFlightTxHashes.get(context.txHash);
|
|
197
259
|
if (existingTxOwner && existingTxOwner !== execId) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
198
260
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (context.unsignedTxHash) {
|
|
205
|
-
const existingUnsignedOwner = syncState.inFlightUnsignedTxHashes.get(context.unsignedTxHash);
|
|
206
|
-
if (existingUnsignedOwner && existingUnsignedOwner !== execId) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
207
|
-
}
|
|
208
|
-
if (await hasCommittedUnsignedTx(context)) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
261
|
+
await assertSenderNonceFresh(context);
|
|
262
|
+
await assertDelegationAuthorized(context);
|
|
263
|
+
if (context.unsignedTxHash) {
|
|
264
|
+
const existingUnsignedOwner = syncState.inFlightUnsignedTxHashes.get(context.unsignedTxHash);
|
|
265
|
+
if (existingUnsignedOwner && existingUnsignedOwner !== execId) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
209
266
|
}
|
|
267
|
+
if (await hasCommittedUnsignedTx(context)) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
210
268
|
if (context.txHash) syncState.inFlightTxHashes.set(context.txHash, execId);
|
|
211
|
-
if (
|
|
269
|
+
if (context.unsignedTxHash) syncState.inFlightUnsignedTxHashes.set(context.unsignedTxHash, execId);
|
|
212
270
|
await runSinglePipe(WriteLedger, context);
|
|
213
|
-
delete context.dedupActive;
|
|
214
|
-
const isActiveAfterWrite = await isUnsignedDedupActive(context);
|
|
215
|
-
context.dedupActive = isActiveAfterWrite;
|
|
216
|
-
if (isActiveAfterWrite) {
|
|
217
|
-
if (!isActiveNow) assertCanonicalActiveTx(context);
|
|
218
|
-
const nonceBn = parseNonceBn(context.tx?.nonce);
|
|
219
|
-
if (!nonceBn || nonceBn.lte(new BN(0))) throw new CustomError("INVALID_NONCE", "tx.nonce should be a positive integer");
|
|
220
|
-
if (await hasCommittedUnsignedTx(context)) throw new CustomError("DUPLICATE_TX", "Can not execute duplicate transaction");
|
|
221
|
-
if (context.unsignedTxHash) syncState.inFlightUnsignedTxHashes.set(context.unsignedTxHash, execId);
|
|
222
|
-
}
|
|
223
271
|
});
|
|
224
272
|
return next();
|
|
225
273
|
} catch (err) {
|
|
@@ -228,18 +276,30 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
228
276
|
});
|
|
229
277
|
const ensureGasFeePaid = (context) => {
|
|
230
278
|
const gasRunner = new Runner("ensure-gas-fee-paid");
|
|
279
|
+
gasRunner.use((ctx, next) => {
|
|
280
|
+
if (ctx.senderState?.address && ctx.stateSnapshot?.[ctx.senderState.address]) ctx.senderState = cloneDeep(ctx.stateSnapshot[ctx.senderState.address]);
|
|
281
|
+
ctx.isFailedTxGasCharge = true;
|
|
282
|
+
ctx.gasPaid = false;
|
|
283
|
+
ctx.senderUpdates = {};
|
|
284
|
+
ctx.senderChange = void 0;
|
|
285
|
+
ctx.updatedAccounts = [];
|
|
286
|
+
ctx.feeVaultChange = void 0;
|
|
287
|
+
ctx.gasVaultChange = void 0;
|
|
288
|
+
return next();
|
|
289
|
+
});
|
|
231
290
|
gasRunner.use(ensureTxGas);
|
|
232
291
|
gasRunner.use(ensureTxCost);
|
|
233
292
|
gasRunner.use(async (ctx, next) => {
|
|
234
293
|
const { tx: tx$1, statedb, senderUpdates, updateVaults } = ctx;
|
|
235
294
|
if (ctx.senderState && ctx.states && tx$1) {
|
|
236
|
-
|
|
295
|
+
if (tx$1.delegator && !ctx.delegationState && !ctx.isImplicitDelegation) return next(new CustomError("INVALID_DELEGATION", "Cannot charge gas to sender without verified delegation"));
|
|
296
|
+
const senderState = ctx.senderState;
|
|
237
297
|
const updates = ctx.states.account.update(senderState, {
|
|
238
298
|
nonce: tx$1.nonce,
|
|
239
|
-
pk: tx$1.pk,
|
|
299
|
+
pk: tx$1.delegator ? senderState.pk : tx$1.pk,
|
|
240
300
|
...senderUpdates
|
|
241
301
|
}, ctx);
|
|
242
|
-
await statedb.account.update(senderState.address, updates, ctx);
|
|
302
|
+
ctx.senderState = await statedb.account.update(senderState.address, updates, ctx) || senderState;
|
|
243
303
|
if (updateVaults) await updateVaults();
|
|
244
304
|
}
|
|
245
305
|
return next();
|
|
@@ -260,9 +320,40 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
260
320
|
});
|
|
261
321
|
});
|
|
262
322
|
};
|
|
323
|
+
const attachSecurityStrikeToError = async (err, ctx) => {
|
|
324
|
+
if (!err || err._strikeRecorded || err._unauthenticatedExtraFailure === true) return;
|
|
325
|
+
const classified = classifyErrorStrikeCategory(err);
|
|
326
|
+
if (!classified.shouldRecordStrike) return;
|
|
327
|
+
const secCfg = ctx.config?.transaction?.security;
|
|
328
|
+
const strikeOnAllErrors = secCfg?.strikeOnAllErrors === true;
|
|
329
|
+
if (!(classified.category === "WAF_REJECT" || classified.category === "INVARIANT_VIOLATION" || classified.category === "PAYMASTER_VIOLATION" || strikeOnAllErrors)) return;
|
|
330
|
+
const actors = await resolveVerifiedStrikeActors(ctx);
|
|
331
|
+
if (actors.length === 0) return;
|
|
332
|
+
err._strikeRecorded = true;
|
|
333
|
+
const fishermanAddress = secCfg?.fishermanAddress;
|
|
334
|
+
const nowMs = ctx.txTime ? +new Date(ctx.txTime) : Date.now();
|
|
335
|
+
const { primaryWarning, slashEvidence } = await recordActorSecurityStrike({
|
|
336
|
+
actors,
|
|
337
|
+
category: classified.category,
|
|
338
|
+
reasonCode: classified.code,
|
|
339
|
+
txHash: ctx.txHash,
|
|
340
|
+
config: ctx.config,
|
|
341
|
+
statedb: ctx.statedb,
|
|
342
|
+
txn: ctx.txn,
|
|
343
|
+
fishermanAddress,
|
|
344
|
+
nowMs,
|
|
345
|
+
telemetry: ctx.gasQuotaTelemetry
|
|
346
|
+
});
|
|
347
|
+
if (primaryWarning && !err.warning) {
|
|
348
|
+
err.warning = primaryWarning;
|
|
349
|
+
ctx.securityWarning = primaryWarning;
|
|
350
|
+
}
|
|
351
|
+
if (slashEvidence && !err.slashEvidence) err.slashEvidence = slashEvidence;
|
|
352
|
+
};
|
|
263
353
|
const execute = async (context, protocols, isRetrySupported = false) => new Promise((resolve, reject) => {
|
|
264
|
-
pre.run(context, (err) => {
|
|
354
|
+
pre.run(context, async (err) => {
|
|
265
355
|
if (err) {
|
|
356
|
+
if (!isRetrySupported) await attachSecurityStrikeToError(err, context);
|
|
266
357
|
context.logger?.error("Failed to prepare transaction", {
|
|
267
358
|
error: err,
|
|
268
359
|
txHash: context.txHash,
|
|
@@ -272,16 +363,30 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
272
363
|
}
|
|
273
364
|
const protocol = protocols[getTxName(context.txType)];
|
|
274
365
|
if (!protocol) return reject(new CustomError("UNSUPPORTED_TX", `Unsupported tx type ${context.txType}`));
|
|
275
|
-
|
|
366
|
+
installStateWriteInterceptor(context);
|
|
367
|
+
protocol.run(context, async (rawError) => {
|
|
368
|
+
const underlyingDb = context.statedb?._underlyingStateDb;
|
|
369
|
+
if (underlyingDb) context.statedb = underlyingDb;
|
|
370
|
+
let error = rawError;
|
|
276
371
|
context.timer?.mark("protocol");
|
|
372
|
+
if (!error) try {
|
|
373
|
+
inspectPostExecutionInvariants(context);
|
|
374
|
+
} catch (invariantErr) {
|
|
375
|
+
error = invariantErr;
|
|
376
|
+
}
|
|
277
377
|
if (isRetrySupported) {
|
|
278
378
|
if (error) return reject(error);
|
|
279
379
|
return resolve(context);
|
|
280
380
|
}
|
|
381
|
+
if (error) {
|
|
382
|
+
await rollbackTrackedStateWrites(context);
|
|
383
|
+
context.events = [];
|
|
384
|
+
await attachSecurityStrikeToError(error, context);
|
|
385
|
+
}
|
|
281
386
|
const txStatus = error ? error.code || "INTERNAL" : "OK";
|
|
282
387
|
const states = getStates(context);
|
|
283
388
|
let txState = states.tx.create(context, txStatus);
|
|
284
|
-
if (shouldPersistTx(error)) try {
|
|
389
|
+
if (shouldPersistTx(error, context)) try {
|
|
285
390
|
if (error) {
|
|
286
391
|
await ensureGasFeePaid(context);
|
|
287
392
|
txState = states.tx.create(context, txStatus);
|
|
@@ -306,7 +411,7 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
306
411
|
txSize: context.txSize,
|
|
307
412
|
senderAddr: context.tx?.from,
|
|
308
413
|
ledgerSequence: context.ledgerSequence,
|
|
309
|
-
status: error ? shouldPersistTx(error) ? "PERSISTED_FAILED" : "FAILED" : "OK",
|
|
414
|
+
status: error ? shouldPersistTx(error, context) ? "PERSISTED_FAILED" : "FAILED" : "OK",
|
|
310
415
|
errorCode: error ? error.code || "INTERNAL" : null,
|
|
311
416
|
retried: Boolean(error)
|
|
312
417
|
});
|
|
@@ -419,7 +524,7 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
419
524
|
txState,
|
|
420
525
|
request: context.extra?.request
|
|
421
526
|
});
|
|
422
|
-
if (txState && shouldPersistTx(err)) {
|
|
527
|
+
if (txState && shouldPersistTx(err, ctx)) {
|
|
423
528
|
ctx.events = [];
|
|
424
529
|
try {
|
|
425
530
|
txState = await runAsLambda(async (txn) => {
|
|
@@ -428,12 +533,30 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
428
533
|
cacheStates: null
|
|
429
534
|
});
|
|
430
535
|
ctx.timer?.mark("lockWait");
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
536
|
+
const strikeSnapshot = snapshotActorSecurityRecords([
|
|
537
|
+
ctx.tx?.delegator || "",
|
|
538
|
+
ctx.tx?.from || "",
|
|
539
|
+
ctx.gasStake?.sponsorAddress || ""
|
|
540
|
+
].filter(Boolean), ctx.statedb, ctx.txHash);
|
|
541
|
+
const hadStrikeBefore = Boolean(err._strikeRecorded);
|
|
542
|
+
const prevWarning = err.warning;
|
|
543
|
+
const prevSlashEvidence = err.slashEvidence;
|
|
544
|
+
try {
|
|
545
|
+
await ensureGasFeePaid(ctx);
|
|
546
|
+
await attachSecurityStrikeToError(err, ctx);
|
|
547
|
+
const state = getStates(context).tx.create(ctx, txStatus);
|
|
548
|
+
await ctx.statedb.tx.create(state.hash, state, ctx);
|
|
549
|
+
await recordUnsignedTxInStateDb(ctx, state.hash);
|
|
550
|
+
ctx.timer?.mark("txstate");
|
|
551
|
+
return state;
|
|
552
|
+
} catch (lambdaErr) {
|
|
553
|
+
restoreActorSecurityRecords(strikeSnapshot, ctx.statedb);
|
|
554
|
+
err._strikeRecorded = hadStrikeBefore;
|
|
555
|
+
err.warning = prevWarning;
|
|
556
|
+
err.slashEvidence = prevSlashEvidence;
|
|
557
|
+
ctx.securityWarning = prevWarning;
|
|
558
|
+
throw lambdaErr;
|
|
559
|
+
}
|
|
437
560
|
}, {
|
|
438
561
|
cleanWorkingSet: true,
|
|
439
562
|
verifyHash: true,
|
|
@@ -481,6 +604,7 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
481
604
|
perf: ctx.timer?.summaryV2()
|
|
482
605
|
});
|
|
483
606
|
} catch (innerErr) {
|
|
607
|
+
await attachSecurityStrikeToError(err, Object.assign({}, ctx, { txn: void 0 }));
|
|
484
608
|
ctx.logger?.error("Failed to save invalid transaction to statedb", {
|
|
485
609
|
error: innerErr,
|
|
486
610
|
txError: error,
|
|
@@ -491,6 +615,7 @@ const createExecute = ({ filter, runAsLambda }) => {
|
|
|
491
615
|
throw error;
|
|
492
616
|
}
|
|
493
617
|
} else {
|
|
618
|
+
await attachSecurityStrikeToError(err, Object.assign({}, ctx, { txn: void 0 }));
|
|
494
619
|
ctx.timer?.setMeta({
|
|
495
620
|
txType: ctx.txType,
|
|
496
621
|
txSize: ctx.txSize,
|