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

[WIP]Make chainspec extendable by plugins #7540

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2ada5d8
Custom chainspec engine
deffrian Oct 1, 2024
d9d4f2a
Optimism & add more types to decoder
deffrian Oct 2, 2024
d61acc3
Update src/Nethermind/Nethermind.Config/ConfigSourceHelper.cs
deffrian Oct 3, 2024
6ea1a10
Update src/Nethermind/Nethermind.Config/ConfigSourceHelper.cs
deffrian Oct 3, 2024
33f8a6c
Small fixes
deffrian Oct 5, 2024
f7b8e32
Update src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecParame…
deffrian Oct 6, 2024
32cbfa4
Small fixes
deffrian Oct 6, 2024
70d2227
Custom chainspec engine
deffrian Oct 1, 2024
da4313b
Optimism & add more types to decoder
deffrian Oct 2, 2024
3c39505
Update src/Nethermind/Nethermind.Config/ConfigSourceHelper.cs
deffrian Oct 3, 2024
c44f8e8
Update src/Nethermind/Nethermind.Config/ConfigSourceHelper.cs
deffrian Oct 3, 2024
7f8ae03
Small fixes
deffrian Oct 5, 2024
be56036
Update src/Nethermind/Nethermind.Specs/ChainSpecStyle/ChainSpecParame…
deffrian Oct 6, 2024
b6f1a53
Small fixes
deffrian Oct 6, 2024
bed5240
Merge remote-tracking branch 'origin/refactor/chainspec-v2' into refa…
deffrian Oct 6, 2024
4b5d291
NethDev
deffrian Oct 6, 2024
777c49a
Clique
deffrian Oct 7, 2024
4261b1e
Fix tests
deffrian Oct 7, 2024
2946160
Runner tests
deffrian Oct 7, 2024
6d0ece3
Merge branch 'master' into refactor/chainspec-v2
deffrian Oct 11, 2024
ddbcc2d
Fix build
deffrian Oct 11, 2024
15cb899
Ethash & switch to json serialization
deffrian Oct 14, 2024
a9dd55e
Fix tests
deffrian Oct 15, 2024
c3d296f
Merge branch 'master' into refactor/chainspec-v2
deffrian Oct 15, 2024
67d89e1
Aura
deffrian Oct 21, 2024
7705a5c
Merge branch 'master' into refactor/chainspec-v2
deffrian Oct 21, 2024
0a7f661
Fix build
deffrian Oct 21, 2024
dde5690
Merge branch 'master' into refactor/chainspec-v2
deffrian Oct 21, 2024
18ce8a2
Fix tests
deffrian Oct 22, 2024
931b4ac
Fix merge tests
deffrian Oct 23, 2024
8e9ab40
Merge branch 'master' into refactor/chainspec-v2
deffrian Oct 23, 2024
300dd01
AuraParameters
deffrian Oct 23, 2024
d488037
Remove comments
deffrian Oct 23, 2024
5c40e24
TODOs
deffrian Oct 24, 2024
24c91c1
Fix spec tests
deffrian Oct 24, 2024
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
5 changes: 4 additions & 1 deletion src/Nethermind/Nethermind.AuRa.Test/AuRaPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 +20,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 AuthorityRoundChainSpecEngineParameters());
Action init = () => auRaPlugin.Init(new AuRaNethermindApi(new ConfigProvider(), new EthereumJsonSerializer(), new TestLogManager(), chainSpec));
init.Should().NotThrow();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.AuRa.Test/AuRaSealValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Nethermind.AuRa.Test
public class AuRaSealValidatorTests
{
private AuRaSealValidator _sealValidator;
private AuRaParameters _auRaParameters;
private AuthorityRoundChainSpecEngineParameters _auRaParameters;
private IAuRaStepCalculator _auRaStepCalculator;
private ILogManager _logManager;
private IWallet _wallet;
Expand All @@ -38,7 +38,7 @@ public class AuRaSealValidatorTests
[SetUp]
public void SetUp()
{
_auRaParameters = new AuRaParameters();
_auRaParameters = new AuthorityRoundChainSpecEngineParameters();
_auRaStepCalculator = Substitute.For<IAuRaStepCalculator>();
_logManager = LimboLogs.Instance;
_wallet = new DevWallet(new WalletConfig(), _logManager);
Expand Down Expand Up @@ -89,7 +89,7 @@ BlockHeaderBuilder GetParentBlock() => Build.A.BlockHeader
TestCaseData GetTestCaseData(
BlockHeaderBuilder parent,
BlockHeaderBuilder block,
Action<AuRaParameters> paramAction = null,
Action<AuthorityRoundChainSpecEngineParameters> paramAction = null,
Repeat repeat = Repeat.No,
bool parentIsHead = true,
bool isValidSealer = true) =>
Expand Down Expand Up @@ -141,7 +141,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<AuthorityRoundChainSpecEngineParameters> 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
94 changes: 94 additions & 0 deletions src/Nethermind/Nethermind.AuRa.Test/ChainSpecLoaderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.IO;
using FluentAssertions;
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<AuthorityRoundChainSpecEngineParameters>();

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<AuthorityRoundChainSpecEngineParameters>();

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<AuthorityRoundChainSpecEngineParameters>();

auraParams.RewriteBytecode.Should().BeEquivalentTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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 +82,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<AuthorityRoundChainSpecEngineParameters>().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 +118,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<AuthorityRoundChainSpecEngineParameters>();
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 @@ -25,7 +25,7 @@ namespace Nethermind.AuRa.Test.Reward
{
public class AuRaRewardCalculatorTests
{
private AuRaParameters _auraParameters;
private AuthorityRoundChainSpecEngineParameters _auraParameters;
private IAbiEncoder _abiEncoder;
private ITransactionProcessor _transactionProcessor;
private Block _block;
Expand All @@ -40,11 +40,11 @@ public void SetUp()
_address10 = TestItem.AddressA;
_address50 = TestItem.AddressB;
_address150 = TestItem.AddressC;
_auraParameters = new AuRaParameters
_auraParameters = new AuthorityRoundChainSpecEngineParameters()
{
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
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 Expand Up @@ -115,7 +116,7 @@ public async Task Process_long_running_branch(int blocksAmount)
{
Address address = TestItem.Addresses[0];
TestSingleReleaseSpecProvider spec = new TestSingleReleaseSpecProvider(ConstantinopleFix.Instance);
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev)
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev)
.Build(spec);
testRpc.TestWallet.UnlockAccount(address, new SecureString());
await testRpc.AddFunds(address, 1.Ether());
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 Expand Up @@ -58,7 +59,7 @@ private async Task<ScenarioBuilder> CreateTestBlockchainAsync(long gasLimit)
IsEip155Enabled = true
});
BlockBuilder blockBuilder = Build.A.Block.Genesis.WithGasLimit(gasLimit);
_testRpcBlockchain = await TestRpcBlockchain.ForTest(SealEngineType.NethDev)
_testRpcBlockchain = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev)
.WithGenesisBlockBuilder(blockBuilder)
.Build(spec);
_testRpcBlockchain.TestWallet.UnlockAccount(_address, new SecureString());
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 Expand Up @@ -149,7 +150,7 @@ private async Task<TestRpcBlockchain> CreateTestRpc()
{
Address address = TestItem.Addresses[0];
TestSingleReleaseSpecProvider spec = new(ConstantinopleFix.Instance);
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev)
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev)
.Build(spec);
testRpc.TestWallet.UnlockAccount(address, new SecureString());
await testRpc.AddFunds(address, 1.Ether());
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 Expand Up @@ -39,7 +40,7 @@ public static IEnumerable<BlockProcessorIntervalHint> BlockProcessorIntervalHint
{
yield return new BlockProcessorIntervalHint
{
ChainSpec = new ChainSpec { SealEngineType = SealEngineType.NethDev, }
ChainSpec = new ChainSpec { SealEngineType = TestSealEngineType.NethDev, }
};
yield return new BlockProcessorIntervalHint
{
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 Expand Up @@ -95,7 +96,7 @@ public async Task Deletes_everything_after_the_missing_level()
[TestCase(65)]
public async Task Suggesting_blocks_works_correctly_after_processor_restart(int suggestedBlocksAmount)
{
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build();
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev).Build();
await testRpc.BlockchainProcessor.StopAsync();
IBlockTree tree = testRpc.BlockTree;
long startingBlockNumber = tree.Head!.Number;
Expand Down Expand Up @@ -130,7 +131,7 @@ public async Task Suggesting_blocks_works_correctly_after_processor_restart(int
[TestCase(6)]
public async Task Fixer_should_not_suggest_block_without_state(int suggestedBlocksAmount)
{
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build();
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev).Build();
await testRpc.BlockchainProcessor.StopAsync();
IBlockTree tree = testRpc.BlockTree;

Expand All @@ -152,7 +153,7 @@ public async Task Fixer_should_not_suggest_block_without_state(int suggestedBloc
[Test, MaxTime(Timeout.MaxTestTime)]
public async Task Fixer_should_not_suggest_block_with_null_block()
{
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build();
TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(TestSealEngineType.NethDev).Build();
await testRpc.BlockchainProcessor.StopAsync();
IBlockTree tree = testRpc.BlockTree;

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
Loading