Skip to content

Commit ddd0585

Browse files
committed
clean up code
1 parent 1b2a9ee commit ddd0585

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/getValidators.integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function setValidator(validator: Address, state: boolean) {
3939

4040
// Tests can be enabled once we run one node per integration test
4141
describe('successfully get validators', () => {
42-
it('when disabling the same validator multiple time', async () => {
42+
it('when disabling the same validator multiple times', async () => {
4343
const randomAccount = privateKeyToAccount(generatePrivateKey()).address;
4444

4545
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(
@@ -79,7 +79,7 @@ describe('successfully get validators', () => {
7979
expect(isAccurateFinal).toBeTruthy();
8080
});
8181

82-
it('when enabling the same validators multiple time', async () => {
82+
it('when enabling the same validators multiple times', async () => {
8383
const randomAccount = privateKeyToAccount(generatePrivateKey()).address;
8484

8585
const { isAccurate: isAccurateInitially, validators: initialValidators } = await getValidators(

src/getValidators.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,36 @@ function getValidatorsFromFunctionData<
6262
return args;
6363
}
6464

65-
function updateAccumulator(acc: Set<Address>, input: Hex) {
66-
const [validators, states] = getValidatorsFromFunctionData({
67-
abi: [setValidatorV3Dot1ABI],
68-
data: input,
69-
});
65+
function iterateThroughValidatorsList(
66+
acc: Set<Address>,
67+
validators: Readonly<Address[]> | undefined,
68+
enabled: Readonly<boolean[]> | undefined,
69+
) {
70+
if (typeof validators === 'undefined' || typeof enabled === 'undefined') {
71+
return acc;
72+
}
73+
74+
const copy = new Set<Address>(acc);
7075

7176
validators.forEach((validator, i) => {
72-
const isAdd = states[i];
77+
const isAdd = enabled[i];
7378
if (isAdd) {
74-
acc.add(validator);
79+
copy.add(validator);
7580
} else {
76-
acc.delete(validator);
81+
copy.delete(validator);
7782
}
7883
});
7984

80-
return acc;
85+
return copy;
86+
}
87+
88+
function updateAccumulator(acc: Set<Address>, input: Hex) {
89+
const [validators, enabled] = getValidatorsFromFunctionData({
90+
abi: [setValidatorV3Dot1ABI],
91+
data: input,
92+
});
93+
94+
return iterateThroughValidatorsList(acc, validators, enabled);
8195
}
8296

8397
export type GetValidatorsParams = {
@@ -150,36 +164,16 @@ export async function getValidators<TChain extends Chain>(
150164
.filter((event) => event.eventName === 'ValidatorsSet')
151165
.reduce((acc, event) => {
152166
const { validators: _validators, enabled: _enabled } = event.args;
153-
154-
if (typeof _validators === 'undefined' || typeof _enabled === 'undefined') {
155-
return acc;
156-
}
157-
158-
const copy = new Set<Address>(acc);
159-
160-
for (let i = 0; i < _validators.length; i++) {
161-
if (_enabled[i]) {
162-
copy.add(_validators[i]);
163-
} else {
164-
copy.delete(_validators[i]);
165-
}
166-
}
167-
168-
return copy;
167+
return iterateThroughValidatorsList(acc, _validators, _enabled);
169168
}, new Set<Address>());
170169

171-
const txs = await Promise.all([
172-
...preV3Dot1Events.map((event) =>
173-
publicClient.getTransaction({
174-
hash: event.transactionHash,
175-
}),
176-
),
177-
...v3Dot1ValidatorsSetEvents.map((event) =>
170+
const txs = await Promise.all(
171+
preV3Dot1Events.map((event) =>
178172
publicClient.getTransaction({
179173
hash: event.transactionHash,
180174
}),
181175
),
182-
]);
176+
);
183177

184178
let isAccurate = true;
185179
const validators = txs.reduce((acc, tx) => {
@@ -253,7 +247,7 @@ export async function getValidators<TChain extends Chain>(
253247
return acc;
254248
}
255249
}
256-
}, new Set<Address>());
250+
}, validatorsFromV3Dot1Events);
257251

258252
return {
259253
isAccurate,

0 commit comments

Comments
 (0)