refactor: move optimistic_update fetch logic to utility function

This commit is contained in:
Derrick Hammer 2023-07-11 01:50:42 -04:00
parent 408e1c4034
commit efc657b790
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 15 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import { Bytes32, capella, LightClientUpdate } from "#types.js";
import { import {
consensusClient, consensusClient,
deserializePubkeys, deserializePubkeys,
getConsensusOptimisticUpdate,
optimisticUpdateFromJSON, optimisticUpdateFromJSON,
optimisticUpdateVerify, optimisticUpdateVerify,
} from "#util.js"; } from "#util.js";
@ -87,16 +88,8 @@ export default class Client extends BaseClient {
} }
protected async getLatestExecution(): Promise<ExecutionInfo | null> { protected async getLatestExecution(): Promise<ExecutionInfo | null> {
const resp = await axios.get( const updateJSON = await getConsensusOptimisticUpdate();
`/eth/v1/beacon/light_client/optimistic_update`, const update = optimisticUpdateFromJSON(updateJSON);
);
const updateJSON = resp.data;
if (!updateJSON) {
throw Error(`fetching optimistic update failed`);
}
const update = optimisticUpdateFromJSON(updateJSON.data);
const verify = await optimisticUpdateVerify( const verify = await optimisticUpdateVerify(
this.latestCommittee as Uint8Array[], this.latestCommittee as Uint8Array[],
update, update,

View File

@ -77,3 +77,15 @@ export function getCommitteeHash(committee: Uint8Array[]): Uint8Array {
export const consensusClient = axios.create(); export const consensusClient = axios.create();
axiosRetry(consensusClient, { retries: 3 }); axiosRetry(consensusClient, { retries: 3 });
export async function getConsensusOptimisticUpdate() {
const resp = await consensusClient.get(
`/eth/v1/beacon/light_client/optimistic_update`,
);
const updateJSON = resp.data;
if (!updateJSON) {
throw Error(`fetching optimistic update failed`);
}
}