Skip to content

Commit

Permalink
Add Cancun hard-fork settings for Chiado testnet (#6457)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo authored and kamilchodola committed Jan 15, 2024
1 parent a977374 commit 09b5526
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 47 deletions.
6 changes: 5 additions & 1 deletion src/Nethermind/Chains/chiado.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
}
},
"params": {

"networkID": 10200,
"gasLimitBoundDivisor": "0x400",
"maximumExtraDataSize": "0x20",
Expand Down Expand Up @@ -69,6 +68,11 @@
"eip3855TransitionTimestamp": "0x646e0e4c",
"eip3860TransitionTimestamp": "0x646e0e4c",
"eip4895TransitionTimestamp": "0x646e0e4c",
"eip1153TransitionTimestamp": "0x65ba8e4c",
"eip4788TransitionTimestamp": "0x65ba8e4c",
"eip4844TransitionTimestamp": "0x65ba8e4c",
"eip5656TransitionTimestamp": "0x65ba8e4c",
"eip6780TransitionTimestamp": "0x65ba8e4c",
"eip1559BaseFeeMaxChangeDenominator": "0x8",
"eip1559ElasticityMultiplier": "0x2",
"eip1559FeeCollector": "0x1559000000000000000000000000000000000000",
Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Chains/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"eip4844TransitionTimestamp": "0x65A77460",
"eip5656TransitionTimestamp": "0x65A77460",
"eip6780TransitionTimestamp": "0x65A77460",
"eip4788ContractAddress": "0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02",
"terminalTotalDifficulty": "A4A470",
"gasLimitBoundDivisor": "0x400",
"maxCodeSize": "0x6000",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@
using Nethermind.Core.Extensions;

namespace Nethermind.Consensus.BeaconBlockRoot;

public class BeaconBlockRootHandler : IBeaconBlockRootHandler
{
public static UInt256 HISTORICAL_ROOTS_LENGTH = 8191;
private static readonly Address DefaultPbbrContractAddress = new Address("0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02");

public void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState stateProvider)
{
if (!spec.IsBeaconBlockRootAvailable ||
block.IsGenesis ||
block.Header.ParentBeaconBlockRoot is null) return;
Address? eip4788Account = spec.Eip4788ContractAddress ?? DefaultPbbrContractAddress;
block.Header.ParentBeaconBlockRoot is null)
return;

Address eip4788Account = spec.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress;

if (!stateProvider.AccountExists(eip4788Account))
return;

UInt256 timestamp = (UInt256)block.Timestamp;
Hash256 parentBeaconBlockRoot = block.ParentBeaconBlockRoot;

UInt256.Mod(timestamp, HISTORICAL_ROOTS_LENGTH, out UInt256 timestampReduced);
UInt256 rootIndex = timestampReduced + HISTORICAL_ROOTS_LENGTH;
UInt256.Mod(timestamp, Eip4788Constants.HistoryBufferLength, out UInt256 timestampReduced);
UInt256 rootIndex = timestampReduced + Eip4788Constants.HistoryBufferLength;

StorageCell tsStorageCell = new(eip4788Account, timestampReduced);
StorageCell brStorageCell = new(eip4788Account, rootIndex);
Expand Down
22 changes: 22 additions & 0 deletions src/Nethermind/Nethermind.Core/Eip4788Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Int256;

namespace Nethermind.Core;

/// <summary>
/// Represents the <see href="https://eips.ethereum.org/EIPS/eip-4788#specification">EIP-4788</see> parameters.
/// </summary>
public static class Eip4788Constants
{
/// <summary>
/// Gets the <c>BEACON_ROOTS_ADDRESS</c> parameter.
/// </summary>
public static readonly Address BeaconRootsAddress = new("0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02");

/// <summary>
/// Gets the <c>HISTORY_BUFFER_LENGTH</c> parameter.
/// </summary>
public static readonly UInt256 HistoryBufferLength = 8191;
}
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.Network.Test/ForkInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public void Fork_id_and_hash_as_expected_on_gnosis(long head, ulong headTimestam
}

[TestCase(0L, 0UL, "0x50d39d7b", ChiadoSpecProvider.ShanghaiTimestamp, "Chiado genesis")]
[TestCase(3945317, ChiadoSpecProvider.ShanghaiTimestamp, "0xa15a4252", 0ul, "First Shanghai timestamp")]
[TestCase(9945337, 9984930320ul, "0xa15a4252", 0ul, "Future Shanghai timestamp")]
[TestCase(3945317, ChiadoSpecProvider.ShanghaiTimestamp, "0xa15a4252", ChiadoSpecProvider.CancunTimestamp, "First Shanghai timestamp")]
[TestCase(4_000_000, ChiadoSpecProvider.CancunTimestamp, "0x5fbc16bc", 0ul, "First Cancun timestamp")]
[TestCase(5_000_000, 9984930320ul, "0x5fbc16bc", 0ul, "Future Cancun timestamp")]
public void Fork_id_and_hash_as_expected_on_chiado(long head, ulong headTimestamp, string forkHashHex, ulong next, string description)
{
ChainSpecLoader loader = new ChainSpecLoader(new EthereumJsonSerializer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ public static IEnumerable<TestCaseData> ChiadoActivations
yield return new TestCaseData((ForkActivation)(1, 20)) { TestName = "(1, 20)" };
yield return new TestCaseData((ForkActivation)(1, ChiadoSpecProvider.ShanghaiTimestamp - 1)) { TestName = "Before Shanghai" };
yield return new TestCaseData((ForkActivation)(1, ChiadoSpecProvider.ShanghaiTimestamp)) { TestName = "Shanghai" };
yield return new TestCaseData(new ForkActivation(1, GoerliSpecProvider.ShanghaiTimestamp + 100000000)) { TestName = "Future" };
yield return new TestCaseData((ForkActivation)(1, ChiadoSpecProvider.CancunTimestamp - 1)) { TestName = "Before Cancun" };
yield return new TestCaseData((ForkActivation)(1, ChiadoSpecProvider.CancunTimestamp)) { TestName = "Cancun" };
yield return new TestCaseData((ForkActivation)(1, ChiadoSpecProvider.CancunTimestamp + 100000000)) { TestName = "Future" };
}
}

Expand All @@ -243,17 +245,10 @@ public void Chiado_loads_properly(ForkActivation forkActivation)
IReleaseSpec? preShanghaiSpec = provider.GetSpec((1, ChiadoSpecProvider.ShanghaiTimestamp - 1));
IReleaseSpec? postShanghaiSpec = provider.GetSpec((1, ChiadoSpecProvider.ShanghaiTimestamp));

VerifyGnosisShanghaiExceptions(preShanghaiSpec, postShanghaiSpec);
VerifyGnosisShanghaiSpecifics(preShanghaiSpec, postShanghaiSpec);
VerifyGnosisCancunSpecifics();
GetTransitionTimestamps(chainSpec.Parameters).Should().AllSatisfy(
t => ValidateSlotByTimestamp(t, ChiadoSpecProvider.BeaconChainGenesisTimestamp, GnosisBlockTime).Should().BeTrue());

Assert.Multiple(() =>
{
Assert.That(Eip4844Constants.BlobGasPriceUpdateFraction, Is.EqualTo((UInt256)1112826));
Assert.That(Eip4844Constants.MaxBlobGasPerBlock, Is.EqualTo(262144));
Assert.That(Eip4844Constants.MinBlobGasPrice, Is.EqualTo(1.GWei()));
Assert.That(Eip4844Constants.TargetBlobGasPerBlock, Is.EqualTo(131072));
});
}

public static IEnumerable<TestCaseData> GnosisActivations
Expand Down Expand Up @@ -290,17 +285,21 @@ public void Gnosis_loads_properly(ForkActivation forkActivation)
Assert.That(provider.ChainId, Is.EqualTo(BlockchainIds.Gnosis));
Assert.That(provider.NetworkId, Is.EqualTo(BlockchainIds.Gnosis));

VerifyGnosisPreShanghaiExceptions(provider);
VerifyGnosisPreShanghaiSpecifics(provider);

IReleaseSpec? preShanghaiSpec = provider.GetSpec((GnosisSpecProvider.LondonBlockNumber + 1,
GnosisSpecProvider.ShanghaiTimestamp - 1));
IReleaseSpec? postShanghaiSpec = provider.GetSpec((GnosisSpecProvider.LondonBlockNumber + 1,
GnosisSpecProvider.ShanghaiTimestamp));

VerifyGnosisShanghaiExceptions(preShanghaiSpec, postShanghaiSpec);
VerifyGnosisShanghaiSpecifics(preShanghaiSpec, postShanghaiSpec);
VerifyGnosisCancunSpecifics();
GetTransitionTimestamps(chainSpec.Parameters).Should().AllSatisfy(
t => ValidateSlotByTimestamp(t, GnosisSpecProvider.BeaconChainGenesisTimestamp, GnosisBlockTime).Should().BeTrue());
}

private static void VerifyGnosisCancunSpecifics()
{
Assert.Multiple(() =>
{
Assert.That(Eip4844Constants.BlobGasPriceUpdateFraction, Is.EqualTo((UInt256)1112826));
Expand All @@ -310,7 +309,7 @@ public void Gnosis_loads_properly(ForkActivation forkActivation)
});
}

private void VerifyGnosisShanghaiExceptions(IReleaseSpec preShanghaiSpec, IReleaseSpec postShanghaiSpec)
private static void VerifyGnosisShanghaiSpecifics(IReleaseSpec preShanghaiSpec, IReleaseSpec postShanghaiSpec)
{
preShanghaiSpec.MaxCodeSize.Should().Be(long.MaxValue);
postShanghaiSpec.MaxCodeSize.Should().Be(24576L);
Expand All @@ -325,10 +324,10 @@ private void VerifyGnosisShanghaiExceptions(IReleaseSpec preShanghaiSpec, IRelea
postShanghaiSpec.IsEip170Enabled.Should().Be(true);
}

private void VerifyGnosisPreShanghaiExceptions(ISpecProvider specProvider)
private static void VerifyGnosisPreShanghaiSpecifics(ISpecProvider specProvider)
{
specProvider.GenesisSpec.MaximumUncleCount.Should().Be(0);
specProvider.GetSpec((ForkActivation)(GnosisSpecProvider.ConstantinopoleBlockNumber - (1))).IsEip1283Enabled.Should()
specProvider.GetSpec((ForkActivation)(GnosisSpecProvider.ConstantinopoleBlockNumber - 1)).IsEip1283Enabled.Should()
.BeFalse();
specProvider.GetSpec((ForkActivation)GnosisSpecProvider.ConstantinopoleBlockNumber).IsEip1283Enabled.Should()
.BeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ bool GetForInnerPathExistence(KeyValuePair<string, JsonElement> o)
Eip5656TransitionTimestamp = chainSpecJson.Params.Eip5656TransitionTimestamp,
Eip6780TransitionTimestamp = chainSpecJson.Params.Eip6780TransitionTimestamp,
Eip4788TransitionTimestamp = chainSpecJson.Params.Eip4788TransitionTimestamp,
Eip4788ContractAddress = chainSpecJson.Params.Eip4788ContractAddress,
Eip4788ContractAddress = chainSpecJson.Params.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress,
TransactionPermissionContract = chainSpecJson.Params.TransactionPermissionContract,
TransactionPermissionContractTransition = chainSpecJson.Params.TransactionPermissionContractTransition,
ValidateChainIdTransition = chainSpecJson.Params.ValidateChainIdTransition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Int256;
Expand Down
4 changes: 3 additions & 1 deletion src/Nethermind/Nethermind.Specs/ChiadoSpecProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ChiadoSpecProvider : ISpecProvider
{
public const ulong BeaconChainGenesisTimestamp = 0x6343ee4c;
public const ulong ShanghaiTimestamp = 0x646e0e4c;
public const ulong CancunTimestamp = 0x65ba8e4c;

private ChiadoSpecProvider() { }

Expand All @@ -20,7 +21,8 @@ private ChiadoSpecProvider() { }
_ => forkActivation.Timestamp switch
{
null or < ShanghaiTimestamp => GenesisSpec,
_ => Shanghai.Instance
< CancunTimestamp => Shanghai.Instance,
_ => Cancun.Instance
}
};

Expand Down
31 changes: 15 additions & 16 deletions src/Nethermind/Nethermind.Specs/Forks/16_Cancun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
using Nethermind.Core;
using Nethermind.Core.Specs;

namespace Nethermind.Specs.Forks
{
public class Cancun : Shanghai
{
private static IReleaseSpec _instance;
namespace Nethermind.Specs.Forks;

protected Cancun()
{
Name = "Cancun";
IsEip1153Enabled = true;
IsEip4788Enabled = true;
IsEip4844Enabled = true;
IsEip5656Enabled = true;
IsEip6780Enabled = true;
Eip4788ContractAddress = new Address("0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02");
}
public class Cancun : Shanghai
{
private static IReleaseSpec _instance;

public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Cancun());
protected Cancun()
{
Name = "Cancun";
IsEip1153Enabled = true;
IsEip4788Enabled = true;
IsEip4844Enabled = true;
IsEip5656Enabled = true;
IsEip6780Enabled = true;
Eip4788ContractAddress = Eip4788Constants.BeaconRootsAddress;
}

public new static IReleaseSpec Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new Cancun());
}

0 comments on commit 09b5526

Please sign in to comment.