<commit_before>﻿using NBitcoin;
using NBitcoin.Crypto;
using NBitcoin.DataEncoders;
using NBitcoin.Protocol;
using NBitcoin.RPC;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace NBitcoin.Tests
{
	public enum CoreNodeState
	{
		Stopped,
		Starting,
		Running,
		Killed
	}
	public class NodeConfigParameters : Dictionary<string, string>
	{
		public void Import(NodeConfigParameters configParameters, bool overrides)
		{
			foreach(var kv in configParameters)
			{
				if(!ContainsKey(kv.Key))
					Add(kv.Key, kv.Value);
				else if(overrides)
				{
					Remove(kv.Key);
					Add(kv.Key, kv.Value);
				}
			}
		}

		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();
			foreach(var kv in this)
				builder.AppendLine(kv.Key + "=" + kv.Value);
			return builder.ToString();
		}
	}

	public class NodeOSDownloadData
	{
		public string Archive
		{
			get; set;
		}
		public string DownloadLink
		{
			get; set;
		}
		public string Executable
		{
			get; set;
		}
		public string Hash
		{
			get; set;
		}
	}

	public partial class NodeDownloadData
	{
		public string Version
		{
			get; set;
		}

		public NodeOSDownloadData Linux
		{
			get; set;
		}

		public NodeOSDownloadData Mac
		{
			get; set;
		}

		public NodeOSDownloadData Windows
		{
			get; set;
		}

		public NodeOSDownloadData GetCurrentOSDownloadData()
		{
			return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows :
				   RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? Linux :
				   RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? Mac :
				   throw new NotSupportedException();
		}
	}
	public class NodeBuilder : IDisposable
	{
		public static NodeBuilder Create(NodeDownloadData downloadData, Network network = null, [CallerMemberNameAttribute]string caller = null)
		{
			network = network ?? Network.RegTest;
			var isFilePath = downloadData.Version.Length >= 2 && downloadData.Version[1] == ':';
			var path = isFilePath ? downloadData.Version : EnsureDownloaded(downloadData);
			if(!Directory.Exists(caller))
				Directory.CreateDirectory(caller);
			return new NodeBuilder(caller, path) { Network = network };
		}

		private static string EnsureDownloaded(NodeDownloadData downloadData)
		{
			if(!Directory.Exists("TestData"))
				Directory.CreateDirectory("TestData");

			var osDownloadData = downloadData.GetCurrentOSDownloadData();
			var bitcoind = Path.Combine("TestData", String.Format(osDownloadData.Executable, downloadData.Version));
			var zip = Path.Combine("TestData", String.Format(osDownloadData.Archive, downloadData.Version));
			if(File.Exists(bitcoind))
				return bitcoind;

			string url = String.Format(osDownloadData.DownloadLink, downloadData.Version);
			HttpClient client = new HttpClient();
			client.Timeout = TimeSpan.FromMinutes(10.0);
			var data = client.GetByteArrayAsync(url).GetAwaiter().GetResult();
			CheckHash(osDownloadData, data);
			File.WriteAllBytes(zip, data);

			if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
			{
				ZipFile.ExtractToDirectory(zip, new FileInfo(zip).Directory.FullName);
			}
			else
			{
				Process.Start("tar", "-zxvf " + zip + " -C TestData").WaitForExit();
			}
			File.Delete(zip);
			return bitcoind;
		}

		private static void CheckHash(NodeOSDownloadData osDownloadData, byte[] data)
		{
			var actual = Encoders.Hex.EncodeData(Hashes.SHA256(data));
			if(actual != osDownloadData.Hash)
				throw new Exception($"Hash of downloaded file does not match (Expected: {osDownloadData.Hash}, Actual: {actual})");
		}

		int last = 0;
		private string _Root;<commit_msg>Update hash comparison to do case insensitive comparison (#412)<commit_after>﻿using NBitcoin.Crypto;
using NBitcoin.DataEncoders;
using NBitcoin.Protocol;
using NBitcoin.RPC;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace NBitcoin.Tests
{
	public enum CoreNodeState
	{
		Stopped,
		Starting,
		Running,
		Killed
	}
	public class NodeConfigParameters : Dictionary<string, string>
	{
		public void Import(NodeConfigParameters configParameters, bool overrides)
		{
			foreach(var kv in configParameters)
			{
				if(!ContainsKey(kv.Key))
					Add(kv.Key, kv.Value);
				else if(overrides)
				{
					Remove(kv.Key);
					Add(kv.Key, kv.Value);
				}
			}
		}

		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();
			foreach(var kv in this)
				builder.AppendLine(kv.Key + "=" + kv.Value);
			return builder.ToString();
		}
	}

	public class NodeOSDownloadData
	{
		public string Archive
		{
			get; set;
		}
		public string DownloadLink
		{
			get; set;
		}
		public string Executable
		{
			get; set;
		}
		public string Hash
		{
			get; set;
		}
	}

	public partial class NodeDownloadData
	{
		public string Version
		{
			get; set;
		}

		public NodeOSDownloadData Linux
		{
			get; set;
		}

		public NodeOSDownloadData Mac
		{
			get; set;
		}

		public NodeOSDownloadData Windows
		{
			get; set;
		}

		public NodeOSDownloadData GetCurrentOSDownloadData()
		{
			return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Windows :
				   RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? Linux :
				   RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? Mac :
				   throw new NotSupportedException();
		}
	}
	public class NodeBuilder : IDisposable
	{
		public static NodeBuilder Create(NodeDownloadData downloadData, Network network = null, [CallerMemberNameAttribute]string caller = null)
		{
			network = network ?? Network.RegTest;
			var isFilePath = downloadData.Version.Length >= 2 && downloadData.Version[1] == ':';
			var path = isFilePath ? downloadData.Version : EnsureDownloaded(downloadData);
			if(!Directory.Exists(caller))
				Directory.CreateDirectory(caller);
			return new NodeBuilder(caller, path) { Network = network };
		}

		private static string EnsureDownloaded(NodeDownloadData downloadData)
		{
			if(!Directory.Exists("TestData"))
				Directory.CreateDirectory("TestData");

			var osDownloadData = downloadData.GetCurrentOSDownloadData();
			var bitcoind = Path.Combine("TestData", String.Format(osDownloadData.Executable, downloadData.Version));
			var zip = Path.Combine("TestData", String.Format(osDownloadData.Archive, downloadData.Version));
			if(File.Exists(bitcoind))
				return bitcoind;

			string url = String.Format(osDownloadData.DownloadLink, downloadData.Version);
			HttpClient client = new HttpClient();
			client.Timeout = TimeSpan.FromMinutes(10.0);
			var data = client.GetByteArrayAsync(url).GetAwaiter().GetResult();
			CheckHash(osDownloadData, data);
			File.WriteAllBytes(zip, data);

			if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
			{
				ZipFile.ExtractToDirectory(zip, new FileInfo(zip).Directory.FullName);
			}
			else
			{
				Process.Start("tar", "-zxvf " + zip + " -C TestData").WaitForExit();
			}
			File.Delete(zip);
			return bitcoind;
		}

		private static void CheckHash(NodeOSDownloadData osDownloadData, byte[] data)
		{
			var actual = Encoders.Hex.EncodeData(Hashes.SHA256(data));
			if(!actual.Equals(osDownloadData.Hash, StringComparison.OrdinalIgnoreCase))
				throw new Exception($"Hash of downloaded file does not match (Expected: {osDownloadData.Hash}, Actual: {actual})");
		}

		int last = 0;
		private string _Root;
{'fraction_non_alphanumeric': 0.10624370594159113, 'fraction_numerical': 0.0026435045317220545, 'mean_word_length': 4.145725388601036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 68, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2748159', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2428, 'n_words': 541}