@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-commit.1142ef1

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