@aztec/bot 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5a9928
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 +6 -5
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +26 -19
- package/dest/base_bot.d.ts +7 -7
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +21 -32
- package/dest/bot.d.ts +5 -4
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +7 -10
- package/dest/config.d.ts +47 -82
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +42 -15
- package/dest/cross_chain_bot.d.ts +56 -0
- package/dest/cross_chain_bot.d.ts.map +1 -0
- package/dest/cross_chain_bot.js +138 -0
- package/dest/factory.d.ts +34 -9
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +293 -169
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/interface.d.ts +2 -6
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +30 -7
- 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/runner.d.ts +4 -3
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +20 -2
- package/dest/store/bot_store.d.ts +30 -5
- package/dest/store/bot_store.d.ts.map +1 -1
- package/dest/store/bot_store.js +37 -6
- package/dest/store/index.d.ts +2 -2
- package/dest/store/index.d.ts.map +1 -1
- package/dest/utils.js +3 -3
- package/package.json +17 -14
- package/src/amm_bot.ts +28 -20
- package/src/base_bot.ts +16 -33
- package/src/bot.ts +11 -10
- package/src/config.ts +47 -18
- package/src/cross_chain_bot.ts +208 -0
- package/src/factory.ts +322 -177
- package/src/index.ts +1 -0
- package/src/interface.ts +7 -7
- package/src/l1_to_l2_seeding.ts +79 -0
- package/src/runner.ts +41 -5
- package/src/store/bot_store.ts +60 -5
- package/src/store/index.ts +1 -1
- package/src/utils.ts +3 -3
package/src/base_bot.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
2
|
-
import {
|
|
3
|
-
BatchCall,
|
|
4
|
-
ContractFunctionInteraction,
|
|
5
|
-
type SendInteractionOptions,
|
|
6
|
-
waitForProven,
|
|
7
|
-
} from '@aztec/aztec.js/contracts';
|
|
2
|
+
import type { SendInteractionOptions } from '@aztec/aztec.js/contracts';
|
|
8
3
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
9
4
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
10
|
-
import {
|
|
5
|
+
import { TxStatus } from '@aztec/aztec.js/tx';
|
|
6
|
+
import type { TxHash, TxReceipt } from '@aztec/aztec.js/tx';
|
|
11
7
|
import { Gas } from '@aztec/stdlib/gas';
|
|
12
8
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
13
|
-
import type {
|
|
9
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
14
10
|
|
|
15
11
|
import type { BotConfig } from './config.js';
|
|
16
12
|
|
|
@@ -22,15 +18,15 @@ export abstract class BaseBot {
|
|
|
22
18
|
|
|
23
19
|
protected constructor(
|
|
24
20
|
public readonly node: AztecNode,
|
|
25
|
-
public readonly wallet:
|
|
21
|
+
public readonly wallet: EmbeddedWallet,
|
|
26
22
|
public readonly defaultAccountAddress: AztecAddress,
|
|
27
23
|
public config: BotConfig,
|
|
28
24
|
) {}
|
|
29
25
|
|
|
30
26
|
public async run(): Promise<TxReceipt | TxHash> {
|
|
31
27
|
this.attempts++;
|
|
32
|
-
const logCtx = { runId: Date.now() * 1000 + Math.floor(Math.random() * 1000) };
|
|
33
28
|
const { followChain, txMinedWaitSeconds } = this.config;
|
|
29
|
+
const logCtx = { runId: Date.now() * 1000 + Math.floor(Math.random() * 1000), followChain, txMinedWaitSeconds };
|
|
34
30
|
|
|
35
31
|
this.log.verbose(`Creating tx`, logCtx);
|
|
36
32
|
const txHash = await this.createAndSendTx(logCtx);
|
|
@@ -40,14 +36,9 @@ export abstract class BaseBot {
|
|
|
40
36
|
return txHash;
|
|
41
37
|
}
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
);
|
|
47
|
-
const receipt = await waitForTx(this.node, txHash, { timeout: txMinedWaitSeconds });
|
|
48
|
-
if (followChain === 'PROVEN') {
|
|
49
|
-
await waitForProven(this.node, receipt, { provenTimeout: txMinedWaitSeconds });
|
|
50
|
-
}
|
|
39
|
+
const waitForStatus = TxStatus[followChain];
|
|
40
|
+
this.log.verbose(`Awaiting tx ${txHash.toString()} to be on the ${followChain} chain`, logCtx);
|
|
41
|
+
const receipt = await waitForTx(this.node, txHash, { timeout: txMinedWaitSeconds, waitForStatus });
|
|
51
42
|
this.successes++;
|
|
52
43
|
this.log.info(
|
|
53
44
|
`Tx #${this.attempts} ${receipt.txHash} successfully mined in block ${receipt.blockNumber} (stats: ${this.successes}/${this.attempts} success)`,
|
|
@@ -66,27 +57,19 @@ export abstract class BaseBot {
|
|
|
66
57
|
return Promise.resolve();
|
|
67
58
|
}
|
|
68
59
|
|
|
69
|
-
protected
|
|
70
|
-
interaction: ContractFunctionInteraction | BatchCall,
|
|
71
|
-
): Promise<SendInteractionOptions> {
|
|
60
|
+
protected getSendMethodOpts(): SendInteractionOptions {
|
|
72
61
|
const { l2GasLimit, daGasLimit, minFeePadding } = this.config;
|
|
73
62
|
|
|
74
63
|
this.wallet.setMinFeePadding(minFeePadding);
|
|
75
64
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.log.verbose(`Estimating gas for transaction`);
|
|
82
|
-
({ estimatedGas: gasSettings } = await interaction.simulate({
|
|
83
|
-
fee: { estimateGas: true },
|
|
84
|
-
from: this.defaultAccountAddress,
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
65
|
+
const gasSettings =
|
|
66
|
+
l2GasLimit !== undefined && l2GasLimit > 0 && daGasLimit !== undefined && daGasLimit > 0
|
|
67
|
+
? { gasLimits: Gas.from({ l2Gas: l2GasLimit, daGas: daGasLimit }) }
|
|
68
|
+
: undefined;
|
|
69
|
+
|
|
87
70
|
return {
|
|
88
71
|
from: this.defaultAccountAddress,
|
|
89
|
-
fee: { gasSettings },
|
|
72
|
+
...(gasSettings ? { fee: { gasSettings } } : {}),
|
|
90
73
|
};
|
|
91
74
|
}
|
|
92
75
|
}
|
package/src/bot.ts
CHANGED
|
@@ -4,8 +4,9 @@ import { TxHash } from '@aztec/aztec.js/tx';
|
|
|
4
4
|
import { times } from '@aztec/foundation/collection';
|
|
5
5
|
import type { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
6
6
|
import type { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
7
|
+
import type { BlockTag } from '@aztec/stdlib/block';
|
|
7
8
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
8
|
-
import type {
|
|
9
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
9
10
|
|
|
10
11
|
import { BaseBot } from './base_bot.js';
|
|
11
12
|
import type { BotConfig } from './config.js';
|
|
@@ -18,7 +19,7 @@ const TRANSFER_AMOUNT = 1;
|
|
|
18
19
|
export class Bot extends BaseBot {
|
|
19
20
|
protected constructor(
|
|
20
21
|
node: AztecNode,
|
|
21
|
-
wallet:
|
|
22
|
+
wallet: EmbeddedWallet,
|
|
22
23
|
defaultAccountAddress: AztecAddress,
|
|
23
24
|
public readonly token: TokenContract | PrivateTokenContract,
|
|
24
25
|
public readonly recipient: AztecAddress,
|
|
@@ -29,10 +30,11 @@ export class Bot extends BaseBot {
|
|
|
29
30
|
|
|
30
31
|
static async create(
|
|
31
32
|
config: BotConfig,
|
|
32
|
-
wallet:
|
|
33
|
+
wallet: EmbeddedWallet,
|
|
33
34
|
aztecNode: AztecNode,
|
|
34
35
|
aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
35
36
|
store: BotStore,
|
|
37
|
+
syncChainTip?: BlockTag,
|
|
36
38
|
): Promise<Bot> {
|
|
37
39
|
const { defaultAccountAddress, token, recipient } = await new BotFactory(
|
|
38
40
|
config,
|
|
@@ -40,6 +42,7 @@ export class Bot extends BaseBot {
|
|
|
40
42
|
store,
|
|
41
43
|
aztecNode,
|
|
42
44
|
aztecNodeAdmin,
|
|
45
|
+
syncChainTip,
|
|
43
46
|
).setup();
|
|
44
47
|
return new Bot(aztecNode, wallet, defaultAccountAddress, token, recipient, config);
|
|
45
48
|
}
|
|
@@ -70,20 +73,18 @@ export class Bot extends BaseBot {
|
|
|
70
73
|
);
|
|
71
74
|
|
|
72
75
|
const batch = new BatchCall(wallet, calls);
|
|
73
|
-
const opts =
|
|
74
|
-
|
|
75
|
-
this.log.verbose(`Simulating transaction with ${calls.length}`, logCtx);
|
|
76
|
-
await batch.simulate({ from: this.defaultAccountAddress });
|
|
76
|
+
const opts = this.getSendMethodOpts();
|
|
77
77
|
|
|
78
78
|
this.log.verbose(`Sending transaction`, logCtx);
|
|
79
|
-
|
|
79
|
+
const { txHash } = await batch.send({ ...opts, wait: NO_WAIT });
|
|
80
|
+
return txHash;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
public async getBalances() {
|
|
83
84
|
if (isStandardTokenContract(this.token)) {
|
|
84
85
|
return {
|
|
85
86
|
sender: await getBalances(this.token, this.defaultAccountAddress),
|
|
86
|
-
recipient: await getBalances(this.token, this.recipient
|
|
87
|
+
recipient: await getBalances(this.token, this.recipient),
|
|
87
88
|
};
|
|
88
89
|
} else {
|
|
89
90
|
return {
|
|
@@ -92,7 +93,7 @@ export class Bot extends BaseBot {
|
|
|
92
93
|
publicBalance: 0n,
|
|
93
94
|
},
|
|
94
95
|
recipient: {
|
|
95
|
-
privateBalance: await getPrivateBalance(this.token, this.recipient
|
|
96
|
+
privateBalance: await getPrivateBalance(this.token, this.recipient),
|
|
96
97
|
publicBalance: 0n,
|
|
97
98
|
},
|
|
98
99
|
};
|
package/src/config.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
type ConfigMappingsType,
|
|
3
3
|
SecretValue,
|
|
4
4
|
booleanConfigHelper,
|
|
5
|
+
floatConfigHelper,
|
|
5
6
|
getConfigFromMappings,
|
|
6
7
|
getDefaultConfig,
|
|
7
8
|
numberConfigHelper,
|
|
@@ -11,17 +12,20 @@ import {
|
|
|
11
12
|
secretStringConfigHelper,
|
|
12
13
|
} from '@aztec/foundation/config';
|
|
13
14
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
14
|
-
import { type DataStoreConfig, dataConfigMappings } from '@aztec/kv-store/config';
|
|
15
15
|
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
16
16
|
import { protocolContractsHash } from '@aztec/protocol-contracts';
|
|
17
|
+
import { type DataStoreConfig, dataConfigMappings } from '@aztec/stdlib/kv-store';
|
|
17
18
|
import { schemas, zodFor } from '@aztec/stdlib/schemas';
|
|
18
19
|
import type { ComponentsVersions } from '@aztec/stdlib/versioning';
|
|
19
20
|
|
|
20
21
|
import { z } from 'zod';
|
|
21
22
|
|
|
22
|
-
const BotFollowChain = ['NONE', '
|
|
23
|
+
const BotFollowChain = ['NONE', 'PROPOSED', 'CHECKPOINTED', 'PROVEN'] as const;
|
|
23
24
|
type BotFollowChain = (typeof BotFollowChain)[number];
|
|
24
25
|
|
|
26
|
+
const BotMode = ['transfer', 'amm', 'crosschain'] as const;
|
|
27
|
+
type BotMode = (typeof BotMode)[number];
|
|
28
|
+
|
|
25
29
|
export enum SupportedTokenContracts {
|
|
26
30
|
TokenContract = 'TokenContract',
|
|
27
31
|
PrivateTokenContract = 'PrivateTokenContract',
|
|
@@ -66,9 +70,9 @@ export type BotConfig = {
|
|
|
66
70
|
maxPendingTxs: number;
|
|
67
71
|
/** Whether to flush after sending each 'setup' transaction */
|
|
68
72
|
flushSetupTransactions: boolean;
|
|
69
|
-
/** L2 gas limit for the tx (empty to
|
|
73
|
+
/** L2 gas limit for the tx (empty to let the bot's wallet estimate). */
|
|
70
74
|
l2GasLimit: number | undefined;
|
|
71
|
-
/** DA gas limit for the tx (empty to
|
|
75
|
+
/** DA gas limit for the tx (empty to let the bot's wallet estimate). */
|
|
72
76
|
daGasLimit: number | undefined;
|
|
73
77
|
/** Token contract to use */
|
|
74
78
|
contract: SupportedTokenContracts;
|
|
@@ -76,8 +80,12 @@ export type BotConfig = {
|
|
|
76
80
|
maxConsecutiveErrors: number;
|
|
77
81
|
/** Stops the bot if service becomes unhealthy */
|
|
78
82
|
stopWhenUnhealthy: boolean;
|
|
79
|
-
/**
|
|
80
|
-
|
|
83
|
+
/** Bot mode: transfer, amm, or crosschain. */
|
|
84
|
+
botMode: BotMode;
|
|
85
|
+
/** Number of L2→L1 messages per tx (crosschain mode). */
|
|
86
|
+
l2ToL1MessagesPerTx: number;
|
|
87
|
+
/** Max L1→L2 messages to keep in-flight (crosschain mode). */
|
|
88
|
+
l1ToL2SeedCount: number;
|
|
81
89
|
} & Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb'>;
|
|
82
90
|
|
|
83
91
|
export const BotConfigSchema = zodFor<BotConfig>()(
|
|
@@ -96,7 +104,7 @@ export const BotConfigSchema = zodFor<BotConfig>()(
|
|
|
96
104
|
privateTransfersPerTx: z.number().int().nonnegative(),
|
|
97
105
|
publicTransfersPerTx: z.number().int().nonnegative(),
|
|
98
106
|
feePaymentMethod: z.literal('fee_juice'),
|
|
99
|
-
minFeePadding: z.number().
|
|
107
|
+
minFeePadding: z.number().nonnegative(),
|
|
100
108
|
noStart: z.boolean(),
|
|
101
109
|
txMinedWaitSeconds: z.number(),
|
|
102
110
|
followChain: z.enum(BotFollowChain),
|
|
@@ -107,7 +115,9 @@ export const BotConfigSchema = zodFor<BotConfig>()(
|
|
|
107
115
|
contract: z.nativeEnum(SupportedTokenContracts),
|
|
108
116
|
maxConsecutiveErrors: z.number().int().nonnegative(),
|
|
109
117
|
stopWhenUnhealthy: z.boolean(),
|
|
110
|
-
|
|
118
|
+
botMode: z.enum(BotMode).default('transfer'),
|
|
119
|
+
l2ToL1MessagesPerTx: z.number().int().nonnegative().default(1),
|
|
120
|
+
l1ToL2SeedCount: z.number().int().nonnegative().default(1),
|
|
111
121
|
dataDirectory: z.string().optional(),
|
|
112
122
|
dataStoreMapSizeKb: z.number().optional(),
|
|
113
123
|
})
|
|
@@ -121,7 +131,6 @@ export const BotConfigSchema = zodFor<BotConfig>()(
|
|
|
121
131
|
l1Mnemonic: undefined,
|
|
122
132
|
l1PrivateKey: undefined,
|
|
123
133
|
senderPrivateKey: undefined,
|
|
124
|
-
dataDirectory: undefined,
|
|
125
134
|
dataStoreMapSizeKb: 1_024 * 1_024,
|
|
126
135
|
...config,
|
|
127
136
|
})),
|
|
@@ -196,7 +205,7 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
196
205
|
minFeePadding: {
|
|
197
206
|
env: 'BOT_MIN_FEE_PADDING',
|
|
198
207
|
description: 'How much is the bot willing to overpay vs. the current base fee',
|
|
199
|
-
...
|
|
208
|
+
...floatConfigHelper(3),
|
|
200
209
|
},
|
|
201
210
|
noStart: {
|
|
202
211
|
env: 'BOT_NO_START',
|
|
@@ -213,10 +222,14 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
213
222
|
description: 'Which chain the bot follows',
|
|
214
223
|
defaultValue: 'NONE',
|
|
215
224
|
parseEnv(val) {
|
|
216
|
-
|
|
225
|
+
const upper = val.toUpperCase();
|
|
226
|
+
if (upper === 'PENDING') {
|
|
227
|
+
return 'CHECKPOINTED';
|
|
228
|
+
}
|
|
229
|
+
if (!(BotFollowChain as readonly string[]).includes(upper)) {
|
|
217
230
|
throw new Error(`Invalid value for BOT_FOLLOW_CHAIN: ${val}`);
|
|
218
231
|
}
|
|
219
|
-
return
|
|
232
|
+
return upper as BotFollowChain;
|
|
220
233
|
},
|
|
221
234
|
},
|
|
222
235
|
maxPendingTxs: {
|
|
@@ -231,12 +244,12 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
231
244
|
},
|
|
232
245
|
l2GasLimit: {
|
|
233
246
|
env: 'BOT_L2_GAS_LIMIT',
|
|
234
|
-
description:
|
|
247
|
+
description: "L2 gas limit for the tx (empty to let the bot's wallet estimate).",
|
|
235
248
|
...optionalNumberConfigHelper(),
|
|
236
249
|
},
|
|
237
250
|
daGasLimit: {
|
|
238
251
|
env: 'BOT_DA_GAS_LIMIT',
|
|
239
|
-
description:
|
|
252
|
+
description: "DA gas limit for the tx (empty to let the bot's wallet estimate).",
|
|
240
253
|
...optionalNumberConfigHelper(),
|
|
241
254
|
},
|
|
242
255
|
contract: {
|
|
@@ -264,10 +277,26 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
264
277
|
description: 'Stops the bot if service becomes unhealthy',
|
|
265
278
|
...booleanConfigHelper(false),
|
|
266
279
|
},
|
|
267
|
-
|
|
268
|
-
env: '
|
|
269
|
-
description: '
|
|
270
|
-
|
|
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),
|
|
271
300
|
},
|
|
272
301
|
...pickConfigMappings(dataConfigMappings, ['dataStoreMapSizeKb', 'dataDirectory']),
|
|
273
302
|
};
|
|
@@ -0,0 +1,208 @@
|
|
|
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 type { 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 { BlockTag } from '@aztec/stdlib/block';
|
|
35
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
36
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
37
|
+
|
|
38
|
+
import { BaseBot } from './base_bot.js';
|
|
39
|
+
import type { BotConfig } from './config.js';
|
|
40
|
+
import { BotFactory } from './factory.js';
|
|
41
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
42
|
+
import type { BotStore, PendingL1ToL2Message } from './store/index.js';
|
|
43
|
+
|
|
44
|
+
/** Stale message threshold: messages older than this are removed. */
|
|
45
|
+
const STALE_MESSAGE_THRESHOLD_MS = 2 * 60 * 60 * 1000; // 2 hours
|
|
46
|
+
|
|
47
|
+
/** Bot that exercises both L2→L1 and L1→L2 cross-chain messaging. */
|
|
48
|
+
export class CrossChainBot extends BaseBot {
|
|
49
|
+
private l2ToL1Sent = 0;
|
|
50
|
+
private l1ToL2Consumed = 0;
|
|
51
|
+
private pendingSeedPromise: Promise<void> | undefined;
|
|
52
|
+
|
|
53
|
+
protected constructor(
|
|
54
|
+
node: AztecNode,
|
|
55
|
+
wallet: EmbeddedWallet,
|
|
56
|
+
defaultAccountAddress: AztecAddress,
|
|
57
|
+
private readonly contract: TestContract,
|
|
58
|
+
private readonly l1Client: ExtendedViemWalletClient,
|
|
59
|
+
private readonly l1Recipient: EthAddress,
|
|
60
|
+
private readonly inboxAddress: EthAddress,
|
|
61
|
+
private readonly rollupVersion: bigint,
|
|
62
|
+
private readonly store: BotStore,
|
|
63
|
+
config: BotConfig,
|
|
64
|
+
private readonly syncChainTip?: BlockTag,
|
|
65
|
+
) {
|
|
66
|
+
super(node, wallet, defaultAccountAddress, config);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static async create(
|
|
70
|
+
config: BotConfig,
|
|
71
|
+
wallet: EmbeddedWallet,
|
|
72
|
+
aztecNode: AztecNode,
|
|
73
|
+
aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
74
|
+
store: BotStore,
|
|
75
|
+
syncChainTip?: BlockTag,
|
|
76
|
+
): Promise<CrossChainBot> {
|
|
77
|
+
if (config.followChain === 'NONE') {
|
|
78
|
+
throw new Error(`CrossChainBot requires followChain to be set (got NONE)`);
|
|
79
|
+
}
|
|
80
|
+
const factory = new BotFactory(config, wallet, store, aztecNode, aztecNodeAdmin, syncChainTip);
|
|
81
|
+
const { defaultAccountAddress, contract, l1Client, rollupVersion } = await factory.setupCrossChain();
|
|
82
|
+
const l1Recipient = EthAddress.fromString(l1Client.account!.address);
|
|
83
|
+
const { l1ContractAddresses } = await aztecNode.getNodeInfo();
|
|
84
|
+
const inboxAddress = EthAddress.fromString(l1ContractAddresses.inboxAddress.toString());
|
|
85
|
+
return new CrossChainBot(
|
|
86
|
+
aztecNode,
|
|
87
|
+
wallet,
|
|
88
|
+
defaultAccountAddress,
|
|
89
|
+
contract,
|
|
90
|
+
l1Client,
|
|
91
|
+
l1Recipient,
|
|
92
|
+
inboxAddress,
|
|
93
|
+
rollupVersion,
|
|
94
|
+
store,
|
|
95
|
+
config,
|
|
96
|
+
syncChainTip,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
protected async createAndSendTx(logCtx: object): Promise<TxHash> {
|
|
101
|
+
const pendingMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
102
|
+
|
|
103
|
+
// Send an L1→L2 message if we're below the threshold and not already seeding one
|
|
104
|
+
if (pendingMessages.length < this.config.l1ToL2SeedCount && !this.pendingSeedPromise) {
|
|
105
|
+
this.pendingSeedPromise = this.seedNewL1ToL2Message()
|
|
106
|
+
.catch(err => this.log.warn(`Failed to seed L1→L2 message: ${err}`, logCtx))
|
|
107
|
+
.finally(() => {
|
|
108
|
+
this.pendingSeedPromise = undefined;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Build batch: always L2→L1, optionally consume L1→L2
|
|
113
|
+
const calls = [];
|
|
114
|
+
|
|
115
|
+
// L2→L1: create messages with random content
|
|
116
|
+
for (let i = 0; i < this.config.l2ToL1MessagesPerTx; i++) {
|
|
117
|
+
calls.push(
|
|
118
|
+
this.contract.methods.create_l2_to_l1_message_arbitrary_recipient_public(Fr.random(), this.l1Recipient),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// L1→L2: consume oldest ready message if available
|
|
123
|
+
const readyMsg = await this.getReadyL1ToL2Message(pendingMessages);
|
|
124
|
+
if (readyMsg) {
|
|
125
|
+
calls.push(
|
|
126
|
+
this.contract.methods.consume_message_from_arbitrary_sender_public(
|
|
127
|
+
Fr.fromHexString(readyMsg.content),
|
|
128
|
+
Fr.fromHexString(readyMsg.secret),
|
|
129
|
+
EthAddress.fromString(readyMsg.sender),
|
|
130
|
+
new Fr(BigInt(readyMsg.globalLeafIndex)),
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
// Delete consumed message immediately so it works with FOLLOW_CHAIN=NONE
|
|
134
|
+
await this.store.deleteL1ToL2Message(readyMsg.msgHash);
|
|
135
|
+
this.l1ToL2Consumed++;
|
|
136
|
+
} else {
|
|
137
|
+
this.log.warn(`No ready L1→L2 message to consume`, {
|
|
138
|
+
...logCtx,
|
|
139
|
+
pendingCount: pendingMessages.length,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const batch = new BatchCall(this.wallet, calls);
|
|
144
|
+
const opts = this.getSendMethodOpts();
|
|
145
|
+
|
|
146
|
+
this.log.verbose(`Sending cross-chain batch with ${calls.length} calls`, logCtx);
|
|
147
|
+
const { txHash } = await batch.send({ ...opts, wait: NO_WAIT });
|
|
148
|
+
return txHash;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected override async onTxMined(receipt: TxReceipt, logCtx: object): Promise<void> {
|
|
152
|
+
// Verify L2→L1 messages appeared in this tx's effects
|
|
153
|
+
const minedReceipt = await this.node.getTxReceipt(receipt.txHash, { includeTxEffect: true });
|
|
154
|
+
const l2ToL1MsgsRaw = minedReceipt.txEffect?.l2ToL1Msgs;
|
|
155
|
+
if (l2ToL1MsgsRaw) {
|
|
156
|
+
const l2ToL1Msgs = l2ToL1MsgsRaw.filter(m => !m.isZero());
|
|
157
|
+
if (l2ToL1Msgs.length >= this.config.l2ToL1MessagesPerTx) {
|
|
158
|
+
this.l2ToL1Sent += l2ToL1Msgs.length;
|
|
159
|
+
} else {
|
|
160
|
+
this.log.error(`Expected ${this.config.l2ToL1MessagesPerTx} L2→L1 messages but found ${l2ToL1Msgs.length}`, {
|
|
161
|
+
...logCtx,
|
|
162
|
+
blockNumber: receipt.blockNumber,
|
|
163
|
+
txHash: receipt.txHash.toString(),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const pendingCount = (await this.store.getUnconsumedL1ToL2Messages()).length;
|
|
169
|
+
this.log.info(`CrossChainBot txs mined`, {
|
|
170
|
+
...logCtx,
|
|
171
|
+
l2ToL1Sent: this.l2ToL1Sent,
|
|
172
|
+
l1ToL2Consumed: this.l1ToL2Consumed,
|
|
173
|
+
l1ToL2Pending: pendingCount,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Finds the oldest pending message that is ready for consumption. */
|
|
178
|
+
private async getReadyL1ToL2Message(
|
|
179
|
+
pendingMessages: PendingL1ToL2Message[],
|
|
180
|
+
): Promise<PendingL1ToL2Message | undefined> {
|
|
181
|
+
const now = Date.now();
|
|
182
|
+
for (const msg of pendingMessages) {
|
|
183
|
+
const ready = await isL1ToL2MessageReady(this.node, Fr.fromHexString(msg.msgHash), this.syncChainTip);
|
|
184
|
+
if (ready) {
|
|
185
|
+
return msg;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Time-based stale detection: if the message is old and still not ready, remove it
|
|
189
|
+
if (now - msg.timestamp > STALE_MESSAGE_THRESHOLD_MS) {
|
|
190
|
+
await this.store.deleteL1ToL2Message(msg.msgHash);
|
|
191
|
+
this.log.warn(`Removed stale L1→L2 message ${msg.msgHash}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Seeds a new L1→L2 message on L1 and stores it. */
|
|
198
|
+
private async seedNewL1ToL2Message(): Promise<void> {
|
|
199
|
+
await seedL1ToL2Message(
|
|
200
|
+
this.l1Client,
|
|
201
|
+
this.inboxAddress,
|
|
202
|
+
this.contract.address,
|
|
203
|
+
this.rollupVersion,
|
|
204
|
+
this.store,
|
|
205
|
+
this.log,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|