public void Can_sync_on_split_of_length_1() { BlockTree miner1Tree = Build.A.BlockTree(_genesisBlock).OfChainLength(6).TestObject; ISyncPeer miner1 = new SyncPeerMock(miner1Tree); ManualResetEvent resetEvent = new ManualResetEvent(false); _synchronizer.SyncEvent += (sender, args) => { TestContext.WriteLine(args.SyncEvent); if (args.SyncEvent == SyncEvent.Completed || args.SyncEvent == SyncEvent.Failed) { resetEvent.Set(); } }; _pool.Start(); _synchronizer.Start(); _pool.AddPeer(miner1); resetEvent.WaitOne(_standardTimeoutUnit); miner1Tree.BestSuggestedHeader.Should().BeEquivalentTo(_blockTree.BestSuggestedHeader, "client agrees with miner before split"); Block splitBlock = Build.A.Block.WithParent(miner1Tree.FindParent(miner1Tree.Head, BlockTreeLookupOptions.TotalDifficultyNotNeeded)).WithDifficulty(miner1Tree.Head.Difficulty - 1).TestObject; Block splitBlockChild = Build.A.Block.WithParent(splitBlock).TestObject; miner1Tree.SuggestBlock(splitBlock); miner1Tree.UpdateMainChain(splitBlock); miner1Tree.SuggestBlock(splitBlockChild); miner1Tree.UpdateMainChain(splitBlockChild); splitBlockChild.Header.Should().BeEquivalentTo(miner1Tree.BestSuggestedHeader, "split as expected"); resetEvent.Reset(); _syncServer.AddNewBlock(splitBlockChild, miner1); resetEvent.WaitOne(_standardTimeoutUnit); Assert.AreEqual(miner1Tree.BestSuggestedHeader.Hash, _blockTree.BestSuggestedHeader.Hash, "client agrees with miner after split"); }
public async Task Does_not_do_full_sync_when_not_needed_with_split() { BlockTree minerTree = Build.A.BlockTree(_genesisBlock).OfChainLength(6).TestObject; ISyncPeer miner1 = new SyncPeerMock(minerTree); AutoResetEvent resetEvent = new AutoResetEvent(false); _synchronizer.SyncEvent += (sender, args) => { if (args.SyncEvent == SyncEvent.Completed || args.SyncEvent == SyncEvent.Failed) { resetEvent.Set(); } }; _pool.Start(); _synchronizer.Start(); _pool.AddPeer(miner1); resetEvent.WaitOne(_standardTimeoutUnit); Assert.AreEqual(minerTree.BestSuggestedHeader.Hash, _blockTree.BestSuggestedHeader.Hash, "client agrees with miner before split"); Block newBlock = Build.A.Block.WithParent(minerTree.Head).TestObject; minerTree.SuggestBlock(newBlock); minerTree.UpdateMainChain(newBlock); ISyncPeer miner2 = Substitute.For <ISyncPeer>(); miner2.GetHeadBlockHeader(Arg.Any <Keccak>(), Arg.Any <CancellationToken>()).Returns(miner1.GetHeadBlockHeader(null, CancellationToken.None)); miner2.Node.Id.Returns(TestItem.PublicKeyB); Assert.AreEqual(newBlock.Number, await miner2.GetHeadBlockHeader(null, Arg.Any <CancellationToken>()), "number as expected"); _pool.Start(); _synchronizer.Start(); _pool.AddPeer(miner2); resetEvent.WaitOne(_standardTimeoutUnit); await miner2.Received().GetBlockHeaders(6, 1, 0, default(CancellationToken)); }
public void Initialize() { var logger = LimboLogs.Instance; var specProvider = MainnetSpecProvider.Instance; var ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, logger); var txStorage = new InMemoryTxStorage(); Peer peerA = SetUpPeerA(); //standard case Peer peerB = SetUpPeerB(); //Session is null Peer peerC = SetUpPeerC(); //Node is null, Caps are empty IPeerManager peerManager = Substitute.For <IPeerManager>(); peerManager.ActivePeers.Returns(new List <Peer> { peerA, peerB, peerC }); peerManager.ConnectedPeers.Returns(new List <Peer> { peerA, peerB, peerA, peerC, peerB }); peerManager.MaxActivePeers.Returns(15); StateProvider stateProvider = new StateProvider(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance); var txPool = new TxPool.TxPool(txStorage, ethereumEcdsa, new FixedBlockChainHeadSpecProvider(specProvider), new TxPoolConfig(), stateProvider, new TxValidator(specProvider.ChainId), LimboLogs.Instance); IDb blockDb = new MemDb(); IDb headerDb = new MemDb(); IDb blockInfoDb = new MemDb(); IBlockTree blockTree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, LimboLogs.Instance); new OnChainTxWatcher(blockTree, txPool, specProvider, LimboLogs.Instance); IReceiptStorage receiptStorage = new InMemoryReceiptStorage(); _signerStore = new Signer(specProvider.ChainId, TestItem.PrivateKeyB, logger); _parityModule = new ParityModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545), _signerStore, new MemKeyStore(new[] { TestItem.PrivateKeyA }), logger, peerManager); var blockNumber = 2; var pendingTransaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)).TestObject; pendingTransaction.Signature.V = 37; stateProvider.CreateAccount(pendingTransaction.SenderAddress, UInt256.UInt128MaxValue); txPool.AddTransaction(pendingTransaction, TxHandlingOptions.None); blockNumber = 1; var transaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)) .WithNonce(100).TestObject; transaction.Signature.V = 37; stateProvider.CreateAccount(transaction.SenderAddress, UInt256.UInt128MaxValue); txPool.AddTransaction(transaction, TxHandlingOptions.None); Block genesis = Build.A.Block.Genesis .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .TestObject; blockTree.SuggestBlock(genesis); blockTree.UpdateMainChain(new[] { genesis }, true); Block previousBlock = genesis; Block block = Build.A.Block.WithNumber(blockNumber).WithParent(previousBlock) .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .WithTransactions(MuirGlacier.Instance, transaction) .TestObject; blockTree.SuggestBlock(block); blockTree.UpdateMainChain(new[] { block }, true); var logEntries = new[] { Build.A.LogEntry.TestObject }; receiptStorage.Insert(block, new TxReceipt() { Bloom = new Bloom(logEntries), Index = 1, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = TestItem.KeccakA, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = transaction.Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }); }
private void MineBlock(BlockTree tree, Block block) { tree.SuggestBlock(block); tree.UpdateMainChain(block); }
public void Initialize() { var logger = LimboLogs.Instance; var specProvider = MainnetSpecProvider.Instance; var ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, logger); var txStorage = new InMemoryTxStorage(); var txPool = new TxPool.TxPool(txStorage, Timestamper.Default, ethereumEcdsa, specProvider, new TxPoolConfig(), new StateProvider(new StateDb(), new MemDb(), LimboLogs.Instance), LimboLogs.Instance); IDb blockDb = new MemDb(); IDb headerDb = new MemDb(); IDb blockInfoDb = new MemDb(); IBlockTree blockTree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, txPool, NullBloomStorage.Instance, LimboLogs.Instance); IReceiptStorage receiptStorage = new InMemoryReceiptStorage(); _parityModule = new ParityModule(ethereumEcdsa, txPool, blockTree, receiptStorage, logger); var blockNumber = 2; var pendingTransaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)).TestObject; pendingTransaction.Signature.V = 37; txPool.AddTransaction(pendingTransaction, TxHandlingOptions.None); blockNumber = 1; var transaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)) .WithNonce(100).TestObject; transaction.Signature.V = 37; txPool.AddTransaction(transaction, TxHandlingOptions.None); Block genesis = Build.A.Block.Genesis .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .TestObject; blockTree.SuggestBlock(genesis); blockTree.UpdateMainChain(new[] { genesis }, true); Block previousBlock = genesis; Block block = Build.A.Block.WithNumber(blockNumber).WithParent(previousBlock) .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .WithTransactions(transaction) .TestObject; blockTree.SuggestBlock(block); blockTree.UpdateMainChain(new[] { block }, true); var logEntries = new[] { Build.A.LogEntry.TestObject }; receiptStorage.Insert(block, false, new TxReceipt() { Bloom = new Bloom(logEntries), Index = 1, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = TestItem.KeccakA, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = transaction.Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }); }
public void Initialize() { LimboLogs logger = LimboLogs.Instance; MainnetSpecProvider specProvider = MainnetSpecProvider.Instance; EthereumEcdsa ethereumEcdsa = new(specProvider.ChainId, logger); Peer peerA = SetUpPeerA(); //standard case Peer peerB = SetUpPeerB(); //Session is null Peer peerC = SetUpPeerC(); //Node is null, Caps are empty IPeerManager peerManager = Substitute.For <IPeerManager>(); peerManager.ActivePeers.Returns(new List <Peer> { peerA, peerB, peerC }); peerManager.ConnectedPeers.Returns(new List <Peer> { peerA, peerB, peerA, peerC, peerB }); peerManager.MaxActivePeers.Returns(15); StateProvider stateProvider = new(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance); StateReader stateReader = new(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance); IDb blockDb = new MemDb(); IDb headerDb = new MemDb(); IDb blockInfoDb = new MemDb(); IBlockTree blockTree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, LimboLogs.Instance); ITransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, blockTree); TxPool.TxPool txPool = new(ethereumEcdsa, new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(specProvider), blockTree, stateProvider), new TxPoolConfig(), new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer()); IReceiptStorage receiptStorage = new InMemoryReceiptStorage(); _signerStore = new Signer(specProvider.ChainId, TestItem.PrivateKeyB, logger); _parityRpcModule = new ParityRpcModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545), _signerStore, new MemKeyStore(new[] { TestItem.PrivateKeyA }), MainnetSpecProvider.Instance, peerManager); int blockNumber = 2; Transaction pendingTransaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)).TestObject; pendingTransaction.Signature.V = 37; stateProvider.CreateAccount(pendingTransaction.SenderAddress, UInt256.UInt128MaxValue); txPool.SubmitTx(pendingTransaction, TxHandlingOptions.None); blockNumber = 1; Transaction transaction1 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)) .WithNonce(100).TestObject; transaction1.Signature.V = 37; stateProvider.CreateAccount(transaction1.SenderAddress, UInt256.UInt128MaxValue); var transaction2 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)) .WithNonce(120).TestObject; transaction2.Signature.V = 37; var transaction3 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false) .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)) .WithNonce(110).TestObject; transaction2.Signature.V = 37; Block genesis = Build.A.Block.Genesis .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .TestObject; blockTree.SuggestBlock(genesis); blockTree.UpdateMainChain(new[] { genesis }, true); Block previousBlock = genesis; Block block = Build.A.Block.WithNumber(blockNumber).WithParent(previousBlock) .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")) .WithTransactions(transaction1, transaction2, transaction3) .TestObject; blockTree.SuggestBlock(block); blockTree.UpdateMainChain(new[] { block }, true); LogEntry[] logEntries = new[] { Build.A.LogEntry.TestObject }; TxReceipt receipt1 = new() { Bloom = new Bloom(logEntries), Index = 0, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = TestItem.KeccakA, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = transaction1.Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }; TxReceipt receipt2 = new() { Bloom = new Bloom(logEntries), Index = 1, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = TestItem.KeccakA, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = transaction2.Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }; TxReceipt receipt3 = new() { Bloom = new Bloom(logEntries), Index = 2, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = TestItem.KeccakA, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = transaction3.Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }; receiptStorage.Insert(block, receipt1, receipt2, receipt3); } private static Peer SetUpPeerA() { Node node = new(TestItem.PublicKeyA, "127.0.0.1", 30303, true); node.ClientId = "Geth/v1.9.21-stable/linux-amd64/go1.15.2"; Peer peer = new(node); peer.InSession = null; peer.OutSession = Substitute.For <ISession>(); peer.OutSession.RemoteNodeId.Returns(TestItem.PublicKeyA); IProtocolHandler protocolHandler = Substitute.For <IProtocolHandler, ISyncPeer>(); peer.OutSession.TryGetProtocolHandler(Protocol.Eth, out Arg.Any <IProtocolHandler>()).Returns(x => { x[1] = protocolHandler; return(true); }); byte version = 65; protocolHandler.ProtocolVersion.Returns(version); if (protocolHandler is ISyncPeer syncPeer) { UInt256 difficulty = 0x5ea4ed; syncPeer.TotalDifficulty.Returns(difficulty); syncPeer.HeadHash.Returns(TestItem.KeccakA); } IProtocolHandler p2PProtocolHandler = Substitute.For <IProtocolHandler, IP2PProtocolHandler>(); peer.OutSession.TryGetProtocolHandler(Protocol.P2P, out Arg.Any <IProtocolHandler>()).Returns(x => { x[1] = p2PProtocolHandler; return(true); }); if (p2PProtocolHandler is IP2PProtocolHandler p2PHandler) { p2PHandler.AgreedCapabilities.Returns(new List <Capability> { new("eth", 65), new("eth", 64) }); }