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

Add Labels to ChainParameters to Enable Hardforks via Single Timestamp #7764

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
124 changes: 124 additions & 0 deletions src/Nethermind/Nethermind.Specs.Test/ChainParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Nethermind.Specs.Test;

public class ChainParametersTests
{

[Test]
public void ChainParameters_should_have_same_properties_as_chainSpecParamsJson()
{
Expand All @@ -29,4 +30,127 @@ public void ChainParameters_should_have_same_properties_as_chainSpecParamsJson()

Assert.That(chainParametersProperties, Is.EquivalentTo(chainSpecParamsJsonProperties));
}

[Test]
public void SettingDencunTransitionTimestamp_SetsAllEipTimestamps()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0x65687fd0;

chainParameters.DencunTransitionTimestamp = timestamp;

Assert.That(chainParameters.Eip4844TransitionTimestamp, Is.EqualTo(timestamp));
Assert.That(chainParameters.Eip4788TransitionTimestamp, Is.EqualTo(timestamp));
Assert.That(chainParameters.Eip1153TransitionTimestamp, Is.EqualTo(timestamp));
Assert.That(chainParameters.Eip5656TransitionTimestamp, Is.EqualTo(timestamp));
Assert.That(chainParameters.Eip6780TransitionTimestamp, Is.EqualTo(timestamp));
}

[Test]
public void GettingDencunTransitionTimestamp_ReturnsTimestampWhenAllMatch()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0x65687fd0;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp;
chainParameters.Eip6780TransitionTimestamp = timestamp;

Assert.That(chainParameters.DencunTransitionTimestamp, Is.EqualTo(timestamp));
}

[Test]
public void GettingDencunTransitionTimestamp_ReturnsNullWhenTimestampsDiffer()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0x65687fd0;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp;
chainParameters.Eip6780TransitionTimestamp = timestamp + 1; // Conflict

Assert.That(chainParameters.DencunTransitionTimestamp, Is.Null);
}

[Test]
public void GettingCancunTransitionTimestamp_ReturnsTimestampWhenAllMatch()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0x12345abc;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp;
chainParameters.Eip6780TransitionTimestamp = timestamp;

Assert.That(chainParameters.CancunTransitionTimestamp, Is.EqualTo(timestamp));
}

[Test]
public void GettingCancunTransitionTimestamp_ReturnsNullWhenTimestampsDiffer()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0x12345abc;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp;
chainParameters.Eip6780TransitionTimestamp = timestamp + 1; // Conflict

Assert.That(chainParameters.CancunTransitionTimestamp, Is.Null);
}

[Test]
public void AreHardforkTimestampsEqual_ReturnsTrueWhenAllMatch()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0xabcdef01;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp;
chainParameters.Eip6780TransitionTimestamp = timestamp;

Assert.That(chainParameters.AreHardforkTimestampsEqual("Dencun", "Cancun", "EIP-1153", "EIP-5656", "EIP-6780"), Is.True);
}

[Test]
public void AreHardforkTimestampsEqual_ReturnsFalseWhenTimestampsDiffer()
{
var chainParameters = new ChainParameters();
ulong timestamp = 0xabcdef01;

chainParameters.Eip4844TransitionTimestamp = timestamp;
chainParameters.Eip4788TransitionTimestamp = timestamp;
chainParameters.Eip1153TransitionTimestamp = timestamp;
chainParameters.Eip5656TransitionTimestamp = timestamp + 1; // Conflict
chainParameters.Eip6780TransitionTimestamp = timestamp;

Assert.That(chainParameters.AreHardforkTimestampsEqual("Dencun", "Cancun", "EIP-1153", "EIP-5656", "EIP-6780"), Is.False);
}

[Test]
public void GetHardforkMapping_ReturnsCorrectMappingForValidHardfork()
{
var mapping = ChainParameters.GetHardforkMapping("Dencun");
Assert.That(mapping, Is.Not.Null);

var chainParameters = new ChainParameters { Eip4844TransitionTimestamp = 0x12345abc };
Assert.That(mapping!(chainParameters), Is.EqualTo(0x12345abc));
}

[Test]
public void GetHardforkMapping_ReturnsNullForInvalidHardfork()
{
var mapping = ChainParameters.GetHardforkMapping("InvalidHardfork");
Assert.That(mapping, Is.Null);
}

}
80 changes: 80 additions & 0 deletions src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
Expand All @@ -9,6 +11,84 @@ namespace Nethermind.Specs.ChainSpecStyle;

public class ChainParameters
{

private static readonly Dictionary<string, Func<ChainParameters, ulong?>> HardforkToEips = new()
{
{ "Dencun", cp => cp.Eip4844TransitionTimestamp },
{ "Cancun", cp => cp.Eip4788TransitionTimestamp },
{ "EIP-1153", cp => cp.Eip1153TransitionTimestamp },
{ "EIP-5656", cp => cp.Eip5656TransitionTimestamp },
{ "EIP-6780", cp => cp.Eip6780TransitionTimestamp }
};
Comment on lines +15 to +22
Copy link
Member

Choose a reason for hiding this comment

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

I was thinking of something more like:

    public static readonly IReadOnlyList<Expression<Func<ChainParameters, ulong?>>> DencunEIPs =
    [
        cp => cp.Eip4844TransitionTimestamp,
        cp => cp.Eip4788TransitionTimestamp,
        cp => cp.Eip1153TransitionTimestamp,
        cp => cp.Eip5656TransitionTimestamp,
        cp => cp.Eip6780TransitionTimestamp
    ];

If we had same interface for ChainParameters and ChainSpecParamsJson that could be potentially reusable.

But that maybe an overkill.



public static Func<ChainParameters, ulong?>? GetHardforkMapping(string hardfork)
{
return HardforkToEips.TryGetValue(hardfork, out var mapping) ? mapping : null;
}

public ulong? DencunTransitionTimestamp
{
get
{
if (AreHardforkTimestampsEqual(
"Dencun", "Cancun", "EIP-1153",
"EIP-5656", "EIP-6780"))
{
return Eip4844TransitionTimestamp;
}

return null;
}
set
{
Eip4844TransitionTimestamp = value;
Eip4788TransitionTimestamp = value;
Eip1153TransitionTimestamp = value;
Eip5656TransitionTimestamp = value;
Eip6780TransitionTimestamp = value;
}
}


public ulong? CancunTransitionTimestamp
{
get
{
if (AreHardforkTimestampsEqual(
"Dencun", "Cancun", "EIP-1153",
"EIP-5656", "EIP-6780"))
{
return Eip4844TransitionTimestamp;
}
return null;
}
set
{
Eip4844TransitionTimestamp = value;
Eip4788TransitionTimestamp = value;
Eip1153TransitionTimestamp = value;
Eip5656TransitionTimestamp = value;
Eip6780TransitionTimestamp = value;
}
}
Comment on lines +30 to +74
Copy link
Member

Choose a reason for hiding this comment

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

Dencun and Cancun are same thing, confusing.


public bool AreHardforkTimestampsEqual(params string[] hardforks)
{
if (hardforks.Length == 0) return false;
ulong? firstTimestamp = HardforkToEips[hardforks[0]](this);
foreach (var hardfork in hardforks)
{
if (HardforkToEips[hardfork](this) != firstTimestamp)
{
return false;
}
}
return true;
}



public long? MaxCodeSize { get; set; }
public long? MaxCodeSizeTransition { get; set; }
public ulong? MaxCodeSizeTransitionTimestamp { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,56 @@ namespace Nethermind.Specs.ChainSpecStyle.Json;

internal class ChainSpecParamsJson
{

[JsonPropertyName("dencun")]
public ulong? DencunTransitionTimestamp
{
get => _dencunTransitionTimestamp;
set
{
_dencunTransitionTimestamp = value;
Eip4844TransitionTimestamp = value;
Eip4788TransitionTimestamp = value;
Eip1153TransitionTimestamp = value;
Eip5656TransitionTimestamp = value;
Eip6780TransitionTimestamp = value;
}
}

[JsonPropertyName("cancun")]
public ulong? CancunTransitionTimestamp
{
get => _cancunTransitionTimestamp;
set
{
_cancunTransitionTimestamp = value;
Eip4844TransitionTimestamp = value;
Eip4788TransitionTimestamp = value;
Eip1153TransitionTimestamp = value;
Eip5656TransitionTimestamp = value;
Eip6780TransitionTimestamp = value;
}
}

private ulong? _dencunTransitionTimestamp;
private ulong? _cancunTransitionTimestamp;

public bool ValidateNoTimestampConflicts()
{
return (DencunTransitionTimestamp == null || (
Eip4844TransitionTimestamp == DencunTransitionTimestamp &&
Eip4788TransitionTimestamp == DencunTransitionTimestamp &&
Eip1153TransitionTimestamp == DencunTransitionTimestamp &&
Eip5656TransitionTimestamp == DencunTransitionTimestamp &&
Eip6780TransitionTimestamp == DencunTransitionTimestamp))
&&
(CancunTransitionTimestamp == null || (
Eip4844TransitionTimestamp == CancunTransitionTimestamp &&
Eip4788TransitionTimestamp == CancunTransitionTimestamp &&
Eip1153TransitionTimestamp == CancunTransitionTimestamp &&
Eip5656TransitionTimestamp == CancunTransitionTimestamp &&
Eip6780TransitionTimestamp == CancunTransitionTimestamp));
}
public ulong? ChainId { get; set; }
public ulong? NetworkId { get; set; }

Expand Down