@aztec/slasher 0.0.1-commit.e558bd1c → 0.0.1-commit.e57c76e
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 +78 -78
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +35 -29
- package/dest/factory/create_facade.d.ts +3 -3
- package/dest/factory/create_facade.d.ts.map +1 -1
- package/dest/factory/create_facade.js +25 -2
- package/dest/factory/create_implementation.d.ts +6 -7
- package/dest/factory/create_implementation.d.ts.map +1 -1
- package/dest/factory/create_implementation.js +8 -56
- package/dest/factory/get_settings.d.ts +4 -4
- package/dest/factory/get_settings.d.ts.map +1 -1
- package/dest/factory/get_settings.js +3 -3
- package/dest/factory/index.d.ts +2 -2
- package/dest/factory/index.d.ts.map +1 -1
- package/dest/factory/index.js +1 -1
- package/dest/generated/slasher-defaults.d.ts +8 -7
- package/dest/generated/slasher-defaults.d.ts.map +1 -1
- package/dest/generated/slasher-defaults.js +7 -6
- package/dest/index.d.ts +6 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +5 -3
- package/dest/null_slasher_client.d.ts +3 -4
- package/dest/null_slasher_client.d.ts.map +1 -1
- package/dest/null_slasher_client.js +1 -4
- package/dest/slash_offenses_collector.d.ts +10 -9
- package/dest/slash_offenses_collector.d.ts.map +1 -1
- package/dest/slash_offenses_collector.js +50 -34
- package/dest/slasher_client.d.ts +112 -0
- package/dest/slasher_client.d.ts.map +1 -0
- package/dest/{tally_slasher_client.js → slasher_client.js} +45 -45
- package/dest/slasher_client_facade.d.ts +6 -8
- package/dest/slasher_client_facade.d.ts.map +1 -1
- package/dest/slasher_client_facade.js +6 -9
- package/dest/slasher_client_interface.d.ts +7 -21
- package/dest/slasher_client_interface.d.ts.map +1 -1
- package/dest/slasher_client_interface.js +1 -4
- package/dest/stores/offenses_store.d.ts +12 -12
- package/dest/stores/offenses_store.d.ts.map +1 -1
- package/dest/stores/offenses_store.js +61 -38
- package/dest/watcher.d.ts +8 -1
- package/dest/watcher.d.ts.map +1 -1
- package/dest/watcher.js +1 -0
- package/dest/watchers/attestations_block_watcher.d.ts +26 -13
- package/dest/watchers/attestations_block_watcher.d.ts.map +1 -1
- package/dest/watchers/attestations_block_watcher.js +76 -61
- package/dest/watchers/attested_invalid_proposal_watcher.d.ts +42 -0
- package/dest/watchers/attested_invalid_proposal_watcher.d.ts.map +1 -0
- package/dest/watchers/attested_invalid_proposal_watcher.js +117 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts +38 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.d.ts.map +1 -0
- package/dest/watchers/broadcasted_invalid_checkpoint_proposal_watcher.js +138 -0
- package/dest/watchers/checkpoint_equivocation_watcher.d.ts +30 -0
- package/dest/watchers/checkpoint_equivocation_watcher.d.ts.map +1 -0
- package/dest/watchers/checkpoint_equivocation_watcher.js +69 -0
- package/dest/watchers/data_withholding_watcher.d.ts +63 -0
- package/dest/watchers/data_withholding_watcher.d.ts.map +1 -0
- package/dest/watchers/data_withholding_watcher.js +193 -0
- package/package.json +10 -10
- package/src/config.ts +42 -29
- package/src/factory/create_facade.ts +32 -4
- package/src/factory/create_implementation.ts +24 -105
- package/src/factory/get_settings.ts +8 -8
- package/src/factory/index.ts +1 -1
- package/src/generated/slasher-defaults.ts +7 -6
- package/src/index.ts +5 -3
- package/src/null_slasher_client.ts +2 -6
- package/src/slash_offenses_collector.ts +70 -36
- package/src/{tally_slasher_client.ts → slasher_client.ts} +63 -54
- package/src/slasher_client_facade.ts +6 -11
- package/src/slasher_client_interface.ts +6 -21
- package/src/stores/offenses_store.ts +73 -47
- package/src/watcher.ts +8 -0
- package/src/watchers/attestations_block_watcher.ts +88 -82
- package/src/watchers/attested_invalid_proposal_watcher.ts +168 -0
- package/src/watchers/broadcasted_invalid_checkpoint_proposal_watcher.ts +192 -0
- package/src/watchers/checkpoint_equivocation_watcher.ts +96 -0
- package/src/watchers/data_withholding_watcher.ts +225 -0
- package/dest/empire_slasher_client.d.ts +0 -190
- package/dest/empire_slasher_client.d.ts.map +0 -1
- package/dest/empire_slasher_client.js +0 -564
- package/dest/stores/payloads_store.d.ts +0 -29
- package/dest/stores/payloads_store.d.ts.map +0 -1
- package/dest/stores/payloads_store.js +0 -128
- package/dest/tally_slasher_client.d.ts +0 -125
- package/dest/tally_slasher_client.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.d.ts +0 -39
- package/dest/watchers/epoch_prune_watcher.d.ts.map +0 -1
- package/dest/watchers/epoch_prune_watcher.js +0 -175
- package/src/empire_slasher_client.ts +0 -649
- package/src/stores/payloads_store.ts +0 -149
- package/src/watchers/epoch_prune_watcher.ts +0 -251
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
-
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
3
|
-
import {
|
|
4
|
-
type SlashPayload,
|
|
5
|
-
type SlashPayloadRound,
|
|
6
|
-
deserializeSlashPayload,
|
|
7
|
-
serializeSlashPayload,
|
|
8
|
-
} from '@aztec/stdlib/slashing';
|
|
9
|
-
|
|
10
|
-
export class SlasherPayloadsStore {
|
|
11
|
-
/** Map from payload address to payload data */
|
|
12
|
-
private payloads: AztecAsyncMap<string, Buffer>;
|
|
13
|
-
|
|
14
|
-
/** Map from `round:payload` to votes */
|
|
15
|
-
private roundPayloadVotes: AztecAsyncMap<string, bigint>;
|
|
16
|
-
|
|
17
|
-
constructor(
|
|
18
|
-
private kvStore: AztecAsyncKVStore,
|
|
19
|
-
private settings?: {
|
|
20
|
-
slashingPayloadLifetimeInRounds?: number;
|
|
21
|
-
},
|
|
22
|
-
) {
|
|
23
|
-
this.payloads = kvStore.openMap('slash-payloads');
|
|
24
|
-
this.roundPayloadVotes = kvStore.openMap('round-payload-votes');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public async getPayloadsForRound(round: bigint): Promise<SlashPayloadRound[]> {
|
|
28
|
-
const payloads: SlashPayloadRound[] = [];
|
|
29
|
-
const votes = await this.getVotesForRound(round);
|
|
30
|
-
for (const [address, votesCount] of votes) {
|
|
31
|
-
const payload = await this.getPayload(address);
|
|
32
|
-
if (payload) {
|
|
33
|
-
payloads.push({ ...payload, votes: votesCount, round });
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return payloads;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async getPayloadAtRound(payloadAddress: EthAddress, round: bigint): Promise<SlashPayloadRound | undefined> {
|
|
40
|
-
const address = payloadAddress.toString();
|
|
41
|
-
const buffer = await this.payloads.getAsync(address);
|
|
42
|
-
if (!buffer) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const data = deserializeSlashPayload(buffer);
|
|
47
|
-
const votes = (await this.roundPayloadVotes.getAsync(this.getPayloadVotesKey(round, address))) ?? 0n;
|
|
48
|
-
|
|
49
|
-
return { ...data, votes, round };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private async getVotesForRound(round: bigint): Promise<[string, bigint][]> {
|
|
53
|
-
const votes: [string, bigint][] = [];
|
|
54
|
-
for await (const [fullKey, roundVotes] of this.roundPayloadVotes.entriesAsync(
|
|
55
|
-
this.getPayloadVotesKeyRangeForRound(round),
|
|
56
|
-
)) {
|
|
57
|
-
// Extract just the address part from the key (remove "round:" prefix)
|
|
58
|
-
const address = fullKey.split(':')[1];
|
|
59
|
-
votes.push([address, roundVotes]);
|
|
60
|
-
}
|
|
61
|
-
return votes;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private getRoundKey(round: bigint): string {
|
|
65
|
-
return round.toString().padStart(16, '0');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private getPayloadVotesKey(round: bigint, payloadAddress: EthAddress | string): string {
|
|
69
|
-
return `${this.getRoundKey(round)}:${payloadAddress.toString()}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private getPayloadVotesKeyRangeForRound(round: bigint): { start: string; end: string } {
|
|
73
|
-
const start = `${this.getRoundKey(round)}:`;
|
|
74
|
-
const end = `${this.getRoundKey(round)}:Z`; // 'Z' sorts after any hex address, 0x-prefixed or not
|
|
75
|
-
return { start, end };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Purge vote payload data for expired rounds. Does not delete actual payload data.
|
|
80
|
-
*/
|
|
81
|
-
public async clearExpiredPayloads(currentRound: bigint): Promise<void> {
|
|
82
|
-
const lifetimeInRounds = this.settings?.slashingPayloadLifetimeInRounds ?? 0;
|
|
83
|
-
if (lifetimeInRounds <= 0) {
|
|
84
|
-
return; // No lifetime configured
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const expiredBefore = currentRound - BigInt(lifetimeInRounds);
|
|
88
|
-
if (expiredBefore < 0) {
|
|
89
|
-
return; // Not enough rounds have passed to expire anything
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Collect expired payload votes by scanning round-payload keys
|
|
93
|
-
const expiredPayloads: string[] = [];
|
|
94
|
-
const expiredVoteKeys: string[] = [];
|
|
95
|
-
|
|
96
|
-
for await (const key of this.roundPayloadVotes.keysAsync({
|
|
97
|
-
end: `${this.getRoundKey(expiredBefore)}:Z`,
|
|
98
|
-
})) {
|
|
99
|
-
const [roundStr, payloadAddress] = key.split(':');
|
|
100
|
-
if (BigInt(roundStr) <= expiredBefore) {
|
|
101
|
-
expiredVoteKeys.push(key);
|
|
102
|
-
expiredPayloads.push(payloadAddress);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (expiredVoteKeys.length === 0) {
|
|
107
|
-
return; // No expired payloads to clean up
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Remove expired payload vote records
|
|
111
|
-
// Note that we do not delete payload data since these could be repurposed in future votes
|
|
112
|
-
await this.kvStore.transactionAsync(async () => {
|
|
113
|
-
for (const key of expiredVoteKeys) {
|
|
114
|
-
await this.roundPayloadVotes.delete(key);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public async incrementPayloadVotes(payloadAddress: EthAddress, round: bigint): Promise<bigint> {
|
|
120
|
-
const key = this.getPayloadVotesKey(round, payloadAddress);
|
|
121
|
-
let newVotes: bigint;
|
|
122
|
-
await this.kvStore.transactionAsync(async () => {
|
|
123
|
-
const currentVotes = (await this.roundPayloadVotes.getAsync(key)) || 0n;
|
|
124
|
-
newVotes = currentVotes + 1n;
|
|
125
|
-
await this.roundPayloadVotes.set(key, newVotes);
|
|
126
|
-
});
|
|
127
|
-
return newVotes!;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public async addPayload(payload: SlashPayloadRound): Promise<void> {
|
|
131
|
-
const address = payload.address.toString();
|
|
132
|
-
|
|
133
|
-
await this.kvStore.transactionAsync(async () => {
|
|
134
|
-
await this.payloads.set(address, serializeSlashPayload(payload));
|
|
135
|
-
await this.roundPayloadVotes.set(this.getPayloadVotesKey(payload.round, address), payload.votes);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
public async getPayload(payloadAddress: EthAddress | string): Promise<SlashPayload | undefined> {
|
|
140
|
-
const address = payloadAddress.toString();
|
|
141
|
-
const buffer = await this.payloads.getAsync(address);
|
|
142
|
-
return buffer ? deserializeSlashPayload(buffer) : undefined;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
public async hasPayload(payload: EthAddress): Promise<boolean> {
|
|
146
|
-
const address = payload.toString();
|
|
147
|
-
return (await this.payloads.getAsync(address)) !== undefined;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import { EpochCache } from '@aztec/epoch-cache';
|
|
2
|
-
import { BlockNumber, EpochNumber } from '@aztec/foundation/branded-types';
|
|
3
|
-
import { chunkBy, merge, pick } from '@aztec/foundation/collection';
|
|
4
|
-
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
6
|
-
import {
|
|
7
|
-
EthAddress,
|
|
8
|
-
L2Block,
|
|
9
|
-
type L2BlockSourceEventEmitter,
|
|
10
|
-
L2BlockSourceEvents,
|
|
11
|
-
type L2PruneUnprovenEvent,
|
|
12
|
-
} from '@aztec/stdlib/block';
|
|
13
|
-
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
14
|
-
import type {
|
|
15
|
-
ICheckpointBlockBuilder,
|
|
16
|
-
ICheckpointsBuilder,
|
|
17
|
-
ITxProvider,
|
|
18
|
-
MerkleTreeWriteOperations,
|
|
19
|
-
SlasherConfig,
|
|
20
|
-
} from '@aztec/stdlib/interfaces/server';
|
|
21
|
-
import { type L1ToL2MessageSource, computeCheckpointOutHash } from '@aztec/stdlib/messaging';
|
|
22
|
-
import { OffenseType, getOffenseTypeName } from '@aztec/stdlib/slashing';
|
|
23
|
-
import type { CheckpointGlobalVariables } from '@aztec/stdlib/tx';
|
|
24
|
-
import {
|
|
25
|
-
ReExFailedTxsError,
|
|
26
|
-
ReExStateMismatchError,
|
|
27
|
-
TransactionsNotAvailableError,
|
|
28
|
-
ValidatorError,
|
|
29
|
-
} from '@aztec/stdlib/validators';
|
|
30
|
-
|
|
31
|
-
import EventEmitter from 'node:events';
|
|
32
|
-
|
|
33
|
-
import { WANT_TO_SLASH_EVENT, type WantToSlashArgs, type Watcher, type WatcherEmitter } from '../watcher.js';
|
|
34
|
-
|
|
35
|
-
const EpochPruneWatcherPenaltiesConfigKeys = ['slashPrunePenalty', 'slashDataWithholdingPenalty'] as const;
|
|
36
|
-
|
|
37
|
-
type EpochPruneWatcherPenalties = Pick<SlasherConfig, (typeof EpochPruneWatcherPenaltiesConfigKeys)[number]>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* This watcher is responsible for detecting chain prunes and creating slashing arguments for the committee.
|
|
41
|
-
* It only wants to slash if:
|
|
42
|
-
* - the transactions are not available
|
|
43
|
-
* - OR the archive roots match when re-building all the blocks in the epoch (i.e. the epoch *could* have been proven)
|
|
44
|
-
*/
|
|
45
|
-
export class EpochPruneWatcher extends (EventEmitter as new () => WatcherEmitter) implements Watcher {
|
|
46
|
-
private log: Logger = createLogger('epoch-prune-watcher');
|
|
47
|
-
|
|
48
|
-
// Store bound function reference for proper listener removal
|
|
49
|
-
private boundHandlePruneL2Blocks = this.handlePruneL2Blocks.bind(this);
|
|
50
|
-
|
|
51
|
-
private penalties: EpochPruneWatcherPenalties;
|
|
52
|
-
|
|
53
|
-
constructor(
|
|
54
|
-
private l2BlockSource: L2BlockSourceEventEmitter,
|
|
55
|
-
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
56
|
-
private epochCache: EpochCache,
|
|
57
|
-
private txProvider: Pick<ITxProvider, 'getAvailableTxs'>,
|
|
58
|
-
private checkpointsBuilder: ICheckpointsBuilder,
|
|
59
|
-
penalties: EpochPruneWatcherPenalties,
|
|
60
|
-
) {
|
|
61
|
-
super();
|
|
62
|
-
this.penalties = pick(penalties, ...EpochPruneWatcherPenaltiesConfigKeys);
|
|
63
|
-
this.log.verbose(
|
|
64
|
-
`EpochPruneWatcher initialized with penalties: valid epoch pruned=${penalties.slashPrunePenalty} data withholding=${penalties.slashDataWithholdingPenalty}`,
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public start() {
|
|
69
|
-
this.l2BlockSource.events.on(L2BlockSourceEvents.L2PruneUnproven, this.boundHandlePruneL2Blocks);
|
|
70
|
-
return Promise.resolve();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public stop() {
|
|
74
|
-
this.l2BlockSource.events.removeListener(L2BlockSourceEvents.L2PruneUnproven, this.boundHandlePruneL2Blocks);
|
|
75
|
-
return Promise.resolve();
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public updateConfig(config: Partial<SlasherConfig>): void {
|
|
79
|
-
this.penalties = merge(this.penalties, pick(config, ...EpochPruneWatcherPenaltiesConfigKeys));
|
|
80
|
-
this.log.verbose('EpochPruneWatcher config updated', this.penalties);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private handlePruneL2Blocks(event: L2PruneUnprovenEvent): void {
|
|
84
|
-
const { blocks, epochNumber } = event;
|
|
85
|
-
void this.processPruneL2Blocks(blocks, epochNumber).catch(err =>
|
|
86
|
-
this.log.error('Error processing pruned L2 blocks', err, { epochNumber }),
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private async emitSlashForEpoch(offense: OffenseType, epochNumber: EpochNumber): Promise<void> {
|
|
91
|
-
const validators = await this.getValidatorsForEpoch(epochNumber);
|
|
92
|
-
if (validators.length === 0) {
|
|
93
|
-
this.log.warn(`No validators found for epoch ${epochNumber} (cannot slash for ${getOffenseTypeName(offense)})`);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const args = this.validatorsToSlashingArgs(validators, offense, epochNumber);
|
|
97
|
-
this.log.verbose(`Created slash for ${getOffenseTypeName(offense)} at epoch ${epochNumber}`, args);
|
|
98
|
-
this.emit(WANT_TO_SLASH_EVENT, args);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private async processPruneL2Blocks(blocks: L2Block[], epochNumber: EpochNumber): Promise<void> {
|
|
102
|
-
try {
|
|
103
|
-
const l1Constants = this.epochCache.getL1Constants();
|
|
104
|
-
const epochBlocks = blocks.filter(b => getEpochAtSlot(b.header.getSlot(), l1Constants) === epochNumber);
|
|
105
|
-
this.log.info(
|
|
106
|
-
`Detected chain prune. Validating epoch ${epochNumber} with blocks ${epochBlocks[0]?.number} to ${epochBlocks[epochBlocks.length - 1]?.number}.`,
|
|
107
|
-
{ blocks: epochBlocks.map(b => b.toBlockInfo()) },
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
await this.validateBlocks(epochBlocks, epochNumber);
|
|
111
|
-
this.log.info(`Pruned epoch ${epochNumber} was valid. Want to slash committee for not having it proven.`);
|
|
112
|
-
await this.emitSlashForEpoch(OffenseType.VALID_EPOCH_PRUNED, epochNumber);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
if (error instanceof TransactionsNotAvailableError) {
|
|
115
|
-
this.log.info(`Data for pruned epoch ${epochNumber} was not available. Will want to slash.`, {
|
|
116
|
-
message: error.message,
|
|
117
|
-
});
|
|
118
|
-
await this.emitSlashForEpoch(OffenseType.DATA_WITHHOLDING, epochNumber);
|
|
119
|
-
} else {
|
|
120
|
-
this.log.error(`Error while validating pruned epoch ${epochNumber}. Will not want to slash.`, error);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public async validateBlocks(blocks: L2Block[], epochNumber: EpochNumber): Promise<void> {
|
|
126
|
-
if (blocks.length === 0) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Sort blocks by block number and group by checkpoint
|
|
131
|
-
const sortedBlocks = [...blocks].sort((a, b) => a.number - b.number);
|
|
132
|
-
const blocksByCheckpoint = chunkBy(sortedBlocks, b => b.checkpointNumber);
|
|
133
|
-
|
|
134
|
-
// Get prior checkpoints in the epoch (in case this was a partial prune) to extract the out hashes
|
|
135
|
-
const priorCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsForEpoch(epochNumber))
|
|
136
|
-
.filter(c => c.number < sortedBlocks[0].checkpointNumber)
|
|
137
|
-
.map(c => c.getCheckpointOutHash());
|
|
138
|
-
let previousCheckpointOutHashes: Fr[] = [...priorCheckpointOutHashes];
|
|
139
|
-
|
|
140
|
-
const fork = await this.checkpointsBuilder.getFork(
|
|
141
|
-
BlockNumber(sortedBlocks[0].header.globalVariables.blockNumber - 1),
|
|
142
|
-
);
|
|
143
|
-
try {
|
|
144
|
-
for (const checkpointBlocks of blocksByCheckpoint) {
|
|
145
|
-
await this.validateCheckpoint(checkpointBlocks, previousCheckpointOutHashes, fork);
|
|
146
|
-
|
|
147
|
-
// Compute checkpoint out hash from all blocks in this checkpoint
|
|
148
|
-
const checkpointOutHash = computeCheckpointOutHash(
|
|
149
|
-
checkpointBlocks.map(b => b.body.txEffects.map(tx => tx.l2ToL1Msgs)),
|
|
150
|
-
);
|
|
151
|
-
previousCheckpointOutHashes = [...previousCheckpointOutHashes, checkpointOutHash];
|
|
152
|
-
}
|
|
153
|
-
} finally {
|
|
154
|
-
await fork.close();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
private async validateCheckpoint(
|
|
159
|
-
checkpointBlocks: L2Block[],
|
|
160
|
-
previousCheckpointOutHashes: Fr[],
|
|
161
|
-
fork: MerkleTreeWriteOperations,
|
|
162
|
-
): Promise<void> {
|
|
163
|
-
const checkpointNumber = checkpointBlocks[0].checkpointNumber;
|
|
164
|
-
this.log.debug(`Validating pruned checkpoint ${checkpointNumber} with ${checkpointBlocks.length} blocks`);
|
|
165
|
-
|
|
166
|
-
// Get L1ToL2Messages once for the entire checkpoint
|
|
167
|
-
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
168
|
-
|
|
169
|
-
// Build checkpoint constants from first block's global variables
|
|
170
|
-
const gv = checkpointBlocks[0].header.globalVariables;
|
|
171
|
-
const constants: CheckpointGlobalVariables = {
|
|
172
|
-
chainId: gv.chainId,
|
|
173
|
-
version: gv.version,
|
|
174
|
-
slotNumber: gv.slotNumber,
|
|
175
|
-
coinbase: gv.coinbase,
|
|
176
|
-
feeRecipient: gv.feeRecipient,
|
|
177
|
-
gasFees: gv.gasFees,
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
// Start checkpoint builder once for all blocks in this checkpoint
|
|
181
|
-
const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(
|
|
182
|
-
checkpointNumber,
|
|
183
|
-
constants,
|
|
184
|
-
l1ToL2Messages,
|
|
185
|
-
previousCheckpointOutHashes,
|
|
186
|
-
fork,
|
|
187
|
-
this.log.getBindings(),
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
// Validate all blocks in the checkpoint sequentially
|
|
191
|
-
for (const block of checkpointBlocks) {
|
|
192
|
-
await this.validateBlockInCheckpoint(block, checkpointBuilder);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
private async validateBlockInCheckpoint(
|
|
197
|
-
blockFromL1: L2Block,
|
|
198
|
-
checkpointBuilder: ICheckpointBlockBuilder,
|
|
199
|
-
): Promise<void> {
|
|
200
|
-
this.log.debug(`Validating pruned block ${blockFromL1.header.globalVariables.blockNumber}`);
|
|
201
|
-
const txHashes = blockFromL1.body.txEffects.map(txEffect => txEffect.txHash);
|
|
202
|
-
// We load txs from the mempool directly, since the TxCollector running in the background has already been
|
|
203
|
-
// trying to fetch them from nodes or via reqresp. If we haven't managed to collect them by now,
|
|
204
|
-
// it's likely that they are not available in the network at all.
|
|
205
|
-
const { txs, missingTxs } = await this.txProvider.getAvailableTxs(txHashes);
|
|
206
|
-
|
|
207
|
-
if (missingTxs && missingTxs.length > 0) {
|
|
208
|
-
throw new TransactionsNotAvailableError(missingTxs);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const gv = blockFromL1.header.globalVariables;
|
|
212
|
-
const { block, failedTxs, numTxs } = await checkpointBuilder.buildBlock(txs, gv.blockNumber, gv.timestamp, {});
|
|
213
|
-
|
|
214
|
-
if (numTxs !== txs.length) {
|
|
215
|
-
// This should be detected by state mismatch, but this makes it easier to debug.
|
|
216
|
-
throw new ValidatorError(`Built block with ${numTxs} txs, expected ${txs.length}`);
|
|
217
|
-
}
|
|
218
|
-
if (failedTxs.length > 0) {
|
|
219
|
-
throw new ReExFailedTxsError(failedTxs.length);
|
|
220
|
-
}
|
|
221
|
-
if (!block.archive.root.equals(blockFromL1.archive.root)) {
|
|
222
|
-
throw new ReExStateMismatchError(blockFromL1.archive.root, block.archive.root);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private async getValidatorsForEpoch(epochNumber: EpochNumber): Promise<EthAddress[]> {
|
|
227
|
-
const { committee } = await this.epochCache.getCommitteeForEpoch(epochNumber);
|
|
228
|
-
if (!committee) {
|
|
229
|
-
this.log.trace(`No committee found for epoch ${epochNumber}`);
|
|
230
|
-
return [];
|
|
231
|
-
}
|
|
232
|
-
return committee;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private validatorsToSlashingArgs(
|
|
236
|
-
validators: EthAddress[],
|
|
237
|
-
offenseType: OffenseType,
|
|
238
|
-
epochOrSlot: EpochNumber,
|
|
239
|
-
): WantToSlashArgs[] {
|
|
240
|
-
const penalty =
|
|
241
|
-
offenseType === OffenseType.DATA_WITHHOLDING
|
|
242
|
-
? this.penalties.slashDataWithholdingPenalty
|
|
243
|
-
: this.penalties.slashPrunePenalty;
|
|
244
|
-
return validators.map(v => ({
|
|
245
|
-
validator: v,
|
|
246
|
-
amount: penalty,
|
|
247
|
-
offenseType,
|
|
248
|
-
epochOrSlot: BigInt(epochOrSlot),
|
|
249
|
-
}));
|
|
250
|
-
}
|
|
251
|
-
}
|