feat: add loggerInfo and loggerErr callbacks to client options

This commit is contained in:
Derrick Hammer 2023-07-14 11:16:38 -04:00
parent 8582db4eb3
commit a901ee76f4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 13 additions and 3 deletions

View File

@ -26,6 +26,8 @@ export interface BaseClientOptions {
store: IStore; store: IStore;
optimisticUpdateCallback: OptimisticUpdateCallback; optimisticUpdateCallback: OptimisticUpdateCallback;
syncDelay?: number; syncDelay?: number;
loggerInfo: (...any) => void;
loggerErr: (...any) => void;
} }
export default abstract class BaseClient { export default abstract class BaseClient {
@ -131,7 +133,9 @@ export default abstract class BaseClient {
); );
if (!(validOrCommittee as boolean)) { if (!(validOrCommittee as boolean)) {
console.log(`Found invalid update at period(${curPeriod})`); this.options.loggerInfo(
`Found invalid update at period(${curPeriod})`,
);
return { return {
syncCommittee: startCommittee, syncCommittee: startCommittee,
period: curPeriod, period: curPeriod,
@ -239,7 +243,7 @@ export default abstract class BaseClient {
this._latestOptimisticUpdate = this._latestOptimisticUpdate =
capella.ssz.LightClientOptimisticUpdate.serialize(update); capella.ssz.LightClientOptimisticUpdate.serialize(update);
console.log( this.options.loggerInfo(
`Optimistic update verified for slot ${update.attestedHeader.beacon.slot}`, `Optimistic update verified for slot ${update.attestedHeader.beacon.slot}`,
); );

View File

@ -29,7 +29,7 @@ export default class Client extends BaseClient {
blockHash, blockHash,
); );
this.subscribe((ei) => { this.subscribe((ei) => {
console.log( this.options.loggerInfo(
`Received a new blockheader: ${ei.blockNumber} ${ei.blockHash}`, `Received a new blockheader: ${ei.blockNumber} ${ei.blockHash}`,
); );
provider.update(ei.blockNumber, ei.blockHash); provider.update(ei.blockNumber, ei.blockHash);

View File

@ -9,6 +9,8 @@ function createDefaultClient(
proverHandler: ProverRequestCallback, proverHandler: ProverRequestCallback,
rpcHandler: Function, rpcHandler: Function,
optimisticUpdateHandler: OptimisticUpdateCallback, optimisticUpdateHandler: OptimisticUpdateCallback,
loggerInfo: (...any) => void,
loggerErr: (...any) => void,
): Client { ): Client {
return new Client({ return new Client({
prover: new Prover(proverHandler), prover: new Prover(proverHandler),
@ -16,6 +18,8 @@ function createDefaultClient(
provider: VerifyingProvider, provider: VerifyingProvider,
rpcHandler, rpcHandler,
optimisticUpdateCallback: optimisticUpdateHandler, optimisticUpdateCallback: optimisticUpdateHandler,
loggerInfo,
loggerErr,
}); });
} }

View File

@ -20,6 +20,8 @@ function createDefaultClient(beaconUrl: string): Client {
return capella.ssz.LightClientOptimisticUpdate.fromJson(update); return capella.ssz.LightClientOptimisticUpdate.fromJson(update);
}, },
loggerInfo: console.log,
loggerErr: console.error,
}; };
const client = new Client(options); const client = new Client(options);