Esempio n. 1
0
        public void Receive(SocketDataBody data)
        {
            if (data.Type == SocketDataType.Receive)
            {
                if (data.Body != string.Empty && data.Body != "deep-sync")
                {
                    var newBlocks = JsonConvert.DeserializeObject <Block[]>(data.Body).ToList();

                    this.blockchain.RemoveBlockInterval(newBlocks.First().Index, newBlocks.Last().Index);

                    foreach (var block in newBlocks)
                    {
                        this.blockchain.AddBlock(block);
                    }

                    Console.WriteLine("End syncing...");
                }
                else if (data.Body == "deep-sync")
                {
                    Console.WriteLine("Start deep sync procedure...");
                    var ip   = data.NodesPair.Item2.Split(':')[0];
                    var port = int.Parse(data.NodesPair.Item2.Split(':')[1]);

                    CommandFabric.RunDynamic($"deep-sync -ip {ip} -p {port}");
                }
                else
                {
                    Console.WriteLine("Already is up-to-date...");
                }
            }
        }
Esempio n. 2
0
        public string Aggregate(SocketDataBody data)
        {
            var block = JsonConvert.DeserializeObject <Block>(data.Body);

            if (block.Index == this.blockchain.LastBlock.Index &&
                block.BlockHash == this.blockchain.LastBlock.BlockHash)
            {
                Console.WriteLine("All is synced...");
                return(string.Empty);
            }
            else
            {
                if (block.Index > this.blockchain.LastBlock.Index)
                {
                    var ip   = data.NodesPair.Item1.Split(':')[0];
                    var port = int.Parse(data.NodesPair.Item1.Split(':')[1]);

                    CommandFabric.RunDynamic($"sync -ip {ip} -p {port}");

                    return(string.Empty);
                }
                else
                {
                    return("sync");
                    //return JsonConvert.SerializeObject(this.blockchain.Blocks.Where(b => b.Index != 0).ToArray());
                }
            }
        }
Esempio n. 3
0
        public void NextBlockMined(string hash, int nonce)
        {
            Console.WriteLine();
            Console.WriteLine("+++++++++++++++++++++++++");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Found hash: {hash}...");
            Console.ResetColor();
            Console.WriteLine("+++++++++++++++++++++++++");

            CommandFabric.RunDynamic($"send-mining-result -nonce {nonce}");
        }
Esempio n. 4
0
        public void Receive(SocketDataBody data)
        {
            Console.WriteLine($"{data.NodesPair.Item1} -> {data.NodesPair.Item2}");
            if (data.Type == SocketDataType.Send)
            {
                var block = JsonConvert.DeserializeObject <Block>(data.Body);

                if (block.Index == this.blockchain.LastBlock.Index + 1 &&
                    block.PreviousBlockHash == this.blockchain.LastBlock.BlockHash)
                {
                    string lastBlockHash = this.blockchain.LastBlock.BlockHash;
                    int    nonce         = block.Nonce;
                    string winnerHash    = CryptographyUtilities.BytesToHex(CryptographyUtilities.CalcSHA256($"{lastBlockHash}{nonce}"));

                    if (!winnerHash.ToCharArray().Take(this.blockchain.Difficulty).All(s => s == '0'))
                    {
                        Console.WriteLine("Incorrect hash...");
                        return;
                    }

                    this.blockchain.AddBlock(block);
                }
                else
                {
                    if (block.Index <= this.blockchain.LastBlock.Index)
                    {
                        Console.WriteLine("Other node is not synced...");
                    }
                    else
                    {
                        Console.WriteLine("This node is not synced...");

                        var ip   = data.NodesPair.Item1.Split(':')[0];
                        var port = int.Parse(data.NodesPair.Item1.Split(':')[1]);

                        CommandFabric.RunDynamic($"sync -ip {ip} -p {port}");
                    }
                }
            }
            else if (data.Type == SocketDataType.Receive)
            {
                if (data.Body == "sync")
                {
                    var ip   = data.NodesPair.Item2.Split(':')[0];
                    var port = int.Parse(data.NodesPair.Item2.Split(':')[1]);

                    CommandFabric.RunDynamic($"sync -ip {ip} -p {port}");
                }
                //if (data.Body != string.Empty)
                //{
                //    var newBlocks = JsonConvert.DeserializeObject<Block[]>(data.Body).ToList();

                //    this.blockchain.RemoveBlockInterval(newBlocks.First().Index, newBlocks.Last().Index);

                //    foreach (var block in newBlocks)
                //    {
                //        this.blockchain.AddBlock(block);
                //    }
                //}
            }
        }