Skip to content

Commit

Permalink
Add proposerBalanceDiff metric
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jul 11, 2022
1 parent 7bf6764 commit b0941dd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ export async function importBlock(chain: ImportBlockModules, fullyVerifiedBlock:

// Register stat metrics about the block after importing it
chain.metrics?.parentBlockDistance.observe(block.message.slot - parentBlock.slot);
chain.metrics?.proposerBalanceDiffAny.observe(fullyVerifiedBlock.proposerBalanceDiff);
chain.metrics?.registerImportedBlock(block.message, fullyVerifiedBlock);

// Note: in-lined from previous handler of ChainEvent.block
const blockRoot = toHexString(chain.config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message));
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type FullyVerifiedBlock = FullyVerifiedBlockFlags & {
block: allForks.SignedBeaconBlock;
postState: CachedBeaconStateAllForks;
parentBlock: ProtoBlock;
proposerBalanceDiff: number;
};

/**
Expand Down
15 changes: 12 additions & 3 deletions packages/beacon-node/src/chain/blocks/verifyBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ export async function verifyBlock(
): Promise<FullyVerifiedBlock> {
const parentBlock = verifyBlockSanityChecks(chain, partiallyVerifiedBlock);

const {postState, executionStatus} = await verifyBlockStateTransition(chain, partiallyVerifiedBlock, opts);
const {postState, executionStatus, proposerBalanceDiff} = await verifyBlockStateTransition(
chain,
partiallyVerifiedBlock,
opts
);

return {
block: partiallyVerifiedBlock.block,
postState,
parentBlock,
skipImportingAttestations: partiallyVerifiedBlock.skipImportingAttestations,
executionStatus,
proposerBalanceDiff,
};
}

Expand Down Expand Up @@ -128,7 +133,7 @@ export async function verifyBlockStateTransition(
chain: VerifyBlockModules,
partiallyVerifiedBlock: PartiallyVerifiedBlock,
opts: BlockProcessOpts
): Promise<{postState: CachedBeaconStateAllForks; executionStatus: ExecutionStatus}> {
): Promise<{postState: CachedBeaconStateAllForks; executionStatus: ExecutionStatus; proposerBalanceDiff: number}> {
const {block, validProposerSignature, validSignatures} = partiallyVerifiedBlock;

// TODO: Skip in process chain segment
Expand Down Expand Up @@ -350,7 +355,11 @@ export async function verifyBlockStateTransition(
logOnPowBlock(chain, block as bellatrix.SignedBeaconBlock);
}

return {postState, executionStatus};
// For metric block profitability
const proposerIndex = block.message.proposerIndex;
const proposerBalanceDiff = postState.balances.get(proposerIndex) - preState.balances.get(proposerIndex);

return {postState, executionStatus, proposerBalanceDiff};
}

function logOnPowBlock(chain: VerifyBlockModules, block: bellatrix.SignedBeaconBlock): void {
Expand Down
15 changes: 15 additions & 0 deletions packages/beacon-node/src/metrics/metrics/lodestar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,23 @@ export function createLodestarMetrics(
labelNames: ["index", "src"],
buckets: [0.1, 1],
}),

// Only for known
proposerBalanceDiffKnown: register.histogram({
name: "validator_monitor_proposer_balance_diff_known_gwei",
help: "Balance diff of known block proposer after importing a valid block",
// TODO: Research what buckets make sense
buckets: [0, 10_000, 100_000],
}),
},

proposerBalanceDiffAny: register.histogram({
name: "lodestar_proposer_balance_diff_any_gwei",
help: "Balance diff of every block proposer after importing a valid block",
// TODO: Research what buckets make sense
buckets: [0, 10_000, 100_000],
}),

// regen metrics

stateCache: {
Expand Down
7 changes: 7 additions & 0 deletions packages/beacon-node/src/metrics/validatorMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IValidatorMonitor {
registerLocalValidator(index: number): void;
registerValidatorStatuses(currentEpoch: Epoch, statuses: IAttesterStatus[], balances?: number[]): void;
registerBeaconBlock(src: OpSource, seenTimestampSec: Seconds, block: allForks.BeaconBlock): void;
registerImportedBlock(block: allForks.BeaconBlock, data: {proposerBalanceDiff: number}): void;
submitUnaggregatedAttestation(
seenTimestampSec: number,
indexedAttestation: IndexedAttestation,
Expand Down Expand Up @@ -275,6 +276,12 @@ export function createValidatorMonitor(
}
},

registerImportedBlock(block, {proposerBalanceDiff}) {
if (validators.has(block.proposerIndex)) {
metrics.validatorMonitor.proposerBalanceDiffKnown.observe(proposerBalanceDiff);
}
},

submitUnaggregatedAttestation(seenTimestampSec, indexedAttestation, subnet, sentPeers) {
const data = indexedAttestation.data;
// Returns the duration between when the attestation `data` could be produced (1/3rd through the slot) and `seenTimestamp`.
Expand Down

0 comments on commit b0941dd

Please sign in to comment.