Skip to content

Commit

Permalink
Make chainspec extendable by plugins (#7540)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Rozmej <[email protected]>
Co-authored-by: Lautaro Emanuel <[email protected]>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent a00c8f5 commit c91e0b2
Show file tree
Hide file tree
Showing 79 changed files with 1,417 additions and 1,230 deletions.
6 changes: 5 additions & 1 deletion src/Nethermind/Nethermind.AuRa.Test/AuRaPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using FluentAssertions;
using Nethermind.Config;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.InitializationSteps;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Test.ChainSpecStyle;
using NUnit.Framework;

namespace Nethermind.AuRa.Test
Expand All @@ -19,7 +21,9 @@ public class AuRaPluginTests
public void Init_when_not_AuRa_doesnt_trow()
{
AuRaPlugin auRaPlugin = new();
Action init = () => auRaPlugin.Init(new AuRaNethermindApi(new ConfigProvider(), new EthereumJsonSerializer(), new TestLogManager(), new ChainSpec()));
ChainSpec chainSpec = new();
chainSpec.EngineChainSpecParametersProvider = new TestChainSpecParametersProvider(new AuRaChainSpecEngineParameters());
Action init = () => auRaPlugin.Init(new AuRaNethermindApi(new ConfigProvider(), new EthereumJsonSerializer(), new TestLogManager(), chainSpec));
init.Should().NotThrow();
}

Expand Down
9 changes: 5 additions & 4 deletions src/Nethermind/Nethermind.AuRa.Test/AuRaSealValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using Nethermind.Blockchain;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Core;
using Nethermind.Core.Crypto;
Expand All @@ -24,7 +25,7 @@ namespace Nethermind.AuRa.Test
public class AuRaSealValidatorTests
{
private AuRaSealValidator _sealValidator;
private AuRaParameters _auRaParameters;
private AuRaChainSpecEngineParameters _auRaParameters;
private IAuRaStepCalculator _auRaStepCalculator;
private ILogManager _logManager;
private IWallet _wallet;
Expand All @@ -38,7 +39,7 @@ public class AuRaSealValidatorTests
[SetUp]
public void SetUp()
{
_auRaParameters = new AuRaParameters();
_auRaParameters = new AuRaChainSpecEngineParameters();
_auRaStepCalculator = Substitute.For<IAuRaStepCalculator>();
_logManager = LimboLogs.Instance;
_wallet = new DevWallet(new WalletConfig(), _logManager);
Expand Down Expand Up @@ -89,7 +90,7 @@ BlockHeaderBuilder GetParentBlock() => Build.A.BlockHeader
TestCaseData GetTestCaseData(
BlockHeaderBuilder parent,
BlockHeaderBuilder block,
Action<AuRaParameters> paramAction = null,
Action<AuRaChainSpecEngineParameters> paramAction = null,
Repeat repeat = Repeat.No,
bool parentIsHead = true,
bool isValidSealer = true) =>
Expand Down Expand Up @@ -141,7 +142,7 @@ TestCaseData GetTestCaseData(
}

[TestCaseSource(nameof(ValidateParamsTests))]
public (bool, object) validate_params(BlockHeader parentBlock, BlockHeader block, Action<AuRaParameters> modifyParameters, Repeat repeat, bool parentIsHead, bool isValidSealer)
public (bool, object) validate_params(BlockHeader parentBlock, BlockHeader block, Action<AuRaChainSpecEngineParameters> modifyParameters, Repeat repeat, bool parentIsHead, bool isValidSealer)
{
_blockTree.Head.Returns(parentIsHead ? new Block(parentBlock) : new Block(Build.A.BlockHeader.WithNumber(parentBlock.Number - 1).TestObject));
_validSealerStrategy.IsValidSealer(Arg.Any<IList<Address>>(), block.Beneficiary, block.AuRaStep.Value, out _).Returns(isValidSealer);
Expand Down
95 changes: 95 additions & 0 deletions src/Nethermind/Nethermind.AuRa.Test/ChainSpecLoaderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Serialization.Json;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using NUnit.Framework;

namespace Nethermind.AuRa.Test;

public class ChainSpecLoaderTest
{
private static ChainSpec LoadChainSpec(string path)
{
ChainSpecLoader chainSpecLoader = new(new EthereumJsonSerializer());
ChainSpec chainSpec = chainSpecLoader.LoadFromFile(path);
return chainSpec;
}

[Test]
public void Can_load_gnosis()
{
string path = Path.Combine(TestContext.CurrentContext.WorkDirectory, "../../../../", "Chains/gnosis.json");
ChainSpec chainSpec = LoadChainSpec(path);

Assert.That(chainSpec.Parameters.Eip1559BaseFeeInitialValue, Is.EqualTo(1.GWei()), $"fork base fee");
Assert.That(chainSpec.NetworkId, Is.EqualTo(100), $"{nameof(chainSpec.NetworkId)}");
Assert.That(chainSpec.Name, Is.EqualTo("GnosisChain"), $"{nameof(chainSpec.Name)}");
Assert.That(chainSpec.SealEngineType, Is.EqualTo(SealEngineType.AuRa), "engine");

int berlinGnosisBlockNumber = 16101500;
chainSpec.Parameters.Eip2565Transition.Should().Be(berlinGnosisBlockNumber);
chainSpec.Parameters.Eip2929Transition.Should().Be(berlinGnosisBlockNumber);
chainSpec.Parameters.Eip2930Transition.Should().Be(berlinGnosisBlockNumber);

chainSpec.Parameters.TerminalTotalDifficulty.ToString()
.Should().Be("8626000000000000000000058750000000000000000000");

var auraParams = chainSpec.EngineChainSpecParametersProvider.GetChainSpecParameters<AuRaChainSpecEngineParameters>();

auraParams.WithdrawalContractAddress.ToString(true)
.Should().Be("0x0B98057eA310F4d31F2a452B414647007d1645d9");
}

[Test]
public void Can_load_chiado()
{
string path = Path.Combine(TestContext.CurrentContext.WorkDirectory, "../../../../", "Chains/chiado.json");
ChainSpec chainSpec = LoadChainSpec(path);

Assert.That(chainSpec.Parameters.Eip1559BaseFeeInitialValue, Is.EqualTo(1.GWei()), $"fork base fee");
Assert.That(chainSpec.NetworkId, Is.EqualTo(10200), $"{nameof(chainSpec.NetworkId)}");
Assert.That(chainSpec.Name, Is.EqualTo("chiado"), $"{nameof(chainSpec.Name)}");
Assert.That(chainSpec.SealEngineType, Is.EqualTo(SealEngineType.AuRa), "engine");

chainSpec.Parameters.TerminalTotalDifficulty.ToString()
.Should().Be("231707791542740786049188744689299064356246512");

var auraParams = chainSpec.EngineChainSpecParametersProvider.GetChainSpecParameters<AuRaChainSpecEngineParameters>();

auraParams.WithdrawalContractAddress.ToString(true)
.Should().Be("0xb97036A26259B7147018913bD58a774cf91acf25");

chainSpec.ShanghaiTimestamp.Should().Be(ChiadoSpecProvider.ShanghaiTimestamp);
chainSpec.ShanghaiTimestamp.Should().Be(ChiadoSpecProvider.Instance.TimestampFork);
}

[Test]
public void Can_load_posdao_with_rewriteBytecode()
{
// TODO: modexp 2565
string path = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Specs/posdao.json");
ChainSpec chainSpec = LoadChainSpec(path);
IDictionary<long, IDictionary<Address, byte[]>> expected = new Dictionary<long, IDictionary<Address, byte[]>>
{
{
21300000, new Dictionary<Address, byte[]>()
{
{new Address("0x1234000000000000000000000000000000000001"), Bytes.FromHexString("0x111")},
{new Address("0x1234000000000000000000000000000000000002"), Bytes.FromHexString("0x222")},
}
}
};

var auraParams = chainSpec.EngineChainSpecParametersProvider.GetChainSpecParameters<AuRaChainSpecEngineParameters>();

auraParams.RewriteBytecode.Should().BeEquivalentTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core;
using Nethermind.Logging;
using Nethermind.Specs.ChainSpecStyle;
using NUnit.Framework;

namespace Nethermind.AuRa.Test.Contract;
Expand Down Expand Up @@ -81,8 +83,11 @@ public class TestGasLimitContractBlockchain : TestContractBlockchain

protected override BlockProcessor CreateBlockProcessor()
{
KeyValuePair<long, Address> blockGasLimitContractTransition = ChainSpec.AuRa.BlockGasLimitContractTransitions.First();
BlockGasLimitContract gasLimitContract = new(AbiEncoder.Instance, blockGasLimitContractTransition.Value, blockGasLimitContractTransition.Key,
KeyValuePair<long, Address> blockGasLimitContractTransition = ChainSpec.EngineChainSpecParametersProvider
.GetChainSpecParameters<AuRaChainSpecEngineParameters>().BlockGasLimitContractTransitions
.First();
BlockGasLimitContract gasLimitContract = new(AbiEncoder.Instance, blockGasLimitContractTransition.Value,
blockGasLimitContractTransition.Key,
new ReadOnlyTxProcessingEnv(
WorldStateManager,
BlockTree.AsReadOnly(), SpecProvider, LimboLogs.Instance));
Expand Down Expand Up @@ -114,8 +119,10 @@ public class TestGasLimitContractBlockchainLateBlockGasLimit : TestGasLimitContr
{
protected override BlockProcessor CreateBlockProcessor()
{
KeyValuePair<long, Address> blockGasLimitContractTransition = ChainSpec.AuRa.BlockGasLimitContractTransitions.First();
ChainSpec.AuRa.BlockGasLimitContractTransitions = new Dictionary<long, Address>() { { 10, blockGasLimitContractTransition.Value } };
var parameters = ChainSpec.EngineChainSpecParametersProvider
.GetChainSpecParameters<AuRaChainSpecEngineParameters>();
KeyValuePair<long, Address> blockGasLimitContractTransition = parameters.BlockGasLimitContractTransitions.First();
parameters.BlockGasLimitContractTransitions = new Dictionary<long, Address>() { { 10, blockGasLimitContractTransition.Value } };
return base.CreateBlockProcessor();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using FluentAssertions;
using Nethermind.Abi;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Rewards;
using Nethermind.Consensus.Rewards;
using Nethermind.Core;
Expand All @@ -25,7 +26,7 @@ namespace Nethermind.AuRa.Test.Reward
{
public class AuRaRewardCalculatorTests
{
private AuRaParameters _auraParameters;
private AuRaChainSpecEngineParameters _auraParameters;
private IAbiEncoder _abiEncoder;
private ITransactionProcessor _transactionProcessor;
private Block _block;
Expand All @@ -40,11 +41,11 @@ public void SetUp()
_address10 = TestItem.AddressA;
_address50 = TestItem.AddressB;
_address150 = TestItem.AddressC;
_auraParameters = new AuRaParameters
_auraParameters = new AuRaChainSpecEngineParameters()
{
BlockRewardContractAddress = _address10,
BlockRewardContractTransition = 10,
BlockReward = new Dictionary<long, UInt256>() { { 0, 200 } },
BlockReward = new SortedDictionary<long, UInt256>() { { 0, 200 } },
};

_abiEncoder = Substitute.For<IAbiEncoder>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
Expand All @@ -27,7 +28,6 @@
using Nethermind.Int256;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Trie.Pruning;
using Nethermind.TxPool;
using NSubstitute;
Expand Down Expand Up @@ -267,12 +267,6 @@ public class TestTxPermissionsBlockchain : TestContractBlockchain

protected override BlockProcessor CreateBlockProcessor()
{
AuRaParameters.Validator validator = new()
{
Addresses = TestItem.Addresses,
ValidatorType = AuRaParameters.ValidatorType.List
};

TransactionPermissionContractVersions =
new LruCache<ValueHash256, UInt256>(PermissionBasedTxFilter.Cache.MaxCacheSize, nameof(TransactionPermissionContract));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Nethermind.Consensus.Rewards;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Specs.Test.ChainSpecStyle;

namespace Nethermind.Blockchain.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Nethermind.Int256;
using Nethermind.JsonRpc.Test.Modules;
using Nethermind.Specs;
using Nethermind.Specs.Test.ChainSpecStyle;
using Nethermind.TxPool;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Nethermind.Logging;
using Nethermind.Specs;
using Nethermind.Specs.Forks;
using Nethermind.Specs.Test.ChainSpecStyle;
using Nethermind.State;
using NSubstitute;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Nethermind.Blockchain.Services;
using Nethermind.Core;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Test.ChainSpecStyle;
using NUnit.Framework;

namespace Nethermind.Blockchain.Test.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.JsonRpc.Test.Modules;
using Nethermind.Specs.Test.ChainSpecStyle;
using Nethermind.State;
using NSubstitute;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using Nethermind.Blockchain.Services;
using Nethermind.Consensus.Clique;
using Nethermind.Core;
using Nethermind.Specs.ChainSpecStyle;
using NSubstitute;
using NUnit.Framework;

Expand All @@ -29,7 +27,7 @@ public void GetBlockProcessorAndProducerIntervalHint_returns_expected_result(

public class BlockProcessorIntervalHint
{
public ChainSpec ChainSpec { get; set; }
public CliqueChainSpecEngineParameters ChainSpec { get; set; }

public ulong ValidatorsCount { get; set; }

Expand All @@ -47,27 +45,27 @@ public static IEnumerable<BlockProcessorIntervalHint> BlockProcessorIntervalHint
{
yield return new BlockProcessorIntervalHint()
{
ChainSpec = new ChainSpec() { SealEngineType = SealEngineType.Clique, Clique = new CliqueParameters() { Period = 15 } },
ChainSpec = new CliqueChainSpecEngineParameters { Period = 15 },
ExpectedProcessingHint = 60,
ExpectedProducingHint = 30
};
yield return new BlockProcessorIntervalHint()
{
ChainSpec = new ChainSpec() { SealEngineType = SealEngineType.Clique, Clique = new CliqueParameters() { Period = 23 } },
ChainSpec = new CliqueChainSpecEngineParameters { Period = 23 },
ExpectedProcessingHint = 92,
ExpectedProducingHint = 46
};
yield return new BlockProcessorIntervalHint()
{
ValidatorsCount = 10,
ChainSpec = new ChainSpec() { SealEngineType = SealEngineType.Clique, Clique = new CliqueParameters() { Period = 23 } },
ChainSpec = new CliqueChainSpecEngineParameters { Period = 23 },
ExpectedProcessingHint = 92,
ExpectedProducingHint = 460
};
yield return new BlockProcessorIntervalHint()
{
ValidatorsCount = 2,
ChainSpec = new ChainSpec() { SealEngineType = SealEngineType.Clique, Clique = new CliqueParameters() { Period = 10 } },
ChainSpec = new CliqueChainSpecEngineParameters { Period = 10 },
ExpectedProcessingHint = 40,
ExpectedProducingHint = 40
};
Expand Down
Loading

0 comments on commit c91e0b2

Please sign in to comment.