@aztec/sequencer-client 0.0.1-commit.6d3c34e → 0.0.1-commit.7035c9bd6
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.
- package/dest/client/sequencer-client.d.ts +12 -7
- package/dest/client/sequencer-client.d.ts.map +1 -1
- package/dest/client/sequencer-client.js +56 -17
- package/dest/config.d.ts +26 -7
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +47 -30
- package/dest/global_variable_builder/global_builder.d.ts +2 -4
- package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
- package/dest/global_variable_builder/global_builder.js +7 -6
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/publisher/config.d.ts +35 -17
- package/dest/publisher/config.d.ts.map +1 -1
- package/dest/publisher/config.js +106 -42
- package/dest/publisher/index.d.ts +2 -1
- package/dest/publisher/index.d.ts.map +1 -1
- package/dest/publisher/l1_tx_failed_store/factory.d.ts +11 -0
- package/dest/publisher/l1_tx_failed_store/factory.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/factory.js +22 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts +59 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/failed_tx_store.js +1 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts +15 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/file_store_failed_tx_store.js +34 -0
- package/dest/publisher/l1_tx_failed_store/index.d.ts +4 -0
- package/dest/publisher/l1_tx_failed_store/index.d.ts.map +1 -0
- package/dest/publisher/l1_tx_failed_store/index.js +2 -0
- package/dest/publisher/sequencer-publisher-factory.d.ts +11 -3
- package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-factory.js +27 -2
- package/dest/publisher/sequencer-publisher-metrics.d.ts +1 -1
- package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-metrics.js +12 -4
- package/dest/publisher/sequencer-publisher.d.ts +30 -10
- package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher.js +362 -56
- package/dest/sequencer/checkpoint_proposal_job.d.ts +42 -11
- package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -1
- package/dest/sequencer/checkpoint_proposal_job.js +322 -122
- package/dest/sequencer/checkpoint_voter.d.ts +3 -2
- package/dest/sequencer/checkpoint_voter.d.ts.map +1 -1
- package/dest/sequencer/checkpoint_voter.js +34 -10
- package/dest/sequencer/events.d.ts +2 -1
- package/dest/sequencer/events.d.ts.map +1 -1
- package/dest/sequencer/index.d.ts +1 -2
- package/dest/sequencer/index.d.ts.map +1 -1
- package/dest/sequencer/index.js +0 -1
- package/dest/sequencer/metrics.d.ts +21 -5
- package/dest/sequencer/metrics.d.ts.map +1 -1
- package/dest/sequencer/metrics.js +122 -30
- package/dest/sequencer/sequencer.d.ts +43 -20
- package/dest/sequencer/sequencer.d.ts.map +1 -1
- package/dest/sequencer/sequencer.js +151 -82
- package/dest/sequencer/timetable.d.ts +4 -6
- package/dest/sequencer/timetable.d.ts.map +1 -1
- package/dest/sequencer/timetable.js +7 -11
- package/dest/sequencer/types.d.ts +2 -2
- package/dest/sequencer/types.d.ts.map +1 -1
- package/dest/test/index.d.ts +3 -5
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/mock_checkpoint_builder.d.ts +23 -19
- package/dest/test/mock_checkpoint_builder.d.ts.map +1 -1
- package/dest/test/mock_checkpoint_builder.js +67 -38
- package/dest/test/utils.d.ts +8 -8
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +12 -11
- package/package.json +29 -28
- package/src/client/sequencer-client.ts +77 -18
- package/src/config.ts +66 -41
- package/src/global_variable_builder/global_builder.ts +6 -5
- package/src/index.ts +1 -6
- package/src/publisher/config.ts +121 -43
- package/src/publisher/index.ts +3 -0
- package/src/publisher/l1_tx_failed_store/factory.ts +32 -0
- package/src/publisher/l1_tx_failed_store/failed_tx_store.ts +55 -0
- package/src/publisher/l1_tx_failed_store/file_store_failed_tx_store.ts +46 -0
- package/src/publisher/l1_tx_failed_store/index.ts +3 -0
- package/src/publisher/sequencer-publisher-factory.ts +38 -6
- package/src/publisher/sequencer-publisher-metrics.ts +7 -3
- package/src/publisher/sequencer-publisher.ts +360 -69
- package/src/sequencer/checkpoint_proposal_job.ts +449 -142
- package/src/sequencer/checkpoint_voter.ts +32 -7
- package/src/sequencer/events.ts +1 -1
- package/src/sequencer/index.ts +0 -1
- package/src/sequencer/metrics.ts +138 -32
- package/src/sequencer/sequencer.ts +200 -91
- package/src/sequencer/timetable.ts +13 -12
- package/src/sequencer/types.ts +1 -1
- package/src/test/index.ts +2 -4
- package/src/test/mock_checkpoint_builder.ts +122 -78
- package/src/test/utils.ts +24 -14
- package/dest/sequencer/block_builder.d.ts +0 -26
- package/dest/sequencer/block_builder.d.ts.map +0 -1
- package/dest/sequencer/block_builder.js +0 -129
- package/src/sequencer/block_builder.ts +0 -216
|
@@ -1,40 +1,58 @@
|
|
|
1
|
-
import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
|
|
2
1
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
3
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
BlockNumber,
|
|
4
|
+
CheckpointNumber,
|
|
5
|
+
EpochNumber,
|
|
6
|
+
IndexWithinCheckpoint,
|
|
7
|
+
SlotNumber,
|
|
8
|
+
} from '@aztec/foundation/branded-types';
|
|
4
9
|
import { randomInt } from '@aztec/foundation/crypto/random';
|
|
10
|
+
import {
|
|
11
|
+
flipSignature,
|
|
12
|
+
generateRecoverableSignature,
|
|
13
|
+
generateUnrecoverableSignature,
|
|
14
|
+
} from '@aztec/foundation/crypto/secp256k1-signer';
|
|
5
15
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
16
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
7
17
|
import { Signature } from '@aztec/foundation/eth-signature';
|
|
8
18
|
import { filter } from '@aztec/foundation/iterator';
|
|
9
|
-
import type
|
|
19
|
+
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
10
20
|
import { sleep, sleepUntil } from '@aztec/foundation/sleep';
|
|
11
21
|
import { type DateProvider, Timer } from '@aztec/foundation/timer';
|
|
12
|
-
import { type TypedEventEmitter, unfreeze } from '@aztec/foundation/types';
|
|
22
|
+
import { type TypedEventEmitter, isErrorClass, unfreeze } from '@aztec/foundation/types';
|
|
13
23
|
import type { P2P } from '@aztec/p2p';
|
|
14
24
|
import type { SlasherClientInterface } from '@aztec/slasher';
|
|
15
25
|
import {
|
|
16
26
|
CommitteeAttestation,
|
|
17
27
|
CommitteeAttestationsAndSigners,
|
|
18
|
-
|
|
28
|
+
L2Block,
|
|
19
29
|
type L2BlockSink,
|
|
30
|
+
type L2BlockSource,
|
|
20
31
|
MaliciousCommitteeAttestationsAndSigners,
|
|
21
32
|
} from '@aztec/stdlib/block';
|
|
22
|
-
import type
|
|
23
|
-
import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
|
|
33
|
+
import { type Checkpoint, validateCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
34
|
+
import { getSlotStartBuildTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
24
35
|
import { Gas } from '@aztec/stdlib/gas';
|
|
25
|
-
import
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
import {
|
|
37
|
+
type BlockBuilderOptions,
|
|
38
|
+
InsufficientValidTxsError,
|
|
39
|
+
type ResolvedSequencerConfig,
|
|
40
|
+
type WorldStateSynchronizer,
|
|
29
41
|
} from '@aztec/stdlib/interfaces/server';
|
|
30
42
|
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
31
|
-
import type {
|
|
32
|
-
|
|
43
|
+
import type {
|
|
44
|
+
BlockProposal,
|
|
45
|
+
BlockProposalOptions,
|
|
46
|
+
CheckpointProposal,
|
|
47
|
+
CheckpointProposalOptions,
|
|
48
|
+
} from '@aztec/stdlib/p2p';
|
|
49
|
+
import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
|
|
33
50
|
import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
|
|
34
51
|
import { type FailedTx, Tx } from '@aztec/stdlib/tx';
|
|
35
52
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
36
53
|
import { Attributes, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
37
54
|
import { CheckpointBuilder, type FullNodeCheckpointsBuilder, type ValidatorClient } from '@aztec/validator-client';
|
|
55
|
+
import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
|
|
38
56
|
|
|
39
57
|
import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
|
|
40
58
|
import type { InvalidateCheckpointRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
|
|
@@ -56,8 +74,13 @@ const TXS_POLLING_MS = 500;
|
|
|
56
74
|
* the Sequencer once the check for being the proposer for the slot has succeeded.
|
|
57
75
|
*/
|
|
58
76
|
export class CheckpointProposalJob implements Traceable {
|
|
77
|
+
protected readonly log: Logger;
|
|
78
|
+
|
|
59
79
|
constructor(
|
|
60
|
-
private readonly
|
|
80
|
+
private readonly slotNow: SlotNumber,
|
|
81
|
+
private readonly targetSlot: SlotNumber,
|
|
82
|
+
private readonly epochNow: EpochNumber,
|
|
83
|
+
private readonly targetEpoch: EpochNumber,
|
|
61
84
|
private readonly checkpointNumber: CheckpointNumber,
|
|
62
85
|
private readonly syncedToBlockNumber: BlockNumber,
|
|
63
86
|
// TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
|
|
@@ -70,6 +93,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
70
93
|
private readonly p2pClient: P2P,
|
|
71
94
|
private readonly worldState: WorldStateSynchronizer,
|
|
72
95
|
private readonly l1ToL2MessageSource: L1ToL2MessageSource,
|
|
96
|
+
private readonly l2BlockSource: L2BlockSource,
|
|
73
97
|
private readonly checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
74
98
|
private readonly blockSink: L2BlockSink,
|
|
75
99
|
private readonly l1Constants: SequencerRollupConstants,
|
|
@@ -81,9 +105,24 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
81
105
|
private readonly metrics: SequencerMetrics,
|
|
82
106
|
private readonly eventEmitter: TypedEventEmitter<SequencerEvents>,
|
|
83
107
|
private readonly setStateFn: (state: SequencerState, slot?: SlotNumber) => void,
|
|
84
|
-
protected readonly log: Logger,
|
|
85
108
|
public readonly tracer: Tracer,
|
|
86
|
-
|
|
109
|
+
bindings?: LoggerBindings,
|
|
110
|
+
) {
|
|
111
|
+
this.log = createLogger('sequencer:checkpoint-proposal', {
|
|
112
|
+
...bindings,
|
|
113
|
+
instanceId: `slot-${this.slotNow}`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The wall-clock slot during which the proposer builds. */
|
|
118
|
+
private get slot(): SlotNumber {
|
|
119
|
+
return this.slotNow;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The wall-clock epoch. */
|
|
123
|
+
private get epoch(): EpochNumber {
|
|
124
|
+
return this.epochNow;
|
|
125
|
+
}
|
|
87
126
|
|
|
88
127
|
/**
|
|
89
128
|
* Executes the checkpoint proposal job.
|
|
@@ -95,7 +134,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
95
134
|
// In fisherman mode, we simulate slashing but don't actually publish to L1
|
|
96
135
|
// These are constant for the whole slot, so we only enqueue them once
|
|
97
136
|
const votesPromises = new CheckpointVoter(
|
|
98
|
-
this.
|
|
137
|
+
this.targetSlot,
|
|
99
138
|
this.publisher,
|
|
100
139
|
this.attestorAddress,
|
|
101
140
|
this.validatorClient,
|
|
@@ -113,7 +152,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
113
152
|
await Promise.all(votesPromises);
|
|
114
153
|
|
|
115
154
|
if (checkpoint) {
|
|
116
|
-
this.metrics.
|
|
155
|
+
this.metrics.recordCheckpointProposalSuccess();
|
|
117
156
|
}
|
|
118
157
|
|
|
119
158
|
// Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
|
|
@@ -122,6 +161,29 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
122
161
|
return;
|
|
123
162
|
}
|
|
124
163
|
|
|
164
|
+
// If pipelining, wait until the submission slot so L1 recognizes the pipelined proposer
|
|
165
|
+
if (this.epochCache.isProposerPipeliningEnabled()) {
|
|
166
|
+
const submissionSlotTimestamp =
|
|
167
|
+
getTimestampForSlot(this.targetSlot, this.l1Constants) - BigInt(this.l1Constants.ethereumSlotDuration);
|
|
168
|
+
this.log.info(`Waiting until submission slot ${this.targetSlot} for L1 submission`, {
|
|
169
|
+
slot: this.slot,
|
|
170
|
+
submissionSlot: this.targetSlot,
|
|
171
|
+
submissionSlotTimestamp,
|
|
172
|
+
});
|
|
173
|
+
await sleepUntil(new Date(Number(submissionSlotTimestamp) * 1000), this.dateProvider.nowAsDate());
|
|
174
|
+
|
|
175
|
+
// After waking, verify the parent checkpoint wasn't pruned during the sleep.
|
|
176
|
+
// We check L1's pending tip directly instead of canProposeAt, which also validates the proposer
|
|
177
|
+
// identity and would fail because the timestamp resolves to a different slot's proposer.
|
|
178
|
+
const l1Tips = await this.publisher.rollupContract.getTips();
|
|
179
|
+
if (l1Tips.pending < this.checkpointNumber - 1) {
|
|
180
|
+
this.log.warn(
|
|
181
|
+
`Parent checkpoint was pruned during pipelining sleep (L1 pending=${l1Tips.pending}, expected>=${this.checkpointNumber - 1}), skipping L1 submission for checkpoint ${this.checkpointNumber}`,
|
|
182
|
+
);
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
125
187
|
// Then send everything to L1
|
|
126
188
|
const l1Response = await this.publisher.sendRequests();
|
|
127
189
|
const proposedAction = l1Response?.successfulActions.find(a => a === 'propose');
|
|
@@ -140,7 +202,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
140
202
|
return {
|
|
141
203
|
// nullish operator needed for tests
|
|
142
204
|
[Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
|
|
143
|
-
[Attributes.SLOT_NUMBER]: this.
|
|
205
|
+
[Attributes.SLOT_NUMBER]: this.targetSlot,
|
|
144
206
|
};
|
|
145
207
|
})
|
|
146
208
|
private async proposeCheckpoint(): Promise<Checkpoint | undefined> {
|
|
@@ -150,8 +212,15 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
150
212
|
const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
|
|
151
213
|
|
|
152
214
|
// Start the checkpoint
|
|
153
|
-
this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.
|
|
154
|
-
this.
|
|
215
|
+
this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.targetSlot);
|
|
216
|
+
this.log.info(`Starting checkpoint proposal`, {
|
|
217
|
+
buildSlot: this.slot,
|
|
218
|
+
submissionSlot: this.targetSlot,
|
|
219
|
+
pipelining: this.epochCache.isProposerPipeliningEnabled(),
|
|
220
|
+
proposer: this.proposer?.toString(),
|
|
221
|
+
coinbase: coinbase.toString(),
|
|
222
|
+
});
|
|
223
|
+
this.metrics.incOpenSlot(this.targetSlot, this.proposer?.toString() ?? 'unknown');
|
|
155
224
|
|
|
156
225
|
// Enqueues checkpoint invalidation (constant for the whole slot)
|
|
157
226
|
if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
|
|
@@ -162,22 +231,33 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
162
231
|
const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(
|
|
163
232
|
coinbase,
|
|
164
233
|
feeRecipient,
|
|
165
|
-
this.
|
|
234
|
+
this.targetSlot,
|
|
166
235
|
);
|
|
167
236
|
|
|
168
237
|
// Collect L1 to L2 messages for the checkpoint and compute their hash
|
|
169
238
|
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
|
|
170
239
|
const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
171
240
|
|
|
241
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
242
|
+
const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.targetEpoch))
|
|
243
|
+
.filter(c => c.checkpointNumber < this.checkpointNumber)
|
|
244
|
+
.map(c => c.checkpointOutHash);
|
|
245
|
+
|
|
246
|
+
// Get the fee asset price modifier from the oracle
|
|
247
|
+
const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
|
|
248
|
+
|
|
172
249
|
// Create a long-lived forked world state for the checkpoint builder
|
|
173
|
-
using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
|
|
250
|
+
await using fork = await this.worldState.fork(this.syncedToBlockNumber, { closeDelayMs: 12_000 });
|
|
174
251
|
|
|
175
252
|
// Create checkpoint builder for the entire slot
|
|
176
253
|
const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
|
|
177
254
|
this.checkpointNumber,
|
|
178
255
|
checkpointGlobalVariables,
|
|
256
|
+
feeAssetPriceModifier,
|
|
179
257
|
l1ToL2Messages,
|
|
258
|
+
previousCheckpointOutHashes,
|
|
180
259
|
fork,
|
|
260
|
+
this.log.getBindings(),
|
|
181
261
|
);
|
|
182
262
|
|
|
183
263
|
// Options for the validator client when creating block and checkpoint proposals
|
|
@@ -191,32 +271,82 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
191
271
|
broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal,
|
|
192
272
|
};
|
|
193
273
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
274
|
+
let blocksInCheckpoint: L2Block[] = [];
|
|
275
|
+
let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
|
|
276
|
+
const checkpointBuildTimer = new Timer();
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
// Main loop: build blocks for the checkpoint
|
|
280
|
+
const result = await this.buildBlocksForCheckpoint(
|
|
281
|
+
checkpointBuilder,
|
|
282
|
+
checkpointGlobalVariables.timestamp,
|
|
283
|
+
inHash,
|
|
284
|
+
blockProposalOptions,
|
|
285
|
+
);
|
|
286
|
+
blocksInCheckpoint = result.blocksInCheckpoint;
|
|
287
|
+
blockPendingBroadcast = result.blockPendingBroadcast;
|
|
288
|
+
} catch (err) {
|
|
289
|
+
// These errors are expected in HA mode, so we yield and let another HA node handle the slot
|
|
290
|
+
// The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
|
|
291
|
+
// which is normal for block building (may have picked different txs)
|
|
292
|
+
if (this.handleHASigningError(err, 'Block proposal')) {
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
throw err;
|
|
296
|
+
}
|
|
201
297
|
|
|
202
298
|
if (blocksInCheckpoint.length === 0) {
|
|
203
|
-
this.log.warn(`No blocks were built for slot ${this.
|
|
204
|
-
this.eventEmitter.emit('checkpoint-empty', { slot: this.
|
|
299
|
+
this.log.warn(`No blocks were built for slot ${this.targetSlot}`, { slot: this.targetSlot });
|
|
300
|
+
this.eventEmitter.emit('checkpoint-empty', { slot: this.targetSlot });
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
|
|
305
|
+
if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
|
|
306
|
+
this.log.warn(
|
|
307
|
+
`Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`,
|
|
308
|
+
{ slot: this.targetSlot, blocksBuilt: blocksInCheckpoint.length, minBlocksForCheckpoint },
|
|
309
|
+
);
|
|
205
310
|
return undefined;
|
|
206
311
|
}
|
|
207
312
|
|
|
208
313
|
// Assemble and broadcast the checkpoint proposal, including the last block that was not
|
|
209
314
|
// broadcasted yet, and wait to collect the committee attestations.
|
|
210
|
-
this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.
|
|
315
|
+
this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.targetSlot);
|
|
211
316
|
const checkpoint = await checkpointBuilder.completeCheckpoint();
|
|
212
317
|
|
|
318
|
+
// Final validation: per-block limits are only checked if the operator set them explicitly.
|
|
319
|
+
// Otherwise, checkpoint-level budgets were already enforced by the redistribution logic.
|
|
320
|
+
try {
|
|
321
|
+
validateCheckpoint(checkpoint, {
|
|
322
|
+
rollupManaLimit: this.l1Constants.rollupManaLimit,
|
|
323
|
+
maxL2BlockGas: this.config.maxL2BlockGas,
|
|
324
|
+
maxDABlockGas: this.config.maxDABlockGas,
|
|
325
|
+
maxTxsPerBlock: this.config.maxTxsPerBlock,
|
|
326
|
+
maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint,
|
|
327
|
+
});
|
|
328
|
+
} catch (err) {
|
|
329
|
+
this.log.error(`Built an invalid checkpoint at slot ${this.slot} (skipping proposal)`, err, {
|
|
330
|
+
checkpoint: checkpoint.header.toInspect(),
|
|
331
|
+
});
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Record checkpoint-level build metrics
|
|
336
|
+
this.metrics.recordCheckpointBuild(
|
|
337
|
+
checkpointBuildTimer.ms(),
|
|
338
|
+
blocksInCheckpoint.length,
|
|
339
|
+
checkpoint.getStats().txCount,
|
|
340
|
+
Number(checkpoint.header.totalManaUsed.toBigInt()),
|
|
341
|
+
);
|
|
342
|
+
|
|
213
343
|
// Do not collect attestations nor publish to L1 in fisherman mode
|
|
214
344
|
if (this.config.fishermanMode) {
|
|
215
345
|
this.log.info(
|
|
216
|
-
`Built checkpoint for slot ${this.
|
|
346
|
+
`Built checkpoint for slot ${this.targetSlot} with ${blocksInCheckpoint.length} blocks. ` +
|
|
217
347
|
`Skipping proposal in fisherman mode.`,
|
|
218
348
|
{
|
|
219
|
-
slot: this.
|
|
349
|
+
slot: this.targetSlot,
|
|
220
350
|
checkpoint: checkpoint.header.toInspect(),
|
|
221
351
|
blocksBuilt: blocksInCheckpoint.length,
|
|
222
352
|
},
|
|
@@ -236,6 +366,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
236
366
|
const proposal = await this.validatorClient.createCheckpointProposal(
|
|
237
367
|
checkpoint.header,
|
|
238
368
|
checkpoint.archive.root,
|
|
369
|
+
feeAssetPriceModifier,
|
|
239
370
|
lastBlock,
|
|
240
371
|
this.proposer,
|
|
241
372
|
checkpointProposalOptions,
|
|
@@ -244,7 +375,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
244
375
|
const blockProposedAt = this.dateProvider.now();
|
|
245
376
|
await this.p2pClient.broadcastCheckpointProposal(proposal);
|
|
246
377
|
|
|
247
|
-
this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.
|
|
378
|
+
this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.targetSlot);
|
|
248
379
|
const attestations = await this.waitForAttestations(proposal);
|
|
249
380
|
const blockAttestedAt = this.dateProvider.now();
|
|
250
381
|
|
|
@@ -252,13 +383,43 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
252
383
|
|
|
253
384
|
// Proposer must sign over the attestations before pushing them to L1
|
|
254
385
|
const signer = this.proposer ?? this.publisher.getSenderAddress();
|
|
255
|
-
|
|
386
|
+
let attestationsSignature: Signature;
|
|
387
|
+
try {
|
|
388
|
+
attestationsSignature = await this.validatorClient.signAttestationsAndSigners(
|
|
389
|
+
attestations,
|
|
390
|
+
signer,
|
|
391
|
+
this.targetSlot,
|
|
392
|
+
this.checkpointNumber,
|
|
393
|
+
);
|
|
394
|
+
} catch (err) {
|
|
395
|
+
// We shouldn't really get here since we yield to another HA node
|
|
396
|
+
// as soon as we see these errors when creating block or checkpoint proposals.
|
|
397
|
+
if (this.handleHASigningError(err, 'Attestations signature')) {
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
256
402
|
|
|
257
403
|
// Enqueue publishing the checkpoint to L1
|
|
258
|
-
this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.
|
|
404
|
+
this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.targetSlot);
|
|
259
405
|
const aztecSlotDuration = this.l1Constants.slotDuration;
|
|
260
|
-
const
|
|
261
|
-
const txTimeoutAt = new Date((
|
|
406
|
+
const submissionSlotStart = Number(getTimestampForSlot(this.targetSlot, this.l1Constants));
|
|
407
|
+
const txTimeoutAt = new Date((submissionSlotStart + aztecSlotDuration) * 1000);
|
|
408
|
+
|
|
409
|
+
// If we have been configured to potentially skip publishing checkpoint then roll the dice here
|
|
410
|
+
if (
|
|
411
|
+
this.config.skipPublishingCheckpointsPercent !== undefined &&
|
|
412
|
+
this.config.skipPublishingCheckpointsPercent > 0
|
|
413
|
+
) {
|
|
414
|
+
const result = Math.max(0, randomInt(100));
|
|
415
|
+
if (result < this.config.skipPublishingCheckpointsPercent) {
|
|
416
|
+
this.log.warn(
|
|
417
|
+
`Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`,
|
|
418
|
+
);
|
|
419
|
+
return checkpoint;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
262
423
|
await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
|
|
263
424
|
txTimeoutAt,
|
|
264
425
|
forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber,
|
|
@@ -266,6 +427,11 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
266
427
|
|
|
267
428
|
return checkpoint;
|
|
268
429
|
} catch (err) {
|
|
430
|
+
if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
|
|
431
|
+
// swallow this error. It's already been logged by a function deeper in the stack
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
|
|
269
435
|
this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
|
|
270
436
|
return undefined;
|
|
271
437
|
}
|
|
@@ -281,19 +447,19 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
281
447
|
inHash: Fr,
|
|
282
448
|
blockProposalOptions: BlockProposalOptions,
|
|
283
449
|
): Promise<{
|
|
284
|
-
blocksInCheckpoint:
|
|
285
|
-
blockPendingBroadcast: { block:
|
|
450
|
+
blocksInCheckpoint: L2Block[];
|
|
451
|
+
blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined;
|
|
286
452
|
}> {
|
|
287
|
-
const blocksInCheckpoint:
|
|
453
|
+
const blocksInCheckpoint: L2Block[] = [];
|
|
288
454
|
const txHashesAlreadyIncluded = new Set<string>();
|
|
289
455
|
const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
|
|
290
456
|
|
|
291
457
|
// Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
|
|
292
|
-
let blockPendingBroadcast: { block:
|
|
458
|
+
let blockPendingBroadcast: { block: L2Block; txs: Tx[] } | undefined = undefined;
|
|
293
459
|
|
|
294
460
|
while (true) {
|
|
295
461
|
const blocksBuilt = blocksInCheckpoint.length;
|
|
296
|
-
const indexWithinCheckpoint = blocksBuilt;
|
|
462
|
+
const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
|
|
297
463
|
const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
|
|
298
464
|
|
|
299
465
|
const secondsIntoSlot = this.getSecondsIntoSlot();
|
|
@@ -301,7 +467,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
301
467
|
|
|
302
468
|
if (!timingInfo.canStart) {
|
|
303
469
|
this.log.debug(`Not enough time left in slot to start another block`, {
|
|
304
|
-
slot: this.
|
|
470
|
+
slot: this.targetSlot,
|
|
305
471
|
blocksBuilt,
|
|
306
472
|
secondsIntoSlot,
|
|
307
473
|
});
|
|
@@ -322,6 +488,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
322
488
|
txHashesAlreadyIncluded,
|
|
323
489
|
});
|
|
324
490
|
|
|
491
|
+
// TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
|
|
325
492
|
if (!buildResult && timingInfo.isLastBlock) {
|
|
326
493
|
// If no block was produced due to not enough txs and this was the last subslot, exit
|
|
327
494
|
break;
|
|
@@ -335,8 +502,8 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
335
502
|
} else if ('error' in buildResult) {
|
|
336
503
|
// If there was an error building the block, just exit the loop and give up the rest of the slot
|
|
337
504
|
if (!(buildResult.error instanceof SequencerInterruptedError)) {
|
|
338
|
-
this.log.warn(`Halting block building for slot ${this.
|
|
339
|
-
slot: this.
|
|
505
|
+
this.log.warn(`Halting block building for slot ${this.targetSlot}`, {
|
|
506
|
+
slot: this.targetSlot,
|
|
340
507
|
blocksBuilt,
|
|
341
508
|
error: buildResult.error,
|
|
342
509
|
});
|
|
@@ -346,16 +513,14 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
346
513
|
|
|
347
514
|
const { block, usedTxs } = buildResult;
|
|
348
515
|
blocksInCheckpoint.push(block);
|
|
516
|
+
usedTxs.forEach(tx => txHashesAlreadyIncluded.add(tx.txHash.toString()));
|
|
349
517
|
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
// Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
|
|
353
|
-
await this.syncProposedBlockToArchiver(block);
|
|
354
|
-
|
|
355
|
-
// If this is the last block, exit the loop now so we start collecting attestations
|
|
518
|
+
// If this is the last block, sync it to the archiver and exit the loop
|
|
519
|
+
// so we can build the checkpoint and start collecting attestations.
|
|
356
520
|
if (timingInfo.isLastBlock) {
|
|
357
|
-
this.
|
|
358
|
-
|
|
521
|
+
await this.syncProposedBlockToArchiver(block);
|
|
522
|
+
this.log.verbose(`Completed final block ${blockNumber} for slot ${this.targetSlot}`, {
|
|
523
|
+
slot: this.targetSlot,
|
|
359
524
|
blockNumber,
|
|
360
525
|
blocksBuilt,
|
|
361
526
|
});
|
|
@@ -363,72 +528,94 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
363
528
|
break;
|
|
364
529
|
}
|
|
365
530
|
|
|
366
|
-
//
|
|
367
|
-
//
|
|
368
|
-
if
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
531
|
+
// Broadcast the block proposal (unless we're in fisherman mode) unless the block is the last one,
|
|
532
|
+
// in which case we'll broadcast it along with the checkpoint at the end of the loop.
|
|
533
|
+
// Note that we only send the block to the archiver if we manage to create the proposal, so if there's
|
|
534
|
+
// a HA error we don't pollute our archiver with a block that won't make it to the chain.
|
|
535
|
+
const proposal = await this.createBlockProposal(block, inHash, usedTxs, blockProposalOptions);
|
|
536
|
+
|
|
537
|
+
// Sync the proposed block to the archiver to make it available, only after we've managed to sign the proposal.
|
|
538
|
+
// We wait for the sync to succeed, as this helps catch consistency errors, even if it means we lose some time for block-building.
|
|
539
|
+
// If this throws, we abort the entire checkpoint.
|
|
540
|
+
await this.syncProposedBlockToArchiver(block);
|
|
541
|
+
|
|
542
|
+
// Once we have a signed proposal and the archiver agreed with our proposed block, then we broadcast it.
|
|
543
|
+
proposal && (await this.p2pClient.broadcastProposal(proposal));
|
|
380
544
|
|
|
381
545
|
// Wait until the next block's start time
|
|
382
546
|
await this.waitUntilNextSubslot(timingInfo.deadline);
|
|
383
547
|
}
|
|
384
548
|
|
|
385
|
-
this.log.verbose(`Block building loop completed for slot ${this.
|
|
386
|
-
slot: this.
|
|
549
|
+
this.log.verbose(`Block building loop completed for slot ${this.targetSlot}`, {
|
|
550
|
+
slot: this.targetSlot,
|
|
387
551
|
blocksBuilt: blocksInCheckpoint.length,
|
|
388
552
|
});
|
|
389
553
|
|
|
390
554
|
return { blocksInCheckpoint, blockPendingBroadcast };
|
|
391
555
|
}
|
|
392
556
|
|
|
557
|
+
/** Creates a block proposal for a given block via the validator client (unless in fisherman mode) */
|
|
558
|
+
private createBlockProposal(
|
|
559
|
+
block: L2Block,
|
|
560
|
+
inHash: Fr,
|
|
561
|
+
usedTxs: Tx[],
|
|
562
|
+
blockProposalOptions: BlockProposalOptions,
|
|
563
|
+
): Promise<BlockProposal | undefined> {
|
|
564
|
+
if (this.config.fishermanMode) {
|
|
565
|
+
this.log.info(`Skipping block proposal for block ${block.number} in fisherman mode`);
|
|
566
|
+
return Promise.resolve(undefined);
|
|
567
|
+
}
|
|
568
|
+
return this.validatorClient.createBlockProposal(
|
|
569
|
+
block.header,
|
|
570
|
+
block.indexWithinCheckpoint,
|
|
571
|
+
inHash,
|
|
572
|
+
block.archive.root,
|
|
573
|
+
usedTxs,
|
|
574
|
+
this.proposer,
|
|
575
|
+
blockProposalOptions,
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
393
579
|
/** Sleeps until it is time to produce the next block in the slot */
|
|
394
580
|
@trackSpan('CheckpointProposalJob.waitUntilNextSubslot')
|
|
395
581
|
private async waitUntilNextSubslot(nextSubslotStart: number) {
|
|
396
|
-
this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.
|
|
397
|
-
this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
|
|
582
|
+
this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.targetSlot);
|
|
583
|
+
this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
|
|
584
|
+
slot: this.targetSlot,
|
|
585
|
+
});
|
|
398
586
|
await this.waitUntilTimeInSlot(nextSubslotStart);
|
|
399
587
|
}
|
|
400
588
|
|
|
401
589
|
/** Builds a single block. Called from the main block building loop. */
|
|
402
590
|
@trackSpan('CheckpointProposalJob.buildSingleBlock')
|
|
403
|
-
|
|
591
|
+
protected async buildSingleBlock(
|
|
404
592
|
checkpointBuilder: CheckpointBuilder,
|
|
405
593
|
opts: {
|
|
406
594
|
forceCreate?: boolean;
|
|
407
595
|
blockTimestamp: bigint;
|
|
408
596
|
blockNumber: BlockNumber;
|
|
409
|
-
indexWithinCheckpoint:
|
|
597
|
+
indexWithinCheckpoint: IndexWithinCheckpoint;
|
|
410
598
|
buildDeadline: Date | undefined;
|
|
411
599
|
txHashesAlreadyIncluded: Set<string>;
|
|
412
600
|
},
|
|
413
|
-
): Promise<{ block:
|
|
601
|
+
): Promise<{ block: L2Block; usedTxs: Tx[] } | { error: Error } | undefined> {
|
|
414
602
|
const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } =
|
|
415
603
|
opts;
|
|
416
604
|
|
|
417
605
|
this.log.verbose(
|
|
418
|
-
`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.
|
|
606
|
+
`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot}`,
|
|
419
607
|
{ ...checkpointBuilder.getConstantData(), ...opts },
|
|
420
608
|
);
|
|
421
609
|
|
|
422
610
|
try {
|
|
423
611
|
// Wait until we have enough txs to build the block
|
|
424
|
-
const minTxs = this.
|
|
425
|
-
const { availableTxs, canStartBuilding } = await this.waitForMinTxs(opts);
|
|
612
|
+
const { availableTxs, canStartBuilding, minTxs } = await this.waitForMinTxs(opts);
|
|
426
613
|
if (!canStartBuilding) {
|
|
427
614
|
this.log.warn(
|
|
428
|
-
`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.
|
|
429
|
-
{ blockNumber, slot: this.
|
|
615
|
+
`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (got ${availableTxs} txs but needs ${minTxs})`,
|
|
616
|
+
{ blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
|
|
430
617
|
);
|
|
431
|
-
this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.
|
|
618
|
+
this.eventEmitter.emit('block-tx-count-check-failed', { minTxs, availableTxs, slot: this.targetSlot });
|
|
432
619
|
this.metrics.recordBlockProposalFailed('insufficient_txs');
|
|
433
620
|
return undefined;
|
|
434
621
|
}
|
|
@@ -436,91 +623,143 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
436
623
|
// Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
|
|
437
624
|
// just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
|
|
438
625
|
const pendingTxs = filter(
|
|
439
|
-
this.p2pClient.
|
|
626
|
+
this.p2pClient.iterateEligiblePendingTxs(),
|
|
440
627
|
tx => !txHashesAlreadyIncluded.has(tx.txHash.toString()),
|
|
441
628
|
);
|
|
442
629
|
|
|
443
630
|
this.log.debug(
|
|
444
|
-
`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.
|
|
445
|
-
{ slot: this.
|
|
631
|
+
`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.targetSlot} with ${availableTxs} available txs`,
|
|
632
|
+
{ slot: this.targetSlot, blockNumber, indexWithinCheckpoint },
|
|
446
633
|
);
|
|
447
|
-
this.setStateFn(SequencerState.CREATING_BLOCK, this.
|
|
448
|
-
|
|
634
|
+
this.setStateFn(SequencerState.CREATING_BLOCK, this.targetSlot);
|
|
635
|
+
|
|
636
|
+
// Per-block limits are operator overrides (from SEQ_MAX_L2_BLOCK_GAS etc.) further capped
|
|
637
|
+
// by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
|
|
638
|
+
// minValidTxs is passed into the builder so it can reject the block *before* updating state.
|
|
639
|
+
const minValidTxs = forceCreate ? 0 : (this.config.minValidTxsPerBlock ?? minTxs);
|
|
640
|
+
const blockBuilderOptions: BlockBuilderOptions = {
|
|
449
641
|
maxTransactions: this.config.maxTxsPerBlock,
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
642
|
+
maxBlockGas:
|
|
643
|
+
this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined
|
|
644
|
+
? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity)
|
|
645
|
+
: undefined,
|
|
453
646
|
deadline: buildDeadline,
|
|
647
|
+
isBuildingProposal: true,
|
|
648
|
+
minValidTxs,
|
|
649
|
+
maxBlocksPerCheckpoint: this.timetable.maxNumberOfBlocks,
|
|
650
|
+
perBlockAllocationMultiplier: this.config.perBlockAllocationMultiplier,
|
|
454
651
|
};
|
|
455
652
|
|
|
456
|
-
// Actually build the block by executing txs
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
653
|
+
// Actually build the block by executing txs. The builder throws InsufficientValidTxsError
|
|
654
|
+
// if the number of successfully processed txs is below minValidTxs, ensuring state is not
|
|
655
|
+
// updated for blocks that will be discarded.
|
|
656
|
+
const buildResult = await this.buildSingleBlockWithCheckpointBuilder(
|
|
657
|
+
checkpointBuilder,
|
|
658
|
+
pendingTxs,
|
|
659
|
+
blockNumber,
|
|
660
|
+
blockTimestamp,
|
|
661
|
+
blockBuilderOptions,
|
|
662
|
+
);
|
|
461
663
|
|
|
462
664
|
// If any txs failed during execution, drop them from the mempool so we don't pick them up again
|
|
463
|
-
await this.dropFailedTxsFromP2P(failedTxs);
|
|
665
|
+
await this.dropFailedTxsFromP2P(buildResult.failedTxs);
|
|
464
666
|
|
|
465
|
-
|
|
466
|
-
// too long, then we may not get to minTxsPerBlock after executing public functions.
|
|
467
|
-
const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
|
|
468
|
-
if (!forceCreate && numTxs < minValidTxs) {
|
|
667
|
+
if (buildResult.status === 'insufficient-valid-txs') {
|
|
469
668
|
this.log.warn(
|
|
470
|
-
`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.
|
|
471
|
-
{
|
|
669
|
+
`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.targetSlot} has too few valid txs to be proposed`,
|
|
670
|
+
{
|
|
671
|
+
slot: this.targetSlot,
|
|
672
|
+
blockNumber,
|
|
673
|
+
numTxs: buildResult.processedCount,
|
|
674
|
+
indexWithinCheckpoint,
|
|
675
|
+
minValidTxs,
|
|
676
|
+
},
|
|
472
677
|
);
|
|
473
|
-
this.eventEmitter.emit('block-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
slot: this.slot,
|
|
678
|
+
this.eventEmitter.emit('block-build-failed', {
|
|
679
|
+
reason: `Insufficient valid txs`,
|
|
680
|
+
slot: this.targetSlot,
|
|
477
681
|
});
|
|
478
682
|
this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
|
|
479
683
|
return undefined;
|
|
480
684
|
}
|
|
481
685
|
|
|
482
686
|
// Block creation succeeded, emit stats and metrics
|
|
687
|
+
const { block, publicProcessorDuration, usedTxs, blockBuildDuration, numTxs } = buildResult;
|
|
688
|
+
|
|
483
689
|
const blockStats = {
|
|
484
690
|
eventName: 'l2-block-built',
|
|
485
691
|
duration: blockBuildDuration,
|
|
486
692
|
publicProcessDuration: publicProcessorDuration,
|
|
487
|
-
rollupCircuitsDuration: blockBuildingTimer.ms(),
|
|
488
693
|
...block.getStats(),
|
|
489
694
|
} satisfies L2BlockBuiltStats;
|
|
490
695
|
|
|
491
696
|
const blockHash = await block.hash();
|
|
492
697
|
const txHashes = block.body.txEffects.map(tx => tx.txHash);
|
|
493
|
-
const manaPerSec =
|
|
698
|
+
const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
|
|
494
699
|
|
|
495
700
|
this.log.info(
|
|
496
|
-
`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.
|
|
701
|
+
`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.targetSlot} with ${numTxs} txs`,
|
|
497
702
|
{ blockHash, txHashes, manaPerSec, ...blockStats },
|
|
498
703
|
);
|
|
499
704
|
|
|
500
|
-
this.eventEmitter.emit('block-proposed', {
|
|
501
|
-
|
|
705
|
+
this.eventEmitter.emit('block-proposed', {
|
|
706
|
+
blockNumber: block.number,
|
|
707
|
+
slot: this.targetSlot,
|
|
708
|
+
buildSlot: this.slotNow,
|
|
709
|
+
});
|
|
710
|
+
this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe());
|
|
502
711
|
|
|
503
712
|
return { block, usedTxs };
|
|
504
713
|
} catch (err: any) {
|
|
505
|
-
this.eventEmitter.emit('block-build-failed', {
|
|
506
|
-
|
|
714
|
+
this.eventEmitter.emit('block-build-failed', {
|
|
715
|
+
reason: err.message,
|
|
716
|
+
slot: this.targetSlot,
|
|
717
|
+
});
|
|
718
|
+
this.log.error(`Error building block`, err, { blockNumber, slot: this.targetSlot });
|
|
507
719
|
this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
|
|
508
720
|
this.metrics.recordFailedBlock();
|
|
509
721
|
return { error: err };
|
|
510
722
|
}
|
|
511
723
|
}
|
|
512
724
|
|
|
725
|
+
/** Uses the checkpoint builder to build a block, catching InsufficientValidTxsError. */
|
|
726
|
+
private async buildSingleBlockWithCheckpointBuilder(
|
|
727
|
+
checkpointBuilder: CheckpointBuilder,
|
|
728
|
+
pendingTxs: AsyncIterable<Tx>,
|
|
729
|
+
blockNumber: BlockNumber,
|
|
730
|
+
blockTimestamp: bigint,
|
|
731
|
+
blockBuilderOptions: BlockBuilderOptions,
|
|
732
|
+
) {
|
|
733
|
+
try {
|
|
734
|
+
const workTimer = new Timer();
|
|
735
|
+
const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
|
|
736
|
+
const blockBuildDuration = workTimer.ms();
|
|
737
|
+
return { ...result, blockBuildDuration, status: 'success' as const };
|
|
738
|
+
} catch (err: unknown) {
|
|
739
|
+
if (isErrorClass(err, InsufficientValidTxsError)) {
|
|
740
|
+
return {
|
|
741
|
+
failedTxs: err.failedTxs,
|
|
742
|
+
processedCount: err.processedCount,
|
|
743
|
+
status: 'insufficient-valid-txs' as const,
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
throw err;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
513
750
|
/** Waits until minTxs are available on the pool for building a block. */
|
|
514
751
|
@trackSpan('CheckpointProposalJob.waitForMinTxs')
|
|
515
752
|
private async waitForMinTxs(opts: {
|
|
516
753
|
forceCreate?: boolean;
|
|
517
754
|
blockNumber: BlockNumber;
|
|
518
|
-
indexWithinCheckpoint:
|
|
755
|
+
indexWithinCheckpoint: IndexWithinCheckpoint;
|
|
519
756
|
buildDeadline: Date | undefined;
|
|
520
|
-
}): Promise<{ canStartBuilding: boolean; availableTxs: number }> {
|
|
521
|
-
const minTxs = this.config.minTxsPerBlock;
|
|
757
|
+
}): Promise<{ canStartBuilding: boolean; availableTxs: number; minTxs: number }> {
|
|
522
758
|
const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
|
|
523
759
|
|
|
760
|
+
// We only allow a block with 0 txs in the first block of the checkpoint
|
|
761
|
+
const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
|
|
762
|
+
|
|
524
763
|
// Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
|
|
525
764
|
const startBuildingDeadline = buildDeadline
|
|
526
765
|
? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000)
|
|
@@ -532,20 +771,20 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
532
771
|
// If we're past deadline, or we have no deadline, give up
|
|
533
772
|
const now = this.dateProvider.nowAsDate();
|
|
534
773
|
if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
|
|
535
|
-
return { canStartBuilding: false, availableTxs
|
|
774
|
+
return { canStartBuilding: false, availableTxs, minTxs };
|
|
536
775
|
}
|
|
537
776
|
|
|
538
777
|
// Wait a bit before checking again
|
|
539
|
-
this.setStateFn(SequencerState.WAITING_FOR_TXS, this.
|
|
778
|
+
this.setStateFn(SequencerState.WAITING_FOR_TXS, this.targetSlot);
|
|
540
779
|
this.log.verbose(
|
|
541
|
-
`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.
|
|
542
|
-
{ blockNumber, slot: this.
|
|
780
|
+
`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.targetSlot} (have ${availableTxs} but need ${minTxs})`,
|
|
781
|
+
{ blockNumber, slot: this.targetSlot, indexWithinCheckpoint },
|
|
543
782
|
);
|
|
544
|
-
await
|
|
783
|
+
await this.waitForTxsPollingInterval();
|
|
545
784
|
availableTxs = await this.p2pClient.getPendingTxCount();
|
|
546
785
|
}
|
|
547
786
|
|
|
548
|
-
return { canStartBuilding: true, availableTxs };
|
|
787
|
+
return { canStartBuilding: true, availableTxs, minTxs };
|
|
549
788
|
}
|
|
550
789
|
|
|
551
790
|
/**
|
|
@@ -582,7 +821,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
582
821
|
const attestationTimeAllowed = this.config.enforceTimeTable
|
|
583
822
|
? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT)!
|
|
584
823
|
: this.l1Constants.slotDuration;
|
|
585
|
-
const attestationDeadline = new Date(this.
|
|
824
|
+
const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
|
|
586
825
|
|
|
587
826
|
this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
|
|
588
827
|
|
|
@@ -597,11 +836,28 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
597
836
|
|
|
598
837
|
collectedAttestationsCount = attestations.length;
|
|
599
838
|
|
|
839
|
+
// Trim attestations to minimum required to save L1 calldata gas
|
|
840
|
+
const localAddresses = this.validatorClient.getValidatorAddresses();
|
|
841
|
+
const trimmed = trimAttestations(
|
|
842
|
+
attestations,
|
|
843
|
+
numberOfRequiredAttestations,
|
|
844
|
+
this.attestorAddress,
|
|
845
|
+
localAddresses,
|
|
846
|
+
);
|
|
847
|
+
if (trimmed.length < attestations.length) {
|
|
848
|
+
this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
|
|
849
|
+
}
|
|
850
|
+
|
|
600
851
|
// Rollup contract requires that the signatures are provided in the order of the committee
|
|
601
|
-
const sorted = orderAttestations(
|
|
852
|
+
const sorted = orderAttestations(trimmed, committee);
|
|
602
853
|
|
|
603
854
|
// Manipulate the attestations if we've been configured to do so
|
|
604
|
-
if (
|
|
855
|
+
if (
|
|
856
|
+
this.config.injectFakeAttestation ||
|
|
857
|
+
this.config.injectHighSValueAttestation ||
|
|
858
|
+
this.config.injectUnrecoverableSignatureAttestation ||
|
|
859
|
+
this.config.shuffleAttestationOrdering
|
|
860
|
+
) {
|
|
605
861
|
return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
|
|
606
862
|
}
|
|
607
863
|
|
|
@@ -630,7 +886,11 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
630
886
|
this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)),
|
|
631
887
|
);
|
|
632
888
|
|
|
633
|
-
if (
|
|
889
|
+
if (
|
|
890
|
+
this.config.injectFakeAttestation ||
|
|
891
|
+
this.config.injectHighSValueAttestation ||
|
|
892
|
+
this.config.injectUnrecoverableSignatureAttestation
|
|
893
|
+
) {
|
|
634
894
|
// Find non-empty attestations that are not from the proposer
|
|
635
895
|
const nonProposerIndices: number[] = [];
|
|
636
896
|
for (let i = 0; i < attestations.length; i++) {
|
|
@@ -640,8 +900,20 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
640
900
|
}
|
|
641
901
|
if (nonProposerIndices.length > 0) {
|
|
642
902
|
const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
|
|
643
|
-
this.
|
|
644
|
-
|
|
903
|
+
if (this.config.injectHighSValueAttestation) {
|
|
904
|
+
this.log.warn(
|
|
905
|
+
`Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
|
|
906
|
+
);
|
|
907
|
+
unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
|
|
908
|
+
} else if (this.config.injectUnrecoverableSignatureAttestation) {
|
|
909
|
+
this.log.warn(
|
|
910
|
+
`Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`,
|
|
911
|
+
);
|
|
912
|
+
unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
|
|
913
|
+
} else {
|
|
914
|
+
this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
|
|
915
|
+
unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
|
|
916
|
+
}
|
|
645
917
|
}
|
|
646
918
|
return new CommitteeAttestationsAndSigners(attestations);
|
|
647
919
|
}
|
|
@@ -650,11 +922,20 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
650
922
|
this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
|
|
651
923
|
|
|
652
924
|
const shuffled = [...attestations];
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
925
|
+
|
|
926
|
+
// Find two non-proposer positions that both have non-empty signatures to swap.
|
|
927
|
+
// This ensures the bitmap doesn't change, so the MaliciousCommitteeAttestationsAndSigners
|
|
928
|
+
// signers array stays correctly aligned with L1's committee reconstruction.
|
|
929
|
+
const swappable: number[] = [];
|
|
930
|
+
for (let k = 0; k < shuffled.length; k++) {
|
|
931
|
+
if (!shuffled[k].signature.isEmpty() && k !== proposerIndex) {
|
|
932
|
+
swappable.push(k);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (swappable.length >= 2) {
|
|
936
|
+
const [i, j] = [swappable[0], swappable[1]];
|
|
937
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
938
|
+
}
|
|
658
939
|
|
|
659
940
|
const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
|
|
660
941
|
return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
|
|
@@ -670,7 +951,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
670
951
|
const failedTxData = failedTxs.map(fail => fail.tx);
|
|
671
952
|
const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
|
|
672
953
|
this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
|
|
673
|
-
await this.p2pClient.
|
|
954
|
+
await this.p2pClient.handleFailedExecution(failedTxHashes);
|
|
674
955
|
}
|
|
675
956
|
|
|
676
957
|
/**
|
|
@@ -678,8 +959,7 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
678
959
|
* Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
|
|
679
960
|
* would never receive its own block without this explicit sync.
|
|
680
961
|
*/
|
|
681
|
-
private async syncProposedBlockToArchiver(block:
|
|
682
|
-
// TODO(palla/mbps): Change default to false once block sync is stable.
|
|
962
|
+
private async syncProposedBlockToArchiver(block: L2Block): Promise<void> {
|
|
683
963
|
if (this.config.skipPushProposedBlocksToArchiver !== false) {
|
|
684
964
|
this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
|
|
685
965
|
blockNumber: block.number,
|
|
@@ -698,27 +978,49 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
698
978
|
private async handleCheckpointEndAsFisherman(checkpoint: Checkpoint | undefined) {
|
|
699
979
|
// Perform L1 fee analysis before clearing requests
|
|
700
980
|
// The callback is invoked asynchronously after the next block is mined
|
|
701
|
-
const feeAnalysis = await this.publisher.analyzeL1Fees(this.
|
|
981
|
+
const feeAnalysis = await this.publisher.analyzeL1Fees(this.targetSlot, analysis =>
|
|
702
982
|
this.metrics.recordFishermanFeeAnalysis(analysis),
|
|
703
983
|
);
|
|
704
984
|
|
|
705
985
|
if (checkpoint) {
|
|
706
|
-
this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.
|
|
986
|
+
this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.targetSlot}`, {
|
|
707
987
|
...checkpoint.toCheckpointInfo(),
|
|
708
988
|
...checkpoint.getStats(),
|
|
709
989
|
feeAnalysisId: feeAnalysis?.id,
|
|
710
990
|
});
|
|
711
991
|
} else {
|
|
712
|
-
this.log.warn(`Validation block building FAILED for slot ${this.
|
|
713
|
-
slot: this.
|
|
992
|
+
this.log.warn(`Validation block building FAILED for slot ${this.targetSlot}`, {
|
|
993
|
+
slot: this.targetSlot,
|
|
714
994
|
feeAnalysisId: feeAnalysis?.id,
|
|
715
995
|
});
|
|
716
|
-
this.metrics.
|
|
996
|
+
this.metrics.recordCheckpointProposalFailed('block_build_failed');
|
|
717
997
|
}
|
|
718
998
|
|
|
719
999
|
this.publisher.clearPendingRequests();
|
|
720
1000
|
}
|
|
721
1001
|
|
|
1002
|
+
/**
|
|
1003
|
+
* Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
|
|
1004
|
+
*/
|
|
1005
|
+
private handleHASigningError(err: any, errorContext: string): boolean {
|
|
1006
|
+
if (err instanceof DutyAlreadySignedError) {
|
|
1007
|
+
this.log.info(`${errorContext} for slot ${this.targetSlot} already signed by another HA node, yielding`, {
|
|
1008
|
+
slot: this.targetSlot,
|
|
1009
|
+
signedByNode: err.signedByNode,
|
|
1010
|
+
});
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
if (err instanceof SlashingProtectionError) {
|
|
1014
|
+
this.log.info(`${errorContext} for slot ${this.targetSlot} blocked by slashing protection, yielding`, {
|
|
1015
|
+
slot: this.targetSlot,
|
|
1016
|
+
existingMessageHash: err.existingMessageHash,
|
|
1017
|
+
attemptedMessageHash: err.attemptedMessageHash,
|
|
1018
|
+
});
|
|
1019
|
+
return true;
|
|
1020
|
+
}
|
|
1021
|
+
return false;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
722
1024
|
/** Waits until a specific time within the current slot */
|
|
723
1025
|
@trackSpan('CheckpointProposalJob.waitUntilTimeInSlot')
|
|
724
1026
|
protected async waitUntilTimeInSlot(targetSecondsIntoSlot: number): Promise<void> {
|
|
@@ -727,6 +1029,11 @@ export class CheckpointProposalJob implements Traceable {
|
|
|
727
1029
|
await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
|
|
728
1030
|
}
|
|
729
1031
|
|
|
1032
|
+
/** Waits the polling interval for transactions. Extracted for test overriding. */
|
|
1033
|
+
protected async waitForTxsPollingInterval(): Promise<void> {
|
|
1034
|
+
await sleep(TXS_POLLING_MS);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
730
1037
|
private getSlotStartBuildTimestamp(): number {
|
|
731
1038
|
return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
|
|
732
1039
|
}
|