コード例 #1
0
        /// <summary>
        /// Adds block to chain if more than %50 of miners agrees it
        /// </summary>
        /// <param name="block"></param>
        /// <param name="blockID"></param>
        public static void AddBlockToChain(Block block)
        {
            for (int a = 0; a < Miners.miners.Count; a++)
            {
                for (int b = 0; b < Miners.miners[a].Count; b++)
                {
                    if (Miners.miners[a][b].Key.BlockID == block.BlockID)
                    {
                        if (Miners.miners[a][b].Key.Time.CompareTo(block.Time) < 0)
                        {
                            block.Time  = Miners.miners[a][b].Key.Time;
                            block.Nonce = Miners.miners[a][b].Key.Nonce;
                            block.Hash  = Miners.miners[a][b].Key.Hash;
                        }
                        Miners.miners[a].RemoveAt(b);
                        break;
                    }
                }
            }

            for (int a = 0; a < Chain.Count; a++)
            {
                if (Chain[a].BlockID == block.BlockID)
                {
                    return;
                }
            }

            Chain.Add(block);

            // notify all miners that a block is added into the local chain
            TCP.SendAllMiners("block" + TCP.JsonSerialize(block));
        }
コード例 #2
0
        public static void MainMethod()
        {
            string title = @"
   _____                   _        _____ _           _       
  / ____|                 | |      / ____| |         (_)      
 | (___  _   _ _ __  _ __ | |_   _| |    | |__   __ _ _ _ __  
  \___ \| | | | '_ \| '_ \| | | | | |    | '_ \ / _` | | '_ \ 
  ____) | |_| | |_) | |_) | | |_| | |____| | | | (_| | | | | |
 |_____/ \__,_| .__/| .__/|_|\__, |\_____|_| |_|\__,_|_|_| |_|
              | |   | |       __/ |                           
              |_|   |_|      |___/                            
";

            Console.WriteLine(title);
            Console.WriteLine(
                "Command List: \n" +
                "connect [ip] --> Connect to website and join other miners. [ip] is webserver's ip.\n" +
                "print [id] --> Print block which given id\n" +
                "exit --> Leave the network\n" +
                "quit --> exit application\n");

            while (true)
            {
                string command = Console.ReadLine();
                command = command.ToLower();

                if (command.StartsWith("connect"))
                {
                    if (command.Length > 8)
                    {
                        command = command.Substring(8);
                    }
                    else
                    {
                        Console.WriteLine("Please enter the ip");
                        continue;
                    }
                    if (!command.Contains("."))
                    {
                        Console.WriteLine("Please enter the ip");
                        continue;
                    }
                    TCP.WebServerIp = command;
                    Miners.ConnectToNetwork();
                    continue;
                }
                if (command.StartsWith("print"))
                {
                    command = command.Substring(6);
                    Console.WriteLine(BlockChain.GetBlock(int.Parse(command)).ToString());
                    break;
                }
                if (command.StartsWith("chain"))
                {
                    Console.WriteLine("\n--------BLOCKCHAIN--------");
                    BlockChain.GetChain().ForEach(b => Console.WriteLine(b.ToString()));
                    Console.WriteLine("--------BLOCKCHAIN--------\n");
                }
                if (command.StartsWith("get"))
                {
                    if (BlockChain.GetChain().Count == 1)
                    {
                        TCP.Send(Miners.minerIPs[0], "getChain");
                    }
                    else
                    {
                        Console.WriteLine("Already have chain");
                    }
                }
                if (command.StartsWith("read"))
                {
                    string path = command.Substring(5);
                    string st   = File.ReadAllText(path);
                    object ret  = TCP.JsonDeserialize(st);
                    var    obj  = TCP.Cast(ret, new { list = new List <Block>() });
                    BlockChain.SetChain(obj.list);
                    TCP.SendWebServer("addMeNow");
                }
                if (command.StartsWith("write"))
                {
                    string path = command.Substring(6);
                    string st   = TCP.JsonSerialize(new { list = BlockChain.GetChain() });
                    File.WriteAllText(path, st);
                }
                if (command.Equals("exit"))
                {
                    Console.WriteLine("Leaving network...\nPlease press a key");
                    Console.ReadKey();
                    Console.Clear();
                    MainMethod();
                    break;
                }
                if (command.Equals("quit"))
                {
                    Environment.Exit(0);
                }
            }
        }