Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve compute new state root when producing block #6195

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function verifyBlocksStateTransitionOnly(
metrics
);

const hashTreeRootTimer = metrics?.stateHashTreeRootTime.startTimer();
const hashTreeRootTimer = metrics?.stateHashTreeRootTime.startTimer({source: "block_transition"});
const stateRoot = postState.hashTreeRoot();
hashTreeRootTimer?.();

Expand Down
6 changes: 6 additions & 0 deletions packages/beacon-node/src/chain/prepareNextSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export class PrepareNextSlotScheduler {
RegenCaller.precomputeEpoch
);

// cache HashObjects for faster hashTreeRoot() later, especially for computeNewStateRoot() if we need to produce a block at slot 0 of epoch
// see https://github.com/ChainSafe/lodestar/issues/6194
const hashTreeRootTimer = this.metrics?.stateHashTreeRootTime.startTimer({source: "prepare_next_slot"});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be the naming convention we use for source?

e.g. here we use camelCase (function names)

onStateCloneMetrics(postState, metrics, "stateTransition");

and recently added (#6143) step metrics also use camelCase

const timer = metrics?.epochTransitionStepTime.startTimer({step: "afterProcessEpoch"});

I kinda like the function names and might be better for consistency, but maybe not always applicable (i.e. if there is no function call specific to the metric)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be the naming convention we use for source?

agree to have some naming convention, we used snake case as suggested by @dapplion and that's my preference. Agree there are consistencies so need some agreement to work on

I don't want to use function names, just which whatever name that makes sense in the context because:

  • some function names are so long so it's not nice to render in grafana, for example verifyBlocksStateTransitionOnly
  • one function could call a metric multiple times

so this one should be more dynamic to me, as long as a PR gets through review process we're fine I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to use function names, just which whatever name that makes sense in the context because:

Makes sense to not be to strict here then, also I guess those labels are mostly used to visualize and separate metric values in a panel. As long as it is visually readable and makes sense in the context of the metrics the naming convention (camelCase vs snake_case) should not matter that much.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake case just reads better in grafana :)

prepareState.hashTreeRoot();
hashTreeRootTimer?.();

// assuming there is no reorg, it caches the checkpoint state & helps avoid doing a full state transition in the next slot
// + when gossip block comes, we need to validate and run state transition
// + if next slot is a skipped slot, it'd help getting target checkpoint state faster to validate attestations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ export function computeNewStateRoot(
const {attestations, syncAggregate, slashing} = postState.proposerRewards;
const proposerReward = BigInt(attestations + syncAggregate + slashing);

return {newStateRoot: postState.hashTreeRoot(), proposerReward};
const hashTreeRootTimer = metrics?.stateHashTreeRootTime.startTimer({source: "compute_new_state_root"});
const newStateRoot = postState.hashTreeRoot();
hashTreeRootTimer?.();

return {newStateRoot, proposerReward};
}
5 changes: 3 additions & 2 deletions packages/beacon-node/src/metrics/metrics/lodestar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ export function createLodestarMetrics(
help: "Time to call commit after process a single block in seconds",
buckets: [0.005, 0.01, 0.02, 0.05, 0.1, 1],
}),
stateHashTreeRootTime: register.histogram({
stateHashTreeRootTime: register.histogram<"source">({
name: "lodestar_stfn_hash_tree_root_seconds",
help: "Time to compute the hash tree root of a post state in seconds",
buckets: [0.005, 0.01, 0.02, 0.05, 0.1, 1],
buckets: [0.05, 0.1, 0.2, 0.5, 1, 1.5],
labelNames: ["source"],
}),
preStateBalancesNodesPopulatedMiss: register.gauge<"source">({
name: "lodestar_stfn_balances_nodes_populated_miss_total",
Expand Down