Пример #1
0
        public async Task BroadcastTx_FromStream_Test()
        {
            var received = new List <TransactionsReceivedEvent>();

            _eventBus.Subscribe <TransactionsReceivedEvent>(t =>
            {
                received.Add(t);
                return(Task.CompletedTask);
            });

            var      pubKey   = "SomePubKey";
            Metadata metadata = new Metadata {
                { GrpcConstants.PubkeyMetadataKey, pubKey }
            };
            var context = BuildServerCallContext(metadata);

            _peerPool.TryAddPeer(GrpcTestPeerHelpers.CreateBasicPeer("127.0.0.1:500", context.GetPublicKey()));
            var transactions = await _osTestHelper.GenerateTransferTransactions(3);

            var requestStream = new TestAsyncStreamReader <Transaction>(transactions.ToArray());

            var result = await _serverService.TransactionBroadcastStream(requestStream, context);

            result.ShouldBe(new VoidReply());

            received.Count.ShouldBe(3);
        }
Пример #2
0
        public async Task ClientStreamingServerHandler_Success_Test()
        {
            var authInterceptor = GetRequiredService <AuthInterceptor>();
            var requestStream   = new TestAsyncStreamReader <string>(new [] { "test1", "test2", "test3" });
            var continuation    = new ClientStreamingServerMethod <string, string>((s, y) => Task.FromResult(s.Current));
            var metadata        = new Metadata
            {
                { GrpcConstants.PubkeyMetadataKey, NetworkTestConstants.FakePubkey2 }
            };
            var context     = BuildServerCallContext(metadata);
            var headerCount = context.RequestHeaders.Count;

            await requestStream.MoveNext();

            var result = await authInterceptor.ClientStreamingServerHandler(requestStream, context, continuation);

            result.ShouldBe("test1");
            context.RequestHeaders.Count.ShouldBeGreaterThan(headerCount);

            await requestStream.MoveNext();

            result = await authInterceptor.ClientStreamingServerHandler(requestStream, context, continuation);

            result.ShouldBe("test2");

            await requestStream.MoveNext();

            result = await authInterceptor.ClientStreamingServerHandler(requestStream, context, continuation);

            result.ShouldBe("test3");
        }
Пример #3
0
        public async Task BroadcastBlockWithTxs_FromStream_Test()
        {
            var received = new List <BlockReceivedEvent>();

            _eventBus.Subscribe <BlockReceivedEvent>(a =>
            {
                received.Add(a);
                return(Task.CompletedTask);
            });

            var blocks = new List <BlockWithTransactions>();

            blocks.Add(_osTestHelper.GenerateBlockWithTransactions(Hash.FromString("block1"), 1, (await _osTestHelper.GenerateTransferTransactions(1)).ToList()));
            blocks.Add(_osTestHelper.GenerateBlockWithTransactions(Hash.FromString("block2"), 2, (await _osTestHelper.GenerateTransferTransactions(2)).ToList()));
            blocks.Add(_osTestHelper.GenerateBlockWithTransactions(Hash.FromString("block3"), 3, (await _osTestHelper.GenerateTransferTransactions(3)).ToList()));

            var context       = BuildServerCallContext();
            var requestStream = new TestAsyncStreamReader <BlockWithTransactions>(blocks.ToArray());

            context.RequestHeaders.Add(new Metadata.Entry(GrpcConstants.PubkeyMetadataKey, NetworkTestConstants.FakePubkey2));
            var result = await _serverService.BlockBroadcastStream(requestStream, context);

            result.ShouldBe(new VoidReply());
            received.Count.ShouldBe(3);
        }
Пример #4
0
        public async Task BroadcastBlockWithTxs_ShouldAddToBlockCache()
        {
            var peer     = _peerPool.GetPeers(true).First();
            var pubkey   = peer.Info.Pubkey;
            var metadata = new Metadata {
                { GrpcConstants.PubkeyMetadataKey, pubkey }
            };

            var block = _osTestHelper.GenerateBlockWithTransactions(Hash.FromString("block1"), 1,
                                                                    (await _osTestHelper.GenerateTransferTransactions(1)).ToList());

            var requestStream = new TestAsyncStreamReader <BlockWithTransactions>(new List <BlockWithTransactions> {
                block
            });
            await _serverService.BlockBroadcastStream(requestStream, BuildServerCallContext(metadata));

            peer.TryAddKnownBlock(block.GetHash()).ShouldBeFalse();
        }
Пример #5
0
        public async Task BroadcastTx_FromStream_Test()
        {
            var received = new List <TransactionsReceivedEvent>();

            _eventBus.Subscribe <TransactionsReceivedEvent>(t =>
            {
                received.Add(t);
                return(Task.CompletedTask);
            });
            var context      = BuildServerCallContext();
            var transactions = await _osTestHelper.GenerateTransferTransactions(3);

            var requestStream = new TestAsyncStreamReader <Transaction>(transactions.ToArray());

            var result = await _service.TransactionBroadcastStream(requestStream, context);

            result.ShouldBe(new VoidReply());

            received.Count.ShouldBe(3);
        }
Пример #6
0
        public async Task BroadcastAnnouncement_FromStream_Test()
        {
            var received = new List <AnnouncementReceivedEventData>();

            _eventBus.Subscribe <AnnouncementReceivedEventData>(a =>
            {
                received.Add(a);
                return(Task.CompletedTask);
            });

            var announcements = new List <BlockAnnouncement>();

            for (var i = 0; i < 5; i++)
            {
                announcements.Add(new BlockAnnouncement
                {
                    BlockHash   = Hash.FromString($"block-{i}"),
                    BlockHeight = 10 + i,
                    HasFork     = false
                });
            }

            var pubkey   = _peerPool.GetPeers(true).First().Info.Pubkey;
            var metadata = new Metadata {
                {
                    GrpcConstants.PubkeyMetadataKey, pubkey
                }
            };

            var context       = BuildServerCallContext(metadata);
            var requestStream = new TestAsyncStreamReader <BlockAnnouncement>(announcements.ToArray());


            var result = await _serverService.AnnouncementBroadcastStream(requestStream, context);

            result.ShouldBe(new VoidReply());
            received.Count.ShouldBe(5);
        }
Пример #7
0
        public async Task LibAnnouncementBroadcastStream_Test()
        {
            var chain = await _blockchainService.GetChainAsync();

            var irreversibleBlockHeight = (int)chain.LastIrreversibleBlockHeight;
            var bestBlockHeight         = (int)chain.BestChainHeight;
            var requestLibAnnouncements = new List <LibAnnouncement>();
            var preBlockHash            = chain.LastIrreversibleBlockHash;
            var preBlockHeight          = irreversibleBlockHeight;

            for (var i = irreversibleBlockHeight + 1; i <= bestBlockHeight; i++)
            {
                var libBlock = _osTestHelper.GenerateBlock(preBlockHash, preBlockHeight);
                requestLibAnnouncements.Add(new LibAnnouncement
                {
                    LibHeight = preBlockHeight,
                    LibHash   = preBlockHash
                });
                preBlockHash = libBlock.GetHash();
                preBlockHeight++;
            }

            var      requestStream = new TestAsyncStreamReader <LibAnnouncement>(requestLibAnnouncements);
            var      pubKey        = NetworkTestConstants.FakePubkey2;
            Metadata metadata      = new Metadata {
                { GrpcConstants.PubkeyMetadataKey, pubKey }
            };
            var context = BuildServerCallContext(metadata);
            var result  = await _serverService.LibAnnouncementBroadcastStream(requestStream, context);

            result.ShouldBe(new VoidReply());

            var peer      = _peerPool.FindPeerByPublicKey(pubKey);
            var lastBlock = requestLibAnnouncements.Last();

            peer.LastKnownLibHash.ShouldBe(lastBlock.LibHash);
            peer.LastKnownLibHeight.ShouldBe(lastBlock.LibHeight);
        }