Beispiel #1
0
        /// <summary>
        /// Initializes parts of the object that are common for both inbound and outbound peers.
        /// </summary>
        /// <param name="inbound"><c>true</c> for inbound peers, <c>false</c> for outbound peers.</param>
        /// <param name="peerEndPoint">IP address and port on the side of the peer.</param>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="parameters">Various settings and requirements related to how the connections with peers are going to be established, or <c>null</c> to use default parameters.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="onDisconnected">Callback that is invoked when peer has finished disconnecting, or <c>null</c> when no notification after the disconnection is required.</param>
        private NetworkPeer(bool inbound,
                            IPEndPoint peerEndPoint,
                            Network network,
                            NetworkPeerConnectionParameters parameters,
                            IDateTimeProvider dateTimeProvider,
                            ILoggerFactory loggerFactory,
                            ISelfEndpointTracker selfEndpointTracker,
                            Action <INetworkPeer> onDisconnected = null)
        {
            this.loggerFactory    = loggerFactory;
            this.dateTimeProvider = dateTimeProvider;

            this.preferredTransactionOptions = parameters.PreferredTransactionOptions;
            this.SupportedTransactionOptions = parameters.PreferredTransactionOptions & ~TransactionOptions.All;

            this.State                = inbound ? NetworkPeerState.Connected : NetworkPeerState.Created;
            this.Inbound              = inbound;
            this.PeerEndPoint         = peerEndPoint;
            this.RemoteSocketEndpoint = this.PeerEndPoint;
            this.RemoteSocketAddress  = this.RemoteSocketEndpoint.Address;
            this.RemoteSocketPort     = this.RemoteSocketEndpoint.Port;

            this.Network             = network;
            this.Behaviors           = new NetworkPeerBehaviorsCollection(this);
            this.selfEndpointTracker = selfEndpointTracker;

            this.onDisconnectedAsyncContext = new AsyncLocal <DisconnectedExecutionAsyncContext>();

            this.ConnectionParameters = parameters ?? new NetworkPeerConnectionParameters();
            this.MyVersion            = this.ConnectionParameters.CreateVersion(this.PeerEndPoint, network, this.dateTimeProvider.GetTimeOffset());

            this.MessageReceived = new AsyncExecutionEvent <INetworkPeer, IncomingMessage>();
            this.StateChanged    = new AsyncExecutionEvent <INetworkPeer, NetworkPeerState>();
            this.onDisconnected  = onDisconnected;
        }
Beispiel #2
0
        /// <summary>Constructor for dependency injection.</summary>
        protected PeerConnector(
            IAsyncProvider asyncProvider,
            IDateTimeProvider dateTimeProvider,
            ILoggerFactory loggerFactory,
            Network network,
            INetworkPeerFactory networkPeerFactory,
            INodeLifetime nodeLifetime,
            NodeSettings nodeSettings,
            ConnectionManagerSettings connectionSettings,
            IPeerAddressManager peerAddressManager,
            ISelfEndpointTracker selfEndpointTracker)
        {
            this.asyncProvider       = asyncProvider;
            this.ConnectorPeers      = new NetworkPeerCollection();
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.network             = network;
            this.networkPeerFactory  = networkPeerFactory;
            this.NodeLifetime        = nodeLifetime;
            this.ConnectionSettings  = connectionSettings;
            this.PeerAddressManager  = peerAddressManager;
            this.networkPeerDisposer = new NetworkPeerDisposer(this.loggerFactory, this.asyncProvider, this.OnPeerDisposed);
            this.selfEndpointTracker = selfEndpointTracker;
            this.Requirements        = new NetworkPeerRequirement {
                MinVersion = nodeSettings.MinProtocolVersion ?? nodeSettings.Network.Consensus.ConsensusFactory.Protocol.MinProtocolVersion
            };

            this.connectionInterval = this.CalculateConnectionInterval();
        }
        private IConnectionManager CreateConnectionManager(
            NodeSettings nodeSettings,
            ConnectionManagerSettings connectionSettings,
            IPeerAddressManager peerAddressManager,
            IPeerConnector peerConnector,
            ISelfEndpointTracker selfEndpointTracker)
        {
            var networkPeerFactory = new Mock <INetworkPeerFactory>();
            var peerDiscovery      = new Mock <IPeerDiscovery>();

            var networkPeerParameters = new NetworkPeerConnectionParameters();

            var connectionManager = new ConnectionManager(
                DateTimeProvider.Default,
                this.LoggerFactory.Object,
                this.Network,
                networkPeerFactory.Object,
                nodeSettings,
                this.nodeLifetime,
                networkPeerParameters,
                peerAddressManager,
                new IPeerConnector[] { peerConnector },
                peerDiscovery.Object,
                selfEndpointTracker,
                connectionSettings,
                new VersionProvider(),
                new Mock <INodeStats>().Object,
                this.asyncProvider);

            networkPeerParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(connectionManager, this.extendedLoggerFactory));

            return(connectionManager);
        }
        private IConnectionManager CreateConnectionManager(
            NodeSettings nodeSettings,
            ConnectionManagerSettings connectionSettings,
            IPeerAddressManager peerAddressManager,
            IPeerConnector peerConnector,
            ISelfEndpointTracker selfEndpointTracker)
        {
            var networkPeerFactory = new Mock <INetworkPeerFactory>();
            var peerDiscovery      = new Mock <IPeerDiscovery>();

            var connectionManager = new ConnectionManager(
                DateTimeProvider.Default,
                this.LoggerFactory.Object,
                this.Network,
                networkPeerFactory.Object,
                nodeSettings,
                this.nodeLifetime,
                this.networkPeerParameters,
                peerAddressManager,
                new IPeerConnector[] { peerConnector },
                peerDiscovery.Object,
                selfEndpointTracker,
                connectionSettings,
                new VersionProvider());

            return(connectionManager);
        }
Beispiel #5
0
        /// <summary>Constructor for dependency injection.</summary>
        protected PeerConnector(
            IAsyncLoopFactory asyncLoopFactory,
            IDateTimeProvider dateTimeProvider,
            ILoggerFactory loggerFactory,
            Network network,
            INetworkPeerFactory networkPeerFactory,
            INodeLifetime nodeLifetime,
            NodeSettings nodeSettings,
            ConnectionManagerSettings connectionSettings,
            IPeerAddressManager peerAddressManager,
            ISelfEndpointTracker selfEndpointTracker)
        {
            this.asyncLoopFactory    = asyncLoopFactory;
            this.ConnectorPeers      = new NetworkPeerCollection();
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.network             = network;
            this.networkPeerFactory  = networkPeerFactory;
            this.nodeLifetime        = nodeLifetime;
            this.ConnectionSettings  = connectionSettings;
            this.peerAddressManager  = peerAddressManager;
            this.networkPeerDisposer = new NetworkPeerDisposer(this.loggerFactory, this.OnPeerDisposed);
            this.selfEndpointTracker = selfEndpointTracker;
            this.Requirements        = new NetworkPeerRequirement {
                MinVersion = nodeSettings.ProtocolVersion
            };

            this.defaultConnectionInterval = TimeSpans.Second;
            this.burstConnectionInterval   = TimeSpan.Zero;
        }
        /// <summary>
        /// Initializes a new instance of the factory.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="payloadProvider">A provider of network payload messages.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="initialBlockDownloadState">Provider of IBD state.</param>
        /// <param name="connectionManagerSettings">Configuration related to incoming and outgoing connections.</param>
        public NetworkPeerFactory(Network network,
                                  IDateTimeProvider dateTimeProvider,
                                  ILoggerFactory loggerFactory,
                                  PayloadProvider payloadProvider,
                                  ISelfEndpointTracker selfEndpointTracker,
                                  IInitialBlockDownloadState initialBlockDownloadState,
                                  ConnectionManagerSettings connectionManagerSettings,
                                  IAsyncProvider asyncProvider,
                                  IPeerAddressManager peerAddressManager)
        {
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));

            this.network                   = network;
            this.dateTimeProvider          = dateTimeProvider;
            this.loggerFactory             = loggerFactory;
            this.payloadProvider           = payloadProvider;
            this.selfEndpointTracker       = selfEndpointTracker;
            this.lastClientId              = 0;
            this.initialBlockDownloadState = initialBlockDownloadState;
            this.connectionManagerSettings = connectionManagerSettings;
            this.asyncProvider             = Guard.NotNull(asyncProvider, nameof(asyncProvider));
            this.peerAddressManager        = peerAddressManager;
        }
 /// <summary>
 /// Constructor for the peer selector.
 /// </summary>
 /// <param name="dateTimeProvider">Provider of datetime.</param>
 /// <param name="loggerFactory">Logger factory.</param>
 /// <param name="peerAddresses">The collection of peer address as managed by the peer address manager.</param>
 /// <param name="selfEndpointTracker">Self endpoint tracker.</param>
 public PeerSelector(IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, ConcurrentDictionary <IPEndPoint, PeerAddress> peerAddresses, ISelfEndpointTracker selfEndpointTracker)
 {
     this.selfEndpointTracker = selfEndpointTracker;
     this.dateTimeProvider    = dateTimeProvider;
     this.loggerFactory       = loggerFactory;
     this.logger        = loggerFactory.CreateLogger(this.GetType().FullName);
     this.peerAddresses = peerAddresses;
     this.random        = new Random();
 }
Beispiel #8
0
 /// <summary>Constructor used by dependency injection.</summary>
 public PeerAddressManager(IDateTimeProvider dateTimeProvider, DataFolder peerFilePath,
                           ILoggerFactory loggerFactory, ISelfEndpointTracker selfEndpointTracker)
 {
     this.dateTimeProvider      = dateTimeProvider;
     this.logger                = loggerFactory.CreateLogger(GetType().FullName);
     this.peerInfoByPeerAddress = new ConcurrentDictionary <IPEndPoint, PeerAddress>();
     this.PeerFilePath          = peerFilePath;
     this.PeerSelector          = new PeerSelector(this.dateTimeProvider, loggerFactory, this.peerInfoByPeerAddress,
                                                   selfEndpointTracker);
     this.fileStorage = new FileStorage <List <PeerAddress> >(this.PeerFilePath.AddressManagerFilePath);
 }
Beispiel #9
0
        /// <summary>
        /// Initializes a new instance of the factory.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="payloadProvider">A provider of network payload messages.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        public NetworkPeerFactory(Network network, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, PayloadProvider payloadProvider, ISelfEndpointTracker selfEndpointTracker)
        {
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));

            this.network             = network;
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.payloadProvider     = payloadProvider;
            this.selfEndpointTracker = selfEndpointTracker;
            this.logger       = loggerFactory.CreateLogger(this.GetType().FullName);
            this.lastClientId = 0;
        }
Beispiel #10
0
        public NodeController(
            ChainIndexer chainIndexer,
            IChainState chainState,
            IConnectionManager connectionManager,
            IDateTimeProvider dateTimeProvider,
            IFullNode fullNode,
            ILoggerFactory loggerFactory,
            NodeSettings nodeSettings,
            Network network,
            IAsyncProvider asyncProvider,
            ISelfEndpointTracker selfEndpointTracker,
            IBlockStore blockStore = null,
            IGetUnspentTransaction getUnspentTransaction             = null,
            INetworkDifficulty networkDifficulty                     = null,
            IPooledGetUnspentTransaction pooledGetUnspentTransaction = null,
            IPooledTransaction pooledTransaction                     = null,
            IConsensusManager consensusManager  = null,
            IInitialBlockDownloadState ibdState = null)
        {
            Guard.NotNull(fullNode, nameof(fullNode));
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(chainIndexer, nameof(chainIndexer));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(nodeSettings, nameof(nodeSettings));
            Guard.NotNull(chainState, nameof(chainState));
            Guard.NotNull(connectionManager, nameof(connectionManager));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));
            Guard.NotNull(asyncProvider, nameof(asyncProvider));
            Guard.NotNull(selfEndpointTracker, nameof(selfEndpointTracker));
            Guard.NotNull(consensusManager, nameof(consensusManager));

            this.chainIndexer        = chainIndexer;
            this.chainState          = chainState;
            this.connectionManager   = connectionManager;
            this.dateTimeProvider    = dateTimeProvider;
            this.fullNode            = fullNode;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.network             = network;
            this.nodeSettings        = nodeSettings;
            this.asyncProvider       = asyncProvider;
            this.selfEndpointTracker = selfEndpointTracker;

            this.blockStore                  = blockStore;
            this.getUnspentTransaction       = getUnspentTransaction;
            this.networkDifficulty           = networkDifficulty;
            this.pooledGetUnspentTransaction = pooledGetUnspentTransaction;
            this.pooledTransaction           = pooledTransaction;
            this.consensusManager            = consensusManager;
            this.ibdState = ibdState;
        }
Beispiel #11
0
        public ConnectionManager(IDateTimeProvider dateTimeProvider,
                                 ILoggerFactory loggerFactory,
                                 Network network,
                                 INetworkPeerFactory networkPeerFactory,
                                 NodeSettings nodeSettings,
                                 INodeLifetime nodeLifetime,
                                 NetworkPeerConnectionParameters parameters,
                                 IPeerAddressManager peerAddressManager,
                                 IEnumerable <IPeerConnector> peerConnectors,
                                 IPeerDiscovery peerDiscovery,
                                 ISelfEndpointTracker selfEndpointTracker,
                                 ConnectionManagerSettings connectionSettings,
                                 IVersionProvider versionProvider,
                                 INodeStats nodeStats,
                                 IAsyncProvider asyncProvider)
        {
            this.connectedPeers      = new NetworkPeerCollection();
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger(GetType().FullName);
            this.Network             = network;
            this.NetworkPeerFactory  = networkPeerFactory;
            this.NodeSettings        = nodeSettings;
            this.nodeLifetime        = nodeLifetime;
            this.asyncProvider       = asyncProvider;
            this.peerAddressManager  = peerAddressManager;
            this.PeerConnectors      = peerConnectors;
            this.peerDiscovery       = peerDiscovery;
            this.ConnectionSettings  = connectionSettings;
            this.networkPeerDisposer = new NetworkPeerDisposer(this.loggerFactory, this.asyncProvider);
            this.Servers             = new List <NetworkPeerServer>();

            this.Parameters = parameters;
            this.Parameters.ConnectCancellation = this.nodeLifetime.ApplicationStopping;
            this.selfEndpointTracker            = selfEndpointTracker;
            this.versionProvider = versionProvider;
            this.ipRangeFilteringEndpointExclusions = new List <IPEndPoint>();
            this.connectedPeersQueue =
                asyncProvider.CreateAndRunAsyncDelegateDequeuer <INetworkPeer>(
                    $"{nameof(ConnectionManager)}-{nameof(this.connectedPeersQueue)}", OnPeerAdded);
            this.disconnectedPerfCounter = new PerformanceCounter();

            this.Parameters.UserAgent =
                $"{this.ConnectionSettings.Agent}:{versionProvider.GetVersion()} ({(int) this.NodeSettings.ProtocolVersion})";

            this.Parameters.Version = this.NodeSettings.ProtocolVersion;

            nodeStats.RegisterStats(AddComponentStats, StatsType.Component, GetType().Name, 1100);
        }
 /// <summary>Constructor for dependency injection.</summary>
 public PeerConnectorConnectNode(
     IAsyncLoopFactory asyncLoopFactory,
     IDateTimeProvider dateTimeProvider,
     ILoggerFactory loggerFactory,
     Network network,
     INetworkPeerFactory networkPeerFactory,
     INodeLifetime nodeLifetime,
     NodeSettings nodeSettings,
     ConnectionManagerSettings connectionSettings,
     IPeerAddressManager peerAddressManager,
     ISelfEndpointTracker selfEndpointTracker) :
     base(asyncLoopFactory, dateTimeProvider, loggerFactory, network, networkPeerFactory, nodeLifetime, nodeSettings, connectionSettings, peerAddressManager, selfEndpointTracker)
 {
     this.Requirements.RequiredServices = NetworkPeerServices.Nothing;
 }
 public PeerConnectorDiscovery(
     IAsyncProvider asyncProvider,
     IDateTimeProvider dateTimeProvider,
     ILoggerFactory loggerFactory,
     Network network,
     INetworkPeerFactory networkPeerFactory,
     INodeLifetime nodeLifetime,
     NodeSettings nodeSettings,
     ConnectionManagerSettings connectionSettings,
     IPeerAddressManager peerAddressManager,
     ISelfEndpointTracker selfEndpointTracker) :
     base(asyncProvider, dateTimeProvider, loggerFactory, network, networkPeerFactory, nodeLifetime, nodeSettings, connectionSettings, peerAddressManager, selfEndpointTracker)
 {
     this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
     this.Requirements.RequiredServices = NetworkPeerServices.Network;
 }
        public PeerConnectorAddNode(
            IAsyncProvider asyncProvider,
            IDateTimeProvider dateTimeProvider,
            ILoggerFactory loggerFactory,
            Network network,
            INetworkPeerFactory networkPeerFactory,
            INodeLifetime nodeLifetime,
            NodeSettings nodeSettings,
            ConnectionManagerSettings connectionSettings,
            IPeerAddressManager peerAddressManager,
            ISelfEndpointTracker selfEndpointTracker) :
            base(asyncProvider, dateTimeProvider, loggerFactory, network, networkPeerFactory, nodeLifetime, nodeSettings, connectionSettings, peerAddressManager, selfEndpointTracker)
        {
            this.logger = loggerFactory.CreateLogger("Impleum.Bitcoin.Fullnode");

            this.Requirements.RequiredServices = NetworkPeerServices.Nothing;
        }
Beispiel #15
0
        public ConnectionManager(IDateTimeProvider dateTimeProvider,
                                 ILoggerFactory loggerFactory,
                                 Network network,
                                 INetworkPeerFactory networkPeerFactory,
                                 NodeSettings nodeSettings,
                                 INodeLifetime nodeLifetime,
                                 NetworkPeerConnectionParameters parameters,
                                 IPeerAddressManager peerAddressManager,
                                 IEnumerable <IPeerConnector> peerConnectors,
                                 IPeerDiscovery peerDiscovery,
                                 ISelfEndpointTracker selfEndpointTracker,
                                 ConnectionManagerSettings connectionSettings,
                                 IVersionProvider versionProvider,
                                 INodeStats nodeStats)
        {
            this.connectedPeers      = new NetworkPeerCollection();
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger("Impleum.Bitcoin.FullNode");
            this.Network             = network;
            this.NetworkPeerFactory  = networkPeerFactory;
            this.NodeSettings        = nodeSettings;
            this.nodeLifetime        = nodeLifetime;
            this.peerAddressManager  = peerAddressManager;
            this.PeerConnectors      = peerConnectors;
            this.peerDiscovery       = peerDiscovery;
            this.ConnectionSettings  = connectionSettings;
            this.networkPeerDisposer = new NetworkPeerDisposer(this.loggerFactory);
            this.Servers             = new List <NetworkPeerServer>();

            this.Parameters = parameters;
            this.Parameters.ConnectCancellation = this.nodeLifetime.ApplicationStopping;
            this.selfEndpointTracker            = selfEndpointTracker;
            this.versionProvider         = versionProvider;
            this.connectedPeersQueue     = new AsyncQueue <INetworkPeer>(this.OnPeerAdded);
            this.disconnectedPerfCounter = new PerformanceCounter();

            this.Parameters.UserAgent = $"{this.ConnectionSettings.Agent}:{versionProvider.GetVersion()} ({(int)this.NodeSettings.ProtocolVersion})";

            this.Parameters.Version = this.NodeSettings.ProtocolVersion;

            nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component, 1100);
        }
Beispiel #16
0
        /// <summary>
        /// Initializes an instance of the object for inbound network peers with already established connection.
        /// </summary>
        /// <param name="peerEndPoint">IP address and port on the side of the peer.</param>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="parameters">Various settings and requirements related to how the connections with peers are going to be established, or <c>null</c> to use default parameters.</param>
        /// <param name="client">Already connected network client.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="networkPeerFactory">Factory for creating P2P network peers.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="onDisconnected">Callback that is invoked when peer has finished disconnecting, or <c>null</c> when no notification after the disconnection is required.</param>
        public NetworkPeer(IPEndPoint peerEndPoint,
                           Network network,
                           NetworkPeerConnectionParameters parameters,
                           TcpClient client,
                           IDateTimeProvider dateTimeProvider,
                           INetworkPeerFactory networkPeerFactory,
                           ILoggerFactory loggerFactory,
                           ISelfEndpointTracker selfEndpointTracker,
                           Action <INetworkPeer> onDisconnected = null)
            : this(true, peerEndPoint, network, parameters, dateTimeProvider, loggerFactory, selfEndpointTracker, onDisconnected)
        {
            this.Connection = networkPeerFactory.CreateNetworkPeerConnection(this, client, this.ProcessMessageAsync);

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName, $"[{this.Connection.Id}-{peerEndPoint}] ");

            this.logger.LogTrace("Connected to peer '{0}'.", this.PeerEndPoint);

            this.InitDefaultBehaviors(this.ConnectionParameters);
            this.Connection.StartReceiveMessages();
        }
Beispiel #17
0
        /// <summary>
        /// Initializes an instance of the object for outbound network peers.
        /// </summary>
        /// <param name="peerEndPoint">IP address and port on the side of the peer.</param>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="parameters">Various settings and requirements related to how the connections with peers are going to be established, or <c>null</c> to use default parameters.</param>
        /// <param name="networkPeerFactory">Factory for creating P2P network peers.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="onDisconnected">Callback that is invoked when peer has finished disconnecting, or <c>null</c> when no notification after the disconnection is required.</param>
        public NetworkPeer(IPEndPoint peerEndPoint,
                           Network network,
                           NetworkPeerConnectionParameters parameters,
                           INetworkPeerFactory networkPeerFactory,
                           IDateTimeProvider dateTimeProvider,
                           ILoggerFactory loggerFactory,
                           ISelfEndpointTracker selfEndpointTracker,
                           Action <INetworkPeer> onDisconnected = null)
            : this(false, peerEndPoint, network, parameters, dateTimeProvider, loggerFactory, selfEndpointTracker, onDisconnected)
        {
            var client = new TcpClient(AddressFamily.InterNetworkV6);

            client.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
            client.Client.ReceiveBufferSize = parameters.ReceiveBufferSize;
            client.Client.SendBufferSize    = parameters.SendBufferSize;

            this.Connection = networkPeerFactory.CreateNetworkPeerConnection(this, client, this.ProcessMessageAsync);

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName, $"[{this.Connection.Id}-{peerEndPoint}] ");
        }
        /// <summary>
        /// Initializes parts of the object that are common for both inbound and outbound peers.
        /// </summary>
        /// <param name="inbound"><c>true</c> for inbound peers, <c>false</c> for outbound peers.</param>
        /// <param name="peerEndPoint">IP address and port on the side of the peer.</param>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="parameters">Various settings and requirements related to how the connections with peers are going to be established, or <c>null</c> to use default parameters.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="onDisconnected">Callback that is invoked when peer has finished disconnecting, or <c>null</c> when no notification after the disconnection is required.</param>
        private NetworkPeer(bool inbound,
                            IPEndPoint peerEndPoint,
                            Network network,
                            NetworkPeerConnectionParameters parameters,
                            IDateTimeProvider dateTimeProvider,
                            ILoggerFactory loggerFactory,
                            ISelfEndpointTracker selfEndpointTracker,
                            IAsyncProvider asyncProvider,
                            Action <INetworkPeer> onDisconnected          = null,
                            Action <IPEndPoint, Payload> onSendingMessage = null)
        {
            this.dateTimeProvider = dateTimeProvider;

            this.preferredTransactionOptions = parameters.PreferredTransactionOptions;
            this.SupportedTransactionOptions = parameters.PreferredTransactionOptions & ~TransactionOptions.All;

            this.State                = inbound ? NetworkPeerState.Connected : NetworkPeerState.Created;
            this.Inbound              = inbound;
            this.PeerEndPoint         = peerEndPoint;
            this.RemoteSocketEndpoint = this.PeerEndPoint;
            this.RemoteSocketAddress  = this.RemoteSocketEndpoint.Address;
            this.RemoteSocketPort     = this.RemoteSocketEndpoint.Port;

            this.Network                    = network;
            this.Behaviors                  = new List <INetworkPeerBehavior>();
            this.selfEndpointTracker        = selfEndpointTracker;
            this.asyncProvider              = asyncProvider;
            this.onDisconnectedAsyncContext = new AsyncLocal <DisconnectedExecutionAsyncContext>();

            this.ConnectionParameters = parameters ?? new NetworkPeerConnectionParameters();
            this.MyVersion            = this.ConnectionParameters.CreateVersion(this.selfEndpointTracker.MyExternalAddress, this.PeerEndPoint, network, this.dateTimeProvider.GetTimeOffset());

            this.MessageReceived  = new AsyncExecutionEvent <INetworkPeer, IncomingMessage>();
            this.StateChanged     = new AsyncExecutionEvent <INetworkPeer, NetworkPeerState>();
            this.onDisconnected   = onDisconnected;
            this.onSendingMessage = onSendingMessage;

            string dequeuerName = $"{nameof(NetworkPeer)}-{nameof(this.asyncPayloadsQueue)}-{this.PeerEndPoint.ToString()}";

            this.asyncPayloadsQueue = asyncProvider.CreateAndRunAsyncDelegateDequeuer <Payload>(dequeuerName, this.SendMessageHandledAsync);
        }
        public ConnectionManager(IDateTimeProvider dateTimeProvider,
                                 ILoggerFactory loggerFactory,
                                 Network network,
                                 INetworkPeerFactory networkPeerFactory,
                                 NodeSettings nodeSettings,
                                 INodeLifetime nodeLifetime,
                                 NetworkPeerConnectionParameters parameters,
                                 IPeerAddressManager peerAddressManager,
                                 IEnumerable <IPeerConnector> peerConnectors,
                                 IPeerDiscovery peerDiscovery,
                                 ISelfEndpointTracker selfEndpointTracker,
                                 ConnectionManagerSettings connectionSettings,
                                 IVersionProvider versionProvider)
        {
            this.connectedPeers      = new NetworkPeerCollection();
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.Network             = network;
            this.NetworkPeerFactory  = networkPeerFactory;
            this.NodeSettings        = nodeSettings;
            this.nodeLifetime        = nodeLifetime;
            this.peerAddressManager  = peerAddressManager;
            this.PeerConnectors      = peerConnectors;
            this.peerDiscovery       = peerDiscovery;
            this.ConnectionSettings  = connectionSettings;
            this.networkPeerDisposer = new NetworkPeerDisposer(this.loggerFactory);
            this.Servers             = new List <NetworkPeerServer>();

            this.Parameters = parameters;
            this.Parameters.ConnectCancellation = this.nodeLifetime.ApplicationStopping;
            this.selfEndpointTracker            = selfEndpointTracker;
            this.versionProvider = versionProvider;

            this.Parameters.UserAgent = $"{this.NodeSettings.Agent}:{versionProvider.GetVersion()}";

            this.Parameters.Version = this.NodeSettings.ProtocolVersion;

            this.downloads = new Dictionary <INetworkPeer, PerformanceSnapshot>();
        }
Beispiel #20
0
        public NodeController(
            ChainIndexer chainIndexer,
            IChainState chainState,
            IConnectionManager connectionManager,
            IDateTimeProvider dateTimeProvider,
            IFullNode fullNode,
            ILoggerFactory loggerFactory,
            NodeSettings nodeSettings,
            Network network,
            IAsyncProvider asyncProvider,
            ISelfEndpointTracker selfEndpointTracker,
            IConsensusManager consensusManager,
            IBlockStore blockStore,
            IInitialBlockDownloadState initialBlockDownloadState,
            ISignals signals,
            IGetUnspentTransaction getUnspentTransaction             = null,
            INetworkDifficulty networkDifficulty                     = null,
            IPooledGetUnspentTransaction pooledGetUnspentTransaction = null,
            IPooledTransaction pooledTransaction                     = null)
        {
            this.asyncProvider       = asyncProvider;
            this.chainIndexer        = chainIndexer;
            this.chainState          = chainState;
            this.connectionManager   = connectionManager;
            this.dateTimeProvider    = dateTimeProvider;
            this.fullNode            = fullNode;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.network             = network;
            this.nodeSettings        = nodeSettings;
            this.selfEndpointTracker = selfEndpointTracker;
            this.signals             = signals;

            this.consensusManager            = consensusManager;
            this.blockStore                  = blockStore;
            this.getUnspentTransaction       = getUnspentTransaction;
            this.initialBlockDownloadState   = initialBlockDownloadState;
            this.networkDifficulty           = networkDifficulty;
            this.pooledGetUnspentTransaction = pooledGetUnspentTransaction;
            this.pooledTransaction           = pooledTransaction;
        }
Beispiel #21
0
        /// <summary>
        /// Initializes a new instance of the factory.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="payloadProvider">A provider of network payload messages.</param>
        /// <param name="selfEndpointTracker">Tracker for endpoints known to be self.</param>
        /// <param name="initialBlockDownloadState">Provider of IBD state.</param>
        /// <param name="connectionManagerSettings">Configuration related to incoming and outgoing connections.</param>
        public NetworkPeerFactory(Network network,
                                  IDateTimeProvider dateTimeProvider,
                                  ILoggerFactory loggerFactory,
                                  PayloadProvider payloadProvider,
                                  ISelfEndpointTracker selfEndpointTracker,
                                  IInitialBlockDownloadState initialBlockDownloadState,
                                  ConnectionManagerSettings connectionManagerSettings)
        {
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));

            this.network             = network;
            this.dateTimeProvider    = dateTimeProvider;
            this.loggerFactory       = loggerFactory;
            this.payloadProvider     = payloadProvider;
            this.selfEndpointTracker = selfEndpointTracker;
            this.logger       = loggerFactory.CreateLogger("Impleum.Bitcoin.FullNode");
            this.lastClientId = 0;
            this.initialBlockDownloadState = initialBlockDownloadState;
            this.connectionManagerSettings = connectionManagerSettings;
        }
Beispiel #22
0
        public TestContext()
        {
            this.Network = KnownNetworks.RegTest;

            this.chainIndexer     = new ChainIndexer(this.Network);
            this.dateTimeProvider = new DateTimeProvider();
            this.hashStore        = new InvalidBlockHashStore(this.dateTimeProvider);

            this.coinView        = new TestInMemoryCoinView(this.chainIndexer.Tip.HashBlock);
            this.HeaderValidator = new Mock <IHeaderValidator>();
            this.HeaderValidator.Setup(hv => hv.ValidateHeader(It.IsAny <ChainedHeader>())).Returns(new ValidationContext());

            this.nodeLifetime = new NodeLifetime();
            this.ibd          = new Mock <IInitialBlockDownloadState>();
            this.BlockPuller  = new Mock <IBlockPuller>();

            this.BlockPuller.Setup(b => b.Initialize(It.IsAny <BlockPuller.OnBlockDownloadedCallback>()))
            .Callback <BlockPuller.OnBlockDownloadedCallback>((d) => { this.blockPullerBlockDownloadCallback = d; });
            this.BlockStore  = new Mock <IBlockStore>();
            this.checkpoints = new Mock <ICheckpoints>();
            this.ChainState  = new Mock <IChainState>();

            string[] param = new string[] { };
            this.nodeSettings      = new NodeSettings(this.Network, args: param);
            this.ConsensusSettings = new ConsensusSettings(this.nodeSettings);

            this.loggerFactory = this.nodeSettings.LoggerFactory;

            this.nodeStats = new NodeStats(this.dateTimeProvider, this.loggerFactory);

            var connectionSettings = new ConnectionManagerSettings(this.nodeSettings);

            this.selfEndpointTracker       = new SelfEndpointTracker(this.loggerFactory, connectionSettings);
            this.Network.Consensus.Options = new ConsensusOptions();

            this.signals       = new Bitcoin.Signals.Signals(this.loggerFactory, null);
            this.asyncProvider = new AsyncProvider(this.loggerFactory, this.signals, this.nodeLifetime);

            // Dont check PoW of a header in this test.
            this.Network.Consensus.ConsensusRules.HeaderValidationRules.RemoveAll(x => x.GetType() == typeof(CheckDifficultyPowRule));

            this.ChainedHeaderTree = new ChainedHeaderTree(
                this.Network,
                this.loggerFactory,
                this.HeaderValidator.Object,
                this.checkpoints.Object,
                this.ChainState.Object,
                this.FinalizedBlockMock.Object,
                this.ConsensusSettings,
                this.hashStore);

            this.peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, this.nodeSettings.DataFolder, this.loggerFactory, this.selfEndpointTracker);

            this.networkPeerFactory = new NetworkPeerFactory(this.Network,
                                                             this.dateTimeProvider,
                                                             this.loggerFactory, new PayloadProvider().DiscoverPayloads(),
                                                             this.selfEndpointTracker,
                                                             this.ibd.Object,
                                                             new ConnectionManagerSettings(this.nodeSettings),
                                                             this.asyncProvider,
                                                             this.peerAddressManager);

            var peerDiscovery = new PeerDiscovery(this.asyncProvider, this.loggerFactory, this.Network, this.networkPeerFactory, this.nodeLifetime, this.nodeSettings, this.peerAddressManager);

            this.connectionManager = new ConnectionManager(this.dateTimeProvider, this.loggerFactory, this.Network, this.networkPeerFactory, this.nodeSettings,
                                                           this.nodeLifetime, new NetworkPeerConnectionParameters(), this.peerAddressManager, new IPeerConnector[] { },
                                                           peerDiscovery, this.selfEndpointTracker, connectionSettings, new VersionProvider(), this.nodeStats, this.asyncProvider);

            this.deployments = new NodeDeployments(this.Network, this.chainIndexer);

            this.consensusRules = new PowConsensusRuleEngine(this.Network, this.loggerFactory, this.dateTimeProvider, this.chainIndexer, this.deployments, this.ConsensusSettings,
                                                             this.checkpoints.Object, this.coinView, this.ChainState.Object, this.hashStore, this.nodeStats, this.asyncProvider, new ConsensusRulesContainer());

            this.consensusRules.SetupRulesEngineParent();

            var tree = new ChainedHeaderTree(this.Network, this.loggerFactory, this.HeaderValidator.Object, this.checkpoints.Object,
                                             this.ChainState.Object, this.FinalizedBlockMock.Object, this.ConsensusSettings, this.hashStore);

            this.PartialValidator = new Mock <IPartialValidator>();
            this.FullValidator    = new Mock <IFullValidator>();

            this.peerBanning = new PeerBanning(this.connectionManager, this.loggerFactory, this.dateTimeProvider, this.peerAddressManager);

            this.IntegrityValidator.Setup(i => i.VerifyBlockIntegrity(It.IsAny <ChainedHeader>(), It.IsAny <Block>()))
            .Returns(new ValidationContext());

            ConsensusManager consensusManager = new ConsensusManager(tree, this.Network, this.loggerFactory, this.ChainState.Object, this.IntegrityValidator.Object,
                                                                     this.PartialValidator.Object, this.FullValidator.Object, this.consensusRules,
                                                                     this.FinalizedBlockMock.Object, this.signals, this.peerBanning, this.ibd.Object, this.chainIndexer,
                                                                     this.BlockPuller.Object, this.BlockStore.Object, this.connectionManager, this.nodeStats, this.nodeLifetime, this.ConsensusSettings, this.dateTimeProvider);

            this.TestConsensusManager = new TestConsensusManager(consensusManager);
        }
Beispiel #23
0
 public SelfEndpointTrackerTests()
 {
     this.selfEndpointTracker = new SelfEndpointTracker();
 }