コード例 #1
0
ファイル: Program.cs プロジェクト: stephanhuewe/NBlockchain
        static void RunCommand(string command, KeyPair keys)
        {
            var args       = command.Split(' ');
            var ownAddress = _addressEncoder.EncodeAddress(keys.PublicKey, 0);

            switch (args[0])
            {
            case "help":
                PrintHelp();
                break;

            case "mine-genesis":
                Console.WriteLine("Mining...");
                _miner.Start(keys, true);
                break;

            case "mine":
                Console.WriteLine("Mining...");
                _miner.Start(keys, false);
                break;

            case "stop-mining":
                Console.WriteLine("Stopping...");
                _miner.Stop();
                break;

            case "peers":
                var peersIn  = _network.GetPeersIn();
                var peersOut = _network.GetPeersOut();
                Console.WriteLine("Incoming peers");
                PrintPeerList(peersIn);
                Console.WriteLine("Outgoing peers");
                PrintPeerList(peersOut);
                break;

            case "balance":
                if (args.Length == 1)
                {
                    Console.WriteLine($"Balance = {_txnRepo.GetAccountBalance(ownAddress)}");
                }
                else
                {
                    Console.WriteLine($"Balance = {_txnRepo.GetAccountBalance(args[1])}");
                }
                break;

            case "best-block":
                var header = _blockRepo.GetBestBlockHeader().Result;
                Console.WriteLine($"Height: {header.Height}, Id: {BitConverter.ToString(header.BlockId)}");
                break;

            case "gen-key":
                var genkeys = _sigService.GetKeyPairFromPhrase(args[1]);
                var address = _addressEncoder.EncodeAddress(genkeys.PublicKey, 0);
                Console.WriteLine($"{address}");
                break;

            case "avg-time":
                var avgTime = _blockRepo.GetAverageBlockTimeInSecs(DateTime.UtcNow.AddHours(-1), DateTime.UtcNow).Result;
                Console.WriteLine($"Avg time: {avgTime}s");
                break;

            case "send":
                if (args.Length != 3)
                {
                    Console.WriteLine("invalid command");
                    return;
                }

                var instruction = new TransferInstruction()
                {
                    PublicKey   = keys.PublicKey,
                    Amount      = Convert.ToInt32(args[2]),
                    Destination = _addressEncoder.ExtractPublicKeyHash(args[1])
                };

                Console.WriteLine($"Signing instruction");
                _sigService.SignInstruction(instruction, keys.PrivateKey);
                var txn = _txnBuilder.Build(new List <Instruction>()
                {
                    instruction
                }).Result;

                Console.WriteLine($"Sending transaction {BitConverter.ToString(txn.TransactionId)}");
                _host.SendTransaction(txn);
                break;

            default:
                Console.WriteLine("invalid command");
                break;
            }
        }