-
Notifications
You must be signed in to change notification settings - Fork 461
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
Adjust Optimism (Mainnet) TotalDifficulty
calculation
#7647
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b73cd38
Extract `TotalDifficulty` calculation
emlautarom1 c391c2e
Add `FixedTotalDifficultyStrategy`
emlautarom1 cb4c791
Inject Optimism-Mainnet specific strategy
emlautarom1 280bf68
Small log improvement
emlautarom1 2f1e822
Merge branch 'master' into feat/total-difficulty-strategy
kamilchodola a30e516
Cleanup DI
emlautarom1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Nethermind/Nethermind.Optimism/OptimismSynchronizerModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using Autofac; | ||
using Nethermind.Blockchain.Synchronization; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Specs; | ||
using Nethermind.Specs.ChainSpecStyle; | ||
using Nethermind.Synchronization; | ||
|
||
namespace Nethermind.Optimism; | ||
|
||
/// <remarks> | ||
/// In Optimism Mainnet, the <see cref="BlockHeader.TotalDifficulty"/> gets resetted to <c>0</c> in the Bedrock block unlike other chains that went through The Merge fork. | ||
/// Calculation is still the same: the current block's <see cref="BlockHeader.TotalDifficulty"/> is the parent's <see cref="BlockHeader.TotalDifficulty"/> plus the current block's <see cref="BlockHeader.Difficulty"/>. | ||
/// <seealso href="https://github.com/NethermindEth/nethermind/issues/7626"/> | ||
/// </remarks> | ||
public sealed class OptimismSynchronizerModule(OptimismParameters parameters, ISpecProvider provider) : Module | ||
{ | ||
private const ulong OptimismMainnetChainId = 0xA; | ||
|
||
protected override void Load(ContainerBuilder builder) | ||
{ | ||
if (provider.ChainId == OptimismMainnetChainId) | ||
{ | ||
builder.AddSingleton<ITotalDifficultyStrategy>( | ||
new FixedTotalDifficultyStrategy( | ||
new CumulativeTotalDifficultyStrategy(), | ||
fixesBlockNumber: parameters.BedrockBlockNumber - 1, | ||
toTotalDifficulty: provider.TerminalTotalDifficulty ?? throw new ArgumentNullException(nameof(provider.TerminalTotalDifficulty)) | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Nethermind/Nethermind.Synchronization.Test/TotalDifficultyStrategyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Test.Builders; | ||
using Nethermind.Int256; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Synchronization.Test; | ||
|
||
public class TotalDifficultyStrategyTests | ||
{ | ||
[Test] | ||
public void CumulativeTotalDifficultyStrategy_own_TotalDifficulty_is_Parent_TotalDifficulty_plus_own_difficulty() | ||
{ | ||
ITotalDifficultyStrategy strategy = new CumulativeTotalDifficultyStrategy(); | ||
|
||
List<(BlockHeader Header, UInt256 ExpectedTotalDifficulty)> headers = | ||
[ | ||
(Build.A.BlockHeader.WithDifficulty(10).WithTotalDifficulty(39).TestObject, 39), | ||
(Build.A.BlockHeader.WithDifficulty(8).TestObject, 29), | ||
(Build.A.BlockHeader.WithDifficulty(5).TestObject, 21), | ||
(Build.A.BlockHeader.WithDifficulty(12).TestObject, 16), | ||
(Build.A.BlockHeader.WithDifficulty(3).TestObject, 4), | ||
(Build.A.BlockHeader.WithDifficulty(1).TestObject, 1), | ||
]; | ||
|
||
for (int i = 0; i < headers.Count - 1; i++) | ||
{ | ||
var header = headers[i].Header; | ||
var parent = headers[i + 1].Header; | ||
|
||
parent.TotalDifficulty = strategy.ParentTotalDifficulty(header); | ||
} | ||
|
||
foreach (var (header, expectedTotalDifficulty) in headers) | ||
{ | ||
header.TotalDifficulty.Should().Be(expectedTotalDifficulty); | ||
} | ||
} | ||
|
||
[Test] | ||
public void FixedTotalDifficultyStrategy_fixes_a_block_header_to_specific_total_difficulty() | ||
{ | ||
ITotalDifficultyStrategy strategy = new FixedTotalDifficultyStrategy( | ||
new CumulativeTotalDifficultyStrategy(), | ||
fixesBlockNumber: 3, | ||
toTotalDifficulty: 21 | ||
); | ||
|
||
List<(BlockHeader Header, UInt256 ExpectedTotalDifficulty)> headers = | ||
[ | ||
(Build.A.BlockHeader.WithNumber(5).WithDifficulty(0).WithTotalDifficulty(0).TestObject, 0), | ||
(Build.A.BlockHeader.WithNumber(4).WithDifficulty(0).TestObject, 0), | ||
(Build.A.BlockHeader.WithNumber(3).WithDifficulty(5).TestObject, 21), | ||
(Build.A.BlockHeader.WithNumber(2).WithDifficulty(12).TestObject, 16), | ||
(Build.A.BlockHeader.WithNumber(1).WithDifficulty(3).TestObject, 4), | ||
(Build.A.BlockHeader.WithNumber(0).WithDifficulty(1).TestObject, 1), | ||
]; | ||
|
||
for (int i = 0; i < headers.Count - 1; i++) | ||
{ | ||
var header = headers[i].Header; | ||
var parent = headers[i + 1].Header; | ||
|
||
parent.TotalDifficulty = strategy.ParentTotalDifficulty(header); | ||
} | ||
|
||
foreach (var (header, expectedTotalDifficulty) in headers) | ||
{ | ||
header.TotalDifficulty.Should().Be(expectedTotalDifficulty); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Nethermind/Nethermind.Synchronization/ITotalDifficultyStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using Nethermind.Core; | ||
using Nethermind.Int256; | ||
|
||
namespace Nethermind.Synchronization; | ||
|
||
public interface ITotalDifficultyStrategy | ||
{ | ||
UInt256 ParentTotalDifficulty(BlockHeader header); | ||
} | ||
|
||
public sealed class CumulativeTotalDifficultyStrategy : ITotalDifficultyStrategy | ||
{ | ||
public UInt256 ParentTotalDifficulty(BlockHeader header) | ||
{ | ||
return (header.TotalDifficulty ?? 0) - header.Difficulty; | ||
} | ||
} | ||
|
||
public sealed class FixedTotalDifficultyStrategy( | ||
ITotalDifficultyStrategy strategy, | ||
long fixesBlockNumber, | ||
UInt256 toTotalDifficulty | ||
) : ITotalDifficultyStrategy | ||
{ | ||
public UInt256 ParentTotalDifficulty(BlockHeader header) | ||
{ | ||
return header.Number > 0 && header.Number - 1 == fixesBlockNumber | ||
? toTotalDifficulty | ||
: strategy.ParentTotalDifficulty(header); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is not needed. Just have one
OptimismSynchronizerModule
for just optimism configuration, the register it later afterMergeSynchronizerModule
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check is actually needed since we want to use a particular difficulty calculation strategy in Optimism Mainnet and not in, for example, Optimism Sepolia.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is unfortunate.