コード例 #1
0
        private bool OnBroadcastCommand(string[] args)
        {
            string        command = args[1].ToLower();
            ISerializable payload = null;

            switch (command)
            {
            case "addr":
                payload = AddrPayload.Create(NetworkAddressWithTime.Create(new IPEndPoint(IPAddress.Parse(args[2]), ushort.Parse(args[3])), NetworkAddressWithTime.NODE_NETWORK, DateTime.UtcNow.ToTimestamp()));
                break;

            case "block":
                if (args[2].Length == 64 || args[2].Length == 66)
                {
                    payload = Blockchain.Default.GetBlock(UInt256.Parse(args[2]));
                }
                else
                {
                    payload = Blockchain.Default.GetBlock(uint.Parse(args[2]));
                }
                break;

            case "getblocks":
            case "getheaders":
                payload = GetBlocksPayload.Create(UInt256.Parse(args[2]));
                break;

            case "getdata":
            case "inv":
                payload = InvPayload.Create(Enum.Parse <InventoryType>(args[2], true), args.Skip(3).Select(p => UInt256.Parse(p)).ToArray());
                break;

            case "tx":
                payload = LocalNode.GetTransaction(UInt256.Parse(args[2]));
                if (payload == null)
                {
                    payload = Blockchain.Default.GetTransaction(UInt256.Parse(args[2]));
                }
                break;

            case "alert":
            case "consensus":
            case "filteradd":
            case "filterload":
            case "headers":
            case "merkleblock":
            case "ping":
            case "pong":
            case "reject":
            case "verack":
            case "version":
                Console.WriteLine($"Command \"{command}\" is not supported.");
                return(true);
            }
            foreach (RemoteNode node in LocalNode.GetRemoteNodes())
            {
                node.EnqueueMessage(command, payload);
            }
            return(true);
        }
コード例 #2
0
        private void OnBroadcastAddressCommand(IPAddress payload, ushort port)
        {
            if (payload == null)
            {
                Console.WriteLine("You must input the payload to relay.");
                return;
            }

            OnBroadcastCommand(MessageCommand.Addr,
                               AddrPayload.Create(
                                   NetworkAddressWithTime.Create(
                                       payload, DateTime.UtcNow.ToTimestamp(),
                                       new FullNodeCapability(),
                                       new ServerCapability(NodeCapabilityType.TcpServer, port))
                                   ));
        }
コード例 #3
0
        public void SizeAndEndPoint_Get()
        {
            var test = new NetworkAddressWithTime()
            {
                Capabilities = new NodeCapability[0], Address = IPAddress.Any, Timestamp = 1
            };

            test.Size.Should().Be(21);

            Assert.AreEqual(test.EndPoint.Port, 0);

            test = NetworkAddressWithTime.Create(IPAddress.Any, 1, new NodeCapability[] { new ServerCapability(NodeCapabilityType.TcpServer, 22) });
            test.Size.Should().Be(24);

            Assert.AreEqual(test.EndPoint.Port, 22);
        }
コード例 #4
0
        public void DeserializeAndSerialize()
        {
            var test  = NetworkAddressWithTime.Create(IPAddress.Any, 1, new NodeCapability[] { new ServerCapability(NodeCapabilityType.TcpServer, 22) });
            var clone = test.ToArray().AsSerializable <NetworkAddressWithTime>();

            CollectionAssert.AreEqual(test.Capabilities.ToByteArray(), clone.Capabilities.ToByteArray());
            Assert.AreEqual(test.EndPoint.ToString(), clone.EndPoint.ToString());
            Assert.AreEqual(test.Timestamp, clone.Timestamp);
            Assert.AreEqual(test.Address, clone.Address);

            Assert.ThrowsException <FormatException>(() => NetworkAddressWithTime.Create(IPAddress.Any, 1,
                                                                                         new NodeCapability[] {
                new ServerCapability(NodeCapabilityType.TcpServer, 22),
                new ServerCapability(NodeCapabilityType.TcpServer, 22)
            }).ToArray().AsSerializable <NetworkAddressWithTime>());
        }
コード例 #5
0
        private void OnGetAddrMessageReceived()
        {
            Random rand = new Random();
            IEnumerable <RemoteNode> peers = LocalNode.Singleton.RemoteNodes.Values
                                             .Where(p => p.ListenerPort > 0)
                                             .GroupBy(p => p.Remote.Address, (k, g) => g.First())
                                             .OrderBy(p => rand.Next())
                                             .Take(AddrPayload.MaxCountToSend);

            NetworkAddressWithTime[] networkAddresses = peers.Select(p => NetworkAddressWithTime.Create(p.Listener, p.Version.Services, p.Version.Timestamp)).ToArray();
            if (networkAddresses.Length == 0)
            {
                return;
            }
            Context.Parent.Tell(Message.Create("addr", AddrPayload.Create(networkAddresses)));
        }
コード例 #6
0
        private bool OnBroadcastCommand(string[] args)
        {
            if (!Enum.TryParse(args[1], true, out MessageCommand command))
            {
                Console.WriteLine($"Command \"{args[1]}\" is not supported.");
                return(true);
            }
            ISerializable payload = null;

            switch (command)
            {
            case MessageCommand.Addr:
                payload = AddrPayload.Create(NetworkAddressWithTime.Create(IPAddress.Parse(args[2]), DateTime.UtcNow.ToTimestamp(), new FullNodeCapability(), new ServerCapability(NodeCapabilityType.TcpServer, ushort.Parse(args[3]))));
                break;

            case MessageCommand.Block:
                if (args[2].Length == 64 || args[2].Length == 66)
                {
                    payload = Blockchain.Singleton.GetBlock(UInt256.Parse(args[2]));
                }
                else
                {
                    payload = Blockchain.Singleton.Store.GetBlock(uint.Parse(args[2]));
                }
                break;

            case MessageCommand.GetBlocks:
            case MessageCommand.GetHeaders:
                payload = GetBlocksPayload.Create(UInt256.Parse(args[2]));
                break;

            case MessageCommand.GetData:
            case MessageCommand.Inv:
                payload = InvPayload.Create(Enum.Parse <InventoryType>(args[2], true), args.Skip(3).Select(UInt256.Parse).ToArray());
                break;

            case MessageCommand.Transaction:
                payload = Blockchain.Singleton.GetTransaction(UInt256.Parse(args[2]));
                break;

            default:
                Console.WriteLine($"Command \"{command}\" is not supported.");
                return(true);
            }
            system.LocalNode.Tell(Message.Create(command, payload));
            return(true);
        }
コード例 #7
0
        private bool OnBroadcastCommand(string[] args)
        {
            string        command = args[1].ToLower();
            ISerializable payload = null;

            switch (command)
            {
            case "addr":
                payload = AddrPayload.Create(NetworkAddressWithTime.Create(new IPEndPoint(IPAddress.Parse(args[2]), ushort.Parse(args[3])), NetworkAddressWithTime.NODE_NETWORK, DateTime.UtcNow.ToTimestamp()));
                break;

            case "block":
                if (args[2].Length == 64 || args[2].Length == 66)
                {
                    payload = Blockchain.Singleton.GetBlock(UInt256.Parse(args[2]));
                }
                else
                {
                    payload = Blockchain.Singleton.Store.GetBlock(uint.Parse(args[2]));
                }
                break;

            case "getblocks":
            case "getheaders":
                payload = GetBlocksPayload.Create(UInt256.Parse(args[2]));
                break;

            case "getdata":
            case "inv":
                payload = InvPayload.Create(Enum.Parse <InventoryType>(args[2], true), args.Skip(3).Select(UInt256.Parse).ToArray());
                break;

            case "tx":
                payload = Blockchain.Singleton.GetTransaction(UInt256.Parse(args[2]));
                break;

            default:
                Console.WriteLine($"Command \"{command}\" is not supported.");
                return(true);
            }
            system.LocalNode.Tell(Message.Create(command, payload));
            return(true);
        }
コード例 #8
0
ファイル: RemoteNode.cs プロジェクト: ys8090/NEU
        private void OnGetAddrMessageReceived()
        {
            if (!localNode.ServiceEnabled)
            {
                return;
            }
            AddrPayload payload;

            lock (localNode.connectedPeers)
            {
                const int MaxCountToSend       = 200;
                IEnumerable <RemoteNode> peers = localNode.connectedPeers.Where(p => p.ListenerEndpoint != null && p.Version != null);
                if (localNode.connectedPeers.Count > MaxCountToSend)
                {
                    Random rand = new Random();
                    peers = peers.OrderBy(p => rand.Next());
                }
                peers   = peers.Take(MaxCountToSend);
                payload = AddrPayload.Create(peers.Select(p => NetworkAddressWithTime.Create(p.ListenerEndpoint, p.Version.Services, p.Version.Timestamp)).ToArray());
            }
            EnqueueMessage("addr", payload, true);
        }
コード例 #9
0
        private void OnGetAddrMessageReceived()
        {
            if (!localNode.ServiceEnabled)
            {
                return;
            }
            AddrPayload payload;

            lock (localNode.connectedPeers)
            {
                const int MaxCountToSend       = 200;
                IEnumerable <RemoteNode> peers = localNode.connectedPeers.Where(p => p.ListenerEndpoint != null && p.Version != null);
                if (localNode.connectedPeers.Count > MaxCountToSend)
                {
                    var rnd = new byte[4];
                    using (var rng = new RNGCryptoServiceProvider())
                        rng.GetBytes(rnd);
                    peers = peers.OrderBy(p => BitConverter.ToInt32(rnd, 0));
                }
                peers   = peers.Take(MaxCountToSend);
                payload = AddrPayload.Create(peers.Select(p => NetworkAddressWithTime.Create(p.ListenerEndpoint, p.Version.Services, p.Version.Timestamp)).ToArray());
            }
            EnqueueMessage("addr", payload, true);
        }
コード例 #10
0
        private async Task OnGetAddrMessageReceivedAsync()
        {
            if (!localNode.ServiceEnabled)
            {
                return;
            }
            AddrPayload payload;

            lock (localNode.connectedPeers)
            {
                payload = AddrPayload.Create(localNode.connectedPeers.Take(10).Select(p => NetworkAddressWithTime.Create(p.Value.RemoteEndpoint, p.Value.Version.Services, p.Value.Version.Timestamp)).ToArray());
            }
            await SendMessageAsync("addr", payload);
        }
コード例 #11
0
        private void OnGetAddrMessageReceived()
        {
            if (!localNode.ServiceEnabled)
            {
                return;
            }
            AddrPayload payload;

            lock (localNode.connectedPeers)
            {
                payload = AddrPayload.Create(localNode.connectedPeers.Where(p => p.ListenerEndpoint != null && p.Version != null).Take(100).Select(p => NetworkAddressWithTime.Create(p.ListenerEndpoint, p.Version.Services, p.Version.Timestamp)).ToArray());
            }
            EnqueueMessage("addr", payload, true);
        }
コード例 #12
0
        private bool OnBroadcastCommand(string[] args)
        {
            string     command    = args[1].ToLower();
            string     hashString = args[2];
            ZoroSystem zoroSystem = system.GetZoroSystem(hashString);
            Blockchain blockchain = system.GetBlockchain(hashString);

            if (zoroSystem == null || blockchain == null)
            {
                Console.WriteLine($"Unknown blockchain hash {hashString}.");
                return(true);
            }
            ISerializable payload = null;

            switch (command)
            {
            case "addr":
                payload = AddrPayload.Create(NetworkAddressWithTime.Create(new IPEndPoint(IPAddress.Parse(args[3]), ushort.Parse(args[4])), NetworkAddressWithTime.NODE_NETWORK, DateTime.UtcNow.ToTimestamp()));
                break;

            case "block":
                if (args[3].Length == 64 || args[3].Length == 66)
                {
                    payload = blockchain.GetBlock(UInt256.Parse(args[3]));
                }
                else
                {
                    payload = blockchain.Store.GetBlock(uint.Parse(args[3]));
                }
                break;

            case "getblocks":
            case "getheaders":
                payload = GetBlocksPayload.Create(UInt256.Parse(args[3]));
                break;

            case "getdata":
                payload = InvPayload.Create(Enum.Parse <InventoryType>(args[3], true), UInt256.Parse(args[4]));
                break;

            case "inv":
            case "getdatagroup":
                payload = InvPayload.Create(Enum.Parse <InventoryType>(args[3], true), args.Skip(4).Select(UInt256.Parse).ToArray());
                break;

            case "tx":
                payload = blockchain.GetTransaction(UInt256.Parse(args[3]));
                break;

            case "alert":
            case "consensus":
            case "filteradd":
            case "filterload":
            case "headers":
            case "merkleblock":
            case "ping":
            case "pong":
            case "reject":
            case "verack":
            case "version":
                Console.WriteLine($"Command \"{command}\" is not supported.");
                return(true);
            }
            zoroSystem.LocalNode.Tell(Message.Create(command, payload));
            return(true);
        }