@aztec/sequencer-client 4.0.0-nightly.20250907 → 4.0.0-nightly.20260108
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/index.d.ts +1 -1
- package/dest/client/sequencer-client.d.ts +10 -8
- package/dest/client/sequencer-client.d.ts.map +1 -1
- package/dest/client/sequencer-client.js +40 -28
- package/dest/config.d.ts +13 -5
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +82 -25
- package/dest/global_variable_builder/global_builder.d.ts +22 -16
- package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
- package/dest/global_variable_builder/global_builder.js +52 -39
- package/dest/global_variable_builder/index.d.ts +1 -1
- 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 +11 -8
- package/dest/publisher/config.d.ts.map +1 -1
- package/dest/publisher/config.js +21 -13
- package/dest/publisher/index.d.ts +2 -2
- package/dest/publisher/index.d.ts.map +1 -1
- package/dest/publisher/index.js +1 -1
- package/dest/publisher/sequencer-publisher-factory.d.ts +11 -5
- package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-factory.js +9 -2
- package/dest/publisher/sequencer-publisher-metrics.d.ts +4 -4
- package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-metrics.js +1 -1
- package/dest/publisher/sequencer-publisher.d.ts +78 -70
- package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher.js +687 -182
- package/dest/sequencer/block_builder.d.ts +6 -10
- package/dest/sequencer/block_builder.d.ts.map +1 -1
- package/dest/sequencer/block_builder.js +21 -10
- package/dest/sequencer/checkpoint_builder.d.ts +63 -0
- package/dest/sequencer/checkpoint_builder.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_builder.js +131 -0
- package/dest/sequencer/checkpoint_proposal_job.d.ts +76 -0
- package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_proposal_job.js +1070 -0
- package/dest/sequencer/checkpoint_voter.d.ts +34 -0
- package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_voter.js +85 -0
- package/dest/sequencer/config.d.ts +3 -2
- package/dest/sequencer/config.d.ts.map +1 -1
- package/dest/sequencer/errors.d.ts +11 -0
- package/dest/sequencer/errors.d.ts.map +1 -0
- package/dest/sequencer/errors.js +15 -0
- package/dest/sequencer/events.d.ts +46 -0
- package/dest/sequencer/events.d.ts.map +1 -0
- package/dest/sequencer/events.js +1 -0
- package/dest/sequencer/index.d.ts +5 -1
- package/dest/sequencer/index.d.ts.map +1 -1
- package/dest/sequencer/index.js +4 -0
- package/dest/sequencer/metrics.d.ts +37 -20
- package/dest/sequencer/metrics.d.ts.map +1 -1
- package/dest/sequencer/metrics.js +211 -85
- package/dest/sequencer/sequencer.d.ts +110 -121
- package/dest/sequencer/sequencer.d.ts.map +1 -1
- package/dest/sequencer/sequencer.js +809 -524
- package/dest/sequencer/timetable.d.ts +57 -21
- package/dest/sequencer/timetable.d.ts.map +1 -1
- package/dest/sequencer/timetable.js +150 -68
- package/dest/sequencer/types.d.ts +3 -0
- package/dest/sequencer/types.d.ts.map +1 -0
- package/dest/sequencer/types.js +1 -0
- package/dest/sequencer/utils.d.ts +20 -28
- package/dest/sequencer/utils.d.ts.map +1 -1
- package/dest/sequencer/utils.js +12 -24
- package/dest/test/index.d.ts +4 -2
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/mock_checkpoint_builder.d.ts +83 -0
- package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
- package/dest/test/mock_checkpoint_builder.js +179 -0
- package/dest/test/utils.d.ts +49 -0
- package/dest/test/utils.d.ts.map +1 -0
- package/dest/test/utils.js +94 -0
- package/dest/tx_validator/nullifier_cache.d.ts +1 -1
- package/dest/tx_validator/nullifier_cache.d.ts.map +1 -1
- package/dest/tx_validator/tx_validator_factory.d.ts +4 -3
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -1
- package/dest/tx_validator/tx_validator_factory.js +12 -9
- package/package.json +32 -31
- package/src/client/sequencer-client.ts +34 -40
- package/src/config.ts +89 -29
- package/src/global_variable_builder/global_builder.ts +67 -59
- package/src/index.ts +2 -0
- package/src/publisher/config.ts +32 -19
- package/src/publisher/index.ts +1 -1
- package/src/publisher/sequencer-publisher-factory.ts +19 -6
- package/src/publisher/sequencer-publisher-metrics.ts +3 -3
- package/src/publisher/sequencer-publisher.ts +418 -242
- package/src/sequencer/README.md +531 -0
- package/src/sequencer/block_builder.ts +28 -30
- package/src/sequencer/checkpoint_builder.ts +217 -0
- package/src/sequencer/checkpoint_proposal_job.ts +722 -0
- package/src/sequencer/checkpoint_voter.ts +105 -0
- package/src/sequencer/config.ts +2 -1
- package/src/sequencer/errors.ts +21 -0
- package/src/sequencer/events.ts +27 -0
- package/src/sequencer/index.ts +4 -0
- package/src/sequencer/metrics.ts +269 -94
- package/src/sequencer/sequencer.ts +508 -675
- package/src/sequencer/timetable.ts +181 -91
- package/src/sequencer/types.ts +6 -0
- package/src/sequencer/utils.ts +24 -29
- package/src/test/index.ts +3 -1
- package/src/test/mock_checkpoint_builder.ts +247 -0
- package/src/test/utils.ts +137 -0
- package/src/tx_validator/tx_validator_factory.ts +13 -7
|
@@ -1,540 +1,426 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getKzg } from '@aztec/blob-lib';
|
|
2
2
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
3
3
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { NoCommitteeError, type RollupContract } from '@aztec/ethereum/contracts';
|
|
5
|
+
import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
6
|
+
import { merge, omit, pick } from '@aztec/foundation/collection';
|
|
7
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
8
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
7
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
8
9
|
import { createLogger } from '@aztec/foundation/log';
|
|
9
10
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
10
|
-
import {
|
|
11
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
11
12
|
import type { TypedEventEmitter } from '@aztec/foundation/types';
|
|
12
13
|
import type { P2P } from '@aztec/p2p';
|
|
13
14
|
import type { SlasherClientInterface } from '@aztec/slasher';
|
|
14
|
-
import type {
|
|
15
|
-
import
|
|
16
|
-
import {
|
|
15
|
+
import type { L2BlockNew, L2BlockSource, ValidateBlockResult } from '@aztec/stdlib/block';
|
|
16
|
+
import type { Checkpoint } from '@aztec/stdlib/checkpoint';
|
|
17
|
+
import { getSlotAtTimestamp, getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
|
|
17
18
|
import {
|
|
18
|
-
type
|
|
19
|
-
type
|
|
19
|
+
type ResolvedSequencerConfig,
|
|
20
|
+
type SequencerConfig,
|
|
20
21
|
SequencerConfigSchema,
|
|
21
22
|
type WorldStateSynchronizer,
|
|
22
23
|
} from '@aztec/stdlib/interfaces/server';
|
|
23
24
|
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
24
|
-
import type { BlockProposalOptions } from '@aztec/stdlib/p2p';
|
|
25
|
-
import { orderAttestations } from '@aztec/stdlib/p2p';
|
|
26
25
|
import { pickFromSchema } from '@aztec/stdlib/schemas';
|
|
27
|
-
import type { L2BlockBuiltStats } from '@aztec/stdlib/stats';
|
|
28
26
|
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
29
|
-
import {
|
|
30
|
-
ContentCommitment,
|
|
31
|
-
type FailedTx,
|
|
32
|
-
GlobalVariables,
|
|
33
|
-
ProposedBlockHeader,
|
|
34
|
-
Tx,
|
|
35
|
-
type TxHash,
|
|
36
|
-
} from '@aztec/stdlib/tx';
|
|
37
|
-
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
38
27
|
import { Attributes, type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
39
28
|
import type { ValidatorClient } from '@aztec/validator-client';
|
|
40
29
|
|
|
41
30
|
import EventEmitter from 'node:events';
|
|
42
|
-
import type { TypedDataDefinition } from 'viem';
|
|
43
31
|
|
|
32
|
+
import { DefaultSequencerConfig } from '../config.js';
|
|
44
33
|
import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
|
|
45
34
|
import type { SequencerPublisherFactory } from '../publisher/sequencer-publisher-factory.js';
|
|
46
|
-
import type {
|
|
47
|
-
import
|
|
35
|
+
import type { InvalidateBlockRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
|
|
36
|
+
import { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
37
|
+
import { CheckpointProposalJob } from './checkpoint_proposal_job.js';
|
|
38
|
+
import { CheckpointVoter } from './checkpoint_voter.js';
|
|
39
|
+
import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
|
|
40
|
+
import type { SequencerEvents } from './events.js';
|
|
48
41
|
import { SequencerMetrics } from './metrics.js';
|
|
49
|
-
import { SequencerTimetable
|
|
50
|
-
import {
|
|
42
|
+
import { SequencerTimetable } from './timetable.js';
|
|
43
|
+
import type { SequencerRollupConstants } from './types.js';
|
|
44
|
+
import { SequencerState } from './utils.js';
|
|
51
45
|
|
|
52
46
|
export { SequencerState };
|
|
53
47
|
|
|
54
|
-
type SequencerRollupConstants = Pick<L1RollupConstants, 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration'>;
|
|
55
|
-
|
|
56
|
-
export type SequencerEvents = {
|
|
57
|
-
['state-changed']: (args: {
|
|
58
|
-
oldState: SequencerState;
|
|
59
|
-
newState: SequencerState;
|
|
60
|
-
secondsIntoSlot?: number;
|
|
61
|
-
slotNumber?: bigint;
|
|
62
|
-
}) => void;
|
|
63
|
-
['proposer-rollup-check-failed']: (args: { reason: string }) => void;
|
|
64
|
-
['tx-count-check-failed']: (args: { minTxs: number; availableTxs: number }) => void;
|
|
65
|
-
['block-build-failed']: (args: { reason: string }) => void;
|
|
66
|
-
['block-publish-failed']: (args: {
|
|
67
|
-
successfulActions?: Action[];
|
|
68
|
-
failedActions?: Action[];
|
|
69
|
-
sentActions?: Action[];
|
|
70
|
-
expiredActions?: Action[];
|
|
71
|
-
}) => void;
|
|
72
|
-
['block-published']: (args: { blockNumber: number; slot: number }) => void;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
48
|
/**
|
|
76
49
|
* Sequencer client
|
|
77
|
-
* -
|
|
78
|
-
* -
|
|
79
|
-
* -
|
|
80
|
-
* -
|
|
81
|
-
* -
|
|
82
|
-
* - Publishes L1 tx(s) to the rollup contract via RollupPublisher.
|
|
50
|
+
* - Checks whether it is elected as proposer for the next slot
|
|
51
|
+
* - Builds multiple blocks and broadcasts them
|
|
52
|
+
* - Collects attestations for the checkpoint
|
|
53
|
+
* - Publishes the checkpoint to L1
|
|
54
|
+
* - Votes for proposals and slashes on L1
|
|
83
55
|
*/
|
|
84
56
|
export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<SequencerEvents>) {
|
|
85
57
|
private runningPromise?: RunningPromise;
|
|
86
|
-
private pollingIntervalMs: number = 1000;
|
|
87
|
-
private maxTxsPerBlock = 32;
|
|
88
|
-
private minTxsPerBlock = 1;
|
|
89
|
-
private maxL1TxInclusionTimeIntoSlot = 0;
|
|
90
58
|
private state = SequencerState.STOPPED;
|
|
91
|
-
private maxBlockSizeInBytes: number = 1024 * 1024;
|
|
92
|
-
private maxBlockGas: Gas = new Gas(100e9, 100e9);
|
|
93
59
|
private metrics: SequencerMetrics;
|
|
94
60
|
|
|
95
|
-
|
|
61
|
+
/** The last slot for which we attempted to vote when sync failed, to prevent duplicate attempts. */
|
|
62
|
+
private lastSlotForVoteWhenSyncFailed: SlotNumber | undefined;
|
|
96
63
|
|
|
97
|
-
|
|
64
|
+
/** The last slot for which we triggered a checkpoint proposal job, to prevent duplicate attempts. */
|
|
65
|
+
private lastSlotForCheckpointProposalJob: SlotNumber | undefined;
|
|
66
|
+
|
|
67
|
+
/** Last successful checkpoint proposed */
|
|
68
|
+
private lastCheckpointProposed: Checkpoint | undefined;
|
|
69
|
+
|
|
70
|
+
/** The last epoch for which we logged strategy comparison in fisherman mode. */
|
|
71
|
+
private lastEpochForStrategyComparison: EpochNumber | undefined;
|
|
98
72
|
|
|
99
73
|
/** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */
|
|
100
74
|
protected timetable!: SequencerTimetable;
|
|
101
|
-
protected enforceTimeTable: boolean = false;
|
|
102
75
|
|
|
103
76
|
// This shouldn't be here as this gets re-created each time we build/propose a block.
|
|
104
77
|
// But we have a number of tests that abuse/rely on this class having a permanent publisher.
|
|
105
78
|
// As long as those tests only configure a single publisher they will continue to work.
|
|
106
79
|
// This will get re-assigned every time the sequencer goes to build a new block to a publisher that is valid
|
|
107
80
|
// for the block proposer.
|
|
81
|
+
// TODO(palla/mbps): Remove this field and fix tests
|
|
108
82
|
protected publisher: SequencerPublisher | undefined;
|
|
109
83
|
|
|
84
|
+
/** Config for the sequencer */
|
|
85
|
+
protected config: ResolvedSequencerConfig = DefaultSequencerConfig;
|
|
86
|
+
|
|
110
87
|
constructor(
|
|
111
88
|
protected publisherFactory: SequencerPublisherFactory,
|
|
112
|
-
protected validatorClient: ValidatorClient
|
|
89
|
+
protected validatorClient: ValidatorClient,
|
|
113
90
|
protected globalsBuilder: GlobalVariableBuilder,
|
|
114
91
|
protected p2pClient: P2P,
|
|
115
92
|
protected worldState: WorldStateSynchronizer,
|
|
116
93
|
protected slasherClient: SlasherClientInterface | undefined,
|
|
117
94
|
protected l2BlockSource: L2BlockSource,
|
|
118
95
|
protected l1ToL2MessageSource: L1ToL2MessageSource,
|
|
119
|
-
protected
|
|
96
|
+
protected checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
120
97
|
protected l1Constants: SequencerRollupConstants,
|
|
121
98
|
protected dateProvider: DateProvider,
|
|
122
99
|
protected epochCache: EpochCache,
|
|
123
100
|
protected rollupContract: RollupContract,
|
|
124
|
-
|
|
101
|
+
config: SequencerConfig,
|
|
125
102
|
protected telemetry: TelemetryClient = getTelemetryClient(),
|
|
126
103
|
protected log = createLogger('sequencer'),
|
|
127
104
|
) {
|
|
128
105
|
super();
|
|
129
106
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
validatorAddresses.length === 0
|
|
134
|
-
? EthAddress.ZERO
|
|
135
|
-
: (this.validatorClient?.getCoinbaseForAttestor(validatorAddresses[0]) ?? EthAddress.ZERO);
|
|
136
|
-
|
|
137
|
-
this.metrics = new SequencerMetrics(telemetry, () => this.state, coinbase, this.rollupContract, 'Sequencer');
|
|
138
|
-
|
|
139
|
-
// Initialize config
|
|
140
|
-
this.updateConfig(this.config);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
get tracer(): Tracer {
|
|
144
|
-
return this.metrics.tracer;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public getValidatorAddresses() {
|
|
148
|
-
return this.validatorClient?.getValidatorAddresses();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
public getConfig() {
|
|
152
|
-
return this.config;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Updates sequencer config by the defined values in the config on input.
|
|
157
|
-
* @param config - New parameters.
|
|
158
|
-
*/
|
|
159
|
-
public updateConfig(config: SequencerConfig) {
|
|
160
|
-
this.log.info(
|
|
161
|
-
`Sequencer config set`,
|
|
162
|
-
omit(pickFromSchema(config, SequencerConfigSchema), 'txPublicSetupAllowList'),
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
if (config.transactionPollingIntervalMS !== undefined) {
|
|
166
|
-
this.pollingIntervalMs = config.transactionPollingIntervalMS;
|
|
167
|
-
}
|
|
168
|
-
if (config.maxTxsPerBlock !== undefined) {
|
|
169
|
-
this.maxTxsPerBlock = config.maxTxsPerBlock;
|
|
170
|
-
}
|
|
171
|
-
if (config.minTxsPerBlock !== undefined) {
|
|
172
|
-
this.minTxsPerBlock = config.minTxsPerBlock;
|
|
173
|
-
}
|
|
174
|
-
if (config.maxDABlockGas !== undefined) {
|
|
175
|
-
this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
|
|
176
|
-
}
|
|
177
|
-
if (config.maxL2BlockGas !== undefined) {
|
|
178
|
-
this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
|
|
179
|
-
}
|
|
180
|
-
if (config.maxBlockSizeInBytes !== undefined) {
|
|
181
|
-
this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
|
|
107
|
+
// Add [FISHERMAN] prefix to logger if in fisherman mode
|
|
108
|
+
if (config.fishermanMode) {
|
|
109
|
+
this.log = log.createChild('[FISHERMAN]');
|
|
182
110
|
}
|
|
183
|
-
if (config.governanceProposerPayload) {
|
|
184
|
-
this.governanceProposerPayload = config.governanceProposerPayload;
|
|
185
|
-
}
|
|
186
|
-
if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
|
|
187
|
-
this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
|
|
188
|
-
}
|
|
189
|
-
if (config.enforceTimeTable !== undefined) {
|
|
190
|
-
this.enforceTimeTable = config.enforceTimeTable;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
this.setTimeTable();
|
|
194
|
-
|
|
195
|
-
// TODO: Just read everything from the config object as needed instead of copying everything into local vars.
|
|
196
111
|
|
|
197
|
-
|
|
198
|
-
|
|
112
|
+
this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
|
|
113
|
+
this.updateConfig(config);
|
|
199
114
|
}
|
|
200
115
|
|
|
201
|
-
|
|
116
|
+
/** Updates sequencer config by the defined values and updates the timetable */
|
|
117
|
+
public updateConfig(config: Partial<SequencerConfig>) {
|
|
118
|
+
const filteredConfig = pickFromSchema(config, SequencerConfigSchema);
|
|
119
|
+
this.log.info(`Updated sequencer config`, omit(filteredConfig, 'txPublicSetupAllowList'));
|
|
120
|
+
this.config = merge(this.config, filteredConfig);
|
|
202
121
|
this.timetable = new SequencerTimetable(
|
|
203
122
|
{
|
|
204
123
|
ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
|
|
205
124
|
aztecSlotDuration: this.aztecSlotDuration,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
125
|
+
l1PublishingTime: this.l1PublishingTime,
|
|
126
|
+
p2pPropagationTime: this.config.attestationPropagationTime,
|
|
127
|
+
blockDurationMs: this.config.blockDurationMs,
|
|
128
|
+
enforce: this.config.enforceTimeTable,
|
|
209
129
|
},
|
|
210
130
|
this.metrics,
|
|
211
131
|
this.log,
|
|
212
132
|
);
|
|
213
133
|
}
|
|
214
134
|
|
|
135
|
+
/** Initializes the sequencer (precomputes tables and creates a publisher). Takes about 3s. */
|
|
215
136
|
public async init() {
|
|
137
|
+
getKzg();
|
|
216
138
|
this.publisher = (await this.publisherFactory.create(undefined)).publisher;
|
|
217
139
|
}
|
|
218
140
|
|
|
219
|
-
/**
|
|
220
|
-
* Starts the sequencer and moves to IDLE state.
|
|
221
|
-
*/
|
|
141
|
+
/** Starts the sequencer and moves to IDLE state. */
|
|
222
142
|
public start() {
|
|
223
|
-
this.
|
|
224
|
-
|
|
143
|
+
this.runningPromise = new RunningPromise(
|
|
144
|
+
this.safeWork.bind(this),
|
|
145
|
+
this.log,
|
|
146
|
+
this.config.sequencerPollingIntervalMS,
|
|
147
|
+
);
|
|
225
148
|
this.setState(SequencerState.IDLE, undefined, { force: true });
|
|
226
149
|
this.runningPromise.start();
|
|
227
150
|
this.log.info('Started sequencer');
|
|
228
151
|
}
|
|
229
152
|
|
|
230
|
-
/**
|
|
231
|
-
* Stops the sequencer from processing txs and moves to STOPPED state.
|
|
232
|
-
*/
|
|
153
|
+
/** Stops the sequencer from building blocks and moves to STOPPED state. */
|
|
233
154
|
public async stop(): Promise<void> {
|
|
234
155
|
this.log.info(`Stopping sequencer`);
|
|
235
|
-
this.
|
|
156
|
+
this.setState(SequencerState.STOPPING, undefined, { force: true });
|
|
236
157
|
this.publisher?.interrupt();
|
|
237
|
-
await this.validatorClient?.stop();
|
|
238
158
|
await this.runningPromise?.stop();
|
|
239
159
|
this.setState(SequencerState.STOPPED, undefined, { force: true });
|
|
240
160
|
this.log.info('Stopped sequencer');
|
|
241
161
|
}
|
|
242
162
|
|
|
243
|
-
/**
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
163
|
+
/** Main sequencer loop with a try/catch */
|
|
164
|
+
protected async safeWork() {
|
|
165
|
+
try {
|
|
166
|
+
await this.work();
|
|
167
|
+
} catch (err) {
|
|
168
|
+
this.emit('checkpoint-error', { error: err as Error });
|
|
169
|
+
if (err instanceof SequencerTooSlowError) {
|
|
170
|
+
// TODO(palla/mbps): Add missing states
|
|
171
|
+
// Log as warn only if we had to abort halfway through the block proposal
|
|
172
|
+
const logLvl = [SequencerState.INITIALIZING_CHECKPOINT, SequencerState.PROPOSER_CHECK].includes(
|
|
173
|
+
err.proposedState,
|
|
174
|
+
)
|
|
175
|
+
? ('debug' as const)
|
|
176
|
+
: ('warn' as const);
|
|
177
|
+
this.log[logLvl](err.message, { now: this.dateProvider.nowInSeconds() });
|
|
178
|
+
} else {
|
|
179
|
+
// Re-throw other errors
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
} finally {
|
|
183
|
+
this.setState(SequencerState.IDLE, undefined);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Returns the current state of the sequencer. */
|
|
247
188
|
public status() {
|
|
248
189
|
return { state: this.state };
|
|
249
190
|
}
|
|
250
191
|
|
|
251
192
|
/**
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
193
|
+
* Main sequencer loop:
|
|
194
|
+
* - Checks if we are up to date
|
|
195
|
+
* - If we are and we are the sequencer, collect txs and build blocks
|
|
196
|
+
* - Build multiple blocks per slot when configured
|
|
197
|
+
* - Collect attestations for the final block
|
|
198
|
+
* - Submit checkpoint
|
|
258
199
|
*/
|
|
259
|
-
|
|
200
|
+
@trackSpan('Sequencer.work')
|
|
201
|
+
protected async work() {
|
|
260
202
|
this.setState(SequencerState.SYNCHRONIZING, undefined);
|
|
203
|
+
const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
|
|
261
204
|
|
|
262
|
-
// Check
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
// Do not go forward with new block if the previous one has not been mined and processed
|
|
266
|
-
if (!syncedTo) {
|
|
205
|
+
// Check if we are synced and it's our slot, grab a publisher, check previous block invalidation, etc
|
|
206
|
+
const checkpointProposalJob = await this.prepareCheckpointProposal(slot, ts, now);
|
|
207
|
+
if (!checkpointProposalJob) {
|
|
267
208
|
return;
|
|
268
209
|
}
|
|
269
210
|
|
|
270
|
-
|
|
271
|
-
const
|
|
211
|
+
// Execute the checkpoint proposal job
|
|
212
|
+
const checkpoint = await checkpointProposalJob.execute();
|
|
272
213
|
|
|
273
|
-
|
|
214
|
+
// Update last checkpoint proposed (currently unused)
|
|
215
|
+
if (checkpoint) {
|
|
216
|
+
this.lastCheckpointProposed = checkpoint;
|
|
217
|
+
}
|
|
274
218
|
|
|
275
|
-
|
|
219
|
+
// Log fee strategy comparison if on fisherman
|
|
220
|
+
if (
|
|
221
|
+
this.config.fishermanMode &&
|
|
222
|
+
(this.lastEpochForStrategyComparison === undefined || epoch > this.lastEpochForStrategyComparison)
|
|
223
|
+
) {
|
|
224
|
+
this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
|
|
225
|
+
this.lastEpochForStrategyComparison = epoch;
|
|
226
|
+
}
|
|
276
227
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
228
|
+
return checkpoint;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Prepares the checkpoint proposal by performing all necessary checks and setup.
|
|
233
|
+
* This is the initial step in the main loop.
|
|
234
|
+
* @returns CheckpointProposalJob if successful, undefined if we are not yet synced or are not the proposer.
|
|
235
|
+
*/
|
|
236
|
+
@trackSpan('Sequencer.prepareCheckpointProposal')
|
|
237
|
+
private async prepareCheckpointProposal(
|
|
238
|
+
slot: SlotNumber,
|
|
239
|
+
ts: bigint,
|
|
240
|
+
now: bigint,
|
|
241
|
+
): Promise<CheckpointProposalJob | undefined> {
|
|
242
|
+
// Check we have not already processed this slot (cheapest check)
|
|
243
|
+
// We only check this if enforce timetable is set, since we want to keep processing the same slot if we are not
|
|
244
|
+
// running against actual time (eg when we use sandbox-style automining)
|
|
245
|
+
if (
|
|
246
|
+
this.lastSlotForCheckpointProposalJob &&
|
|
247
|
+
this.lastSlotForCheckpointProposalJob >= slot &&
|
|
248
|
+
this.config.enforceTimeTable
|
|
249
|
+
) {
|
|
250
|
+
this.log.trace(`Slot ${slot} has already been processed`);
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// But if we have already proposed for this slot, the we definitely have to skip it, automining or not
|
|
255
|
+
if (this.lastCheckpointProposed && this.lastCheckpointProposed.header.slotNumber >= slot) {
|
|
256
|
+
this.log.trace(`Slot ${slot} has already been published as checkpoint ${this.lastCheckpointProposed.number}`);
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Check all components are synced to latest as seen by the archiver (queries all subsystems)
|
|
261
|
+
const syncedTo = await this.checkSync({ ts, slot });
|
|
262
|
+
if (!syncedTo) {
|
|
263
|
+
await this.tryVoteWhenSyncFails({ slot, ts });
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// TODO(palla/mbps): Compute proper checkpoint number
|
|
268
|
+
const checkpointNumber = CheckpointNumber.fromBlockNumber(BlockNumber(syncedTo.blockNumber + 1));
|
|
269
|
+
|
|
270
|
+
const logCtx = {
|
|
281
271
|
now,
|
|
282
272
|
syncedToL1Ts: syncedTo.l1Timestamp,
|
|
283
273
|
syncedToL2Slot: getSlotAtTimestamp(syncedTo.l1Timestamp, this.l1Constants),
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
newBlockNumber,
|
|
274
|
+
slot,
|
|
275
|
+
slotTs: ts,
|
|
276
|
+
checkpointNumber,
|
|
288
277
|
isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex'),
|
|
289
278
|
};
|
|
290
279
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
280
|
+
// Check that we are a proposer for the next slot
|
|
281
|
+
this.setState(SequencerState.PROPOSER_CHECK, slot);
|
|
282
|
+
const [canPropose, proposer] = await this.checkCanPropose(slot);
|
|
283
|
+
|
|
284
|
+
// If we are not a proposer check if we should invalidate a invalid block, and bail
|
|
285
|
+
if (!canPropose) {
|
|
286
|
+
await this.considerInvalidatingBlock(syncedTo, slot);
|
|
287
|
+
return undefined;
|
|
297
288
|
}
|
|
298
289
|
|
|
299
|
-
// Check that the slot is not taken by a block already
|
|
290
|
+
// Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
|
|
300
291
|
if (syncedTo.block && syncedTo.block.header.getSlot() >= slot) {
|
|
301
|
-
this.log.
|
|
292
|
+
this.log.warn(
|
|
302
293
|
`Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`,
|
|
303
|
-
{ ...
|
|
304
|
-
);
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Or that we haven't published it ourselves
|
|
309
|
-
if (this.lastBlockPublished && this.lastBlockPublished.header.getSlot() >= slot) {
|
|
310
|
-
this.log.debug(
|
|
311
|
-
`Cannot propose block at next L2 slot ${slot} since that slot was taken by our own block ${this.lastBlockPublished.number}`,
|
|
312
|
-
{ ...syncLogData, block: this.lastBlockPublished.header.toInspect() },
|
|
294
|
+
{ ...logCtx, block: syncedTo.block.header.toInspect() },
|
|
313
295
|
);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
// Check that we are a proposer for the next slot
|
|
318
|
-
let proposerInNextSlot: EthAddress | undefined;
|
|
319
|
-
try {
|
|
320
|
-
proposerInNextSlot = await this.epochCache.getProposerAttesterAddressInNextSlot();
|
|
321
|
-
} catch (e) {
|
|
322
|
-
if (e instanceof NoCommitteeError) {
|
|
323
|
-
this.log.warn(
|
|
324
|
-
`Cannot propose block ${newBlockNumber} at next L2 slot ${slot} since the committee does not exist on L1`,
|
|
325
|
-
);
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// If get proposer in next slot is undefined, then the committee is empty and anyone may propose.
|
|
331
|
-
// If the committee is defined and not empty, but none of our validators are the proposer, then stop.
|
|
332
|
-
const validatorAddresses = this.validatorClient!.getValidatorAddresses();
|
|
333
|
-
if (proposerInNextSlot !== undefined && !validatorAddresses.some(addr => addr.equals(proposerInNextSlot))) {
|
|
334
|
-
this.log.debug(`Cannot propose block ${newBlockNumber} since we are not a proposer`, {
|
|
335
|
-
us: validatorAddresses,
|
|
336
|
-
proposer: proposerInNextSlot,
|
|
337
|
-
...syncLogData,
|
|
338
|
-
});
|
|
339
|
-
// If the pending chain is invalid, we may need to invalidate the block if no one else is doing it.
|
|
340
|
-
if (!syncedTo.pendingChainValidationStatus.valid) {
|
|
341
|
-
// We pass i undefined here to get any available publisher.
|
|
342
|
-
const { publisher } = await this.publisherFactory.create(undefined);
|
|
343
|
-
await this.considerInvalidatingBlock(syncedTo, slot, validatorAddresses, publisher);
|
|
344
|
-
}
|
|
345
|
-
return;
|
|
296
|
+
this.metrics.recordBlockProposalPrecheckFailed('slot_already_taken');
|
|
297
|
+
return undefined;
|
|
346
298
|
}
|
|
347
299
|
|
|
348
|
-
// Check with the rollup if we can indeed propose at the next L2 slot. This check should not fail
|
|
349
|
-
// if all the previous checks are good, but we do it just in case.
|
|
350
|
-
const proposerAddressInNextSlot = proposerInNextSlot ?? EthAddress.ZERO;
|
|
351
|
-
|
|
352
300
|
// We now need to get ourselves a publisher.
|
|
353
301
|
// The returned attestor will be the one we provided if we provided one.
|
|
354
302
|
// Otherwise it will be a valid attestor for the returned publisher.
|
|
355
|
-
|
|
356
|
-
|
|
303
|
+
// In fisherman mode, pass undefined to use the fisherman's own keystore instead of the actual proposer's
|
|
304
|
+
const proposerForPublisher = this.config.fishermanMode ? undefined : proposer;
|
|
305
|
+
const { attestorAddress, publisher } = await this.publisherFactory.create(proposerForPublisher);
|
|
357
306
|
this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
|
|
358
|
-
|
|
359
307
|
this.publisher = publisher;
|
|
360
308
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
309
|
+
// In fisherman mode, set the actual proposer's address for simulations
|
|
310
|
+
if (this.config.fishermanMode && proposer) {
|
|
311
|
+
publisher.setProposerAddressForSimulation(proposer);
|
|
312
|
+
this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
|
|
313
|
+
}
|
|
365
314
|
|
|
366
315
|
// Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
|
|
316
|
+
// TODO(palla/mbps): We need to invalidate checkpoints, not blocks
|
|
367
317
|
const invalidateBlock = await publisher.simulateInvalidateBlock(syncedTo.pendingChainValidationStatus);
|
|
318
|
+
|
|
319
|
+
// Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
|
|
320
|
+
// if all the previous checks are good, but we do it just in case.
|
|
368
321
|
const canProposeCheck = await publisher.canProposeAtNextEthBlock(
|
|
369
|
-
|
|
370
|
-
|
|
322
|
+
syncedTo.archive,
|
|
323
|
+
proposer ?? EthAddress.ZERO,
|
|
371
324
|
invalidateBlock,
|
|
372
325
|
);
|
|
373
326
|
|
|
374
327
|
if (canProposeCheck === undefined) {
|
|
375
328
|
this.log.warn(
|
|
376
|
-
`Cannot propose
|
|
377
|
-
|
|
329
|
+
`Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`,
|
|
330
|
+
logCtx,
|
|
378
331
|
);
|
|
379
|
-
this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed' });
|
|
380
|
-
|
|
381
|
-
|
|
332
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed', slot });
|
|
333
|
+
this.metrics.recordBlockProposalPrecheckFailed('rollup_contract_check_failed');
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (canProposeCheck.slot !== slot) {
|
|
382
338
|
this.log.warn(
|
|
383
339
|
`Cannot propose block due to slot mismatch with rollup contract (this can be caused by a clock out of sync). Expected slot ${slot} but got ${canProposeCheck.slot}.`,
|
|
384
|
-
{ ...
|
|
340
|
+
{ ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
|
|
385
341
|
);
|
|
386
|
-
this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch' });
|
|
387
|
-
|
|
388
|
-
|
|
342
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch', slot });
|
|
343
|
+
this.metrics.recordBlockProposalPrecheckFailed('slot_mismatch');
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (canProposeCheck.checkpointNumber !== checkpointNumber) {
|
|
389
348
|
this.log.warn(
|
|
390
|
-
`Cannot propose
|
|
391
|
-
{ ...
|
|
349
|
+
`Cannot propose due to block mismatch with rollup contract (this can be caused by a pending archiver sync). Expected checkpoint ${checkpointNumber} but got ${canProposeCheck.checkpointNumber}.`,
|
|
350
|
+
{ ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
|
|
392
351
|
);
|
|
393
|
-
this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch' });
|
|
394
|
-
|
|
352
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch', slot });
|
|
353
|
+
this.metrics.recordBlockProposalPrecheckFailed('block_number_mismatch');
|
|
354
|
+
return undefined;
|
|
395
355
|
}
|
|
396
356
|
|
|
397
|
-
this.
|
|
398
|
-
|
|
399
|
-
{ ...syncLogData, validatorAddresses },
|
|
400
|
-
);
|
|
357
|
+
this.lastSlotForCheckpointProposalJob = slot;
|
|
358
|
+
this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, { ...logCtx, proposer });
|
|
401
359
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
coinbase,
|
|
405
|
-
feeRecipient,
|
|
360
|
+
// Create and return the checkpoint proposal job
|
|
361
|
+
return this.createCheckpointProposalJob(
|
|
406
362
|
slot,
|
|
363
|
+
checkpointNumber,
|
|
364
|
+
syncedTo.blockNumber,
|
|
365
|
+
proposer,
|
|
366
|
+
publisher,
|
|
367
|
+
attestorAddress,
|
|
368
|
+
invalidateBlock,
|
|
407
369
|
);
|
|
408
|
-
|
|
409
|
-
const { timestamp } = newGlobalVariables;
|
|
410
|
-
const signerFn = (msg: TypedDataDefinition) =>
|
|
411
|
-
this.validatorClient!.signWithAddress(attestorAddress, msg).then(s => s.toString());
|
|
412
|
-
|
|
413
|
-
const enqueueGovernanceSignalPromise =
|
|
414
|
-
this.governanceProposerPayload && !this.governanceProposerPayload.isZero()
|
|
415
|
-
? publisher
|
|
416
|
-
.enqueueGovernanceCastSignal(this.governanceProposerPayload, slot, timestamp, attestorAddress, signerFn)
|
|
417
|
-
.catch(err => {
|
|
418
|
-
this.log.error(`Error enqueuing governance vote`, err, { blockNumber: newBlockNumber, slot });
|
|
419
|
-
return false;
|
|
420
|
-
})
|
|
421
|
-
: Promise.resolve(false);
|
|
422
|
-
|
|
423
|
-
const enqueueSlashingActionsPromise = this.slasherClient
|
|
424
|
-
? this.slasherClient
|
|
425
|
-
.getProposerActions(slot)
|
|
426
|
-
.then(actions => publisher.enqueueSlashingActions(actions, slot, timestamp, attestorAddress, signerFn))
|
|
427
|
-
.catch(err => {
|
|
428
|
-
this.log.error(`Error enqueuing slashing actions`, err, { blockNumber: newBlockNumber, slot });
|
|
429
|
-
return false;
|
|
430
|
-
})
|
|
431
|
-
: Promise.resolve(false);
|
|
432
|
-
|
|
433
|
-
if (invalidateBlock && !this.config.skipInvalidateBlockAsProposer) {
|
|
434
|
-
publisher.enqueueInvalidateBlock(invalidateBlock);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
this.setState(SequencerState.INITIALIZING_PROPOSAL, slot);
|
|
438
|
-
this.log.verbose(`Preparing proposal for block ${newBlockNumber} at slot ${slot}`, {
|
|
439
|
-
proposer: proposerInNextSlot?.toString(),
|
|
440
|
-
coinbase,
|
|
441
|
-
publisher: publisher.getSenderAddress(),
|
|
442
|
-
feeRecipient,
|
|
443
|
-
globalVariables: newGlobalVariables.toInspect(),
|
|
444
|
-
chainTipArchive,
|
|
445
|
-
blockNumber: newBlockNumber,
|
|
446
|
-
slot,
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
// If I created a "partial" header here that should make our job much easier.
|
|
450
|
-
const proposalHeader = ProposedBlockHeader.from({
|
|
451
|
-
...newGlobalVariables,
|
|
452
|
-
timestamp: newGlobalVariables.timestamp,
|
|
453
|
-
lastArchiveRoot: chainTipArchive,
|
|
454
|
-
contentCommitment: ContentCommitment.empty(),
|
|
455
|
-
totalManaUsed: Fr.ZERO,
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
let block: L2Block | undefined;
|
|
459
|
-
|
|
460
|
-
const pendingTxCount = await this.p2pClient.getPendingTxCount();
|
|
461
|
-
if (pendingTxCount >= this.minTxsPerBlock) {
|
|
462
|
-
// We don't fetch exactly maxTxsPerBlock txs here because we may not need all of them if we hit a limit before,
|
|
463
|
-
// and also we may need to fetch more if we don't have enough valid txs.
|
|
464
|
-
const pendingTxs = this.p2pClient.iteratePendingTxs();
|
|
465
|
-
try {
|
|
466
|
-
block = await this.buildBlockAndEnqueuePublish(
|
|
467
|
-
pendingTxs,
|
|
468
|
-
proposalHeader,
|
|
469
|
-
newGlobalVariables,
|
|
470
|
-
proposerInNextSlot,
|
|
471
|
-
invalidateBlock,
|
|
472
|
-
publisher,
|
|
473
|
-
);
|
|
474
|
-
} catch (err: any) {
|
|
475
|
-
this.emit('block-build-failed', { reason: err.message });
|
|
476
|
-
if (err instanceof FormattedViemError) {
|
|
477
|
-
this.log.verbose(`Unable to build/enqueue block ${err.message}`);
|
|
478
|
-
} else {
|
|
479
|
-
this.log.error(`Error building/enqueuing block`, err, { blockNumber: newBlockNumber, slot });
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
} else {
|
|
483
|
-
this.log.verbose(
|
|
484
|
-
`Not enough txs to build block ${newBlockNumber} at slot ${slot} (got ${pendingTxCount} txs, need ${this.minTxsPerBlock})`,
|
|
485
|
-
{ chainTipArchive, blockNumber: newBlockNumber, slot },
|
|
486
|
-
);
|
|
487
|
-
this.emit('tx-count-check-failed', { minTxs: this.minTxsPerBlock, availableTxs: pendingTxCount });
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
await Promise.all([enqueueGovernanceSignalPromise, enqueueSlashingActionsPromise]);
|
|
491
|
-
|
|
492
|
-
const l1Response = await publisher.sendRequests();
|
|
493
|
-
const proposedBlock = l1Response?.successfulActions.find(a => a === 'propose');
|
|
494
|
-
if (proposedBlock) {
|
|
495
|
-
this.lastBlockPublished = block;
|
|
496
|
-
this.emit('block-published', { blockNumber: newBlockNumber, slot: Number(slot) });
|
|
497
|
-
this.metrics.incFilledSlot(publisher.getSenderAddress().toString());
|
|
498
|
-
} else if (block) {
|
|
499
|
-
this.emit('block-publish-failed', l1Response ?? {});
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
this.setState(SequencerState.IDLE, undefined);
|
|
503
370
|
}
|
|
504
371
|
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
372
|
+
protected createCheckpointProposalJob(
|
|
373
|
+
slot: SlotNumber,
|
|
374
|
+
checkpointNumber: CheckpointNumber,
|
|
375
|
+
syncedToBlockNumber: BlockNumber,
|
|
376
|
+
proposer: EthAddress | undefined,
|
|
377
|
+
publisher: SequencerPublisher,
|
|
378
|
+
attestorAddress: EthAddress,
|
|
379
|
+
invalidateBlock: InvalidateBlockRequest | undefined,
|
|
380
|
+
): CheckpointProposalJob {
|
|
381
|
+
return new CheckpointProposalJob(
|
|
382
|
+
slot,
|
|
383
|
+
checkpointNumber,
|
|
384
|
+
syncedToBlockNumber,
|
|
385
|
+
proposer,
|
|
386
|
+
publisher,
|
|
387
|
+
attestorAddress,
|
|
388
|
+
invalidateBlock,
|
|
389
|
+
this.validatorClient,
|
|
390
|
+
this.globalsBuilder,
|
|
391
|
+
this.p2pClient,
|
|
392
|
+
this.worldState,
|
|
393
|
+
this.l1ToL2MessageSource,
|
|
394
|
+
this.checkpointsBuilder,
|
|
395
|
+
this.l1Constants,
|
|
396
|
+
this.config,
|
|
397
|
+
this.timetable,
|
|
398
|
+
this.slasherClient,
|
|
399
|
+
this.epochCache,
|
|
400
|
+
this.dateProvider,
|
|
401
|
+
this.metrics,
|
|
402
|
+
this,
|
|
403
|
+
this.setState.bind(this),
|
|
404
|
+
this.log,
|
|
405
|
+
this.tracer,
|
|
406
|
+
);
|
|
523
407
|
}
|
|
524
408
|
|
|
525
409
|
/**
|
|
526
|
-
*
|
|
410
|
+
* Internal helper for setting the sequencer state and checks if we have enough time left in the slot to transition to the new state.
|
|
527
411
|
* @param proposedState - The new state to transition to.
|
|
528
412
|
* @param slotNumber - The current slot number.
|
|
529
413
|
* @param force - Whether to force the transition even if the sequencer is stopped.
|
|
530
414
|
*/
|
|
531
|
-
setState(
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
415
|
+
protected setState(
|
|
416
|
+
proposedState: SequencerState,
|
|
417
|
+
slotNumber: SlotNumber | undefined,
|
|
418
|
+
opts: { force?: boolean } = {},
|
|
419
|
+
): void {
|
|
420
|
+
if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
|
|
421
|
+
this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
|
|
422
|
+
throw new SequencerInterruptedError();
|
|
423
|
+
}
|
|
538
424
|
if (this.state === SequencerState.STOPPED && !opts.force) {
|
|
539
425
|
this.log.warn(`Cannot set sequencer from ${this.state} to ${proposedState} as it is stopped.`);
|
|
540
426
|
return;
|
|
@@ -545,326 +431,201 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
545
431
|
this.timetable.assertTimeLeft(proposedState, secondsIntoSlot);
|
|
546
432
|
}
|
|
547
433
|
|
|
548
|
-
|
|
434
|
+
const boringStates = [SequencerState.IDLE, SequencerState.SYNCHRONIZING];
|
|
435
|
+
const logLevel =
|
|
436
|
+
boringStates.includes(proposedState) && boringStates.includes(this.state)
|
|
437
|
+
? ('trace' as const)
|
|
438
|
+
: ('debug' as const);
|
|
439
|
+
this.log[logLevel](`Transitioning from ${this.state} to ${proposedState}`, { slotNumber, secondsIntoSlot });
|
|
440
|
+
|
|
549
441
|
this.emit('state-changed', {
|
|
550
442
|
oldState: this.state,
|
|
551
443
|
newState: proposedState,
|
|
552
444
|
secondsIntoSlot,
|
|
553
|
-
slotNumber,
|
|
445
|
+
slot: slotNumber,
|
|
554
446
|
});
|
|
555
447
|
this.state = proposedState;
|
|
556
448
|
}
|
|
557
449
|
|
|
558
|
-
private async dropFailedTxsFromP2P(failedTxs: FailedTx[]) {
|
|
559
|
-
if (failedTxs.length === 0) {
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
const failedTxData = failedTxs.map(fail => fail.tx);
|
|
563
|
-
const failedTxHashes = failedTxData.map(tx => tx.getTxHash());
|
|
564
|
-
this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
|
|
565
|
-
await this.p2pClient.deleteTxs(failedTxHashes);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
protected getBlockBuilderOptions(slot: number): PublicProcessorLimits {
|
|
569
|
-
// Deadline for processing depends on whether we're proposing a block
|
|
570
|
-
const secondsIntoSlot = this.getSecondsIntoSlot(slot);
|
|
571
|
-
const processingEndTimeWithinSlot = this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
|
|
572
|
-
|
|
573
|
-
// Deadline is only set if enforceTimeTable is enabled.
|
|
574
|
-
const deadline = this.enforceTimeTable
|
|
575
|
-
? new Date((this.getSlotStartBuildTimestamp(slot) + processingEndTimeWithinSlot) * 1000)
|
|
576
|
-
: undefined;
|
|
577
|
-
return {
|
|
578
|
-
maxTransactions: this.maxTxsPerBlock,
|
|
579
|
-
maxBlockSize: this.maxBlockSizeInBytes,
|
|
580
|
-
maxBlockGas: this.maxBlockGas,
|
|
581
|
-
deadline,
|
|
582
|
-
};
|
|
583
|
-
}
|
|
584
|
-
|
|
585
450
|
/**
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
* @dev MUST throw instead of exiting early to ensure that world-state
|
|
589
|
-
* is being rolled back if the block is dropped.
|
|
590
|
-
*
|
|
591
|
-
* @param pendingTxs - Iterable of pending transactions to construct the block from
|
|
592
|
-
* @param proposalHeader - The partial header constructed for the proposal
|
|
593
|
-
* @param newGlobalVariables - The global variables for the new block
|
|
594
|
-
* @param proposerAddress - The address of the proposer
|
|
451
|
+
* Returns whether all dependencies have caught up.
|
|
452
|
+
* We don't check against the previous block submitted since it may have been reorg'd out.
|
|
595
453
|
*/
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
const slot = proposalHeader.slotNumber.toBigInt();
|
|
611
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(blockNumber);
|
|
612
|
-
|
|
613
|
-
// this.metrics.recordNewBlock(blockNumber, validTxs.length);
|
|
614
|
-
const workTimer = new Timer();
|
|
615
|
-
this.setState(SequencerState.CREATING_BLOCK, slot);
|
|
616
|
-
|
|
617
|
-
try {
|
|
618
|
-
const blockBuilderOptions = this.getBlockBuilderOptions(Number(slot));
|
|
619
|
-
const buildBlockRes = await this.blockBuilder.buildBlock(
|
|
620
|
-
pendingTxs,
|
|
621
|
-
l1ToL2Messages,
|
|
622
|
-
newGlobalVariables,
|
|
623
|
-
blockBuilderOptions,
|
|
624
|
-
);
|
|
625
|
-
const { publicGas, block, publicProcessorDuration, numTxs, numMsgs, blockBuildingTimer, usedTxs, failedTxs } =
|
|
626
|
-
buildBlockRes;
|
|
627
|
-
const blockBuildDuration = workTimer.ms();
|
|
628
|
-
await this.dropFailedTxsFromP2P(failedTxs);
|
|
629
|
-
|
|
630
|
-
const minTxsPerBlock = this.minTxsPerBlock;
|
|
631
|
-
if (numTxs < minTxsPerBlock) {
|
|
632
|
-
this.log.warn(
|
|
633
|
-
`Block ${blockNumber} has too few txs to be proposed (got ${numTxs} but required ${minTxsPerBlock})`,
|
|
634
|
-
{ slot, blockNumber, numTxs },
|
|
635
|
-
);
|
|
636
|
-
throw new Error(`Block has too few successful txs to be proposed`);
|
|
637
|
-
}
|
|
454
|
+
protected async checkSync(args: { ts: bigint; slot: SlotNumber }): Promise<SequencerSyncCheckResult | undefined> {
|
|
455
|
+
// Check that the archiver and dependencies have synced to the previous L1 slot at least
|
|
456
|
+
// TODO(#14766): Archiver reports L1 timestamp based on L1 blocks seen, which means that a missed L1 block will
|
|
457
|
+
// cause the archiver L1 timestamp to fall behind, and cause this sequencer to start processing one L1 slot later.
|
|
458
|
+
const l1Timestamp = await this.l2BlockSource.getL1Timestamp();
|
|
459
|
+
const { slot, ts } = args;
|
|
460
|
+
if (l1Timestamp === undefined || l1Timestamp + BigInt(this.l1Constants.ethereumSlotDuration) < ts) {
|
|
461
|
+
this.log.debug(`Cannot propose block at next L2 slot ${slot} due to pending sync from L1`, {
|
|
462
|
+
slot,
|
|
463
|
+
ts,
|
|
464
|
+
l1Timestamp,
|
|
465
|
+
});
|
|
466
|
+
return undefined;
|
|
467
|
+
}
|
|
638
468
|
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
...block.getStats(),
|
|
650
|
-
};
|
|
651
|
-
|
|
652
|
-
const blockHash = await block.hash();
|
|
653
|
-
const txHashes = block.body.txEffects.map(tx => tx.txHash);
|
|
654
|
-
this.log.info(
|
|
655
|
-
`Built block ${block.number} for slot ${slot} with ${numTxs} txs and ${numMsgs} messages. ${
|
|
656
|
-
publicGas.l2Gas / workTimer.s()
|
|
657
|
-
} mana/s`,
|
|
658
|
-
{
|
|
659
|
-
blockHash,
|
|
660
|
-
globalVariables: block.header.globalVariables.toInspect(),
|
|
661
|
-
txHashes,
|
|
662
|
-
...blockStats,
|
|
663
|
-
},
|
|
664
|
-
);
|
|
469
|
+
const syncedBlocks = await Promise.all([
|
|
470
|
+
this.worldState.status().then(({ syncSummary }) => ({
|
|
471
|
+
number: syncSummary.latestBlockNumber,
|
|
472
|
+
hash: syncSummary.latestBlockHash,
|
|
473
|
+
})),
|
|
474
|
+
this.l2BlockSource.getL2Tips().then(t => t.latest),
|
|
475
|
+
this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
|
|
476
|
+
this.l1ToL2MessageSource.getL2Tips().then(t => t.latest),
|
|
477
|
+
this.l2BlockSource.getPendingChainValidationStatus(),
|
|
478
|
+
] as const);
|
|
665
479
|
|
|
666
|
-
|
|
667
|
-
const attestations = await this.collectAttestations(block, usedTxs, proposerAddress);
|
|
668
|
-
if (attestations !== undefined) {
|
|
669
|
-
this.log.verbose(`Collected ${attestations.length} attestations`, { blockHash, blockNumber });
|
|
670
|
-
}
|
|
480
|
+
const [worldState, l2BlockSource, p2p, l1ToL2MessageSource, pendingChainValidationStatus] = syncedBlocks;
|
|
671
481
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
482
|
+
// Handle zero as a special case, since the block hash won't match across services if we're changing the prefilled data for the genesis block,
|
|
483
|
+
// as the world state can compute the new genesis block hash, but other components use the hardcoded constant.
|
|
484
|
+
const result =
|
|
485
|
+
(l2BlockSource.number === 0 && worldState.number === 0 && p2p.number === 0 && l1ToL2MessageSource.number === 0) ||
|
|
486
|
+
(worldState.hash === l2BlockSource.hash &&
|
|
487
|
+
p2p.hash === l2BlockSource.hash &&
|
|
488
|
+
l1ToL2MessageSource.hash === l2BlockSource.hash);
|
|
680
489
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
[Attributes.BLOCK_ARCHIVE]: block.archive.toString(),
|
|
684
|
-
[Attributes.BLOCK_TXS_COUNT]: txHashes.length,
|
|
685
|
-
}))
|
|
686
|
-
protected async collectAttestations(
|
|
687
|
-
block: L2Block,
|
|
688
|
-
txs: Tx[],
|
|
689
|
-
proposerAddress: EthAddress | undefined,
|
|
690
|
-
): Promise<CommitteeAttestation[] | undefined> {
|
|
691
|
-
const { committee } = await this.epochCache.getCommittee(block.header.getSlot());
|
|
692
|
-
|
|
693
|
-
// We checked above that the committee is defined, so this should never happen.
|
|
694
|
-
if (!committee) {
|
|
695
|
-
throw new Error('No committee when collecting attestations');
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
if (committee.length === 0) {
|
|
699
|
-
this.log.verbose(`Attesting committee is empty`);
|
|
490
|
+
if (!result) {
|
|
491
|
+
this.log.debug(`Sequencer sync check failed`, { worldState, l2BlockSource, p2p, l1ToL2MessageSource });
|
|
700
492
|
return undefined;
|
|
701
|
-
} else {
|
|
702
|
-
this.log.debug(`Attesting committee length is ${committee.length}`);
|
|
703
493
|
}
|
|
704
494
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
495
|
+
// Special case for genesis state
|
|
496
|
+
const blockNumber = worldState.number;
|
|
497
|
+
if (blockNumber < INITIAL_L2_BLOCK_NUM) {
|
|
498
|
+
const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
499
|
+
return { blockNumber: BlockNumber(INITIAL_L2_BLOCK_NUM - 1), archive, l1Timestamp, pendingChainValidationStatus };
|
|
709
500
|
}
|
|
710
501
|
|
|
711
|
-
const
|
|
502
|
+
const block = await this.l2BlockSource.getL2BlockNew(blockNumber);
|
|
503
|
+
if (!block) {
|
|
504
|
+
// this shouldn't really happen because a moment ago we checked that all components were in sync
|
|
505
|
+
this.log.error(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`);
|
|
506
|
+
return undefined;
|
|
507
|
+
}
|
|
712
508
|
|
|
713
|
-
|
|
714
|
-
|
|
509
|
+
return {
|
|
510
|
+
block,
|
|
511
|
+
blockNumber: block.number,
|
|
512
|
+
archive: block.archive.root,
|
|
513
|
+
l1Timestamp,
|
|
514
|
+
pendingChainValidationStatus,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
715
517
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
block.header.state,
|
|
723
|
-
txs,
|
|
724
|
-
proposerAddress,
|
|
725
|
-
blockProposalOptions,
|
|
726
|
-
);
|
|
518
|
+
/**
|
|
519
|
+
* Checks if we are the proposer for the next slot.
|
|
520
|
+
* @returns True if we can propose, and the proposer address (undefined if anyone can propose)
|
|
521
|
+
*/
|
|
522
|
+
protected async checkCanPropose(slot: SlotNumber): Promise<[boolean, EthAddress | undefined]> {
|
|
523
|
+
let proposer: EthAddress | undefined;
|
|
727
524
|
|
|
728
|
-
|
|
729
|
-
|
|
525
|
+
try {
|
|
526
|
+
proposer = await this.epochCache.getProposerAttesterAddressInSlot(slot);
|
|
527
|
+
} catch (e) {
|
|
528
|
+
if (e instanceof NoCommitteeError) {
|
|
529
|
+
this.log.warn(`Cannot propose at next L2 slot ${slot} since the committee does not exist on L1`);
|
|
530
|
+
return [false, undefined];
|
|
531
|
+
}
|
|
532
|
+
this.log.error(`Error getting proposer for slot ${slot}`, e);
|
|
533
|
+
return [false, undefined];
|
|
730
534
|
}
|
|
731
535
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
536
|
+
// If proposer is undefined, then the committee is empty and anyone may propose
|
|
537
|
+
if (proposer === undefined) {
|
|
538
|
+
return [true, undefined];
|
|
539
|
+
}
|
|
540
|
+
// In fisherman mode, just return the current proposer
|
|
541
|
+
if (this.config.fishermanMode) {
|
|
542
|
+
return [true, proposer];
|
|
736
543
|
}
|
|
737
544
|
|
|
738
|
-
this.
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
const attestationTimeAllowed = this.enforceTimeTable
|
|
742
|
-
? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_BLOCK)!
|
|
743
|
-
: this.aztecSlotDuration;
|
|
744
|
-
|
|
745
|
-
this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
|
|
746
|
-
|
|
747
|
-
const timer = new Timer();
|
|
748
|
-
let collectedAttestionsCount: number = 0;
|
|
749
|
-
try {
|
|
750
|
-
const attestationDeadline = new Date(this.dateProvider.now() + attestationTimeAllowed * 1000);
|
|
751
|
-
const attestations = await this.validatorClient.collectAttestations(
|
|
752
|
-
proposal,
|
|
753
|
-
numberOfRequiredAttestations,
|
|
754
|
-
attestationDeadline,
|
|
755
|
-
);
|
|
756
|
-
|
|
757
|
-
collectedAttestionsCount = attestations.length;
|
|
545
|
+
const validatorAddresses = this.validatorClient.getValidatorAddresses();
|
|
546
|
+
const weAreProposer = validatorAddresses.some(addr => addr.equals(proposer));
|
|
758
547
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
if (err && err instanceof AttestationTimeoutError) {
|
|
763
|
-
collectedAttestionsCount = err.collectedCount;
|
|
764
|
-
}
|
|
765
|
-
throw err;
|
|
766
|
-
} finally {
|
|
767
|
-
this.metrics.recordCollectedAttestations(collectedAttestionsCount, timer.ms());
|
|
548
|
+
if (!weAreProposer) {
|
|
549
|
+
this.log.debug(`Cannot propose at slot ${slot} since we are not a proposer`, { validatorAddresses, proposer });
|
|
550
|
+
return [false, proposer];
|
|
768
551
|
}
|
|
552
|
+
|
|
553
|
+
return [true, proposer];
|
|
769
554
|
}
|
|
770
555
|
|
|
771
556
|
/**
|
|
772
|
-
*
|
|
773
|
-
*
|
|
557
|
+
* Tries to vote on slashing actions and governance when the sync check fails but we're past the max time for initializing a proposal.
|
|
558
|
+
* This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
|
|
774
559
|
*/
|
|
775
|
-
@trackSpan('
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
protected async enqueuePublishL2Block(
|
|
779
|
-
block: L2Block,
|
|
780
|
-
attestations: CommitteeAttestation[] | undefined,
|
|
781
|
-
txHashes: TxHash[],
|
|
782
|
-
invalidateBlock: InvalidateBlockRequest | undefined,
|
|
783
|
-
publisher: SequencerPublisher,
|
|
784
|
-
): Promise<void> {
|
|
785
|
-
// Publishes new block to the network and awaits the tx to be mined
|
|
786
|
-
this.setState(SequencerState.PUBLISHING_BLOCK, block.header.globalVariables.slotNumber.toBigInt());
|
|
560
|
+
@trackSpan('Seqeuencer.tryVoteWhenSyncFails', ({ slot }) => ({ [Attributes.SLOT_NUMBER]: slot }))
|
|
561
|
+
protected async tryVoteWhenSyncFails(args: { slot: SlotNumber; ts: bigint }): Promise<void> {
|
|
562
|
+
const { slot } = args;
|
|
787
563
|
|
|
788
|
-
//
|
|
789
|
-
|
|
790
|
-
|
|
564
|
+
// Prevent duplicate attempts in the same slot
|
|
565
|
+
if (this.lastSlotForVoteWhenSyncFailed === slot) {
|
|
566
|
+
this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
791
569
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
570
|
+
// Check if we're past the max time for initializing a proposal
|
|
571
|
+
const secondsIntoSlot = this.getSecondsIntoSlot(slot);
|
|
572
|
+
const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
|
|
573
|
+
|
|
574
|
+
// If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
|
|
575
|
+
// We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
|
|
576
|
+
if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
|
|
577
|
+
this.log.trace(`Not attempting to vote since there is still time for block building`, {
|
|
578
|
+
secondsIntoSlot,
|
|
579
|
+
maxAllowedTime,
|
|
580
|
+
});
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
|
|
585
|
+
secondsIntoSlot,
|
|
586
|
+
maxAllowedTime,
|
|
795
587
|
});
|
|
796
588
|
|
|
797
|
-
if
|
|
798
|
-
|
|
589
|
+
// Check if we're a proposer or proposal is open
|
|
590
|
+
const [canPropose, proposer] = await this.checkCanPropose(slot);
|
|
591
|
+
if (!canPropose) {
|
|
592
|
+
this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, { slot, proposer });
|
|
593
|
+
return;
|
|
799
594
|
}
|
|
800
|
-
}
|
|
801
595
|
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
* We don't check against the previous block submitted since it may have been reorg'd out.
|
|
805
|
-
* @returns Boolean indicating if our dependencies are synced to the latest block.
|
|
806
|
-
*/
|
|
807
|
-
protected async getChainTip(): Promise<
|
|
808
|
-
| {
|
|
809
|
-
block?: L2Block;
|
|
810
|
-
blockNumber: number;
|
|
811
|
-
archive: Fr;
|
|
812
|
-
l1Timestamp: bigint;
|
|
813
|
-
pendingChainValidationStatus: ValidateBlockResult;
|
|
814
|
-
}
|
|
815
|
-
| undefined
|
|
816
|
-
> {
|
|
817
|
-
const syncedBlocks = await Promise.all([
|
|
818
|
-
this.worldState.status().then(({ syncSummary }) => ({
|
|
819
|
-
number: syncSummary.latestBlockNumber,
|
|
820
|
-
hash: syncSummary.latestBlockHash,
|
|
821
|
-
})),
|
|
822
|
-
this.l2BlockSource.getL2Tips().then(t => t.latest),
|
|
823
|
-
this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
|
|
824
|
-
this.l1ToL2MessageSource.getL2Tips().then(t => t.latest),
|
|
825
|
-
this.l2BlockSource.getL1Timestamp(),
|
|
826
|
-
this.l2BlockSource.getPendingChainValidationStatus(),
|
|
827
|
-
] as const);
|
|
596
|
+
// Mark this slot as attempted
|
|
597
|
+
this.lastSlotForVoteWhenSyncFailed = slot;
|
|
828
598
|
|
|
829
|
-
|
|
830
|
-
|
|
599
|
+
// Get a publisher for voting
|
|
600
|
+
const { attestorAddress, publisher } = await this.publisherFactory.create(proposer);
|
|
831
601
|
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
? worldState.number === 0 && p2p.number === 0 && l1ToL2MessageSource.number === 0
|
|
837
|
-
: worldState.hash === l2BlockSource.hash &&
|
|
838
|
-
p2p.hash === l2BlockSource.hash &&
|
|
839
|
-
l1ToL2MessageSource.hash === l2BlockSource.hash;
|
|
602
|
+
this.log.debug(`Attempting to vote despite sync failure at slot ${slot}`, {
|
|
603
|
+
attestorAddress,
|
|
604
|
+
slot,
|
|
605
|
+
});
|
|
840
606
|
|
|
841
|
-
|
|
842
|
-
|
|
607
|
+
// Enqueue governance and slashing votes
|
|
608
|
+
const voter = new CheckpointVoter(
|
|
609
|
+
slot,
|
|
610
|
+
publisher,
|
|
611
|
+
attestorAddress,
|
|
612
|
+
this.validatorClient,
|
|
613
|
+
this.slasherClient,
|
|
614
|
+
this.l1Constants,
|
|
615
|
+
this.config,
|
|
616
|
+
this.metrics,
|
|
617
|
+
this.log,
|
|
618
|
+
);
|
|
619
|
+
const votesPromises = voter.enqueueVotes();
|
|
620
|
+
const votes = await Promise.all(votesPromises);
|
|
843
621
|
|
|
844
|
-
if (!
|
|
845
|
-
|
|
622
|
+
if (votes.every(p => !p)) {
|
|
623
|
+
this.log.debug(`No votes to enqueue for slot ${slot}`);
|
|
624
|
+
return;
|
|
846
625
|
}
|
|
847
626
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
const block = await this.l2BlockSource.getBlock(blockNumber);
|
|
851
|
-
if (!block) {
|
|
852
|
-
// this shouldn't really happen because a moment ago we checked that all components were in sync
|
|
853
|
-
this.log.warn(`Failed to get L2 block ${blockNumber} from the archiver with all components in sync`, logData);
|
|
854
|
-
return undefined;
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
return {
|
|
858
|
-
block,
|
|
859
|
-
blockNumber: block.number,
|
|
860
|
-
archive: block.archive.root,
|
|
861
|
-
l1Timestamp,
|
|
862
|
-
pendingChainValidationStatus,
|
|
863
|
-
};
|
|
864
|
-
} else {
|
|
865
|
-
const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
866
|
-
return { blockNumber: INITIAL_L2_BLOCK_NUM - 1, archive, l1Timestamp, pendingChainValidationStatus };
|
|
867
|
-
}
|
|
627
|
+
this.log.info(`Voting in slot ${slot} despite sync failure`, { slot });
|
|
628
|
+
await publisher.sendRequests();
|
|
868
629
|
}
|
|
869
630
|
|
|
870
631
|
/**
|
|
@@ -874,27 +635,26 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
874
635
|
* and if they fail, any sequencer will try as well.
|
|
875
636
|
*/
|
|
876
637
|
protected async considerInvalidatingBlock(
|
|
877
|
-
syncedTo:
|
|
878
|
-
currentSlot:
|
|
879
|
-
ourValidatorAddresses: EthAddress[],
|
|
880
|
-
publisher: SequencerPublisher,
|
|
638
|
+
syncedTo: SequencerSyncCheckResult,
|
|
639
|
+
currentSlot: SlotNumber,
|
|
881
640
|
): Promise<void> {
|
|
882
641
|
const { pendingChainValidationStatus, l1Timestamp } = syncedTo;
|
|
883
642
|
if (pendingChainValidationStatus.valid) {
|
|
884
643
|
return;
|
|
885
644
|
}
|
|
886
645
|
|
|
887
|
-
const
|
|
888
|
-
const
|
|
889
|
-
const
|
|
646
|
+
const invalidBlockNumber = pendingChainValidationStatus.block.blockNumber;
|
|
647
|
+
const invalidBlockTimestamp = pendingChainValidationStatus.block.timestamp;
|
|
648
|
+
const timeSinceChainInvalid = this.dateProvider.nowInSeconds() - Number(invalidBlockTimestamp);
|
|
649
|
+
const ourValidatorAddresses = this.validatorClient.getValidatorAddresses();
|
|
890
650
|
|
|
891
651
|
const { secondsBeforeInvalidatingBlockAsCommitteeMember, secondsBeforeInvalidatingBlockAsNonCommitteeMember } =
|
|
892
652
|
this.config;
|
|
893
653
|
|
|
894
654
|
const logData = {
|
|
895
|
-
invalidL1Timestamp,
|
|
655
|
+
invalidL1Timestamp: invalidBlockTimestamp,
|
|
896
656
|
l1Timestamp,
|
|
897
|
-
invalidBlock: pendingChainValidationStatus.block
|
|
657
|
+
invalidBlock: pendingChainValidationStatus.block,
|
|
898
658
|
secondsBeforeInvalidatingBlockAsCommitteeMember,
|
|
899
659
|
secondsBeforeInvalidatingBlockAsNonCommitteeMember,
|
|
900
660
|
ourValidatorAddresses,
|
|
@@ -922,6 +682,24 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
922
682
|
return;
|
|
923
683
|
}
|
|
924
684
|
|
|
685
|
+
let validatorToUse: EthAddress;
|
|
686
|
+
if (invalidateAsCommitteeMember) {
|
|
687
|
+
// When invalidating as a committee member, use first validator that's actually in the committee
|
|
688
|
+
const { committee } = await this.epochCache.getCommittee(currentSlot);
|
|
689
|
+
if (committee) {
|
|
690
|
+
const committeeSet = new Set(committee.map(addr => addr.toString()));
|
|
691
|
+
validatorToUse =
|
|
692
|
+
ourValidatorAddresses.find(addr => committeeSet.has(addr.toString())) ?? ourValidatorAddresses[0];
|
|
693
|
+
} else {
|
|
694
|
+
validatorToUse = ourValidatorAddresses[0];
|
|
695
|
+
}
|
|
696
|
+
} else {
|
|
697
|
+
// When invalidating as a non-committee member, use the first validator
|
|
698
|
+
validatorToUse = ourValidatorAddresses[0];
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const { publisher } = await this.publisherFactory.create(validatorToUse);
|
|
702
|
+
|
|
925
703
|
const invalidateBlock = await publisher.simulateInvalidateBlock(pendingChainValidationStatus);
|
|
926
704
|
if (!invalidateBlock) {
|
|
927
705
|
this.log.warn(`Failed to simulate invalidate block`, logData);
|
|
@@ -936,31 +714,86 @@ export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<Sequ
|
|
|
936
714
|
);
|
|
937
715
|
|
|
938
716
|
publisher.enqueueInvalidateBlock(invalidateBlock);
|
|
939
|
-
|
|
717
|
+
|
|
718
|
+
if (!this.config.fishermanMode) {
|
|
719
|
+
await publisher.sendRequests();
|
|
720
|
+
} else {
|
|
721
|
+
this.log.info('Invalidating block in fisherman mode, clearing pending requests');
|
|
722
|
+
publisher.clearPendingRequests();
|
|
723
|
+
}
|
|
940
724
|
}
|
|
941
725
|
|
|
942
|
-
private
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
726
|
+
private logStrategyComparison(epoch: EpochNumber, publisher: SequencerPublisher): void {
|
|
727
|
+
const feeAnalyzer = publisher.getL1FeeAnalyzer();
|
|
728
|
+
if (!feeAnalyzer) {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const comparison = feeAnalyzer.getStrategyComparison();
|
|
733
|
+
if (comparison.length === 0) {
|
|
734
|
+
this.log.debug(`No strategy data available yet for epoch ${epoch}`);
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
this.log.info(`L1 Fee Strategy Performance Report - End of Epoch ${epoch}`, {
|
|
739
|
+
epoch: Number(epoch),
|
|
740
|
+
totalAnalyses: comparison[0]?.totalAnalyses,
|
|
741
|
+
strategies: comparison.map(s => ({
|
|
742
|
+
id: s.strategyId,
|
|
743
|
+
name: s.strategyName,
|
|
744
|
+
inclusionRate: `${(s.inclusionRate * 100).toFixed(1)}%`,
|
|
745
|
+
inclusionCount: `${s.inclusionCount}/${s.totalAnalyses}`,
|
|
746
|
+
avgCostEth: s.avgEstimatedCostEth.toFixed(6),
|
|
747
|
+
totalCostEth: s.totalEstimatedCostEth.toFixed(6),
|
|
748
|
+
avgOverpaymentEth: s.avgOverpaymentEth.toFixed(6),
|
|
749
|
+
totalOverpaymentEth: s.totalOverpaymentEth.toFixed(6),
|
|
750
|
+
avgPriorityFeeDeltaGwei: s.avgPriorityFeeDeltaGwei.toFixed(2),
|
|
751
|
+
})),
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
private getSlotStartBuildTimestamp(slotNumber: SlotNumber): number {
|
|
756
|
+
return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
|
|
948
757
|
}
|
|
949
758
|
|
|
950
|
-
private getSecondsIntoSlot(slotNumber:
|
|
759
|
+
private getSecondsIntoSlot(slotNumber: SlotNumber): number {
|
|
951
760
|
const slotStartTimestamp = this.getSlotStartBuildTimestamp(slotNumber);
|
|
952
761
|
return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
|
|
953
762
|
}
|
|
954
763
|
|
|
955
|
-
get aztecSlotDuration() {
|
|
764
|
+
public get aztecSlotDuration() {
|
|
956
765
|
return this.l1Constants.slotDuration;
|
|
957
766
|
}
|
|
958
767
|
|
|
959
|
-
get maxL2BlockGas(): number | undefined {
|
|
768
|
+
public get maxL2BlockGas(): number | undefined {
|
|
960
769
|
return this.config.maxL2BlockGas;
|
|
961
770
|
}
|
|
962
771
|
|
|
963
772
|
public getSlasherClient(): SlasherClientInterface | undefined {
|
|
964
773
|
return this.slasherClient;
|
|
965
774
|
}
|
|
775
|
+
|
|
776
|
+
public get tracer(): Tracer {
|
|
777
|
+
return this.metrics.tracer;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
public getValidatorAddresses() {
|
|
781
|
+
return this.validatorClient?.getValidatorAddresses();
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
public getConfig() {
|
|
785
|
+
return this.config;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
private get l1PublishingTime(): number {
|
|
789
|
+
return this.config.l1PublishingTime ?? this.l1Constants.ethereumSlotDuration;
|
|
790
|
+
}
|
|
966
791
|
}
|
|
792
|
+
|
|
793
|
+
type SequencerSyncCheckResult = {
|
|
794
|
+
block?: L2BlockNew;
|
|
795
|
+
blockNumber: BlockNumber;
|
|
796
|
+
archive: Fr;
|
|
797
|
+
l1Timestamp: bigint;
|
|
798
|
+
pendingChainValidationStatus: ValidateBlockResult;
|
|
799
|
+
};
|