-
Notifications
You must be signed in to change notification settings - Fork 597
Expand file tree
/
Copy pathsequencers.ts
More file actions
120 lines (106 loc) · 4.26 KB
/
sequencers.ts
File metadata and controls
120 lines (106 loc) · 4.26 KB
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
import { Fr, createCompatibleClient } from '@aztec/aztec.js';
import { GSEContract, RollupContract, createEthereumChain, getL1ContractsConfigEnvVars } from '@aztec/ethereum';
import type { LogFn, Logger } from '@aztec/foundation/log';
import { RollupAbi, TestERC20Abi } from '@aztec/l1-artifacts';
import { createPublicClient, createWalletClient, fallback, getContract, http } from '@spalladino/viem';
import { mnemonicToAccount } from '@spalladino/viem/accounts';
export async function sequencers(opts: {
command: 'list' | 'add' | 'remove' | 'who-next' | 'flush';
who?: string;
mnemonic?: string;
bn254SecretKey?: bigint;
rpcUrl: string;
l1RpcUrls: string[];
chainId: number;
blockNumber?: number;
log: LogFn;
debugLogger: Logger;
}) {
const { command, who: maybeWho, mnemonic, bn254SecretKey, rpcUrl, l1RpcUrls, chainId, log, debugLogger } = opts;
const client = await createCompatibleClient(rpcUrl, debugLogger);
const { l1ContractAddresses } = await client.getNodeInfo();
const chain = createEthereumChain(l1RpcUrls, chainId);
const publicClient = createPublicClient({
chain: chain.chainInfo,
transport: fallback(l1RpcUrls.map(url => http(url))),
});
const walletClient = mnemonic
? createWalletClient({
account: mnemonicToAccount(mnemonic),
chain: chain.chainInfo,
transport: fallback(l1RpcUrls.map(url => http(url))),
})
: undefined;
const rollup = new RollupContract(publicClient, l1ContractAddresses.rollupAddress);
const writeableRollup = walletClient
? getContract({
address: l1ContractAddresses.rollupAddress.toString(),
abi: RollupAbi,
client: walletClient,
})
: undefined;
const who = (maybeWho as `0x{string}`) ?? walletClient?.account.address.toString();
if (command === 'list') {
const sequencers = await rollup.getAttesters();
if (sequencers.length === 0) {
log(`No sequencers registered on rollup`);
} else {
log(`Registered sequencers on rollup:`);
for (const sequencer of sequencers) {
log(' ' + sequencer.toString());
}
}
} else if (command === 'add') {
if (!who || !writeableRollup || !walletClient) {
throw new Error(`Missing sequencer address`);
}
log(`Adding ${who} as sequencer`);
const stakingAsset = getContract({
address: await rollup.getStakingAsset(),
abi: TestERC20Abi,
client: walletClient,
});
const config = getL1ContractsConfigEnvVars();
const bn254SecretKeyFieldElement = bn254SecretKey ? new Fr(bn254SecretKey) : Fr.random();
const gseAddress = await rollup.getGSE();
const gseContract = new GSEContract(publicClient, gseAddress);
const registrationTuple = await gseContract.makeRegistrationTuple(bn254SecretKeyFieldElement.toBigInt());
await Promise.all(
[
await stakingAsset.write.mint([walletClient.account.address, config.activationThreshold], {} as any),
await stakingAsset.write.approve([rollup.address, config.activationThreshold], {} as any),
].map(txHash => publicClient.waitForTransactionReceipt({ hash: txHash })),
);
const hash = await writeableRollup.write.deposit([
who,
who,
registrationTuple.publicKeyInG1,
registrationTuple.publicKeyInG2,
registrationTuple.proofOfPossession,
true,
]);
await publicClient.waitForTransactionReceipt({ hash });
log(`Added in tx ${hash}`);
} else if (command === 'flush') {
if (!writeableRollup) {
throw new Error(`Missing sequencer address`);
}
log(`Flushing staking entry queue`);
const hash = await writeableRollup.write.flushEntryQueue();
await publicClient.waitForTransactionReceipt({ hash });
log(`Flushed staking entry queue in tx ${hash}`);
} else if (command === 'remove') {
if (!who || !writeableRollup) {
throw new Error(`Missing sequencer address`);
}
log(`Removing ${who} as sequencer`);
const hash = await writeableRollup.write.initiateWithdraw([who, who]);
await publicClient.waitForTransactionReceipt({ hash });
log(`Removed in tx ${hash}`);
} else if (command === 'who-next') {
const next = await rollup.getCurrentProposer();
log(`Sequencer expected to build is ${next}`);
} else {
throw new Error(`Unknown command ${command}`);
}
}