public ListenerTester(ServerTester tester, bool nodeOnly)
        {
            _Network = tester.Configuration.Indexer.Network;
            Random rand = new Random();

            _Server = tester;
            _Server._disposables.Add(this);

            _Listener = new QBitNinjaNodeListener(_Server.Configuration);

            _NodeServer   = new NodeServer(_Server.Configuration.Indexer.Network, internalPort: rand.Next(20000, 50000));
            _NodeListener = new EventLoopMessageListener <IncomingMessage>(NewNodeMessage);
            _NodeServer.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _NodeServer.LocalEndpoint.Port);
            _NodeServer.AllowLocalPeers  = true;
            _NodeServer.InboundNodeConnectionParameters.IsRelay = true;
            _NodeServer.AllMessages.AddMessageListener(_NodeListener);
            _NodeServer.Listen();

            _Listener.Configuration.Indexer.Node = "127.0.0.1:" + _NodeServer.LocalEndpoint.Port;
            if (!nodeOnly)
            {
                _Listener.Listen();
            }

            _Server.Configuration.Indexer.CreateIndexer().Index(_Server.Configuration.Indexer.Network.GetGenesis());

            _Server.ChainBuilder.SkipIndexer     = true;
            _Server.ChainBuilder.NewBlock       += ChainBuilder_NewBlock;
            _Server.ChainBuilder.NewTransaction += ChainBuilder_NewTransaction;

            var genesis = _Server.Configuration.Indexer.Network.GetGenesis();

            _Blocks.AddOrReplace(genesis.Header.GetHash(), genesis);
        }
Exemple #2
0
            //		public TestNetwork Network { get; private set; }

            //public int SeedServerIndex {
            //	set {
            //		Network.AddSeed (new NetworkAddress(nodeServers [value].ExternalEndpoint));
            //	}
            //}

            public TesterNodeServerSet(int count)
            {
                //		Network = new TestNetwork ();

                for (int i = 0; i < count; i++)
                {
                    int port         = 3380 + i;
                    int internalPort = port;

                    NodeServer Server = new NodeServer(JsonLoader <Network> .Instance.Value, internalPort: internalPort);

                    Server.NodeAdded += (NodeServer sender, Node node) => {
                        LogMessageContext.Create("Node added to test server");
                    };

                    NodeConnectionParameters         nodeConnectionParameters = new NodeConnectionParameters();
                    NBitcoin.Protocol.AddressManager addressManager           = new NBitcoin.Protocol.AddressManager();

                    nodeConnectionParameters.TemplateBehaviors.Add(new AddressManagerBehavior(addressManager));
                    Server.InboundNodeConnectionParameters = nodeConnectionParameters;

                    Server.AllowLocalPeers  = true;
                    Server.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), port);
                    Server.Listen();

                    nodeServers.Add(Server);
                }
            }
        public TestServerSet(int count)
        {
            Network = new TestNetwork();

            for (int i = 0; i < count; i++)
            {
                int port         = 3380 + i;
                int internalPort = port;

                NodeServer Server = new NodeServer(Network, internalPort: internalPort);

                NodeConnectionParameters nodeConnectionParameters = new NodeConnectionParameters();
                AddressManager           addressManager           = new AddressManager();

                nodeConnectionParameters.TemplateBehaviors.Add(new AddressManagerBehavior(addressManager));
                Server.InboundNodeConnectionParameters = nodeConnectionParameters;


                Server.AllowLocalPeers  = true;
                Server.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), port);
                Server.Listen();

                Assert.AreEqual(true, Server.IsListening);

                nodeServers.Add(Server);
            }

            Trace.Information("\n\nServers are listening\n\n");
        }
Exemple #4
0
		public NodeServerTester()
		{
			int retry = 0;
			var network = Network.RegTest;
			Network = network;
			while(true)
			{
				try
				{
					var a = _Rand.Next(4000, 60000);
					var b = _Rand.Next(4000, 60000);
					_Server1 = new NodeServer(network, internalPort: a);
					_Server1.AllowLocalPeers = true;
					_Server1.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), a);
					_Server1.Listen();
					Assert.True(_Server1.IsListening);
					_Server2 = new NodeServer(network, internalPort: b);
					_Server2.AllowLocalPeers = true;
					_Server2.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), b);
					_Server2.Listen();
					Assert.True(_Server2.IsListening);
					break;
				}
				catch(Exception)
				{
					if(_Server1 != null)
						_Server1.Dispose();
					if(_Server2 != null)
						_Server2.Dispose();
					retry++;
					if(retry == 5)
						throw;
				}
			}
		}
 internal MiniNode CreateLocalNode()
 {
     NodeServer nodeServer = new NodeServer(Client.Configuration.Network, internalPort: (ushort)RandomUtils.GetInt32());
     nodeServer.Listen();
     _NodeServer = nodeServer;
     Indexer.Configuration.Node = "127.0.0.1:" + nodeServer.LocalEndpoint.Port;
     return new MiniNode(this, nodeServer);
 }
 public NodeServerTester()
 {
     _Server1 = new NodeServer(Network.Main, internalPort: 3390);
     _Server1.AllowLocalPeers  = true;
     _Server1.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6(), 3390);
     _Server1.NATRuleName      = NATRuleName;
     _Server1.Listen();
     _Server2 = new NodeServer(Network.Main, internalPort: 3391);
     _Server2.AllowLocalPeers  = true;
     _Server2.NATRuleName      = NATRuleName;
     _Server2.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6(), 3391);
     _Server2.Listen();
 }
Exemple #7
0
 public bool Start()
 {
     try
     {
         _Server.Listen();
         NodeServerTrace.Information("Server listening");
         return(true);
     }
     catch (Exception e)
     {
         NodeServerTrace.Error("Listen", e);
     }
     return(false);
 }
Exemple #8
0
        public NodeServerTester()
        {
            var a = _Rand.Next(4000, 60000);
            var b = _Rand.Next(4000, 60000);

            _Server1 = new NodeServer(Network.Main, internalPort: a);
            _Server1.AllowLocalPeers  = true;
            _Server1.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6(), a);
            _Server1.NATRuleName      = NATRuleName;
            _Server1.Listen();
            _Server2 = new NodeServer(Network.Main, internalPort: b);
            _Server2.AllowLocalPeers  = true;
            _Server2.NATRuleName      = NATRuleName;
            _Server2.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6(), b);
            _Server2.Listen();
        }
Exemple #9
0
        private void StartNodeServer()
        {
            var logs = new StringBuilder();

            logs.AppendLine("Node listening on:");

            foreach (NodeServerEndpoint listen in this.connectionManagerSettings.Listen)
            {
                NodeConnectionParameters cloneParameters = this.Parameters.Clone();
                var server = new NodeServer(this.Network)
                {
                    LocalEndpoint    = listen.Endpoint,
                    ExternalEndpoint = this.connectionManagerSettings.ExternalEndpoint
                };

                this.Servers.Add(server);
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(true, this, this.loggerFactory)
                {
                    Whitelisted = listen.Whitelisted
                });

                server.InboundNodeConnectionParameters = cloneParameters;
                try
                {
                    server.Listen();
                }
                catch (SocketException e)
                {
                    this.logger.LogCritical("Unable to listen on port {0} (you can change the port using '-port=[number]'). Error message: {1}", listen.Endpoint.Port, e.Message);
                    throw e;
                }

                logs.Append(listen.Endpoint.Address + ":" + listen.Endpoint.Port);
                if (listen.Whitelisted)
                {
                    logs.Append(" (whitelisted)");
                }

                logs.AppendLine();
            }

            this.logger.LogInformation(logs.ToString());
        }
Exemple #10
0
        public NodeServerTester(Network network = null)
        {
            int retry = 0;

            network = network ?? Network.TestNet;
            while (true)
            {
                try
                {
                    var a = _Rand.Next(4000, 60000);
                    var b = _Rand.Next(4000, 60000);
                    _Server1 = new NodeServer(network, internalPort: a);
                    _Server1.AllowLocalPeers  = true;
                    _Server1.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), a);
                    _Server1.Listen();
                    _Server2 = new NodeServer(network, internalPort: b);
                    _Server2.AllowLocalPeers  = true;
                    _Server2.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1").MapToIPv6Ex(), b);
                    _Server2.Listen();
                    break;
                }
                catch (Exception)
                {
                    if (_Server1 != null)
                    {
                        _Server1.Dispose();
                    }
                    if (_Server2 != null)
                    {
                        _Server2.Dispose();
                    }
                    retry++;
                    if (retry == 5)
                    {
                        throw;
                    }
                }
            }
        }
Exemple #11
0
        public ListenerTester(ServerTester tester)
        {
            _Network = tester.Configuration.Indexer.Network;
            Random rand = new Random();

            _Server = tester;
            _Server._disposables.Add(this);
            _Listener = new QBitNinjaListener(_Server.Configuration);

            _NodeServer   = new NodeServer(_Server.Configuration.Indexer.Network, internalPort: rand.Next(20000, 50000));
            _NodeListener = new EventLoopMessageListener <IncomingMessage>(NewNodeMessage);
            _NodeServer.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _NodeServer.LocalEndpoint.Port);
            _NodeServer.AllowLocalPeers  = true;
            _NodeServer.IsRelay          = true;
            _NodeServer.AllMessages.AddMessageListener(_NodeListener);
            _NodeServer.Listen();

            _Listener.Configuration.Indexer.Node = "127.0.0.1:" + _NodeServer.LocalEndpoint.Port;
            _Listener.Listen();

            _Server.ChainBuilder.SkipIndexer     = true;
            _Server.ChainBuilder.NewBlock       += ChainBuilder_NewBlock;
            _Server.ChainBuilder.NewTransaction += ChainBuilder_NewTransaction;
        }
        public void Start()
        {
            this.parameters.UserAgent = "StratisBitcoin:" + GetVersion();
            this.parameters.Version   = this.NodeSettings.ProtocolVersion;
            if (this.connectionManagerSettings.Connect.Count == 0)
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this));
                this.DiscoveredNodeGroup = CreateNodeGroup(cloneParameters, this.discoveredNodeRequiredService);
                this.DiscoveredNodeGroup.CustomGroupSelector = WellKnownGroupSelectors.ByNetwork;                 //is the default, but I want to use it
                this.DiscoveredNodeGroup.Connect();
            }
            else
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this));
                cloneParameters.TemplateBehaviors.Remove <AddressManagerBehavior>();
                var addrman = new AddressManager();
                addrman.Add(this.connectionManagerSettings.Connect.Select(c => new NetworkAddress(c)).ToArray(), IPAddress.Loopback);
                var addrmanBehavior = new AddressManagerBehavior(addrman);
                addrmanBehavior.Mode = AddressManagerBehaviorMode.None;
                cloneParameters.TemplateBehaviors.Add(addrmanBehavior);

                this.ConnectNodeGroup = CreateNodeGroup(cloneParameters, NodeServices.Nothing);
                this.ConnectNodeGroup.MaximumNodeConnection = this.connectionManagerSettings.Connect.Count;
                this.ConnectNodeGroup.CustomGroupSelector   = WellKnownGroupSelectors.ByEndpoint;
                this.ConnectNodeGroup.Connect();
            }

            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this));
                cloneParameters.TemplateBehaviors.Remove <AddressManagerBehavior>();
                var addrman = new AddressManager();
                addrman.Add(this.connectionManagerSettings.AddNode.Select(c => new NetworkAddress(c)).ToArray(), IPAddress.Loopback);
                var addrmanBehavior = new AddressManagerBehavior(addrman);
                addrmanBehavior.Mode = AddressManagerBehaviorMode.AdvertizeDiscover;
                cloneParameters.TemplateBehaviors.Add(addrmanBehavior);

                this.AddNodeNodeGroup = CreateNodeGroup(cloneParameters, NodeServices.Nothing);
                this.AddNodeNodeGroup.MaximumNodeConnection = this.connectionManagerSettings.AddNode.Count;
                this.AddNodeNodeGroup.CustomGroupSelector   = WellKnownGroupSelectors.ByEndpoint;
                this.AddNodeNodeGroup.Connect();
            }

            StringBuilder logs = new StringBuilder();

            logs.AppendLine("Node listening on:");
            foreach (NodeServerEndpoint listen in this.connectionManagerSettings.Listen)
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                var server = new NodeServer(this.Network);

                server.LocalEndpoint    = listen.Endpoint;
                server.ExternalEndpoint = this.connectionManagerSettings.ExternalEndpoint;
                this.Servers.Add(server);
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(true, this)
                {
                    Whitelisted = listen.Whitelisted
                });
                server.InboundNodeConnectionParameters = cloneParameters;
                server.Listen();
                logs.Append(listen.Endpoint.Address + ":" + listen.Endpoint.Port);
                if (listen.Whitelisted)
                {
                    logs.Append(" (whitelisted)");
                }
                logs.AppendLine();
            }
            Logs.ConnectionManager.LogInformation(logs.ToString());
        }
Exemple #13
0
        public void Start()
        {
            this.logger.LogTrace("()");

            this.parameters.UserAgent = $"{this.NodeSettings.Agent}:{this.GetVersion()}";
            this.parameters.Version   = this.NodeSettings.ProtocolVersion;

            if (this.connectionManagerSettings.Connect.Count == 0)
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this, this.loggerFactory));
                this.DiscoveredNodeGroup = CreateNodeGroup(cloneParameters, this.discoveredNodeRequiredService);
                this.DiscoveredNodeGroup.CustomGroupSelector = WellKnownGroupSelectors.ByNetwork; // It is the default, but I want to use it.
            }
            else
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this, this.loggerFactory));
                cloneParameters.TemplateBehaviors.Remove <AddressManagerBehavior>();

                var addrman = new AddressManager();
                addrman.Add(this.connectionManagerSettings.Connect.Select(c => new NetworkAddress(c)).ToArray(), IPAddress.Loopback);

                var addrmanBehavior = new AddressManagerBehavior(addrman)
                {
                    PeersToDiscover = 10
                };
                addrmanBehavior.Mode = AddressManagerBehaviorMode.None;
                cloneParameters.TemplateBehaviors.Add(addrmanBehavior);

                this.ConnectNodeGroup = CreateNodeGroup(cloneParameters, NodeServices.Nothing);
                this.ConnectNodeGroup.MaximumNodeConnection = this.connectionManagerSettings.Connect.Count;
                this.ConnectNodeGroup.CustomGroupSelector   = WellKnownGroupSelectors.ByEndpoint;
            }

            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this, this.loggerFactory));
                cloneParameters.TemplateBehaviors.Remove <AddressManagerBehavior>();
                var addrman = new AddressManager();
                addrman.Add(this.connectionManagerSettings.AddNode.Select(c => new NetworkAddress(c)).ToArray(), IPAddress.Loopback);
                var addrmanBehavior = new AddressManagerBehavior(addrman)
                {
                    PeersToDiscover = 10
                };
                addrmanBehavior.Mode = AddressManagerBehaviorMode.AdvertizeDiscover;
                cloneParameters.TemplateBehaviors.Add(addrmanBehavior);

                this.AddNodeNodeGroup = CreateNodeGroup(cloneParameters, NodeServices.Nothing);
                this.AddNodeNodeGroup.MaximumNodeConnection = this.connectionManagerSettings.AddNode.Count;
                this.AddNodeNodeGroup.CustomGroupSelector   = WellKnownGroupSelectors.ByEndpoint;
            }

            // Related the groups to each other to prevent duplicate connections.
            RelatedNodesGroups relGroups = new RelatedNodesGroups();

            relGroups.Register("Discovered", this.DiscoveredNodeGroup);
            relGroups.Register("Connect", this.ConnectNodeGroup);
            relGroups.Register("AddNode", this.AddNodeNodeGroup);
            this.DiscoveredNodeGroup?.Connect();
            this.ConnectNodeGroup?.Connect();
            this.AddNodeNodeGroup?.Connect();

            StringBuilder logs = new StringBuilder();

            logs.AppendLine("Node listening on:");
            foreach (NodeServerEndpoint listen in this.connectionManagerSettings.Listen)
            {
                NodeConnectionParameters cloneParameters = this.parameters.Clone();
                var server = new NodeServer(this.Network)
                {
                    LocalEndpoint    = listen.Endpoint,
                    ExternalEndpoint = this.connectionManagerSettings.ExternalEndpoint
                };

                this.Servers.Add(server);
                cloneParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(true, this, this.loggerFactory)
                {
                    Whitelisted = listen.Whitelisted
                });

                server.InboundNodeConnectionParameters = cloneParameters;
                server.Listen();

                logs.Append(listen.Endpoint.Address + ":" + listen.Endpoint.Port);
                if (listen.Whitelisted)
                {
                    logs.Append(" (whitelisted)");
                }
                logs.AppendLine();
            }
            this.logger.LogInformation(logs.ToString());

            this.logger.LogTrace("(-)");
        }