public bool ReadSettings() { try { string json_str = File.ReadAllText(json_file_name); JObject response = JObject.Parse(json_str); JArray coins = response.SelectToken("coins").Value <JArray>(); this.coins.Clear(); foreach (var item in coins) { var coin = new CoinSettings(); coin.name = item.SelectToken("name").Value <string>(); coin.url = item.SelectToken("url").Value <string>(); coin.diff_min = item.SelectToken("diff_min").Value <double>(); coin.diff_max = item.SelectToken("diff_max").Value <double>(); coin.diff_path = item.SelectToken("diff_path").Value <string>(); coin.diff_work_min = item.SelectToken("diff_work_min").Value <double>(); this.coins.Add(coin); } } catch (Exception ex) {} return(true); }
public CoinSettingsFixture() { Value = new CoinSettings { Name = "CoinLib", FeePerTransaction = 1L, GenesisBlock = new CurrencyBlock { Index = 0L, PreviousHash = new byte[] { 0 }, Timestamp = 1465154705L, Nonce = 0L, Objects = new BlockObject[] { }, Transactions = new[] { new Transaction { Id = "63ec3ac02f822450039df13ddf7c3c0f19bab4acd4dc928c62fcd78d5ebc6dba" } } }, Mining = new CoinSettings.MiningSettings { MiningReward = 5000000000L }, ProofOfWork = new CoinSettings.ProofOfWorkSettings { BaseDifficulty = 2147483647L, EveryXBlocks = 5, PowCurve = 5 }, }; }
public Miner(ICurrencyBlockchain blockchain, IProofOfWork proofOfWork, IOptions <CoinSettings> coinSettings, ILogger <Miner> logger) { _blockchain = blockchain; _proofOfWork = proofOfWork; _logger = logger; _coinSettings = coinSettings.Value; }
public KeyValuePair <double, double> GetCoinDifficultyByName(string coin_name) { // Select all url for this coin for (int i = 0; i < this.coins.Count; ++i) { CoinSettings coin = coins[i]; if (coin.name.ToLower() == coin_name.ToLower()) { HostTemplate?templ = templates.Find(x => x.name == coin.template); if (templ == null) { return(new KeyValuePair <double, double>(-1, 0)); } var diff = GetCoinDifficulty(ref coin, templ); this.coins[i] = coin; // save if (diff.Key >= 0) { return(diff); } } } return(new KeyValuePair <double, double>(-1, 0)); }
public KeyValuePair <double, double> GetCoinDifficulty(ref CoinSettings coin) { if (GetCoinDataIfNeeded(ref coin)) { try { int rate_max = 5, rate_min = 0; double diff = 0; if (coin.response != null) { string str = coin.response.SelectToken(coin.diff_path).ToString(); Double.TryParse(str.Replace(".", ","), out diff); if (coin.diff_work_min >= diff) { return(new KeyValuePair <double, double>(-1, 0)); } } else if (!Double.TryParse(coin.response_raw.Replace(".", ","), out diff)) { return(new KeyValuePair <double, double>(-1, 0)); } double rate = (rate_max - rate_min) / (coin.diff_max - coin.diff_min) * (diff - coin.diff_min); return(new KeyValuePair <double, double>(diff == -1.00 ? -1.01 : diff, rate)); } catch (Exception ex) { } } return(new KeyValuePair <double, double>(-1, 0)); }
public OperatorController(Operator @operator, Blockchain blockchain, IHashProvider hashProvider, IOptions <CoinSettings> coinSettings) { _operator = @operator; _blockchain = blockchain; _hashProvider = hashProvider; _coinSettings = coinSettings.Value; }
private void coinsListView_MouseDoubleClick(object sender, MouseEventArgs e) { #region //определение элемента листвью int curr_item = 0; try { Point mousePositioni = coinsListView.PointToClient(Control.MousePosition); ListViewHitTestInfo hiti = coinsListView.HitTest(mousePositioni); curr_item = hiti.Item.Index; } catch (Exception ex) { } //three step to detected which of columns of items to was clicked int columnindex = 0; try { Point mousePosition = coinsListView.PointToClient(Control.MousePosition); ListViewHitTestInfo hit = coinsListView.HitTest(mousePosition); columnindex = hit.Item.SubItems.IndexOf(hit.SubItem); } catch (Exception ex) { } if (coinsListView.Items.Count == 0) { return; } #endregion // Remove if (columnindex == 4) { plugin.coins.RemoveAt(curr_item); plugin.SaveSettings(); saved = true; reloadPoolsList(); return; } sel_coin = curr_item; CoinSettings coin = plugin.coins[curr_item]; coinTextBox.Text = coin.name.ToUpper(); diffMinTextBox.Text = coin.diff_min.ToString(); diffMaxTextBox.Text = coin.diff_max.ToString(); urlTextBox.Text = coin.url; minTextBox.Text = coin.diff_work_min.ToString(); templatesComboBox.SelectedIndex = 0; var templ_i = plugin.GetTemplateByName(coin.template); if (templ_i != null) { templatesComboBox.SelectedIndex = templ_i.Value.Key + 1; } modifyButton.Visible = sel_coin >= 0; checkButton.Visible = sel_coin >= 0; rateLabel.Text = ""; }
private bool GetCoinDataIfNeeded(ref CoinSettings coin) { if ((DateTime.Now - coin.res_date).TotalSeconds < 10) { return(true); } return(GetCoinData(ref coin)); }
public void Check(IHashProvider hashProvider, CoinSettings coinSettings) { // Check if the transaction hash is correct if (Hash != hashProvider.ComputeHashBytes(this)) { throw new TransactionAssertionException($"Invalid transaction hash '{Hash}'"); } Data.Check(coinSettings, hashProvider); }
public bool ReadSettings() { try { string json_str = File.ReadAllText(json_file_name); JObject response = JObject.Parse(json_str); JArray templates = response.SelectToken("templates").Value <JArray>(); this.templates.Clear(); foreach (var item in templates) { var templ = new HostTemplate(); templ.name = item.SelectToken("name").Value <string>(); JObject headers = item.SelectToken("req_headers").Value <JObject>(); templ.req_headers = new Dictionary <string, string>(); foreach (var item_h in headers) { templ.req_headers[item_h.Key] = item_h.Value.Value <string>(); } JObject cookies = item.SelectToken("req_cookies").Value <JObject>(); templ.req_cookies = new Dictionary <string, string>(); foreach (var item_c in cookies) { templ.req_cookies[item_c.Key] = item_c.Value.Value <string>(); } templ.diff_row_regex = item.SelectToken("diff_row_regex").Value <string>(); templ.diff_data_regex = item.SelectToken("diff_data_regex").Value <string>(); templ.diff_data_i = item.SelectToken("diff_data_i").Value <int>(); this.templates.Add(templ); } JArray coins = response.SelectToken("coins").Value <JArray>(); this.coins.Clear(); foreach (var item in coins) { var coin = new CoinSettings(); coin.name = item.SelectToken("name").Value <string>(); coin.url = item.SelectToken("url").Value <string>(); coin.template = item.SelectToken("template").Value <string>(); coin.diff_min = item.SelectToken("diff_min").Value <double>(); coin.diff_max = item.SelectToken("diff_max").Value <double>(); coin.diff_work_min = item.SelectToken("diff_work_min").Value <double>(); this.coins.Add(coin); } } catch (Exception ex) {} return(true); }
private CoinSettings coinFromFields() { CoinSettings coin = new CoinSettings(); coin.name = coinTextBox.Text.ToLower(); Double.TryParse(diffMinTextBox.Text, out coin.diff_min); Double.TryParse(diffMaxTextBox.Text, out coin.diff_max); coin.url = urlTextBox.Text; coin.diff_path = diffPathTextBox.Text; Double.TryParse(minTextBox.Text, out coin.diff_work_min); return(coin); }
public Blockchain(IOptions <CoinSettings> coinSettings, ICurrencyBlockRepository blocks, IProofOfWork proofOfWork, ITransactionRepository transactions, IHashProvider hashProvider, JsonSerializerSettings jsonSettings, ILogger <Blockchain> logger) { _coinSettings = coinSettings.Value; _blocks = blocks; _transactions = transactions; _hashProvider = hashProvider; _logger = logger; _jsonSettings = jsonSettings; _proofOfWork = proofOfWork; Init(); }
public Operator(Blockchain blockchain, IHashProvider hashProvider, IWalletProvider walletProvider, IOptions <CoinSettings> coinSettings, ILogger <Operator> logger) { _blockchain = blockchain; _hashProvider = hashProvider; _walletProvider = walletProvider; _coinSettings = coinSettings.Value; _logger = logger; }
public KeyValuePair <double, double> GetCoinDifficulty(ref CoinSettings coin, HostTemplate?templ) { if (GetCoinDataIfNeeded(ref coin)) { try { int rate_max = 5, rate_min = 0; double diff = 0; if (coin.response != null && templ != null) { // Ищем строки foreach (Match m in Regex.Matches(coin.response, templ.Value.diff_row_regex, RegexOptions.IgnoreCase)) { if (m.Success) { string row_html = m.Groups[1].Captures[0].ToString(); if (!row_html.ToLower().Contains(coin.name.ToLower())) { continue; } int idx_ = 0; foreach (Match m2 in Regex.Matches(row_html, templ.Value.diff_data_regex, RegexOptions.IgnoreCase)) { if (m2.Success && idx_ == templ.Value.diff_data_i) { string str = m2.Groups[1].Captures[0].ToString(); Double.TryParse(str.Replace(".", ","), out diff); if (coin.diff_work_min >= diff) { return(new KeyValuePair <double, double>(-1, 0)); } double rate = (rate_max - rate_min) / (coin.diff_max - coin.diff_min) * (diff - coin.diff_min); return(new KeyValuePair <double, double>(diff == -1.00 ? -1.01 : diff, rate)); } idx_ += 1; } } } } else { return(new KeyValuePair <double, double>(-1, 0)); } } catch (Exception ex) { } } return(new KeyValuePair <double, double>(-1, 0)); }
private void addButton_Click(object sender, EventArgs e) { if (coinTextBox.Text == "") { return; } CoinSettings coin = coinFromFields(); plugin.coins.Add(coin); sel_coin = -1; ClearForm(); reloadPoolsList(); }
private void modifyButton_Click(object sender, EventArgs e) { if (sel_coin < 0 || coinTextBox.Text == "") { return; } CoinSettings coin = coinFromFields(); plugin.coins[sel_coin] = coin; reloadPoolsList(); plugin.SaveSettings(); saved = true; }
private bool GetCoinData(ref CoinSettings coin) { coin.req_date = DateTime.Now; bool sts = false; bool?req_str = baseFunc.Json.Request(coin.url, out coin.response, out coin.response_raw, false, req_timeout); if (req_str == true || coin.response_raw.Length > 0) { coin.res_date = DateTime.Now; sts = true; } return(sts); }
public static bool?Request(CoinSettings coin, HostTemplate templ, out string response, bool escaped = false, int timeout = 10000) { try { string query = coin.url; if (!escaped) { query = Uri.EscapeUriString(coin.url); } Trace.WriteLine("Request: " + query); // Send request Uri url = new Uri(query); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); foreach (var item_h in templ.req_headers) { switch (item_h.Key) { case "User-Agent": req.UserAgent = item_h.Value; break; } } if (req.CookieContainer == null) { req.CookieContainer = new CookieContainer(); } foreach (var item_c in templ.req_cookies) { req.CookieContainer.Add(new Cookie(item_c.Key, item_c.Value, "/", url.Host)); } req.Timeout = timeout; WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); StreamReader sr = new StreamReader(stream); response = sr.ReadToEnd(); return(response != null && response.Length > 0); } catch (Exception ex) { Trace.WriteLine("Request error: " + coin.url + "\n" + ex.Message); response = null; return(null); } }
private bool GetCoinData(ref CoinSettings coin) { coin.req_date = DateTime.Now; bool sts = false; var templ_i = GetTemplateByName(coin.template); bool?req_str = Request(coin, templ_i.Value.Value, out coin.response, false, req_timeout); if (req_str == true) { coin.res_date = DateTime.Now; sts = true; } return(sts); }
private CoinSettings coinFromFields() { CoinSettings coin = new CoinSettings(); coin.name = coinTextBox.Text.ToLower(); Double.TryParse(diffMinTextBox.Text, out coin.diff_min); Double.TryParse(diffMaxTextBox.Text, out coin.diff_max); coin.url = urlTextBox.Text; Double.TryParse(minTextBox.Text, out coin.diff_work_min); coin.template = ""; if (templatesComboBox.SelectedIndex > 0) { coin.template = (string)templatesComboBox.Items[templatesComboBox.SelectedIndex]; } return(coin); }
public void Check(CoinSettings coinSettings, IHashProvider hashProvider) { // Check if the signature of all input transactions are correct (transaction data is signed by the public key of the address) var checks = Inputs.Select(input => { var hash = hashProvider.ComputeHashBytes(new { Transaction = input.TransactionId, input.Index, input.Address }); return(new { valid = Ed25519.VerifySignature(input.Address, input.Signature, hash), input }); }); foreach (var check in checks) { if (!check.valid) { throw new TransactionAssertionException($"Invalid transaction input signature at index '{check.input.Index}'"); } } // Check if the sum of input transactions are greater than output transactions, it needs to leave some room for the transaction fee var sumOfInputsAmount = Inputs.Sum(x => x.Amount); var sumOfOutputsAmount = Outputs.Sum(x => x.Amount); if (sumOfInputsAmount >= sumOfOutputsAmount) { throw new TransactionAssertionException($"Invalid transaction balance: inputs sum '{sumOfInputsAmount}', outputs sum '{sumOfOutputsAmount}'"); } // Check if there is enough to cover the transaction fee var isEnoughFee = sumOfInputsAmount - sumOfOutputsAmount >= coinSettings.FeePerTransaction; if (!isEnoughFee) { throw new TransactionAssertionException($"Not enough fee: expected '{coinSettings.FeePerTransaction}' got '{sumOfInputsAmount - sumOfOutputsAmount}'"); } }
public KeyValuePair <double, double> GetCoinDifficultyByName(string coin_name) { // Select all url for this coin for (int i = 0; i < this.coins.Count; ++i) { CoinSettings coin = coins[i]; if (coin.name.ToLower() == coin_name.ToLower()) { var diff = GetCoinDifficulty(ref coin); this.coins[i] = coin; // save if (diff.Key >= 0) { return(diff); } } } return(new KeyValuePair <double, double>(-1, 0)); }
private void checkButton_Click(object sender, EventArgs e) { if (sel_coin < 0) { return; } CoinSettings coin = plugin.coins[sel_coin]; var diff = plugin.GetCoinDifficulty(ref coin); if (diff.Key >= 0) { string txt = String.Format("Difficulty {0:0.000} [{1:0.00}]", diff.Key, diff.Value); rateLabel.Text = txt; } else { rateLabel.Text = "Something wrong"; } plugin.coins[sel_coin] = coin; }
public CoinBasedProofOfWork(IOptions <CoinSettings> coinSettings, IHashProvider hashProvider, ILogger <CoinBasedProofOfWork> logger = null) { _hashProvider = hashProvider; _logger = logger; _coinSettings = coinSettings.Value; }