From e245f833e56b05cf11850cb8537d9dbba01de746 Mon Sep 17 00:00:00 2001 From: Joe Andrews Date: Thu, 10 Oct 2024 17:02:10 +0100 Subject: [PATCH] feat: add validator address to logs (#9143) Add address logging to validators for metrics dashboard in validator.ts --------- Co-authored-by: Mitch Co-authored-by: Rahul Kothari --- yarn-project/foundation/src/log/logger.ts | 25 +++++++++++++------ .../src/key_store/interface.ts | 8 ++++++ .../src/key_store/local_key_store.ts | 10 ++++++++ .../validator-client/src/validator.ts | 4 +-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/yarn-project/foundation/src/log/logger.ts b/yarn-project/foundation/src/log/logger.ts index 0f245882550..9a0dd25cd4f 100644 --- a/yarn-project/foundation/src/log/logger.ts +++ b/yarn-project/foundation/src/log/logger.ts @@ -36,22 +36,31 @@ export type DebugLogger = Logger; * If DEBUG="[module]" env is set, will enable debug logging if the module matches. * Uses npm debug for debug level and console.error for other levels. * @param name - Name of the module. + * @param fixedLogData - Additional data to include in the log message. + * @usage createDebugLogger('aztec:validator', {validatorAddress: '0x1234...'}); + * // will always add the validator address to the log labels * @returns A debug logger. */ -export function createDebugLogger(name: string): DebugLogger { + +export function createDebugLogger(name: string, fixedLogData?: LogData): DebugLogger { const debugLogger = debug(name); + const attatchFixedLogData = (data?: LogData) => ({ ...fixedLogData, ...data }); + const logger = { silent: () => {}, - error: (msg: string, err?: unknown, data?: LogData) => logWithDebug(debugLogger, 'error', fmtErr(msg, err), data), - warn: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'warn', msg, data), - info: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'info', msg, data), - verbose: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'verbose', msg, data), - debug: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, data), + error: (msg: string, err?: unknown, data?: LogData) => + logWithDebug(debugLogger, 'error', fmtErr(msg, err), attatchFixedLogData(data)), + warn: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'warn', msg, attatchFixedLogData(data)), + info: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'info', msg, attatchFixedLogData(data)), + verbose: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'verbose', msg, attatchFixedLogData(data)), + debug: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, attatchFixedLogData(data)), }; - return Object.assign((msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, data), logger); + return Object.assign( + (msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, attatchFixedLogData(data)), + logger, + ); } - /** A callback to capture all logs. */ export type LogHandler = (level: LogLevel, namespace: string, msg: string, data?: LogData) => void; diff --git a/yarn-project/validator-client/src/key_store/interface.ts b/yarn-project/validator-client/src/key_store/interface.ts index 2b96c90377b..a3ded5580a1 100644 --- a/yarn-project/validator-client/src/key_store/interface.ts +++ b/yarn-project/validator-client/src/key_store/interface.ts @@ -1,3 +1,4 @@ +import { type EthAddress } from '@aztec/circuits.js'; import { type Buffer32 } from '@aztec/foundation/buffer'; import { type Signature } from '@aztec/foundation/eth-signature'; @@ -6,6 +7,13 @@ import { type Signature } from '@aztec/foundation/eth-signature'; * A keystore interface that can be replaced with a local keystore / remote signer service */ export interface ValidatorKeyStore { + /** + * Get the address of the signer + * + * @returns the address + */ + getAddress(): EthAddress; + sign(message: Buffer32): Promise; /** * Flavor of sign message that followed EIP-712 eth signed message prefix diff --git a/yarn-project/validator-client/src/key_store/local_key_store.ts b/yarn-project/validator-client/src/key_store/local_key_store.ts index 736ee361a45..436bf4b0c33 100644 --- a/yarn-project/validator-client/src/key_store/local_key_store.ts +++ b/yarn-project/validator-client/src/key_store/local_key_store.ts @@ -1,5 +1,6 @@ import { type Buffer32 } from '@aztec/foundation/buffer'; import { Secp256k1Signer } from '@aztec/foundation/crypto'; +import { type EthAddress } from '@aztec/foundation/eth-address'; import { type Signature } from '@aztec/foundation/eth-signature'; import { type ValidatorKeyStore } from './interface.js'; @@ -16,6 +17,15 @@ export class LocalKeyStore implements ValidatorKeyStore { this.signer = new Secp256k1Signer(privateKey); } + /** + * Get the address of the signer + * + * @returns the address + */ + public getAddress(): EthAddress { + return this.signer.address; + } + /** * Sign a message with the keystore private key * diff --git a/yarn-project/validator-client/src/validator.ts b/yarn-project/validator-client/src/validator.ts index d364f9565fe..a3d72931695 100644 --- a/yarn-project/validator-client/src/validator.ts +++ b/yarn-project/validator-client/src/validator.ts @@ -40,9 +40,9 @@ export class ValidatorClient extends WithTracer implements Validator { private attestationPoolingIntervalMs: number, private attestationWaitTimeoutMs: number, telemetry: TelemetryClient, - private log = createDebugLogger('aztec:validator'), + private log = createDebugLogger('aztec:validator', { validatorAddress: keyStore.getAddress().toString() }), ) { - // Instatntiate tracer + // Instantiate tracer super(telemetry, 'Validator'); //TODO: We need to setup and store all of the currently active validators https://github.com/AztecProtocol/aztec-packages/issues/7962