@aztec/bot 0.0.1-fake-ceab37513c → 0.0.2-commit.217f559981
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/dest/amm_bot.d.ts +9 -10
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +27 -20
- package/dest/base_bot.d.ts +12 -9
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +28 -29
- package/dest/bot.d.ts +9 -10
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +12 -10
- package/dest/config.d.ts +82 -63
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +61 -31
- package/dest/cross_chain_bot.d.ts +54 -0
- package/dest/cross_chain_bot.d.ts.map +1 -0
- package/dest/cross_chain_bot.js +140 -0
- package/dest/factory.d.ts +27 -34
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +271 -151
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -0
- package/dest/interface.d.ts +2 -2
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +1 -1
- package/dest/l1_to_l2_seeding.d.ts +8 -0
- package/dest/l1_to_l2_seeding.d.ts.map +1 -0
- package/dest/l1_to_l2_seeding.js +63 -0
- package/dest/rpc.d.ts +1 -1
- package/dest/runner.d.ts +12 -13
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +445 -61
- package/dest/store/bot_store.d.ts +69 -0
- package/dest/store/bot_store.d.ts.map +1 -0
- package/dest/store/bot_store.js +138 -0
- package/dest/store/index.d.ts +2 -0
- package/dest/store/index.d.ts.map +1 -0
- package/dest/store/index.js +1 -0
- package/dest/utils.d.ts +4 -4
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +5 -5
- package/package.json +19 -13
- package/src/amm_bot.ts +40 -32
- package/src/base_bot.ts +27 -39
- package/src/bot.ts +25 -13
- package/src/config.ts +101 -72
- package/src/cross_chain_bot.ts +209 -0
- package/src/factory.ts +313 -177
- package/src/index.ts +2 -0
- package/src/interface.ts +1 -1
- package/src/l1_to_l2_seeding.ts +79 -0
- package/src/runner.ts +33 -24
- package/src/store/bot_store.ts +196 -0
- package/src/store/index.ts +1 -0
- package/src/utils.ts +10 -5
package/src/config.ts
CHANGED
|
@@ -6,20 +6,25 @@ import {
|
|
|
6
6
|
getDefaultConfig,
|
|
7
7
|
numberConfigHelper,
|
|
8
8
|
optionalNumberConfigHelper,
|
|
9
|
+
pickConfigMappings,
|
|
9
10
|
secretFrConfigHelper,
|
|
10
11
|
secretStringConfigHelper,
|
|
11
12
|
} from '@aztec/foundation/config';
|
|
12
|
-
import { Fr } from '@aztec/foundation/
|
|
13
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
14
|
+
import { type DataStoreConfig, dataConfigMappings } from '@aztec/kv-store/config';
|
|
13
15
|
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
+
import { protocolContractsHash } from '@aztec/protocol-contracts';
|
|
17
|
+
import { schemas, zodFor } from '@aztec/stdlib/schemas';
|
|
16
18
|
import type { ComponentsVersions } from '@aztec/stdlib/versioning';
|
|
17
19
|
|
|
18
20
|
import { z } from 'zod';
|
|
19
21
|
|
|
20
|
-
const BotFollowChain = ['NONE', '
|
|
22
|
+
const BotFollowChain = ['NONE', 'PROPOSED', 'CHECKPOINTED', 'PROVEN'] as const;
|
|
21
23
|
type BotFollowChain = (typeof BotFollowChain)[number];
|
|
22
24
|
|
|
25
|
+
const BotMode = ['transfer', 'amm', 'crosschain'] as const;
|
|
26
|
+
type BotMode = (typeof BotMode)[number];
|
|
27
|
+
|
|
23
28
|
export enum SupportedTokenContracts {
|
|
24
29
|
TokenContract = 'TokenContract',
|
|
25
30
|
PrivateTokenContract = 'PrivateTokenContract',
|
|
@@ -30,8 +35,6 @@ export type BotConfig = {
|
|
|
30
35
|
nodeUrl: string | undefined;
|
|
31
36
|
/** The URL to the Aztec node admin API to force-flush txs if configured. */
|
|
32
37
|
nodeAdminUrl: string | undefined;
|
|
33
|
-
/** URL to the PXE for sending txs, or undefined if an in-proc PXE is used. */
|
|
34
|
-
pxeUrl: string | undefined;
|
|
35
38
|
/** Url of the ethereum host. */
|
|
36
39
|
l1RpcUrls: string[] | undefined;
|
|
37
40
|
/** The mnemonic for the account to bridge fee juice from L1. */
|
|
@@ -44,8 +47,6 @@ export type BotConfig = {
|
|
|
44
47
|
senderPrivateKey: SecretValue<Fr> | undefined;
|
|
45
48
|
/** Optional salt to use to instantiate the sender account */
|
|
46
49
|
senderSalt: Fr | undefined;
|
|
47
|
-
/** Encryption secret for a recipient account. */
|
|
48
|
-
recipientEncryptionSecret: SecretValue<Fr>;
|
|
49
50
|
/** Salt for the token contract instantiation. */
|
|
50
51
|
tokenSalt: Fr;
|
|
51
52
|
/** Every how many seconds should a new tx be sent. */
|
|
@@ -56,6 +57,8 @@ export type BotConfig = {
|
|
|
56
57
|
publicTransfersPerTx: number;
|
|
57
58
|
/** How to handle fee payments. */
|
|
58
59
|
feePaymentMethod: 'fee_juice';
|
|
60
|
+
/** 'How much is the bot willing to overpay vs. the current min fee' */
|
|
61
|
+
minFeePadding: number;
|
|
59
62
|
/** True to not automatically setup or start the bot on initialization. */
|
|
60
63
|
noStart: boolean;
|
|
61
64
|
/** How long to wait for a tx to be mined before reporting an error. */
|
|
@@ -76,52 +79,62 @@ export type BotConfig = {
|
|
|
76
79
|
maxConsecutiveErrors: number;
|
|
77
80
|
/** Stops the bot if service becomes unhealthy */
|
|
78
81
|
stopWhenUnhealthy: boolean;
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
/** Bot mode: transfer, amm, or crosschain. */
|
|
83
|
+
botMode: BotMode;
|
|
84
|
+
/** Number of L2→L1 messages per tx (crosschain mode). */
|
|
85
|
+
l2ToL1MessagesPerTx: number;
|
|
86
|
+
/** Max L1→L2 messages to keep in-flight (crosschain mode). */
|
|
87
|
+
l1ToL2SeedCount: number;
|
|
88
|
+
} & Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb'>;
|
|
82
89
|
|
|
83
|
-
export const BotConfigSchema =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
90
|
+
export const BotConfigSchema = zodFor<BotConfig>()(
|
|
91
|
+
z
|
|
92
|
+
.object({
|
|
93
|
+
nodeUrl: z.string().optional(),
|
|
94
|
+
nodeAdminUrl: z.string().optional(),
|
|
95
|
+
l1RpcUrls: z.array(z.string()).optional(),
|
|
96
|
+
l1Mnemonic: schemas.SecretValue(z.string()).optional(),
|
|
97
|
+
l1PrivateKey: schemas.SecretValue(z.string()).optional(),
|
|
98
|
+
l1ToL2MessageTimeoutSeconds: z.number(),
|
|
99
|
+
senderPrivateKey: schemas.SecretValue(schemas.Fr).optional(),
|
|
100
|
+
senderSalt: schemas.Fr.optional(),
|
|
101
|
+
tokenSalt: schemas.Fr,
|
|
102
|
+
txIntervalSeconds: z.number(),
|
|
103
|
+
privateTransfersPerTx: z.number().int().nonnegative(),
|
|
104
|
+
publicTransfersPerTx: z.number().int().nonnegative(),
|
|
105
|
+
feePaymentMethod: z.literal('fee_juice'),
|
|
106
|
+
minFeePadding: z.number().int().nonnegative(),
|
|
107
|
+
noStart: z.boolean(),
|
|
108
|
+
txMinedWaitSeconds: z.number(),
|
|
109
|
+
followChain: z.enum(BotFollowChain),
|
|
110
|
+
maxPendingTxs: z.number().int().nonnegative(),
|
|
111
|
+
flushSetupTransactions: z.boolean(),
|
|
112
|
+
l2GasLimit: z.number().int().nonnegative().optional(),
|
|
113
|
+
daGasLimit: z.number().int().nonnegative().optional(),
|
|
114
|
+
contract: z.nativeEnum(SupportedTokenContracts),
|
|
115
|
+
maxConsecutiveErrors: z.number().int().nonnegative(),
|
|
116
|
+
stopWhenUnhealthy: z.boolean(),
|
|
117
|
+
botMode: z.enum(BotMode).default('transfer'),
|
|
118
|
+
l2ToL1MessagesPerTx: z.number().int().nonnegative().default(1),
|
|
119
|
+
l1ToL2SeedCount: z.number().int().nonnegative().default(1),
|
|
120
|
+
dataDirectory: z.string().optional(),
|
|
121
|
+
dataStoreMapSizeKb: z.number().optional(),
|
|
122
|
+
})
|
|
123
|
+
.transform(config => ({
|
|
124
|
+
nodeUrl: undefined,
|
|
125
|
+
nodeAdminUrl: undefined,
|
|
126
|
+
l1RpcUrls: undefined,
|
|
127
|
+
senderSalt: undefined,
|
|
128
|
+
l2GasLimit: undefined,
|
|
129
|
+
daGasLimit: undefined,
|
|
130
|
+
l1Mnemonic: undefined,
|
|
131
|
+
l1PrivateKey: undefined,
|
|
132
|
+
senderPrivateKey: undefined,
|
|
133
|
+
dataDirectory: undefined,
|
|
134
|
+
dataStoreMapSizeKb: 1_024 * 1_024,
|
|
135
|
+
...config,
|
|
136
|
+
})),
|
|
137
|
+
);
|
|
125
138
|
|
|
126
139
|
export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
127
140
|
nodeUrl: {
|
|
@@ -132,10 +145,6 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
132
145
|
env: 'AZTEC_NODE_ADMIN_URL',
|
|
133
146
|
description: 'The URL to the Aztec node admin API to force-flush txs if configured.',
|
|
134
147
|
},
|
|
135
|
-
pxeUrl: {
|
|
136
|
-
env: 'BOT_PXE_URL',
|
|
137
|
-
description: 'URL to the PXE for sending txs, or undefined if an in-proc PXE is used.',
|
|
138
|
-
},
|
|
139
148
|
l1RpcUrls: {
|
|
140
149
|
env: 'ETHEREUM_HOSTS',
|
|
141
150
|
description: 'URL of the ethereum host.',
|
|
@@ -163,18 +172,12 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
163
172
|
},
|
|
164
173
|
senderSalt: {
|
|
165
174
|
env: 'BOT_ACCOUNT_SALT',
|
|
166
|
-
description: 'The salt to use to
|
|
175
|
+
description: 'The salt to use to deploy the sender account.',
|
|
167
176
|
parseEnv: (val: string) => (val ? Fr.fromHexString(val) : undefined),
|
|
168
177
|
},
|
|
169
|
-
recipientEncryptionSecret: {
|
|
170
|
-
env: 'BOT_RECIPIENT_ENCRYPTION_SECRET',
|
|
171
|
-
description: 'Encryption secret for a recipient account.',
|
|
172
|
-
printDefault: sv => sv?.getValue(),
|
|
173
|
-
...secretFrConfigHelper(Fr.fromHexString('0xcafecafe')),
|
|
174
|
-
},
|
|
175
178
|
tokenSalt: {
|
|
176
179
|
env: 'BOT_TOKEN_SALT',
|
|
177
|
-
description: '
|
|
180
|
+
description: 'The salt to use to deploy the token contract.',
|
|
178
181
|
parseEnv: (val: string) => Fr.fromHexString(val),
|
|
179
182
|
defaultValue: Fr.fromHexString('1'),
|
|
180
183
|
},
|
|
@@ -199,6 +202,11 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
199
202
|
parseEnv: val => (val as 'fee_juice') || undefined,
|
|
200
203
|
defaultValue: 'fee_juice',
|
|
201
204
|
},
|
|
205
|
+
minFeePadding: {
|
|
206
|
+
env: 'BOT_MIN_FEE_PADDING',
|
|
207
|
+
description: 'How much is the bot willing to overpay vs. the current base fee',
|
|
208
|
+
...numberConfigHelper(3),
|
|
209
|
+
},
|
|
202
210
|
noStart: {
|
|
203
211
|
env: 'BOT_NO_START',
|
|
204
212
|
description: 'True to not automatically setup or start the bot on initialization.',
|
|
@@ -214,10 +222,14 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
214
222
|
description: 'Which chain the bot follows',
|
|
215
223
|
defaultValue: 'NONE',
|
|
216
224
|
parseEnv(val) {
|
|
217
|
-
|
|
225
|
+
const upper = val.toUpperCase();
|
|
226
|
+
if (upper === 'PENDING') {
|
|
227
|
+
return 'CHECKPOINTED';
|
|
228
|
+
}
|
|
229
|
+
if (!(BotFollowChain as readonly string[]).includes(upper)) {
|
|
218
230
|
throw new Error(`Invalid value for BOT_FOLLOW_CHAIN: ${val}`);
|
|
219
231
|
}
|
|
220
|
-
return
|
|
232
|
+
return upper as BotFollowChain;
|
|
221
233
|
},
|
|
222
234
|
},
|
|
223
235
|
maxPendingTxs: {
|
|
@@ -265,11 +277,28 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
265
277
|
description: 'Stops the bot if service becomes unhealthy',
|
|
266
278
|
...booleanConfigHelper(false),
|
|
267
279
|
},
|
|
268
|
-
|
|
269
|
-
env: '
|
|
270
|
-
description: '
|
|
271
|
-
|
|
280
|
+
botMode: {
|
|
281
|
+
env: 'BOT_MODE',
|
|
282
|
+
description: 'Bot mode: transfer, amm, or crosschain',
|
|
283
|
+
defaultValue: 'transfer' as BotMode,
|
|
284
|
+
parseEnv(val: string) {
|
|
285
|
+
if (!(BotMode as readonly string[]).includes(val)) {
|
|
286
|
+
throw new Error(`Invalid value for BOT_MODE: ${val}`);
|
|
287
|
+
}
|
|
288
|
+
return val as BotMode;
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
l2ToL1MessagesPerTx: {
|
|
292
|
+
env: 'BOT_L2_TO_L1_MESSAGES_PER_TX',
|
|
293
|
+
description: 'Number of L2→L1 messages per tx (crosschain mode)',
|
|
294
|
+
...numberConfigHelper(1),
|
|
295
|
+
},
|
|
296
|
+
l1ToL2SeedCount: {
|
|
297
|
+
env: 'BOT_L1_TO_L2_SEED_COUNT',
|
|
298
|
+
description: 'Max L1→L2 messages to keep in-flight (crosschain mode)',
|
|
299
|
+
...numberConfigHelper(1),
|
|
272
300
|
},
|
|
301
|
+
...pickConfigMappings(dataConfigMappings, ['dataStoreMapSizeKb', 'dataDirectory']),
|
|
273
302
|
};
|
|
274
303
|
|
|
275
304
|
export function getBotConfigFromEnv(): BotConfig {
|
|
@@ -282,7 +311,7 @@ export function getBotDefaultConfig(): BotConfig {
|
|
|
282
311
|
|
|
283
312
|
export function getVersions(): Partial<ComponentsVersions> {
|
|
284
313
|
return {
|
|
285
|
-
|
|
314
|
+
l2ProtocolContractsHash: protocolContractsHash.toString(),
|
|
286
315
|
l2CircuitsVkTreeRoot: getVKTreeRoot().toString(),
|
|
287
316
|
};
|
|
288
317
|
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CrossChainBot exercises L2->L1 and L1->L2 messaging.
|
|
3
|
+
*
|
|
4
|
+
* createAndSendTx onTxMined
|
|
5
|
+
* ────────────────────────────────────── ──────────────────────────────
|
|
6
|
+
*
|
|
7
|
+
* 1. SEED (fire-and-forget) 3. VERIFY L2->L1
|
|
8
|
+
* if store has fewer pending messages Query getTxEffect, confirm
|
|
9
|
+
* than seedCount and no seed is the expected L2->L1 messages
|
|
10
|
+
* in-flight: appeared in tx effects.
|
|
11
|
+
* * kick off L1 inbox tx
|
|
12
|
+
* * store msg on completion
|
|
13
|
+
*
|
|
14
|
+
* 2. BUILD & SEND BATCH
|
|
15
|
+
* Always:
|
|
16
|
+
* N x create_l2_to_l1_message
|
|
17
|
+
* (random content, fixed
|
|
18
|
+
* L1 recipient)
|
|
19
|
+
* If a ready L1->L2 msg exists:
|
|
20
|
+
* 1 x consume_message_from_
|
|
21
|
+
* arbitrary_sender_public
|
|
22
|
+
* delete consumed msg from store
|
|
23
|
+
* Send batch tx (no wait)
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
27
|
+
import { BatchCall, NO_WAIT } from '@aztec/aztec.js/contracts';
|
|
28
|
+
import { isL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
29
|
+
import { TxHash, TxReceipt } from '@aztec/aztec.js/tx';
|
|
30
|
+
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
31
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
32
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
33
|
+
import type { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
34
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
35
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
36
|
+
|
|
37
|
+
import { BaseBot } from './base_bot.js';
|
|
38
|
+
import type { BotConfig } from './config.js';
|
|
39
|
+
import { BotFactory } from './factory.js';
|
|
40
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
41
|
+
import type { BotStore, PendingL1ToL2Message } from './store/index.js';
|
|
42
|
+
|
|
43
|
+
/** Stale message threshold: messages older than this are removed. */
|
|
44
|
+
const STALE_MESSAGE_THRESHOLD_MS = 2 * 60 * 60 * 1000; // 2 hours
|
|
45
|
+
|
|
46
|
+
/** Bot that exercises both L2→L1 and L1→L2 cross-chain messaging. */
|
|
47
|
+
export class CrossChainBot extends BaseBot {
|
|
48
|
+
private l2ToL1Sent = 0;
|
|
49
|
+
private l1ToL2Consumed = 0;
|
|
50
|
+
private pendingSeedPromise: Promise<void> | undefined;
|
|
51
|
+
|
|
52
|
+
protected constructor(
|
|
53
|
+
node: AztecNode,
|
|
54
|
+
wallet: EmbeddedWallet,
|
|
55
|
+
defaultAccountAddress: AztecAddress,
|
|
56
|
+
private readonly contract: TestContract,
|
|
57
|
+
private readonly l1Client: ExtendedViemWalletClient,
|
|
58
|
+
private readonly l1Recipient: EthAddress,
|
|
59
|
+
private readonly inboxAddress: EthAddress,
|
|
60
|
+
private readonly rollupVersion: bigint,
|
|
61
|
+
private readonly store: BotStore,
|
|
62
|
+
config: BotConfig,
|
|
63
|
+
) {
|
|
64
|
+
super(node, wallet, defaultAccountAddress, config);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static async create(
|
|
68
|
+
config: BotConfig,
|
|
69
|
+
wallet: EmbeddedWallet,
|
|
70
|
+
aztecNode: AztecNode,
|
|
71
|
+
aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
72
|
+
store: BotStore,
|
|
73
|
+
): Promise<CrossChainBot> {
|
|
74
|
+
if (config.followChain === 'NONE') {
|
|
75
|
+
throw new Error(`CrossChainBot requires followChain to be set (got NONE)`);
|
|
76
|
+
}
|
|
77
|
+
const factory = new BotFactory(config, wallet, store, aztecNode, aztecNodeAdmin);
|
|
78
|
+
const { defaultAccountAddress, contract, l1Client, rollupVersion } = await factory.setupCrossChain();
|
|
79
|
+
const l1Recipient = EthAddress.fromString(l1Client.account!.address);
|
|
80
|
+
const { l1ContractAddresses } = await aztecNode.getNodeInfo();
|
|
81
|
+
const inboxAddress = EthAddress.fromString(l1ContractAddresses.inboxAddress.toString());
|
|
82
|
+
return new CrossChainBot(
|
|
83
|
+
aztecNode,
|
|
84
|
+
wallet,
|
|
85
|
+
defaultAccountAddress,
|
|
86
|
+
contract,
|
|
87
|
+
l1Client,
|
|
88
|
+
l1Recipient,
|
|
89
|
+
inboxAddress,
|
|
90
|
+
rollupVersion,
|
|
91
|
+
store,
|
|
92
|
+
config,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected async createAndSendTx(logCtx: object): Promise<TxHash> {
|
|
97
|
+
const pendingMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
98
|
+
|
|
99
|
+
// Send an L1→L2 message if we're below the threshold and not already seeding one
|
|
100
|
+
if (pendingMessages.length < this.config.l1ToL2SeedCount && !this.pendingSeedPromise) {
|
|
101
|
+
this.pendingSeedPromise = this.seedNewL1ToL2Message()
|
|
102
|
+
.catch(err => this.log.warn(`Failed to seed L1→L2 message: ${err}`, logCtx))
|
|
103
|
+
.finally(() => {
|
|
104
|
+
this.pendingSeedPromise = undefined;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Build batch: always L2→L1, optionally consume L1→L2
|
|
109
|
+
const calls = [];
|
|
110
|
+
|
|
111
|
+
// L2→L1: create messages with random content
|
|
112
|
+
for (let i = 0; i < this.config.l2ToL1MessagesPerTx; i++) {
|
|
113
|
+
calls.push(
|
|
114
|
+
this.contract.methods.create_l2_to_l1_message_arbitrary_recipient_public(Fr.random(), this.l1Recipient),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// L1→L2: consume oldest ready message if available
|
|
119
|
+
const readyMsg = await this.getReadyL1ToL2Message(pendingMessages);
|
|
120
|
+
if (readyMsg) {
|
|
121
|
+
calls.push(
|
|
122
|
+
this.contract.methods.consume_message_from_arbitrary_sender_public(
|
|
123
|
+
Fr.fromHexString(readyMsg.content),
|
|
124
|
+
Fr.fromHexString(readyMsg.secret),
|
|
125
|
+
EthAddress.fromString(readyMsg.sender),
|
|
126
|
+
new Fr(BigInt(readyMsg.globalLeafIndex)),
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
// Delete consumed message immediately so it works with FOLLOW_CHAIN=NONE
|
|
130
|
+
await this.store.deleteL1ToL2Message(readyMsg.msgHash);
|
|
131
|
+
this.l1ToL2Consumed++;
|
|
132
|
+
} else {
|
|
133
|
+
this.log.warn(`No ready L1→L2 message to consume`, {
|
|
134
|
+
...logCtx,
|
|
135
|
+
pendingCount: pendingMessages.length,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const batch = new BatchCall(this.wallet, calls);
|
|
140
|
+
const opts = await this.getSendMethodOpts(batch);
|
|
141
|
+
|
|
142
|
+
this.log.verbose(`Sending cross-chain batch with ${calls.length} calls`, logCtx);
|
|
143
|
+
return batch.send({ ...opts, wait: NO_WAIT });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
protected override async onTxMined(receipt: TxReceipt, logCtx: object): Promise<void> {
|
|
147
|
+
// Verify L2→L1 messages appeared in this tx's effects
|
|
148
|
+
const indexed = await this.node.getTxEffect(receipt.txHash);
|
|
149
|
+
if (indexed) {
|
|
150
|
+
const l2ToL1Msgs = indexed.data.l2ToL1Msgs.filter(m => !m.isZero());
|
|
151
|
+
if (l2ToL1Msgs.length >= this.config.l2ToL1MessagesPerTx) {
|
|
152
|
+
this.l2ToL1Sent += l2ToL1Msgs.length;
|
|
153
|
+
} else {
|
|
154
|
+
this.log.error(`Expected ${this.config.l2ToL1MessagesPerTx} L2→L1 messages but found ${l2ToL1Msgs.length}`, {
|
|
155
|
+
...logCtx,
|
|
156
|
+
blockNumber: receipt.blockNumber,
|
|
157
|
+
txHash: receipt.txHash.toString(),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const pendingCount = (await this.store.getUnconsumedL1ToL2Messages()).length;
|
|
163
|
+
this.log.info(`CrossChainBot txs mined`, {
|
|
164
|
+
...logCtx,
|
|
165
|
+
l2ToL1Sent: this.l2ToL1Sent,
|
|
166
|
+
l1ToL2Consumed: this.l1ToL2Consumed,
|
|
167
|
+
l1ToL2Pending: pendingCount,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Finds the oldest pending message that is ready for consumption. */
|
|
172
|
+
private async getReadyL1ToL2Message(
|
|
173
|
+
pendingMessages: PendingL1ToL2Message[],
|
|
174
|
+
): Promise<PendingL1ToL2Message | undefined> {
|
|
175
|
+
const now = Date.now();
|
|
176
|
+
for (const msg of pendingMessages) {
|
|
177
|
+
const ready = await isL1ToL2MessageReady(this.node, Fr.fromHexString(msg.msgHash), {
|
|
178
|
+
// Use forPublicConsumption: false so we wait until blockNumber >= messageBlockNumber.
|
|
179
|
+
// With forPublicConsumption: true, the check returns true one block early (the sequencer
|
|
180
|
+
// includes L1→L2 messages before executing the block's txs), but gas estimation simulates
|
|
181
|
+
// against the current world state which doesn't yet have the message.
|
|
182
|
+
// See https://linear.app/aztec-labs/issue/A-548 for details.
|
|
183
|
+
forPublicConsumption: false,
|
|
184
|
+
});
|
|
185
|
+
if (ready) {
|
|
186
|
+
return msg;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Time-based stale detection: if the message is old and still not ready, remove it
|
|
190
|
+
if (now - msg.timestamp > STALE_MESSAGE_THRESHOLD_MS) {
|
|
191
|
+
await this.store.deleteL1ToL2Message(msg.msgHash);
|
|
192
|
+
this.log.warn(`Removed stale L1→L2 message ${msg.msgHash}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Seeds a new L1→L2 message on L1 and stores it. */
|
|
199
|
+
private async seedNewL1ToL2Message(): Promise<void> {
|
|
200
|
+
await seedL1ToL2Message(
|
|
201
|
+
this.l1Client,
|
|
202
|
+
this.inboxAddress,
|
|
203
|
+
this.contract.address,
|
|
204
|
+
this.rollupVersion,
|
|
205
|
+
this.store,
|
|
206
|
+
this.log,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|