Skip to content

Commit

Permalink
adapt RpcClient to modules (#171)
Browse files Browse the repository at this point in the history
* adapt RpcClient to modules

* fix json issue

* fix size calculate

* Fix getnep5balances name issue

* add wallet rpc methods, reconstruct rpc UT

* PR correction

* move models to rpc server

* delete models from rpcclient

* change plugins with model

* format names

* move models back to rpc client

* fix build error

* change test file

* auto calculate network fee, update version

* format code

* fix issue #1416 Deal with ContractParameterType.Any

* change GetStorage parameter to enable id query

* add test for issue #1416

* PR correction

* use snake_case in json

* update neo version

* Update src/RpcClient/RpcClient.cs

Co-Authored-By: Shargon <[email protected]>

* Update src/RpcClient/RpcClient.cs

Co-Authored-By: Shargon <[email protected]>

* PR correction

* update neo version

Co-authored-by: Vitor Nazário Coelho <[email protected]>
Co-authored-by: Nicole <[email protected]>
Co-authored-by: Shargon <[email protected]>
  • Loading branch information
4 people authored Mar 19, 2020
1 parent 862cc79 commit bbcfa56
Show file tree
Hide file tree
Showing 49 changed files with 2,382 additions and 952 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ This module works in conjunction with RpcServer, otherwise, just local storage (
## C# SDK

### RpcClient
The RpcClient Plugin is an individual SDK which is used to call NEO RPC methods for development using.
In order to use the module for interacting with NEP5 functionalities `RPC NEP5 Tracker` Plugin will be needed.
The RpcClient Project is an individual SDK that is used to interact with NEO blockchain through NEO RPC methods for development using. The main functions include RPC calling, Transaction making, Contract deployment & calling, and Asset transfering.
It needs a NEO node with the `RpcServer` plugin as a provider. And the provider needs more plugins like `RpcNep5Tracker` and `ApplicationLogs` if you want to call RPC methods supplied by the plugins.
4 changes: 2 additions & 2 deletions src/LevelDBStore/LevelDBStore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-CI00847</Version>
<Version>3.0.0-CI00863</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Neo</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00855" />
<PackageReference Include="Neo" Version="3.0.0-CI00863" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/RocksDBStore/RocksDBStore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-CI00847</Version>
<Version>3.0.0-CI00863</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Neo.Plugins.Storage</RootNamespace>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00855" />
<PackageReference Include="Neo" Version="3.0.0-CI00863" />
<PackageReference Include="RocksDbNative" Version="6.2.2" />
<PackageReference Include="RocksDbSharp" Version="6.2.2" />
</ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions src/RpcClient/ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public RpcInvokeResult TestInvoke(UInt160 scriptHash, string operation, params o
/// <param name="contractScript">contract script</param>
/// <param name="manifest">contract manifest</param>
/// <param name="key">sender KeyPair</param>
/// <param name="networkFee">transaction NetworkFee, set to be 0 if you don't need higher priority</param>
/// <returns></returns>
public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifest manifest, KeyPair key, long networkFee = 0)
public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifest manifest, KeyPair key)
{
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
Expand All @@ -55,7 +54,7 @@ public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifes

UInt160 sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();
Transaction tx = new TransactionManager(rpcClient, sender)
.MakeTransaction(script, null, null, networkFee)
.MakeTransaction(script, null, null)
.AddSignature(key)
.Sign()
.Tx;
Expand Down
37 changes: 37 additions & 0 deletions src/RpcClient/Models/RpcAccount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Neo.IO.Json;

namespace Neo.Network.RPC.Models
{
public class RpcAccount
{
public string Address { get; set; }

public bool HasKey { get; set; }

public string Label { get; set; }

public bool WatchOnly { get; set; }

public JObject ToJson()
{
return new JObject
{
["address"] = Address,
["haskey"] = HasKey,
["label"] = Label,
["watchonly"] = WatchOnly
};
}

public static RpcAccount FromJson(JObject json)
{
return new RpcAccount
{
Address = json["address"].AsString(),
HasKey = json["haskey"].AsBoolean(),
Label = json["label"]?.AsString(),
WatchOnly = json["watchonly"].AsBoolean(),
};
}
}
}
71 changes: 71 additions & 0 deletions src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Network.RPC.Models
{
public class RpcApplicationLog
{
public UInt256 TxId { get; set; }

public TriggerType Trigger { get; set; }

public VMState VMState { get; set; }

public long GasConsumed { get; set; }

public List<ContractParameter> Stack { get; set; }

public List<RpcNotifyEventArgs> Notifications { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["txid"] = TxId?.ToString();
json["trigger"] = Trigger;
json["vmstate"] = VMState;
json["gas_consumed"] = GasConsumed.ToString();
json["stack"] = Stack.Select(q => q.ToJson()).ToArray();
json["notifications"] = Notifications.Select(q => q.ToJson()).ToArray();
return json;
}

public static RpcApplicationLog FromJson(JObject json)
{
RpcApplicationLog log = new RpcApplicationLog();
log.TxId = json["txid"] is null ? null : UInt256.Parse(json["txid"].AsString());
log.Trigger = json["trigger"].TryGetEnum<TriggerType>();
log.VMState = json["vmstate"].TryGetEnum<VMState>();
log.GasConsumed = long.Parse(json["gas_consumed"].AsString());
log.Stack = ((JArray)json["stack"]).Select(p => ContractParameter.FromJson(p)).ToList();
log.Notifications = ((JArray)json["notifications"]).Select(p => RpcNotifyEventArgs.FromJson(p)).ToList();
return log;
}
}

public class RpcNotifyEventArgs
{
public UInt160 Contract { get; set; }

public ContractParameter State { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["contract"] = Contract.ToString();
json["state"] = State.ToJson();
return json;
}

public static RpcNotifyEventArgs FromJson(JObject json)
{
return new RpcNotifyEventArgs
{
Contract = UInt160.Parse(json["contract"].AsString()),
State = ContractParameter.FromJson(json["state"])
};
}
}
}
14 changes: 4 additions & 10 deletions src/RpcClient/Models/RpcBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,24 @@ public class RpcBlock
{
public Block Block { get; set; }

public int Confirmations { get; set; }
public uint Confirmations { get; set; }

public UInt256 NextBlockHash { get; set; }

public JObject ToJson()
{
JObject json = Block.ToJson();
json["confirmations"] = Confirmations;
if (NextBlockHash != null)
{
json["nextblockhash"] = NextBlockHash.ToString();
}
json["nextblockhash"] = NextBlockHash?.ToString();
return json;
}

public static RpcBlock FromJson(JObject json)
{
RpcBlock block = new RpcBlock();
block.Block = Block.FromJson(json);
block.Confirmations = (int)json["confirmations"].AsNumber();
if (json["nextblockhash"] != null)
{
block.NextBlockHash = UInt256.Parse(json["nextblockhash"].AsString());
}
block.Confirmations = (uint)json["confirmations"].AsNumber();
block.NextBlockHash = json["nextblockhash"] is null ? null : UInt256.Parse(json["nextblockhash"].AsString());
return block;
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/RpcClient/Models/RpcBlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,24 @@ public class RpcBlockHeader
{
public Header Header { get; set; }

public int Confirmations { get; set; }
public uint Confirmations { get; set; }

public UInt256 NextBlockHash { get; set; }

public JObject ToJson()
{
JObject json = Header.ToJson();
json["confirmations"] = Confirmations;
if (NextBlockHash != null)
{
json["nextblockhash"] = NextBlockHash.ToString();
}
json["nextblockhash"] = NextBlockHash?.ToString();
return json;
}

public static RpcBlockHeader FromJson(JObject json)
{
RpcBlockHeader block = new RpcBlockHeader();
block.Header = Header.FromJson(json);
block.Confirmations = (int)json["confirmations"].AsNumber();
if (json["nextblockhash"] != null)
{
block.NextBlockHash = UInt256.Parse(json["nextblockhash"].AsString());
}
block.Confirmations = (uint)json["confirmations"].AsNumber();
block.NextBlockHash = json["nextblockhash"] is null ? null : UInt256.Parse(json["nextblockhash"].AsString());
return block;
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/RpcClient/Models/RpcContractState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.SmartContract.Manifest;
using System;

public class RpcContractState
{
public ContractState ContractState { get; set; }

public JObject ToJson()
{
return ContractState.ToJson();
}

public static RpcContractState FromJson(JObject json)
{
RpcContractState state = new RpcContractState();
state.ContractState = new ContractState
{
Id = (int)json["id"].AsNumber(),
Script = Convert.FromBase64String(json["script"].AsString()),
Manifest = ContractManifest.FromJson(json["manifest"])
};
return state;
}
}
25 changes: 21 additions & 4 deletions src/RpcClient/Models/RpcInvokeResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.Linq;

namespace Neo.Network.RPC.Models
Expand All @@ -8,29 +9,45 @@ public class RpcInvokeResult
{
public string Script { get; set; }

public string State { get; set; }
public VM.VMState State { get; set; }

public string GasConsumed { get; set; }

public ContractParameter[] Stack { get; set; }

public string Tx { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["script"] = Script;
json["state"] = State;
json["gas_consumed"] = GasConsumed;
json["stack"] = new JArray(Stack.Select(p => p.ToJson()));
try
{
json["stack"] = new JArray(Stack.Select(p => p.ToJson()));
}
catch (InvalidOperationException)
{
// ContractParameter.ToJson() may cause InvalidOperationException
json["stack"] = "error: recursive reference";
}
if (!string.IsNullOrEmpty(Tx)) json["tx"] = Tx;
return json;
}

public static RpcInvokeResult FromJson(JObject json)
{
RpcInvokeResult invokeScriptResult = new RpcInvokeResult();
invokeScriptResult.Script = json["script"].AsString();
invokeScriptResult.State = json["state"].AsString();
invokeScriptResult.State = json["state"].TryGetEnum<VM.VMState>();
invokeScriptResult.GasConsumed = json["gas_consumed"].AsString();
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => ContractParameter.FromJson(p)).ToArray();
try
{
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => ContractParameter.FromJson(p)).ToArray();
}
catch { }
invokeScriptResult.Tx = json["tx"]?.AsString();
return invokeScriptResult;
}
}
Expand Down
29 changes: 17 additions & 12 deletions src/RpcClient/Models/RpcNep5Balances.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
using Neo.IO.Json;
using Neo.Wallets;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Neo.Network.RPC.Models
{
public class RpcNep5Balances
{
public string Address { get; set; }
public UInt160 UserScriptHash { get; set; }

public RpcNep5Balance[] Balances { get; set; }
public List<RpcNep5Balance> Balances { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["address"] = Address;
json["balance"] = Balances.Select(p => p.ToJson()).ToArray();
json["address"] = UserScriptHash.ToAddress();
return json;
}

public static RpcNep5Balances FromJson(JObject json)
{
RpcNep5Balances nep5Balance = new RpcNep5Balances();
nep5Balance.Address = json["address"].AsString();
//List<Balance> listBalance = new List<Balance>();
nep5Balance.Balances = ((JArray)json["balance"]).Select(p => RpcNep5Balance.FromJson(p)).ToArray();
RpcNep5Balances nep5Balance = new RpcNep5Balances
{
Balances = ((JArray)json["balance"]).Select(p => RpcNep5Balance.FromJson(p)).ToList(),
UserScriptHash = json["address"].AsString().ToScriptHash()
};
return nep5Balance;
}
}
Expand All @@ -41,16 +44,18 @@ public JObject ToJson()
JObject json = new JObject();
json["asset_hash"] = AssetHash.ToString();
json["amount"] = Amount.ToString();
json["last_updated_block"] = LastUpdatedBlock.ToString();
json["last_updated_block"] = LastUpdatedBlock;
return json;
}

public static RpcNep5Balance FromJson(JObject json)
{
RpcNep5Balance balance = new RpcNep5Balance();
balance.AssetHash = UInt160.Parse(json["asset_hash"].AsString());
balance.Amount = BigInteger.Parse(json["amount"].AsString());
balance.LastUpdatedBlock = uint.Parse(json["last_updated_block"].AsString());
RpcNep5Balance balance = new RpcNep5Balance
{
AssetHash = UInt160.Parse(json["asset_hash"].AsString()),
Amount = BigInteger.Parse(json["amount"].AsString()),
LastUpdatedBlock = (uint)json["last_updated_block"].AsNumber()
};
return balance;
}
}
Expand Down
Loading

0 comments on commit bbcfa56

Please sign in to comment.