public async Task BroadcastBlock_ThrowNetworkException_Test() { _testContext.MockedPeers[0] .Setup(p => p.EnqueueBlock(It.IsAny <BlockWithTransactions>(), It.IsAny <Action <NetworkException> >())) .Callback <BlockWithTransactions, Action <NetworkException> >((block, action) => action.Invoke(new NetworkException())); _testContext.MockedPeers[1] .Setup(p => p.EnqueueBlock(It.IsAny <BlockWithTransactions>(), It.IsAny <Action <NetworkException> >())) .Callback <BlockWithTransactions, Action <NetworkException> >((block, action) => action.Invoke(new NetworkException("Unrecoverable", NetworkExceptionType.Unrecoverable))); _testContext.MockedPeers[2] .Setup(p => p.EnqueueBlock(It.IsAny <BlockWithTransactions>(), It.IsAny <Action <NetworkException> >())) .Callback <BlockWithTransactions, Action <NetworkException> >((block, action) => action.Invoke(new NetworkException("PeerUnstable", NetworkExceptionType.PeerUnstable))); var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(chainId: 1, height: 2); var blockWithTx = new BlockWithTransactions { Header = blockHeader }; await _networkService.BroadcastBlockWithTransactionsAsync(blockWithTx); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.EnqueueBlock(blockWithTx, It.IsAny <Action <NetworkException> >()), Times.Once()); peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Once()); } _testContext.MockAElfNetworkServer.Verify(s => s.TrySchedulePeerReconnectionAsync(It.IsAny <IPeer>()), Times.Once()); }
public async Task BroadcastBlock_OnePeerKnowsBlock_Test() { var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(1, 2); var blockWithTransactions = new BlockWithTransactions { Header = blockHeader }; _peerPool.FindPeerByPublicKey("Pubkey0").TryAddKnownBlock(blockWithTransactions.GetHash()); await _networkService.BroadcastBlockWithTransactionsAsync(blockWithTransactions); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Once()); } _testContext.MockedPeers[0].Verify( p => p.EnqueueBlock(blockWithTransactions, It.IsAny <Action <NetworkException> >()), Times.Never); _testContext.MockedPeers[1].Verify( p => p.EnqueueBlock(blockWithTransactions, It.IsAny <Action <NetworkException> >()), Times.Once()); _testContext.MockedPeers[2].Verify( p => p.EnqueueBlock(blockWithTransactions, It.IsAny <Action <NetworkException> >()), Times.Once()); }
public async Task BroadcastAnnouncement_Test() { var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(chainId: 1, height: 2); await _networkService.BroadcastAnnounceAsync(blockHeader); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.EnqueueAnnouncement(It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Once()); } foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Once()); } await _networkService.BroadcastAnnounceAsync(blockHeader); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.EnqueueAnnouncement(It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Once()); } }
public async Task BroadcastBlock_Test() { var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(chainId: 1, height: 2); var blockWithTx = new BlockWithTransactions { Header = blockHeader }; await _networkService.BroadcastBlockWithTransactionsAsync(blockWithTx); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Once()); } await _networkService.BroadcastBlockWithTransactionsAsync(blockWithTx); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.EnqueueBlock(blockWithTx, It.IsAny <Action <NetworkException> >()), Times.Once()); } }
public async Task BroadcastAnnouncement_OldBlock_Test() { var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(chainId: 1, height: 2); blockHeader.Time = TimestampHelper.GetUtcNow() - TimestampHelper.DurationFromMinutes( NetworkConstants.DefaultMaxBlockAgeToBroadcastInMinutes + 1); await _networkService.BroadcastAnnounceAsync(blockHeader); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Never); } foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.EnqueueAnnouncement(It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Never); } }
public async Task BroadcastAnnouncement_OnePeerKnowsBlock_Test() { var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(1, 2); _peerPool.FindPeerByPublicKey("Pubkey0").TryAddKnownBlock(blockHeader.GetHash()); await _networkService.BroadcastAnnounceAsync(blockHeader); foreach (var peer in _testContext.MockedPeers) { peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Once()); } _testContext.MockedPeers[0].Verify(p => p.EnqueueAnnouncement( It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Never); _testContext.MockedPeers[1].Verify(p => p.EnqueueAnnouncement( It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Once()); _testContext.MockedPeers[2].Verify(p => p.EnqueueAnnouncement( It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()), It.IsAny <Action <NetworkException> >()), Times.Once()); }