@aztec/sequencer-client 0.0.0-test.0
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/README.md +45 -0
- package/dest/client/index.d.ts +2 -0
- package/dest/client/index.d.ts.map +1 -0
- package/dest/client/index.js +1 -0
- package/dest/client/sequencer-client.d.ts +71 -0
- package/dest/client/sequencer-client.d.ts.map +1 -0
- package/dest/client/sequencer-client.js +117 -0
- package/dest/config.d.ts +29 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +143 -0
- package/dest/global_variable_builder/global_builder.d.ts +32 -0
- package/dest/global_variable_builder/global_builder.d.ts.map +1 -0
- package/dest/global_variable_builder/global_builder.js +79 -0
- package/dest/global_variable_builder/index.d.ts +2 -0
- package/dest/global_variable_builder/index.d.ts.map +1 -0
- package/dest/global_variable_builder/index.js +1 -0
- package/dest/index.d.ts +8 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +9 -0
- package/dest/publisher/config.d.ts +31 -0
- package/dest/publisher/config.d.ts.map +1 -0
- package/dest/publisher/config.js +35 -0
- package/dest/publisher/index.d.ts +2 -0
- package/dest/publisher/index.d.ts.map +1 -0
- package/dest/publisher/index.js +1 -0
- package/dest/publisher/sequencer-publisher-metrics.d.ts +25 -0
- package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -0
- package/dest/publisher/sequencer-publisher-metrics.js +129 -0
- package/dest/publisher/sequencer-publisher.d.ts +152 -0
- package/dest/publisher/sequencer-publisher.d.ts.map +1 -0
- package/dest/publisher/sequencer-publisher.js +481 -0
- package/dest/sequencer/allowed.d.ts +3 -0
- package/dest/sequencer/allowed.d.ts.map +1 -0
- package/dest/sequencer/allowed.js +27 -0
- package/dest/sequencer/config.d.ts +2 -0
- package/dest/sequencer/config.d.ts.map +1 -0
- package/dest/sequencer/config.js +1 -0
- package/dest/sequencer/index.d.ts +4 -0
- package/dest/sequencer/index.d.ts.map +1 -0
- package/dest/sequencer/index.js +3 -0
- package/dest/sequencer/metrics.d.ts +24 -0
- package/dest/sequencer/metrics.d.ts.map +1 -0
- package/dest/sequencer/metrics.js +102 -0
- package/dest/sequencer/sequencer.d.ts +180 -0
- package/dest/sequencer/sequencer.d.ts.map +1 -0
- package/dest/sequencer/sequencer.js +623 -0
- package/dest/sequencer/timetable.d.ts +38 -0
- package/dest/sequencer/timetable.d.ts.map +1 -0
- package/dest/sequencer/timetable.js +110 -0
- package/dest/sequencer/utils.d.ts +48 -0
- package/dest/sequencer/utils.d.ts.map +1 -0
- package/dest/sequencer/utils.js +53 -0
- package/dest/slasher/factory.d.ts +7 -0
- package/dest/slasher/factory.d.ts.map +1 -0
- package/dest/slasher/factory.js +8 -0
- package/dest/slasher/index.d.ts +3 -0
- package/dest/slasher/index.d.ts.map +1 -0
- package/dest/slasher/index.js +2 -0
- package/dest/slasher/slasher_client.d.ts +75 -0
- package/dest/slasher/slasher_client.d.ts.map +1 -0
- package/dest/slasher/slasher_client.js +132 -0
- package/dest/test/index.d.ts +17 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +10 -0
- package/dest/tx_validator/archive_cache.d.ts +14 -0
- package/dest/tx_validator/archive_cache.d.ts.map +1 -0
- package/dest/tx_validator/archive_cache.js +22 -0
- package/dest/tx_validator/gas_validator.d.ts +14 -0
- package/dest/tx_validator/gas_validator.d.ts.map +1 -0
- package/dest/tx_validator/gas_validator.js +78 -0
- package/dest/tx_validator/nullifier_cache.d.ts +16 -0
- package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
- package/dest/tx_validator/nullifier_cache.js +24 -0
- package/dest/tx_validator/phases_validator.d.ts +12 -0
- package/dest/tx_validator/phases_validator.d.ts.map +1 -0
- package/dest/tx_validator/phases_validator.js +80 -0
- package/dest/tx_validator/test_utils.d.ts +23 -0
- package/dest/tx_validator/test_utils.d.ts.map +1 -0
- package/dest/tx_validator/test_utils.js +26 -0
- package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
- package/dest/tx_validator/tx_validator_factory.js +50 -0
- package/package.json +121 -0
- package/src/client/index.ts +1 -0
- package/src/client/sequencer-client.ts +219 -0
- package/src/config.ts +179 -0
- package/src/global_variable_builder/global_builder.ts +108 -0
- package/src/global_variable_builder/index.ts +1 -0
- package/src/index.ts +10 -0
- package/src/publisher/config.ts +75 -0
- package/src/publisher/index.ts +1 -0
- package/src/publisher/sequencer-publisher-metrics.ts +176 -0
- package/src/publisher/sequencer-publisher.ts +625 -0
- package/src/sequencer/allowed.ts +36 -0
- package/src/sequencer/config.ts +1 -0
- package/src/sequencer/index.ts +3 -0
- package/src/sequencer/metrics.ts +137 -0
- package/src/sequencer/sequencer.ts +759 -0
- package/src/sequencer/timetable.ts +123 -0
- package/src/sequencer/utils.ts +74 -0
- package/src/slasher/factory.ts +15 -0
- package/src/slasher/index.ts +2 -0
- package/src/slasher/slasher_client.ts +193 -0
- package/src/test/index.ts +20 -0
- package/src/tx_validator/archive_cache.ts +28 -0
- package/src/tx_validator/gas_validator.ts +101 -0
- package/src/tx_validator/nullifier_cache.ts +30 -0
- package/src/tx_validator/phases_validator.ts +98 -0
- package/src/tx_validator/test_utils.ts +48 -0
- package/src/tx_validator/tx_validator_factory.ts +120 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/aztec.js';
|
|
2
|
+
|
|
3
|
+
import type { SequencerMetrics } from './metrics.js';
|
|
4
|
+
import { SequencerState } from './utils.js';
|
|
5
|
+
|
|
6
|
+
export class SequencerTimetable {
|
|
7
|
+
/** How late into the slot can we be to start working */
|
|
8
|
+
public readonly initialTime = 3;
|
|
9
|
+
|
|
10
|
+
/** How long it takes to get ready to start building */
|
|
11
|
+
public readonly blockPrepareTime = 1;
|
|
12
|
+
|
|
13
|
+
/** How long it takes to for proposals and attestations to travel across the p2p layer (one-way) */
|
|
14
|
+
public readonly attestationPropagationTime = 2;
|
|
15
|
+
|
|
16
|
+
/** How much time we spend validating and processing a block after building it, and assembling the proposal to send to attestors */
|
|
17
|
+
public readonly blockValidationTime = 1;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How long it takes to get a published block into L1. L1 builders typically accept txs up to 4 seconds into their slot,
|
|
21
|
+
* but we'll timeout sooner to give it more time to propagate (remember we also have blobs!). Still, when working in anvil,
|
|
22
|
+
* we can just post in the very last second of the L1 slot and still expect the tx to be accepted.
|
|
23
|
+
*/
|
|
24
|
+
public readonly l1PublishingTime;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
private readonly ethereumSlotDuration: number,
|
|
28
|
+
private readonly aztecSlotDuration: number,
|
|
29
|
+
private readonly maxL1TxInclusionTimeIntoSlot: number,
|
|
30
|
+
private readonly enforce: boolean = true,
|
|
31
|
+
private readonly metrics?: SequencerMetrics,
|
|
32
|
+
private readonly log = createLogger('sequencer:timetable'),
|
|
33
|
+
) {
|
|
34
|
+
this.l1PublishingTime = this.ethereumSlotDuration - this.maxL1TxInclusionTimeIntoSlot;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private get afterBlockBuildingTimeNeededWithoutReexec() {
|
|
38
|
+
return this.blockValidationTime + this.attestationPropagationTime * 2 + this.l1PublishingTime;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getBlockProposalExecTimeEnd(secondsIntoSlot: number): number {
|
|
42
|
+
// We are N seconds into the slot. We need to account for `afterBlockBuildingTimeNeededWithoutReexec` seconds,
|
|
43
|
+
// send then split the remaining time between the re-execution and the block building.
|
|
44
|
+
const maxAllowed = this.aztecSlotDuration - this.afterBlockBuildingTimeNeededWithoutReexec;
|
|
45
|
+
const available = maxAllowed - secondsIntoSlot;
|
|
46
|
+
const executionTimeEnd = secondsIntoSlot + available / 2;
|
|
47
|
+
this.log.debug(`Block proposal execution time deadline is ${executionTimeEnd}`, {
|
|
48
|
+
secondsIntoSlot,
|
|
49
|
+
maxAllowed,
|
|
50
|
+
available,
|
|
51
|
+
executionTimeEnd,
|
|
52
|
+
});
|
|
53
|
+
return executionTimeEnd;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private get afterBlockReexecTimeNeeded() {
|
|
57
|
+
return this.attestationPropagationTime + this.l1PublishingTime;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public getValidatorReexecTimeEnd(secondsIntoSlot: number): number {
|
|
61
|
+
// We need to leave for `afterBlockReexecTimeNeeded` seconds available.
|
|
62
|
+
const validationTimeEnd = this.aztecSlotDuration - this.afterBlockReexecTimeNeeded;
|
|
63
|
+
this.log.debug(`Validator re-execution time deadline is ${validationTimeEnd}`, {
|
|
64
|
+
secondsIntoSlot,
|
|
65
|
+
validationTimeEnd,
|
|
66
|
+
});
|
|
67
|
+
return validationTimeEnd;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public getMaxAllowedTime(state: SequencerState): number | undefined {
|
|
71
|
+
switch (state) {
|
|
72
|
+
case SequencerState.STOPPED:
|
|
73
|
+
case SequencerState.IDLE:
|
|
74
|
+
case SequencerState.SYNCHRONIZING:
|
|
75
|
+
case SequencerState.PROPOSER_CHECK:
|
|
76
|
+
return; // We don't really care about times for this states
|
|
77
|
+
case SequencerState.INITIALIZING_PROPOSAL:
|
|
78
|
+
return this.initialTime;
|
|
79
|
+
case SequencerState.CREATING_BLOCK:
|
|
80
|
+
return this.initialTime + this.blockPrepareTime;
|
|
81
|
+
case SequencerState.COLLECTING_ATTESTATIONS:
|
|
82
|
+
return this.aztecSlotDuration - this.l1PublishingTime - 2 * this.attestationPropagationTime;
|
|
83
|
+
case SequencerState.PUBLISHING_BLOCK:
|
|
84
|
+
return this.aztecSlotDuration - this.l1PublishingTime;
|
|
85
|
+
default: {
|
|
86
|
+
const _exhaustiveCheck: never = state;
|
|
87
|
+
throw new Error(`Unexpected state: ${state}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public assertTimeLeft(newState: SequencerState, secondsIntoSlot: number) {
|
|
93
|
+
if (!this.enforce) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const maxAllowedTime = this.getMaxAllowedTime(newState);
|
|
98
|
+
if (maxAllowedTime === undefined) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const bufferSeconds = maxAllowedTime - secondsIntoSlot;
|
|
103
|
+
if (bufferSeconds < 0) {
|
|
104
|
+
throw new SequencerTooSlowError(newState, maxAllowedTime, secondsIntoSlot);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.metrics?.recordStateTransitionBufferMs(Math.floor(bufferSeconds * 1000), newState);
|
|
108
|
+
this.log.trace(`Enough time to transition to ${newState}`, { maxAllowedTime, secondsIntoSlot });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export class SequencerTooSlowError extends Error {
|
|
113
|
+
constructor(
|
|
114
|
+
public readonly proposedState: SequencerState,
|
|
115
|
+
public readonly maxAllowedTime: number,
|
|
116
|
+
public readonly currentTime: number,
|
|
117
|
+
) {
|
|
118
|
+
super(
|
|
119
|
+
`Too far into slot for ${proposedState} (time into slot ${currentTime}s greater than ${maxAllowedTime}s allowance)`,
|
|
120
|
+
);
|
|
121
|
+
this.name = 'SequencerTooSlowError';
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
+
import { Signature } from '@aztec/foundation/eth-signature';
|
|
3
|
+
import type { BlockAttestation } from '@aztec/stdlib/p2p';
|
|
4
|
+
|
|
5
|
+
export enum SequencerState {
|
|
6
|
+
/**
|
|
7
|
+
* Sequencer is stopped and not processing any txs from the pool.
|
|
8
|
+
*/
|
|
9
|
+
STOPPED = 'STOPPED',
|
|
10
|
+
/**
|
|
11
|
+
* Sequencer is awaiting the next call to work().
|
|
12
|
+
*/
|
|
13
|
+
IDLE = 'IDLE',
|
|
14
|
+
/**
|
|
15
|
+
* Synchronizing with the L2 chain.
|
|
16
|
+
*/
|
|
17
|
+
SYNCHRONIZING = 'SYNCHRONIZING',
|
|
18
|
+
/**
|
|
19
|
+
* Checking if we are the proposer for the current slot.
|
|
20
|
+
*/
|
|
21
|
+
PROPOSER_CHECK = 'PROPOSER_CHECK',
|
|
22
|
+
/**
|
|
23
|
+
* Initializing the block proposal. Will move to CREATING_BLOCK if there are valid txs to include, or back to SYNCHRONIZING otherwise.
|
|
24
|
+
*/
|
|
25
|
+
INITIALIZING_PROPOSAL = 'INITIALIZING_PROPOSAL',
|
|
26
|
+
/**
|
|
27
|
+
* Creating a new L2 block. Includes processing public function calls and running rollup circuits. Will move to PUBLISHING_CONTRACT_DATA.
|
|
28
|
+
*/
|
|
29
|
+
CREATING_BLOCK = 'CREATING_BLOCK',
|
|
30
|
+
/**
|
|
31
|
+
* Collecting attestations from its peers. Will move to PUBLISHING_BLOCK.
|
|
32
|
+
*/
|
|
33
|
+
COLLECTING_ATTESTATIONS = 'COLLECTING_ATTESTATIONS',
|
|
34
|
+
/**
|
|
35
|
+
* Sending the tx to L1 with the L2 block data and awaiting it to be mined. Will move to SYNCHRONIZING.
|
|
36
|
+
*/
|
|
37
|
+
PUBLISHING_BLOCK = 'PUBLISHING_BLOCK',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type SequencerStateCallback = () => SequencerState;
|
|
41
|
+
|
|
42
|
+
export function sequencerStateToNumber(state: SequencerState): number {
|
|
43
|
+
return Object.values(SequencerState).indexOf(state);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Order Attestations
|
|
47
|
+
*
|
|
48
|
+
* Returns attestation signatures in the order of a series of provided ethereum addresses
|
|
49
|
+
* The rollup smart contract expects attestations to appear in the order of the committee
|
|
50
|
+
*
|
|
51
|
+
* @todo: perform this logic within the memory attestation store instead?
|
|
52
|
+
*/
|
|
53
|
+
export async function orderAttestations(
|
|
54
|
+
attestations: BlockAttestation[],
|
|
55
|
+
orderAddresses: EthAddress[],
|
|
56
|
+
): Promise<Signature[]> {
|
|
57
|
+
// Create a map of sender addresses to BlockAttestations
|
|
58
|
+
const attestationMap = new Map<string, BlockAttestation>();
|
|
59
|
+
|
|
60
|
+
for (const attestation of attestations) {
|
|
61
|
+
const sender = await attestation.getSender();
|
|
62
|
+
if (sender) {
|
|
63
|
+
attestationMap.set(sender.toString(), attestation);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Create the ordered array based on the orderAddresses, else return an empty signature
|
|
68
|
+
const orderedAttestations = orderAddresses.map(address => {
|
|
69
|
+
const addressString = address.toString();
|
|
70
|
+
return attestationMap.get(addressString)?.signature || Signature.empty();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return orderedAttestations;
|
|
74
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { L1ContractsConfig, L1ReaderConfig } from '@aztec/ethereum';
|
|
2
|
+
import type { L2BlockSourceEventEmitter } from '@aztec/stdlib/block';
|
|
3
|
+
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
|
|
4
|
+
|
|
5
|
+
import { SlasherClient } from './slasher_client.js';
|
|
6
|
+
import type { SlasherConfig } from './slasher_client.js';
|
|
7
|
+
|
|
8
|
+
export const createSlasherClient = (
|
|
9
|
+
_config: SlasherConfig & L1ContractsConfig & L1ReaderConfig,
|
|
10
|
+
l2BlockSource: L2BlockSourceEventEmitter,
|
|
11
|
+
telemetry: TelemetryClient = getTelemetryClient(),
|
|
12
|
+
) => {
|
|
13
|
+
const config = { ..._config };
|
|
14
|
+
return new SlasherClient(config, l2BlockSource, telemetry);
|
|
15
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type L1ContractsConfig,
|
|
3
|
+
type L1ReaderConfig,
|
|
4
|
+
type ViemPublicClient,
|
|
5
|
+
createEthereumChain,
|
|
6
|
+
} from '@aztec/ethereum';
|
|
7
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
8
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
9
|
+
import { SlashFactoryAbi } from '@aztec/l1-artifacts';
|
|
10
|
+
import {
|
|
11
|
+
type L2BlockId,
|
|
12
|
+
type L2BlockSourceEvent,
|
|
13
|
+
type L2BlockSourceEventEmitter,
|
|
14
|
+
L2BlockSourceEvents,
|
|
15
|
+
} from '@aztec/stdlib/block';
|
|
16
|
+
import { type TelemetryClient, WithTracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
17
|
+
|
|
18
|
+
import { type GetContractReturnType, createPublicClient, fallback, getAddress, getContract, http } from 'viem';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Enum defining the possible states of the Slasher client.
|
|
22
|
+
*/
|
|
23
|
+
export enum SlasherClientState {
|
|
24
|
+
IDLE,
|
|
25
|
+
RUNNING,
|
|
26
|
+
STOPPED,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The synchronization status of the Slasher client.
|
|
31
|
+
*/
|
|
32
|
+
export interface SlasherSyncState {
|
|
33
|
+
/**
|
|
34
|
+
* The current state of the slasher client.
|
|
35
|
+
*/
|
|
36
|
+
state: SlasherClientState;
|
|
37
|
+
/**
|
|
38
|
+
* The block number that the slasher client is synced to.
|
|
39
|
+
*/
|
|
40
|
+
syncedToL2Block: L2BlockId;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SlasherConfig {
|
|
44
|
+
blockCheckIntervalMS: number;
|
|
45
|
+
blockRequestBatchSize: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type SlashEvent = {
|
|
49
|
+
epoch: bigint;
|
|
50
|
+
amount: bigint;
|
|
51
|
+
lifetime: bigint;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @notice A Hypomeiones slasher client implementation
|
|
56
|
+
*
|
|
57
|
+
* Hypomeiones: a class of individuals in ancient Sparta who were considered inferior or lesser citizens compared
|
|
58
|
+
* to the full Spartan citizens.
|
|
59
|
+
*
|
|
60
|
+
* The implementation here is less than ideal. It exists, not to be the end all be all, but to show that
|
|
61
|
+
* slashing can be done with this mechanism.
|
|
62
|
+
*
|
|
63
|
+
* The implementation is VERY brute in the sense that it only looks for pruned blocks and then tries to slash
|
|
64
|
+
* the full committee of that.
|
|
65
|
+
* If it sees a prune, it will mark the full epoch as "to be slashed".
|
|
66
|
+
*
|
|
67
|
+
* Also, it is not particularly smart around what it should if there were to be multiple slashing events.
|
|
68
|
+
*
|
|
69
|
+
* A few improvements:
|
|
70
|
+
* - Only vote on the proposal if it is possible to reach, e.g., if 6 votes are needed and only 4 slots are left don't vote.
|
|
71
|
+
* - Stop voting on a payload once it is processed.
|
|
72
|
+
* - Only vote on the proposal if it have not already been executed
|
|
73
|
+
* - Caveat, we need to fully decide if it is acceptable to have the same payload address multiple times. In the current
|
|
74
|
+
* slash factory that could mean slashing the same committee for the same error multiple times.
|
|
75
|
+
* - Decide how to deal with multiple slashing events in the same round.
|
|
76
|
+
* - This could be that multiple epochs are pruned in the same round, but with the current naive implementation we could end up
|
|
77
|
+
* slashing only the first, because the "lifetime" of the second would have passed after that vote
|
|
78
|
+
*/
|
|
79
|
+
export class SlasherClient extends WithTracer {
|
|
80
|
+
private slashEvents: SlashEvent[] = [];
|
|
81
|
+
|
|
82
|
+
protected slashFactoryContract?: GetContractReturnType<typeof SlashFactoryAbi, ViemPublicClient> = undefined;
|
|
83
|
+
|
|
84
|
+
// The amount to slash for a prune.
|
|
85
|
+
// Note that we set it to 0, such that no actual slashing will happen, but the event will be fired,
|
|
86
|
+
// showing that the slashing mechanism is working.
|
|
87
|
+
private slashingAmount: bigint = 0n;
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
private config: SlasherConfig & L1ContractsConfig & L1ReaderConfig,
|
|
91
|
+
private l2BlockSource: L2BlockSourceEventEmitter,
|
|
92
|
+
telemetry: TelemetryClient = getTelemetryClient(),
|
|
93
|
+
private log = createLogger('slasher'),
|
|
94
|
+
) {
|
|
95
|
+
super(telemetry, 'slasher');
|
|
96
|
+
|
|
97
|
+
if (config.l1Contracts.slashFactoryAddress && config.l1Contracts.slashFactoryAddress !== EthAddress.ZERO) {
|
|
98
|
+
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
99
|
+
const publicClient = createPublicClient({
|
|
100
|
+
chain: chain.chainInfo,
|
|
101
|
+
transport: fallback(chain.rpcUrls.map(url => http(url))),
|
|
102
|
+
pollingInterval: config.viemPollingIntervalMS,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
this.slashFactoryContract = getContract({
|
|
106
|
+
address: getAddress(config.l1Contracts.slashFactoryAddress.toString()),
|
|
107
|
+
abi: SlashFactoryAbi,
|
|
108
|
+
client: publicClient,
|
|
109
|
+
});
|
|
110
|
+
} else {
|
|
111
|
+
this.log.warn('No slash factory address found, slashing will not be enabled');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.log.info(`Slasher client initialized`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public start() {
|
|
118
|
+
this.log.info('Starting Slasher client...');
|
|
119
|
+
this.l2BlockSource.on(L2BlockSourceEvents.L2PruneDetected, this.handlePruneL2Blocks.bind(this));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// This is where we should put a bunch of the improvements mentioned earlier.
|
|
123
|
+
public async getSlashPayload(slotNumber: bigint): Promise<EthAddress | undefined> {
|
|
124
|
+
if (!this.slashFactoryContract) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// As long as the slot is greater than the lifetime, we want to keep deleting the first element
|
|
129
|
+
// since it will not make sense to include anymore.
|
|
130
|
+
while (this.slashEvents.length > 0 && this.slashEvents[0].lifetime < slotNumber) {
|
|
131
|
+
this.slashEvents.shift();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (this.slashEvents.length == 0) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const slashEvent = this.slashEvents[0];
|
|
139
|
+
|
|
140
|
+
const [payloadAddress, isDeployed] = await this.slashFactoryContract.read.getAddressAndIsDeployed([
|
|
141
|
+
slashEvent.epoch,
|
|
142
|
+
slashEvent.amount,
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
if (!isDeployed) {
|
|
146
|
+
// The proposal cannot be executed until it is deployed
|
|
147
|
+
this.log.verbose(`Voting on not yet deployed payload: ${payloadAddress}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return EthAddress.fromString(payloadAddress);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public handleBlockStreamEvent(event: L2BlockSourceEvent): Promise<void> {
|
|
154
|
+
this.log.debug(`Handling block stream event ${event.type}`);
|
|
155
|
+
switch (event.type) {
|
|
156
|
+
case L2BlockSourceEvents.L2PruneDetected:
|
|
157
|
+
this.handlePruneL2Blocks(event);
|
|
158
|
+
break;
|
|
159
|
+
default: {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return Promise.resolve();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Allows consumers to stop the instance of the slasher client.
|
|
168
|
+
* 'ready' will now return 'false' and the running promise that keeps the client synced is interrupted.
|
|
169
|
+
*/
|
|
170
|
+
public stop() {
|
|
171
|
+
this.log.debug('Stopping Slasher client...');
|
|
172
|
+
this.l2BlockSource.removeListener(L2BlockSourceEvents.L2PruneDetected, this.handlePruneL2Blocks.bind(this));
|
|
173
|
+
this.log.info('Slasher client stopped.');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// I need to get the slot number from the block that was just pruned
|
|
177
|
+
private handlePruneL2Blocks(event: L2BlockSourceEvent): void {
|
|
178
|
+
const { slotNumber, epochNumber } = event;
|
|
179
|
+
this.log.info(`Detected chain prune. Punishing the validators at epoch ${epochNumber}`);
|
|
180
|
+
|
|
181
|
+
// Set the lifetime such that we have a full round that we could vote throughout.
|
|
182
|
+
const slotsIntoRound = slotNumber % BigInt(this.config.slashingRoundSize);
|
|
183
|
+
const toNext = slotsIntoRound == 0n ? 0n : BigInt(this.config.slashingRoundSize) - slotsIntoRound;
|
|
184
|
+
|
|
185
|
+
const lifetime = slotNumber + toNext + BigInt(this.config.slashingRoundSize);
|
|
186
|
+
|
|
187
|
+
this.slashEvents.push({
|
|
188
|
+
epoch: epochNumber,
|
|
189
|
+
amount: this.slashingAmount,
|
|
190
|
+
lifetime,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PublicProcessorFactory } from '@aztec/simulator/server';
|
|
2
|
+
|
|
3
|
+
import { SequencerClient } from '../client/sequencer-client.js';
|
|
4
|
+
import type { SequencerPublisher } from '../publisher/sequencer-publisher.js';
|
|
5
|
+
import { Sequencer } from '../sequencer/sequencer.js';
|
|
6
|
+
import type { SequencerTimetable } from '../sequencer/timetable.js';
|
|
7
|
+
|
|
8
|
+
class TestSequencer_ extends Sequencer {
|
|
9
|
+
public override publicProcessorFactory!: PublicProcessorFactory;
|
|
10
|
+
public override timetable!: SequencerTimetable;
|
|
11
|
+
public override publisher!: SequencerPublisher;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type TestSequencer = TestSequencer_;
|
|
15
|
+
|
|
16
|
+
class TestSequencerClient_ extends SequencerClient {
|
|
17
|
+
public override sequencer!: TestSequencer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type TestSequencerClient = TestSequencerClient_;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import type { ArchiveSource } from '@aztec/p2p';
|
|
3
|
+
import type { MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
|
|
4
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Implements an archive source by checking a DB and an in-memory collection.
|
|
8
|
+
* Intended for validating transactions as they are added to a block.
|
|
9
|
+
*/
|
|
10
|
+
export class ArchiveCache implements ArchiveSource {
|
|
11
|
+
archives: Map<string, bigint>;
|
|
12
|
+
|
|
13
|
+
constructor(private db: MerkleTreeReadOperations) {
|
|
14
|
+
this.archives = new Map<string, bigint>();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async getArchiveIndices(archives: Fr[]): Promise<(bigint | undefined)[]> {
|
|
18
|
+
const toCheckDb = archives.filter(n => !this.archives.has(n.toString()));
|
|
19
|
+
const dbHits = await this.db.findLeafIndices(MerkleTreeId.ARCHIVE, toCheckDb);
|
|
20
|
+
dbHits.forEach((x, index) => {
|
|
21
|
+
if (x !== undefined) {
|
|
22
|
+
this.archives.set(toCheckDb[index].toString(), x);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return archives.map(n => this.archives.get(n.toString()));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
4
|
+
import { getExecutionRequestsByPhase } from '@aztec/simulator/server';
|
|
5
|
+
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
6
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
|
+
import type { GasFees } from '@aztec/stdlib/gas';
|
|
8
|
+
import { type Tx, TxExecutionPhase, type TxValidationResult, type TxValidator } from '@aztec/stdlib/tx';
|
|
9
|
+
|
|
10
|
+
/** Provides a view into public contract state */
|
|
11
|
+
export interface PublicStateSource {
|
|
12
|
+
storageRead: (contractAddress: AztecAddress, slot: Fr) => Promise<Fr>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class GasTxValidator implements TxValidator<Tx> {
|
|
16
|
+
#log = createLogger('sequencer:tx_validator:tx_gas');
|
|
17
|
+
#publicDataSource: PublicStateSource;
|
|
18
|
+
#feeJuiceAddress: AztecAddress;
|
|
19
|
+
#gasFees: GasFees;
|
|
20
|
+
|
|
21
|
+
constructor(publicDataSource: PublicStateSource, feeJuiceAddress: AztecAddress, gasFees: GasFees) {
|
|
22
|
+
this.#publicDataSource = publicDataSource;
|
|
23
|
+
this.#feeJuiceAddress = feeJuiceAddress;
|
|
24
|
+
this.#gasFees = gasFees;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async validateTx(tx: Tx): Promise<TxValidationResult> {
|
|
28
|
+
if (await this.#shouldSkip(tx)) {
|
|
29
|
+
return Promise.resolve({ result: 'skipped', reason: ['Insufficient fee per gas'] });
|
|
30
|
+
}
|
|
31
|
+
return this.#validateTxFee(tx);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check whether the tx's max fees are valid for the current block, and skip if not.
|
|
36
|
+
* We skip instead of invalidating since the tx may become eligible later.
|
|
37
|
+
* Note that circuits check max fees even if fee payer is unset, so we
|
|
38
|
+
* keep this validation even if the tx does not pay fees.
|
|
39
|
+
*/
|
|
40
|
+
async #shouldSkip(tx: Tx): Promise<boolean> {
|
|
41
|
+
const gasSettings = tx.data.constants.txContext.gasSettings;
|
|
42
|
+
|
|
43
|
+
// Skip the tx if its max fees are not enough for the current block's gas fees.
|
|
44
|
+
const maxFeesPerGas = gasSettings.maxFeesPerGas;
|
|
45
|
+
const notEnoughMaxFees =
|
|
46
|
+
maxFeesPerGas.feePerDaGas.lt(this.#gasFees.feePerDaGas) ||
|
|
47
|
+
maxFeesPerGas.feePerL2Gas.lt(this.#gasFees.feePerL2Gas);
|
|
48
|
+
|
|
49
|
+
if (notEnoughMaxFees) {
|
|
50
|
+
this.#log.warn(`Skipping transaction ${await tx.getTxHash()} due to insufficient fee per gas`, {
|
|
51
|
+
txMaxFeesPerGas: maxFeesPerGas.toInspect(),
|
|
52
|
+
currentGasFees: this.#gasFees.toInspect(),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return notEnoughMaxFees;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async #validateTxFee(tx: Tx): Promise<TxValidationResult> {
|
|
59
|
+
const feePayer = tx.data.feePayer;
|
|
60
|
+
|
|
61
|
+
// Compute the maximum fee that this tx may pay, based on its gasLimits and maxFeePerGas
|
|
62
|
+
const feeLimit = tx.data.constants.txContext.gasSettings.getFeeLimit();
|
|
63
|
+
|
|
64
|
+
// Read current balance of the feePayer
|
|
65
|
+
const initialBalance = await this.#publicDataSource.storageRead(
|
|
66
|
+
this.#feeJuiceAddress,
|
|
67
|
+
await computeFeePayerBalanceStorageSlot(feePayer),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// If there is a claim in this tx that increases the fee payer balance in Fee Juice, add it to balance
|
|
71
|
+
const setupFns = getExecutionRequestsByPhase(tx, TxExecutionPhase.SETUP);
|
|
72
|
+
const increasePublicBalanceSelector = await FunctionSelector.fromSignature(
|
|
73
|
+
'_increase_public_balance((Field),u128)',
|
|
74
|
+
);
|
|
75
|
+
const claimFunctionCall = setupFns.find(
|
|
76
|
+
fn =>
|
|
77
|
+
fn.callContext.contractAddress.equals(this.#feeJuiceAddress) &&
|
|
78
|
+
fn.callContext.msgSender.equals(this.#feeJuiceAddress) &&
|
|
79
|
+
fn.args.length > 2 &&
|
|
80
|
+
// Public functions get routed through the dispatch function, whose first argument is the target function selector.
|
|
81
|
+
fn.args[0].equals(increasePublicBalanceSelector.toField()) &&
|
|
82
|
+
fn.args[1].equals(feePayer.toField()) &&
|
|
83
|
+
!fn.callContext.isStaticCall,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// The claim amount is at index 2 in the args array because:
|
|
87
|
+
// - Index 0: Target function selector (due to dispatch routing)
|
|
88
|
+
// - Index 1: Amount recipient
|
|
89
|
+
// - Index 2: Amount being claimed
|
|
90
|
+
const balance = claimFunctionCall ? initialBalance.add(claimFunctionCall.args[2]) : initialBalance;
|
|
91
|
+
if (balance.lt(feeLimit)) {
|
|
92
|
+
this.#log.warn(`Rejecting transaction due to not enough fee payer balance`, {
|
|
93
|
+
feePayer,
|
|
94
|
+
balance: balance.toBigInt(),
|
|
95
|
+
feeLimit: feeLimit.toBigInt(),
|
|
96
|
+
});
|
|
97
|
+
return { result: 'invalid', reason: ['Insufficient fee payer balance'] };
|
|
98
|
+
}
|
|
99
|
+
return { result: 'valid' };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { NullifierSource } from '@aztec/p2p';
|
|
2
|
+
import type { MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
|
|
3
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implements a nullifier source by checking a DB and an in-memory collection.
|
|
7
|
+
* Intended for validating transactions as they are added to a block.
|
|
8
|
+
*/
|
|
9
|
+
export class NullifierCache implements NullifierSource {
|
|
10
|
+
nullifiers: Set<string>;
|
|
11
|
+
|
|
12
|
+
constructor(private db: MerkleTreeReadOperations) {
|
|
13
|
+
this.nullifiers = new Set();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async nullifiersExist(nullifiers: Buffer[]): Promise<boolean[]> {
|
|
17
|
+
const cacheResults = nullifiers.map(n => this.nullifiers.has(n.toString()));
|
|
18
|
+
const toCheckDb = nullifiers.filter((_n, index) => !cacheResults[index]);
|
|
19
|
+
const dbHits = await this.db.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, toCheckDb);
|
|
20
|
+
|
|
21
|
+
let dbIndex = 0;
|
|
22
|
+
return nullifiers.map((_n, index) => cacheResults[index] || dbHits[dbIndex++] !== undefined);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public addNullifiers(nullifiers: Buffer[]) {
|
|
26
|
+
for (const nullifier of nullifiers) {
|
|
27
|
+
this.nullifiers.add(nullifier.toString());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|