|
| 1 | +// Copyright (c) 2021 Demerzel Solutions Limited |
| 2 | +// This file is part of the Nethermind library. |
| 3 | +// |
| 4 | +// The Nethermind library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The Nethermind library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +// |
| 17 | + |
| 18 | +using System.Net; |
| 19 | +using FluentAssertions; |
| 20 | +using Nethermind.Core; |
| 21 | +using Nethermind.Core.Specs; |
| 22 | +using Nethermind.Core.Test.Builders; |
| 23 | +using Nethermind.Core.Timers; |
| 24 | +using Nethermind.Logging; |
| 25 | +using Nethermind.Network.P2P; |
| 26 | +using Nethermind.Network.P2P.Subprotocols.Eth.V65; |
| 27 | +using Nethermind.Network.Test.Builders; |
| 28 | +using Nethermind.Stats; |
| 29 | +using Nethermind.Stats.Model; |
| 30 | +using Nethermind.Synchronization; |
| 31 | +using Nethermind.TxPool; |
| 32 | +using NSubstitute; |
| 33 | +using NUnit.Framework; |
| 34 | + |
| 35 | +namespace Nethermind.Network.Test.P2P.Subprotocols.Eth.V65 |
| 36 | +{ |
| 37 | + [TestFixture, Parallelizable(ParallelScope.Self)] |
| 38 | + public class Eth65ProtocolHandlerTests |
| 39 | + { |
| 40 | + private ISession _session; |
| 41 | + private IMessageSerializationService _svc; |
| 42 | + private ISyncServer _syncManager; |
| 43 | + private ITxPool _transactionPool; |
| 44 | + private IPooledTxsRequestor _pooledTxsRequestor; |
| 45 | + private ISpecProvider _specProvider; |
| 46 | + private Block _genesisBlock; |
| 47 | + private Eth65ProtocolHandler _handler; |
| 48 | + |
| 49 | + [SetUp] |
| 50 | + public void Setup() |
| 51 | + { |
| 52 | + _svc = Build.A.SerializationService().WithEth65().TestObject; |
| 53 | + |
| 54 | + NetworkDiagTracer.IsEnabled = true; |
| 55 | + |
| 56 | + _session = Substitute.For<ISession>(); |
| 57 | + Node node = new Node(TestItem.PublicKeyA, new IPEndPoint(IPAddress.Broadcast, 30303)); |
| 58 | + _session.Node.Returns(node); |
| 59 | + _syncManager = Substitute.For<ISyncServer>(); |
| 60 | + _transactionPool = Substitute.For<ITxPool>(); |
| 61 | + _pooledTxsRequestor = Substitute.For<IPooledTxsRequestor>(); |
| 62 | + _specProvider = Substitute.For<ISpecProvider>(); |
| 63 | + _genesisBlock = Build.A.Block.Genesis.TestObject; |
| 64 | + _syncManager.Head.Returns(_genesisBlock.Header); |
| 65 | + _syncManager.Genesis.Returns(_genesisBlock.Header); |
| 66 | + ITimerFactory timerFactory = Substitute.For<ITimerFactory>(); |
| 67 | + _handler = new Eth65ProtocolHandler( |
| 68 | + _session, |
| 69 | + _svc, |
| 70 | + new NodeStatsManager(timerFactory, LimboLogs.Instance), |
| 71 | + _syncManager, |
| 72 | + _transactionPool, |
| 73 | + _pooledTxsRequestor, |
| 74 | + _specProvider, |
| 75 | + LimboLogs.Instance); |
| 76 | + _handler.Init(); |
| 77 | + } |
| 78 | + |
| 79 | + [TearDown] |
| 80 | + public void TearDown() |
| 81 | + { |
| 82 | + _handler.Dispose(); |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + public void Metadata_correct() |
| 87 | + { |
| 88 | + _handler.ProtocolCode.Should().Be("eth"); |
| 89 | + _handler.Name.Should().Be("eth65"); |
| 90 | + _handler.ProtocolVersion.Should().Be(65); |
| 91 | + _handler.MessageIdSpaceSize.Should().Be(17); |
| 92 | + _handler.IncludeInTxPool.Should().BeTrue(); |
| 93 | + _handler.ClientId.Should().Be(_session.Node?.ClientId); |
| 94 | + _handler.HeadHash.Should().BeNull(); |
| 95 | + _handler.HeadNumber.Should().Be(0); |
| 96 | + } |
| 97 | + |
| 98 | + [TestCase(1)] |
| 99 | + [TestCase(3199)] |
| 100 | + [TestCase(3200)] |
| 101 | + public void should_send_up_to_3200_hashes_in_one_NewPooledTransactionHashesMessage(int txCount) |
| 102 | + { |
| 103 | + Transaction[] txs = new Transaction[txCount]; |
| 104 | + |
| 105 | + for (int i = 0; i < txCount; i++) |
| 106 | + { |
| 107 | + txs[i] = Build.A.Transaction.SignedAndResolved().TestObject; |
| 108 | + } |
| 109 | + |
| 110 | + _handler.SendNewTransactions(txs); |
| 111 | + |
| 112 | + _session.Received(1).DeliverMessage(Arg.Is<NewPooledTransactionHashesMessage>(m => m.Hashes.Count == txCount)); |
| 113 | + } |
| 114 | + |
| 115 | + [TestCase(3201)] |
| 116 | + [TestCase(10000)] |
| 117 | + [TestCase(20000)] |
| 118 | + public void should_send_more_than_3200_hashes_in_more_than_one_NewPooledTransactionHashesMessage(int txCount) |
| 119 | + { |
| 120 | + const int maxHashesCount = 3200; |
| 121 | + int messagesCount = txCount / maxHashesCount + 1; |
| 122 | + int nonFullMsgTxsCount = txCount % maxHashesCount; |
| 123 | + Transaction[] txs = new Transaction[txCount]; |
| 124 | + |
| 125 | + for (int i = 0; i < txCount; i++) |
| 126 | + { |
| 127 | + txs[i] = Build.A.Transaction.SignedAndResolved().TestObject; |
| 128 | + } |
| 129 | + |
| 130 | + _handler.SendNewTransactions(txs); |
| 131 | + |
| 132 | + _session.Received(messagesCount).DeliverMessage(Arg.Is<NewPooledTransactionHashesMessage>(m => m.Hashes.Count == maxHashesCount || m.Hashes.Count == nonFullMsgTxsCount)); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments