diff --git a/neo-cli/CLI/MainService.Blockchain.cs b/neo-cli/CLI/MainService.Blockchain.cs index 97acd5dda..8927ce421 100644 --- a/neo-cli/CLI/MainService.Blockchain.cs +++ b/neo-cli/CLI/MainService.Blockchain.cs @@ -1,5 +1,4 @@ using Neo.ConsoleService; -using Neo.Ledger; using Neo.SmartContract.Native; using System; @@ -16,7 +15,7 @@ partial class MainService [ConsoleCommand("export blocks", Category = "Blockchain Commands")] private void OnExportBlocksStartCountCommand(uint start, uint count = uint.MaxValue, string path = null) { - uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View); + uint height = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView); if (height < start) { Console.WriteLine("Error: invalid start height."); diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index 2f1069acc..7f89dd0dd 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -24,7 +24,7 @@ private void OnDeployCommand(string filePath, string manifestPath = null) Transaction tx; try { - tx = CurrentWallet.MakeTransaction(script); + tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, script); } catch (InvalidOperationException e) { @@ -37,7 +37,7 @@ private void OnDeployCommand(string filePath, string manifestPath = null) Console.WriteLine($"Contract hash: {hash}"); Console.WriteLine($"Gas: {new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)}"); Console.WriteLine(); - SignAndSendTx(tx); + SignAndSendTx(NeoSystem.StoreView, tx); } /// @@ -84,7 +84,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra if (NoWallet()) return; try { - tx = CurrentWallet.MakeTransaction(tx.Script, sender, signers, maxGas: (long)gas.Value); + tx = CurrentWallet.MakeTransaction(NeoSystem.StoreView, tx.Script, sender, signers, maxGas: (long)gas.Value); } catch (InvalidOperationException e) { @@ -95,7 +95,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra { return; } - SignAndSendTx(tx); + SignAndSendTx(NeoSystem.StoreView, tx); } } } diff --git a/neo-cli/CLI/MainService.NEP17.cs b/neo-cli/CLI/MainService.NEP17.cs index 4a17f3d35..c6e838bfa 100644 --- a/neo-cli/CLI/MainService.NEP17.cs +++ b/neo-cli/CLI/MainService.NEP17.cs @@ -1,6 +1,5 @@ using Neo.ConsoleService; using Neo.IO.Json; -using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.SmartContract; using Neo.SmartContract.Native; @@ -24,7 +23,8 @@ partial class MainService [ConsoleCommand("transfer", Category = "NEP17 Commands")] private void OnTransferCommand(UInt160 tokenHash, UInt160 to, decimal amount, string data = null, UInt160 from = null, UInt160[] signersAccounts = null) { - var asset = new AssetDescriptor(tokenHash); + var snapshot = NeoSystem.StoreView; + var asset = new AssetDescriptor(snapshot, tokenHash); var value = new BigDecimal(amount, asset.Decimals); if (NoWallet()) return; @@ -32,7 +32,7 @@ private void OnTransferCommand(UInt160 tokenHash, UInt160 to, decimal amount, st Transaction tx; try { - tx = CurrentWallet.MakeTransaction(new[] + tx = CurrentWallet.MakeTransaction(snapshot, new[] { new TransferOutput { @@ -58,7 +58,7 @@ private void OnTransferCommand(UInt160 tokenHash, UInt160 to, decimal amount, st { return; } - SignAndSendTx(tx); + SignAndSendTx(snapshot, tx); } /// @@ -73,7 +73,7 @@ private void OnBalanceOfCommand(UInt160 tokenHash, UInt160 address) arg["type"] = "Hash160"; arg["value"] = address.ToString(); - var asset = new AssetDescriptor(tokenHash); + var asset = new AssetDescriptor(NeoSystem.StoreView, tokenHash); if (!OnInvokeWithResult(tokenHash, "balanceOf", out StackItem balanceResult, null, new JArray(arg))) return; @@ -90,8 +90,7 @@ private void OnBalanceOfCommand(UInt160 tokenHash, UInt160 address) [ConsoleCommand("name", Category = "NEP17 Commands")] private void OnNameCommand(UInt160 tokenHash) { - var snapshot = Blockchain.Singleton.GetSnapshot(); - ContractState contract = NativeContract.ContractManagement.GetContract(snapshot, tokenHash); + ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash); if (contract == null) Console.WriteLine($"Contract hash not exist: {tokenHash}"); else Console.WriteLine($"Result : {contract.Manifest.Name.ToString()}"); } @@ -117,7 +116,7 @@ private void OnTotalSupplyCommand(UInt160 tokenHash) { if (!OnInvokeWithResult(tokenHash, "totalSupply", out StackItem result, null)) return; - var asset = new AssetDescriptor(tokenHash); + var asset = new AssetDescriptor(NeoSystem.StoreView, tokenHash); var totalSupply = new BigDecimal(((PrimitiveType)result).GetInteger(), asset.Decimals); Console.WriteLine($"Result : {totalSupply}"); diff --git a/neo-cli/CLI/MainService.Network.cs b/neo-cli/CLI/MainService.Network.cs index 7748fb84e..50cd9a1d6 100644 --- a/neo-cli/CLI/MainService.Network.cs +++ b/neo-cli/CLI/MainService.Network.cs @@ -2,7 +2,6 @@ using Neo.ConsoleService; using Neo.IO; using Neo.IO.Json; -using Neo.Ledger; using Neo.Network.P2P; using Neo.Network.P2P.Capabilities; using Neo.Network.P2P.Payloads; @@ -45,7 +44,7 @@ private void OnBroadcastAddressCommand(IPAddress payload, ushort port) [ConsoleCommand("broadcast block", Category = "Network Commands")] private void OnBroadcastGetBlocksByHashCommand(UInt256 hash) { - OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(Blockchain.Singleton.View, hash)); + OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, hash)); } /// @@ -55,7 +54,7 @@ private void OnBroadcastGetBlocksByHashCommand(UInt256 hash) [ConsoleCommand("broadcast block", Category = "Network Commands")] private void OnBroadcastGetBlocksByHeightCommand(uint height) { - OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(Blockchain.Singleton.View, height)); + OnBroadcastCommand(MessageCommand.Block, NativeContract.Ledger.GetBlock(NeoSystem.StoreView, height)); } /// @@ -107,7 +106,7 @@ private void OnBroadcastInvCommand(InventoryType type, UInt256[] payload) [ConsoleCommand("broadcast transaction", Category = "Network Commands")] private void OnBroadcastTransactionCommand(UInt256 hash) { - if (Blockchain.Singleton.MemPool.TryGetValue(hash, out Transaction tx)) + if (NeoSystem.MemPool.TryGetValue(hash, out Transaction tx)) OnBroadcastCommand(MessageCommand.Transaction, tx); } @@ -131,7 +130,7 @@ private void OnRelayCommand(JObject jsonObjectToRelay) try { - ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToRelay.ToString()); + ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToRelay.ToString(), NeoSystem.StoreView); if (!context.Completed) { Console.WriteLine("The signature is incomplete."); diff --git a/neo-cli/CLI/MainService.Node.cs b/neo-cli/CLI/MainService.Node.cs index 0dd438c20..11d566b71 100644 --- a/neo-cli/CLI/MainService.Node.cs +++ b/neo-cli/CLI/MainService.Node.cs @@ -1,6 +1,5 @@ using Akka.Actor; using Neo.ConsoleService; -using Neo.Ledger; using Neo.Network.P2P; using Neo.Network.P2P.Payloads; using Neo.SmartContract.Native; @@ -23,7 +22,7 @@ private void OnShowPoolCommand(bool verbose = false) int verifiedCount, unverifiedCount; if (verbose) { - Blockchain.Singleton.MemPool.GetVerifiedAndUnverifiedTransactions( + NeoSystem.MemPool.GetVerifiedAndUnverifiedTransactions( out IEnumerable verifiedTransactions, out IEnumerable unverifiedTransactions); Console.WriteLine("Verified Transactions:"); @@ -38,10 +37,10 @@ private void OnShowPoolCommand(bool verbose = false) } else { - verifiedCount = Blockchain.Singleton.MemPool.VerifiedCount; - unverifiedCount = Blockchain.Singleton.MemPool.UnVerifiedCount; + verifiedCount = NeoSystem.MemPool.VerifiedCount; + unverifiedCount = NeoSystem.MemPool.UnVerifiedCount; } - Console.WriteLine($"total: {Blockchain.Singleton.MemPool.Count}, verified: {verifiedCount}, unverified: {unverifiedCount}"); + Console.WriteLine($"total: {NeoSystem.MemPool.Count}, verified: {verifiedCount}, unverified: {unverifiedCount}"); } /// @@ -59,8 +58,8 @@ private void OnShowStateCommand() { while (!cancel.Token.IsCancellationRequested) { - NeoSystem.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View)))); - await Task.Delay(Blockchain.TimePerBlock, cancel.Token); + NeoSystem.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView)))); + await Task.Delay(NeoSystem.Settings.TimePerBlock, cancel.Token); } }); Task task = Task.Run(async () => @@ -69,14 +68,15 @@ private void OnShowStateCommand() while (!cancel.Token.IsCancellationRequested) { - uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View); - uint headerHeight = Blockchain.Singleton.HeaderCache.Last?.Index ?? height; + uint height = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView); + uint headerHeight = NeoSystem.HeaderCache.Last?.Index ?? height; Console.SetCursorPosition(0, 0); - WriteLineWithoutFlicker($"block: {height}/{headerHeight} connected: {LocalNode.Singleton.ConnectedCount} unconnected: {LocalNode.Singleton.UnconnectedCount}", Console.WindowWidth - 1); + LocalNode localNode = await NeoSystem.LocalNode.Ask(new LocalNode.GetInstance()); + WriteLineWithoutFlicker($"block: {height}/{headerHeight} connected: {localNode.ConnectedCount} unconnected: {localNode.UnconnectedCount}", Console.WindowWidth - 1); int linesWritten = 1; - foreach (RemoteNode node in LocalNode.Singleton.GetRemoteNodes().OrderByDescending(u => u.LastBlockIndex).Take(Console.WindowHeight - 2).ToArray()) + foreach (RemoteNode node in localNode.GetRemoteNodes().OrderByDescending(u => u.LastBlockIndex).Take(Console.WindowHeight - 2).ToArray()) { Console.WriteLine( $" ip: {node.Remote.Address,-15}\tport: {node.Remote.Port,-5}\tlisten: {node.ListenerTcpPort,-5}\theight: {node.LastBlockIndex,-7}"); diff --git a/neo-cli/CLI/MainService.Wallet.cs b/neo-cli/CLI/MainService.Wallet.cs index 6edbde9d4..af9415e76 100644 --- a/neo-cli/CLI/MainService.Wallet.cs +++ b/neo-cli/CLI/MainService.Wallet.cs @@ -2,8 +2,8 @@ using Neo.ConsoleService; using Neo.Cryptography.ECC; using Neo.IO.Json; -using Neo.Ledger; using Neo.Network.P2P.Payloads; +using Neo.Persistence; using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.Wallets; @@ -343,33 +343,31 @@ private void OnListAddressCommand() { if (NoWallet()) return; - using (var snapshot = Blockchain.Singleton.GetSnapshot()) + var snapshot = NeoSystem.StoreView; + foreach (var account in CurrentWallet.GetAccounts()) { - foreach (var account in CurrentWallet.GetAccounts()) - { - var contract = account.Contract; - var type = "Nonstandard"; + var contract = account.Contract; + var type = "Nonstandard"; - if (account.WatchOnly) - { - type = "WatchOnly"; - } - else if (contract.Script.IsMultiSigContract()) - { - type = "MultiSignature"; - } - else if (contract.Script.IsSignatureContract()) - { - type = "Standard"; - } - else if (NativeContract.ContractManagement.GetContract(snapshot, account.ScriptHash) != null) - { - type = "Deployed-Nonstandard"; - } - - Console.WriteLine($"{" Address: "}{account.Address}\t{type}"); - Console.WriteLine($"{"ScriptHash: "}{account.ScriptHash}\n"); + if (account.WatchOnly) + { + type = "WatchOnly"; } + else if (contract.Script.IsMultiSigContract()) + { + type = "MultiSignature"; + } + else if (contract.Script.IsSignatureContract()) + { + type = "Standard"; + } + else if (NativeContract.ContractManagement.GetContract(snapshot, account.ScriptHash) != null) + { + type = "Deployed-Nonstandard"; + } + + Console.WriteLine($"{" Address: "}{account.Address}\t{type}"); + Console.WriteLine($"{"ScriptHash: "}{account.ScriptHash}\n"); } } @@ -379,16 +377,17 @@ private void OnListAddressCommand() [ConsoleCommand("list asset", Category = "Wallet Commands")] private void OnListAssetCommand() { + var snapshot = NeoSystem.StoreView; if (NoWallet()) return; foreach (UInt160 account in CurrentWallet.GetAccounts().Select(p => p.ScriptHash)) { Console.WriteLine(account.ToAddress()); - Console.WriteLine($"NEO: {CurrentWallet.GetBalance(NativeContract.NEO.Hash, account)}"); - Console.WriteLine($"GAS: {CurrentWallet.GetBalance(NativeContract.GAS.Hash, account)}"); + Console.WriteLine($"NEO: {CurrentWallet.GetBalance(snapshot, NativeContract.NEO.Hash, account)}"); + Console.WriteLine($"GAS: {CurrentWallet.GetBalance(snapshot, NativeContract.GAS.Hash, account)}"); Console.WriteLine(); } Console.WriteLine("----------------------------------------------------"); - Console.WriteLine($"Total: NEO: {CurrentWallet.GetAvailable(NativeContract.NEO.Hash),10} GAS: {CurrentWallet.GetAvailable(NativeContract.GAS.Hash),18}"); + Console.WriteLine($"Total: NEO: {CurrentWallet.GetAvailable(snapshot, NativeContract.NEO.Hash),10} GAS: {CurrentWallet.GetAvailable(snapshot, NativeContract.GAS.Hash),18}"); Console.WriteLine(); Console.WriteLine("NEO hash: " + NativeContract.NEO.Hash); Console.WriteLine("GAS hash: " + NativeContract.GAS.Hash); @@ -425,7 +424,8 @@ private void OnSignCommand(JObject jsonObjectToSign) } try { - ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToSign.ToString()); + var snapshot = NeoSystem.StoreView; + ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToSign.ToString(), snapshot); if (!CurrentWallet.Sign(context)) { Console.WriteLine("The private key that can sign the data is not found."); @@ -462,9 +462,9 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, string data Console.WriteLine("Incorrect password"); return; } - + var snapshot = NeoSystem.StoreView; Transaction tx; - AssetDescriptor descriptor = new AssetDescriptor(asset); + AssetDescriptor descriptor = new AssetDescriptor(snapshot, asset); if (!BigDecimal.TryParse(amount, descriptor.Decimals, out BigDecimal decimalAmount) || decimalAmount.Sign <= 0) { Console.WriteLine("Incorrect Amount Format"); @@ -472,7 +472,7 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, string data } try { - tx = CurrentWallet.MakeTransaction(new[] + tx = CurrentWallet.MakeTransaction(snapshot, new[] { new TransferOutput { @@ -501,7 +501,7 @@ private void OnSendCommand(UInt160 asset, UInt160 to, string amount, string data return; } - ContractParametersContext context = new ContractParametersContext(tx); + ContractParametersContext context = new ContractParametersContext(snapshot, tx); CurrentWallet.Sign(context); if (context.Completed) { @@ -524,12 +524,10 @@ private void OnShowGasCommand() { if (NoWallet()) return; BigInteger gas = BigInteger.Zero; - using (var snapshot = Blockchain.Singleton.GetSnapshot()) - { - uint height = NativeContract.Ledger.CurrentIndex(snapshot) + 1; - foreach (UInt160 account in CurrentWallet.GetAccounts().Select(p => p.ScriptHash)) - gas += NativeContract.NEO.UnclaimedGas(snapshot, account, height); - } + var snapshot = NeoSystem.StoreView; + uint height = NativeContract.Ledger.CurrentIndex(snapshot) + 1; + foreach (UInt160 account in CurrentWallet.GetAccounts().Select(p => p.ScriptHash)) + gas += NativeContract.NEO.UnclaimedGas(snapshot, account, height); Console.WriteLine($"Unclaimed gas: {new BigDecimal(gas, NativeContract.GAS.Decimals)}"); } @@ -591,12 +589,12 @@ private void OnChangePasswordCommand() } } - private void SignAndSendTx(Transaction tx) + private void SignAndSendTx(DataCache snapshot, Transaction tx) { ContractParametersContext context; try { - context = new ContractParametersContext(tx); + context = new ContractParametersContext(snapshot, tx); } catch (InvalidOperationException e) { diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index af07011c7..f31870e65 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -172,13 +172,13 @@ public void CreateWallet(string path, string password) } } - private static IEnumerable GetBlocks(Stream stream, bool read_start = false) + private IEnumerable GetBlocks(Stream stream, bool read_start = false) { using BinaryReader r = new BinaryReader(stream); uint start = read_start ? r.ReadUInt32() : 0; uint count = r.ReadUInt32(); uint end = start + count - 1; - uint currentHeight = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View); + uint currentHeight = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView); if (end <= currentHeight) yield break; for (uint height = start; height <= end; height++) { @@ -218,7 +218,7 @@ private IEnumerable GetBlocksFromFile() IsCompressed = p.EndsWith(".zip") }).OrderBy(p => p.Start); - uint height = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View); + uint height = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView); foreach (var path in paths) { if (path.Start > height + 1) break; @@ -350,7 +350,7 @@ public async void Start(string[] args) _ = new Logger(); - NeoSystem = new NeoSystem(Settings.Default.Storage.Engine, Settings.Default.Storage.Path); + NeoSystem = new NeoSystem(ProtocolSettings.Default, Settings.Default.Storage.Engine, Settings.Default.Storage.Path); foreach (var plugin in Plugin.Plugins) { @@ -445,7 +445,7 @@ private void WriteBlocks(uint start, uint count, string path, bool writeStart) { for (uint i = start; i <= end; i++) { - Block block = NativeContract.Ledger.GetBlock(Blockchain.Singleton.View, i); + Block block = NativeContract.Ledger.GetBlock(NeoSystem.StoreView, i); byte[] array = block.ToArray(); fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int)); fs.Write(array, 0, array.Length); @@ -471,24 +471,22 @@ private static void WriteLineWithoutFlicker(string message = "", int maxWidth = private void SendTransaction(byte[] script, UInt160 account = null, long gas = TestModeGas) { Signer[] signers = System.Array.Empty(); + var snapshot = NeoSystem.StoreView; if (account != null) { - using (var snapshot = Blockchain.Singleton.GetSnapshot()) - { - signers = CurrentWallet.GetAccounts() - .Where(p => !p.Lock && !p.WatchOnly && p.ScriptHash == account && NativeContract.GAS.BalanceOf(snapshot, p.ScriptHash).Sign > 0) - .Select(p => new Signer() { Account = p.ScriptHash, Scopes = WitnessScope.CalledByEntry }) - .ToArray(); - } + signers = CurrentWallet.GetAccounts() + .Where(p => !p.Lock && !p.WatchOnly && p.ScriptHash == account && NativeContract.GAS.BalanceOf(snapshot, p.ScriptHash).Sign > 0) + .Select(p => new Signer() { Account = p.ScriptHash, Scopes = WitnessScope.CalledByEntry }) + .ToArray(); } try { - Transaction tx = CurrentWallet.MakeTransaction(script, account, signers, maxGas: gas); + Transaction tx = CurrentWallet.MakeTransaction(snapshot, script, account, signers, maxGas: gas); Console.WriteLine($"Invoking script with: '{tx.Script.ToBase64String()}'"); - using (ApplicationEngine engine = ApplicationEngine.Run(tx.Script, container: tx, gas: gas)) + using (ApplicationEngine engine = ApplicationEngine.Run(tx.Script, snapshot, container: tx, gas: gas)) { PrintExecutionOutput(engine, true); if (engine.State == VMState.FAULT) return; @@ -499,7 +497,7 @@ private void SendTransaction(byte[] script, UInt160 account = null, long gas = T return; } - SignAndSendTx(tx); + SignAndSendTx(NeoSystem.StoreView, tx); } catch (InvalidOperationException e) { @@ -533,7 +531,7 @@ private bool OnInvokeWithResult(UInt160 scriptHash, string operation, out StackI } } - ContractState contract = NativeContract.ContractManagement.GetContract(Blockchain.Singleton.View, scriptHash); + ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash); if (contract == null) { Console.WriteLine("Contract does not exist."); @@ -564,7 +562,7 @@ private bool OnInvokeWithResult(UInt160 scriptHash, string operation, out StackI tx.Script = script; } - using ApplicationEngine engine = ApplicationEngine.Run(script, container: verificable, gas: gas); + using ApplicationEngine engine = ApplicationEngine.Run(script, NeoSystem.StoreView, container: verificable, gas: gas); PrintExecutionOutput(engine, showStack); result = engine.State == VMState.FAULT ? null : engine.ResultStack.Peek(); return engine.State != VMState.FAULT; diff --git a/neo-cli/neo-cli.csproj b/neo-cli/neo-cli.csproj index 2f31f7ea6..76c1bc9fa 100644 --- a/neo-cli/neo-cli.csproj +++ b/neo-cli/neo-cli.csproj @@ -1,4 +1,4 @@ - + 2016-2021 The Neo Project @@ -23,7 +23,7 @@ - +