Skip to content

Commit

Permalink
Aura/fix fullsync after fastblocks (NethermindEth#2049)
Browse files Browse the repository at this point in the history
* For AuRa FullSync can't be in parallel with FastBlocks

* Move checking if validator is finalized to going backward instead of forward

* Revert "For AuRa FullSync can't be in parallel with FastBlocks"

This reverts commit b4edce5.

* Revert "For AuRa FullSync can't be in parallel with FastBlocks (NethermindEth#2047)"

This reverts commit f679530.

* fix tests
  • Loading branch information
LukaszRozmej authored Jun 24, 2020
1 parent f679530 commit 9569b44
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 122 deletions.
28 changes: 20 additions & 8 deletions src/Nethermind/Chains/xdai.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "xDai Chain",
"name": "DaiChain",
"engine": {
"authorityRound": {
"params": {
Expand All @@ -16,11 +16,21 @@
},
"1300": {
"safeContract": "0x22e1229a2c5b95a60983b5577f745a603284f535"
},
"9186425": {
"contract": "0xB87BE9f7196F2AE084Ca1DE6af5264292976e013"
}
}
},
"blockRewardContractAddress": "0x867305d19606aadba405ce534e303d0e225f9556",
"blockRewardContractTransition": 1310
"blockRewardContractTransition": 1310,
"blockRewardContractTransitions": {
"9186425": "0x481c034c6d9441db23Ea48De68BCAe812C5d39bA"
},
"randomnessContractAddress": {
"9186425": "0x5870b0527DeDB1cFBD9534343Feda1a41Ce47766"
},
"posdaoTransition": 9186425
}
}
},
Expand All @@ -43,7 +53,9 @@
"eip1706Transition": 7298030,
"eip1884Transition": 7298030,
"eip2028Transition": 7298030,
"registrar": "0x1ec97dc137f5168af053c24460a1200502e1a9d2"
"registrar": "0x6B53721D4f2Fb9514B85f5C49b197D857e36Cf03",
"transactionPermissionContract": "0x7Dd7032AA75A37ea0b150f57F899119C7379A78b",
"transactionPermissionContractTransition": 9186425
},
"genesis": {
"seal": {
Expand Down Expand Up @@ -89,7 +101,7 @@
}
},
"7298030": {
"info": "EIP 1108 transition",
"info": "Istanbul HF",
"price": {
"alt_bn128_const_operations": {
"price": 150
Expand All @@ -111,7 +123,7 @@
}
},
"7298030": {
"info": "EIP 1108 transition",
"info": "Istanbul HF",
"price": {
"alt_bn128_const_operations": {
"price": 6000
Expand All @@ -134,7 +146,7 @@
}
},
"7298030": {
"info": "EIP 1108 transition",
"info": "Istanbul HF",
"price": {
"alt_bn128_pairing": {
"base": 45000,
Expand All @@ -150,7 +162,7 @@
"name": "blake2_f",
"pricing": {
"7298030": {
"info": "EIP 1108 transition",
"info": "Istanbul HF",
"price": {
"blake2_f": {
"gas_per_round": 1
Expand Down Expand Up @@ -225,4 +237,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Initialize()
_logManager = LimboLogs.Instance;
_validSealerStrategy = Substitute.For<IValidSealerStrategy>();

_validatorStore.GetValidators().Returns(new Address[] {TestItem.AddressA, TestItem.AddressB, TestItem.AddressC});
_validatorStore.GetValidators(Arg.Any<long?>()).Returns(new Address[] {TestItem.AddressA, TestItem.AddressB, TestItem.AddressC});

Rlp.Decoders[typeof(BlockInfo)] = new BlockInfoDecoder(true);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public static IEnumerable FinalizingTests
[TestCaseSource(nameof(FinalizingTests))]
public void correctly_finalizes_blocks_in_chain(int chainLength, long twoThirdsMajorityTransition, Address[] blockCreators, int notFinalizedExpectedCount)
{
_validatorStore.GetValidators().Returns(blockCreators);
_validatorStore.GetValidators(Arg.Any<long?>()).Returns(blockCreators);

var blockTreeBuilder = Build.A.BlockTree();
HashSet<BlockHeader> finalizedBlocks = new HashSet<BlockHeader>();
Expand Down Expand Up @@ -152,7 +152,7 @@ public void correctly_finalizes_blocks_in_already_in_chain_on_initialize()
[TestCase(4, 5, ExpectedResult = new[] {1, 3, 1, 0, 0})]
public int[] correctly_finalizes_blocks_on_reorganisations(int validators, int chainLength)
{
_validatorStore.GetValidators().Returns(TestItem.Addresses.Take(validators).ToArray());
_validatorStore.GetValidators(Arg.Any<long?>()).Returns(TestItem.Addresses.Take(validators).ToArray());

void ProcessBlock(BlockTreeBuilder blockTreeBuilder1, int level, int index)
{
Expand Down Expand Up @@ -212,38 +212,11 @@ public long GetLastFinalizedBy_test(int chainLength, Address[] beneficiaries, in
var result = finalizationManager.GetLastLevelFinalizedBy(blockTree.Head.Hash);
return result;
}

public static IEnumerable GetFinalizedLevelTests
{
get
{
yield return new TestCaseData(2, 1, new[] {TestItem.AddressA, TestItem.AddressB}, 2) {ExpectedResult = null};
yield return new TestCaseData(10, 9, new[] {TestItem.AddressA, TestItem.AddressB}, 2) {ExpectedResult = null};
yield return new TestCaseData(10, 8, new[] {TestItem.AddressA, TestItem.AddressB}, 2) {ExpectedResult = 9};
yield return new TestCaseData(10, 3, new[] {TestItem.AddressA, TestItem.AddressB}, 2) {ExpectedResult = 4};
yield return new TestCaseData(10, 3, new[] {TestItem.AddressA, TestItem.AddressB, TestItem.AddressC}, 2) {ExpectedResult = 4};
yield return new TestCaseData(10, 3, new[] {TestItem.AddressA, TestItem.AddressB, TestItem.AddressC}, 3) {ExpectedResult = 5};
yield return new TestCaseData(10, 3, new[] {TestItem.AddressA, TestItem.AddressB, TestItem.AddressC}, 4) {ExpectedResult = null};
}
}

[TestCaseSource(nameof(GetFinalizedLevelTests))]
public long? GetFinalizedLevel_test(int chainLength, int levelToCheck, Address[] beneficiaries, int minForFinalization)
{
SetupValidators(beneficiaries, minForFinalization);
_validSealerStrategy.IsValidSealer(Arg.Any<IList<Address>>(), Arg.Any<Address>(), Arg.Any<long>()).Returns(c => beneficiaries.GetItemRoundRobin(c.Arg<long>()) == c.Arg<Address>());
var blockTreeBuilder = Build.A.BlockTree().OfChainLength(chainLength, 0, 0, beneficiaries);
var blockTree = blockTreeBuilder.TestObject;
var finalizationManager = new AuRaBlockFinalizationManager(blockTree, blockTreeBuilder.ChainLevelInfoRepository, _blockProcessor, _validatorStore, _validSealerStrategy, _logManager);

var result = finalizationManager.GetFinalizedLevel(levelToCheck);
return result;
}

private void SetupValidators(Address[] beneficiaries, int minForFinalization)
{
var validators = beneficiaries.Union(TestItem.Addresses.TakeLast(Math.Max(0, minForFinalization - 1) * 2 - beneficiaries.Length)).ToArray();
_validatorStore.GetValidators().Returns(validators);
_validatorStore.GetValidators(Arg.Any<long?>()).Returns(validators);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public long initializes_validator_when_on_nonconsecutive_block(long blockNumber,
_validator = GetValidator(validatorType);
IAuRaValidator validator = new MultiValidator(_validator, _factory, _blockTree, _validatorStore, _finalizationManager, default, _logManager);
var validatorBlockLevel = (blockNumber - 1)/10*10;
_finalizationManager.GetFinalizedLevel(validatorBlockLevel).Returns(finalizedLastValidatorBlockLevel ? blockNumber - 2 : (long?) null);
_finalizationManager.GetLastLevelFinalizedBy(_block.ParentHash).Returns(finalizedLastValidatorBlockLevel ? blockNumber - 2 : 1);
_block.Header.Number = blockNumber;
validator.OnBlockProcessingStart(_block);
return _innerValidators.Keys.Last();
Expand Down Expand Up @@ -261,4 +261,4 @@ private static AuRaParameters.Validator GetValidator(AuRaParameters.ValidatorTyp
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,5 @@ public interface IBlockFinalizationManager : IDisposable
/// <returns>Last level that was finalized by block hash.</returns>
/// <remarks>This is used when we have nonconsecutive block processing, like just switching from Fast to Full sync or when producing blocks. It is used when trying to find a non-finalized InitChange event.</remarks>
long GetLastLevelFinalizedBy(Keccak blockHash);

/// <summary>
/// Get last level at which the <see cref="blockLevel"/> is finalized.
/// </summary>
/// <param name="blockLevel">Block level which finality we want to check.</param>
/// <returns>Block level at which the argument level is finalized. Null when its not finalized.</returns>
/// <remarks>This is used when we have nonconsecutive block processing, like just switching from Fast to Full sync or when producing blocks. It is used when trying to find if a validator transition is already finalized.</remarks>
long? GetFinalizedLevel(long blockLevel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,5 @@ public interface ISyncConfig : IConfig

[ConfigItem(Description = "Should use beam sync to fix corrupted state DB (dev use).", DefaultValue = "false")]
public bool BeamSyncFixMode { get; set; }

[ConfigItem(Description = "If FullSync can be run in parallel with FastBlocks. It is false for AuRa chains.", DefaultValue = "true")]
public bool AllowFullSyncParallelToFastBlocks { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ public class SyncConfig : ISyncConfig
public int BeamSyncContextTimeout { get; set; } = 4;
public int BeamSyncPreProcessorTimeout { get; set; } = 15;
public bool BeamSyncFixMode { get; set; } = false;
public bool AllowFullSyncParallelToFastBlocks { get; set; } = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ private IReadOnlyList<BlockHeader> GetFinalizedBlocks(BlockHeader block)
return finalizedBlocks;
}

private Address[] Validators => _validatorStore.GetValidators();

/* Simple, unoptimized method implementation for reference:
private IReadOnlyList<BlockHeader> GetFinalizedBlocks(BlockHeader block)
{
Expand Down Expand Up @@ -270,40 +268,10 @@ public long GetLastLevelFinalizedBy(Keccak blockHash)
return 0;
}

public long? GetFinalizedLevel(long blockLevel)
{
BlockInfo GetBlockInfo(long level)
{
var chainLevelInfo = _chainLevelInfoRepository.LoadLevel(level);
return chainLevelInfo?.MainChainBlock ?? chainLevelInfo?.BlockInfos[0];
}

var validators = new HashSet<Address>();
var minSealersForFinalization = GetMinSealersForFinalization(blockLevel);
var blockInfo = GetBlockInfo(blockLevel);
while (blockInfo != null)
{
var block = _blockTree.FindHeader(blockInfo.BlockHash, BlockTreeLookupOptions.None);
if (_validSealerStrategy.IsValidSealer(Validators, block.Beneficiary, block.AuRaStep.Value))
{
validators.Add(block.Beneficiary);
if (validators.Count >= minSealersForFinalization)
{
return block.Number;
}
}

blockLevel++;
blockInfo = GetBlockInfo(blockLevel);
}

return null;
}

private int GetMinSealersForFinalization(long blockNumber) =>
blockNumber == 0
? 1
: Validators.MinSealersForFinalization(blockNumber >= _twoThirdsMajorityTransition);
: _validatorStore.GetValidators(blockNumber).MinSealersForFinalization(blockNumber >= _twoThirdsMajorityTransition);

public long LastFinalizedBlockLevel
{
Expand Down Expand Up @@ -398,4 +366,4 @@ public BlockHeader GetBlockThatWillBeFinalized(out HashSet<Address> validators,
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public void OnBlockProcessingStart(Block block, ProcessingOptions options = Proc
if (!canSetValidatorAsCurrent)
{
SetCurrentValidator(previousValidatorInfo, parentHeader);
finalizedAtBlockNumber = _blockFinalizationManager.GetFinalizedLevel(validatorInfo.Key);
canSetValidatorAsCurrent = finalizedAtBlockNumber != null;
finalizedAtBlockNumber = _blockFinalizationManager.GetLastLevelFinalizedBy(block.ParentHash);
canSetValidatorAsCurrent = finalizedAtBlockNumber >= validatorInfo.Key;
}

if (canSetValidatorAsCurrent)
Expand Down Expand Up @@ -237,4 +237,4 @@ public void TryReportSkipped(BlockHeader header, BlockHeader parent)

public IEnumerable<Transaction> GetTransactions(BlockHeader parent, long gasLimit) => _currentValidator is ITxSource txSource ? txSource.GetTransactions(parent, gasLimit) : Enumerable.Empty<Transaction>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
using System.Threading;
using Nethermind.Blockchain;
using Nethermind.Config;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Evm.Tracing;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules;
using Nethermind.JsonRpc.Modules.Proof;
using Nethermind.KeyStore;
using Nethermind.Logging;
using Nethermind.Runner.Ethereum;
using Nethermind.Runner.Ethereum.Context;
Expand Down Expand Up @@ -51,6 +56,8 @@ public void Proof_module_is_registered_if_configured()
EthereumRunnerContext context = Build.ContextWithMocks();
context.ConfigProvider = configProvider;
context.RpcModuleProvider = rpcModuleProvider;
context.Signer = new Signer(ChainId.Mainnet, TestItem.PrivateKeyA, LimboLogs.Instance);
context.KeyStore = Substitute.For<IKeyStore>();

RegisterRpcModules registerRpcModules = new RegisterRpcModules(context);
registerRpcModules.Execute(CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Linq;
using Nethermind.Blockchain.Processing;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Blockchain.Validators;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Config;
Expand All @@ -41,7 +40,6 @@ public class InitializeBlockchainAuRa : InitializeBlockchain
public InitializeBlockchainAuRa(AuRaEthereumRunnerContext context) : base(context)
{
_context = context;
_context.Config<ISyncConfig>().AllowFullSyncParallelToFastBlocks = false;
}

protected override BlockProcessor CreateBlockProcessor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,16 +666,6 @@ void Test()
}
}
}

public ScenarioBuilder WhenAuRa()
{
_overwrites.Add(() =>
{
SyncConfig.AllowFullSyncParallelToFastBlocks = false;
});

return this;
}
}

public static ScenarioBuilder GoesLikeThis()
Expand Down Expand Up @@ -898,17 +888,6 @@ public void Just_after_finishing_state_sync_but_not_fast_blocks()
.WhenBeamSyncIsConfigured()
.TheSyncModeShouldBe(SyncMode.Full | SyncMode.FastBlocks);
}

[Test]
public void Just_after_finishing_state_sync_but_not_fast_blocks_on_AuRa()
{
Scenario.GoesLikeThis()
.IfThisNodeFinishedStateSyncButNotFastBlocks()
.AndGoodPeersAreKnown()
.WhenBeamSyncIsConfigured()
.WhenAuRa()
.TheSyncModeShouldBe(SyncMode.FastBlocks);
}

[Test]
public void When_finished_fast_sync_and_pre_pivot_block_appears()
Expand Down Expand Up @@ -1110,4 +1089,4 @@ public void When_long_range_state_catch_up_is_needed()
.TheSyncModeShouldBe(SyncMode.StateNodes | SyncMode.Beam);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ private bool ShouldBeInFullSyncMode(Snapshot best)
bool notInBeamSync = !best.IsInBeamSync;
bool notInFastSync = !best.IsInFastSync;
bool notInStateSync = !best.IsInStateSync;
bool notInConflictWithFastBlocks = _syncConfig.AllowFullSyncParallelToFastBlocks || !best.IsInFastBlocks;

if (_logger.IsTrace)
{
Expand All @@ -285,16 +284,14 @@ private bool ShouldBeInFullSyncMode(Snapshot best)
_logger.Trace("notInBeamSync " + notInBeamSync);
_logger.Trace("notInFastSync " + notInFastSync);
_logger.Trace("notInStateSync " + notInStateSync);
_logger.Trace("notInConflictWithFastBlocks " + notInConflictWithFastBlocks);
}

return desiredPeerKnown &&
postPivotPeerAvailable &&
hasFastSyncBeenActive &&
notInBeamSync &&
notInFastSync &&
notInStateSync &&
notInConflictWithFastBlocks;
notInStateSync;
}

private bool ShouldBeInFastBlocksMode(Snapshot best)
Expand Down Expand Up @@ -491,4 +488,4 @@ public Snapshot(long processed, long state, long block, long header, long peerBl
public UInt256 PeerDifficulty { get; }
}
}
}
}

0 comments on commit 9569b44

Please sign in to comment.