コード例 #1
0
        public JObject OnCheckpointCreate(JArray @params)
        {
            string filename = @params[0].AsString();

            if (ProtocolSettings.Default.StandbyValidators.Length > 1)
            {
                throw new Exception("Checkpoint create is only supported on single node express instances");
            }

            if (store is Persistence.RocksDbStore rocksDbStore)
            {
                var defaultAccount = System.RpcServer.Wallet.GetAccounts().Single(a => a.IsDefault);
                BlockchainOperations.CreateCheckpoint(
                    rocksDbStore,
                    filename,
                    ProtocolSettings.Default.Magic,
                    defaultAccount.ScriptHash.ToAddress());

                return(filename);
            }
            else
            {
                throw new Exception("Checkpoint create is only supported for RocksDb storage implementation");
            }
        }
コード例 #2
0
            async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var(chain, _) = Program.LoadExpressChain(Input);

                    var account = chain.GetAccount(Account);
                    if (account == null)
                    {
                        throw new Exception($"Account {Account} not found.");
                    }

                    if (BlockchainOperations.TryLoadContract(Contract, out var contract, out var errorMessage))
                    {
                        console.WriteLine($"Deploying contract {contract.Name} ({contract.Hash}) {(SaveMetadata ? "and contract metadata" : "")}");
                        var tx = await BlockchainOperations.DeployContract(chain, contract, account, SaveMetadata);

                        console.WriteLine($"InvocationTransaction {tx.Hash} submitted");
                    }
                    else
                    {
                        throw new Exception(errorMessage);
                    }

                    return(0);
                }
コード例 #3
0
            async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var(chain, _) = Program.LoadExpressChain(Input);
                    var hash = GetScriptHash(Contract);

                    var contract = await BlockchainOperations.GetContract(chain, hash);

                    if (contract != null)
                    {
                        var json = JsonConvert.SerializeObject(contract, Formatting.Indented);
                        console.WriteLine(json);
                    }
                    else
                    {
                        console.WriteError($"Contract {Contract} not found");
                    }
                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #4
0
            private async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var(chain, _) = Program.LoadExpressChain(Input);
                    var contracts = await BlockchainOperations.ListContracts(chain);

                    for (int i = 0; i < contracts.Count; i++)
                    {
                        var contract = contracts[i];
                        var json     = JsonConvert.SerializeObject(contract, Formatting.Indented);
                        console.WriteLine(json);
                    }

                    if (contracts.Count == 0)
                    {
                        console.WriteLine("no contracts deployed");
                    }

                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #5
0
            async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var(chain, _) = Program.LoadExpressChain(Input);
                    var hash = GetScriptHash(Contract);

                    var storages = await BlockchainOperations.GetStorage(chain, hash);

                    foreach (var storage in storages)
                    {
                        var key   = storage.Key.ToByteArray();
                        var value = storage.Value.ToByteArray();

                        console.Write("0x");
                        console.WriteLine(key.ToHexString(true));
                        console.Write("  key (as string)   : ");
                        console.WriteLine(Encoding.UTF8.GetString(key));
                        console.Write("  value (as bytes)  : 0x");
                        console.WriteLine(value.ToHexString(true));
                        console.Write("        (as string) : ");
                        console.WriteLine(Encoding.UTF8.GetString(value));
                        console.WriteLine($"  constant value    : {storage.Constant}");
                    }

                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #6
0
        static string GetScriptHash(string contract)
        {
            if (Neo.UInt160.TryParse(contract, out var _))
            {
                return(contract);
            }

            if (BlockchainOperations.TryLoadContract(contract, out var _contract, out var errorMessage))
            {
                return(_contract.Hash);
            }

            throw new Exception(errorMessage);
        }
コード例 #7
0
            async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                try
                {
                    if (!File.Exists(InvocationFile))
                    {
                        console.WriteError($"Invocation file {InvocationFile} couldn't be found");
                        console.WriteWarning("    Note: The arguments for the contract invoke command changed significantly in the v1.1 release.");
                        console.WriteWarning("          Please see https://neo-project.github.io/neo-express/command-reference#contract-invoke-subcommand for details.\n");

                        app.ShowHelp();
                        return(1);
                    }

                    var(chain, _) = Program.LoadExpressChain(Input);

                    if (Test)
                    {
                        var result = await BlockchainOperations.TestInvokeContract(chain, InvocationFile);

                        console.WriteLine($"Tx: {result.Tx}");
                        console.WriteLine($"Gas Consumed: {result.GasConsumed}");
                        console.WriteLine("Result Stack:");
                        foreach (var v in result.ReturnStack)
                        {
                            console.WriteLine($"\t{v.Value} ({v.Type})");
                        }
                    }
                    else
                    {
                        var account = chain.GetAccount(Account);
                        if (account == null)
                        {
                            throw new Exception("Invalid Account");
                        }

                        var tx = await BlockchainOperations.InvokeContract(chain, InvocationFile, account);

                        console.WriteLine($"InvocationTransaction {tx.Hash} submitted");
                    }
                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #8
0
        private async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
        {
            try
            {
                var(chain, _) = Program.LoadExpressChain(Input);
                var index = NodeIndex.GetValueOrDefault();

                if (!NodeIndex.HasValue && chain.ConsensusNodes.Count > 1)
                {
                    throw new Exception("Node index not specified");
                }

                if (index >= chain.ConsensusNodes.Count || index < 0)
                {
                    throw new Exception("Invalid node index");
                }

                var node   = chain.ConsensusNodes[index];
                var folder = node.GetBlockchainPath();

                if (Reset && Directory.Exists(folder))
                {
                    Directory.Delete(folder, true);
                }

                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                using (var cts = new CancellationTokenSource())
                {
                    console.CancelKeyPress += (sender, args) => cts.Cancel();

                    await BlockchainOperations.RunBlockchainAsync(folder, chain, index, SecondsPerBlock,
                                                                  console.Out, cts.Token)
                    .ConfigureAwait(false);
                }

                return(0);
            }
            catch (Exception ex)
            {
                console.WriteError(ex.Message);
                app.ShowHelp();
                return(1);
            }
        }
コード例 #9
0
            private int OnExecute(CommandLineApplication app, IConsole console)
            {
                string checkpointTempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                try
                {
                    var filename = ValidateCheckpointFileName(Name);
                    var(chain, _) = Program.LoadExpressChain(Input);

                    if (chain.ConsensusNodes.Count > 1)
                    {
                        throw new Exception("Checkpoint restore is only supported on single node express instances");
                    }

                    var node           = chain.ConsensusNodes[0];
                    var blockchainPath = node.GetBlockchainPath();
                    if (!Force && Directory.Exists(blockchainPath))
                    {
                        throw new Exception("You must specify force to restore a checkpoint to an existing blockchain.");
                    }

                    ZipFile.ExtractToDirectory(filename, checkpointTempPath);

                    if (Directory.Exists(blockchainPath))
                    {
                        Directory.Delete(blockchainPath, true);
                    }

                    BlockchainOperations.RestoreCheckpoint(chain, blockchainPath, checkpointTempPath);

                    console.WriteLine($"Checkpoint {Name} sucessfully restored");
                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
                finally
                {
                    if (Directory.Exists(checkpointTempPath))
                    {
                        Directory.Delete(checkpointTempPath, true);
                    }
                }
            }
コード例 #10
0
        private int OnExecute(CommandLineApplication app, IConsole console)
        {
            try
            {
                var output = Program.GetDefaultFilename(Output);
                if (File.Exists(output) && !Force)
                {
                    throw new Exception("You must specify --force to overwrite an existing file");
                }

                var count = (Count == 0 ? 1 : Count);
                if (PreloadGas > 0 && count != 1)
                {
                    throw new Exception("you can only specify --preload-gas for a single node neo-express blockchain");
                }

                var chain = BlockchainOperations.CreateBlockchain(count);
                chain.Save(output);

                console.WriteLine($"Created {count} node privatenet at {output}");
                console.WriteWarning("    Note: The private keys for the accounts in this file are are *not* encrypted.");
                console.WriteWarning("          Do not use these accounts on MainNet or in any other system where security is a concern.");

                if (PreloadGas > 0)
                {
                    var node   = chain.ConsensusNodes[0];
                    var folder = node.GetBlockchainPath();

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    using var cts           = new CancellationTokenSource();
                    console.CancelKeyPress += (sender, args) => cts.Cancel();
                    BlockchainOperations.PreloadGas(folder, chain, 0, PreloadGas, console.Out, cts.Token);
                }

                return(0);
            }
            catch (Exception ex)
            {
                console.WriteError(ex.Message);
                app.ShowHelp();
                return(1);
            }
        }
コード例 #11
0
        private int OnExecute(CommandLineApplication app, IConsole console)
        {
            try
            {
                var(chain, _) = Program.LoadExpressChain(Input);
                var password = Prompt.GetPassword("Input password to use for exported wallets");

                BlockchainOperations.ExportBlockchain(chain, Directory.GetCurrentDirectory(), password, msg => console.WriteLine(msg));

                return(0);
            }
            catch (Exception ex)
            {
                console.WriteError(ex.Message);
                app.ShowHelp();
                return(1);
            }
        }
コード例 #12
0
            private int OnExecute(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var(chain, filename) = Program.LoadExpressChain(Input);
                    if (chain.IsReservedName(Name))
                    {
                        throw new Exception($"{Name} is a reserved name. Choose a different wallet name.");
                    }

                    var existingWallet = chain.GetWallet(Name);
                    if (existingWallet != null)
                    {
                        if (!Force)
                        {
                            throw new Exception($"{Name} dev wallet already exists. Use --force to overwrite.");
                        }

                        chain.Wallets.Remove(existingWallet);
                    }

                    var wallet = BlockchainOperations.CreateWallet(Name);
                    (chain.Wallets ?? (chain.Wallets = new List <ExpressWallet>(1)))
                    .Add(wallet);
                    chain.Save(filename);

                    console.WriteLine(Name);
                    foreach (var account in wallet.Accounts)
                    {
                        console.WriteLine($"    {account.ScriptHash}");
                    }
                    console.WriteWarning("    Note: The private keys for the accounts in this wallet are *not* encrypted.");
                    console.WriteWarning("          Do not use these accounts on MainNet or in any other system where security is a concern.");

                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #13
0
            private int OnExecute(CommandLineApplication app, IConsole console)
            {
                try
                {
                    var output = string.IsNullOrEmpty(Output)
                       ? Path.Combine(Directory.GetCurrentDirectory(), $"{Name}.wallet.json")
                       : Output;

                    if (File.Exists(output))
                    {
                        if (Force)
                        {
                            File.Delete(output);
                        }
                        else
                        {
                            throw new Exception("You must specify force to overwrite an exported wallet.");
                        }
                    }

                    var(chain, _) = Program.LoadExpressChain(Input);
                    var wallet = chain.GetWallet(Name);
                    if (wallet == null)
                    {
                        console.WriteLine($"{Name} privatenet wallet not found.");
                    }
                    else
                    {
                        var password = Prompt.GetPassword("Input password to use for exported wallet");
                        BlockchainOperations.ExportWallet(wallet, output, password);
                        console.WriteLine($"{Name} privatenet wallet exported to {output}");
                    }

                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
            }
コード例 #14
0
            private async Task <int> OnExecuteAsync(CommandLineApplication app, IConsole console)
            {
                string checkpointTempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                try
                {
                    var filename = ValidateCheckpointFileName(Name);
                    var(chain, _) = Program.LoadExpressChain(Input);

                    if (chain.ConsensusNodes.Count > 1)
                    {
                        throw new Exception("Checkpoint run is only supported on single node express instances");
                    }

                    ZipFile.ExtractToDirectory(filename, checkpointTempPath);

                    using (var cts = new CancellationTokenSource())
                    {
                        console.CancelKeyPress += (sender, args) => cts.Cancel();

                        await BlockchainOperations.RunCheckpointAsync(checkpointTempPath, chain, SecondsPerBlock,
                                                                      console.Out, cts.Token)
                        .ConfigureAwait(false);
                    }
                    return(0);
                }
                catch (Exception ex)
                {
                    console.WriteError(ex.Message);
                    app.ShowHelp();
                    return(1);
                }
                finally
                {
                    Directory.Delete(checkpointTempPath, true);
                }
            }