forked from OffchainLabs/arbitrum-orbit-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetValidators.ts
191 lines (170 loc) Β· 5.9 KB
/
getValidators.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
import {
Address,
Chain,
Hex,
PublicClient,
Transport,
decodeFunctionData,
getAbiItem,
getFunctionSelector,
} from 'viem';
import { rollupCreatorABI } from './contracts/RollupCreator';
import { rollupCreatorABI as rollupCreatorV1Dot1ABI } from './contracts/RollupCreator/v1.1';
import { upgradeExecutorABI } from './contracts/UpgradeExecutor';
import { gnosisSafeL2ABI } from './contracts/GnosisSafeL2';
import { rollupABI } from './contracts/Rollup';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';
const createRollupABI = getAbiItem({ abi: rollupCreatorABI, name: 'createRollup' });
const createRollupFunctionSelector = getFunctionSelector(createRollupABI);
const createRollupV1Dot1ABI = getAbiItem({ abi: rollupCreatorV1Dot1ABI, name: 'createRollup' });
const createRollupV1Dot1FunctionSelector = getFunctionSelector(createRollupV1Dot1ABI);
const setValidatorABI = getAbiItem({ abi: rollupABI, name: 'setValidator' });
const setValidatorFunctionSelector = getFunctionSelector(setValidatorABI);
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: rollupABI, name: 'OwnerFunctionCalled' });
function getValidatorsFromFunctionData<
TAbi extends
| (typeof createRollupABI)[]
| (typeof createRollupV1Dot1ABI)[]
| (typeof setValidatorABI)[],
>({ abi, data }: { abi: TAbi; data: Hex }) {
const { args } = decodeFunctionData({
abi,
data,
});
return args;
}
function updateAccumulator(acc: Set<Address>, input: Hex) {
const [validators, states] = getValidatorsFromFunctionData({
abi: [setValidatorABI],
data: input,
});
validators.forEach((validator, i) => {
const isAdd = states[i];
if (isAdd) {
acc.add(validator);
} else {
acc.delete(validator);
}
});
return acc;
}
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 events = await getLogsWithBatching(publicClient, {
address: rollup,
event: ownerFunctionCalledEventAbi,
args: { id: 6n },
fromBlock: blockNumber,
});
const txs = await Promise.all(
events.map((event) =>
publicClient.getTransaction({
hash: event.transactionHash,
}),
),
);
let isAccurate = true;
const validators = txs.reduce((acc, tx) => {
const txSelectedFunction = tx.input.slice(0, 10);
switch (txSelectedFunction) {
case createRollupFunctionSelector: {
const [{ validators }] = getValidatorsFromFunctionData({
abi: [createRollupABI],
data: tx.input,
});
return new Set([...acc, ...validators]);
}
case createRollupV1Dot1FunctionSelector: {
const [{ validators }] = getValidatorsFromFunctionData({
abi: [createRollupV1Dot1ABI],
data: tx.input,
});
return new Set([...acc, ...validators]);
}
case setValidatorFunctionSelector: {
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 { args: executeCallCalldata } = decodeFunctionData({
abi: [executeCallABI],
data: execTransactionCalldata[2],
});
return updateAccumulator(acc, executeCallCalldata[1]);
}
default: {
console.warn(`[getValidators] unknown 4bytes, tx id: ${tx.hash}`);
isAccurate = false;
return acc;
}
}
}, new Set<Address>());
return {
isAccurate,
validators: [...validators],
};
}