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

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