Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

remove singletons #740

Merged
merged 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions neo-cli/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neo.ConsoleService;
using Neo.Ledger;
using Neo.SmartContract.Native;
using System;

Expand All @@ -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.");
Expand Down
8 changes: 4 additions & 4 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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)
{
Expand All @@ -95,7 +95,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra
{
return;
}
SignAndSendTx(tx);
SignAndSendTx(NeoSystem.StoreView, tx);
}
}
}
15 changes: 7 additions & 8 deletions neo-cli/CLI/MainService.NEP17.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,15 +23,16 @@ 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;

Transaction tx;
try
{
tx = CurrentWallet.MakeTransaction(new[]
tx = CurrentWallet.MakeTransaction(snapshot, new[]
{
new TransferOutput
{
Expand All @@ -58,7 +58,7 @@ private void OnTransferCommand(UInt160 tokenHash, UInt160 to, decimal amount, st
{
return;
}
SignAndSendTx(tx);
SignAndSendTx(snapshot, tx);
}

/// <summary>
Expand All @@ -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;

Expand All @@ -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()}");
}
Expand All @@ -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}");
Expand Down
9 changes: 4 additions & 5 deletions neo-cli/CLI/MainService.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/// <summary>
Expand All @@ -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));
}

/// <summary>
Expand Down Expand Up @@ -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);
}

Expand All @@ -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.");
Expand Down
22 changes: 11 additions & 11 deletions neo-cli/CLI/MainService.Node.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +22,7 @@ private void OnShowPoolCommand(bool verbose = false)
int verifiedCount, unverifiedCount;
if (verbose)
{
Blockchain.Singleton.MemPool.GetVerifiedAndUnverifiedTransactions(
NeoSystem.MemPool.GetVerifiedAndUnverifiedTransactions(
out IEnumerable<Transaction> verifiedTransactions,
out IEnumerable<Transaction> unverifiedTransactions);
Console.WriteLine("Verified Transactions:");
Expand All @@ -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}");
}

/// <summary>
Expand All @@ -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 () =>
Expand All @@ -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<LocalNode>(new LocalNode.GetInstance());
bettybao1209 marked this conversation as resolved.
Show resolved Hide resolved
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}");
Expand Down
80 changes: 39 additions & 41 deletions neo-cli/CLI/MainService.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -462,17 +462,17 @@ 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");
return;
}
try
{
tx = CurrentWallet.MakeTransaction(new[]
tx = CurrentWallet.MakeTransaction(snapshot, new[]
{
new TransferOutput
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)}");
}

Expand Down Expand Up @@ -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)
{
Expand Down
Loading