Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NEO SDK RPC module #850

Merged
merged 68 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
03c9a20
Add NEO SDK based on RPC client
chenquanyu Jun 17, 2019
4f84a63
add rpc interface methods for neo3
chenquanyu Jun 18, 2019
16c1471
update unit test
chenquanyu Jun 18, 2019
79523dd
add unit test
chenquanyu Jun 19, 2019
fe06992
Update TransactionHelper.cs
joeqian10 Jun 19, 2019
b5b2cd6
implement sdk rpc client methods
chenquanyu Jun 20, 2019
efd27a8
backup files
joeqian10 Jun 21, 2019
c78e965
change class name
chenquanyu Jun 21, 2019
cf7a184
Merge remote-tracking branch 'erik/master' into dev
chenquanyu Jun 21, 2019
66b269c
remove uncompleted modules for pull request
chenquanyu Jun 21, 2019
26b4334
Merge branch 'master' of https://github.com/chenquanyu/neo
joeqian10 Jun 21, 2019
8c0c37f
Merge branch 'master' into dev
vncoelho Jun 21, 2019
b2705be
Merge branch 'master' into dev
chenquanyu Jun 24, 2019
3ae6480
change json deserialize method with Neo JObject
chenquanyu Jun 24, 2019
5de69a0
Merge remote-tracking branch 'erik/master'
chenquanyu Jun 24, 2019
ca1048e
modified JSON implementation, added FromJson()
joeqian10 Jun 26, 2019
af6d7e0
Merge remote-tracking branch 'erik/master'
chenquanyu Jun 26, 2019
2f58594
more RPC change
joeqian10 Jun 26, 2019
d5fa4a7
PR correction
chenquanyu Jun 26, 2019
90b225c
RPC module fix, remove newton.json
joeqian10 Jun 26, 2019
a08d5e9
fix
joeqian10 Jun 26, 2019
238abd5
Merge remote-tracking branch 'erik/master'
chenquanyu Jun 26, 2019
7c193b5
Merge branch 'dev' into master
chenquanyu Jun 26, 2019
71dad15
Merge pull request #2 from chenquanyu/master
chenquanyu Jun 26, 2019
e2b187b
fix getblock issue
chenquanyu Jun 27, 2019
0d58ad6
merge
chenquanyu Jun 27, 2019
0d5f720
Merge remote-tracking branch 'erik/master'
chenquanyu Jun 27, 2019
6ecf691
Merge pull request #3 from chenquanyu/master
chenquanyu Jun 27, 2019
cfa4622
Merge branch 'master' into dev
shargon Jun 28, 2019
663111e
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 1, 2019
a8df8e7
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 5, 2019
8cb2413
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 8, 2019
789eb26
Merge branch 'master' into dev
shargon Jul 8, 2019
3064b9e
PR correction
chenquanyu Jul 8, 2019
8d27ccb
Merge branch 'master' into dev
chenquanyu Jul 8, 2019
ad33da2
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 8, 2019
ec12ede
Merge branch 'dev' into master
chenquanyu Jul 8, 2019
afae7b1
Merge pull request #4 from chenquanyu/master
chenquanyu Jul 8, 2019
53d9ef3
PR Correction
chenquanyu Jul 12, 2019
dc46b10
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 12, 2019
058819b
Merge pull request #5 from chenquanyu/master
chenquanyu Jul 12, 2019
4fd233b
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 17, 2019
4710d14
PR Correction: rename RPC models
chenquanyu Jul 17, 2019
4b630b2
Merge pull request #6 from chenquanyu/master
chenquanyu Jul 17, 2019
403b38d
Merge branch 'master' into dev
vncoelho Jul 17, 2019
5920a48
Merge remote-tracking branch 'erik/master'
chenquanyu Jul 18, 2019
10a0597
PR Correction
chenquanyu Jul 18, 2019
a289067
Merge branch 'dev' into master
chenquanyu Jul 18, 2019
664ca6c
resolve conflicts
chenquanyu Jul 18, 2019
51088ee
Merge pull request #7 from chenquanyu/master
chenquanyu Jul 18, 2019
2442fe7
Clean code
shargon Jul 25, 2019
137ddb5
Clean code
shargon Jul 25, 2019
ac169c6
Clean code
shargon Jul 25, 2019
8ff77b6
Clean code
shargon Jul 25, 2019
4bc742d
Update RpcValidateAddressResult.cs
shargon Jul 25, 2019
b958970
Clean code
shargon Jul 25, 2019
ea0b6b4
Merge branch 'master' into dev
shargon Jul 25, 2019
b01b353
Merge pull request #8 from chenquanyu/dev
chenquanyu Jul 25, 2019
0d9b99a
PR correction
chenquanyu Jul 25, 2019
4d3e3a2
Merge pull request #9 from chenquanyu/master
chenquanyu Jul 25, 2019
73a53ac
Merge branch 'master' into dev
chenquanyu Jul 26, 2019
eee837a
Merge branch 'master' into dev
chenquanyu Jul 30, 2019
7da09eb
Merge remote-tracking branch 'erik/master' into dev
chenquanyu Jul 30, 2019
2214846
Merge branch 'dev'
chenquanyu Jul 30, 2019
51e30f3
Merge branch 'master' into dev
erikzhang Jul 30, 2019
881af0b
Merge branch 'dev' of https://github.com/chenquanyu/neo into dev
chenquanyu Jul 30, 2019
b7403ec
Move test file to the right place
chenquanyu Jul 30, 2019
c5c0de1
Merge branch 'master' into dev
chenquanyu Jul 30, 2019
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
540 changes: 540 additions & 0 deletions neo.UnitTests/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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

((BlockBase)block).FromJson

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)
chenquanyu marked this conversation as resolved.
Show resolved Hide resolved
{
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

((BlockBase)header).FromJson

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lock9 what is wrong here?

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the easiest way to recover Transaction from JSON RPC repsonse is to set verbose to false.

bool verbose = _params.Count >= 2 && _params[1].AsBoolean();

Then you can easily get the Transaction by deserializing from the hex.

The block is the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Erik,
The json constains more information when verbose is true
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But in SDK, you just receive it and convert it into a Transaction. So there's no difference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikzhang dalao, so we only need to provide the GetRawTransaction() method with verbose set to false inside, and let the developers using this method to decide if they need to deserialize Transaction from the HEX, am I understanding this right?
image
Or should I provide another GetRawTransaction() method to deserialize the HEX like following code?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have wrapped the Transaction to SDK_Transaction to contain more infotrmation, or we can just keep one method to get Transaction:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SDK is intended to be used mainly by code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is an SDK for developers.

{
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