Пример #1
0
        static void TestBlockChain(string[] args)
        {
            if (args.Length >= 1)
            {
                Port = int.Parse(args[0]);
            }
            if (args.Length >= 2)
            {
                name = args[1];
            }

            if (Port > 0)
            {
                Server = new P2PServer(ipAddress, Port, PhillyCoin);
                Server.Start();
                Console.WriteLine($"P2P server is running on {ipAddress}:{Port}......");
            }
            if (name != "Unkown")
            {
                Console.WriteLine($"Current user is {name}");
            }

            Console.WriteLine("=========================");
            Console.WriteLine("1. Connect to a server");
            Console.WriteLine("2. Add a transaction");
            Console.WriteLine("3. Display Blockchain");
            Console.WriteLine("4. Exit");
            Console.WriteLine("=========================");

            int selection = 0;

            while (selection != 4)
            {
                switch (selection)
                {
                case 1:
                    Console.WriteLine("Please enter the server URL");
                    string serverURL = Console.ReadLine();
                    Client = new P2PClient(PhillyCoin);
                    Client.Connect($"{serverURL}/Blockchain");
                    break;

                case 2:
                    Console.WriteLine("Please enter the receiver name");
                    string receiverName = Console.ReadLine();
                    Console.WriteLine("Please enter the amount");
                    string amount = Console.ReadLine();
                    PhillyCoin.CreateTransaction(new Transaction(name, receiverName, int.Parse(amount)));
                    PhillyCoin.ProcessPendingTransactions(name);
                    Client = new P2PClient(PhillyCoin);
                    Client.Broadcast(JsonConvert.SerializeObject(PhillyCoin));
                    break;

                case 3:
                    Console.WriteLine("BlockChain");
                    Console.WriteLine(JsonConvert.SerializeObject(PhillyCoin, Formatting.Indented));
                    break;
                }

                Console.WriteLine("Please select an action");
                string action = Console.ReadLine();
                selection = int.Parse(action);
            }

            Client.Close();
        }
Пример #2
0
 public void CloseConnections()
 {
     //Close P2P
     P2PClient.Close();
 }