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

core, rpcdaemon: add check on NoPreMergeConfig and terminal total difficulty #1949

Merged
merged 23 commits into from
Apr 12, 2024

Conversation

lupin012
Copy link
Contributor

No description provided.

@lupin012 lupin012 requested a review from canepat March 29, 2024 17:01
@canepat canepat marked this pull request as draft April 2, 2024 10:07
@canepat
Copy link
Member

canepat commented Apr 2, 2024

Let's try to avoid this kind of defensive programming and see if we can instead identify where and why pre-merge rule set can be null in the first place

@lupin012 lupin012 marked this pull request as ready for review April 8, 2024 17:46
@canepat
Copy link
Member

canepat commented Apr 9, 2024

Let's try to avoid this kind of defensive programming and see if we can instead identify where and why pre-merge rule set can be null in the first place

In order to trigger a nullptr dereference in MergeRuleSet the following conditions must be true:

  1. ChainConfig instance has NoPreMergeConfig rule set config as a result of parsing any JSON chain config containing an unknown rule set
  2. the same ChainConfig instance has non-null terminal_total_difficulty as a result of parsing the same JSON

As a consequence of both 1. and 2. being met, pre_merge_rule_set function returns a nullptr, which is then received at construction time by the MergeRuleSet instance created in rule_set_factory.

However, here the point is that an implicit invariant of ChainConfig is violated. Theoretically, the conditions 1. and 2. cannot be true at the same time because the first one means the chain has no pre-merge rule set because the merge happens at genesis and the second one means the chain has non-zero terminal total difficulty identifying where the merge point is (clearly not at genesis).

Hence, a proper solution here implies making the ChainConfig invariant explicit by:

  • enforcing the invariant when building a new ChainConfig instance in ChainConfig::from_json by returning std::nullopt (i.e. invalid configuration) if this is not the case
  • put an assertion in rule_set_factory to check the invariant immediately after the pre-merge rule set creation

@lupin012 lupin012 changed the title rpcdaemon: check pre_merge_rule_set_ is not null before invoke methods on it rpcdaemon: add check on PreMergeRuleSet and total difficulty Apr 10, 2024
@canepat canepat changed the title rpcdaemon: add check on PreMergeRuleSet and total difficulty core, rpcdaemon: add check on PreMergeRuleSet and total difficulty Apr 12, 2024
@canepat canepat requested a review from yperbasis April 12, 2024 15:38
@canepat
Copy link
Member

canepat commented Apr 12, 2024

@yperbasis this PR looks good to me at least for preventing null pointer dereference when pre_merge_rule_set_ is nullptr in MergeRuleSet (e.g. for Holesky config when checked against empty block), so I'm going to merge it right now. Please review it sooner or later and suggest any improvement.

@canepat canepat added the maintenance Some maintenance work (fix, refactor, rename, test...) label Apr 12, 2024
@canepat canepat changed the title core, rpcdaemon: add check on PreMergeRuleSet and total difficulty core, rpcdaemon: add check on NoPreMergeConfig and terminal total difficulty Apr 12, 2024
@canepat canepat merged commit 73c32a5 into master Apr 12, 2024
4 checks passed
@canepat canepat deleted the fix_in_case_null_pre_merge_rule_set branch April 12, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Some maintenance work (fix, refactor, rename, test...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants