Skip to content

Commit

Permalink
Truncate and format bytes as 0x123456789abc
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Oct 2, 2023
1 parent d40f1cd commit 0fa1682
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/utils/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ export function prettyBytesShort(root: Uint8Array | string): string {
const str = typeof root === "string" ? root : toHexString(root);
return `${str.slice(0, 6)}…`;
}

/**
* Truncate and format bytes as `0x123456789abc`
* 6 bytes is sufficient to avoid collisions and it allows to easily look up
* values on explorers like beaconcha.in while improving readability of logs
*/
export function truncBytes(root: Uint8Array | string): string {
const str = typeof root === "string" ? root : toHexString(root);
return str.slice(0, 14);
}
8 changes: 4 additions & 4 deletions packages/validator/src/services/doppelgangerService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {fromHexString} from "@chainsafe/ssz";
import {Epoch, ValidatorIndex} from "@lodestar/types";
import {Api, ApiError, routes} from "@lodestar/api";
import {Logger, prettyBytes, sleep} from "@lodestar/utils";
import {Logger, sleep, truncBytes} from "@lodestar/utils";
import {computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {ISlashingProtection} from "../slashingProtection/index.js";
import {ProcessShutdownCallback, PubkeyHex} from "../types.js";
Expand Down Expand Up @@ -76,19 +76,19 @@ export class DoppelgangerService {
if (attestedInPreviousEpoch) {
remainingEpochs = REMAINING_EPOCHS_IF_SKIPPED;
this.logger.info("Doppelganger detection skipped, attestation from previous epoch exists in database", {
pubkey: prettyBytes(pubkeyHex),
pubkey: truncBytes(pubkeyHex),
previousEpoch,
});
} else {
this.logger.info("Registered validator for doppelganger detection", {
pubkey: prettyBytes(pubkeyHex),
pubkey: truncBytes(pubkeyHex),
remainingEpochs,
nextEpochToCheck,
});
}
} else {
this.logger.info("Doppelganger detection skipped, validator initialized before genesis", {
pubkey: prettyBytes(pubkeyHex),
pubkey: truncBytes(pubkeyHex),
currentEpoch,
});
}
Expand Down

0 comments on commit 0fa1682

Please sign in to comment.