Add ValidateMetadata hook to CommonOpts for metadata validation #235
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add ValidateMetadata hook to CommonOpts
Summary
This PR introduces a new, optional
ValidateMetadatahook to theCommonOptsstruct in go-eth2-client. This function receives amap[string]anycontaining the metadata returned by a beacon node (for example, thedependent_rootin validator duties responses) and returns an error if the metadata should be considered invalid. If provided, the client library will invoke this hook on all API calls that return metadata before accepting the result. This allows applications to reject stale or inconsistent data from individual nodes before it is used.Motivation
In multi-node beacon chain setups (e.g. using a pool of beacon clients for high availability), a synchronization issue can occur: one node may respond faster but with outdated state, leading to incorrect metadata. For example, after subscribing to head events, a call to
GET /eth/v1/validator/duties/proposer/{epoch}could return results from a client that isn’t yet fully synced. Itsdependent_root(the block root that the response depends on) might be behind the latest head, resulting in inconsistent or stale duties being returned.The
ValidateMetadatahook addresses this by giving users a way to programmatically inspect and validate the returned metadata before it is accepted. If the hook returns an error, the library treats that response as invalid, allowing the caller to retry the request against another client. This mechanism is particularly useful for ensuring that all clients in a multi-instance configuration return coherent and up-to-date metadata.Implementation
A new field
ValidateMetadata func(map[string]any) erroris added to theCommonOptsstruct. It is optional; if it isnil, no validation is performed (preserving the existing behavior).All relevant API handlers that process metadata now invoke this hook. For example, in the proposer duties provider (and similarly in attester duties, sync committee duties, etc.), after unmarshalling the response, the code does:
This ensures that we only return a result if the metadata passes validation.
No existing endpoints or return types are changed. The hook is simply an additional layer on top of the response processing.
Benefits
dependent_root,execution_optimistic, etc.) meet their expectations. This avoids using stale or invalid metadata for validator duties or other operations.ValidateMetadatafunction can contain any custom logic. For instance, it could verify chain IDs, check block roots, validate timestamps, or enforce other application-specific rules on the metadata.Backward Compatibility
ValidateMetadatafield is optional and defaults tonil. Existing code that does not use this field will operate exactly as before.