Skip to content

Commit

Permalink
Add NEO SDK RPC module (neo-project#850)
Browse files Browse the repository at this point in the history
* Add NEO SDK based on RPC client

* add rpc interface methods for neo3

* update unit test

* add unit test

* Update TransactionHelper.cs

Changed for neo 3.0, not final yet.

* implement sdk rpc client methods

* backup files

* change class name

* remove uncompleted modules for pull request

* change json deserialize method with Neo JObject

* modified JSON implementation, added FromJson()

* more RPC change

* PR correction

* RPC module fix, remove newton.json

* fix

* fix getblock issue

* PR correction

* PR Correction

* PR Correction: rename RPC models

* PR Correction

* resolve conflicts

* Clean code

* Clean code

* Clean code

* Clean code

* Update RpcValidateAddressResult.cs

* Clean code

* PR correction

* Move test file to the right place
  • Loading branch information
chenquanyu authored and Tommo-L committed Jun 22, 2020
1 parent 1064e77 commit 81d6754
Show file tree
Hide file tree
Showing 23 changed files with 1,482 additions and 3 deletions.
540 changes: 540 additions & 0 deletions neo.UnitTests/Network/RPC/UT_RpcClient.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,13 @@ public JObject ToJson()
json["manifest"] = Manifest.ToJson();
return json;
}

public static ContractState FromJson(JObject json)
{
ContractState contractState = new ContractState();
contractState.Script = json["script"].AsString().HexToBytes();
contractState.Manifest = ContractManifest.FromJson(json["manifest"]);
return contractState;
}
}
}
11 changes: 11 additions & 0 deletions neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -106,6 +107,16 @@ public override JObject ToJson()
return json;
}

public new static Block FromJson(JObject json)
{
Block block = new Block();
BlockBase blockBase = block;
blockBase.FromJson(json);
block.ConsensusData = ConsensusData.FromJson(json["consensus_data"]);
block.Transactions = ((JArray)json["tx"]).Select(p => Transaction.FromJson(p)).ToArray();
return block;
}

public TrimmedBlock Trim()
{
return new TrimmedBlock
Expand Down
16 changes: 14 additions & 2 deletions neo/Network/P2P/Payloads/BlockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.Wallets;
using System;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads
{
Expand All @@ -32,7 +33,7 @@ public UInt256 Hash
}
}

public virtual int Size =>
public virtual int Size =>
sizeof(uint) + //Version
PrevHash.Size + //PrevHash
MerkleRoot.Size + //MerkleRoot
Expand All @@ -41,7 +42,7 @@ public UInt256 Hash
NextConsensus.Size + //NextConsensus
1 + //
Witness.Size; //Witness

Witness[] IVerifiable.Witnesses
{
get
Expand Down Expand Up @@ -111,6 +112,17 @@ public virtual JObject ToJson()
return json;
}

public void FromJson(JObject json)
{
Version = (uint)json["version"].AsNumber();
PrevHash = UInt256.Parse(json["previousblockhash"].AsString());
MerkleRoot = UInt256.Parse(json["merkleroot"].AsString());
Timestamp = (ulong)json["time"].AsNumber();
Index = (uint)json["index"].AsNumber();
NextConsensus = json["nextconsensus"].AsString().ToScriptHash();
Witness = ((JArray)json["witnesses"]).Select(p => Witness.FromJson(p)).FirstOrDefault();
}

public virtual bool Verify(Snapshot snapshot)
{
Header prev_header = snapshot.GetHeader(PrevHash);
Expand Down
10 changes: 10 additions & 0 deletions neo/Network/P2P/Payloads/ConsensusData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using System.Globalization;
using System.IO;

namespace Neo.Network.P2P.Payloads
Expand Down Expand Up @@ -45,5 +46,14 @@ public JObject ToJson()
json["nonce"] = Nonce.ToString("x16");
return json;
}

public static ConsensusData FromJson(JObject json)
{
ConsensusData block = new ConsensusData();
block.PrimaryIndex = (uint)json["primary"].AsNumber();
block.Nonce = ulong.Parse(json["nonce"].AsString(), NumberStyles.HexNumber);
return block;
}

}
}
14 changes: 13 additions & 1 deletion neo/Network/P2P/Payloads/Header.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Neo.Ledger;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Wallets;
using System;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads
{
Expand Down Expand Up @@ -51,5 +54,14 @@ public TrimmedBlock Trim()
Hashes = new UInt256[0]
};
}

public new static Header FromJson(JObject json)
{
Header header = new Header();
BlockBase blockBase = header;
blockBase.FromJson(json);
return header;
}

}
}
15 changes: 15 additions & 0 deletions neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ public JObject ToJson()
return json;
}

public static Transaction FromJson(JObject json)
{
Transaction tx = new Transaction();
tx.Version = byte.Parse(json["version"].AsString());
tx.Nonce = uint.Parse(json["nonce"].AsString());
tx.Sender = json["sender"].AsString().ToScriptHash();
tx.SystemFee = long.Parse(json["sys_fee"].AsString());
tx.NetworkFee = long.Parse(json["net_fee"].AsString());
tx.ValidUntilBlock = uint.Parse(json["valid_until_block"].AsString());
tx.Attributes = ((JArray)json["attributes"]).Select(p => TransactionAttribute.FromJson(p)).ToArray();
tx.Script = json["script"].AsString().HexToBytes();
tx.Witnesses = ((JArray)json["witnesses"]).Select(p => Witness.FromJson(p)).ToArray();
return tx;
}

bool IInventory.Verify(Snapshot snapshot)
{
return Verify(snapshot, Enumerable.Empty<Transaction>());
Expand Down
8 changes: 8 additions & 0 deletions neo/Network/P2P/Payloads/TransactionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ public JObject ToJson()
json["data"] = Data.ToHexString();
return json;
}

public static TransactionAttribute FromJson(JObject json)
{
TransactionAttribute transactionAttribute = new TransactionAttribute();
transactionAttribute.Usage = (TransactionAttributeUsage)(byte.Parse(json["usage"].AsString()));
transactionAttribute.Data = json["data"].AsString().HexToBytes();
return transactionAttribute;
}
}
}
8 changes: 8 additions & 0 deletions neo/Network/P2P/Payloads/Witness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@ public JObject ToJson()
json["verification"] = VerificationScript.ToHexString();
return json;
}

public static Witness FromJson(JObject json)
{
Witness witness = new Witness();
witness.InvocationScript = json["invocation"].AsString().HexToBytes();
witness.VerificationScript = json["verification"].AsString().HexToBytes();
return witness;
}
}
}
37 changes: 37 additions & 0 deletions neo/Network/RPC/Models/RpcBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;

namespace Neo.Network.RPC.Models
{
public class RpcBlock
{
public Block Block { get; set; }

public int? Confirmations { get; set; }

public UInt256 NextBlockHash { get; set; }

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

public static RpcBlock FromJson(JObject json)
{
RpcBlock block = new RpcBlock();
block.Block = Block.FromJson(json);
if (json["confirmations"] != null)
{
block.Confirmations = (int)json["confirmations"].AsNumber();
block.NextBlockHash = UInt256.Parse(json["nextblockhash"].AsString());
}
return block;
}
}
}
37 changes: 37 additions & 0 deletions neo/Network/RPC/Models/RpcBlockHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;

namespace Neo.Network.RPC.Models
{
public class RpcBlockHeader
{
public Header Header { get; set; }

public int? Confirmations { get; set; }

public UInt256 NextBlockHash { get; set; }

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

public static RpcBlockHeader FromJson(JObject json)
{
RpcBlockHeader block = new RpcBlockHeader();
block.Header = Header.FromJson(json);
if (json["confirmations"] != null)
{
block.Confirmations = (int)json["confirmations"].AsNumber();
block.NextBlockHash = UInt256.Parse(json["nextblockhash"].AsString());
}
return block;
}
}
}
69 changes: 69 additions & 0 deletions neo/Network/RPC/Models/RpcInvokeResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Neo.IO.Json;
using Newtonsoft.Json;
using System.Linq;

namespace Neo.Network.RPC.Models
{
public class RpcInvokeResult
{
[JsonProperty(PropertyName = "script")]
public string Script { get; set; }

[JsonProperty(PropertyName = "state")]
public string State { get; set; }

[JsonProperty(PropertyName = "gas_consumed")]
public string GasConsumed { get; set; }

[JsonProperty(PropertyName = "stack")]
public RpcStack[] Stack { get; set; }

[JsonProperty(PropertyName = "tx")]
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()));
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.GasConsumed = json["gas_consumed"].AsString();
invokeScriptResult.Tx = json["tx"].AsString();
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => RpcStack.FromJson(p)).ToArray();
return invokeScriptResult;
}
}

public class RpcStack
{
public string Type { get; set; }

public string Value { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["type"] = Type;
json["value"] = Value;
return json;
}

public static RpcStack FromJson(JObject json)
{
RpcStack stackJson = new RpcStack();
stackJson.Type = json["type"].AsString();
stackJson.Value = json["value"].AsString();
return stackJson;
}
}
}
57 changes: 57 additions & 0 deletions neo/Network/RPC/Models/RpcNep5Balances.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Neo.IO.Json;
using System.Linq;
using System.Numerics;

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

public RpcNep5Balance[] Balances { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["address"] = Address;
json["balance"] = Balances.Select(p => p.ToJson()).ToArray();
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();
return nep5Balance;
}
}

public class RpcNep5Balance
{
public UInt160 AssetHash { get; set; }

public BigInteger Amount { get; set; }

public uint LastUpdatedBlock { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
json["asset_hash"] = AssetHash.ToArray().ToHexString();
json["amount"] = Amount.ToString();
json["last_updated_block"] = LastUpdatedBlock.ToString();
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());
return balance;
}
}
}
Loading

0 comments on commit 81d6754

Please sign in to comment.