Example #1
0
        public async Task <CoreNode> CreateNodeAsync(bool start = false)
        {
            var child = Path.Combine(Root, Last.ToString());

            Last++;
            try
            {
                var cfgPath = Path.Combine(child, "data", "bitcoin.conf");
                if (File.Exists(cfgPath))
                {
                    var config      = NodeConfigParameters.Load(cfgPath);
                    var rpcPort     = config["regtest.rpcport"];
                    var rpcUser     = config["regtest.rpcuser"];
                    var rpcPassword = config["regtest.rpcpassword"];
                    var pidFileName = config["regtest.pid"];
                    var credentials = new NetworkCredential(rpcUser, rpcPassword);

                    try
                    {
                        var rpc = new RPCClient(credentials, new Uri("http://127.0.0.1:" + rpcPort + "/"), Network.RegTest);
                        await rpc.StopAsync();

                        var pidFile = Path.Combine(child, "data", "regtest", pidFileName);
                        if (File.Exists(pidFile))
                        {
                            var pid = File.ReadAllText(pidFile);
                            using (var process = Process.GetProcessById(int.Parse(pid)))
                            {
                                process.WaitForExit();
                            }
                        }
                        else
                        {
                            var allProcesses      = Process.GetProcesses();
                            var bitcoindProcesses = allProcesses.Where(x => x.ProcessName.Contains("bitcoind"));
                            if (bitcoindProcesses.Count() == 1)
                            {
                                var bitcoind = bitcoindProcesses.First();
                                bitcoind.WaitForExit();
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                await IoHelpers.DeleteRecursivelyWithMagicDustAsync(child);
                await TryRemoveWorkingDirectoryAsync();

                Directory.CreateDirectory(WorkingDirectory);
            }
            catch (DirectoryNotFoundException)
            {
            }
            var node = await CoreNode.CreateAsync(child, this);

            Nodes.Add(node);
            if (start)
            {
                await node.StartAsync();
            }
            return(node);
        }