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

Commit a63d826

Browse files
author
Erik Zhang
committed
upgrade to 2.1.0
1. Add "sendmany" rpc api 2. Add "show utxo" command 3. Save peers state to file 4. Improve the help message
1 parent 8d71161 commit a63d826

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

neo-cli/Network/RPC/RpcServerWithWallet.cs

+48-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ protected override JObject Process(string method, JArray _params)
3737
UInt256 assetId = UInt256.Parse(_params[0].AsString());
3838
UInt160 scriptHash = Wallet.ToScriptHash(_params[1].AsString());
3939
Fixed8 value = Fixed8.Parse(_params[2].AsString());
40-
Fixed8 fee = _params.Count >= 4 ? Fixed8.Parse(_params[3].AsString()) : Fixed8.Zero;
41-
UInt160 change_address = _params.Count >= 5 ? Wallet.ToScriptHash(_params[4].AsString()) : null;
4240
if (value <= Fixed8.Zero)
4341
throw new RpcException(-32602, "Invalid params");
42+
Fixed8 fee = _params.Count >= 4 ? Fixed8.Parse(_params[3].AsString()) : Fixed8.Zero;
43+
if (value < Fixed8.Zero)
44+
throw new RpcException(-32602, "Invalid params");
45+
UInt160 change_address = _params.Count >= 5 ? Wallet.ToScriptHash(_params[4].AsString()) : null;
4446
ContractTransaction tx = Program.Wallet.MakeTransaction(new ContractTransaction
4547
{
4648
Outputs = new[]
@@ -69,6 +71,50 @@ protected override JObject Process(string method, JArray _params)
6971
return context.ToJson();
7072
}
7173
}
74+
case "sendmany":
75+
if (Program.Wallet == null)
76+
throw new RpcException(-400, "Access denied");
77+
else
78+
{
79+
JArray to = (JArray)_params[0];
80+
if (to.Count == 0)
81+
throw new RpcException(-32602, "Invalid params");
82+
TransactionOutput[] outputs = new TransactionOutput[to.Count];
83+
for (int i = 0; i < to.Count; i++)
84+
{
85+
outputs[i] = new TransactionOutput
86+
{
87+
AssetId = UInt256.Parse(to[i]["asset"].AsString()),
88+
Value = Fixed8.Parse(to[i]["value"].AsString()),
89+
ScriptHash = Wallet.ToScriptHash(to[i]["address"].AsString())
90+
};
91+
if (outputs[i].Value <= Fixed8.Zero)
92+
throw new RpcException(-32602, "Invalid params");
93+
}
94+
Fixed8 fee = _params.Count >= 2 ? Fixed8.Parse(_params[1].AsString()) : Fixed8.Zero;
95+
if (fee < Fixed8.Zero)
96+
throw new RpcException(-32602, "Invalid params");
97+
UInt160 change_address = _params.Count >= 3 ? Wallet.ToScriptHash(_params[2].AsString()) : null;
98+
ContractTransaction tx = Program.Wallet.MakeTransaction(new ContractTransaction
99+
{
100+
Outputs = outputs
101+
}, change_address: change_address, fee: fee);
102+
if (tx == null)
103+
throw new RpcException(-300, "Insufficient funds");
104+
SignatureContext context = new SignatureContext(tx);
105+
Program.Wallet.Sign(context);
106+
if (context.Completed)
107+
{
108+
tx.Scripts = context.GetScripts();
109+
Program.Wallet.SaveTransaction(tx);
110+
LocalNode.Relay(tx);
111+
return tx.ToJson();
112+
}
113+
else
114+
{
115+
return context.ToJson();
116+
}
117+
}
72118
case "getnewaddress":
73119
if (Program.Wallet == null)
74120
throw new RpcException(-400, "Access denied");

neo-cli/Shell/MainService.cs

+55-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Neo.Shell
1919
{
2020
internal class MainService : ConsoleServiceBase
2121
{
22+
private const string PeerStatePath = "peers.dat";
23+
2224
private RpcServerWithWallet rpc;
2325
private ConsensusWithPolicy consensus;
2426

@@ -265,10 +267,12 @@ private bool OnHelpCommand(string[] args)
265267
"Wallet Commands:\n" +
266268
"\tcreate wallet <path>\n" +
267269
"\topen wallet <path>\n" +
270+
"\tupgrade wallet <path>\n" +
268271
"\trebuild index\n" +
269272
"\tlist address\n" +
270273
"\tlist asset\n" +
271274
"\tlist key\n" +
275+
"\tshow utxo [id|alias]\n" +
272276
"\tshow gas\n" +
273277
"\tclaim gas\n" +
274278
"\tcreate address [n=1]\n" +
@@ -279,8 +283,10 @@ private bool OnHelpCommand(string[] args)
279283
"\tshow state\n" +
280284
"\tshow node\n" +
281285
"\tshow pool\n" +
286+
"\texport blocks [path=chain.acc]\n" +
282287
"Advanced Commands:\n" +
283-
"\tstart consensus\n");
288+
"\tstart consensus\n" +
289+
"\trefresh policy\n");
284290
return true;
285291
}
286292

@@ -604,6 +610,8 @@ private bool OnShowCommand(string[] args)
604610
return OnShowPoolCommand(args);
605611
case "state":
606612
return OnShowStateCommand(args);
613+
case "utxo":
614+
return OnShowUtxoCommand(args);
607615
default:
608616
return base.OnCommand(args);
609617
}
@@ -634,9 +642,51 @@ private bool OnShowStateCommand(string[] args)
634642
return true;
635643
}
636644

645+
private bool OnShowUtxoCommand(string[] args)
646+
{
647+
if (Program.Wallet == null)
648+
{
649+
Console.WriteLine("You have to open the wallet first.");
650+
return true;
651+
}
652+
IEnumerable<Coin> coins = Program.Wallet.FindUnspentCoins();
653+
if (args.Length >= 3)
654+
{
655+
UInt256 assetId;
656+
switch (args[2].ToLower())
657+
{
658+
case "neo":
659+
case "ans":
660+
assetId = Blockchain.SystemShare.Hash;
661+
break;
662+
case "gas":
663+
case "anc":
664+
assetId = Blockchain.SystemCoin.Hash;
665+
break;
666+
default:
667+
assetId = UInt256.Parse(args[2]);
668+
break;
669+
}
670+
coins = coins.Where(p => p.Output.AssetId.Equals(assetId));
671+
}
672+
Coin[] coins_array = coins.ToArray();
673+
const int MAX_SHOW = 100;
674+
for (int i = 0; i < coins_array.Length && i < MAX_SHOW; i++)
675+
Console.WriteLine($"{coins_array[i].Reference.PrevHash}:{coins_array[i].Reference.PrevIndex}");
676+
if (coins_array.Length > MAX_SHOW)
677+
Console.WriteLine($"({coins_array.Length - MAX_SHOW} more)");
678+
Console.WriteLine($"total: {coins_array.Length} UTXOs");
679+
return true;
680+
}
681+
637682
protected internal override void OnStart(string[] args)
638683
{
639684
Blockchain.RegisterBlockchain(new LevelDBBlockchain(Settings.Default.DataDirectoryPath));
685+
if (File.Exists(PeerStatePath))
686+
using (FileStream fs = new FileStream(PeerStatePath, FileMode.Open, FileAccess.Read, FileShare.Read))
687+
{
688+
LocalNode.LoadState(fs);
689+
}
640690
LocalNode = new LocalNode();
641691
Task.Run(() =>
642692
{
@@ -700,6 +750,10 @@ protected internal override void OnStop()
700750
if (consensus != null) consensus.Dispose();
701751
if (rpc != null) rpc.Dispose();
702752
LocalNode.Dispose();
753+
using (FileStream fs = new FileStream(PeerStatePath, FileMode.Create, FileAccess.Write, FileShare.None))
754+
{
755+
LocalNode.SaveState(fs);
756+
}
703757
Blockchain.Default.Dispose();
704758
}
705759

neo-cli/neo-cli.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Copyright>2016-2017 The Neo Project</Copyright>
55
<AssemblyTitle>Neo.CLI</AssemblyTitle>
6-
<Version>2.0.2</Version>
6+
<Version>2.1.0</Version>
77
<Authors>The Neo Project</Authors>
88
<TargetFramework>netcoreapp1.0</TargetFramework>
99
<AssemblyName>neo-cli</AssemblyName>
@@ -31,7 +31,7 @@
3131

3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
34-
<PackageReference Include="Neo" Version="2.0.2" />
34+
<PackageReference Include="Neo" Version="2.1.0" />
3535
</ItemGroup>
3636

3737
</Project>

0 commit comments

Comments
 (0)