-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathgetValidators.ts
More file actions
241 lines (210 loc) Β· 7.82 KB
/
getValidators.ts
File metadata and controls
241 lines (210 loc) Β· 7.82 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
Address,
Chain,
Hex,
PublicClient,
Transport,
decodeFunctionData,
getAbiItem,
getFunctionSelector,
} from 'viem';
import { rollupCreatorABI as rollupCreatorV2Dot1ABI } from './contracts/RollupCreator/v2.1';
import { rollupCreatorABI as rollupCreatorV1Dot1ABI } from './contracts/RollupCreator/v1.1';
import { upgradeExecutorABI } from './contracts/UpgradeExecutor';
import { gnosisSafeL2ABI } from './contracts/GnosisSafeL2';
import { rollupABI as rollupV3Dot1ABI } from './contracts/Rollup';
import { rollupABI as rollupV2Dot1ABI } from './contracts/Rollup/v2.1';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';
const createRollupV2Dot1ABI = getAbiItem({ abi: rollupCreatorV2Dot1ABI, name: 'createRollup' });
const createRollupV2Dot1FunctionSelector = getFunctionSelector(createRollupV2Dot1ABI);
const createRollupV1Dot1ABI = getAbiItem({ abi: rollupCreatorV1Dot1ABI, name: 'createRollup' });
const createRollupV1Dot1FunctionSelector = getFunctionSelector(createRollupV1Dot1ABI);
const setValidatorPreV3Dot1ABI = getAbiItem({ abi: rollupV2Dot1ABI, name: 'setValidator' });
const setValidatorPreV3Dot1FunctionSelector = getFunctionSelector(setValidatorPreV3Dot1ABI);
const executeCallABI = getAbiItem({ abi: upgradeExecutorABI, name: 'executeCall' });
const upgradeExecutorExecuteCallFunctionSelector = getFunctionSelector(executeCallABI);
const execTransactionABI = getAbiItem({ abi: gnosisSafeL2ABI, name: 'execTransaction' });
const safeL2FunctionSelector = getFunctionSelector(execTransactionABI);
const ownerFunctionCalledEventAbi = getAbiItem({
abi: rollupV2Dot1ABI,
name: 'OwnerFunctionCalled',
});
const validatorsSetEventAbi = getAbiItem({ abi: rollupV3Dot1ABI, name: 'ValidatorsSet' });
function getValidatorsFromFunctionData<
TAbi extends
| (typeof createRollupV2Dot1ABI)[]
| (typeof createRollupV1Dot1ABI)[]
| (typeof setValidatorPreV3Dot1ABI)[],
>({ abi, data }: { abi: TAbi; data: Hex }) {
const { args } = decodeFunctionData({
abi,
data,
});
return args;
}
function iterateThroughValidatorsList(
acc: Set<Address>,
validators: Readonly<Address[]> | undefined,
enabled: Readonly<boolean[]> | undefined,
) {
if (typeof validators === 'undefined' || typeof enabled === 'undefined') {
return acc;
}
const copy = new Set<Address>(acc);
validators.forEach((validator, i) => {
const isAdd = enabled[i];
if (isAdd) {
copy.add(validator);
} else {
copy.delete(validator);
}
});
return copy;
}
function updateAccumulator(acc: Set<Address>, input: Hex) {
const [validators, enabled] = getValidatorsFromFunctionData({
abi: [setValidatorPreV3Dot1ABI],
data: input,
});
return iterateThroughValidatorsList(acc, validators, enabled);
}
export type GetValidatorsParams = {
/** Address of the rollup we're getting list of validators from */
rollup: Address;
};
export type GetValidatorsReturnType = {
/**
* If logs contain unknown signature, validators list might:
* - contain false positives (validators that were removed, but returned as validator)
* - contain false negatives (validators that were added, but not present in the list)
*/
isAccurate: boolean;
/** List of validators for the given rollup */
validators: Address[];
};
/**
*
* @param {PublicClient} publicClient - The chain Viem Public Client
* @param {GetValidatorsParams} GetValidatorsParams {@link GetValidatorsParams}
*
* @returns Promise<{@link GetValidatorsReturnType}>
*
* @remarks validators list is not guaranteed to be exhaustive if the `isAccurate` flag is false.
* It might contain false positive (validators that were removed, but returned as validator)
* or false negative (validators that were added, but not present in the list)
*
* @example
* const { isAccurate, validators } = getValidators(client, { rollup: '0xc47dacfbaa80bd9d8112f4e8069482c2a3221336' });
*
* if (isAccurate) {
* // Validators were all fetched properly
* } else {
* // Validators list is not guaranteed to be accurate
* }
*/
export async function getValidators<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
{ rollup }: GetValidatorsParams,
): Promise<GetValidatorsReturnType> {
let blockNumber: bigint;
try {
const createRollupTransactionHash = await createRollupFetchTransactionHash({
rollup,
publicClient,
});
const receipt = await publicClient.waitForTransactionReceipt({
hash: createRollupTransactionHash,
});
blockNumber = receipt.blockNumber;
} catch (e) {
blockNumber = 0n;
}
const preV3Dot1Events = await getLogsWithBatching(publicClient, {
address: rollup,
event: ownerFunctionCalledEventAbi,
args: { id: 6n },
fromBlock: blockNumber,
});
const v3Dot1ValidatorsSetEvents = await getLogsWithBatching(publicClient, {
address: rollup,
event: validatorsSetEventAbi,
fromBlock: blockNumber,
});
const validatorsFromV3Dot1Events = v3Dot1ValidatorsSetEvents
.filter((event) => event.eventName === 'ValidatorsSet')
.reduce((acc, event) => {
const { validators: _validators, enabled: _enabled } = event.args;
return iterateThroughValidatorsList(acc, _validators, _enabled);
}, new Set<Address>());
/** For pre v3.1, the OwnerFunctionCalled event is emitted when the validators list is updated
* the event is emitted without the validators list and the new states in the event args
* so we have to grab the tx and decode the calldata to get the validators list
*/
const preV3Dot1Txs = await Promise.all(
preV3Dot1Events.map((event) =>
publicClient.getTransaction({
hash: event.transactionHash,
}),
),
);
let isAccurate = true;
const validators = preV3Dot1Txs.reduce((acc, tx) => {
const txSelectedFunction = tx.input.slice(0, 10);
switch (txSelectedFunction) {
case createRollupV2Dot1FunctionSelector: {
const [{ validators }] = getValidatorsFromFunctionData({
abi: [createRollupV2Dot1ABI],
data: tx.input,
});
return new Set([...acc, ...validators]);
}
case createRollupV1Dot1FunctionSelector: {
const [{ validators }] = getValidatorsFromFunctionData({
abi: [createRollupV1Dot1ABI],
data: tx.input,
});
return new Set([...acc, ...validators]);
}
case setValidatorPreV3Dot1FunctionSelector: {
return updateAccumulator(acc, tx.input);
}
case upgradeExecutorExecuteCallFunctionSelector: {
const { args: executeCallCalldata } = decodeFunctionData({
abi: [executeCallABI],
data: tx.input,
});
return updateAccumulator(acc, executeCallCalldata[1]);
}
case safeL2FunctionSelector: {
const { args: execTransactionCalldata } = decodeFunctionData({
abi: [execTransactionABI],
data: tx.input,
});
const execTransactionCalldataData = execTransactionCalldata[2];
const execTransactionCalldataDataFnSelector = execTransactionCalldataData.slice(0, 10);
if (execTransactionCalldataDataFnSelector !== upgradeExecutorExecuteCallFunctionSelector) {
console.warn(
`[getValidators] unable to decode "execTransaction" calldata, tx id: ${tx.hash}`,
);
isAccurate = false;
return acc;
}
const { args: executeCallCalldata } = decodeFunctionData({
abi: [executeCallABI],
data: execTransactionCalldataData,
});
return updateAccumulator(acc, executeCallCalldata[1]);
}
default: {
console.warn(`[getValidators] unknown 4bytes, tx id: ${tx.hash}`);
isAccurate = false;
return acc;
}
}
}, validatorsFromV3Dot1Events);
return {
isAccurate,
validators: [...validators],
};
}