@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-fake-ceab37513c

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 +46 -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 +42 -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 +1 -0
  23. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  24. package/dest/publisher/sequencer-publisher-metrics.js +36 -1
  25. package/dest/publisher/sequencer-publisher.d.ts +101 -68
  26. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  27. package/dest/publisher/sequencer-publisher.js +603 -211
  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 +126 -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 +118 -79
  43. package/dest/sequencer/sequencer.d.ts.map +1 -1
  44. package/dest/sequencer/sequencer.js +588 -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 +56 -29
  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 +24 -24
  59. package/package.json +42 -43
  60. package/src/client/sequencer-client.ts +94 -84
  61. package/src/config.ts +53 -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 +89 -0
  67. package/src/publisher/sequencer-publisher-metrics.ts +23 -1
  68. package/src/publisher/sequencer-publisher.ts +725 -234
  69. package/src/sequencer/block_builder.ts +216 -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 +758 -414
  75. package/src/sequencer/timetable.ts +103 -32
  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 +39 -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,74 +1,89 @@
1
1
  import { Blob } from '@aztec/blob-lib';
2
2
  import { createBlobSinkClient } from '@aztec/blob-sink/client';
3
- import { FormattedViemError, RollupContract, formatViemError } from '@aztec/ethereum';
4
- import { toHex } from '@aztec/foundation/bigint-buffer';
3
+ import { FormattedViemError, MULTI_CALL_3_ADDRESS, Multicall3, RollupContract, formatViemError, tryExtractEvent } from '@aztec/ethereum';
4
+ import { sumBigint } from '@aztec/foundation/bigint';
5
+ import { toHex as toPaddedHex } from '@aztec/foundation/bigint-buffer';
5
6
  import { EthAddress } from '@aztec/foundation/eth-address';
7
+ import { Signature } from '@aztec/foundation/eth-signature';
6
8
  import { createLogger } from '@aztec/foundation/log';
9
+ import { bufferToHex } from '@aztec/foundation/string';
7
10
  import { Timer } from '@aztec/foundation/timer';
8
- import { ForwarderAbi, RollupAbi } from '@aztec/l1-artifacts';
9
- import { ConsensusPayload, SignatureDomainSeparator, getHashedSignaturePayload } from '@aztec/stdlib/p2p';
11
+ import { EmpireBaseAbi, ErrorsAbi, RollupAbi } from '@aztec/l1-artifacts';
12
+ import { encodeSlashConsensusVotes } from '@aztec/slasher';
13
+ import { CommitteeAttestation, CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
10
14
  import { getTelemetryClient } from '@aztec/telemetry-client';
11
- import pick from 'lodash.pick';
12
- import { encodeFunctionData } from 'viem';
15
+ import { encodeFunctionData, toHex } from 'viem';
13
16
  import { SequencerPublisherMetrics } from './sequencer-publisher-metrics.js';
14
- export var VoteType = /*#__PURE__*/ function(VoteType) {
15
- VoteType[VoteType["GOVERNANCE"] = 0] = "GOVERNANCE";
16
- VoteType[VoteType["SLASHING"] = 1] = "SLASHING";
17
- return VoteType;
18
- }({});
17
+ export const Actions = [
18
+ 'invalidate-by-invalid-attestation',
19
+ 'invalidate-by-insufficient-attestations',
20
+ 'propose',
21
+ 'governance-signal',
22
+ 'empire-slashing-signal',
23
+ 'create-empire-payload',
24
+ 'execute-empire-payload',
25
+ 'vote-offenses',
26
+ 'execute-slash'
27
+ ];
28
+ // Sorting for actions such that invalidations go before proposals, and proposals go before votes
29
+ export const compareActions = (a, b)=>Actions.indexOf(a) - Actions.indexOf(b);
19
30
  export class SequencerPublisher {
20
- interrupted = false;
31
+ config;
32
+ interrupted;
21
33
  metrics;
22
34
  epochCache;
23
- forwarderContract;
24
- governanceLog = createLogger('sequencer:publisher:governance');
25
- governanceProposerAddress;
26
- governancePayload = EthAddress.ZERO;
27
- slashingLog = createLogger('sequencer:publisher:slashing');
28
- slashingProposerAddress;
29
- getSlashPayload = undefined;
30
- myLastVotes = {
31
- [0]: 0n,
32
- [1]: 0n
33
- };
34
- log = createLogger('sequencer:publisher');
35
+ governanceLog;
36
+ slashingLog;
37
+ lastActions;
38
+ log;
35
39
  ethereumSlotDuration;
36
40
  blobSinkClient;
37
41
  // @note - with blobs, the below estimate seems too large.
38
42
  // Total used for full block from int_l1_pub e2e test: 1m (of which 86k is 1x blob)
39
43
  // Total used for emptier block from above test: 429k (of which 84k is 1x blob)
40
44
  static PROPOSE_GAS_GUESS = 12_000_000n;
45
+ // A CALL to a cold address is 2700 gas
46
+ static MULTICALL_OVERHEAD_GAS_GUESS = 5000n;
47
+ // Gas report for VotingWithSigTest shows a max gas of 100k, but we've seen it cost 700k+ in testnet
48
+ static VOTE_GAS_GUESS = 800_000n;
41
49
  l1TxUtils;
42
50
  rollupContract;
43
51
  govProposerContract;
44
52
  slashingProposerContract;
45
- requests = [];
53
+ slashFactoryContract;
54
+ requests;
46
55
  constructor(config, deps){
56
+ this.config = config;
57
+ this.interrupted = false;
58
+ this.governanceLog = createLogger('sequencer:publisher:governance');
59
+ this.slashingLog = createLogger('sequencer:publisher:slashing');
60
+ this.lastActions = {};
61
+ this.requests = [];
62
+ this.log = deps.log ?? createLogger('sequencer:publisher');
47
63
  this.ethereumSlotDuration = BigInt(config.ethereumSlotDuration);
48
64
  this.epochCache = deps.epochCache;
49
- this.blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config);
65
+ this.lastActions = deps.lastActions;
66
+ this.blobSinkClient = deps.blobSinkClient ?? createBlobSinkClient(config, {
67
+ logger: createLogger('sequencer:blob-sink:client')
68
+ });
50
69
  const telemetry = deps.telemetry ?? getTelemetryClient();
51
- this.metrics = new SequencerPublisherMetrics(telemetry, 'SequencerPublisher');
70
+ this.metrics = deps.metrics ?? new SequencerPublisherMetrics(telemetry, 'SequencerPublisher');
52
71
  this.l1TxUtils = deps.l1TxUtils;
53
72
  this.rollupContract = deps.rollupContract;
54
- this.forwarderContract = deps.forwarderContract;
55
73
  this.govProposerContract = deps.governanceProposerContract;
56
74
  this.slashingProposerContract = deps.slashingProposerContract;
75
+ this.rollupContract.listenToSlasherChanged(async ()=>{
76
+ this.log.info('Slashing proposer changed');
77
+ const newSlashingProposer = await this.rollupContract.getSlashingProposer();
78
+ this.slashingProposerContract = newSlashingProposer;
79
+ });
80
+ this.slashFactoryContract = deps.slashFactoryContract;
57
81
  }
58
- registerSlashPayloadGetter(callback) {
59
- this.getSlashPayload = callback;
60
- }
61
- getForwarderAddress() {
62
- return EthAddress.fromString(this.forwarderContract.getAddress());
82
+ getRollupContract() {
83
+ return this.rollupContract;
63
84
  }
64
85
  getSenderAddress() {
65
- return EthAddress.fromString(this.l1TxUtils.getSenderAddress());
66
- }
67
- getGovernancePayload() {
68
- return this.governancePayload;
69
- }
70
- setGovernancePayload(payload) {
71
- this.governancePayload = payload;
86
+ return this.l1TxUtils.getSenderAddress();
72
87
  }
73
88
  addRequest(request) {
74
89
  this.requests.push(request);
@@ -91,8 +106,10 @@ export class SequencerPublisher {
91
106
  return undefined;
92
107
  }
93
108
  const currentL2Slot = this.getCurrentL2Slot();
94
- this.log.debug(`Current L2 slot: ${currentL2Slot}`);
109
+ this.log.debug(`Sending requests on L2 slot ${currentL2Slot}`);
95
110
  const validRequests = requestsToProcess.filter((request)=>request.lastValidL2Slot >= currentL2Slot);
111
+ const validActions = validRequests.map((x)=>x.action);
112
+ const expiredActions = requestsToProcess.filter((request)=>request.lastValidL2Slot < currentL2Slot).map((x)=>x.action);
96
113
  if (validRequests.length !== requestsToProcess.length) {
97
114
  this.log.warn(`Some requests were expired for slot ${currentL2Slot}`, {
98
115
  validRequests: validRequests.map((request)=>({
@@ -109,56 +126,96 @@ export class SequencerPublisher {
109
126
  this.log.debug(`No valid requests to send`);
110
127
  return undefined;
111
128
  }
112
- // @note - we can only have one gas config and one blob config per bundle
129
+ // @note - we can only have one blob config per bundle
113
130
  // find requests with gas and blob configs
114
131
  // See https://github.com/AztecProtocol/aztec-packages/issues/11513
115
- const gasConfigs = requestsToProcess.filter((request)=>request.gasConfig);
116
- const blobConfigs = requestsToProcess.filter((request)=>request.blobConfig);
117
- if (gasConfigs.length > 1 || blobConfigs.length > 1) {
118
- throw new Error('Multiple gas or blob configs found');
132
+ const gasConfigs = requestsToProcess.filter((request)=>request.gasConfig).map((request)=>request.gasConfig);
133
+ const blobConfigs = requestsToProcess.filter((request)=>request.blobConfig).map((request)=>request.blobConfig);
134
+ if (blobConfigs.length > 1) {
135
+ throw new Error('Multiple blob configs found');
119
136
  }
120
- const gasConfig = gasConfigs[0]?.gasConfig;
121
- const blobConfig = blobConfigs[0]?.blobConfig;
137
+ const blobConfig = blobConfigs[0];
138
+ // Merge gasConfigs. Yields the sum of gasLimits, and the earliest txTimeoutAt, or undefined if no gasConfig sets them.
139
+ const gasLimits = gasConfigs.map((g)=>g?.gasLimit).filter((g)=>g !== undefined);
140
+ const gasLimit = gasLimits.length > 0 ? sumBigint(gasLimits) : undefined; // sum
141
+ const txTimeoutAts = gasConfigs.map((g)=>g?.txTimeoutAt).filter((g)=>g !== undefined);
142
+ const txTimeoutAt = txTimeoutAts.length > 0 ? new Date(Math.min(...txTimeoutAts.map((g)=>g.getTime()))) : undefined; // earliest
143
+ const txConfig = {
144
+ gasLimit,
145
+ txTimeoutAt
146
+ };
147
+ // Sort the requests so that proposals always go first
148
+ // This ensures the committee gets precomputed correctly
149
+ validRequests.sort((a, b)=>compareActions(a.action, b.action));
122
150
  try {
123
151
  this.log.debug('Forwarding transactions', {
124
- validRequests: validRequests.map((request)=>request.action)
152
+ validRequests: validRequests.map((request)=>request.action),
153
+ txConfig
125
154
  });
126
- const result = await this.forwarderContract.forward(validRequests.map((request)=>request.request), this.l1TxUtils, gasConfig, blobConfig, this.log);
127
- this.callbackBundledTransactions(validRequests, result);
128
- return result;
155
+ const result = await Multicall3.forward(validRequests.map((request)=>request.request), this.l1TxUtils, txConfig, blobConfig, this.rollupContract.address, this.log);
156
+ const { successfulActions = [], failedActions = [] } = this.callbackBundledTransactions(validRequests, result);
157
+ return {
158
+ result,
159
+ expiredActions,
160
+ sentActions: validActions,
161
+ successfulActions,
162
+ failedActions
163
+ };
129
164
  } catch (err) {
130
165
  const viemError = formatViemError(err);
131
166
  this.log.error(`Failed to publish bundled transactions`, viemError);
132
167
  return undefined;
133
168
  } finally{
134
169
  try {
135
- this.metrics.recordSenderBalance(await this.l1TxUtils.getSenderBalance(), this.l1TxUtils.getSenderAddress());
170
+ this.metrics.recordSenderBalance(await this.l1TxUtils.getSenderBalance(), this.l1TxUtils.getSenderAddress().toString());
136
171
  } catch (err) {
137
172
  this.log.warn(`Failed to record balance after sending tx: ${err}`);
138
173
  }
139
174
  }
140
175
  }
141
176
  callbackBundledTransactions(requests, result) {
142
- const success = result?.receipt.status === 'success';
143
- const logger = success ? this.log.info : this.log.error;
144
- for (const request of requests){
145
- logger(`Bundled [${request.action}] transaction [${success ? 'succeeded' : 'failed'}]`);
146
- request.onResult?.(request.request, result);
177
+ const actionsListStr = requests.map((r)=>r.action).join(', ');
178
+ if (result instanceof FormattedViemError) {
179
+ this.log.error(`Failed to publish bundled transactions (${actionsListStr})`, result);
180
+ return {
181
+ failedActions: requests.map((r)=>r.action)
182
+ };
183
+ } else {
184
+ this.log.verbose(`Published bundled transactions (${actionsListStr})`, {
185
+ result,
186
+ requests
187
+ });
188
+ const successfulActions = [];
189
+ const failedActions = [];
190
+ for (const request of requests){
191
+ if (request.checkSuccess(request.request, result)) {
192
+ successfulActions.push(request.action);
193
+ } else {
194
+ failedActions.push(request.action);
195
+ }
196
+ }
197
+ return {
198
+ successfulActions,
199
+ failedActions
200
+ };
147
201
  }
148
202
  }
149
203
  /**
150
204
  * @notice Will call `canProposeAtNextEthBlock` to make sure that it is possible to propose
151
205
  * @param tipArchive - The archive to check
152
206
  * @returns The slot and block number if it is possible to propose, undefined otherwise
153
- */ canProposeAtNextEthBlock(tipArchive) {
207
+ */ canProposeAtNextEthBlock(tipArchive, msgSender, opts = {}) {
208
+ // TODO: #14291 - should loop through multiple keys to check if any of them can propose
154
209
  const ignoredErrors = [
155
210
  'SlotAlreadyInChain',
156
211
  'InvalidProposer',
157
212
  'InvalidArchive'
158
213
  ];
159
- return this.rollupContract.canProposeAtNextEthBlock(tipArchive, this.getForwarderAddress().toString(), this.ethereumSlotDuration).catch((err)=>{
214
+ return this.rollupContract.canProposeAtNextEthBlock(tipArchive.toBuffer(), msgSender.toString(), this.ethereumSlotDuration, opts).catch((err)=>{
160
215
  if (err instanceof FormattedViemError && ignoredErrors.find((e)=>err.message.includes(e))) {
161
- this.log.debug(err.message);
216
+ this.log.warn(`Failed canProposeAtTime check with ${ignoredErrors.find((e)=>err.message.includes(e))}`, {
217
+ error: err.message
218
+ });
162
219
  } else {
163
220
  this.log.error(err.name, err);
164
221
  }
@@ -166,137 +223,475 @@ export class SequencerPublisher {
166
223
  });
167
224
  }
168
225
  /**
169
- * @notice Will call `validateHeader` to make sure that it is possible to propose
226
+ * @notice Will simulate `validateHeader` to make sure that the block header is valid
227
+ * @dev This is a convenience function that can be used by the sequencer to validate a "partial" header.
228
+ * It will throw if the block header is invalid.
229
+ * @param header - The block header to validate
230
+ */ async validateBlockHeader(header, opts) {
231
+ const flags = {
232
+ ignoreDA: true,
233
+ ignoreSignatures: true
234
+ };
235
+ const args = [
236
+ header.toViem(),
237
+ CommitteeAttestationsAndSigners.empty().getPackedAttestations(),
238
+ [],
239
+ Signature.empty().toViemSignature(),
240
+ `0x${'0'.repeat(64)}`,
241
+ header.contentCommitment.blobsHash.toString(),
242
+ flags
243
+ ];
244
+ const ts = BigInt((await this.l1TxUtils.getBlock()).timestamp + this.ethereumSlotDuration);
245
+ // use sender balance to simulate
246
+ const balance = await this.l1TxUtils.getSenderBalance();
247
+ this.log.debug(`Simulating validateHeader with balance: ${balance}`);
248
+ await this.l1TxUtils.simulate({
249
+ to: this.rollupContract.address,
250
+ data: encodeFunctionData({
251
+ abi: RollupAbi,
252
+ functionName: 'validateHeaderWithAttestations',
253
+ args
254
+ }),
255
+ from: MULTI_CALL_3_ADDRESS
256
+ }, {
257
+ time: ts + 1n
258
+ }, [
259
+ {
260
+ address: MULTI_CALL_3_ADDRESS,
261
+ balance
262
+ },
263
+ ...await this.rollupContract.makePendingBlockNumberOverride(opts?.forcePendingBlockNumber)
264
+ ]);
265
+ this.log.debug(`Simulated validateHeader`);
266
+ }
267
+ /**
268
+ * Simulate making a call to invalidate a block with invalid attestations. Returns undefined if no need to invalidate.
269
+ * @param block - The block to invalidate and the criteria for invalidation (as returned by the archiver)
270
+ */ async simulateInvalidateBlock(validationResult) {
271
+ if (validationResult.valid) {
272
+ return undefined;
273
+ }
274
+ const { reason, block } = validationResult;
275
+ const blockNumber = block.blockNumber;
276
+ const logData = {
277
+ ...block,
278
+ reason
279
+ };
280
+ const currentBlockNumber = await this.rollupContract.getBlockNumber();
281
+ if (currentBlockNumber < validationResult.block.blockNumber) {
282
+ this.log.verbose(`Skipping block ${blockNumber} invalidation since it has already been removed from the pending chain`, {
283
+ currentBlockNumber,
284
+ ...logData
285
+ });
286
+ return undefined;
287
+ }
288
+ const request = this.buildInvalidateBlockRequest(validationResult);
289
+ this.log.debug(`Simulating invalidate block ${blockNumber}`, {
290
+ ...logData,
291
+ request
292
+ });
293
+ try {
294
+ const { gasUsed } = await this.l1TxUtils.simulate(request, undefined, undefined, ErrorsAbi);
295
+ this.log.verbose(`Simulation for invalidate block ${blockNumber} succeeded`, {
296
+ ...logData,
297
+ request,
298
+ gasUsed
299
+ });
300
+ return {
301
+ request,
302
+ gasUsed,
303
+ blockNumber,
304
+ forcePendingBlockNumber: blockNumber - 1,
305
+ reason
306
+ };
307
+ } catch (err) {
308
+ const viemError = formatViemError(err);
309
+ // If the error is due to the block not being in the pending chain, and it was indeed removed by someone else,
310
+ // we can safely ignore it and return undefined so we go ahead with block building.
311
+ if (viemError.message?.includes('Rollup__BlockNotInPendingChain')) {
312
+ this.log.verbose(`Simulation for invalidate block ${blockNumber} failed due to block not being in pending chain`, {
313
+ ...logData,
314
+ request,
315
+ error: viemError.message
316
+ });
317
+ const latestPendingBlockNumber = await this.rollupContract.getBlockNumber();
318
+ if (latestPendingBlockNumber < blockNumber) {
319
+ this.log.verbose(`Block number ${blockNumber} has already been invalidated`, {
320
+ ...logData
321
+ });
322
+ return undefined;
323
+ } else {
324
+ this.log.error(`Simulation for invalidate ${blockNumber} failed and it is still in pending chain`, viemError, logData);
325
+ throw new Error(`Failed to simulate invalidate block ${blockNumber} while it is still in pending chain`, {
326
+ cause: viemError
327
+ });
328
+ }
329
+ }
330
+ // Otherwise, throw. We cannot build the next block if we cannot invalidate the previous one.
331
+ this.log.error(`Simulation for invalidate block ${blockNumber} failed`, viemError, logData);
332
+ throw new Error(`Failed to simulate invalidate block ${blockNumber}`, {
333
+ cause: viemError
334
+ });
335
+ }
336
+ }
337
+ buildInvalidateBlockRequest(validationResult) {
338
+ if (validationResult.valid) {
339
+ throw new Error('Cannot invalidate a valid block');
340
+ }
341
+ const { block, committee, reason, attestations } = validationResult;
342
+ const logData = {
343
+ ...block,
344
+ reason
345
+ };
346
+ this.log.debug(`Simulating invalidate block ${block.blockNumber}`, logData);
347
+ const attestationsAndSigners = new CommitteeAttestationsAndSigners(attestations).getPackedAttestations();
348
+ if (reason === 'invalid-attestation') {
349
+ return this.rollupContract.buildInvalidateBadAttestationRequest(block.blockNumber, attestationsAndSigners, committee, validationResult.invalidIndex);
350
+ } else if (reason === 'insufficient-attestations') {
351
+ return this.rollupContract.buildInvalidateInsufficientAttestationsRequest(block.blockNumber, attestationsAndSigners, committee);
352
+ } else {
353
+ const _ = reason;
354
+ throw new Error(`Unknown reason for invalidation`);
355
+ }
356
+ }
357
+ /**
358
+ * @notice Will simulate `propose` to make sure that the block is valid for submission
170
359
  *
171
360
  * @dev Throws if unable to propose
172
361
  *
173
- * @param header - The header to propose
174
- * @param digest - The digest that attestations are signing over
362
+ * @param block - The block to propose
363
+ * @param attestationData - The block's attestation data
175
364
  *
176
- */ async validateBlockForSubmission(header, attestationData = {
177
- digest: Buffer.alloc(32),
178
- signatures: []
179
- }) {
365
+ */ async validateBlockForSubmission(block, attestationsAndSigners, attestationsAndSignersSignature, options) {
180
366
  const ts = BigInt((await this.l1TxUtils.getBlock()).timestamp + this.ethereumSlotDuration);
181
- const formattedSignatures = attestationData.signatures.map((attest)=>attest.toViemSignature());
182
- const flags = {
183
- ignoreDA: true,
184
- ignoreSignatures: formattedSignatures.length == 0
185
- };
367
+ // If we have no attestations, we still need to provide the empty attestations
368
+ // so that the committee is recalculated correctly
369
+ const ignoreSignatures = attestationsAndSigners.attestations.length === 0;
370
+ if (ignoreSignatures) {
371
+ const { committee } = await this.epochCache.getCommittee(block.header.globalVariables.slotNumber.toBigInt());
372
+ if (!committee) {
373
+ this.log.warn(`No committee found for slot ${block.header.globalVariables.slotNumber.toBigInt()}`);
374
+ throw new Error(`No committee found for slot ${block.header.globalVariables.slotNumber.toBigInt()}`);
375
+ }
376
+ attestationsAndSigners.attestations = committee.map((committeeMember)=>CommitteeAttestation.fromAddress(committeeMember));
377
+ }
378
+ const blobs = await Blob.getBlobsPerBlock(block.body.toBlobFields());
379
+ const blobInput = Blob.getPrefixedEthBlobCommitments(blobs);
186
380
  const args = [
187
- `0x${header.toBuffer().toString('hex')}`,
188
- formattedSignatures,
189
- `0x${attestationData.digest.toString('hex')}`,
190
- ts,
191
- `0x${header.contentCommitment.blobsHash.toString('hex')}`,
192
- flags
381
+ {
382
+ header: block.header.toPropose().toViem(),
383
+ archive: toHex(block.archive.root.toBuffer()),
384
+ stateReference: block.header.state.toViem(),
385
+ oracleInput: {
386
+ feeAssetPriceModifier: 0n
387
+ }
388
+ },
389
+ attestationsAndSigners.getPackedAttestations(),
390
+ attestationsAndSigners.getSigners().map((signer)=>signer.toString()),
391
+ attestationsAndSignersSignature.toViemSignature(),
392
+ blobInput
193
393
  ];
194
- await this.rollupContract.validateHeader(args, this.getForwarderAddress().toString());
394
+ await this.simulateProposeTx(args, ts, options);
195
395
  return ts;
196
396
  }
197
- async getCurrentEpochCommittee() {
198
- const committee = await this.rollupContract.getCurrentEpochCommittee();
199
- return committee.map(EthAddress.fromString);
200
- }
201
- async enqueueCastVoteHelper(slotNumber, timestamp, voteType, payload, base) {
202
- if (this.myLastVotes[voteType] >= slotNumber) {
397
+ async enqueueCastSignalHelper(slotNumber, timestamp, signalType, payload, base, signerAddress, signer) {
398
+ if (this.lastActions[signalType] && this.lastActions[signalType] === slotNumber) {
399
+ this.log.debug(`Skipping duplicate vote cast signal ${signalType} for slot ${slotNumber}`);
203
400
  return false;
204
401
  }
205
402
  if (payload.equals(EthAddress.ZERO)) {
206
403
  return false;
207
404
  }
208
- const round = await base.computeRound(slotNumber);
209
- const [proposer, roundInfo] = await Promise.all([
210
- this.rollupContract.getProposerAt(timestamp),
211
- base.getRoundInfo(this.rollupContract.address, round)
212
- ]);
213
- if (proposer.toLowerCase() !== this.getForwarderAddress().toString().toLowerCase()) {
405
+ if (signerAddress.equals(EthAddress.ZERO)) {
406
+ this.log.warn(`Cannot enqueue vote cast signal ${signalType} for address zero at slot ${slotNumber}`);
214
407
  return false;
215
408
  }
216
- if (roundInfo.lastVote >= slotNumber) {
409
+ const round = await base.computeRound(slotNumber);
410
+ const roundInfo = await base.getRoundInfo(this.rollupContract.address, round);
411
+ if (roundInfo.lastSignalSlot >= slotNumber) {
217
412
  return false;
218
413
  }
219
- const cachedLastVote = this.myLastVotes[voteType];
220
- this.myLastVotes[voteType] = slotNumber;
414
+ const cachedLastVote = this.lastActions[signalType];
415
+ this.lastActions[signalType] = slotNumber;
416
+ const action = signalType;
417
+ const request = await base.createSignalRequestWithSignature(payload.toString(), slotNumber, this.config.l1ChainId, signerAddress.toString(), signer);
418
+ this.log.debug(`Created ${action} request with signature`, {
419
+ request,
420
+ round,
421
+ signer: this.l1TxUtils.client.account?.address,
422
+ lastValidL2Slot: slotNumber
423
+ });
424
+ try {
425
+ await this.l1TxUtils.simulate(request, {
426
+ time: timestamp
427
+ }, [], ErrorsAbi);
428
+ this.log.debug(`Simulation for ${action} at slot ${slotNumber} succeeded`, {
429
+ request
430
+ });
431
+ } catch (err) {
432
+ this.log.error(`Failed simulation for ${action} at slot ${slotNumber} (enqueuing the action anyway)`, err);
433
+ // Yes, we enqueue the request anyway, in case there was a bug with the simulation itself
434
+ }
435
+ // TODO(palla/slash): All votes (governance and slashing) should txTimeoutAt at the end of the slot.
221
436
  this.addRequest({
222
- action: voteType === 0 ? 'governance-vote' : 'slashing-vote',
223
- request: base.createVoteRequest(payload.toString()),
437
+ gasConfig: {
438
+ gasLimit: SequencerPublisher.VOTE_GAS_GUESS
439
+ },
440
+ action,
441
+ request,
224
442
  lastValidL2Slot: slotNumber,
225
- onResult: (_request, result)=>{
226
- if (!result || result.receipt.status !== 'success') {
227
- this.myLastVotes[voteType] = cachedLastVote;
443
+ checkSuccess: (_request, result)=>{
444
+ const success = result && result.receipt && result.receipt.status === 'success' && tryExtractEvent(result.receipt.logs, base.address.toString(), EmpireBaseAbi, 'SignalCast');
445
+ const logData = {
446
+ ...result,
447
+ slotNumber,
448
+ round,
449
+ payload: payload.toString()
450
+ };
451
+ if (!success) {
452
+ this.log.error(`Signaling in [${action}] for ${payload} at slot ${slotNumber} in round ${round} failed`, logData);
453
+ this.lastActions[signalType] = cachedLastVote;
454
+ return false;
228
455
  } else {
229
- this.log.info(`Cast [${voteType}] vote for slot ${slotNumber}`);
456
+ this.log.info(`Signaling in [${action}] for ${payload} at slot ${slotNumber} in round ${round} succeeded`, logData);
457
+ return true;
230
458
  }
231
459
  }
232
460
  });
233
461
  return true;
234
462
  }
235
- async getVoteConfig(slotNumber, voteType) {
236
- if (voteType === 0) {
237
- return {
238
- payload: this.governancePayload,
239
- base: this.govProposerContract
240
- };
241
- } else if (voteType === 1) {
242
- if (!this.getSlashPayload) {
243
- return undefined;
244
- }
245
- const slashPayload = await this.getSlashPayload(slotNumber);
246
- if (!slashPayload) {
247
- return undefined;
248
- }
249
- return {
250
- payload: slashPayload,
251
- base: this.slashingProposerContract
252
- };
253
- }
254
- throw new Error('Unreachable: Invalid vote type');
255
- }
256
463
  /**
257
- * Enqueues a castVote transaction to cast a vote for a given slot number.
258
- * @param slotNumber - The slot number to cast a vote for.
259
- * @param timestamp - The timestamp of the slot to cast a vote for.
260
- * @param voteType - The type of vote to cast.
261
- * @returns True if the vote was successfully enqueued, false otherwise.
262
- */ async enqueueCastVote(slotNumber, timestamp, voteType) {
263
- const voteConfig = await this.getVoteConfig(slotNumber, voteType);
264
- if (!voteConfig) {
464
+ * Enqueues a governance castSignal transaction to cast a signal for a given slot number.
465
+ * @param slotNumber - The slot number to cast a signal for.
466
+ * @param timestamp - The timestamp of the slot to cast a signal for.
467
+ * @returns True if the signal was successfully enqueued, false otherwise.
468
+ */ enqueueGovernanceCastSignal(governancePayload, slotNumber, timestamp, signerAddress, signer) {
469
+ return this.enqueueCastSignalHelper(slotNumber, timestamp, 'governance-signal', governancePayload, this.govProposerContract, signerAddress, signer);
470
+ }
471
+ /** Enqueues all slashing actions as returned by the slasher client. */ async enqueueSlashingActions(actions, slotNumber, timestamp, signerAddress, signer) {
472
+ if (actions.length === 0) {
473
+ this.log.debug(`No slashing actions to enqueue for slot ${slotNumber}`);
265
474
  return false;
266
475
  }
267
- const { payload, base } = voteConfig;
268
- return this.enqueueCastVoteHelper(slotNumber, timestamp, voteType, payload, base);
476
+ for (const action of actions){
477
+ switch(action.type){
478
+ case 'vote-empire-payload':
479
+ {
480
+ if (this.slashingProposerContract?.type !== 'empire') {
481
+ this.log.error('Cannot vote for empire payload on non-empire slashing contract');
482
+ break;
483
+ }
484
+ this.log.debug(`Enqueuing slashing vote for payload ${action.payload} at slot ${slotNumber}`, {
485
+ signerAddress
486
+ });
487
+ await this.enqueueCastSignalHelper(slotNumber, timestamp, 'empire-slashing-signal', action.payload, this.slashingProposerContract, signerAddress, signer);
488
+ break;
489
+ }
490
+ case 'create-empire-payload':
491
+ {
492
+ this.log.debug(`Enqueuing slashing create payload at slot ${slotNumber}`, {
493
+ slotNumber,
494
+ signerAddress
495
+ });
496
+ const request = this.slashFactoryContract.buildCreatePayloadRequest(action.data);
497
+ await this.simulateAndEnqueueRequest('create-empire-payload', request, (receipt)=>!!this.slashFactoryContract.tryExtractSlashPayloadCreatedEvent(receipt.logs), slotNumber, timestamp);
498
+ break;
499
+ }
500
+ case 'execute-empire-payload':
501
+ {
502
+ this.log.debug(`Enqueuing slashing execute payload at slot ${slotNumber}`, {
503
+ slotNumber,
504
+ signerAddress
505
+ });
506
+ if (this.slashingProposerContract?.type !== 'empire') {
507
+ this.log.error('Cannot execute slashing payload on non-empire slashing contract');
508
+ return false;
509
+ }
510
+ const empireSlashingProposer = this.slashingProposerContract;
511
+ const request = empireSlashingProposer.buildExecuteRoundRequest(action.round);
512
+ await this.simulateAndEnqueueRequest('execute-empire-payload', request, (receipt)=>!!empireSlashingProposer.tryExtractPayloadSubmittedEvent(receipt.logs), slotNumber, timestamp);
513
+ break;
514
+ }
515
+ case 'vote-offenses':
516
+ {
517
+ this.log.debug(`Enqueuing slashing vote for ${action.votes.length} votes at slot ${slotNumber}`, {
518
+ slotNumber,
519
+ round: action.round,
520
+ votesCount: action.votes.length,
521
+ signerAddress
522
+ });
523
+ if (this.slashingProposerContract?.type !== 'tally') {
524
+ this.log.error('Cannot vote for slashing offenses on non-tally slashing contract');
525
+ return false;
526
+ }
527
+ const tallySlashingProposer = this.slashingProposerContract;
528
+ const votes = bufferToHex(encodeSlashConsensusVotes(action.votes));
529
+ const request = await tallySlashingProposer.buildVoteRequestFromSigner(votes, slotNumber, signer);
530
+ await this.simulateAndEnqueueRequest('vote-offenses', request, (receipt)=>!!tallySlashingProposer.tryExtractVoteCastEvent(receipt.logs), slotNumber, timestamp);
531
+ break;
532
+ }
533
+ case 'execute-slash':
534
+ {
535
+ this.log.debug(`Enqueuing slash execution for round ${action.round} at slot ${slotNumber}`, {
536
+ slotNumber,
537
+ round: action.round,
538
+ signerAddress
539
+ });
540
+ if (this.slashingProposerContract?.type !== 'tally') {
541
+ this.log.error('Cannot execute slashing offenses on non-tally slashing contract');
542
+ return false;
543
+ }
544
+ const tallySlashingProposer = this.slashingProposerContract;
545
+ const request = tallySlashingProposer.buildExecuteRoundRequest(action.round, action.committees);
546
+ await this.simulateAndEnqueueRequest('execute-slash', request, (receipt)=>!!tallySlashingProposer.tryExtractRoundExecutedEvent(receipt.logs), slotNumber, timestamp);
547
+ break;
548
+ }
549
+ default:
550
+ {
551
+ const _ = action;
552
+ throw new Error(`Unknown slashing action type: ${action.type}`);
553
+ }
554
+ }
555
+ }
556
+ return true;
269
557
  }
270
558
  /**
271
559
  * Proposes a L2 block on L1.
272
560
  *
273
561
  * @param block - L2 block to propose.
274
562
  * @returns True if the tx has been enqueued, throws otherwise. See #9315
275
- */ async enqueueProposeL2Block(block, attestations, txHashes, opts = {}) {
276
- const consensusPayload = new ConsensusPayload(block.header, block.archive.root, txHashes ?? []);
277
- const digest = await getHashedSignaturePayload(consensusPayload, SignatureDomainSeparator.blockAttestation);
278
- const blobs = await Blob.getBlobs(block.body.toBlobFields());
563
+ */ async enqueueProposeL2Block(block, attestationsAndSigners, attestationsAndSignersSignature, opts = {}) {
564
+ const proposedBlockHeader = block.header.toPropose();
565
+ const blobs = await Blob.getBlobsPerBlock(block.body.toBlobFields());
279
566
  const proposeTxArgs = {
280
- header: block.header.toBuffer(),
567
+ header: proposedBlockHeader,
281
568
  archive: block.archive.root.toBuffer(),
282
- blockHash: (await block.header.hash()).toBuffer(),
569
+ stateReference: block.header.state,
283
570
  body: block.body.toBuffer(),
284
571
  blobs,
285
- attestations,
286
- txHashes: txHashes ?? []
572
+ attestationsAndSigners,
573
+ attestationsAndSignersSignature
287
574
  };
288
- // @note This will make sure that we are passing the checks for our header ASSUMING that the data is also made available
289
- // This means that we can avoid the simulation issues in later checks.
290
- // By simulation issue, I mean the fact that the block.timestamp is equal to the last block, not the next, which
291
- // make time consistency checks break.
292
- const ts = await this.validateBlockForSubmission(block.header, {
293
- digest: digest.toBuffer(),
294
- signatures: attestations ?? []
575
+ let ts;
576
+ try {
577
+ // @note This will make sure that we are passing the checks for our header ASSUMING that the data is also made available
578
+ // This means that we can avoid the simulation issues in later checks.
579
+ // By simulation issue, I mean the fact that the block.timestamp is equal to the last block, not the next, which
580
+ // make time consistency checks break.
581
+ // TODO(palla): Check whether we're validating twice, once here and once within addProposeTx, since we call simulateProposeTx in both places.
582
+ ts = await this.validateBlockForSubmission(block, attestationsAndSigners, attestationsAndSignersSignature, opts);
583
+ } catch (err) {
584
+ this.log.error(`Block validation failed. ${err instanceof Error ? err.message : 'No error message'}`, err, {
585
+ ...block.getStats(),
586
+ slotNumber: block.header.globalVariables.slotNumber.toBigInt(),
587
+ forcePendingBlockNumber: opts.forcePendingBlockNumber
588
+ });
589
+ throw err;
590
+ }
591
+ this.log.verbose(`Enqueuing block propose transaction`, {
592
+ ...block.toBlockInfo(),
593
+ ...opts
295
594
  });
296
- this.log.debug(`Submitting propose transaction`);
297
595
  await this.addProposeTx(block, proposeTxArgs, opts, ts);
298
596
  return true;
299
597
  }
598
+ enqueueInvalidateBlock(request, opts = {}) {
599
+ if (!request) {
600
+ return;
601
+ }
602
+ // We issued the simulation against the rollup contract, so we need to account for the overhead of the multicall3
603
+ const gasLimit = this.l1TxUtils.bumpGasLimit(BigInt(Math.ceil(Number(request.gasUsed) * 64 / 63)));
604
+ const { gasUsed, blockNumber } = request;
605
+ const logData = {
606
+ gasUsed,
607
+ blockNumber,
608
+ gasLimit,
609
+ opts
610
+ };
611
+ this.log.verbose(`Enqueuing invalidate block request`, logData);
612
+ this.addRequest({
613
+ action: `invalidate-by-${request.reason}`,
614
+ request: request.request,
615
+ gasConfig: {
616
+ gasLimit,
617
+ txTimeoutAt: opts.txTimeoutAt
618
+ },
619
+ lastValidL2Slot: this.getCurrentL2Slot() + 2n,
620
+ checkSuccess: (_req, result)=>{
621
+ const success = result && result.receipt && result.receipt.status === 'success' && tryExtractEvent(result.receipt.logs, this.rollupContract.address, RollupAbi, 'BlockInvalidated');
622
+ if (!success) {
623
+ this.log.warn(`Invalidate block ${request.blockNumber} failed`, {
624
+ ...result,
625
+ ...logData
626
+ });
627
+ } else {
628
+ this.log.info(`Invalidate block ${request.blockNumber} succeeded`, {
629
+ ...result,
630
+ ...logData
631
+ });
632
+ }
633
+ return !!success;
634
+ }
635
+ });
636
+ }
637
+ async simulateAndEnqueueRequest(action, request, checkSuccess, slotNumber, timestamp) {
638
+ const logData = {
639
+ slotNumber,
640
+ timestamp,
641
+ gasLimit: undefined
642
+ };
643
+ if (this.lastActions[action] && this.lastActions[action] === slotNumber) {
644
+ this.log.debug(`Skipping duplicate action ${action} for slot ${slotNumber}`);
645
+ return false;
646
+ }
647
+ const cachedLastActionSlot = this.lastActions[action];
648
+ this.lastActions[action] = slotNumber;
649
+ this.log.debug(`Simulating ${action} for slot ${slotNumber}`, logData);
650
+ let gasUsed;
651
+ try {
652
+ ({ gasUsed } = await this.l1TxUtils.simulate(request, {
653
+ time: timestamp
654
+ }, [], ErrorsAbi)); // TODO(palla/slash): Check the timestamp logic
655
+ this.log.verbose(`Simulation for ${action} succeeded`, {
656
+ ...logData,
657
+ request,
658
+ gasUsed
659
+ });
660
+ } catch (err) {
661
+ const viemError = formatViemError(err);
662
+ this.log.error(`Simulation for ${action} at ${slotNumber} failed`, viemError, logData);
663
+ return false;
664
+ }
665
+ // We issued the simulation against the rollup contract, so we need to account for the overhead of the multicall3
666
+ const gasLimit = this.l1TxUtils.bumpGasLimit(BigInt(Math.ceil(Number(gasUsed) * 64 / 63)));
667
+ logData.gasLimit = gasLimit;
668
+ this.log.debug(`Enqueuing ${action}`, logData);
669
+ this.addRequest({
670
+ action,
671
+ request,
672
+ gasConfig: {
673
+ gasLimit
674
+ },
675
+ lastValidL2Slot: slotNumber,
676
+ checkSuccess: (_req, result)=>{
677
+ const success = result && result.receipt && result.receipt.status === 'success' && checkSuccess(result.receipt);
678
+ if (!success) {
679
+ this.log.warn(`Action ${action} at ${slotNumber} failed`, {
680
+ ...result,
681
+ ...logData
682
+ });
683
+ this.lastActions[action] = cachedLastActionSlot;
684
+ } else {
685
+ this.log.info(`Action ${action} at ${slotNumber} succeeded`, {
686
+ ...result,
687
+ ...logData
688
+ });
689
+ }
690
+ return !!success;
691
+ }
692
+ });
693
+ return true;
694
+ }
300
695
  /**
301
696
  * Calling `interrupt` will cause any in progress call to `publishRollup` to return `false` asap.
302
697
  * Be warned, the call may return false even if the tx subsequently gets successfully mined.
@@ -310,13 +705,13 @@ export class SequencerPublisher {
310
705
  this.interrupted = false;
311
706
  this.l1TxUtils.restart();
312
707
  }
313
- async prepareProposeTx(encodedData, timestamp) {
708
+ async prepareProposeTx(encodedData, timestamp, options) {
314
709
  const kzg = Blob.getViemKzgInstance();
315
- const blobInput = Blob.getEthBlobEvaluationInputs(encodedData.blobs);
710
+ const blobInput = Blob.getPrefixedEthBlobCommitments(encodedData.blobs);
316
711
  this.log.debug('Validating blob input', {
317
712
  blobInput
318
713
  });
319
- const blobEvaluationGas = await this.l1TxUtils.estimateGas(this.l1TxUtils.walletClient.account, {
714
+ const blobEvaluationGas = await this.l1TxUtils.estimateGas(this.getSenderAddress().toString(), {
320
715
  to: this.rollupContract.address,
321
716
  data: encodeFunctionData({
322
717
  abi: RollupAbi,
@@ -335,47 +730,51 @@ export class SequencerPublisher {
335
730
  });
336
731
  throw new Error('Failed to validate blobs');
337
732
  });
338
- const attestations = encodedData.attestations ? encodedData.attestations.map((attest)=>attest.toViemSignature()) : [];
339
- const txHashes = encodedData.txHashes ? encodedData.txHashes.map((txHash)=>txHash.toString()) : [];
733
+ const signers = encodedData.attestationsAndSigners.getSigners().map((signer)=>signer.toString());
340
734
  const args = [
341
735
  {
342
- header: `0x${encodedData.header.toString('hex')}`,
343
- archive: `0x${encodedData.archive.toString('hex')}`,
736
+ header: encodedData.header.toViem(),
737
+ archive: toHex(encodedData.archive),
738
+ stateReference: encodedData.stateReference.toViem(),
344
739
  oracleInput: {
345
740
  // We are currently not modifying these. See #9963
346
741
  feeAssetPriceModifier: 0n
347
- },
348
- blockHash: `0x${encodedData.blockHash.toString('hex')}`,
349
- txHashes
742
+ }
350
743
  },
351
- attestations,
744
+ encodedData.attestationsAndSigners.getPackedAttestations(),
745
+ signers,
746
+ encodedData.attestationsAndSignersSignature.toViemSignature(),
352
747
  blobInput
353
748
  ];
749
+ const { rollupData, simulationResult } = await this.simulateProposeTx(args, timestamp, options);
750
+ return {
751
+ args,
752
+ blobEvaluationGas,
753
+ rollupData,
754
+ simulationResult
755
+ };
756
+ }
757
+ /**
758
+ * Simulates the propose tx with eth_simulateV1
759
+ * @param args - The propose tx args
760
+ * @param timestamp - The timestamp to simulate proposal at
761
+ * @returns The simulation result
762
+ */ async simulateProposeTx(args, timestamp, options) {
354
763
  const rollupData = encodeFunctionData({
355
764
  abi: RollupAbi,
356
765
  functionName: 'propose',
357
766
  args
358
767
  });
359
- const forwarderData = encodeFunctionData({
360
- abi: ForwarderAbi,
361
- functionName: 'forward',
362
- args: [
363
- [
364
- this.rollupContract.address
365
- ],
366
- [
367
- rollupData
368
- ]
369
- ]
370
- });
371
- const simulationResult = await this.l1TxUtils.simulateGasUsed({
372
- to: this.getForwarderAddress().toString(),
373
- data: forwarderData,
768
+ // override the pending block number if requested
769
+ const forcePendingBlockNumberStateDiff = (options.forcePendingBlockNumber !== undefined ? await this.rollupContract.makePendingBlockNumberOverride(options.forcePendingBlockNumber) : []).flatMap((override)=>override.stateDiff ?? []);
770
+ const simulationResult = await this.l1TxUtils.simulate({
771
+ to: this.rollupContract.address,
772
+ data: rollupData,
374
773
  gas: SequencerPublisher.PROPOSE_GAS_GUESS
375
774
  }, {
376
775
  // @note we add 1n to the timestamp because geth implementation doesn't like simulation timestamp to be equal to the current block timestamp
377
776
  time: timestamp + 1n,
378
- // @note reth should have a 30m gas limit per block but throws errors that this tx is beyond limit
777
+ // @note reth should have a 30m gas limit per block but throws errors that this tx is beyond limit so we increase here
379
778
  gasLimit: SequencerPublisher.PROPOSE_GAS_GUESS * 2n
380
779
  }, [
381
780
  {
@@ -383,24 +782,20 @@ export class SequencerPublisher {
383
782
  // @note we override checkBlob to false since blobs are not part simulate()
384
783
  stateDiff: [
385
784
  {
386
- slot: toHex(RollupContract.checkBlobStorageSlot, true),
387
- value: toHex(0n, true)
388
- }
785
+ slot: toPaddedHex(RollupContract.checkBlobStorageSlot, true),
786
+ value: toPaddedHex(0n, true)
787
+ },
788
+ ...forcePendingBlockNumberStateDiff
389
789
  ]
390
790
  }
391
- ], {
791
+ ], RollupAbi, {
392
792
  // @note fallback gas estimate to use if the node doesn't support simulation API
393
793
  fallbackGasEstimate: SequencerPublisher.PROPOSE_GAS_GUESS
394
794
  }).catch((err)=>{
395
- const { message, metaMessages } = formatViemError(err);
396
- this.log.error(`Failed to simulate gas used`, message, {
397
- metaMessages
398
- });
399
- throw new Error('Failed to simulate gas used');
795
+ this.log.error(`Failed to simulate propose tx`, err);
796
+ throw err;
400
797
  });
401
798
  return {
402
- args,
403
- blobEvaluationGas,
404
799
  rollupData,
405
800
  simulationResult
406
801
  };
@@ -408,9 +803,14 @@ export class SequencerPublisher {
408
803
  async addProposeTx(block, encodedData, opts = {}, timestamp) {
409
804
  const timer = new Timer();
410
805
  const kzg = Blob.getViemKzgInstance();
411
- const { rollupData, simulationResult, blobEvaluationGas } = await this.prepareProposeTx(encodedData, timestamp);
806
+ const { rollupData, simulationResult, blobEvaluationGas } = await this.prepareProposeTx(encodedData, timestamp, opts);
412
807
  const startBlock = await this.l1TxUtils.getBlockNumber();
413
- const blockHash = await block.hash();
808
+ const gasLimit = this.l1TxUtils.bumpGasLimit(BigInt(Math.ceil(Number(simulationResult.gasUsed) * 64 / 63)) + blobEvaluationGas + SequencerPublisher.MULTICALL_OVERHEAD_GAS_GUESS);
809
+ // Send the blobs to the blob sink preemptively. This helps in tests where the sequencer mistakingly thinks that the propose
810
+ // tx fails but it does get mined. We make sure that the blobs are sent to the blob sink regardless of the tx outcome.
811
+ void this.blobSinkClient.sendBlobsToBlobSink(encodedData.blobs).catch((_err)=>{
812
+ this.log.error('Failed to send blobs to blob sink');
813
+ });
414
814
  return this.addRequest({
415
815
  action: 'propose',
416
816
  request: {
@@ -420,62 +820,54 @@ export class SequencerPublisher {
420
820
  lastValidL2Slot: block.header.globalVariables.slotNumber.toBigInt(),
421
821
  gasConfig: {
422
822
  ...opts,
423
- gasLimit: this.l1TxUtils.bumpGasLimit(simulationResult + blobEvaluationGas)
823
+ gasLimit
424
824
  },
425
825
  blobConfig: {
426
826
  blobs: encodedData.blobs.map((b)=>b.data),
427
827
  kzg
428
828
  },
429
- onResult: (request, result)=>{
829
+ checkSuccess: (_request, result)=>{
430
830
  if (!result) {
431
- return;
831
+ return false;
432
832
  }
433
833
  const { receipt, stats, errorMsg } = result;
434
- if (receipt.status === 'success') {
834
+ const success = receipt && receipt.status === 'success' && tryExtractEvent(receipt.logs, this.rollupContract.address, RollupAbi, 'L2BlockProposed');
835
+ if (success) {
435
836
  const endBlock = receipt.blockNumber;
436
837
  const inclusionBlocks = Number(endBlock - startBlock);
838
+ const { calldataGas, calldataSize, sender } = stats;
437
839
  const publishStats = {
438
840
  gasPrice: receipt.effectiveGasPrice,
439
841
  gasUsed: receipt.gasUsed,
440
842
  blobGasUsed: receipt.blobGasUsed ?? 0n,
441
843
  blobDataGas: receipt.blobGasPrice ?? 0n,
442
844
  transactionHash: receipt.transactionHash,
443
- ...pick(stats, 'calldataGas', 'calldataSize', 'sender'),
845
+ calldataGas,
846
+ calldataSize,
847
+ sender,
444
848
  ...block.getStats(),
445
849
  eventName: 'rollup-published-to-l1',
446
850
  blobCount: encodedData.blobs.length,
447
851
  inclusionBlocks
448
852
  };
449
- this.log.verbose(`Published L2 block to L1 rollup contract`, {
853
+ this.log.info(`Published L2 block to L1 rollup contract`, {
450
854
  ...stats,
451
- ...block.getStats()
855
+ ...block.getStats(),
856
+ ...receipt
452
857
  });
453
858
  this.metrics.recordProcessBlockTx(timer.ms(), publishStats);
454
- // Send the blobs to the blob sink
455
- this.sendBlobsToBlobSink(receipt.blockHash, encodedData.blobs).catch((_err)=>{
456
- this.log.error('Failed to send blobs to blob sink');
457
- });
458
859
  return true;
459
860
  } else {
460
861
  this.metrics.recordFailedTx('process');
461
- this.log.error(`Rollup process tx reverted. ${errorMsg ?? 'No error message'}`, undefined, {
862
+ this.log.error(`Rollup process tx failed: ${errorMsg ?? 'no error message'}`, undefined, {
462
863
  ...block.getStats(),
864
+ receipt,
463
865
  txHash: receipt.transactionHash,
464
- blockHash,
465
866
  slotNumber: block.header.globalVariables.slotNumber.toBigInt()
466
867
  });
868
+ return false;
467
869
  }
468
870
  }
469
871
  });
470
872
  }
471
- /**
472
- * Send blobs to the blob sink
473
- *
474
- * If a blob sink url is configured, then we send blobs to the blob sink
475
- * - for now we use the blockHash as the identifier for the blobs;
476
- * In the future this will move to be the beacon block id - which takes a bit more work
477
- * to calculate and will need to be mocked in e2e tests
478
- */ sendBlobsToBlobSink(blockHash, blobs) {
479
- return this.blobSinkClient.sendBlobsToBlobSink(blockHash, blobs);
480
- }
481
873
  }