-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathupdate_l1_validators.ts
212 lines (194 loc) · 7.28 KB
/
update_l1_validators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { EthCheatCodes } from '@aztec/aztec.js';
import { type EthAddress } from '@aztec/circuits.js';
import { createEthereumChain, getL1ContractsConfigEnvVars, isAnvilTestChain } from '@aztec/ethereum';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';
import { RollupAbi } from '@aztec/l1-artifacts';
import { createPublicClient, createWalletClient, getContract, http } from 'viem';
import { generatePrivateKey, mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';
export interface RollupCommandArgs {
rpcUrl: string;
chainId: number;
privateKey?: string;
mnemonic?: string;
rollupAddress: EthAddress;
}
export interface LoggerArgs {
log: LogFn;
debugLogger: DebugLogger;
}
export function generateL1Account() {
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);
account.address;
return {
privateKey,
address: account.address,
};
}
export async function addL1Validator({
rpcUrl,
chainId,
privateKey,
mnemonic,
validatorAddress,
rollupAddress,
log,
debugLogger,
}: RollupCommandArgs & LoggerArgs & { validatorAddress: EthAddress }) {
const dualLog = makeDualLog(log, debugLogger);
const publicClient = getPublicClient(rpcUrl, chainId);
const walletClient = getWalletClient(rpcUrl, chainId, privateKey, mnemonic);
const rollup = getContract({
address: rollupAddress.toString(),
abi: RollupAbi,
client: walletClient,
});
dualLog(`Adding validator ${validatorAddress.toString()} to rollup ${rollupAddress.toString()}`);
const txHash = await rollup.write.addValidator([validatorAddress.toString()]);
dualLog(`Transaction hash: ${txHash}`);
await publicClient.waitForTransactionReceipt({ hash: txHash });
if (isAnvilTestChain(chainId)) {
dualLog(`Funding validator on L1`);
const cheatCodes = new EthCheatCodes(rpcUrl, debugLogger);
await cheatCodes.setBalance(validatorAddress, 10n ** 20n);
} else {
const balance = await publicClient.getBalance({ address: validatorAddress.toString() });
const balanceInEth = Number(balance) / 10 ** 18;
dualLog(`Validator balance: ${balanceInEth.toFixed(6)} ETH`);
if (balanceInEth === 0) {
dualLog(`WARNING: Validator has no balance. Remember to fund it!`);
}
}
}
export async function removeL1Validator({
rpcUrl,
chainId,
privateKey,
mnemonic,
validatorAddress,
rollupAddress,
log,
debugLogger,
}: RollupCommandArgs & LoggerArgs & { validatorAddress: EthAddress }) {
const dualLog = makeDualLog(log, debugLogger);
const publicClient = getPublicClient(rpcUrl, chainId);
const walletClient = getWalletClient(rpcUrl, chainId, privateKey, mnemonic);
const rollup = getContract({
address: rollupAddress.toString(),
abi: RollupAbi,
client: walletClient,
});
dualLog(`Removing validator ${validatorAddress.toString()} from rollup ${rollupAddress.toString()}`);
const txHash = await rollup.write.removeValidator([validatorAddress.toString()]);
dualLog(`Transaction hash: ${txHash}`);
await publicClient.waitForTransactionReceipt({ hash: txHash });
}
export async function pruneRollup({
rpcUrl,
chainId,
privateKey,
mnemonic,
rollupAddress,
log,
debugLogger,
}: RollupCommandArgs & LoggerArgs) {
const dualLog = makeDualLog(log, debugLogger);
const publicClient = getPublicClient(rpcUrl, chainId);
const walletClient = getWalletClient(rpcUrl, chainId, privateKey, mnemonic);
const rollup = getContract({
address: rollupAddress.toString(),
abi: RollupAbi,
client: walletClient,
});
dualLog(`Trying prune`);
const txHash = await rollup.write.prune();
dualLog(`Transaction hash: ${txHash}`);
await publicClient.waitForTransactionReceipt({ hash: txHash });
}
export async function fastForwardEpochs({
rpcUrl,
chainId,
rollupAddress,
numEpochs,
log,
debugLogger,
}: RollupCommandArgs & LoggerArgs & { numEpochs: bigint }) {
const dualLog = makeDualLog(log, debugLogger);
const publicClient = getPublicClient(rpcUrl, chainId);
const rollup = getContract({
address: rollupAddress.toString(),
abi: RollupAbi,
client: publicClient,
});
const cheatCodes = new EthCheatCodes(rpcUrl, debugLogger);
const currentSlot = await rollup.read.getCurrentSlot();
const l2SlotsInEpoch = await rollup.read.EPOCH_DURATION();
const timestamp = await rollup.read.getTimestampForSlot([currentSlot + l2SlotsInEpoch * numEpochs]);
dualLog(`Fast forwarding ${numEpochs} epochs to ${timestamp}`);
try {
await cheatCodes.warp(Number(timestamp));
dualLog(`Fast forwarded ${numEpochs} epochs to ${timestamp}`);
} catch (error) {
if (error instanceof Error && error.message.includes("is lower than or equal to previous block's timestamp")) {
dualLog(`Someone else fast forwarded the chain to a point after/equal to the target time`);
} else {
// Re-throw other errors
throw error;
}
}
}
export async function debugRollup({ rpcUrl, chainId, rollupAddress, log }: RollupCommandArgs & LoggerArgs) {
const config = getL1ContractsConfigEnvVars();
const publicClient = getPublicClient(rpcUrl, chainId);
const rollup = getContract({
address: rollupAddress.toString(),
abi: RollupAbi,
client: publicClient,
});
const pendingNum = await rollup.read.getPendingBlockNumber();
log(`Pending block num: ${pendingNum}`);
const provenNum = await rollup.read.getProvenBlockNumber();
log(`Proven block num: ${provenNum}`);
const validators = await rollup.read.getValidators();
log(`Validators: ${validators.map(v => v.toString()).join(', ')}`);
const committee = await rollup.read.getCurrentEpochCommittee();
log(`Committee: ${committee.map(v => v.toString()).join(', ')}`);
const archive = await rollup.read.archive();
log(`Archive: ${archive}`);
const epochNum = await rollup.read.getCurrentEpoch();
log(`Current epoch: ${epochNum}`);
const epoch = await rollup.read.epochs([epochNum]);
log(`Epoch Sample Seed: ${epoch[0].toString()}, Next Seed: ${epoch[1].toString()}`);
const slot = await rollup.read.getCurrentSlot();
log(`Current slot: ${slot}`);
const proposerDuringPrevL1Block = await rollup.read.getCurrentProposer();
log(`Proposer during previous L1 block: ${proposerDuringPrevL1Block}`);
const nextBlockTS = BigInt((await publicClient.getBlock()).timestamp + BigInt(config.ethereumSlotDuration));
const proposer = await rollup.read.getProposerAt([nextBlockTS]);
log(`Proposer NOW: ${proposer.toString()}`);
}
function makeDualLog(log: LogFn, debugLogger: DebugLogger) {
return (msg: string) => {
log(msg);
debugLogger.info(msg);
};
}
function getPublicClient(rpcUrl: string, chainId: number) {
const chain = createEthereumChain(rpcUrl, chainId);
return createPublicClient({ chain: chain.chainInfo, transport: http(rpcUrl) });
}
function getWalletClient(
rpcUrl: string,
chainId: number,
privateKey: string | undefined,
mnemonic: string | undefined,
) {
if (!privateKey && !mnemonic) {
throw new Error('Either privateKey or mnemonic must be provided to create a wallet client');
}
const chain = createEthereumChain(rpcUrl, chainId);
const account = !privateKey
? mnemonicToAccount(mnemonic!)
: privateKeyToAccount(`${privateKey.startsWith('0x') ? '' : '0x'}${privateKey}` as `0x${string}`);
return createWalletClient({ account, chain: chain.chainInfo, transport: http(rpcUrl) });
}