@aztec/sequencer-client 0.0.0-test.1 → 0.0.1-commit.001888fc
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 +50 -33
- package/dest/client/sequencer-client.d.ts.map +1 -1
- package/dest/client/sequencer-client.js +164 -57
- package/dest/config.d.ts +35 -17
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +150 -81
- package/dest/global_variable_builder/global_builder.d.ts +24 -15
- package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
- package/dest/global_variable_builder/global_builder.js +63 -44
- package/dest/global_variable_builder/index.d.ts +1 -1
- package/dest/index.d.ts +2 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -3
- package/dest/publisher/config.d.ts +48 -27
- package/dest/publisher/config.d.ts.map +1 -1
- package/dest/publisher/config.js +109 -32
- package/dest/publisher/index.d.ts +4 -1
- package/dest/publisher/index.d.ts.map +1 -1
- package/dest/publisher/index.js +3 -0
- 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 +52 -0
- package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -0
- package/dest/publisher/sequencer-publisher-factory.js +76 -0
- package/dest/publisher/sequencer-publisher-metrics.d.ts +5 -4
- package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher-metrics.js +34 -62
- package/dest/publisher/sequencer-publisher.d.ts +158 -93
- package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
- package/dest/publisher/sequencer-publisher.js +1452 -255
- package/dest/sequencer/checkpoint_proposal_job.d.ts +100 -0
- package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_proposal_job.js +1240 -0
- package/dest/sequencer/checkpoint_voter.d.ts +35 -0
- package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
- package/dest/sequencer/checkpoint_voter.js +109 -0
- package/dest/sequencer/config.d.ts +7 -1
- 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 +4 -2
- package/dest/sequencer/index.d.ts.map +1 -1
- package/dest/sequencer/index.js +3 -1
- package/dest/sequencer/metrics.d.ts +60 -12
- package/dest/sequencer/metrics.d.ts.map +1 -1
- package/dest/sequencer/metrics.js +283 -67
- package/dest/sequencer/sequencer.d.ts +157 -135
- package/dest/sequencer/sequencer.d.ts.map +1 -1
- package/dest/sequencer/sequencer.js +966 -523
- package/dest/sequencer/timetable.d.ts +75 -25
- package/dest/sequencer/timetable.d.ts.map +1 -1
- package/dest/sequencer/timetable.js +174 -62
- 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 -38
- package/dest/sequencer/utils.d.ts.map +1 -1
- package/dest/sequencer/utils.js +12 -47
- package/dest/test/index.d.ts +9 -3
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +0 -4
- package/dest/test/mock_checkpoint_builder.d.ts +99 -0
- package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
- package/dest/test/mock_checkpoint_builder.js +231 -0
- package/dest/test/utils.d.ts +53 -0
- package/dest/test/utils.d.ts.map +1 -0
- package/dest/test/utils.js +104 -0
- package/package.json +46 -45
- package/src/client/sequencer-client.ts +220 -104
- package/src/config.ts +172 -96
- package/src/global_variable_builder/global_builder.ts +85 -55
- package/src/index.ts +1 -3
- package/src/publisher/config.ts +150 -59
- package/src/publisher/index.ts +7 -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 +124 -0
- package/src/publisher/sequencer-publisher-metrics.ts +33 -63
- package/src/publisher/sequencer-publisher.ts +1266 -311
- package/src/sequencer/README.md +531 -0
- package/src/sequencer/checkpoint_proposal_job.ts +967 -0
- package/src/sequencer/checkpoint_voter.ts +130 -0
- package/src/sequencer/config.ts +8 -0
- package/src/sequencer/errors.ts +21 -0
- package/src/sequencer/events.ts +27 -0
- package/src/sequencer/index.ts +3 -1
- package/src/sequencer/metrics.ts +362 -73
- package/src/sequencer/sequencer.ts +712 -586
- package/src/sequencer/timetable.ts +224 -64
- package/src/sequencer/types.ts +6 -0
- package/src/sequencer/utils.ts +28 -60
- package/src/test/index.ts +11 -5
- package/src/test/mock_checkpoint_builder.ts +323 -0
- package/src/test/utils.ts +167 -0
- package/dest/sequencer/allowed.d.ts +0 -3
- package/dest/sequencer/allowed.d.ts.map +0 -1
- package/dest/sequencer/allowed.js +0 -27
- package/dest/slasher/factory.d.ts +0 -7
- package/dest/slasher/factory.d.ts.map +0 -1
- package/dest/slasher/factory.js +0 -8
- package/dest/slasher/index.d.ts +0 -3
- package/dest/slasher/index.d.ts.map +0 -1
- package/dest/slasher/index.js +0 -2
- package/dest/slasher/slasher_client.d.ts +0 -75
- package/dest/slasher/slasher_client.d.ts.map +0 -1
- package/dest/slasher/slasher_client.js +0 -132
- package/dest/tx_validator/archive_cache.d.ts +0 -14
- package/dest/tx_validator/archive_cache.d.ts.map +0 -1
- package/dest/tx_validator/archive_cache.js +0 -22
- package/dest/tx_validator/gas_validator.d.ts +0 -14
- package/dest/tx_validator/gas_validator.d.ts.map +0 -1
- package/dest/tx_validator/gas_validator.js +0 -78
- package/dest/tx_validator/nullifier_cache.d.ts +0 -16
- package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
- package/dest/tx_validator/nullifier_cache.js +0 -24
- package/dest/tx_validator/phases_validator.d.ts +0 -12
- package/dest/tx_validator/phases_validator.d.ts.map +0 -1
- package/dest/tx_validator/phases_validator.js +0 -80
- package/dest/tx_validator/test_utils.d.ts +0 -23
- package/dest/tx_validator/test_utils.d.ts.map +0 -1
- package/dest/tx_validator/test_utils.js +0 -26
- package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
- package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
- package/dest/tx_validator/tx_validator_factory.js +0 -50
- package/src/sequencer/allowed.ts +0 -36
- package/src/slasher/factory.ts +0 -15
- package/src/slasher/index.ts +0 -2
- package/src/slasher/slasher_client.ts +0 -193
- package/src/tx_validator/archive_cache.ts +0 -28
- package/src/tx_validator/gas_validator.ts +0 -101
- package/src/tx_validator/nullifier_cache.ts +0 -30
- package/src/tx_validator/phases_validator.ts +0 -98
- package/src/tx_validator/test_utils.ts +0 -48
- package/src/tx_validator/tx_validator_factory.ts +0 -120
|
@@ -1,759 +1,885 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getKzg } from '@aztec/blob-lib';
|
|
2
2
|
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
3
|
-
import {
|
|
3
|
+
import type { EpochCache } from '@aztec/epoch-cache';
|
|
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';
|
|
4
8
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
5
|
-
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
6
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
7
9
|
import { createLogger } from '@aztec/foundation/log';
|
|
8
10
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
9
|
-
import {
|
|
11
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
12
|
+
import type { TypedEventEmitter } from '@aztec/foundation/types';
|
|
10
13
|
import type { P2P } from '@aztec/p2p';
|
|
11
|
-
import type {
|
|
12
|
-
import type {
|
|
13
|
-
import {
|
|
14
|
-
import
|
|
15
|
-
import type { ContractDataSource } from '@aztec/stdlib/contract';
|
|
16
|
-
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
17
|
-
import { Gas } from '@aztec/stdlib/gas';
|
|
14
|
+
import type { SlasherClientInterface } from '@aztec/slasher';
|
|
15
|
+
import type { BlockData, L2BlockSink, L2BlockSource, ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
16
|
+
import type { Checkpoint } from '@aztec/stdlib/checkpoint';
|
|
17
|
+
import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
|
|
18
18
|
import {
|
|
19
|
-
type
|
|
19
|
+
type ResolvedSequencerConfig,
|
|
20
|
+
type SequencerConfig,
|
|
20
21
|
SequencerConfigSchema,
|
|
21
22
|
type WorldStateSynchronizer,
|
|
22
|
-
type WorldStateSynchronizerStatus,
|
|
23
23
|
} from '@aztec/stdlib/interfaces/server';
|
|
24
24
|
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
25
25
|
import { pickFromSchema } from '@aztec/stdlib/schemas';
|
|
26
|
-
import
|
|
27
|
-
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
|
|
28
|
-
import {
|
|
29
|
-
BlockHeader,
|
|
30
|
-
ContentCommitment,
|
|
31
|
-
type GlobalVariables,
|
|
32
|
-
StateReference,
|
|
33
|
-
Tx,
|
|
34
|
-
type TxHash,
|
|
35
|
-
} from '@aztec/stdlib/tx';
|
|
26
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
36
27
|
import { Attributes, type TelemetryClient, type Tracer, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
37
|
-
import type
|
|
28
|
+
import { FullNodeCheckpointsBuilder, NodeKeystoreAdapter, type ValidatorClient } from '@aztec/validator-client';
|
|
29
|
+
|
|
30
|
+
import EventEmitter from 'node:events';
|
|
38
31
|
|
|
32
|
+
import { DefaultSequencerConfig } from '../config.js';
|
|
39
33
|
import type { GlobalVariableBuilder } from '../global_variable_builder/global_builder.js';
|
|
40
|
-
import
|
|
41
|
-
import type {
|
|
42
|
-
import {
|
|
43
|
-
import {
|
|
44
|
-
import
|
|
34
|
+
import type { SequencerPublisherFactory } from '../publisher/sequencer-publisher-factory.js';
|
|
35
|
+
import type { InvalidateCheckpointRequest, SequencerPublisher } from '../publisher/sequencer-publisher.js';
|
|
36
|
+
import { CheckpointProposalJob } from './checkpoint_proposal_job.js';
|
|
37
|
+
import { CheckpointVoter } from './checkpoint_voter.js';
|
|
38
|
+
import { SequencerInterruptedError, SequencerTooSlowError } from './errors.js';
|
|
39
|
+
import type { SequencerEvents } from './events.js';
|
|
45
40
|
import { SequencerMetrics } from './metrics.js';
|
|
46
|
-
import { SequencerTimetable
|
|
47
|
-
import {
|
|
41
|
+
import { SequencerTimetable } from './timetable.js';
|
|
42
|
+
import type { SequencerRollupConstants } from './types.js';
|
|
43
|
+
import { SequencerState } from './utils.js';
|
|
48
44
|
|
|
49
45
|
export { SequencerState };
|
|
50
46
|
|
|
51
|
-
type SequencerRollupConstants = Pick<L1RollupConstants, 'ethereumSlotDuration' | 'l1GenesisTime' | 'slotDuration'>;
|
|
52
|
-
|
|
53
47
|
/**
|
|
54
48
|
* Sequencer client
|
|
55
|
-
* -
|
|
56
|
-
* -
|
|
57
|
-
* -
|
|
58
|
-
* -
|
|
59
|
-
* -
|
|
60
|
-
* - Publishes L1 tx(s) to the rollup contract via RollupPublisher.
|
|
49
|
+
* - Checks whether it is elected as proposer for the next slot
|
|
50
|
+
* - Builds multiple blocks and broadcasts them
|
|
51
|
+
* - Collects attestations for the checkpoint
|
|
52
|
+
* - Publishes the checkpoint to L1
|
|
53
|
+
* - Votes for proposals and slashes on L1
|
|
61
54
|
*/
|
|
62
|
-
export class Sequencer {
|
|
55
|
+
export class Sequencer extends (EventEmitter as new () => TypedEventEmitter<SequencerEvents>) {
|
|
63
56
|
private runningPromise?: RunningPromise;
|
|
64
|
-
private pollingIntervalMs: number = 1000;
|
|
65
|
-
private maxTxsPerBlock = 32;
|
|
66
|
-
private minTxsPerBlock = 1;
|
|
67
|
-
private maxL1TxInclusionTimeIntoSlot = 0;
|
|
68
|
-
// TODO: zero values should not be allowed for the following 2 values in PROD
|
|
69
|
-
private _coinbase = EthAddress.ZERO;
|
|
70
|
-
private _feeRecipient = AztecAddress.ZERO;
|
|
71
57
|
private state = SequencerState.STOPPED;
|
|
72
|
-
private allowedInSetup: AllowedElement[] = [];
|
|
73
|
-
private maxBlockSizeInBytes: number = 1024 * 1024;
|
|
74
|
-
private maxBlockGas: Gas = new Gas(100e9, 100e9);
|
|
75
58
|
private metrics: SequencerMetrics;
|
|
76
|
-
|
|
59
|
+
|
|
60
|
+
/** The last slot for which we attempted to perform our voting duties with degraded block production */
|
|
61
|
+
private lastSlotForFallbackVote: SlotNumber | undefined;
|
|
62
|
+
|
|
63
|
+
/** The last slot for which we logged "no committee" warning, to avoid spam */
|
|
64
|
+
private lastSlotForNoCommitteeWarning: SlotNumber | undefined;
|
|
65
|
+
|
|
66
|
+
/** The last slot for which we triggered a checkpoint proposal job, to prevent duplicate attempts. */
|
|
67
|
+
private lastSlotForCheckpointProposalJob: SlotNumber | undefined;
|
|
68
|
+
|
|
69
|
+
/** Last successful checkpoint proposed */
|
|
70
|
+
private lastCheckpointProposed: Checkpoint | undefined;
|
|
71
|
+
|
|
72
|
+
/** The last epoch for which we logged strategy comparison in fisherman mode. */
|
|
73
|
+
private lastEpochForStrategyComparison: EpochNumber | undefined;
|
|
77
74
|
|
|
78
75
|
/** The maximum number of seconds that the sequencer can be into a slot to transition to a particular state. */
|
|
79
76
|
protected timetable!: SequencerTimetable;
|
|
80
77
|
|
|
81
|
-
|
|
78
|
+
/** Config for the sequencer */
|
|
79
|
+
protected config: ResolvedSequencerConfig = DefaultSequencerConfig;
|
|
82
80
|
|
|
83
81
|
constructor(
|
|
84
|
-
protected
|
|
85
|
-
protected validatorClient: ValidatorClient
|
|
82
|
+
protected publisherFactory: SequencerPublisherFactory,
|
|
83
|
+
protected validatorClient: ValidatorClient,
|
|
86
84
|
protected globalsBuilder: GlobalVariableBuilder,
|
|
87
85
|
protected p2pClient: P2P,
|
|
88
86
|
protected worldState: WorldStateSynchronizer,
|
|
89
|
-
protected slasherClient:
|
|
90
|
-
protected
|
|
91
|
-
protected l2BlockSource: L2BlockSource,
|
|
87
|
+
protected slasherClient: SlasherClientInterface | undefined,
|
|
88
|
+
protected l2BlockSource: L2BlockSource & L2BlockSink,
|
|
92
89
|
protected l1ToL2MessageSource: L1ToL2MessageSource,
|
|
93
|
-
protected
|
|
94
|
-
protected contractDataSource: ContractDataSource,
|
|
90
|
+
protected checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
95
91
|
protected l1Constants: SequencerRollupConstants,
|
|
96
92
|
protected dateProvider: DateProvider,
|
|
97
|
-
protected
|
|
98
|
-
|
|
93
|
+
protected epochCache: EpochCache,
|
|
94
|
+
protected rollupContract: RollupContract,
|
|
95
|
+
config: SequencerConfig,
|
|
96
|
+
protected telemetry: TelemetryClient = getTelemetryClient(),
|
|
99
97
|
protected log = createLogger('sequencer'),
|
|
100
98
|
) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// Register the block builder with the validator client for re-execution
|
|
104
|
-
this.validatorClient?.registerBlockBuilder(this.buildBlock.bind(this));
|
|
105
|
-
|
|
106
|
-
// Register the slasher on the publisher to fetch slashing payloads
|
|
107
|
-
this.publisher.registerSlashPayloadGetter(this.slasherClient.getSlashPayload.bind(this.slasherClient));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get tracer(): Tracer {
|
|
111
|
-
return this.metrics.tracer;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Updates sequencer config.
|
|
116
|
-
* @param config - New parameters.
|
|
117
|
-
*/
|
|
118
|
-
public async updateConfig(config: SequencerConfig) {
|
|
119
|
-
this.log.info(`Sequencer config set`, omit(pickFromSchema(config, SequencerConfigSchema), 'allowedInSetup'));
|
|
99
|
+
super();
|
|
120
100
|
|
|
121
|
-
if
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (config.maxTxsPerBlock !== undefined) {
|
|
125
|
-
this.maxTxsPerBlock = config.maxTxsPerBlock;
|
|
126
|
-
}
|
|
127
|
-
if (config.minTxsPerBlock !== undefined) {
|
|
128
|
-
this.minTxsPerBlock = config.minTxsPerBlock;
|
|
129
|
-
}
|
|
130
|
-
if (config.maxDABlockGas !== undefined) {
|
|
131
|
-
this.maxBlockGas = new Gas(config.maxDABlockGas, this.maxBlockGas.l2Gas);
|
|
132
|
-
}
|
|
133
|
-
if (config.maxL2BlockGas !== undefined) {
|
|
134
|
-
this.maxBlockGas = new Gas(this.maxBlockGas.daGas, config.maxL2BlockGas);
|
|
135
|
-
}
|
|
136
|
-
if (config.coinbase) {
|
|
137
|
-
this._coinbase = config.coinbase;
|
|
138
|
-
}
|
|
139
|
-
if (config.feeRecipient) {
|
|
140
|
-
this._feeRecipient = config.feeRecipient;
|
|
141
|
-
}
|
|
142
|
-
if (config.allowedInSetup) {
|
|
143
|
-
this.allowedInSetup = config.allowedInSetup;
|
|
144
|
-
} else {
|
|
145
|
-
this.allowedInSetup = await getDefaultAllowedSetupFunctions();
|
|
146
|
-
}
|
|
147
|
-
if (config.maxBlockSizeInBytes !== undefined) {
|
|
148
|
-
this.maxBlockSizeInBytes = config.maxBlockSizeInBytes;
|
|
149
|
-
}
|
|
150
|
-
if (config.governanceProposerPayload) {
|
|
151
|
-
this.publisher.setGovernancePayload(config.governanceProposerPayload);
|
|
152
|
-
}
|
|
153
|
-
if (config.maxL1TxInclusionTimeIntoSlot !== undefined) {
|
|
154
|
-
this.maxL1TxInclusionTimeIntoSlot = config.maxL1TxInclusionTimeIntoSlot;
|
|
101
|
+
// Add [FISHERMAN] prefix to logger if in fisherman mode
|
|
102
|
+
if (config.fishermanMode) {
|
|
103
|
+
this.log = log.createChild('[FISHERMAN]');
|
|
155
104
|
}
|
|
156
|
-
if (config.enforceTimeTable !== undefined) {
|
|
157
|
-
this.enforceTimeTable = config.enforceTimeTable;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
this.setTimeTable();
|
|
161
105
|
|
|
162
|
-
|
|
163
|
-
this.config
|
|
106
|
+
this.metrics = new SequencerMetrics(telemetry, this.rollupContract, 'Sequencer');
|
|
107
|
+
this.updateConfig(config);
|
|
164
108
|
}
|
|
165
109
|
|
|
166
|
-
|
|
110
|
+
/** Updates sequencer config by the defined values and updates the timetable */
|
|
111
|
+
public updateConfig(config: Partial<SequencerConfig>) {
|
|
112
|
+
const filteredConfig = pickFromSchema(config, SequencerConfigSchema);
|
|
113
|
+
this.log.info(`Updated sequencer config`, omit(filteredConfig, 'txPublicSetupAllowListExtend'));
|
|
114
|
+
this.config = merge(this.config, filteredConfig);
|
|
167
115
|
this.timetable = new SequencerTimetable(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
116
|
+
{
|
|
117
|
+
ethereumSlotDuration: this.l1Constants.ethereumSlotDuration,
|
|
118
|
+
aztecSlotDuration: this.aztecSlotDuration,
|
|
119
|
+
l1PublishingTime: this.l1PublishingTime,
|
|
120
|
+
p2pPropagationTime: this.config.attestationPropagationTime,
|
|
121
|
+
blockDurationMs: this.config.blockDurationMs,
|
|
122
|
+
enforce: this.config.enforceTimeTable,
|
|
123
|
+
},
|
|
172
124
|
this.metrics,
|
|
173
125
|
this.log,
|
|
174
126
|
);
|
|
175
|
-
this.log.verbose(`Sequencer timetable updated`, { enforceTimeTable: this.enforceTimeTable });
|
|
176
127
|
}
|
|
177
128
|
|
|
178
|
-
/**
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
129
|
+
/** Initializes the sequencer (precomputes tables). Takes about 3s. */
|
|
130
|
+
public init() {
|
|
131
|
+
getKzg();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Starts the sequencer and moves to IDLE state. */
|
|
135
|
+
public start() {
|
|
136
|
+
this.runningPromise = new RunningPromise(
|
|
137
|
+
this.safeWork.bind(this),
|
|
138
|
+
this.log,
|
|
139
|
+
this.config.sequencerPollingIntervalMS,
|
|
140
|
+
);
|
|
141
|
+
this.setState(SequencerState.IDLE, undefined, { force: true });
|
|
185
142
|
this.runningPromise.start();
|
|
186
|
-
this.log.info(
|
|
143
|
+
this.log.info('Started sequencer');
|
|
187
144
|
}
|
|
188
145
|
|
|
189
|
-
/**
|
|
190
|
-
* Stops the sequencer from processing txs and moves to STOPPED state.
|
|
191
|
-
*/
|
|
146
|
+
/** Stops the sequencer from building blocks and moves to STOPPED state. */
|
|
192
147
|
public async stop(): Promise<void> {
|
|
193
|
-
this.log.
|
|
194
|
-
|
|
148
|
+
this.log.info(`Stopping sequencer`);
|
|
149
|
+
this.setState(SequencerState.STOPPING, undefined, { force: true });
|
|
150
|
+
this.publisherFactory.interruptAll();
|
|
195
151
|
await this.runningPromise?.stop();
|
|
196
|
-
this.
|
|
197
|
-
this.publisher.interrupt();
|
|
198
|
-
this.setState(SequencerState.STOPPED, 0n, true /** force */);
|
|
152
|
+
this.setState(SequencerState.STOPPED, undefined, { force: true });
|
|
199
153
|
this.log.info('Stopped sequencer');
|
|
200
154
|
}
|
|
201
155
|
|
|
202
|
-
/**
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
156
|
+
/** Main sequencer loop with a try/catch */
|
|
157
|
+
protected async safeWork() {
|
|
158
|
+
try {
|
|
159
|
+
await this.work();
|
|
160
|
+
} catch (err) {
|
|
161
|
+
this.emit('checkpoint-error', { error: err as Error });
|
|
162
|
+
if (err instanceof SequencerTooSlowError) {
|
|
163
|
+
// Log as warn only if we had to abort halfway through the block proposal
|
|
164
|
+
const logLvl = [SequencerState.INITIALIZING_CHECKPOINT, SequencerState.PROPOSER_CHECK].includes(
|
|
165
|
+
err.proposedState,
|
|
166
|
+
)
|
|
167
|
+
? ('debug' as const)
|
|
168
|
+
: ('warn' as const);
|
|
169
|
+
this.log[logLvl](err.message, { now: this.dateProvider.nowInSeconds() });
|
|
170
|
+
} else {
|
|
171
|
+
// Re-throw other errors
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
} finally {
|
|
175
|
+
this.setState(SequencerState.IDLE, undefined);
|
|
176
|
+
}
|
|
210
177
|
}
|
|
211
178
|
|
|
212
|
-
/**
|
|
213
|
-
* Returns the current state of the sequencer.
|
|
214
|
-
* @returns An object with a state entry with one of SequencerState.
|
|
215
|
-
*/
|
|
179
|
+
/** Returns the current state of the sequencer. */
|
|
216
180
|
public status() {
|
|
217
181
|
return { state: this.state };
|
|
218
182
|
}
|
|
219
183
|
|
|
220
|
-
/** Forces the sequencer to bypass all time and tx count checks for the next block and build anyway. */
|
|
221
|
-
public flush() {
|
|
222
|
-
this.isFlushing = true;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
184
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
185
|
+
* Main sequencer loop:
|
|
186
|
+
* - Checks if we are up to date
|
|
187
|
+
* - If we are and we are the sequencer, collect txs and build blocks
|
|
188
|
+
* - Build multiple blocks per slot when configured
|
|
189
|
+
* - Collect attestations for the final block
|
|
190
|
+
* - Submit checkpoint
|
|
232
191
|
*/
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
if
|
|
192
|
+
@trackSpan('Sequencer.work')
|
|
193
|
+
protected async work() {
|
|
194
|
+
this.setState(SequencerState.SYNCHRONIZING, undefined);
|
|
195
|
+
const { slot, ts, now, epoch } = this.epochCache.getEpochAndSlotInNextL1Slot();
|
|
196
|
+
|
|
197
|
+
// Check if we are synced and it's our slot, grab a publisher, check previous block invalidation, etc
|
|
198
|
+
const checkpointProposalJob = await this.prepareCheckpointProposal(epoch, slot, ts, now);
|
|
199
|
+
if (!checkpointProposalJob) {
|
|
239
200
|
return;
|
|
240
201
|
}
|
|
241
202
|
|
|
242
|
-
|
|
203
|
+
// Execute the checkpoint proposal job
|
|
204
|
+
const checkpoint = await checkpointProposalJob.execute();
|
|
243
205
|
|
|
244
|
-
|
|
206
|
+
// Update last checkpoint proposed (currently unused)
|
|
207
|
+
if (checkpoint) {
|
|
208
|
+
this.lastCheckpointProposed = checkpoint;
|
|
209
|
+
}
|
|
245
210
|
|
|
246
|
-
//
|
|
247
|
-
|
|
211
|
+
// Log fee strategy comparison if on fisherman
|
|
212
|
+
if (
|
|
213
|
+
this.config.fishermanMode &&
|
|
214
|
+
(this.lastEpochForStrategyComparison === undefined || epoch > this.lastEpochForStrategyComparison)
|
|
215
|
+
) {
|
|
216
|
+
this.logStrategyComparison(epoch, checkpointProposalJob.getPublisher());
|
|
217
|
+
this.lastEpochForStrategyComparison = epoch;
|
|
218
|
+
}
|
|
248
219
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
220
|
+
return checkpoint;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Prepares the checkpoint proposal by performing all necessary checks and setup.
|
|
225
|
+
* This is the initial step in the main loop.
|
|
226
|
+
* @returns CheckpointProposalJob if successful, undefined if we are not yet synced or are not the proposer.
|
|
227
|
+
*/
|
|
228
|
+
@trackSpan('Sequencer.prepareCheckpointProposal')
|
|
229
|
+
private async prepareCheckpointProposal(
|
|
230
|
+
epoch: EpochNumber,
|
|
231
|
+
slot: SlotNumber,
|
|
232
|
+
ts: bigint,
|
|
233
|
+
now: bigint,
|
|
234
|
+
): Promise<CheckpointProposalJob | undefined> {
|
|
235
|
+
// Check we have not already processed this slot (cheapest check)
|
|
236
|
+
// We only check this if enforce timetable is set, since we want to keep processing the same slot if we are not
|
|
237
|
+
// running against actual time (eg when we use sandbox-style automining)
|
|
238
|
+
if (
|
|
239
|
+
this.lastSlotForCheckpointProposalJob &&
|
|
240
|
+
this.lastSlotForCheckpointProposalJob >= slot &&
|
|
241
|
+
this.config.enforceTimeTable
|
|
242
|
+
) {
|
|
243
|
+
this.log.trace(`Slot ${slot} has already been processed`);
|
|
244
|
+
return undefined;
|
|
253
245
|
}
|
|
254
246
|
|
|
255
|
-
|
|
247
|
+
// But if we have already proposed for this slot, the we definitely have to skip it, automining or not
|
|
248
|
+
if (this.lastCheckpointProposed && this.lastCheckpointProposed.header.slotNumber >= slot) {
|
|
249
|
+
this.log.trace(`Slot ${slot} has already been published as checkpoint ${this.lastCheckpointProposed.number}`);
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
256
252
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
this.
|
|
261
|
-
|
|
262
|
-
|
|
253
|
+
// Check all components are synced to latest as seen by the archiver (queries all subsystems)
|
|
254
|
+
const syncedTo = await this.checkSync({ ts, slot });
|
|
255
|
+
if (!syncedTo) {
|
|
256
|
+
await this.tryVoteWhenSyncFails({ slot, ts });
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
263
259
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
VoteType.GOVERNANCE,
|
|
268
|
-
);
|
|
269
|
-
const enqueueSlashingVotePromise = this.publisher.enqueueCastVote(
|
|
270
|
-
slot,
|
|
271
|
-
newGlobalVariables.timestamp.toBigInt(),
|
|
272
|
-
VoteType.SLASHING,
|
|
273
|
-
);
|
|
260
|
+
// If escape hatch is open for this epoch, do not start checkpoint proposal work and do not attempt invalidations.
|
|
261
|
+
// Still perform governance/slashing voting (as proposer) once per slot.
|
|
262
|
+
const isEscapeHatchOpen = await this.epochCache.isEscapeHatchOpen(epoch);
|
|
274
263
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
264
|
+
if (isEscapeHatchOpen) {
|
|
265
|
+
this.setState(SequencerState.PROPOSER_CHECK, slot);
|
|
266
|
+
const [canPropose, proposer] = await this.checkCanPropose(slot);
|
|
267
|
+
if (canPropose) {
|
|
268
|
+
await this.tryVoteWhenEscapeHatchOpen({ slot, proposer });
|
|
269
|
+
} else {
|
|
270
|
+
this.log.trace(`Escape hatch open but we are not proposer, skipping vote-only actions`, {
|
|
271
|
+
slot,
|
|
272
|
+
epoch,
|
|
273
|
+
proposer,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
281
278
|
|
|
282
|
-
//
|
|
283
|
-
const
|
|
284
|
-
new AppendOnlyTreeSnapshot(chainTipArchive, 1),
|
|
285
|
-
ContentCommitment.empty(),
|
|
286
|
-
StateReference.empty(),
|
|
287
|
-
newGlobalVariables,
|
|
288
|
-
Fr.ZERO,
|
|
289
|
-
Fr.ZERO,
|
|
290
|
-
);
|
|
279
|
+
// Next checkpoint follows from the last synced one
|
|
280
|
+
const checkpointNumber = CheckpointNumber(syncedTo.checkpointNumber + 1);
|
|
291
281
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
282
|
+
const logCtx = {
|
|
283
|
+
now,
|
|
284
|
+
syncedToL2Slot: syncedTo.syncedL2Slot,
|
|
285
|
+
slot,
|
|
286
|
+
slotTs: ts,
|
|
287
|
+
checkpointNumber,
|
|
288
|
+
isPendingChainValid: pick(syncedTo.pendingChainValidationStatus, 'valid', 'reason', 'invalidIndex'),
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// Check that we are a proposer for the next slot
|
|
292
|
+
this.setState(SequencerState.PROPOSER_CHECK, slot);
|
|
293
|
+
const [canPropose, proposer] = await this.checkCanPropose(slot);
|
|
294
|
+
|
|
295
|
+
// If we are not a proposer check if we should invalidate an invalid checkpoint, and bail
|
|
296
|
+
if (!canPropose) {
|
|
297
|
+
await this.considerInvalidatingCheckpoint(syncedTo, slot);
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
298
300
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
this.log.debug(
|
|
305
|
-
`Not enough txs to build block ${newBlockNumber} at slot ${slot}: got ${pendingTxCount} txs, need ${this.minTxsPerBlock}`,
|
|
301
|
+
// Check that the slot is not taken by a block already (should never happen, since only us can propose for this slot)
|
|
302
|
+
if (syncedTo.blockData && syncedTo.blockData.header.getSlot() >= slot) {
|
|
303
|
+
this.log.warn(
|
|
304
|
+
`Cannot propose block at next L2 slot ${slot} since that slot was taken by block ${syncedTo.blockNumber}`,
|
|
305
|
+
{ ...logCtx, block: syncedTo.blockData.header.toInspect() },
|
|
306
306
|
);
|
|
307
|
+
this.metrics.recordCheckpointPrecheckFailed('slot_already_taken');
|
|
308
|
+
return undefined;
|
|
307
309
|
}
|
|
308
310
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
});
|
|
311
|
+
// We now need to get ourselves a publisher.
|
|
312
|
+
// The returned attestor will be the one we provided if we provided one.
|
|
313
|
+
// Otherwise it will be a valid attestor for the returned publisher.
|
|
314
|
+
// In fisherman mode, pass undefined to use the fisherman's own keystore instead of the actual proposer's
|
|
315
|
+
const proposerForPublisher = this.config.fishermanMode ? undefined : proposer;
|
|
316
|
+
const { attestorAddress, publisher } = await this.publisherFactory.create(proposerForPublisher);
|
|
317
|
+
this.log.verbose(`Created publisher at address ${publisher.getSenderAddress()} for attestor ${attestorAddress}`);
|
|
318
|
+
|
|
319
|
+
// In fisherman mode, set the actual proposer's address for simulations
|
|
320
|
+
if (this.config.fishermanMode && proposer) {
|
|
321
|
+
publisher.setProposerAddressForSimulation(proposer);
|
|
322
|
+
this.log.debug(`Set proposer address ${proposer} for simulation in fisherman mode`);
|
|
323
|
+
}
|
|
315
324
|
|
|
316
|
-
|
|
325
|
+
// Prepare invalidation request if the pending chain is invalid (returns undefined if no need)
|
|
326
|
+
const invalidateCheckpoint = await publisher.simulateInvalidateCheckpoint(syncedTo.pendingChainValidationStatus);
|
|
327
|
+
|
|
328
|
+
// Check with the rollup contract if we can indeed propose at the next L2 slot. This check should not fail
|
|
329
|
+
// if all the previous checks are good, but we do it just in case.
|
|
330
|
+
const canProposeCheck = await publisher.canProposeAtNextEthBlock(
|
|
331
|
+
syncedTo.archive,
|
|
332
|
+
proposer ?? EthAddress.ZERO,
|
|
333
|
+
invalidateCheckpoint,
|
|
334
|
+
);
|
|
317
335
|
|
|
318
|
-
if (
|
|
319
|
-
this.
|
|
336
|
+
if (canProposeCheck === undefined) {
|
|
337
|
+
this.log.warn(
|
|
338
|
+
`Cannot propose checkpoint ${checkpointNumber} at slot ${slot} due to failed rollup contract check`,
|
|
339
|
+
logCtx,
|
|
340
|
+
);
|
|
341
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Rollup contract check failed', slot });
|
|
342
|
+
this.metrics.recordCheckpointPrecheckFailed('rollup_contract_check_failed');
|
|
343
|
+
return undefined;
|
|
320
344
|
}
|
|
321
345
|
|
|
322
|
-
|
|
323
|
-
|
|
346
|
+
if (canProposeCheck.slot !== slot) {
|
|
347
|
+
this.log.warn(
|
|
348
|
+
`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}.`,
|
|
349
|
+
{ ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
|
|
350
|
+
);
|
|
351
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Slot mismatch', slot });
|
|
352
|
+
this.metrics.recordCheckpointPrecheckFailed('slot_mismatch');
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
324
355
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
// Re-throw other errors
|
|
334
|
-
throw err;
|
|
335
|
-
}
|
|
336
|
-
} finally {
|
|
337
|
-
this.setState(SequencerState.IDLE, 0n);
|
|
356
|
+
if (canProposeCheck.checkpointNumber !== checkpointNumber) {
|
|
357
|
+
this.log.warn(
|
|
358
|
+
`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}.`,
|
|
359
|
+
{ ...logCtx, rollup: canProposeCheck, expectedSlot: slot },
|
|
360
|
+
);
|
|
361
|
+
this.emit('proposer-rollup-check-failed', { reason: 'Block mismatch', slot });
|
|
362
|
+
this.metrics.recordCheckpointPrecheckFailed('block_number_mismatch');
|
|
363
|
+
return undefined;
|
|
338
364
|
}
|
|
365
|
+
|
|
366
|
+
this.lastSlotForCheckpointProposalJob = slot;
|
|
367
|
+
await this.p2pClient.prepareForSlot(slot);
|
|
368
|
+
this.log.info(`Preparing checkpoint proposal ${checkpointNumber} at slot ${slot}`, { ...logCtx, proposer });
|
|
369
|
+
|
|
370
|
+
// Create and return the checkpoint proposal job
|
|
371
|
+
return this.createCheckpointProposalJob(
|
|
372
|
+
epoch,
|
|
373
|
+
slot,
|
|
374
|
+
checkpointNumber,
|
|
375
|
+
syncedTo.blockNumber,
|
|
376
|
+
proposer,
|
|
377
|
+
publisher,
|
|
378
|
+
attestorAddress,
|
|
379
|
+
invalidateCheckpoint,
|
|
380
|
+
);
|
|
339
381
|
}
|
|
340
382
|
|
|
341
|
-
|
|
342
|
-
|
|
383
|
+
protected createCheckpointProposalJob(
|
|
384
|
+
epoch: EpochNumber,
|
|
385
|
+
slot: SlotNumber,
|
|
386
|
+
checkpointNumber: CheckpointNumber,
|
|
387
|
+
syncedToBlockNumber: BlockNumber,
|
|
388
|
+
proposer: EthAddress | undefined,
|
|
389
|
+
publisher: SequencerPublisher,
|
|
390
|
+
attestorAddress: EthAddress,
|
|
391
|
+
invalidateCheckpoint: InvalidateCheckpointRequest | undefined,
|
|
392
|
+
): CheckpointProposalJob {
|
|
393
|
+
return new CheckpointProposalJob(
|
|
394
|
+
epoch,
|
|
395
|
+
slot,
|
|
396
|
+
checkpointNumber,
|
|
397
|
+
syncedToBlockNumber,
|
|
398
|
+
proposer,
|
|
399
|
+
publisher,
|
|
400
|
+
attestorAddress,
|
|
401
|
+
invalidateCheckpoint,
|
|
402
|
+
this.validatorClient,
|
|
403
|
+
this.globalsBuilder,
|
|
404
|
+
this.p2pClient,
|
|
405
|
+
this.worldState,
|
|
406
|
+
this.l1ToL2MessageSource,
|
|
407
|
+
this.l2BlockSource,
|
|
408
|
+
this.checkpointsBuilder,
|
|
409
|
+
this.l2BlockSource,
|
|
410
|
+
this.l1Constants,
|
|
411
|
+
this.config,
|
|
412
|
+
this.timetable,
|
|
413
|
+
this.slasherClient,
|
|
414
|
+
this.epochCache,
|
|
415
|
+
this.dateProvider,
|
|
416
|
+
this.metrics,
|
|
417
|
+
this,
|
|
418
|
+
this.setState.bind(this),
|
|
419
|
+
this.tracer,
|
|
420
|
+
this.log.getBindings(),
|
|
421
|
+
);
|
|
343
422
|
}
|
|
344
423
|
|
|
345
424
|
/**
|
|
346
|
-
*
|
|
347
|
-
* @param tipArchive - The archive of the previous block.
|
|
348
|
-
* @param proposalBlockNumber - The block number of the proposal.
|
|
349
|
-
* @returns The slot number if we can propose at the next block, otherwise undefined.
|
|
425
|
+
* Returns the current sequencer state.
|
|
350
426
|
*/
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
if (!result) {
|
|
355
|
-
return undefined;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const [slot, blockNumber] = result;
|
|
359
|
-
|
|
360
|
-
if (proposalBlockNumber !== blockNumber) {
|
|
361
|
-
const msg = `Sequencer block number mismatch. Expected ${proposalBlockNumber} but got ${blockNumber}.`;
|
|
362
|
-
this.log.warn(msg);
|
|
363
|
-
throw new Error(msg);
|
|
364
|
-
}
|
|
365
|
-
return slot;
|
|
427
|
+
public getState(): SequencerState {
|
|
428
|
+
return this.state;
|
|
366
429
|
}
|
|
367
430
|
|
|
368
431
|
/**
|
|
369
|
-
*
|
|
432
|
+
* Internal helper for setting the sequencer state and checks if we have enough time left in the slot to transition to the new state.
|
|
370
433
|
* @param proposedState - The new state to transition to.
|
|
371
|
-
* @param
|
|
434
|
+
* @param slotNumber - The current slot number.
|
|
372
435
|
* @param force - Whether to force the transition even if the sequencer is stopped.
|
|
373
|
-
*
|
|
374
|
-
* @dev If the `currentSlotNumber` doesn't matter (e.g. transitioning to IDLE), pass in `0n`;
|
|
375
|
-
* it is only used to check if we have enough time left in the slot to transition to the new state.
|
|
376
436
|
*/
|
|
377
|
-
setState(
|
|
378
|
-
|
|
437
|
+
protected setState(
|
|
438
|
+
proposedState: SequencerState,
|
|
439
|
+
slotNumber: SlotNumber | undefined,
|
|
440
|
+
opts: { force?: boolean } = {},
|
|
441
|
+
): void {
|
|
442
|
+
if (this.state === SequencerState.STOPPING && proposedState !== SequencerState.STOPPED && !opts.force) {
|
|
443
|
+
this.log.warn(`Cannot set sequencer to ${proposedState} as it is stopping.`);
|
|
444
|
+
throw new SequencerInterruptedError();
|
|
445
|
+
}
|
|
446
|
+
if (this.state === SequencerState.STOPPED && !opts.force) {
|
|
379
447
|
this.log.warn(`Cannot set sequencer from ${this.state} to ${proposedState} as it is stopped.`);
|
|
380
448
|
return;
|
|
381
449
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
450
|
+
let secondsIntoSlot = undefined;
|
|
451
|
+
if (slotNumber !== undefined) {
|
|
452
|
+
secondsIntoSlot = this.getSecondsIntoSlot(slotNumber);
|
|
453
|
+
this.timetable.assertTimeLeft(proposedState, secondsIntoSlot);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const boringStates = [SequencerState.IDLE, SequencerState.SYNCHRONIZING];
|
|
457
|
+
const logLevel =
|
|
458
|
+
boringStates.includes(proposedState) && boringStates.includes(this.state)
|
|
459
|
+
? ('trace' as const)
|
|
460
|
+
: ('debug' as const);
|
|
461
|
+
this.log[logLevel](`Transitioning from ${this.state} to ${proposedState}`, { slotNumber, secondsIntoSlot });
|
|
462
|
+
|
|
463
|
+
this.emit('state-changed', {
|
|
464
|
+
oldState: this.state,
|
|
465
|
+
newState: proposedState,
|
|
466
|
+
secondsIntoSlot,
|
|
467
|
+
slot: slotNumber,
|
|
468
|
+
});
|
|
385
469
|
this.state = proposedState;
|
|
386
470
|
}
|
|
387
471
|
|
|
388
472
|
/**
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
* Shared between the sequencer and the validator for re-execution
|
|
392
|
-
*
|
|
393
|
-
* @param pendingTxs - The pending transactions to construct the block from
|
|
394
|
-
* @param newGlobalVariables - The global variables for the new block
|
|
395
|
-
* @param historicalHeader - The historical header of the parent
|
|
396
|
-
* @param opts - Whether to just validate the block as a validator, as opposed to building it as a proposal
|
|
473
|
+
* Returns whether all dependencies have caught up.
|
|
474
|
+
* We don't check against the previous block submitted since it may have been reorg'd out.
|
|
397
475
|
*/
|
|
398
|
-
protected async
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(BigInt(blockNumber));
|
|
407
|
-
const msgCount = l1ToL2Messages.length;
|
|
408
|
-
|
|
409
|
-
this.log.verbose(`Building block ${blockNumber} for slot ${slot}`, {
|
|
410
|
-
slot,
|
|
411
|
-
blockNumber,
|
|
412
|
-
msgCount,
|
|
413
|
-
validator: opts.validateOnly,
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
// Sync to the previous block at least
|
|
417
|
-
await this.worldState.syncImmediate(blockNumber - 1);
|
|
418
|
-
this.log.debug(`Synced to previous block ${blockNumber - 1}`);
|
|
419
|
-
|
|
420
|
-
// NB: separating the dbs because both should update the state
|
|
421
|
-
const publicProcessorFork = await this.worldState.fork();
|
|
422
|
-
const orchestratorFork = await this.worldState.fork();
|
|
423
|
-
|
|
424
|
-
const previousBlockHeader =
|
|
425
|
-
(await this.l2BlockSource.getBlock(blockNumber - 1))?.header ?? orchestratorFork.getInitialHeader();
|
|
426
|
-
|
|
427
|
-
try {
|
|
428
|
-
const processor = this.publicProcessorFactory.create(publicProcessorFork, newGlobalVariables, true);
|
|
429
|
-
const blockBuildingTimer = new Timer();
|
|
430
|
-
const blockBuilder = this.blockBuilderFactory.create(orchestratorFork);
|
|
431
|
-
await blockBuilder.startNewBlock(newGlobalVariables, l1ToL2Messages, previousBlockHeader);
|
|
432
|
-
|
|
433
|
-
// Deadline for processing depends on whether we're proposing a block
|
|
434
|
-
const secondsIntoSlot = this.getSecondsIntoSlot(slot);
|
|
435
|
-
const processingEndTimeWithinSlot = opts.validateOnly
|
|
436
|
-
? this.timetable.getValidatorReexecTimeEnd(secondsIntoSlot)
|
|
437
|
-
: this.timetable.getBlockProposalExecTimeEnd(secondsIntoSlot);
|
|
438
|
-
|
|
439
|
-
// Deadline is only set if enforceTimeTable is enabled.
|
|
440
|
-
const deadline = this.enforceTimeTable
|
|
441
|
-
? new Date((this.getSlotStartTimestamp(slot) + processingEndTimeWithinSlot) * 1000)
|
|
442
|
-
: undefined;
|
|
443
|
-
|
|
444
|
-
this.log.verbose(`Processing pending txs`, {
|
|
476
|
+
protected async checkSync(args: { ts: bigint; slot: SlotNumber }): Promise<SequencerSyncCheckResult | undefined> {
|
|
477
|
+
// Check that the archiver has fully synced the L2 slot before the one we want to propose in.
|
|
478
|
+
// TODO(#14766): Archiver reports L1 timestamp based on L1 blocks seen, which means that a missed L1 block will
|
|
479
|
+
// cause the archiver L1 timestamp to fall behind, and cause this sequencer to start processing one L1 slot later.
|
|
480
|
+
const syncedL2Slot = await this.l2BlockSource.getSyncedL2SlotNumber();
|
|
481
|
+
const { slot } = args;
|
|
482
|
+
if (syncedL2Slot === undefined || syncedL2Slot + 1 < slot) {
|
|
483
|
+
this.log.debug(`Cannot propose block at next L2 slot ${slot} due to pending sync from L1`, {
|
|
445
484
|
slot,
|
|
446
|
-
|
|
447
|
-
now: new Date(this.dateProvider.now()),
|
|
448
|
-
deadline,
|
|
485
|
+
syncedL2Slot,
|
|
449
486
|
});
|
|
487
|
+
return undefined;
|
|
488
|
+
}
|
|
450
489
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
)
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const proposerLimits = {
|
|
462
|
-
maxTransactions: this.maxTxsPerBlock,
|
|
463
|
-
maxBlockSize: this.maxBlockSizeInBytes,
|
|
464
|
-
maxBlockGas: this.maxBlockGas,
|
|
465
|
-
};
|
|
466
|
-
const limits = opts.validateOnly ? { deadline } : { deadline, ...proposerLimits };
|
|
467
|
-
const [publicProcessorDuration, [processedTxs, failedTxs]] = await elapsed(() =>
|
|
468
|
-
processor.process(pendingTxs, limits, validators),
|
|
469
|
-
);
|
|
470
|
-
|
|
471
|
-
if (!opts.validateOnly && failedTxs.length > 0) {
|
|
472
|
-
const failedTxData = failedTxs.map(fail => fail.tx);
|
|
473
|
-
const failedTxHashes = await Tx.getHashes(failedTxData);
|
|
474
|
-
this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
|
|
475
|
-
await this.p2pClient.deleteTxs(failedTxHashes);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (
|
|
479
|
-
!opts.validateOnly && // We check for minTxCount only if we are proposing a block, not if we are validating it
|
|
480
|
-
!this.isFlushing && // And we skip the check when flushing, since we want all pending txs to go out, no matter if too few
|
|
481
|
-
this.minTxsPerBlock !== undefined &&
|
|
482
|
-
processedTxs.length < this.minTxsPerBlock
|
|
483
|
-
) {
|
|
484
|
-
this.log.warn(
|
|
485
|
-
`Block ${blockNumber} has too few txs to be proposed (got ${processedTxs.length} but required ${this.minTxsPerBlock})`,
|
|
486
|
-
{ slot, blockNumber, processedTxCount: processedTxs.length },
|
|
487
|
-
);
|
|
488
|
-
throw new Error(`Block has too few successful txs to be proposed`);
|
|
489
|
-
}
|
|
490
|
+
const syncedBlocks = await Promise.all([
|
|
491
|
+
this.worldState.status().then(({ syncSummary }) => ({
|
|
492
|
+
number: syncSummary.latestBlockNumber,
|
|
493
|
+
hash: syncSummary.latestBlockHash,
|
|
494
|
+
})),
|
|
495
|
+
this.l2BlockSource.getL2Tips().then(t => t.proposed),
|
|
496
|
+
this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
|
|
497
|
+
this.l1ToL2MessageSource.getL2Tips().then(t => t.proposed),
|
|
498
|
+
this.l2BlockSource.getPendingChainValidationStatus(),
|
|
499
|
+
] as const);
|
|
490
500
|
|
|
491
|
-
|
|
492
|
-
await blockBuilder.addTxs(processedTxs);
|
|
493
|
-
const end = process.hrtime.bigint();
|
|
494
|
-
const duration = Number(end - start) / 1_000;
|
|
495
|
-
this.metrics.recordBlockBuilderTreeInsertions(duration);
|
|
501
|
+
const [worldState, l2BlockSource, p2p, l1ToL2MessageSource, pendingChainValidationStatus] = syncedBlocks;
|
|
496
502
|
|
|
497
|
-
|
|
498
|
-
|
|
503
|
+
// 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,
|
|
504
|
+
// as the world state can compute the new genesis block hash, but other components use the hardcoded constant.
|
|
505
|
+
// TODO(palla/mbps): Fix the above. All components should be able to handle dynamic genesis block hashes.
|
|
506
|
+
const result =
|
|
507
|
+
(l2BlockSource.number === 0 && worldState.number === 0 && p2p.number === 0 && l1ToL2MessageSource.number === 0) ||
|
|
508
|
+
(worldState.hash === l2BlockSource.hash &&
|
|
509
|
+
p2p.hash === l2BlockSource.hash &&
|
|
510
|
+
l1ToL2MessageSource.hash === l2BlockSource.hash);
|
|
499
511
|
|
|
500
|
-
|
|
501
|
-
|
|
512
|
+
if (!result) {
|
|
513
|
+
this.log.debug(`Sequencer sync check failed`, { worldState, l2BlockSource, p2p, l1ToL2MessageSource });
|
|
514
|
+
return undefined;
|
|
515
|
+
}
|
|
502
516
|
|
|
517
|
+
// Special case for genesis state
|
|
518
|
+
const blockNumber = worldState.number;
|
|
519
|
+
if (blockNumber < INITIAL_L2_BLOCK_NUM) {
|
|
520
|
+
const archive = new Fr((await this.worldState.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
503
521
|
return {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
numFailedTxs: failedTxs.length,
|
|
510
|
-
blockBuildingTimer,
|
|
522
|
+
checkpointNumber: CheckpointNumber.ZERO,
|
|
523
|
+
blockNumber: BlockNumber.ZERO,
|
|
524
|
+
archive,
|
|
525
|
+
syncedL2Slot,
|
|
526
|
+
pendingChainValidationStatus,
|
|
511
527
|
};
|
|
512
|
-
} finally {
|
|
513
|
-
// We create a fresh processor each time to reset any cached state (eg storage writes)
|
|
514
|
-
// We wait a bit to close the forks since the processor may still be working on a dangling tx
|
|
515
|
-
// which was interrupted due to the processingDeadline being hit.
|
|
516
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
517
|
-
setTimeout(async () => {
|
|
518
|
-
try {
|
|
519
|
-
await publicProcessorFork.close();
|
|
520
|
-
await orchestratorFork.close();
|
|
521
|
-
} catch (err) {
|
|
522
|
-
// This can happen if the sequencer is stopped before we hit this timeout.
|
|
523
|
-
this.log.warn(`Error closing forks for block processing`, err);
|
|
524
|
-
}
|
|
525
|
-
}, 5000);
|
|
526
528
|
}
|
|
529
|
+
|
|
530
|
+
const blockData = await this.l2BlockSource.getBlockData(blockNumber);
|
|
531
|
+
if (!blockData) {
|
|
532
|
+
// this shouldn't really happen because a moment ago we checked that all components were in sync
|
|
533
|
+
this.log.error(`Failed to get L2 block data ${blockNumber} from the archiver with all components in sync`);
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return {
|
|
538
|
+
blockData,
|
|
539
|
+
blockNumber: blockData.header.getBlockNumber(),
|
|
540
|
+
checkpointNumber: blockData.checkpointNumber,
|
|
541
|
+
archive: blockData.archive.root,
|
|
542
|
+
syncedL2Slot,
|
|
543
|
+
pendingChainValidationStatus,
|
|
544
|
+
};
|
|
527
545
|
}
|
|
528
546
|
|
|
529
547
|
/**
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* @dev MUST throw instead of exiting early to ensure that world-state
|
|
533
|
-
* is being rolled back if the block is dropped.
|
|
534
|
-
*
|
|
535
|
-
* @param pendingTxs - Iterable of pending transactions to construct the block from
|
|
536
|
-
* @param proposalHeader - The partial header constructed for the proposal
|
|
548
|
+
* Checks if we are the proposer for the next slot.
|
|
549
|
+
* @returns True if we can propose, and the proposer address (undefined if anyone can propose)
|
|
537
550
|
*/
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}))
|
|
541
|
-
private async buildBlockAndEnqueuePublish(
|
|
542
|
-
pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
|
|
543
|
-
proposalHeader: BlockHeader,
|
|
544
|
-
): Promise<void> {
|
|
545
|
-
await this.publisher.validateBlockForSubmission(proposalHeader);
|
|
546
|
-
|
|
547
|
-
const newGlobalVariables = proposalHeader.globalVariables;
|
|
548
|
-
const blockNumber = newGlobalVariables.blockNumber.toNumber();
|
|
549
|
-
const slot = newGlobalVariables.slotNumber.toBigInt();
|
|
550
|
-
|
|
551
|
-
// this.metrics.recordNewBlock(blockNumber, validTxs.length);
|
|
552
|
-
const workTimer = new Timer();
|
|
553
|
-
this.setState(SequencerState.CREATING_BLOCK, slot);
|
|
551
|
+
protected async checkCanPropose(slot: SlotNumber): Promise<[boolean, EthAddress | undefined]> {
|
|
552
|
+
let proposer: EthAddress | undefined;
|
|
554
553
|
|
|
555
554
|
try {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
publicProcessDuration: publicProcessorDuration,
|
|
569
|
-
rollupCircuitsDuration: blockBuildingTimer.ms(),
|
|
570
|
-
...block.getStats(),
|
|
571
|
-
};
|
|
555
|
+
proposer = await this.epochCache.getProposerAttesterAddressInSlot(slot);
|
|
556
|
+
} catch (e) {
|
|
557
|
+
if (e instanceof NoCommitteeError) {
|
|
558
|
+
if (this.lastSlotForNoCommitteeWarning !== slot) {
|
|
559
|
+
this.lastSlotForNoCommitteeWarning = slot;
|
|
560
|
+
this.log.warn(`Cannot propose at next L2 slot ${slot} since the committee does not exist on L1`);
|
|
561
|
+
}
|
|
562
|
+
return [false, undefined];
|
|
563
|
+
}
|
|
564
|
+
this.log.error(`Error getting proposer for slot ${slot}`, e);
|
|
565
|
+
return [false, undefined];
|
|
566
|
+
}
|
|
572
567
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
globalVariables: block.header.globalVariables.toInspect(),
|
|
582
|
-
txHashes,
|
|
583
|
-
...blockStats,
|
|
584
|
-
},
|
|
585
|
-
);
|
|
568
|
+
// If proposer is undefined, then the committee is empty and anyone may propose
|
|
569
|
+
if (proposer === undefined) {
|
|
570
|
+
return [true, undefined];
|
|
571
|
+
}
|
|
572
|
+
// In fisherman mode, just return the current proposer
|
|
573
|
+
if (this.config.fishermanMode) {
|
|
574
|
+
return [true, proposer];
|
|
575
|
+
}
|
|
586
576
|
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
const attestations = await this.collectAttestations(block, txHashes);
|
|
590
|
-
if (attestations !== undefined) {
|
|
591
|
-
this.log.verbose(`Collected ${attestations.length} attestations`, { blockHash, blockNumber });
|
|
592
|
-
}
|
|
593
|
-
stopCollectingAttestationsTimer();
|
|
577
|
+
const validatorAddresses = this.validatorClient.getValidatorAddresses();
|
|
578
|
+
const weAreProposer = validatorAddresses.some(addr => addr.equals(proposer));
|
|
594
579
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
throw err;
|
|
580
|
+
if (!weAreProposer) {
|
|
581
|
+
this.log.debug(`Cannot propose at slot ${slot} since we are not a proposer`, { validatorAddresses, proposer });
|
|
582
|
+
return [false, proposer];
|
|
599
583
|
}
|
|
584
|
+
|
|
585
|
+
return [true, proposer];
|
|
600
586
|
}
|
|
601
587
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
}))
|
|
607
|
-
protected async
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
if (
|
|
612
|
-
this.log.
|
|
613
|
-
return
|
|
614
|
-
} else {
|
|
615
|
-
this.log.debug(`Attesting committee length is ${committee.length}`);
|
|
588
|
+
/**
|
|
589
|
+
* Tries to vote on slashing actions and governance when the sync check fails but we're past the max time for initializing a proposal.
|
|
590
|
+
* This allows the sequencer to participate in governance/slashing votes even when it cannot build blocks.
|
|
591
|
+
*/
|
|
592
|
+
@trackSpan('Seqeuencer.tryVoteWhenSyncFails', ({ slot }) => ({ [Attributes.SLOT_NUMBER]: slot }))
|
|
593
|
+
protected async tryVoteWhenSyncFails(args: { slot: SlotNumber; ts: bigint }): Promise<void> {
|
|
594
|
+
const { slot } = args;
|
|
595
|
+
|
|
596
|
+
// Prevent duplicate attempts in the same slot
|
|
597
|
+
if (this.lastSlotForFallbackVote === slot) {
|
|
598
|
+
this.log.trace(`Already attempted to vote in slot ${slot} (skipping)`);
|
|
599
|
+
return;
|
|
616
600
|
}
|
|
617
601
|
|
|
618
|
-
if
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
602
|
+
// Check if we're past the max time for initializing a proposal
|
|
603
|
+
const secondsIntoSlot = this.getSecondsIntoSlot(slot);
|
|
604
|
+
const maxAllowedTime = this.timetable.getMaxAllowedTime(SequencerState.INITIALIZING_CHECKPOINT);
|
|
605
|
+
|
|
606
|
+
// If we haven't exceeded the time limit for initializing a proposal, don't proceed with voting
|
|
607
|
+
// We use INITIALIZING_PROPOSAL time limit because if we're past that, we can't build a block anyway
|
|
608
|
+
if (maxAllowedTime === undefined || secondsIntoSlot <= maxAllowedTime) {
|
|
609
|
+
this.log.trace(`Not attempting to vote since there is still time for block building`, {
|
|
610
|
+
secondsIntoSlot,
|
|
611
|
+
maxAllowedTime,
|
|
612
|
+
});
|
|
613
|
+
return;
|
|
622
614
|
}
|
|
623
615
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
616
|
+
this.log.trace(`Sync for slot ${slot} failed, checking for voting opportunities`, {
|
|
617
|
+
secondsIntoSlot,
|
|
618
|
+
maxAllowedTime,
|
|
619
|
+
});
|
|
627
620
|
|
|
628
|
-
|
|
629
|
-
const
|
|
630
|
-
if (!
|
|
631
|
-
|
|
632
|
-
|
|
621
|
+
// Check if we're a proposer or proposal is open
|
|
622
|
+
const [canPropose, proposer] = await this.checkCanPropose(slot);
|
|
623
|
+
if (!canPropose) {
|
|
624
|
+
this.log.trace(`Cannot vote in slot ${slot} since we are not a proposer`, { slot, proposer });
|
|
625
|
+
return;
|
|
633
626
|
}
|
|
634
627
|
|
|
635
|
-
this
|
|
636
|
-
this.
|
|
628
|
+
// Mark this slot as attempted
|
|
629
|
+
this.lastSlotForFallbackVote = slot;
|
|
630
|
+
|
|
631
|
+
// Get a publisher for voting
|
|
632
|
+
const { attestorAddress, publisher } = await this.publisherFactory.create(proposer);
|
|
637
633
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
634
|
+
this.log.debug(`Attempting to vote despite sync failure at slot ${slot}`, {
|
|
635
|
+
attestorAddress,
|
|
636
|
+
slot,
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
// Enqueue governance and slashing votes
|
|
640
|
+
const voter = new CheckpointVoter(
|
|
641
|
+
slot,
|
|
642
|
+
publisher,
|
|
643
|
+
attestorAddress,
|
|
644
|
+
this.validatorClient,
|
|
645
|
+
this.slasherClient,
|
|
646
|
+
this.l1Constants,
|
|
647
|
+
this.config,
|
|
648
|
+
this.metrics,
|
|
649
|
+
this.log,
|
|
646
650
|
);
|
|
651
|
+
const votesPromises = voter.enqueueVotes();
|
|
652
|
+
const votes = await Promise.all(votesPromises);
|
|
653
|
+
|
|
654
|
+
if (votes.every(p => !p)) {
|
|
655
|
+
this.log.debug(`No votes to enqueue for slot ${slot}`);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
647
658
|
|
|
648
|
-
|
|
649
|
-
|
|
659
|
+
this.log.info(`Voting in slot ${slot} despite sync failure`, { slot });
|
|
660
|
+
await publisher.sendRequests();
|
|
650
661
|
}
|
|
651
662
|
|
|
652
663
|
/**
|
|
653
|
-
*
|
|
654
|
-
*
|
|
664
|
+
* Tries to vote on slashing actions and governance proposals when escape hatch is open.
|
|
665
|
+
* This allows the sequencer to participate in voting without performing checkpoint proposal work.
|
|
655
666
|
*/
|
|
656
|
-
@trackSpan('Sequencer.
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
667
|
+
@trackSpan('Sequencer.tryVoteWhenEscapeHatchOpen', ({ slot }) => ({ [Attributes.SLOT_NUMBER]: slot }))
|
|
668
|
+
protected async tryVoteWhenEscapeHatchOpen(args: {
|
|
669
|
+
slot: SlotNumber;
|
|
670
|
+
proposer: EthAddress | undefined;
|
|
671
|
+
}): Promise<void> {
|
|
672
|
+
const { slot, proposer } = args;
|
|
673
|
+
|
|
674
|
+
// Prevent duplicate attempts in the same slot
|
|
675
|
+
if (this.lastSlotForFallbackVote === slot) {
|
|
676
|
+
this.log.trace(`Already attempted to vote in slot ${slot} (escape hatch open, skipping)`);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
666
679
|
|
|
667
|
-
//
|
|
668
|
-
|
|
669
|
-
const txTimeoutAt = new Date((this.getSlotStartTimestamp(slot) + this.aztecSlotDuration) * 1000);
|
|
680
|
+
// Mark this slot as attempted
|
|
681
|
+
this.lastSlotForFallbackVote = slot;
|
|
670
682
|
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
});
|
|
683
|
+
const { attestorAddress, publisher } = await this.publisherFactory.create(proposer);
|
|
684
|
+
|
|
685
|
+
this.log.debug(`Escape hatch open for slot ${slot}, attempting vote-only actions`, { slot, attestorAddress });
|
|
686
|
+
|
|
687
|
+
const voter = new CheckpointVoter(
|
|
688
|
+
slot,
|
|
689
|
+
publisher,
|
|
690
|
+
attestorAddress,
|
|
691
|
+
this.validatorClient,
|
|
692
|
+
this.slasherClient,
|
|
693
|
+
this.l1Constants,
|
|
694
|
+
this.config,
|
|
695
|
+
this.metrics,
|
|
696
|
+
this.log,
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
const votesPromises = voter.enqueueVotes();
|
|
700
|
+
const votes = await Promise.all(votesPromises);
|
|
674
701
|
|
|
675
|
-
if (!
|
|
676
|
-
|
|
702
|
+
if (votes.every(p => !p)) {
|
|
703
|
+
this.log.debug(`No votes to enqueue for slot ${slot} (escape hatch open)`);
|
|
704
|
+
return;
|
|
677
705
|
}
|
|
706
|
+
|
|
707
|
+
this.log.info(`Voting in slot ${slot} (escape hatch open)`, { slot });
|
|
708
|
+
await publisher.sendRequests();
|
|
678
709
|
}
|
|
679
710
|
|
|
680
711
|
/**
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
*
|
|
712
|
+
* Considers invalidating a block if the pending chain is invalid. Depends on how long the invalid block
|
|
713
|
+
* has been there without being invalidated and whether the sequencer is in the committee or not. We always
|
|
714
|
+
* have the proposer try to invalidate, but if they fail, the sequencers in the committee are expected to try,
|
|
715
|
+
* and if they fail, any sequencer will try as well.
|
|
684
716
|
*/
|
|
685
|
-
protected async
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
this.l2BlockSource.getL2Tips().then(t => t.latest),
|
|
694
|
-
this.p2pClient.getStatus().then(p2p => p2p.syncedToL2Block),
|
|
695
|
-
this.l1ToL2MessageSource.getBlockNumber(),
|
|
696
|
-
] as const);
|
|
717
|
+
protected async considerInvalidatingCheckpoint(
|
|
718
|
+
syncedTo: SequencerSyncCheckResult,
|
|
719
|
+
currentSlot: SlotNumber,
|
|
720
|
+
): Promise<void> {
|
|
721
|
+
const { pendingChainValidationStatus, syncedL2Slot } = syncedTo;
|
|
722
|
+
if (pendingChainValidationStatus.valid) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
697
725
|
|
|
698
|
-
const
|
|
726
|
+
const invalidCheckpointNumber = pendingChainValidationStatus.checkpoint.checkpointNumber;
|
|
727
|
+
const invalidCheckpointTimestamp = pendingChainValidationStatus.checkpoint.timestamp;
|
|
728
|
+
const timeSinceChainInvalid = this.dateProvider.nowInSeconds() - Number(invalidCheckpointTimestamp);
|
|
729
|
+
const ourValidatorAddresses = this.validatorClient.getValidatorAddresses();
|
|
730
|
+
|
|
731
|
+
const { secondsBeforeInvalidatingBlockAsCommitteeMember, secondsBeforeInvalidatingBlockAsNonCommitteeMember } =
|
|
732
|
+
this.config;
|
|
733
|
+
|
|
734
|
+
const logData = {
|
|
735
|
+
invalidL1Timestamp: invalidCheckpointTimestamp,
|
|
736
|
+
syncedL2Slot,
|
|
737
|
+
invalidCheckpoint: pendingChainValidationStatus.checkpoint,
|
|
738
|
+
secondsBeforeInvalidatingBlockAsCommitteeMember,
|
|
739
|
+
secondsBeforeInvalidatingBlockAsNonCommitteeMember,
|
|
740
|
+
ourValidatorAddresses,
|
|
741
|
+
currentSlot,
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
const inCurrentCommittee = () =>
|
|
745
|
+
this.epochCache
|
|
746
|
+
.getCommittee(currentSlot)
|
|
747
|
+
.then(c => c?.committee?.some(member => ourValidatorAddresses.some(addr => addr.equals(member))));
|
|
748
|
+
|
|
749
|
+
const invalidateAsCommitteeMember =
|
|
750
|
+
secondsBeforeInvalidatingBlockAsCommitteeMember !== undefined &&
|
|
751
|
+
secondsBeforeInvalidatingBlockAsCommitteeMember > 0 &&
|
|
752
|
+
timeSinceChainInvalid > secondsBeforeInvalidatingBlockAsCommitteeMember &&
|
|
753
|
+
(await inCurrentCommittee());
|
|
754
|
+
|
|
755
|
+
const invalidateAsNonCommitteeMember =
|
|
756
|
+
secondsBeforeInvalidatingBlockAsNonCommitteeMember !== undefined &&
|
|
757
|
+
secondsBeforeInvalidatingBlockAsNonCommitteeMember > 0 &&
|
|
758
|
+
timeSinceChainInvalid > secondsBeforeInvalidatingBlockAsNonCommitteeMember;
|
|
759
|
+
|
|
760
|
+
if (!invalidateAsCommitteeMember && !invalidateAsNonCommitteeMember) {
|
|
761
|
+
this.log.debug(`Not invalidating pending chain`, logData);
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
699
764
|
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
//
|
|
703
|
-
|
|
704
|
-
(
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
l2BlockSourceNumber: l2BlockSource.number,
|
|
716
|
-
l2BlockSourceHash: l2BlockSource.hash,
|
|
717
|
-
p2pNumber: p2p.number,
|
|
718
|
-
p2pHash: p2p.hash,
|
|
719
|
-
l1ToL2MessageSourceNumber: l1ToL2MessageSource,
|
|
720
|
-
});
|
|
765
|
+
let validatorToUse: EthAddress;
|
|
766
|
+
if (invalidateAsCommitteeMember) {
|
|
767
|
+
// When invalidating as a committee member, use first validator that's actually in the committee
|
|
768
|
+
const { committee } = await this.epochCache.getCommittee(currentSlot);
|
|
769
|
+
if (committee) {
|
|
770
|
+
const committeeSet = new Set(committee.map(addr => addr.toString()));
|
|
771
|
+
validatorToUse =
|
|
772
|
+
ourValidatorAddresses.find(addr => committeeSet.has(addr.toString())) ?? ourValidatorAddresses[0];
|
|
773
|
+
} else {
|
|
774
|
+
validatorToUse = ourValidatorAddresses[0];
|
|
775
|
+
}
|
|
776
|
+
} else {
|
|
777
|
+
// When invalidating as a non-committee member, use the first validator
|
|
778
|
+
validatorToUse = ourValidatorAddresses[0];
|
|
779
|
+
}
|
|
721
780
|
|
|
722
|
-
|
|
723
|
-
|
|
781
|
+
const { publisher } = await this.publisherFactory.create(validatorToUse);
|
|
782
|
+
|
|
783
|
+
const invalidateCheckpoint = await publisher.simulateInvalidateCheckpoint(pendingChainValidationStatus);
|
|
784
|
+
if (!invalidateCheckpoint) {
|
|
785
|
+
this.log.warn(`Failed to simulate invalidate checkpoint`, logData);
|
|
786
|
+
return;
|
|
724
787
|
}
|
|
725
|
-
if (worldState.number >= INITIAL_L2_BLOCK_NUM) {
|
|
726
|
-
const block = await this.l2BlockSource.getBlock(worldState.number);
|
|
727
|
-
if (!block) {
|
|
728
|
-
// this shouldn't really happen because a moment ago we checked that all components were in synch
|
|
729
|
-
return undefined;
|
|
730
|
-
}
|
|
731
788
|
|
|
732
|
-
|
|
789
|
+
this.log.info(
|
|
790
|
+
invalidateAsCommitteeMember
|
|
791
|
+
? `Invalidating checkpoint ${invalidCheckpointNumber} as committee member`
|
|
792
|
+
: `Invalidating checkpoint ${invalidCheckpointNumber} as non-committee member`,
|
|
793
|
+
logData,
|
|
794
|
+
);
|
|
795
|
+
|
|
796
|
+
publisher.enqueueInvalidateCheckpoint(invalidateCheckpoint);
|
|
797
|
+
|
|
798
|
+
if (!this.config.fishermanMode) {
|
|
799
|
+
await publisher.sendRequests();
|
|
733
800
|
} else {
|
|
734
|
-
|
|
735
|
-
|
|
801
|
+
this.log.info('Invalidating checkpoint in fisherman mode, clearing pending requests');
|
|
802
|
+
publisher.clearPendingRequests();
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
private logStrategyComparison(epoch: EpochNumber, publisher: SequencerPublisher): void {
|
|
807
|
+
const feeAnalyzer = publisher.getL1FeeAnalyzer();
|
|
808
|
+
if (!feeAnalyzer) {
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
const comparison = feeAnalyzer.getStrategyComparison();
|
|
813
|
+
if (comparison.length === 0) {
|
|
814
|
+
this.log.debug(`No strategy data available yet for epoch ${epoch}`);
|
|
815
|
+
return;
|
|
736
816
|
}
|
|
817
|
+
|
|
818
|
+
this.log.info(`L1 Fee Strategy Performance Report - End of Epoch ${epoch}`, {
|
|
819
|
+
epoch: Number(epoch),
|
|
820
|
+
totalAnalyses: comparison[0]?.totalAnalyses,
|
|
821
|
+
strategies: comparison.map(s => ({
|
|
822
|
+
id: s.strategyId,
|
|
823
|
+
name: s.strategyName,
|
|
824
|
+
inclusionRate: `${(s.inclusionRate * 100).toFixed(1)}%`,
|
|
825
|
+
inclusionCount: `${s.inclusionCount}/${s.totalAnalyses}`,
|
|
826
|
+
avgCostEth: s.avgEstimatedCostEth.toFixed(6),
|
|
827
|
+
totalCostEth: s.totalEstimatedCostEth.toFixed(6),
|
|
828
|
+
avgOverpaymentEth: s.avgOverpaymentEth.toFixed(6),
|
|
829
|
+
totalOverpaymentEth: s.totalOverpaymentEth.toFixed(6),
|
|
830
|
+
avgPriorityFeeDeltaGwei: s.avgPriorityFeeDeltaGwei.toFixed(2),
|
|
831
|
+
})),
|
|
832
|
+
});
|
|
737
833
|
}
|
|
738
834
|
|
|
739
|
-
private
|
|
740
|
-
return
|
|
835
|
+
private getSlotStartBuildTimestamp(slotNumber: SlotNumber): number {
|
|
836
|
+
return getSlotStartBuildTimestamp(slotNumber, this.l1Constants);
|
|
741
837
|
}
|
|
742
838
|
|
|
743
|
-
private getSecondsIntoSlot(slotNumber:
|
|
744
|
-
const slotStartTimestamp = this.
|
|
839
|
+
private getSecondsIntoSlot(slotNumber: SlotNumber): number {
|
|
840
|
+
const slotStartTimestamp = this.getSlotStartBuildTimestamp(slotNumber);
|
|
745
841
|
return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
|
|
746
842
|
}
|
|
747
843
|
|
|
748
|
-
get aztecSlotDuration() {
|
|
844
|
+
public get aztecSlotDuration() {
|
|
749
845
|
return this.l1Constants.slotDuration;
|
|
750
846
|
}
|
|
751
847
|
|
|
752
|
-
get
|
|
753
|
-
return this.
|
|
848
|
+
public get maxL2BlockGas(): number | undefined {
|
|
849
|
+
return this.config.maxL2BlockGas;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
public getSlasherClient(): SlasherClientInterface | undefined {
|
|
853
|
+
return this.slasherClient;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
public get tracer(): Tracer {
|
|
857
|
+
return this.metrics.tracer;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
public getValidatorAddresses() {
|
|
861
|
+
return this.validatorClient?.getValidatorAddresses();
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/** Updates the publisher factory's node keystore adapter after a keystore reload. */
|
|
865
|
+
public updatePublisherNodeKeyStore(adapter: NodeKeystoreAdapter): void {
|
|
866
|
+
this.publisherFactory.updateNodeKeyStore(adapter);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
public getConfig() {
|
|
870
|
+
return this.config;
|
|
754
871
|
}
|
|
755
872
|
|
|
756
|
-
get
|
|
757
|
-
return this.
|
|
873
|
+
private get l1PublishingTime(): number {
|
|
874
|
+
return this.config.l1PublishingTime ?? this.l1Constants.ethereumSlotDuration;
|
|
758
875
|
}
|
|
759
876
|
}
|
|
877
|
+
|
|
878
|
+
type SequencerSyncCheckResult = {
|
|
879
|
+
blockData?: BlockData;
|
|
880
|
+
checkpointNumber: CheckpointNumber;
|
|
881
|
+
blockNumber: BlockNumber;
|
|
882
|
+
archive: Fr;
|
|
883
|
+
syncedL2Slot: SlotNumber;
|
|
884
|
+
pendingChainValidationStatus: ValidateCheckpointResult;
|
|
885
|
+
};
|