コード例 #1
0
        public void NotifyPeersAboutNewBlock()
        {
            var notification = new
            {
                blocksCount          = this.Chain.Blocks.Count,
                cumulativeDifficulty = this.Chain.CalcCumulativeDifficulty(),
                nodeUrl = this.SelfUrl
            };

            foreach (var nodeId in this.Peers)
            {
                var peerUrl = this.Peers[nodeId.Key];
                Console.WriteLine("Notifying peer {0} about the new block", peerUrl);

                try
                {
                    var result = WebRequester
                                 .Post(peerUrl + "/api/Node/peers/notify-new-block", notification);
                }
                catch (Exception ex)
                {
                    Console
                    .WriteLine("Notifying peer {0} failed, due to {1}", peerUrl, ex.Message);
                }
            }
        }
コード例 #2
0
 private static void ConnectPeers(string urlFrom, string urlTo)
 {
     try
     {
         Console.WriteLine("Connecting {0} to {1} and vise versa", urlFrom, urlTo);
         //prevent recursive callings
         WebRequester.Post(urlFrom + "/api/Node/peers/connect", new Peer()
         {
             NodeUrl = urlTo, IsRecursive = false
         });
         //WebRequester.Post(urlFrom + "/api/Node/peers/connect", new Peer() { NodeUrl = urlTo ,IsRecursive=true});
         //Thread.Sleep(2000);
         //WebRequester.Post(urlTo + "/api/Node/peers/connect", new Peer() { NodeUrl = urlFrom, IsRecursive = true });
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
コード例 #3
0
        }                                    // the unique chain ID (hash of the genesis block)


        public void BroadcastTransactionToAllPeers(Transaction tran)
        {
            foreach (var nodeId in this.Peers)
            {
                var peerUrl = this.Peers[nodeId.Key];

                Console.WriteLine("Broadcasting a transaction {0} to peer {1}"
                                  , CryptoUtils.BytesToHex(tran.TransactionDataHash), peerUrl);

                try
                {
                    var result = WebRequester
                                 .Post(peerUrl + "/api/Node/transactions/send", tran);
                }
                catch (Exception ex)
                {
                    Console
                    .WriteLine("Broadcasting a transaction to {0} failed, due to {1}"
                               , peerUrl, ex.Message);
                }
            }
        }
コード例 #4
0
        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)));
            }
        }
コード例 #5
0
        public IActionResult RequestNoCoins(FaucetRequestViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index", model));
            }

            bool   isValid = true;
            string msg     = "You successfully requested 500000 NoCoins";

            var daylyReuestCountKey = model.Address + "->" + DateTime.UtcNow.ToShortDateString();

            if (appData.FaucetDaylyRequestsCount.ContainsKey(daylyReuestCountKey))
            {
                var dailyAddressReq = appData.FaucetDaylyRequestsCount[daylyReuestCountKey];
                if (dailyAddressReq >= Config.FaucetDailyAddressRequestCountMax)
                {
                    isValid = false;
                    msg     = string.Format("You are not able to recieve more than {0} NoCoins requests per day, per address!"
                                            , Config.FaucetDailyAddressRequestCountMax);
                }
            }
            else
            {
                appData.FaucetDaylyRequestsCount[daylyReuestCountKey] = 0;
            }


            if (isValid && appData.FaucetRequests.ContainsKey(model.Address))
            {
                DateTime lastRequestTime = appData.FaucetRequests[model.Address];
                if (lastRequestTime.AddMinutes(Config.FaucetWaitMinutes) > DateTime.Now)
                {
                    isValid = false;
                    msg     = "You should wait a bit more to request more NoCoins!";
                }
            }



            if (isValid)
            {
                appData.FaucetDaylyRequestsCount[daylyReuestCountKey]++;
                appData.FaucetRequests[model.Address] = DateTime.Now;

                var faucetTran = new Transaction(
                    Faucet.FaucetAddress,                         // from address
                    model.Address,                                // to address
                    500000,                                       // value of transfer
                    Config.MinTransactionFee,                     // fee
                    GeneralUtils.NowInISO8601(),                  // dateCreated
                    model.Message ?? "Faucet ->" + model.Address, // data (payload / comments)
                    Faucet.FaucetPublicKey                        // senderPubKey
                    );

                faucetTran.SetSignature(Faucet.FaucetPrivateKey);

                //posting the Faucet tran to other controller, where the tran is added and broadcasted
                WebRequester.Post(Helper.GetSelfUrl(Request) + "/api/Node/transactions/send", faucetTran);

                // this.GetNodeSingleton().Chain.AddNewTransaction(faucetTran);
                //TODO:think about if I should mine the tran or not??

                //this.GetNodeSingleton().Chain.MineNextBlock(Seeder.MinerAddress, 1);
            }
            return(RedirectToAction("RequestResult", new { success = isValid, msg = msg }));
        }