@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-commit.0208eb9

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.
Files changed (135) hide show
  1. package/dest/client/index.d.ts +1 -1
  2. package/dest/client/sequencer-client.d.ts +31 -31
  3. package/dest/client/sequencer-client.d.ts.map +1 -1
  4. package/dest/client/sequencer-client.js +82 -60
  5. package/dest/config.d.ts +15 -16
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +118 -70
  8. package/dest/global_variable_builder/global_builder.d.ts +26 -15
  9. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  10. package/dest/global_variable_builder/global_builder.js +62 -44
  11. package/dest/global_variable_builder/index.d.ts +1 -1
  12. package/dest/index.d.ts +2 -4
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -3
  15. package/dest/publisher/config.d.ts +15 -12
  16. package/dest/publisher/config.d.ts.map +1 -1
  17. package/dest/publisher/config.js +32 -19
  18. package/dest/publisher/index.d.ts +3 -1
  19. package/dest/publisher/index.d.ts.map +1 -1
  20. package/dest/publisher/index.js +3 -0
  21. package/dest/publisher/sequencer-publisher-factory.d.ts +44 -0
  22. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -0
  23. package/dest/publisher/sequencer-publisher-factory.js +51 -0
  24. package/dest/publisher/sequencer-publisher-metrics.d.ts +5 -4
  25. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  26. package/dest/publisher/sequencer-publisher-metrics.js +34 -62
  27. package/dest/publisher/sequencer-publisher.d.ts +134 -88
  28. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  29. package/dest/publisher/sequencer-publisher.js +1172 -254
  30. package/dest/sequencer/checkpoint_proposal_job.d.ts +100 -0
  31. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_proposal_job.js +1188 -0
  33. package/dest/sequencer/checkpoint_voter.d.ts +35 -0
  34. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  35. package/dest/sequencer/checkpoint_voter.js +109 -0
  36. package/dest/sequencer/config.d.ts +7 -1
  37. package/dest/sequencer/config.d.ts.map +1 -1
  38. package/dest/sequencer/errors.d.ts +11 -0
  39. package/dest/sequencer/errors.d.ts.map +1 -0
  40. package/dest/sequencer/errors.js +15 -0
  41. package/dest/sequencer/events.d.ts +46 -0
  42. package/dest/sequencer/events.d.ts.map +1 -0
  43. package/dest/sequencer/events.js +1 -0
  44. package/dest/sequencer/index.d.ts +4 -2
  45. package/dest/sequencer/index.d.ts.map +1 -1
  46. package/dest/sequencer/index.js +3 -1
  47. package/dest/sequencer/metrics.d.ts +48 -12
  48. package/dest/sequencer/metrics.d.ts.map +1 -1
  49. package/dest/sequencer/metrics.js +213 -68
  50. package/dest/sequencer/sequencer.d.ts +146 -137
  51. package/dest/sequencer/sequencer.d.ts.map +1 -1
  52. package/dest/sequencer/sequencer.js +971 -525
  53. package/dest/sequencer/timetable.d.ts +76 -24
  54. package/dest/sequencer/timetable.d.ts.map +1 -1
  55. package/dest/sequencer/timetable.js +177 -61
  56. package/dest/sequencer/types.d.ts +3 -0
  57. package/dest/sequencer/types.d.ts.map +1 -0
  58. package/dest/sequencer/types.js +1 -0
  59. package/dest/sequencer/utils.d.ts +20 -38
  60. package/dest/sequencer/utils.d.ts.map +1 -1
  61. package/dest/sequencer/utils.js +12 -47
  62. package/dest/test/index.d.ts +9 -1
  63. package/dest/test/index.d.ts.map +1 -1
  64. package/dest/test/index.js +0 -4
  65. package/dest/test/mock_checkpoint_builder.d.ts +95 -0
  66. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  67. package/dest/test/mock_checkpoint_builder.js +220 -0
  68. package/dest/test/utils.d.ts +53 -0
  69. package/dest/test/utils.d.ts.map +1 -0
  70. package/dest/test/utils.js +103 -0
  71. package/package.json +47 -45
  72. package/src/client/sequencer-client.ts +106 -107
  73. package/src/config.ts +131 -81
  74. package/src/global_variable_builder/global_builder.ts +84 -55
  75. package/src/index.ts +1 -3
  76. package/src/publisher/config.ts +45 -32
  77. package/src/publisher/index.ts +4 -0
  78. package/src/publisher/sequencer-publisher-factory.ts +92 -0
  79. package/src/publisher/sequencer-publisher-metrics.ts +33 -63
  80. package/src/publisher/sequencer-publisher.ts +990 -302
  81. package/src/sequencer/README.md +531 -0
  82. package/src/sequencer/checkpoint_proposal_job.ts +872 -0
  83. package/src/sequencer/checkpoint_voter.ts +130 -0
  84. package/src/sequencer/config.ts +8 -0
  85. package/src/sequencer/errors.ts +21 -0
  86. package/src/sequencer/events.ts +27 -0
  87. package/src/sequencer/index.ts +3 -1
  88. package/src/sequencer/metrics.ts +288 -73
  89. package/src/sequencer/sequencer.ts +714 -588
  90. package/src/sequencer/timetable.ts +221 -62
  91. package/src/sequencer/types.ts +6 -0
  92. package/src/sequencer/utils.ts +28 -60
  93. package/src/test/index.ts +12 -4
  94. package/src/test/mock_checkpoint_builder.ts +309 -0
  95. package/src/test/utils.ts +164 -0
  96. package/dest/sequencer/allowed.d.ts +0 -3
  97. package/dest/sequencer/allowed.d.ts.map +0 -1
  98. package/dest/sequencer/allowed.js +0 -27
  99. package/dest/slasher/factory.d.ts +0 -7
  100. package/dest/slasher/factory.d.ts.map +0 -1
  101. package/dest/slasher/factory.js +0 -8
  102. package/dest/slasher/index.d.ts +0 -3
  103. package/dest/slasher/index.d.ts.map +0 -1
  104. package/dest/slasher/index.js +0 -2
  105. package/dest/slasher/slasher_client.d.ts +0 -75
  106. package/dest/slasher/slasher_client.d.ts.map +0 -1
  107. package/dest/slasher/slasher_client.js +0 -132
  108. package/dest/tx_validator/archive_cache.d.ts +0 -14
  109. package/dest/tx_validator/archive_cache.d.ts.map +0 -1
  110. package/dest/tx_validator/archive_cache.js +0 -22
  111. package/dest/tx_validator/gas_validator.d.ts +0 -14
  112. package/dest/tx_validator/gas_validator.d.ts.map +0 -1
  113. package/dest/tx_validator/gas_validator.js +0 -78
  114. package/dest/tx_validator/nullifier_cache.d.ts +0 -16
  115. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  116. package/dest/tx_validator/nullifier_cache.js +0 -24
  117. package/dest/tx_validator/phases_validator.d.ts +0 -12
  118. package/dest/tx_validator/phases_validator.d.ts.map +0 -1
  119. package/dest/tx_validator/phases_validator.js +0 -80
  120. package/dest/tx_validator/test_utils.d.ts +0 -23
  121. package/dest/tx_validator/test_utils.d.ts.map +0 -1
  122. package/dest/tx_validator/test_utils.js +0 -26
  123. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  124. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  125. package/dest/tx_validator/tx_validator_factory.js +0 -50
  126. package/src/sequencer/allowed.ts +0 -36
  127. package/src/slasher/factory.ts +0 -15
  128. package/src/slasher/index.ts +0 -2
  129. package/src/slasher/slasher_client.ts +0 -193
  130. package/src/tx_validator/archive_cache.ts +0 -28
  131. package/src/tx_validator/gas_validator.ts +0 -101
  132. package/src/tx_validator/nullifier_cache.ts +0 -30
  133. package/src/tx_validator/phases_validator.ts +0 -98
  134. package/src/tx_validator/test_utils.ts +0 -48
  135. package/src/tx_validator/tx_validator_factory.ts +0 -120
@@ -1,36 +1,49 @@
1
- import type { L2Block } from '@aztec/aztec.js';
2
- import { Blob } from '@aztec/blob-lib';
3
- import { type BlobSinkClientInterface, createBlobSinkClient } from '@aztec/blob-sink/client';
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { Blob, getBlobsPerL1Block, getPrefixedEthBlobCommitments } from '@aztec/blob-lib';
4
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
+ import type { L1ContractsConfig } from '@aztec/ethereum/config';
5
5
  import {
6
- FormattedViemError,
7
- type ForwarderContract,
8
- type GasPrice,
6
+ type EmpireSlashingProposerContract,
9
7
  type GovernanceProposerContract,
10
8
  type IEmpireBase,
9
+ MULTI_CALL_3_ADDRESS,
10
+ Multicall3,
11
+ RollupContract,
12
+ type TallySlashingProposerContract,
13
+ type ViemCommitteeAttestations,
14
+ type ViemHeader,
15
+ } from '@aztec/ethereum/contracts';
16
+ import { type L1FeeAnalysisResult, L1FeeAnalyzer } from '@aztec/ethereum/l1-fee-analysis';
17
+ import {
11
18
  type L1BlobInputs,
12
- type L1ContractsConfig,
13
- type L1GasConfig,
19
+ type L1TxConfig,
14
20
  type L1TxRequest,
15
- RollupContract,
16
- type SlashingProposerContract,
21
+ MAX_L1_TX_LIMIT,
17
22
  type TransactionStats,
18
- formatViemError,
19
- } from '@aztec/ethereum';
23
+ WEI_CONST,
24
+ } from '@aztec/ethereum/l1-tx-utils';
20
25
  import type { L1TxUtilsWithBlobs } from '@aztec/ethereum/l1-tx-utils-with-blobs';
21
- import { toHex } from '@aztec/foundation/bigint-buffer';
26
+ import { FormattedViemError, formatViemError, mergeAbis, tryExtractEvent } from '@aztec/ethereum/utils';
27
+ import { sumBigint } from '@aztec/foundation/bigint';
28
+ import { toHex as toPaddedHex } from '@aztec/foundation/bigint-buffer';
29
+ import { CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
30
+ import { pick } from '@aztec/foundation/collection';
31
+ import type { Fr } from '@aztec/foundation/curves/bn254';
22
32
  import { EthAddress } from '@aztec/foundation/eth-address';
23
- import type { Signature } from '@aztec/foundation/eth-signature';
24
- import { createLogger } from '@aztec/foundation/log';
25
- import { Timer } from '@aztec/foundation/timer';
26
- import { ForwarderAbi, RollupAbi } from '@aztec/l1-artifacts';
27
- import { ConsensusPayload, SignatureDomainSeparator, getHashedSignaturePayload } from '@aztec/stdlib/p2p';
28
- import type { L1PublishBlockStats } from '@aztec/stdlib/stats';
29
- import { type BlockHeader, TxHash } from '@aztec/stdlib/tx';
30
- import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
31
-
32
- import pick from 'lodash.pick';
33
- import { type TransactionReceipt, encodeFunctionData } from 'viem';
33
+ import { Signature, type ViemSignature } from '@aztec/foundation/eth-signature';
34
+ import { type Logger, createLogger } from '@aztec/foundation/log';
35
+ import { bufferToHex } from '@aztec/foundation/string';
36
+ import { DateProvider, Timer } from '@aztec/foundation/timer';
37
+ import { EmpireBaseAbi, ErrorsAbi, RollupAbi } from '@aztec/l1-artifacts';
38
+ import { type ProposerSlashAction, encodeSlashConsensusVotes } from '@aztec/slasher';
39
+ import { CommitteeAttestationsAndSigners, type ValidateCheckpointResult } from '@aztec/stdlib/block';
40
+ import type { Checkpoint } from '@aztec/stdlib/checkpoint';
41
+ import { SlashFactoryContract } from '@aztec/stdlib/l1-contracts';
42
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
43
+ import type { L1PublishCheckpointStats } from '@aztec/stdlib/stats';
44
+ import { type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
45
+
46
+ import { type StateOverride, type TransactionReceipt, type TypedDataDefinition, encodeFunctionData, toHex } from 'viem';
34
47
 
35
48
  import type { PublisherConfig, TxSenderConfig } from './config.js';
36
49
  import { SequencerPublisherMetrics } from './sequencer-publisher-metrics.js';
@@ -38,131 +51,243 @@ import { SequencerPublisherMetrics } from './sequencer-publisher-metrics.js';
38
51
  /** Arguments to the process method of the rollup contract */
39
52
  type L1ProcessArgs = {
40
53
  /** The L2 block header. */
41
- header: Buffer;
54
+ header: CheckpointHeader;
42
55
  /** A root of the archive tree after the L2 block is applied. */
43
56
  archive: Buffer;
44
- /** The L2 block's leaf in the archive tree. */
45
- blockHash: Buffer;
46
57
  /** L2 block blobs containing all tx effects. */
47
58
  blobs: Blob[];
48
- /** L2 block tx hashes */
49
- txHashes: TxHash[];
50
59
  /** Attestations */
51
- attestations?: Signature[];
60
+ attestationsAndSigners: CommitteeAttestationsAndSigners;
61
+ /** Attestations and signers signature */
62
+ attestationsAndSignersSignature: Signature;
52
63
  };
53
64
 
54
- export enum VoteType {
55
- GOVERNANCE,
56
- SLASHING,
57
- }
65
+ export const Actions = [
66
+ 'invalidate-by-invalid-attestation',
67
+ 'invalidate-by-insufficient-attestations',
68
+ 'propose',
69
+ 'governance-signal',
70
+ 'empire-slashing-signal',
71
+ 'create-empire-payload',
72
+ 'execute-empire-payload',
73
+ 'vote-offenses',
74
+ 'execute-slash',
75
+ ] as const;
58
76
 
59
- type GetSlashPayloadCallBack = (slotNumber: bigint) => Promise<EthAddress | undefined>;
77
+ export type Action = (typeof Actions)[number];
78
+
79
+ type GovernanceSignalAction = Extract<Action, 'governance-signal' | 'empire-slashing-signal'>;
80
+
81
+ // Sorting for actions such that invalidations go before proposals, and proposals go before votes
82
+ export const compareActions = (a: Action, b: Action) => Actions.indexOf(a) - Actions.indexOf(b);
83
+
84
+ export type InvalidateCheckpointRequest = {
85
+ request: L1TxRequest;
86
+ reason: 'invalid-attestation' | 'insufficient-attestations';
87
+ gasUsed: bigint;
88
+ checkpointNumber: CheckpointNumber;
89
+ forcePendingCheckpointNumber: CheckpointNumber;
90
+ };
60
91
 
61
- type Action = 'propose' | 'governance-vote' | 'slashing-vote';
62
92
  interface RequestWithExpiry {
63
93
  action: Action;
64
94
  request: L1TxRequest;
65
- lastValidL2Slot: bigint;
66
- gasConfig?: L1GasConfig;
95
+ lastValidL2Slot: SlotNumber;
96
+ gasConfig?: Pick<L1TxConfig, 'txTimeoutAt' | 'gasLimit'>;
67
97
  blobConfig?: L1BlobInputs;
68
- onResult?: (
98
+ checkSuccess: (
69
99
  request: L1TxRequest,
70
- result?: { receipt: TransactionReceipt; gasPrice: GasPrice; stats?: TransactionStats; errorMsg?: string },
71
- ) => void;
100
+ result?: { receipt: TransactionReceipt; stats?: TransactionStats; errorMsg?: string },
101
+ ) => boolean;
72
102
  }
73
103
 
74
104
  export class SequencerPublisher {
75
105
  private interrupted = false;
76
106
  private metrics: SequencerPublisherMetrics;
77
- private epochCache: EpochCache;
78
- private forwarderContract: ForwarderContract;
107
+ public epochCache: EpochCache;
79
108
 
80
109
  protected governanceLog = createLogger('sequencer:publisher:governance');
81
- protected governanceProposerAddress?: EthAddress;
82
- private governancePayload: EthAddress = EthAddress.ZERO;
83
-
84
110
  protected slashingLog = createLogger('sequencer:publisher:slashing');
85
- protected slashingProposerAddress?: EthAddress;
86
- private getSlashPayload?: GetSlashPayloadCallBack = undefined;
87
111
 
88
- private myLastVotes: Record<VoteType, bigint> = {
89
- [VoteType.GOVERNANCE]: 0n,
90
- [VoteType.SLASHING]: 0n,
91
- };
112
+ protected lastActions: Partial<Record<Action, SlotNumber>> = {};
92
113
 
93
- protected log = createLogger('sequencer:publisher');
114
+ private isPayloadEmptyCache: Map<string, boolean> = new Map<string, boolean>();
115
+
116
+ protected log: Logger;
94
117
  protected ethereumSlotDuration: bigint;
95
118
 
96
- private blobSinkClient: BlobSinkClientInterface;
97
- // @note - with blobs, the below estimate seems too large.
98
- // Total used for full block from int_l1_pub e2e test: 1m (of which 86k is 1x blob)
99
- // Total used for emptier block from above test: 429k (of which 84k is 1x blob)
100
- public static PROPOSE_GAS_GUESS: bigint = 12_000_000n;
119
+ private blobClient: BlobClientInterface;
120
+
121
+ /** Address to use for simulations in fisherman mode (actual proposer's address) */
122
+ private proposerAddressForSimulation?: EthAddress;
123
+
124
+ /** L1 fee analyzer for fisherman mode */
125
+ private l1FeeAnalyzer?: L1FeeAnalyzer;
126
+ // A CALL to a cold address is 2700 gas
127
+ public static MULTICALL_OVERHEAD_GAS_GUESS = 5000n;
128
+
129
+ // Gas report for VotingWithSigTest shows a max gas of 100k, but we've seen it cost 700k+ in testnet
130
+ public static VOTE_GAS_GUESS: bigint = 800_000n;
101
131
 
102
132
  public l1TxUtils: L1TxUtilsWithBlobs;
103
133
  public rollupContract: RollupContract;
104
134
  public govProposerContract: GovernanceProposerContract;
105
- public slashingProposerContract: SlashingProposerContract;
135
+ public slashingProposerContract: EmpireSlashingProposerContract | TallySlashingProposerContract | undefined;
136
+ public slashFactoryContract: SlashFactoryContract;
137
+
138
+ public readonly tracer: Tracer;
106
139
 
107
140
  protected requests: RequestWithExpiry[] = [];
108
141
 
109
142
  constructor(
110
- config: TxSenderConfig & PublisherConfig & Pick<L1ContractsConfig, 'ethereumSlotDuration'>,
143
+ private config: TxSenderConfig & PublisherConfig & Pick<L1ContractsConfig, 'ethereumSlotDuration'>,
111
144
  deps: {
112
145
  telemetry?: TelemetryClient;
113
- blobSinkClient?: BlobSinkClientInterface;
114
- forwarderContract: ForwarderContract;
146
+ blobClient: BlobClientInterface;
115
147
  l1TxUtils: L1TxUtilsWithBlobs;
116
148
  rollupContract: RollupContract;
117
- slashingProposerContract: SlashingProposerContract;
149
+ slashingProposerContract: EmpireSlashingProposerContract | TallySlashingProposerContract | undefined;
118
150
  governanceProposerContract: GovernanceProposerContract;
151
+ slashFactoryContract: SlashFactoryContract;
119
152
  epochCache: EpochCache;
153
+ dateProvider: DateProvider;
154
+ metrics: SequencerPublisherMetrics;
155
+ lastActions: Partial<Record<Action, SlotNumber>>;
156
+ log?: Logger;
120
157
  },
121
158
  ) {
159
+ this.log = deps.log ?? createLogger('sequencer:publisher');
122
160
  this.ethereumSlotDuration = BigInt(config.ethereumSlotDuration);
123
161
  this.epochCache = deps.epochCache;
162
+ this.lastActions = deps.lastActions;
124
163
 
125
- this.blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
164
+ this.blobClient = deps.blobClient;
126
165
 
127
166
  const telemetry = deps.telemetry ?? getTelemetryClient();
128
- this.metrics = new SequencerPublisherMetrics(telemetry, 'SequencerPublisher');
167
+ this.metrics = deps.metrics ?? new SequencerPublisherMetrics(telemetry, 'SequencerPublisher');
168
+ this.tracer = telemetry.getTracer('SequencerPublisher');
129
169
  this.l1TxUtils = deps.l1TxUtils;
130
170
 
131
171
  this.rollupContract = deps.rollupContract;
132
- this.forwarderContract = deps.forwarderContract;
133
172
 
134
173
  this.govProposerContract = deps.governanceProposerContract;
135
174
  this.slashingProposerContract = deps.slashingProposerContract;
136
- }
137
175
 
138
- public registerSlashPayloadGetter(callback: GetSlashPayloadCallBack) {
139
- this.getSlashPayload = callback;
176
+ this.rollupContract.listenToSlasherChanged(async () => {
177
+ this.log.info('Slashing proposer changed');
178
+ const newSlashingProposer = await this.rollupContract.getSlashingProposer();
179
+ this.slashingProposerContract = newSlashingProposer;
180
+ });
181
+ this.slashFactoryContract = deps.slashFactoryContract;
182
+
183
+ // Initialize L1 fee analyzer for fisherman mode
184
+ if (config.fishermanMode) {
185
+ this.l1FeeAnalyzer = new L1FeeAnalyzer(
186
+ this.l1TxUtils.client,
187
+ deps.dateProvider,
188
+ createLogger('sequencer:publisher:fee-analyzer'),
189
+ );
190
+ }
140
191
  }
141
192
 
142
- public getForwarderAddress() {
143
- return EthAddress.fromString(this.forwarderContract.getAddress());
193
+ public getRollupContract(): RollupContract {
194
+ return this.rollupContract;
144
195
  }
145
196
 
146
197
  public getSenderAddress() {
147
- return EthAddress.fromString(this.l1TxUtils.getSenderAddress());
198
+ return this.l1TxUtils.getSenderAddress();
148
199
  }
149
200
 
150
- public getGovernancePayload() {
151
- return this.governancePayload;
201
+ /**
202
+ * Gets the L1 fee analyzer instance (only available in fisherman mode)
203
+ */
204
+ public getL1FeeAnalyzer(): L1FeeAnalyzer | undefined {
205
+ return this.l1FeeAnalyzer;
152
206
  }
153
207
 
154
- public setGovernancePayload(payload: EthAddress) {
155
- this.governancePayload = payload;
208
+ /**
209
+ * Sets the proposer address to use for simulations in fisherman mode.
210
+ * @param proposerAddress - The actual proposer's address to use for balance lookups in simulations
211
+ */
212
+ public setProposerAddressForSimulation(proposerAddress: EthAddress | undefined) {
213
+ this.proposerAddressForSimulation = proposerAddress;
156
214
  }
157
215
 
158
216
  public addRequest(request: RequestWithExpiry) {
159
217
  this.requests.push(request);
160
218
  }
161
219
 
162
- public getCurrentL2Slot(): bigint {
220
+ public getCurrentL2Slot(): SlotNumber {
163
221
  return this.epochCache.getEpochAndSlotNow().slot;
164
222
  }
165
223
 
224
+ /**
225
+ * Clears all pending requests without sending them.
226
+ */
227
+ public clearPendingRequests(): void {
228
+ const count = this.requests.length;
229
+ this.requests = [];
230
+ if (count > 0) {
231
+ this.log.debug(`Cleared ${count} pending request(s)`);
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Analyzes L1 fees for the pending requests without sending them.
237
+ * This is used in fisherman mode to validate fee calculations.
238
+ * @param l2SlotNumber - The L2 slot number for this analysis
239
+ * @param onComplete - Optional callback to invoke when analysis completes (after block is mined)
240
+ * @returns The analysis result (incomplete until block mines), or undefined if no requests
241
+ */
242
+ public async analyzeL1Fees(
243
+ l2SlotNumber: SlotNumber,
244
+ onComplete?: (analysis: L1FeeAnalysisResult) => void,
245
+ ): Promise<L1FeeAnalysisResult | undefined> {
246
+ if (!this.l1FeeAnalyzer) {
247
+ this.log.warn('L1 fee analyzer not available (not in fisherman mode)');
248
+ return undefined;
249
+ }
250
+
251
+ const requestsToAnalyze = [...this.requests];
252
+ if (requestsToAnalyze.length === 0) {
253
+ this.log.debug('No requests to analyze for L1 fees');
254
+ return undefined;
255
+ }
256
+
257
+ // Extract blob config from requests (if any)
258
+ const blobConfigs = requestsToAnalyze.filter(request => request.blobConfig).map(request => request.blobConfig);
259
+ const blobConfig = blobConfigs[0];
260
+
261
+ // Get gas configs
262
+ const gasConfigs = requestsToAnalyze.filter(request => request.gasConfig).map(request => request.gasConfig);
263
+ const gasLimits = gasConfigs.map(g => g?.gasLimit).filter((g): g is bigint => g !== undefined);
264
+ const gasLimit = gasLimits.length > 0 ? gasLimits.reduce((sum, g) => sum + g, 0n) : 0n;
265
+
266
+ // Get the transaction requests
267
+ const l1Requests = requestsToAnalyze.map(r => r.request);
268
+
269
+ // Start the analysis
270
+ const analysisId = await this.l1FeeAnalyzer.startAnalysis(
271
+ l2SlotNumber,
272
+ gasLimit > 0n ? gasLimit : MAX_L1_TX_LIMIT,
273
+ l1Requests,
274
+ blobConfig,
275
+ onComplete,
276
+ );
277
+
278
+ this.log.info('Started L1 fee analysis', {
279
+ analysisId,
280
+ l2SlotNumber: l2SlotNumber.toString(),
281
+ requestCount: requestsToAnalyze.length,
282
+ hasBlobConfig: !!blobConfig,
283
+ gasLimit: gasLimit.toString(),
284
+ actions: requestsToAnalyze.map(r => r.action),
285
+ });
286
+
287
+ // Return the analysis result (will be incomplete until block mines)
288
+ return this.l1FeeAnalyzer.getAnalysis(analysisId);
289
+ }
290
+
166
291
  /**
167
292
  * Sends all requests that are still valid.
168
293
  * @returns one of:
@@ -170,15 +295,20 @@ export class SequencerPublisher {
170
295
  * - a receipt and errorMsg if it failed on L1
171
296
  * - undefined if no valid requests are found OR the tx failed to send.
172
297
  */
298
+ @trackSpan('SequencerPublisher.sendRequests')
173
299
  public async sendRequests() {
174
300
  const requestsToProcess = [...this.requests];
175
301
  this.requests = [];
176
- if (this.interrupted) {
302
+ if (this.interrupted || requestsToProcess.length === 0) {
177
303
  return undefined;
178
304
  }
179
305
  const currentL2Slot = this.getCurrentL2Slot();
180
- this.log.debug(`Current L2 slot: ${currentL2Slot}`);
306
+ this.log.debug(`Sending requests on L2 slot ${currentL2Slot}`);
181
307
  const validRequests = requestsToProcess.filter(request => request.lastValidL2Slot >= currentL2Slot);
308
+ const validActions = validRequests.map(x => x.action);
309
+ const expiredActions = requestsToProcess
310
+ .filter(request => request.lastValidL2Slot < currentL2Slot)
311
+ .map(x => x.action);
182
312
 
183
313
  if (validRequests.length !== requestsToProcess.length) {
184
314
  this.log.warn(`Some requests were expired for slot ${currentL2Slot}`, {
@@ -198,39 +328,63 @@ export class SequencerPublisher {
198
328
  return undefined;
199
329
  }
200
330
 
201
- // @note - we can only have one gas config and one blob config per bundle
331
+ // @note - we can only have one blob config per bundle
202
332
  // find requests with gas and blob configs
203
333
  // See https://github.com/AztecProtocol/aztec-packages/issues/11513
204
- const gasConfigs = requestsToProcess.filter(request => request.gasConfig);
205
- const blobConfigs = requestsToProcess.filter(request => request.blobConfig);
334
+ const gasConfigs = requestsToProcess.filter(request => request.gasConfig).map(request => request.gasConfig);
335
+ const blobConfigs = requestsToProcess.filter(request => request.blobConfig).map(request => request.blobConfig);
336
+
337
+ if (blobConfigs.length > 1) {
338
+ throw new Error('Multiple blob configs found');
339
+ }
206
340
 
207
- if (gasConfigs.length > 1 || blobConfigs.length > 1) {
208
- throw new Error('Multiple gas or blob configs found');
341
+ const blobConfig = blobConfigs[0];
342
+
343
+ // Merge gasConfigs. Yields the sum of gasLimits, and the earliest txTimeoutAt, or undefined if no gasConfig sets them.
344
+ const gasLimits = gasConfigs.map(g => g?.gasLimit).filter((g): g is bigint => g !== undefined);
345
+ let gasLimit = gasLimits.length > 0 ? sumBigint(gasLimits) : undefined; // sum
346
+ // Cap at L1 block gas limit so the node accepts the tx ("gas limit too high" otherwise).
347
+ const maxGas = MAX_L1_TX_LIMIT;
348
+ if (gasLimit !== undefined && gasLimit > maxGas) {
349
+ this.log.debug('Capping bundled tx gas limit to L1 max', {
350
+ requested: gasLimit,
351
+ capped: maxGas,
352
+ });
353
+ gasLimit = maxGas;
209
354
  }
355
+ const txTimeoutAts = gasConfigs.map(g => g?.txTimeoutAt).filter((g): g is Date => g !== undefined);
356
+ const txTimeoutAt = txTimeoutAts.length > 0 ? new Date(Math.min(...txTimeoutAts.map(g => g.getTime()))) : undefined; // earliest
357
+ const txConfig: RequestWithExpiry['gasConfig'] = { gasLimit, txTimeoutAt };
210
358
 
211
- const gasConfig = gasConfigs[0]?.gasConfig;
212
- const blobConfig = blobConfigs[0]?.blobConfig;
359
+ // Sort the requests so that proposals always go first
360
+ // This ensures the committee gets precomputed correctly
361
+ validRequests.sort((a, b) => compareActions(a.action, b.action));
213
362
 
214
363
  try {
215
364
  this.log.debug('Forwarding transactions', {
216
365
  validRequests: validRequests.map(request => request.action),
366
+ txConfig,
217
367
  });
218
- const result = await this.forwarderContract.forward(
368
+ const result = await Multicall3.forward(
219
369
  validRequests.map(request => request.request),
220
370
  this.l1TxUtils,
221
- gasConfig,
371
+ txConfig,
222
372
  blobConfig,
373
+ this.rollupContract.address,
223
374
  this.log,
224
375
  );
225
- this.callbackBundledTransactions(validRequests, result);
226
- return result;
376
+ const { successfulActions = [], failedActions = [] } = this.callbackBundledTransactions(validRequests, result);
377
+ return { result, expiredActions, sentActions: validActions, successfulActions, failedActions };
227
378
  } catch (err) {
228
379
  const viemError = formatViemError(err);
229
380
  this.log.error(`Failed to publish bundled transactions`, viemError);
230
381
  return undefined;
231
382
  } finally {
232
383
  try {
233
- this.metrics.recordSenderBalance(await this.l1TxUtils.getSenderBalance(), this.l1TxUtils.getSenderAddress());
384
+ this.metrics.recordSenderBalance(
385
+ await this.l1TxUtils.getSenderBalance(),
386
+ this.l1TxUtils.getSenderAddress().toString(),
387
+ );
234
388
  } catch (err) {
235
389
  this.log.warn(`Failed to record balance after sending tx: ${err}`);
236
390
  }
@@ -239,13 +393,24 @@ export class SequencerPublisher {
239
393
 
240
394
  private callbackBundledTransactions(
241
395
  requests: RequestWithExpiry[],
242
- result?: { receipt: TransactionReceipt; gasPrice: GasPrice },
396
+ result?: { receipt: TransactionReceipt } | FormattedViemError,
243
397
  ) {
244
- const success = result?.receipt.status === 'success';
245
- const logger = success ? this.log.info : this.log.error;
246
- for (const request of requests) {
247
- logger(`Bundled [${request.action}] transaction [${success ? 'succeeded' : 'failed'}]`);
248
- request.onResult?.(request.request, result);
398
+ const actionsListStr = requests.map(r => r.action).join(', ');
399
+ if (result instanceof FormattedViemError) {
400
+ this.log.error(`Failed to publish bundled transactions (${actionsListStr})`, result);
401
+ return { failedActions: requests.map(r => r.action) };
402
+ } else {
403
+ this.log.verbose(`Published bundled transactions (${actionsListStr})`, { result, requests });
404
+ const successfulActions: Action[] = [];
405
+ const failedActions: Action[] = [];
406
+ for (const request of requests) {
407
+ if (request.checkSuccess(request.request, result)) {
408
+ successfulActions.push(request.action);
409
+ } else {
410
+ failedActions.push(request.action);
411
+ }
412
+ }
413
+ return { successfulActions, failedActions };
249
414
  }
250
415
  }
251
416
 
@@ -254,176 +419,632 @@ export class SequencerPublisher {
254
419
  * @param tipArchive - The archive to check
255
420
  * @returns The slot and block number if it is possible to propose, undefined otherwise
256
421
  */
257
- public canProposeAtNextEthBlock(tipArchive: Buffer) {
422
+ public canProposeAtNextEthBlock(
423
+ tipArchive: Fr,
424
+ msgSender: EthAddress,
425
+ opts: { forcePendingCheckpointNumber?: CheckpointNumber } = {},
426
+ ) {
427
+ // TODO: #14291 - should loop through multiple keys to check if any of them can propose
258
428
  const ignoredErrors = ['SlotAlreadyInChain', 'InvalidProposer', 'InvalidArchive'];
429
+
259
430
  return this.rollupContract
260
- .canProposeAtNextEthBlock(tipArchive, this.getForwarderAddress().toString(), this.ethereumSlotDuration)
431
+ .canProposeAtNextEthBlock(tipArchive.toBuffer(), msgSender.toString(), Number(this.ethereumSlotDuration), {
432
+ forcePendingCheckpointNumber: opts.forcePendingCheckpointNumber,
433
+ })
261
434
  .catch(err => {
262
435
  if (err instanceof FormattedViemError && ignoredErrors.find(e => err.message.includes(e))) {
263
- this.log.debug(err.message);
436
+ this.log.warn(`Failed canProposeAtTime check with ${ignoredErrors.find(e => err.message.includes(e))}`, {
437
+ error: err.message,
438
+ });
264
439
  } else {
265
440
  this.log.error(err.name, err);
266
441
  }
267
442
  return undefined;
268
443
  });
269
444
  }
445
+ /**
446
+ * @notice Will simulate `validateHeader` to make sure that the block header is valid
447
+ * @dev This is a convenience function that can be used by the sequencer to validate a "partial" header.
448
+ * It will throw if the block header is invalid.
449
+ * @param header - The block header to validate
450
+ */
451
+ @trackSpan('SequencerPublisher.validateBlockHeader')
452
+ public async validateBlockHeader(
453
+ header: CheckpointHeader,
454
+ opts?: { forcePendingCheckpointNumber: CheckpointNumber | undefined },
455
+ ): Promise<void> {
456
+ const flags = { ignoreDA: true, ignoreSignatures: true };
457
+
458
+ const args = [
459
+ header.toViem(),
460
+ CommitteeAttestationsAndSigners.empty().getPackedAttestations(),
461
+ [], // no signers
462
+ Signature.empty().toViemSignature(),
463
+ `0x${'0'.repeat(64)}`, // 32 empty bytes
464
+ header.blobsHash.toString(),
465
+ flags,
466
+ ] as const;
467
+
468
+ const ts = BigInt((await this.l1TxUtils.getBlock()).timestamp + this.ethereumSlotDuration);
469
+ const stateOverrides = await this.rollupContract.makePendingCheckpointNumberOverride(
470
+ opts?.forcePendingCheckpointNumber,
471
+ );
472
+ let balance = 0n;
473
+ if (this.config.fishermanMode) {
474
+ // In fisherman mode, we can't know where the proposer is publishing from
475
+ // so we just add sufficient balance to the multicall3 address
476
+ balance = 10n * WEI_CONST * WEI_CONST; // 10 ETH
477
+ } else {
478
+ balance = await this.l1TxUtils.getSenderBalance();
479
+ }
480
+ stateOverrides.push({
481
+ address: MULTI_CALL_3_ADDRESS,
482
+ balance,
483
+ });
484
+
485
+ await this.l1TxUtils.simulate(
486
+ {
487
+ to: this.rollupContract.address,
488
+ data: encodeFunctionData({ abi: RollupAbi, functionName: 'validateHeaderWithAttestations', args }),
489
+ from: MULTI_CALL_3_ADDRESS,
490
+ },
491
+ { time: ts + 1n },
492
+ stateOverrides,
493
+ );
494
+ this.log.debug(`Simulated validateHeader`);
495
+ }
270
496
 
271
497
  /**
272
- * @notice Will call `validateHeader` to make sure that it is possible to propose
273
- *
274
- * @dev Throws if unable to propose
275
- *
276
- * @param header - The header to propose
277
- * @param digest - The digest that attestations are signing over
278
- *
498
+ * Simulate making a call to invalidate a checkpoint with invalid attestations. Returns undefined if no need to invalidate.
499
+ * @param validationResult - The validation result indicating which checkpoint to invalidate (as returned by the archiver)
279
500
  */
280
- public async validateBlockForSubmission(
281
- header: BlockHeader,
282
- attestationData: { digest: Buffer; signatures: Signature[] } = {
283
- digest: Buffer.alloc(32),
284
- signatures: [],
285
- },
501
+ public async simulateInvalidateCheckpoint(
502
+ validationResult: ValidateCheckpointResult,
503
+ ): Promise<InvalidateCheckpointRequest | undefined> {
504
+ if (validationResult.valid) {
505
+ return undefined;
506
+ }
507
+
508
+ const { reason, checkpoint } = validationResult;
509
+ const checkpointNumber = checkpoint.checkpointNumber;
510
+ const logData = { ...checkpoint, reason };
511
+
512
+ const currentCheckpointNumber = await this.rollupContract.getCheckpointNumber();
513
+ if (currentCheckpointNumber < checkpointNumber) {
514
+ this.log.verbose(
515
+ `Skipping checkpoint ${checkpointNumber} invalidation since it has already been removed from the pending chain`,
516
+ { currentCheckpointNumber, ...logData },
517
+ );
518
+ return undefined;
519
+ }
520
+
521
+ const request = this.buildInvalidateCheckpointRequest(validationResult);
522
+ this.log.debug(`Simulating invalidate checkpoint ${checkpointNumber}`, { ...logData, request });
523
+
524
+ try {
525
+ const { gasUsed } = await this.l1TxUtils.simulate(
526
+ request,
527
+ undefined,
528
+ undefined,
529
+ mergeAbis([request.abi ?? [], ErrorsAbi]),
530
+ );
531
+ this.log.verbose(`Simulation for invalidate checkpoint ${checkpointNumber} succeeded`, {
532
+ ...logData,
533
+ request,
534
+ gasUsed,
535
+ });
536
+
537
+ return {
538
+ request,
539
+ gasUsed,
540
+ checkpointNumber,
541
+ forcePendingCheckpointNumber: CheckpointNumber(checkpointNumber - 1),
542
+ reason,
543
+ };
544
+ } catch (err) {
545
+ const viemError = formatViemError(err);
546
+
547
+ // If the error is due to the checkpoint not being in the pending chain, and it was indeed removed by someone else,
548
+ // we can safely ignore it and return undefined so we go ahead with checkpoint building.
549
+ if (viemError.message?.includes('Rollup__CheckpointNotInPendingChain')) {
550
+ this.log.verbose(
551
+ `Simulation for invalidate checkpoint ${checkpointNumber} failed due to checkpoint not being in pending chain`,
552
+ { ...logData, request, error: viemError.message },
553
+ );
554
+ const latestPendingCheckpointNumber = await this.rollupContract.getCheckpointNumber();
555
+ if (latestPendingCheckpointNumber < checkpointNumber) {
556
+ this.log.verbose(`Checkpoint ${checkpointNumber} has already been invalidated`, { ...logData });
557
+ return undefined;
558
+ } else {
559
+ this.log.error(
560
+ `Simulation for invalidate checkpoint ${checkpointNumber} failed and it is still in pending chain`,
561
+ viemError,
562
+ logData,
563
+ );
564
+ throw new Error(
565
+ `Failed to simulate invalidate checkpoint ${checkpointNumber} while it is still in pending chain`,
566
+ {
567
+ cause: viemError,
568
+ },
569
+ );
570
+ }
571
+ }
572
+
573
+ // Otherwise, throw. We cannot build the next checkpoint if we cannot invalidate the previous one.
574
+ this.log.error(`Simulation for invalidate checkpoint ${checkpointNumber} failed`, viemError, logData);
575
+ throw new Error(`Failed to simulate invalidate checkpoint ${checkpointNumber}`, { cause: viemError });
576
+ }
577
+ }
578
+
579
+ private buildInvalidateCheckpointRequest(validationResult: ValidateCheckpointResult) {
580
+ if (validationResult.valid) {
581
+ throw new Error('Cannot invalidate a valid checkpoint');
582
+ }
583
+
584
+ const { checkpoint, committee, reason } = validationResult;
585
+ const logData = { ...checkpoint, reason };
586
+ this.log.debug(`Building invalidate checkpoint ${checkpoint.checkpointNumber} request`, logData);
587
+
588
+ const attestationsAndSigners = new CommitteeAttestationsAndSigners(
589
+ validationResult.attestations,
590
+ ).getPackedAttestations();
591
+
592
+ if (reason === 'invalid-attestation') {
593
+ return this.rollupContract.buildInvalidateBadAttestationRequest(
594
+ checkpoint.checkpointNumber,
595
+ attestationsAndSigners,
596
+ committee,
597
+ validationResult.invalidIndex,
598
+ );
599
+ } else if (reason === 'insufficient-attestations') {
600
+ return this.rollupContract.buildInvalidateInsufficientAttestationsRequest(
601
+ checkpoint.checkpointNumber,
602
+ attestationsAndSigners,
603
+ committee,
604
+ );
605
+ } else {
606
+ const _: never = reason;
607
+ throw new Error(`Unknown reason for invalidation`);
608
+ }
609
+ }
610
+
611
+ /** Simulates `propose` to make sure that the checkpoint is valid for submission */
612
+ @trackSpan('SequencerPublisher.validateCheckpointForSubmission')
613
+ public async validateCheckpointForSubmission(
614
+ checkpoint: Checkpoint,
615
+ attestationsAndSigners: CommitteeAttestationsAndSigners,
616
+ attestationsAndSignersSignature: Signature,
617
+ options: { forcePendingCheckpointNumber?: CheckpointNumber },
286
618
  ): Promise<bigint> {
287
619
  const ts = BigInt((await this.l1TxUtils.getBlock()).timestamp + this.ethereumSlotDuration);
288
620
 
289
- const formattedSignatures = attestationData.signatures.map(attest => attest.toViemSignature());
290
- const flags = { ignoreDA: true, ignoreSignatures: formattedSignatures.length == 0 };
621
+ // TODO(palla/mbps): This should not be needed, there's no flow where we propose with zero attestations. Or is there?
622
+ // If we have no attestations, we still need to provide the empty attestations
623
+ // so that the committee is recalculated correctly
624
+ // const ignoreSignatures = attestationsAndSigners.attestations.length === 0;
625
+ // if (ignoreSignatures) {
626
+ // const { committee } = await this.epochCache.getCommittee(block.header.globalVariables.slotNumber);
627
+ // if (!committee) {
628
+ // this.log.warn(`No committee found for slot ${block.header.globalVariables.slotNumber}`);
629
+ // throw new Error(`No committee found for slot ${block.header.globalVariables.slotNumber}`);
630
+ // }
631
+ // attestationsAndSigners.attestations = committee.map(committeeMember =>
632
+ // CommitteeAttestation.fromAddress(committeeMember),
633
+ // );
634
+ // }
635
+
636
+ const blobFields = checkpoint.toBlobFields();
637
+ const blobs = getBlobsPerL1Block(blobFields);
638
+ const blobInput = getPrefixedEthBlobCommitments(blobs);
291
639
 
292
640
  const args = [
293
- `0x${header.toBuffer().toString('hex')}`,
294
- formattedSignatures,
295
- `0x${attestationData.digest.toString('hex')}`,
296
- ts,
297
- `0x${header.contentCommitment.blobsHash.toString('hex')}`,
298
- flags,
641
+ {
642
+ header: checkpoint.header.toViem(),
643
+ archive: toHex(checkpoint.archive.root.toBuffer()),
644
+ oracleInput: {
645
+ feeAssetPriceModifier: 0n,
646
+ },
647
+ },
648
+ attestationsAndSigners.getPackedAttestations(),
649
+ attestationsAndSigners.getSigners().map(signer => signer.toString()),
650
+ attestationsAndSignersSignature.toViemSignature(),
651
+ blobInput,
299
652
  ] as const;
300
653
 
301
- await this.rollupContract.validateHeader(args, this.getForwarderAddress().toString());
654
+ await this.simulateProposeTx(args, ts, options);
302
655
  return ts;
303
656
  }
304
657
 
305
- public async getCurrentEpochCommittee(): Promise<EthAddress[]> {
306
- const committee = await this.rollupContract.getCurrentEpochCommittee();
307
- return committee.map(EthAddress.fromString);
308
- }
309
-
310
- private async enqueueCastVoteHelper(
311
- slotNumber: bigint,
658
+ private async enqueueCastSignalHelper(
659
+ slotNumber: SlotNumber,
312
660
  timestamp: bigint,
313
- voteType: VoteType,
661
+ signalType: GovernanceSignalAction,
314
662
  payload: EthAddress,
315
663
  base: IEmpireBase,
664
+ signerAddress: EthAddress,
665
+ signer: (msg: TypedDataDefinition) => Promise<`0x${string}`>,
316
666
  ): Promise<boolean> {
317
- if (this.myLastVotes[voteType] >= slotNumber) {
667
+ if (this.lastActions[signalType] && this.lastActions[signalType] === slotNumber) {
668
+ this.log.debug(`Skipping duplicate vote cast signal ${signalType} for slot ${slotNumber}`);
318
669
  return false;
319
670
  }
320
671
  if (payload.equals(EthAddress.ZERO)) {
321
672
  return false;
322
673
  }
674
+ if (signerAddress.equals(EthAddress.ZERO)) {
675
+ this.log.warn(`Cannot enqueue vote cast signal ${signalType} for address zero at slot ${slotNumber}`);
676
+ return false;
677
+ }
323
678
  const round = await base.computeRound(slotNumber);
324
- const [proposer, roundInfo] = await Promise.all([
325
- this.rollupContract.getProposerAt(timestamp),
326
- base.getRoundInfo(this.rollupContract.address, round),
327
- ]);
679
+ const roundInfo = await base.getRoundInfo(this.rollupContract.address, round);
680
+
681
+ if (roundInfo.quorumReached) {
682
+ return false;
683
+ }
328
684
 
329
- if (proposer.toLowerCase() !== this.getForwarderAddress().toString().toLowerCase()) {
685
+ if (roundInfo.lastSignalSlot >= slotNumber) {
330
686
  return false;
331
687
  }
332
- if (roundInfo.lastVote >= slotNumber) {
688
+
689
+ if (await this.isPayloadEmpty(payload)) {
690
+ this.log.warn(`Skipping vote cast for payload with empty code`);
333
691
  return false;
334
692
  }
335
693
 
336
- const cachedLastVote = this.myLastVotes[voteType];
337
- this.myLastVotes[voteType] = slotNumber;
694
+ const cachedLastVote = this.lastActions[signalType];
695
+ this.lastActions[signalType] = slotNumber;
696
+ const action = signalType;
697
+
698
+ const request = await base.createSignalRequestWithSignature(
699
+ payload.toString(),
700
+ slotNumber,
701
+ this.config.l1ChainId,
702
+ signerAddress.toString(),
703
+ signer,
704
+ );
705
+ this.log.debug(`Created ${action} request with signature`, {
706
+ request,
707
+ round,
708
+ signer: this.l1TxUtils.client.account?.address,
709
+ lastValidL2Slot: slotNumber,
710
+ });
338
711
 
712
+ try {
713
+ await this.l1TxUtils.simulate(request, { time: timestamp }, [], mergeAbis([request.abi ?? [], ErrorsAbi]));
714
+ this.log.debug(`Simulation for ${action} at slot ${slotNumber} succeeded`, { request });
715
+ } catch (err) {
716
+ this.log.error(`Failed simulation for ${action} at slot ${slotNumber} (enqueuing the action anyway)`, err);
717
+ // Yes, we enqueue the request anyway, in case there was a bug with the simulation itself
718
+ }
719
+
720
+ // TODO(palla/slash): All votes (governance and slashing) should txTimeoutAt at the end of the slot.
339
721
  this.addRequest({
340
- action: voteType === VoteType.GOVERNANCE ? 'governance-vote' : 'slashing-vote',
341
- request: base.createVoteRequest(payload.toString()),
722
+ gasConfig: { gasLimit: SequencerPublisher.VOTE_GAS_GUESS },
723
+ action,
724
+ request,
342
725
  lastValidL2Slot: slotNumber,
343
- onResult: (_request, result) => {
344
- if (!result || result.receipt.status !== 'success') {
345
- this.myLastVotes[voteType] = cachedLastVote;
726
+ checkSuccess: (_request, result) => {
727
+ const success =
728
+ result &&
729
+ result.receipt &&
730
+ result.receipt.status === 'success' &&
731
+ tryExtractEvent(result.receipt.logs, base.address.toString(), EmpireBaseAbi, 'SignalCast');
732
+
733
+ const logData = { ...result, slotNumber, round, payload: payload.toString() };
734
+ if (!success) {
735
+ this.log.error(
736
+ `Signaling in ${action} for ${payload} at slot ${slotNumber} in round ${round} failed`,
737
+ logData,
738
+ );
739
+ this.lastActions[signalType] = cachedLastVote;
740
+ return false;
346
741
  } else {
347
- this.log.info(`Cast [${voteType}] vote for slot ${slotNumber}`);
742
+ this.log.info(
743
+ `Signaling in ${action} for ${payload} at slot ${slotNumber} in round ${round} succeeded`,
744
+ logData,
745
+ );
746
+ return true;
348
747
  }
349
748
  },
350
749
  });
351
750
  return true;
352
751
  }
353
752
 
354
- private async getVoteConfig(
355
- slotNumber: bigint,
356
- voteType: VoteType,
357
- ): Promise<{ payload: EthAddress; base: IEmpireBase } | undefined> {
358
- if (voteType === VoteType.GOVERNANCE) {
359
- return { payload: this.governancePayload, base: this.govProposerContract };
360
- } else if (voteType === VoteType.SLASHING) {
361
- if (!this.getSlashPayload) {
362
- return undefined;
363
- }
364
- const slashPayload = await this.getSlashPayload(slotNumber);
365
- if (!slashPayload) {
366
- return undefined;
367
- }
368
- return { payload: slashPayload, base: this.slashingProposerContract };
753
+ private async isPayloadEmpty(payload: EthAddress): Promise<boolean> {
754
+ const key = payload.toString();
755
+ const cached = this.isPayloadEmptyCache.get(key);
756
+ if (cached) {
757
+ return cached;
369
758
  }
370
- throw new Error('Unreachable: Invalid vote type');
759
+ const isEmpty = !(await this.l1TxUtils.getCode(payload));
760
+ this.isPayloadEmptyCache.set(key, isEmpty);
761
+ return isEmpty;
371
762
  }
372
763
 
373
764
  /**
374
- * Enqueues a castVote transaction to cast a vote for a given slot number.
375
- * @param slotNumber - The slot number to cast a vote for.
376
- * @param timestamp - The timestamp of the slot to cast a vote for.
377
- * @param voteType - The type of vote to cast.
378
- * @returns True if the vote was successfully enqueued, false otherwise.
765
+ * Enqueues a governance castSignal transaction to cast a signal for a given slot number.
766
+ * @param slotNumber - The slot number to cast a signal for.
767
+ * @param timestamp - The timestamp of the slot to cast a signal for.
768
+ * @returns True if the signal was successfully enqueued, false otherwise.
379
769
  */
380
- public async enqueueCastVote(slotNumber: bigint, timestamp: bigint, voteType: VoteType): Promise<boolean> {
381
- const voteConfig = await this.getVoteConfig(slotNumber, voteType);
382
- if (!voteConfig) {
770
+ public enqueueGovernanceCastSignal(
771
+ governancePayload: EthAddress,
772
+ slotNumber: SlotNumber,
773
+ timestamp: bigint,
774
+ signerAddress: EthAddress,
775
+ signer: (msg: TypedDataDefinition) => Promise<`0x${string}`>,
776
+ ): Promise<boolean> {
777
+ return this.enqueueCastSignalHelper(
778
+ slotNumber,
779
+ timestamp,
780
+ 'governance-signal',
781
+ governancePayload,
782
+ this.govProposerContract,
783
+ signerAddress,
784
+ signer,
785
+ );
786
+ }
787
+
788
+ /** Enqueues all slashing actions as returned by the slasher client. */
789
+ public async enqueueSlashingActions(
790
+ actions: ProposerSlashAction[],
791
+ slotNumber: SlotNumber,
792
+ timestamp: bigint,
793
+ signerAddress: EthAddress,
794
+ signer: (msg: TypedDataDefinition) => Promise<`0x${string}`>,
795
+ ): Promise<boolean> {
796
+ if (actions.length === 0) {
797
+ this.log.debug(`No slashing actions to enqueue for slot ${slotNumber}`);
383
798
  return false;
384
799
  }
385
- const { payload, base } = voteConfig;
386
- return this.enqueueCastVoteHelper(slotNumber, timestamp, voteType, payload, base);
800
+
801
+ for (const action of actions) {
802
+ switch (action.type) {
803
+ case 'vote-empire-payload': {
804
+ if (this.slashingProposerContract?.type !== 'empire') {
805
+ this.log.error('Cannot vote for empire payload on non-empire slashing contract');
806
+ break;
807
+ }
808
+ this.log.debug(`Enqueuing slashing vote for payload ${action.payload} at slot ${slotNumber}`, {
809
+ signerAddress,
810
+ });
811
+ await this.enqueueCastSignalHelper(
812
+ slotNumber,
813
+ timestamp,
814
+ 'empire-slashing-signal',
815
+ action.payload,
816
+ this.slashingProposerContract,
817
+ signerAddress,
818
+ signer,
819
+ );
820
+ break;
821
+ }
822
+
823
+ case 'create-empire-payload': {
824
+ this.log.debug(`Enqueuing slashing create payload at slot ${slotNumber}`, { slotNumber, signerAddress });
825
+ const request = this.slashFactoryContract.buildCreatePayloadRequest(action.data);
826
+ await this.simulateAndEnqueueRequest(
827
+ 'create-empire-payload',
828
+ request,
829
+ (receipt: TransactionReceipt) =>
830
+ !!this.slashFactoryContract.tryExtractSlashPayloadCreatedEvent(receipt.logs),
831
+ slotNumber,
832
+ timestamp,
833
+ );
834
+ break;
835
+ }
836
+
837
+ case 'execute-empire-payload': {
838
+ this.log.debug(`Enqueuing slashing execute payload at slot ${slotNumber}`, { slotNumber, signerAddress });
839
+ if (this.slashingProposerContract?.type !== 'empire') {
840
+ this.log.error('Cannot execute slashing payload on non-empire slashing contract');
841
+ return false;
842
+ }
843
+ const empireSlashingProposer = this.slashingProposerContract as EmpireSlashingProposerContract;
844
+ const request = empireSlashingProposer.buildExecuteRoundRequest(action.round);
845
+ await this.simulateAndEnqueueRequest(
846
+ 'execute-empire-payload',
847
+ request,
848
+ (receipt: TransactionReceipt) => !!empireSlashingProposer.tryExtractPayloadSubmittedEvent(receipt.logs),
849
+ slotNumber,
850
+ timestamp,
851
+ );
852
+ break;
853
+ }
854
+
855
+ case 'vote-offenses': {
856
+ this.log.debug(`Enqueuing slashing vote for ${action.votes.length} votes at slot ${slotNumber}`, {
857
+ slotNumber,
858
+ round: action.round,
859
+ votesCount: action.votes.length,
860
+ signerAddress,
861
+ });
862
+ if (this.slashingProposerContract?.type !== 'tally') {
863
+ this.log.error('Cannot vote for slashing offenses on non-tally slashing contract');
864
+ return false;
865
+ }
866
+ const tallySlashingProposer = this.slashingProposerContract as TallySlashingProposerContract;
867
+ const votes = bufferToHex(encodeSlashConsensusVotes(action.votes));
868
+ const request = await tallySlashingProposer.buildVoteRequestFromSigner(votes, slotNumber, signer);
869
+ await this.simulateAndEnqueueRequest(
870
+ 'vote-offenses',
871
+ request,
872
+ (receipt: TransactionReceipt) => !!tallySlashingProposer.tryExtractVoteCastEvent(receipt.logs),
873
+ slotNumber,
874
+ timestamp,
875
+ );
876
+ break;
877
+ }
878
+
879
+ case 'execute-slash': {
880
+ this.log.debug(`Enqueuing slash execution for round ${action.round} at slot ${slotNumber}`, {
881
+ slotNumber,
882
+ round: action.round,
883
+ signerAddress,
884
+ });
885
+ if (this.slashingProposerContract?.type !== 'tally') {
886
+ this.log.error('Cannot execute slashing offenses on non-tally slashing contract');
887
+ return false;
888
+ }
889
+ const tallySlashingProposer = this.slashingProposerContract as TallySlashingProposerContract;
890
+ const request = tallySlashingProposer.buildExecuteRoundRequest(action.round, action.committees);
891
+ await this.simulateAndEnqueueRequest(
892
+ 'execute-slash',
893
+ request,
894
+ (receipt: TransactionReceipt) => !!tallySlashingProposer.tryExtractRoundExecutedEvent(receipt.logs),
895
+ slotNumber,
896
+ timestamp,
897
+ );
898
+ break;
899
+ }
900
+
901
+ default: {
902
+ const _: never = action;
903
+ throw new Error(`Unknown slashing action type: ${(action as ProposerSlashAction).type}`);
904
+ }
905
+ }
906
+ }
907
+
908
+ return true;
387
909
  }
388
910
 
389
- /**
390
- * Proposes a L2 block on L1.
391
- *
392
- * @param block - L2 block to propose.
393
- * @returns True if the tx has been enqueued, throws otherwise. See #9315
394
- */
395
- public async enqueueProposeL2Block(
396
- block: L2Block,
397
- attestations?: Signature[],
398
- txHashes?: TxHash[],
399
- opts: { txTimeoutAt?: Date } = {},
400
- ): Promise<boolean> {
401
- const consensusPayload = new ConsensusPayload(block.header, block.archive.root, txHashes ?? []);
911
+ /** Simulates and enqueues a proposal for a checkpoint on L1 */
912
+ public async enqueueProposeCheckpoint(
913
+ checkpoint: Checkpoint,
914
+ attestationsAndSigners: CommitteeAttestationsAndSigners,
915
+ attestationsAndSignersSignature: Signature,
916
+ opts: { txTimeoutAt?: Date; forcePendingCheckpointNumber?: CheckpointNumber } = {},
917
+ ): Promise<void> {
918
+ const checkpointHeader = checkpoint.header;
402
919
 
403
- const digest = await getHashedSignaturePayload(consensusPayload, SignatureDomainSeparator.blockAttestation);
920
+ const blobFields = checkpoint.toBlobFields();
921
+ const blobs = getBlobsPerL1Block(blobFields);
404
922
 
405
- const blobs = await Blob.getBlobs(block.body.toBlobFields());
406
923
  const proposeTxArgs = {
407
- header: block.header.toBuffer(),
408
- archive: block.archive.root.toBuffer(),
409
- blockHash: (await block.header.hash()).toBuffer(),
410
- body: block.body.toBuffer(),
924
+ header: checkpointHeader,
925
+ archive: checkpoint.archive.root.toBuffer(),
411
926
  blobs,
412
- attestations,
413
- txHashes: txHashes ?? [],
927
+ attestationsAndSigners,
928
+ attestationsAndSignersSignature,
414
929
  };
415
930
 
416
- // @note This will make sure that we are passing the checks for our header ASSUMING that the data is also made available
417
- // This means that we can avoid the simulation issues in later checks.
418
- // By simulation issue, I mean the fact that the block.timestamp is equal to the last block, not the next, which
419
- // make time consistency checks break.
420
- const ts = await this.validateBlockForSubmission(block.header, {
421
- digest: digest.toBuffer(),
422
- signatures: attestations ?? [],
931
+ let ts: bigint;
932
+
933
+ try {
934
+ // @note This will make sure that we are passing the checks for our header ASSUMING that the data is also made available
935
+ // This means that we can avoid the simulation issues in later checks.
936
+ // By simulation issue, I mean the fact that the block.timestamp is equal to the last block, not the next, which
937
+ // make time consistency checks break.
938
+ // TODO(palla): Check whether we're validating twice, once here and once within addProposeTx, since we call simulateProposeTx in both places.
939
+ ts = await this.validateCheckpointForSubmission(
940
+ checkpoint,
941
+ attestationsAndSigners,
942
+ attestationsAndSignersSignature,
943
+ opts,
944
+ );
945
+ } catch (err: any) {
946
+ this.log.error(`Checkpoint validation failed. ${err instanceof Error ? err.message : 'No error message'}`, err, {
947
+ ...checkpoint.getStats(),
948
+ slotNumber: checkpoint.header.slotNumber,
949
+ forcePendingCheckpointNumber: opts.forcePendingCheckpointNumber,
950
+ });
951
+ throw err;
952
+ }
953
+
954
+ this.log.verbose(`Enqueuing checkpoint propose transaction`, { ...checkpoint.toCheckpointInfo(), ...opts });
955
+ await this.addProposeTx(checkpoint, proposeTxArgs, opts, ts);
956
+ }
957
+
958
+ public enqueueInvalidateCheckpoint(
959
+ request: InvalidateCheckpointRequest | undefined,
960
+ opts: { txTimeoutAt?: Date } = {},
961
+ ) {
962
+ if (!request) {
963
+ return;
964
+ }
965
+
966
+ // We issued the simulation against the rollup contract, so we need to account for the overhead of the multicall3
967
+ const gasLimit = this.l1TxUtils.bumpGasLimit(BigInt(Math.ceil((Number(request.gasUsed) * 64) / 63)));
968
+
969
+ const { gasUsed, checkpointNumber } = request;
970
+ const logData = { gasUsed, checkpointNumber, gasLimit, opts };
971
+ this.log.verbose(`Enqueuing invalidate checkpoint request`, logData);
972
+ this.addRequest({
973
+ action: `invalidate-by-${request.reason}`,
974
+ request: request.request,
975
+ gasConfig: { gasLimit, txTimeoutAt: opts.txTimeoutAt },
976
+ lastValidL2Slot: SlotNumber(this.getCurrentL2Slot() + 2),
977
+ checkSuccess: (_req, result) => {
978
+ const success =
979
+ result &&
980
+ result.receipt &&
981
+ result.receipt.status === 'success' &&
982
+ tryExtractEvent(result.receipt.logs, this.rollupContract.address, RollupAbi, 'CheckpointInvalidated');
983
+ if (!success) {
984
+ this.log.warn(`Invalidate checkpoint ${request.checkpointNumber} failed`, { ...result, ...logData });
985
+ } else {
986
+ this.log.info(`Invalidate checkpoint ${request.checkpointNumber} succeeded`, { ...result, ...logData });
987
+ }
988
+ return !!success;
989
+ },
423
990
  });
991
+ }
992
+
993
+ private async simulateAndEnqueueRequest(
994
+ action: Action,
995
+ request: L1TxRequest,
996
+ checkSuccess: (receipt: TransactionReceipt) => boolean | undefined,
997
+ slotNumber: SlotNumber,
998
+ timestamp: bigint,
999
+ ) {
1000
+ const logData = { slotNumber, timestamp, gasLimit: undefined as bigint | undefined };
1001
+ if (this.lastActions[action] && this.lastActions[action] === slotNumber) {
1002
+ this.log.debug(`Skipping duplicate action ${action} for slot ${slotNumber}`);
1003
+ return false;
1004
+ }
1005
+
1006
+ const cachedLastActionSlot = this.lastActions[action];
1007
+ this.lastActions[action] = slotNumber;
1008
+
1009
+ this.log.debug(`Simulating ${action} for slot ${slotNumber}`, logData);
1010
+
1011
+ let gasUsed: bigint;
1012
+ const simulateAbi = mergeAbis([request.abi ?? [], ErrorsAbi]);
1013
+ try {
1014
+ ({ gasUsed } = await this.l1TxUtils.simulate(request, { time: timestamp }, [], simulateAbi)); // TODO(palla/slash): Check the timestamp logic
1015
+ this.log.verbose(`Simulation for ${action} succeeded`, { ...logData, request, gasUsed });
1016
+ } catch (err) {
1017
+ const viemError = formatViemError(err, simulateAbi);
1018
+ this.log.error(`Simulation for ${action} at ${slotNumber} failed`, viemError, logData);
1019
+
1020
+ return false;
1021
+ }
1022
+
1023
+ // We issued the simulation against the rollup contract, so we need to account for the overhead of the multicall3
1024
+ const gasLimit = this.l1TxUtils.bumpGasLimit(BigInt(Math.ceil((Number(gasUsed) * 64) / 63)));
1025
+ logData.gasLimit = gasLimit;
424
1026
 
425
- this.log.debug(`Submitting propose transaction`);
426
- await this.addProposeTx(block, proposeTxArgs, opts, ts);
1027
+ // Store the ABI used for simulation on the request so Multicall3.forward can decode errors
1028
+ // when the tx is sent and a revert is diagnosed via simulation.
1029
+ const requestWithAbi = { ...request, abi: simulateAbi };
1030
+
1031
+ this.log.debug(`Enqueuing ${action}`, logData);
1032
+ this.addRequest({
1033
+ action,
1034
+ request: requestWithAbi,
1035
+ gasConfig: { gasLimit },
1036
+ lastValidL2Slot: slotNumber,
1037
+ checkSuccess: (_req, result) => {
1038
+ const success = result && result.receipt && result.receipt.status === 'success' && checkSuccess(result.receipt);
1039
+ if (!success) {
1040
+ this.log.warn(`Action ${action} at ${slotNumber} failed`, { ...result, ...logData });
1041
+ this.lastActions[action] = cachedLastActionSlot;
1042
+ } else {
1043
+ this.log.info(`Action ${action} at ${slotNumber} succeeded`, { ...result, ...logData });
1044
+ }
1045
+ return !!success;
1046
+ },
1047
+ });
427
1048
  return true;
428
1049
  }
429
1050
 
@@ -444,114 +1065,190 @@ export class SequencerPublisher {
444
1065
  this.l1TxUtils.restart();
445
1066
  }
446
1067
 
447
- private async prepareProposeTx(encodedData: L1ProcessArgs, timestamp: bigint) {
1068
+ private async prepareProposeTx(
1069
+ encodedData: L1ProcessArgs,
1070
+ timestamp: bigint,
1071
+ options: { forcePendingCheckpointNumber?: CheckpointNumber },
1072
+ ) {
448
1073
  const kzg = Blob.getViemKzgInstance();
449
- const blobInput = Blob.getEthBlobEvaluationInputs(encodedData.blobs);
1074
+ const blobInput = getPrefixedEthBlobCommitments(encodedData.blobs);
450
1075
  this.log.debug('Validating blob input', { blobInput });
451
- const blobEvaluationGas = await this.l1TxUtils
452
- .estimateGas(
453
- this.l1TxUtils.walletClient.account,
454
- {
455
- to: this.rollupContract.address,
456
- data: encodeFunctionData({
457
- abi: RollupAbi,
458
- functionName: 'validateBlobs',
459
- args: [blobInput],
460
- }),
461
- },
462
- {},
463
- {
464
- blobs: encodedData.blobs.map(b => b.data),
465
- kzg,
466
- },
467
- )
468
- .catch(err => {
469
- const { message, metaMessages } = formatViemError(err);
470
- this.log.error(`Failed to validate blobs`, message, { metaMessages });
471
- throw new Error('Failed to validate blobs');
472
- });
473
1076
 
474
- const attestations = encodedData.attestations
475
- ? encodedData.attestations.map(attest => attest.toViemSignature())
476
- : [];
477
- const txHashes = encodedData.txHashes ? encodedData.txHashes.map(txHash => txHash.toString()) : [];
1077
+ // Get blob evaluation gas
1078
+ let blobEvaluationGas: bigint;
1079
+ if (this.config.fishermanMode) {
1080
+ // In fisherman mode, we can't estimate blob gas because estimateGas doesn't support state overrides
1081
+ // Use a fixed estimate.
1082
+ blobEvaluationGas = BigInt(encodedData.blobs.length) * 21_000n;
1083
+ this.log.debug(`Using fixed blob evaluation gas estimate in fisherman mode: ${blobEvaluationGas}`);
1084
+ } else {
1085
+ // Normal mode - use estimateGas with blob inputs
1086
+ blobEvaluationGas = await this.l1TxUtils
1087
+ .estimateGas(
1088
+ this.getSenderAddress().toString(),
1089
+ {
1090
+ to: this.rollupContract.address,
1091
+ data: encodeFunctionData({
1092
+ abi: RollupAbi,
1093
+ functionName: 'validateBlobs',
1094
+ args: [blobInput],
1095
+ }),
1096
+ },
1097
+ {},
1098
+ {
1099
+ blobs: encodedData.blobs.map(b => b.data),
1100
+ kzg,
1101
+ },
1102
+ )
1103
+ .catch(err => {
1104
+ const { message, metaMessages } = formatViemError(err);
1105
+ this.log.error(`Failed to validate blobs`, message, { metaMessages });
1106
+ throw new Error('Failed to validate blobs');
1107
+ });
1108
+ }
1109
+ const signers = encodedData.attestationsAndSigners.getSigners().map(signer => signer.toString());
1110
+
478
1111
  const args = [
479
1112
  {
480
- header: `0x${encodedData.header.toString('hex')}`,
481
- archive: `0x${encodedData.archive.toString('hex')}`,
1113
+ header: encodedData.header.toViem(),
1114
+ archive: toHex(encodedData.archive),
482
1115
  oracleInput: {
483
1116
  // We are currently not modifying these. See #9963
484
1117
  feeAssetPriceModifier: 0n,
485
1118
  },
486
- blockHash: `0x${encodedData.blockHash.toString('hex')}`,
487
- txHashes,
488
1119
  },
489
- attestations,
1120
+ encodedData.attestationsAndSigners.getPackedAttestations(),
1121
+ signers,
1122
+ encodedData.attestationsAndSignersSignature.toViemSignature(),
490
1123
  blobInput,
491
1124
  ] as const;
492
1125
 
1126
+ const { rollupData, simulationResult } = await this.simulateProposeTx(args, timestamp, options);
1127
+
1128
+ return { args, blobEvaluationGas, rollupData, simulationResult };
1129
+ }
1130
+
1131
+ /**
1132
+ * Simulates the propose tx with eth_simulateV1
1133
+ * @param args - The propose tx args
1134
+ * @param timestamp - The timestamp to simulate proposal at
1135
+ * @returns The simulation result
1136
+ */
1137
+ private async simulateProposeTx(
1138
+ args: readonly [
1139
+ {
1140
+ readonly header: ViemHeader;
1141
+ readonly archive: `0x${string}`;
1142
+ readonly oracleInput: {
1143
+ readonly feeAssetPriceModifier: 0n;
1144
+ };
1145
+ },
1146
+ ViemCommitteeAttestations,
1147
+ `0x${string}`[], // Signers
1148
+ ViemSignature,
1149
+ `0x${string}`,
1150
+ ],
1151
+ timestamp: bigint,
1152
+ options: { forcePendingCheckpointNumber?: CheckpointNumber },
1153
+ ) {
493
1154
  const rollupData = encodeFunctionData({
494
1155
  abi: RollupAbi,
495
1156
  functionName: 'propose',
496
1157
  args,
497
1158
  });
498
1159
 
499
- const forwarderData = encodeFunctionData({
500
- abi: ForwarderAbi,
501
- functionName: 'forward',
502
- args: [[this.rollupContract.address], [rollupData]],
503
- });
1160
+ // override the pending checkpoint number if requested
1161
+ const forcePendingCheckpointNumberStateDiff = (
1162
+ options.forcePendingCheckpointNumber !== undefined
1163
+ ? await this.rollupContract.makePendingCheckpointNumberOverride(options.forcePendingCheckpointNumber)
1164
+ : []
1165
+ ).flatMap(override => override.stateDiff ?? []);
1166
+
1167
+ const stateOverrides: StateOverride = [
1168
+ {
1169
+ address: this.rollupContract.address,
1170
+ // @note we override checkBlob to false since blobs are not part simulate()
1171
+ stateDiff: [
1172
+ { slot: toPaddedHex(RollupContract.checkBlobStorageSlot, true), value: toPaddedHex(0n, true) },
1173
+ ...forcePendingCheckpointNumberStateDiff,
1174
+ ],
1175
+ },
1176
+ ];
1177
+ // In fisherman mode, simulate as the proposer but with sufficient balance
1178
+ if (this.proposerAddressForSimulation) {
1179
+ stateOverrides.push({
1180
+ address: this.proposerAddressForSimulation.toString(),
1181
+ balance: 10n * WEI_CONST * WEI_CONST, // 10 ETH
1182
+ });
1183
+ }
504
1184
 
505
1185
  const simulationResult = await this.l1TxUtils
506
- .simulateGasUsed(
1186
+ .simulate(
507
1187
  {
508
- to: this.getForwarderAddress().toString(),
509
- data: forwarderData,
510
- gas: SequencerPublisher.PROPOSE_GAS_GUESS,
1188
+ to: this.rollupContract.address,
1189
+ data: rollupData,
1190
+ gas: MAX_L1_TX_LIMIT,
1191
+ ...(this.proposerAddressForSimulation && { from: this.proposerAddressForSimulation.toString() }),
511
1192
  },
512
1193
  {
513
1194
  // @note we add 1n to the timestamp because geth implementation doesn't like simulation timestamp to be equal to the current block timestamp
514
1195
  time: timestamp + 1n,
515
- // @note reth should have a 30m gas limit per block but throws errors that this tx is beyond limit
516
- gasLimit: SequencerPublisher.PROPOSE_GAS_GUESS * 2n,
1196
+ // @note reth should have a 30m gas limit per block but throws errors that this tx is beyond limit so we increase here
1197
+ gasLimit: MAX_L1_TX_LIMIT * 2n,
517
1198
  },
518
- [
519
- {
520
- address: this.rollupContract.address,
521
- // @note we override checkBlob to false since blobs are not part simulate()
522
- stateDiff: [
523
- {
524
- slot: toHex(RollupContract.checkBlobStorageSlot, true),
525
- value: toHex(0n, true),
526
- },
527
- ],
528
- },
529
- ],
1199
+ stateOverrides,
1200
+ RollupAbi,
530
1201
  {
531
1202
  // @note fallback gas estimate to use if the node doesn't support simulation API
532
- fallbackGasEstimate: SequencerPublisher.PROPOSE_GAS_GUESS,
1203
+ fallbackGasEstimate: MAX_L1_TX_LIMIT,
533
1204
  },
534
1205
  )
535
1206
  .catch(err => {
536
- const { message, metaMessages } = formatViemError(err);
537
- this.log.error(`Failed to simulate gas used`, message, { metaMessages });
538
- throw new Error('Failed to simulate gas used');
1207
+ // In fisherman mode, we expect ValidatorSelection__MissingProposerSignature since fisherman doesn't have proposer signature
1208
+ const viemError = formatViemError(err);
1209
+ if (this.config.fishermanMode && viemError.message?.includes('ValidatorSelection__MissingProposerSignature')) {
1210
+ this.log.debug(`Ignoring expected ValidatorSelection__MissingProposerSignature error in fisherman mode`);
1211
+ // Return a minimal simulation result with the fallback gas estimate
1212
+ return {
1213
+ gasUsed: MAX_L1_TX_LIMIT,
1214
+ logs: [],
1215
+ };
1216
+ }
1217
+ this.log.error(`Failed to simulate propose tx`, viemError);
1218
+ throw err;
539
1219
  });
540
1220
 
541
- return { args, blobEvaluationGas, rollupData, simulationResult };
1221
+ return { rollupData, simulationResult };
542
1222
  }
543
1223
 
544
1224
  private async addProposeTx(
545
- block: L2Block,
1225
+ checkpoint: Checkpoint,
546
1226
  encodedData: L1ProcessArgs,
547
- opts: { txTimeoutAt?: Date } = {},
1227
+ opts: { txTimeoutAt?: Date; forcePendingCheckpointNumber?: CheckpointNumber } = {},
548
1228
  timestamp: bigint,
549
1229
  ): Promise<void> {
1230
+ const slot = checkpoint.header.slotNumber;
550
1231
  const timer = new Timer();
551
1232
  const kzg = Blob.getViemKzgInstance();
552
- const { rollupData, simulationResult, blobEvaluationGas } = await this.prepareProposeTx(encodedData, timestamp);
1233
+ const { rollupData, simulationResult, blobEvaluationGas } = await this.prepareProposeTx(
1234
+ encodedData,
1235
+ timestamp,
1236
+ opts,
1237
+ );
553
1238
  const startBlock = await this.l1TxUtils.getBlockNumber();
554
- const blockHash = await block.hash();
1239
+ const gasLimit = this.l1TxUtils.bumpGasLimit(
1240
+ BigInt(Math.ceil((Number(simulationResult.gasUsed) * 64) / 63)) +
1241
+ blobEvaluationGas +
1242
+ SequencerPublisher.MULTICALL_OVERHEAD_GAS_GUESS, // We issue the simulation against the rollup contract, so we need to account for the overhead of the multicall3
1243
+ );
1244
+
1245
+ // Send the blobs to the blob client preemptively. This helps in tests where the sequencer mistakingly thinks that the propose
1246
+ // tx fails but it does get mined. We make sure that the blobs are sent to the blob client regardless of the tx outcome.
1247
+ void Promise.resolve().then(() =>
1248
+ this.blobClient.sendBlobsToFilestore(encodedData.blobs).catch(_err => {
1249
+ this.log.error('Failed to send blobs to blob client');
1250
+ }),
1251
+ );
555
1252
 
556
1253
  return this.addRequest({
557
1254
  action: 'propose',
@@ -559,67 +1256,58 @@ export class SequencerPublisher {
559
1256
  to: this.rollupContract.address,
560
1257
  data: rollupData,
561
1258
  },
562
- lastValidL2Slot: block.header.globalVariables.slotNumber.toBigInt(),
563
- gasConfig: {
564
- ...opts,
565
- gasLimit: this.l1TxUtils.bumpGasLimit(simulationResult + blobEvaluationGas),
566
- },
1259
+ lastValidL2Slot: checkpoint.header.slotNumber,
1260
+ gasConfig: { ...opts, gasLimit },
567
1261
  blobConfig: {
568
1262
  blobs: encodedData.blobs.map(b => b.data),
569
1263
  kzg,
570
1264
  },
571
- onResult: (request, result) => {
1265
+ checkSuccess: (_request, result) => {
572
1266
  if (!result) {
573
- return;
1267
+ return false;
574
1268
  }
575
1269
  const { receipt, stats, errorMsg } = result;
576
- if (receipt.status === 'success') {
1270
+ const success =
1271
+ receipt &&
1272
+ receipt.status === 'success' &&
1273
+ tryExtractEvent(receipt.logs, this.rollupContract.address, RollupAbi, 'CheckpointProposed');
1274
+
1275
+ if (success) {
577
1276
  const endBlock = receipt.blockNumber;
578
1277
  const inclusionBlocks = Number(endBlock - startBlock);
579
- const publishStats: L1PublishBlockStats = {
1278
+ const { calldataGas, calldataSize, sender } = stats!;
1279
+ const publishStats: L1PublishCheckpointStats = {
580
1280
  gasPrice: receipt.effectiveGasPrice,
581
1281
  gasUsed: receipt.gasUsed,
582
1282
  blobGasUsed: receipt.blobGasUsed ?? 0n,
583
1283
  blobDataGas: receipt.blobGasPrice ?? 0n,
584
1284
  transactionHash: receipt.transactionHash,
585
- ...pick(stats!, 'calldataGas', 'calldataSize', 'sender'),
586
- ...block.getStats(),
1285
+ calldataGas,
1286
+ calldataSize,
1287
+ sender,
1288
+ ...checkpoint.getStats(),
587
1289
  eventName: 'rollup-published-to-l1',
588
1290
  blobCount: encodedData.blobs.length,
589
1291
  inclusionBlocks,
590
1292
  };
591
- this.log.verbose(`Published L2 block to L1 rollup contract`, { ...stats, ...block.getStats() });
592
- this.metrics.recordProcessBlockTx(timer.ms(), publishStats);
593
-
594
- // Send the blobs to the blob sink
595
- this.sendBlobsToBlobSink(receipt.blockHash, encodedData.blobs).catch(_err => {
596
- this.log.error('Failed to send blobs to blob sink');
1293
+ this.log.info(`Published checkpoint ${checkpoint.number} at slot ${slot} to rollup contract`, {
1294
+ ...stats,
1295
+ ...checkpoint.getStats(),
1296
+ ...pick(receipt, 'transactionHash', 'blockHash'),
597
1297
  });
1298
+ this.metrics.recordProcessBlockTx(timer.ms(), publishStats);
598
1299
 
599
1300
  return true;
600
1301
  } else {
601
1302
  this.metrics.recordFailedTx('process');
602
-
603
- this.log.error(`Rollup process tx reverted. ${errorMsg ?? 'No error message'}`, undefined, {
604
- ...block.getStats(),
605
- txHash: receipt.transactionHash,
606
- blockHash,
607
- slotNumber: block.header.globalVariables.slotNumber.toBigInt(),
608
- });
1303
+ this.log.error(
1304
+ `Publishing checkpoint at slot ${slot} failed with ${errorMsg ?? 'no error message'}`,
1305
+ undefined,
1306
+ { ...checkpoint.getStats(), ...receipt },
1307
+ );
1308
+ return false;
609
1309
  }
610
1310
  },
611
1311
  });
612
1312
  }
613
-
614
- /**
615
- * Send blobs to the blob sink
616
- *
617
- * If a blob sink url is configured, then we send blobs to the blob sink
618
- * - for now we use the blockHash as the identifier for the blobs;
619
- * In the future this will move to be the beacon block id - which takes a bit more work
620
- * to calculate and will need to be mocked in e2e tests
621
- */
622
- protected sendBlobsToBlobSink(blockHash: string, blobs: Blob[]): Promise<boolean> {
623
- return this.blobSinkClient.sendBlobsToBlobSink(blockHash, blobs);
624
- }
625
1313
  }