Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }