コード例 #1
0
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var peerPool    = context.ServiceProvider.GetRequiredService <IPeerPool>();
            var testContext = context.ServiceProvider.GetRequiredService <NetworkServicePropagationTestContext>();

            for (var i = 0; i < 3; i++)
            {
                var peer                   = new Mock <IPeer>();
                var knownBlockHashes       = new KnownHashContainer();
                var knownTransactionHashes = new KnownHashContainer();

                peer.Setup(p => p.Info).Returns(new PeerConnectionInfo {
                    Pubkey = "Pubkey" + i.ToString()
                });
                peer.Setup(p => p.IsReady).Returns(true);
                peer.Setup(p => p.TryAddKnownBlock(It.IsAny <Hash>()))
                .Returns <Hash>(blockHash => knownBlockHashes.TryAdd(blockHash));
                peer.Setup(p => p.KnowsBlock(It.IsAny <Hash>()))
                .Returns <Hash>(blockHash => knownBlockHashes.HasHash(blockHash));

                peer.Setup(p => p.TryAddKnownTransaction(It.IsAny <Hash>()))
                .Returns <Hash>(txHash => knownTransactionHashes.TryAdd(txHash));
                peer.Setup(p => p.KnowsTransaction(It.IsAny <Hash>()))
                .Returns <Hash>(txHash => knownTransactionHashes.HasHash(txHash));
                SetupBroadcastCallbacks(peer);

                peerPool.TryAddPeer(peer.Object);
                testContext.MockedPeers.Add(peer);
            }
        }
コード例 #2
0
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            NetworkServicePropagationTestContext testContext = new NetworkServicePropagationTestContext();

            List <IPeer> peers = null;

            var peerPoolMock           = new Mock <IPeerPool>();
            var knownBlockHashes       = new KnownHashContainer();
            var knownTransactionHashes = new KnownHashContainer();

            peerPoolMock.Setup(p => p.GetPeers(It.IsAny <bool>())).Returns <bool>(adr =>
            {
                if (peers != null)
                {
                    return(peers);
                }

                var propPeerOne = new Mock <IPeer>();

                propPeerOne.Setup(p => p.TryAddKnownBlock(It.IsAny <Hash>()))
                .Returns <Hash>(blockHash => knownBlockHashes.TryAdd(blockHash));
                propPeerOne.Setup(p => p.KnowsBlock(It.IsAny <Hash>()))
                .Returns <Hash>(blockHash => knownBlockHashes.HasHash(blockHash));

                propPeerOne.Setup(p => p.TryAddKnownTransaction(It.IsAny <Hash>()))
                .Returns <Hash>(txHash => knownTransactionHashes.TryAdd(txHash));
                propPeerOne.Setup(p => p.KnowsTransaction(It.IsAny <Hash>()))
                .Returns <Hash>(txHash => knownTransactionHashes.HasHash(txHash));

                SetupBroadcastCallbacks(propPeerOne);

                peers = new List <IPeer> {
                    propPeerOne.Object
                };

                testContext.MockedPeers.Add(propPeerOne);

                return(peers);
            });

            context.Services.AddSingleton <IPeerPool>(o => peerPoolMock.Object);
            context.Services.AddSingleton <NetworkServicePropagationTestContext>(o => testContext);
            context.Services.AddTransient(o => Mock.Of <IBroadcastPrivilegedPubkeyListProvider>());
        }