@aztec/sequencer-client 0.0.0-test.0 → 0.0.1-commit.03f7ef2

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