@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-commit.5476d83

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