Beispiel #1
0
        static void Main(string[] args)
        {
            var node = new BitcoinNode();

            node.OnLog              += Node_OnLog;
            node.OnPeerConnected    += Node_OnPeerConnected;
            node.OnPeerDisconnected += Node_OnPeerDisconnected;

            node.Start();

            node.AddPeer(new IPEndPoint(IPAddress.Parse("94.23.59.197"), 8333));

            Console.ReadKey();
        }
Beispiel #2
0
        private static async Task RunNode()
        {
            try
            {
                BlockChain.Init();

                var cp_btc = new ChainParams();

                Node        = new BitcoinNode <BitcoinNodePeer>(cp_btc, new IPEndPoint(IPAddress.Any, 8336));
                Node.OnLog += Node_OnLog;

                Node.Start();

                //var ct = node.AddPeer(new IPEndPoint(IPAddress.Parse("192.168.254.6"), 8333));
                //var ct2 = node.AddPeer(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8333));

                //if no peers, add seeds
                if (BlockChain.Peers.Count == 0)
                {
                    Console.WriteLine("No peers found, adding seed nodes..");
                    foreach (var seed in DNSSeeds)
                    {
                        try
                        {
                            var ips = await Dns.GetHostAddressesAsync(seed);

                            foreach (var ip in ips)
                            {
                                var ep = new IPEndPoint(ip, 8333);
                                BlockChain.Peers.Add(ep.AsHash(), new PeerInfo()
                                {
                                    Ip          = ep,
                                    LastSeen    = DateTime.Now,
                                    LastVersion = new bitcoin_lib.P2P.Version()
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"No ips found for seed: {seed} ({ex.Message})");
                        }
                    }
                }

                //connect to last 8 peers
                await TryAddLastPeers();

                bool exit = false;
                Console.CancelKeyPress += (s, e) =>
                {
                    Console.WriteLine($"Shutting down..");
                    exit = true;

                    //doesnt work in .net core it seems..
                    //https://github.com/dotnet/coreclr/issues/8565
                };

                while (!exit)
                {
                    //ping the peers every 60s
                    if ((DateTime.Now - LastPingSent).TotalSeconds >= 60)
                    {
                        //disconnect a high ping node
                        BitcoinNodePeer hpn = Node.EnumeratePeers().Where(a => a.LastPing != TimeSpan.Zero).OrderByDescending(a => a.LastPing.TotalMilliseconds).FirstOrDefault();
                        if (hpn != default)
                        {
                            hpn.Disconnect();
                        }

                        var pt = new List <Task>();
                        foreach (var n in Node.EnumeratePeers())
                        {
                            pt.Add(n.StartPing());
                        }
                        await Task.WhenAll(pt);

                        LastPingSent = DateTime.Now;
                    }

                    //try to get 8 peers
                    if (Node.EnumeratePeers().Count() < 8)
                    {
                        //ask for more peers if we dont have enough
                        if (BlockChain.Peers.Count < 100)
                        {
                            Console.WriteLine($"Not enough known peers, asking for more..");
                            foreach (var n in Node.EnumeratePeers())
                            {
                                var addr = new GetAddr();
                                await n.WriteMessage(addr);
                            }

                            //wait 2s for nodes to reply
                            await Task.Delay(2000);
                        }

                        await TryAddLastPeers();
                    }

                    await Task.Delay(100);
                }

                await ShutdownNode();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }