public void SyncChainFromPeerInfo(AboutInfo peerChainInfo) { try { var thisChainDiff = this.Chain.CalcCumulativeDifficulty(); var peerChainDiff = peerChainInfo.CumulativeDifficulty; if (peerChainDiff > thisChainDiff) { Console.WriteLine("@Chain sync started.Peer: {0}." + " Expected chain length = {1}, expected cummulative difficulty = {2}.", peerChainInfo.NodeUrl, peerChainInfo.BlocksCount, peerChainDiff); var blocks = JsonConvert.DeserializeObject <List <Block> >(WebRequester.Get(peerChainInfo.NodeUrl + "/api/Node/blocks")); var chainIncreased = this.Chain.ProcessLongerChain(blocks); if (chainIncreased) { this.NotifyPeersAboutNewBlock(); } } } catch (Exception ex) { Console.WriteLine("Error loading the chain: " + ex.Message); } }
static void Main(string[] args) { //const string exchangesUrl = "https://min-api.cryptocompare.com/data/all/exchanges"; //var requester = new WebRequester(); //var response = requester.Get(exchangesUrl) // .GetAwaiter() // .GetResult(); const string appHostUrl = "http://localhost:23520/"; var requester = new WebRequester("Fulano", "1234"); var tokenResponse = requester.GetToken(appHostUrl + "token") .GetAwaiter() .GetResult(); var testList = requester.Get <List <string> >(appHostUrl + "api/Test/") .GetAwaiter() .GetResult(); }
public decimal GetCryptoCoinActualPrice(Exchange exchange, CryptoCoin coin, string currencySymbol) { var url = BaseURL + "price"; var parameters = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("fsym", coin.Symbol), new KeyValuePair <string, string>("tsyms", currencySymbol), new KeyValuePair <string, string>("e", exchange.Name) }; var response = _requester.Get(url, parameters).GetAwaiter().GetResult(); var responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var dcRiesponse = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseString); if (dcRiesponse.ContainsKey(currencySymbol)) { return(Convert.ToDecimal(dcRiesponse[currencySymbol])); } else { return(0); } }
public void SyncPendingTransactionsFromPeerInfo(AboutInfo peerChainInfo) { try { if (peerChainInfo.PendingTransactions > 0) { Console.WriteLine( "Pending transactions sync started.Peer: {0}", peerChainInfo.NodeUrl); var transactions = JsonConvert.DeserializeObject <List <Transaction> >(WebRequester.Get( peerChainInfo.NodeUrl + "/api/Node/transactions/pending")); foreach (var tran in transactions) { var addedTran = this.Chain.AddNewTransaction(tran); if (addedTran.TransactionDataHash != null) { // Added a new pending tx --> broadcast it to all known peers this.BroadcastTransactionToAllPeers(addedTran); } } } } catch (Exception err) { Console.WriteLine("Error loading the pending transactions: " + err.Message); } }
public IActionResult ConnectoToPeer(Peer info) { var peerUrl = info.NodeUrl; if (string.IsNullOrEmpty(peerUrl)) { return(BadRequest("Missing 'peerUrl' in the request body")); } Console.WriteLine("Trying to connect to peer: " + peerUrl); try { var node = this.GetNodeSingleton(); var result = JsonConvert.DeserializeObject <AboutInfo>(WebRequester.Get(peerUrl + "/api/Node/about")); if (node.NodeId == result.NodeId) { return(BadRequest("Cannot connect to self")); } else if (node.Peers.ContainsKey(result.NodeId)) { return(BadRequest("Error: already connected to peer: " + peerUrl)); } else if (node.ChainId != result.ChainId) { return(BadRequest("Error: chain ID cannot be different")); } else { // Remove all peers with the same URL + add the new peer ????? //why - isn't this handled by the second check?? //foreach (var nodeId in node.Peers) // if (node.Peers[nodeId.Key] == peerUrl) // node.Peers.Remove(nodeId.Key); node.Peers[result.NodeId] = peerUrl; Console.WriteLine("Successfully connected to peer: " + peerUrl); if (!info.IsRecursive) { // Try to connect back the remote peer to self WebRequester.Post(peerUrl + "/api/Node/peers/connect", new Peer() { NodeUrl = node.SelfUrl, IsRecursive = true }); //THINK ABOUT THIS!!! (should be in or outside) // Synchronize the blockchain + pending transactions node.SyncChainFromPeerInfo(result); node.SyncPendingTransactionsFromPeerInfo(result); } return(Ok(new { message = "Connected to peer: " + peerUrl })); } } catch (Exception ex) { Console.WriteLine("Error: connecting to peer: {0} failed", peerUrl); return(BadRequest(string.Format("Cannot connect to peer: {0}, due to {1}", peerUrl, ex.Message))); } }