internal void HandleConnectCommand(INetworkManager networkManager)
        {
            Console.WriteLine("Specify the IP to connect to (ip:port)");
            Console.Write("> ");
            var connPortInput = Console.ReadLine().ToLower();

            try
            {
                string connectionIp = connPortInput.Split(':')[0];
                int    connectPort  = ushort.Parse(connPortInput.Split(':')[1]);
                networkManager.ConnectToPeer(new NetworkNode(ConnectionType.Outbound, new IPEndPoint(IPAddress.Parse(connectionIp), connectPort)));
            }
            catch (Exception)
            {
                Console.WriteLine("Something went wrong. Command aborted.");
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            CryptographyCommandhandler cryptographyCmdHandler = new CryptographyCommandhandler(new KeyGenerator());

            cryptographyCmdHandler.HandleGenerateKeysCommand(out string walletPubKey, out string walletPrivKey);
            PushKeyPair(walletPubKey, walletPrivKey);

            Console.WriteLine("Your new public key: " + walletPubKey);
            Console.WriteLine("Your new private key: " + walletPrivKey);
            Console.WriteLine("Loading blockchain..");

            var       networkIdentifier = "testnet";
            var       services          = SetupDI(networkIdentifier, walletPubKey, walletPrivKey);
            ushort    listeningPort     = NetworkConstants.DefaultListeningPort;
            IPAddress publicIP          = IPAddress.Parse("127.0.0.1"); // Our public IP so other nodes can find us, todo

            if (args.Length > 1 && args[0] == "-port")
            {
                listeningPort = ushort.Parse(args[1]);
            }

            IBlockchainRepository  blockchainRepo     = services.GetService <IBlockchainRepository>();
            ITransactionRepository transactionRepo    = services.GetService <ITransactionRepository>();
            ITransactionCreator    transactionCreator = services.GetService <ITransactionCreator>();
            ITimestamper           timestamper        = services.GetService <ITimestamper>();
            ISkuRepository         skuRepository      = services.GetService <ISkuRepository>();
            INetworkManager        networkManager     = services.GetService <INetworkManager>();
            ILoggerFactory         loggerFactory      = services.GetService <ILoggerFactory>();
            ISkuRepository         skuRepo            = services.GetService <ISkuRepository>();
            Miner miner = services.GetService <Miner>();

            _logger = loggerFactory.CreateLogger <Program>();
            Blockchain blockchain = blockchainRepo.GetChainByNetId(networkIdentifier);

            // Command handlers, only large commands are handles by these separate handlers.
            AccountsCommandHandler             accountsCmdHandler       = new AccountsCommandHandler(transactionRepo, networkIdentifier);
            SkusCommandHandler                 skusCmdHandler           = new SkusCommandHandler(blockchainRepo, timestamper, skuRepository, networkIdentifier);
            TransactionsCommandHandler         transactionsCmdHandler   = new TransactionsCommandHandler(transactionRepo, networkIdentifier);
            TransactionPoolCommandHandler      txpoolCmdHandler         = new TransactionPoolCommandHandler();
            TransferTokensCommandHandler       transferTokensCmdHandler = new TransferTokensCommandHandler(transactionRepo, transactionCreator);
            CreateSkuCommandHandler            createSkuCmdHandler      = new CreateSkuCommandHandler(transactionRepo, transactionCreator);
            TransferSupplyCommandHandler       transferSupplyCmdHandler = new TransferSupplyCommandHandler(skuRepository, transactionRepo, transactionCreator, networkIdentifier);
            NetworkingCommandHandler           networkingCmdHandler     = new NetworkingCommandHandler();
            TransactionGeneratorCommandHandler txGeneratorCmdHandler    = new TransactionGeneratorCommandHandler(miner, transactionCreator, skuRepo, blockchainRepo);
            CreateSupplyCommandHandler         createSupplyCmdHandler   = new CreateSupplyCommandHandler(skuRepository, transactionRepo, transactionCreator, networkIdentifier);
            DestroySupplyCommandHandler        destroySupplyCmdHandler  = new DestroySupplyCommandHandler(skuRepository, transactionRepo, transactionCreator, networkIdentifier);

            _logger.LogInformation("Loaded blockchain. Current height: {Height}", blockchain.CurrentHeight == -1 ? "GENESIS" : blockchain.CurrentHeight.ToString());
            networkManager.AcceptConnections(publicIP, listeningPort, new CancellationTokenSource());

            networkManager.ConnectToPeer(new NetworkNode(ConnectionType.Outbound, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345)));

            PrintConsoleCommands();

            var skuTransactions = 0;
            var txpool          = ConcurrentTransactionPool.GetInstance();

            EventPublisher.GetInstance().OnValidTransactionReceived += (object sender, TransactionReceivedEventArgs txargs) =>
            {
                if (txargs.Transaction.Action == TransactionAction.CreateSku.ToString())
                {
                    skuTransactions++;
                }

                if (skuTransactions > 200000 && txpool.Count() < 1)
                {
                    miner.StopMining(true);
                    txGeneratorCmdHandler.HandleStopCommand();
                }
            };

            var input = "";

            while (input != "exit")
            {
                input = Console.ReadLine().ToLower();
                switch (input)
                {
                case "help":
                    PrintConsoleCommands();
                    break;

                case "transactiongenerator startandmine":
                    txGeneratorCmdHandler.HandleStartCommand(true);
                    break;

                case "transactiongenerator start":
                    txGeneratorCmdHandler.HandleStartCommand(false);
                    break;

                case "transactiongenerator stop":
                    txGeneratorCmdHandler.HandleStopCommand();
                    break;

                case "generatekeys":
                    cryptographyCmdHandler.HandleGenerateKeysCommand(out walletPubKey, out walletPrivKey);
                    PushKeyPair(walletPubKey, walletPrivKey);
                    Console.WriteLine("Your new public key: " + walletPubKey);
                    Console.WriteLine("Your new private key: " + walletPrivKey);
                    Console.Write("> ");
                    break;

                case "accounts":
                case "users":
                case "balances":
                    accountsCmdHandler.HandleCommand();
                    break;

                case "skus":
                    skusCmdHandler.HandleCommand();
                    break;

                case "txpool":
                case "transactionpool":
                case "pendingtransactions":
                    txpoolCmdHandler.HandleCommand(miner.TransactionPool);
                    break;

                case "transactions":
                    transactionsCmdHandler.HandleCommand();
                    break;

                case "startmining":
                    miner.StartMining();
                    Console.Write("> ");
                    break;

                case "stopmining":
                    miner.StopMining(true);
                    PrintConsoleCommands();
                    break;

                case "resetblockchain":
                    networkManager.Dispose();
                    _logger.LogWarning("All network connections shut down.");
                    miner.StopMining(false);
                    blockchainRepo.Delete(networkIdentifier);
                    _logger.LogInformation("Local blockchain file deleted as requested by user");
                    Console.WriteLine("Please restart the logistichain executable now to reload a fresh blockchain instance!");
                    break;

                case "transfertokens":
                    transferTokensCmdHandler.HandleCommand(networkIdentifier);
                    break;

                case "createsku":
                    createSkuCmdHandler.HandleCommand(networkIdentifier);
                    break;

                case "transfersupply":
                    transferSupplyCmdHandler.HandleCommand();
                    break;

                case "createsupply":
                    createSupplyCmdHandler.HandleCommand();
                    break;

                case "destroysupply":
                    destroySupplyCmdHandler.HandleCommand();
                    break;

                case "networking setport":
                    listeningPort = networkingCmdHandler.HandleSetPortCommand(listeningPort);
                    break;

                case "networking setaddress":
                    publicIP = networkingCmdHandler.HandleSetAddressCommand(publicIP);
                    break;

                case "networking connect":
                    networkingCmdHandler.HandleConnectCommand(networkManager);
                    break;

                case "networking disconnect":
                    networkingCmdHandler.HandleDisconnectCommand(networkManager);
                    break;

                case "networking pool":
                    networkingCmdHandler.HandleListPoolCommand(NetworkNodesPool.GetInstance(loggerFactory));
                    break;

                case "networking stop":
                    networkManager.Dispose();
                    break;

                case "networking start":
                    if (networkManager.IsDisposed)
                    {
                        networkManager = GetService <INetworkManager>(services);
                    }
                    networkManager.AcceptConnections(publicIP, listeningPort, new CancellationTokenSource());
                    break;

                case "networking restart":
                    networkManager.Dispose();
                    Thread.Sleep(1000);
                    networkManager = GetService <INetworkManager>(services);
                    networkManager.AcceptConnections(publicIP, listeningPort, new CancellationTokenSource());
                    break;

                default:
                    Console.WriteLine("I don't recognize that command.");
                    Console.Write("> ");
                    break;
                }
            }
        }