Skip to content

Commit 7cb1124

Browse files
committed
address review comments
1 parent 6414eb8 commit 7cb1124

File tree

1 file changed

+64
-49
lines changed

1 file changed

+64
-49
lines changed

src/getValidators.ts

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -101,64 +101,18 @@ export type GetValidatorsReturnType = {
101101
validators: Address[];
102102
};
103103

104-
/**
105-
*
106-
* @param {PublicClient} publicClient - The chain Viem Public Client
107-
* @param {GetValidatorsParams} GetValidatorsParams {@link GetValidatorsParams}
108-
*
109-
* @returns Promise<{@link GetValidatorsReturnType}>
110-
*
111-
* @remarks validators list is not guaranteed to be exhaustive if the `isAccurate` flag is false.
112-
* It might contain false positive (validators that were removed, but returned as validator)
113-
* or false negative (validators that were added, but not present in the list)
114-
*
115-
* @example
116-
* const { isAccurate, validators } = getValidators(client, { rollup: '0xc47dacfbaa80bd9d8112f4e8069482c2a3221336' });
117-
*
118-
* if (isAccurate) {
119-
* // Validators were all fetched properly
120-
* } else {
121-
* // Validators list is not guaranteed to be accurate
122-
* }
123-
*/
124-
export async function getValidators<TChain extends Chain>(
104+
async function getValidatorsPreV3Dot1<TChain extends Chain>(
125105
publicClient: PublicClient<Transport, TChain>,
126106
{ rollup }: GetValidatorsParams,
107+
blockNumber: bigint,
127108
): Promise<GetValidatorsReturnType> {
128-
let blockNumber: bigint;
129-
try {
130-
const createRollupTransactionHash = await createRollupFetchTransactionHash({
131-
rollup,
132-
publicClient,
133-
});
134-
const receipt = await publicClient.waitForTransactionReceipt({
135-
hash: createRollupTransactionHash,
136-
});
137-
blockNumber = receipt.blockNumber;
138-
} catch (e) {
139-
blockNumber = 0n;
140-
}
141-
142109
const preV3Dot1Events = await getLogsWithBatching(publicClient, {
143110
address: rollup,
144111
event: ownerFunctionCalledEventAbi,
145112
args: { id: 6n },
146113
fromBlock: blockNumber,
147114
});
148115

149-
const v3Dot1ValidatorsSetEvents = await getLogsWithBatching(publicClient, {
150-
address: rollup,
151-
event: validatorsSetEventAbi,
152-
fromBlock: blockNumber,
153-
});
154-
155-
const validatorsFromV3Dot1Events = v3Dot1ValidatorsSetEvents
156-
.filter((event) => event.eventName === 'ValidatorsSet')
157-
.reduce((acc, event) => {
158-
const { validators: _validators, enabled: _enabled } = event.args;
159-
return iterateThroughValidatorsList(acc, _validators, _enabled);
160-
}, new Set<Address>());
161-
162116
/** For pre v3.1, the OwnerFunctionCalled event is emitted when the validators list is updated
163117
* the event is emitted without the validators list and the new states in the event args
164118
* so we have to grab the tx and decode the calldata to get the validators list
@@ -232,10 +186,71 @@ export async function getValidators<TChain extends Chain>(
232186
return acc;
233187
}
234188
}
235-
}, validatorsFromV3Dot1Events);
189+
}, new Set<Address>());
236190

237191
return {
238192
isAccurate,
239193
validators: [...validators],
240194
};
241195
}
196+
197+
/**
198+
*
199+
* @param {PublicClient} publicClient - The chain Viem Public Client
200+
* @param {GetValidatorsParams} GetValidatorsParams {@link GetValidatorsParams}
201+
*
202+
* @returns Promise<{@link GetValidatorsReturnType}>
203+
*
204+
* @remarks validators list is not guaranteed to be exhaustive if the `isAccurate` flag is false.
205+
* It might contain false positive (validators that were removed, but returned as validator)
206+
* or false negative (validators that were added, but not present in the list)
207+
*
208+
* @example
209+
* const { isAccurate, validators } = getValidators(client, { rollup: '0xc47dacfbaa80bd9d8112f4e8069482c2a3221336' });
210+
*
211+
* if (isAccurate) {
212+
* // Validators were all fetched properly
213+
* } else {
214+
* // Validators list is not guaranteed to be accurate
215+
* }
216+
*/
217+
export async function getValidators<TChain extends Chain>(
218+
publicClient: PublicClient<Transport, TChain>,
219+
{ rollup }: GetValidatorsParams,
220+
): Promise<GetValidatorsReturnType> {
221+
let blockNumber: bigint;
222+
try {
223+
const createRollupTransactionHash = await createRollupFetchTransactionHash({
224+
rollup,
225+
publicClient,
226+
});
227+
const receipt = await publicClient.waitForTransactionReceipt({
228+
hash: createRollupTransactionHash,
229+
});
230+
blockNumber = receipt.blockNumber;
231+
} catch (e) {
232+
blockNumber = 0n;
233+
}
234+
235+
const v3Dot1ValidatorsSetEvents = await getLogsWithBatching(publicClient, {
236+
address: rollup,
237+
event: validatorsSetEventAbi,
238+
fromBlock: blockNumber,
239+
});
240+
241+
const validatorsFromV3Dot1Events = v3Dot1ValidatorsSetEvents
242+
.filter((event) => event.eventName === 'ValidatorsSet')
243+
.reduce((acc, event) => {
244+
const { validators: _validators, enabled: _enabled } = event.args;
245+
return iterateThroughValidatorsList(acc, _validators, _enabled);
246+
}, new Set<Address>());
247+
248+
if (validatorsFromV3Dot1Events.size > 0) {
249+
return {
250+
isAccurate: true,
251+
validators: [...validatorsFromV3Dot1Events],
252+
};
253+
}
254+
255+
return await getValidatorsPreV3Dot1(publicClient, { rollup }, blockNumber);
256+
}

0 commit comments

Comments
 (0)