Skip to content

Commit

Permalink
Merge pull request #5 from chenquanyu/master
Browse files Browse the repository at this point in the history
PR correction
  • Loading branch information
chenquanyu authored Jul 12, 2019
2 parents afae7b1 + dc46b10 commit 058819b
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 48 deletions.
2 changes: 1 addition & 1 deletion neo.UnitTests/SDK/UT_RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void TestInvokeFunction()
response["result"] = json;
MockResponse(response.ToString());

var result = rpc.InvokeFunction("af7c7328eee5a275a3bcaee2bf0cf662b5e739be", "balanceOf", new[] { new SDK_StackJson { Type = "Hash160", Value = "91b83e96f2a7c4fdf0c1688441ec61986c7cae26" } });
var result = rpc.InvokeFunction("af7c7328eee5a275a3bcaee2bf0cf662b5e739be", "balanceOf", new[] { new SDK_Stack { Type = "Hash160", Value = "91b83e96f2a7c4fdf0c1688441ec61986c7cae26" } });
Assert.AreEqual(json.ToString(), result.ToJson().ToString());
}

Expand Down
54 changes: 54 additions & 0 deletions neo.UnitTests/UT_FifoSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Caching;
using System.Linq;

namespace Neo.UnitTests
{
[TestClass]
public class UT_FifoSet
{
[TestMethod]
public void FifoSetTest()
{
var a = UInt256.Zero;
var b = new UInt256();
var c = new UInt256(new byte[32] {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01
});

var set = new FIFOSet<UInt256>(3);

Assert.IsTrue(set.Add(a));
Assert.IsFalse(set.Add(a));
Assert.IsFalse(set.Add(b));
Assert.IsTrue(set.Add(c));

CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a, c });

var d = new UInt256(new byte[32] {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x02
});

// Testing Fifo max size
Assert.IsTrue(set.Add(d));
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a, c, d });

var e = new UInt256(new byte[32] {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x03
});

Assert.IsTrue(set.Add(e));
Assert.IsFalse(set.Add(e));
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { c, d, e });
}
}
}
20 changes: 16 additions & 4 deletions neo.UnitTests/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Neo.VM;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System.Threading.Tasks;

namespace Neo.UnitTests
{
Expand Down Expand Up @@ -104,8 +105,14 @@ public void FeeIsMultiSigContract()
using (var unlockA = walletA.Unlock("123"))
using (var unlockB = walletB.Unlock("123"))
{
var a = walletA.CreateAccount();
var b = walletB.CreateAccount();
var ta = new Task<WalletAccount>(() => walletA.CreateAccount());
var tb = new Task<WalletAccount>(() => walletB.CreateAccount());
ta.Start();
tb.Start();
Task.WaitAll(ta, tb);

var a = ta.Result;
var b = tb.Result;

var multiSignContract = Contract.CreateMultiSigContract(2,
new ECPoint[]
Expand All @@ -114,8 +121,13 @@ public void FeeIsMultiSigContract()
b.GetKey().PublicKey
});

var acc = walletA.CreateAccount(multiSignContract, a.GetKey());
acc = walletB.CreateAccount(multiSignContract, b.GetKey());
ta = new Task<WalletAccount>(() => walletA.CreateAccount(multiSignContract, a.GetKey()));
tb = new Task<WalletAccount>(() => walletB.CreateAccount(multiSignContract, b.GetKey()));
ta.Start();
tb.Start();
Task.WaitAll(ta, tb);

var acc = tb.Result;

// Fake balance

Expand Down
18 changes: 14 additions & 4 deletions neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,21 @@ public static ECPoint DeserializeFrom(BinaryReader reader, ECCurve curve)
{
case 0x02:
case 0x03:
reader.Read(buffer, 1, expectedLength);
return DecodePoint(buffer.Take(1 + expectedLength).ToArray(), curve);
{
if (reader.Read(buffer, 1, expectedLength) != expectedLength)
{
throw new FormatException();
}
return DecodePoint(buffer.Take(1 + expectedLength).ToArray(), curve);
}
case 0x04:
reader.Read(buffer, 1, expectedLength * 2);
return DecodePoint(buffer, curve);
{
if (reader.Read(buffer, 1, expectedLength * 2) != expectedLength * 2)
{
throw new FormatException();
}
return DecodePoint(buffer, curve);
}
default:
throw new FormatException("Invalid point encoding " + buffer[0]);
}
Expand Down
2 changes: 1 addition & 1 deletion neo/IO/Caching/FIFOSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ExceptWith(IEnumerable<UInt256> hashes)

public IEnumerator<T> GetEnumerator()
{
var entries = dictionary.Values.Cast<T>().ToArray();
var entries = dictionary.Keys.Cast<T>().ToArray();
foreach (var entry in entries) yield return entry;
}

Expand Down
3 changes: 2 additions & 1 deletion neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ internal void UpdatePoolForBlockPersisted(Block block, Snapshot snapshot)
if (policyChanged)
{
foreach (PoolItem item in _unverifiedSortedTransactions.Reverse())
_system.Blockchain.Tell(item.Tx, ActorRefs.NoSender);
if(item.Tx.FeePerByte >= _feePerByte)
_system.Blockchain.Tell(item.Tx, ActorRefs.NoSender);
_unverifiedTransactions.Clear();
_unverifiedSortedTransactions.Clear();
}
Expand Down
4 changes: 2 additions & 2 deletions neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public override JObject ToJson()
return json;
}

public static Block FromJson(JObject json)
public new static Block FromJson(JObject json)
{
Block block = new Block();
block.PraseFromJson(json);
(block as BlockBase).FromJson(json);
block.ConsensusData = ConsensusData.FromJson(json["consensus_data"]);
block.Transactions = ((JArray)json["tx"]).Select(p => Transaction.FromJson(p)).ToArray();
return block;
Expand Down
2 changes: 1 addition & 1 deletion neo/Network/P2P/Payloads/BlockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public virtual JObject ToJson()
return json;
}

public void PraseFromJson(JObject json)
public void FromJson(JObject json)
{
Version = (uint)json["version"].AsNumber();
PrevHash = UInt256.Parse(json["previousblockhash"].AsString());
Expand Down
4 changes: 2 additions & 2 deletions neo/Network/P2P/Payloads/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public TrimmedBlock Trim()
};
}

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

Expand Down
7 changes: 4 additions & 3 deletions neo/SDK/RPC/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ public void Dispose()
httpClient?.Dispose();
}

public async Task<JObject> SendAsync(RPCRequest request)
public async Task<RPCResponse> SendAsync(RPCRequest request)
{
var requestJson = request.ToJson().ToString();
var result = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(requestJson, Encoding.UTF8));
var content = await result.Content.ReadAsStringAsync();
var response = RPCResponse.FromJson(JObject.Parse(content));
response.RawResponse = content;

if (response.Error != null)
{
throw new NeoSdkException(response.Error.Code, response.Error.Message, response.Error.Data);
}

return response.Result;
return response;
}

public JObject Send(RPCRequest request)
public RPCResponse Send(RPCRequest request)
{
try
{
Expand Down
9 changes: 2 additions & 7 deletions neo/SDK/RPC/IRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface IRpcClient
/// Returns the result after calling a smart contract at scripthash with the given operation and parameters.
/// This RPC call does not affect the blockchain in any way.
/// </summary>
SDK_InvokeScriptResult InvokeFunction(string address, string function, SDK_StackJson[] stacks);
SDK_InvokeScriptResult InvokeFunction(string address, string function, SDK_Stack[] stacks);

/// <summary>
/// Returns the result after passing a script through the VM.
Expand All @@ -141,11 +141,6 @@ public interface IRpcClient
/// Verifies that the address is a correct NEO address.
/// </summary>
SDK_ValidateAddressResult ValidateAddress(string address);

/// <summary>
/// Returns the balance of all NEP-5 assets in the specified address.
/// </summary>
SDK_Nep5Balances GetNep5Balances(string address);


}
}
12 changes: 7 additions & 5 deletions neo/SDK/RPC/Model/RPCResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ namespace Neo.SDK.RPC.Model
public class RPCResponse
{
public int? Id { get; set; }

public string Jsonrpc { get; set; }

public RPCResponseError Error { get; set; }

public JObject Result { get; set; }

public string RawResponse { get; set; }

/// <summary>
/// Parse from json
/// </summary>
Expand Down Expand Up @@ -49,9 +51,9 @@ public JObject ToJson()
public class RPCResponseError
{
public int Code { get; set; }

public string Message { get; set; }

public JObject Data { get; set; }

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions neo/SDK/RPC/Model/SDK_InvokeScriptResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SDK_InvokeScriptResult
public string GasConsumed { get; set; }

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

[JsonProperty(PropertyName = "tx")]
public string Tx { get; set; }
Expand All @@ -39,12 +39,12 @@ public static SDK_InvokeScriptResult FromJson(JObject json)
invokeScriptResult.State = json["state"].AsString();
invokeScriptResult.GasConsumed = json["gas_consumed"].AsString();
invokeScriptResult.Tx = json["tx"].AsString();
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => SDK_StackJson.FromJson(p)).ToArray();
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => SDK_Stack.FromJson(p)).ToArray();
return invokeScriptResult;
}
}

public class SDK_StackJson
public class SDK_Stack
{
public string Type { get; set; }

Expand All @@ -58,9 +58,9 @@ public JObject ToJson()
return json;
}

public static SDK_StackJson FromJson(JObject json)
public static SDK_Stack FromJson(JObject json)
{
SDK_StackJson stackJson = new SDK_StackJson();
SDK_Stack stackJson = new SDK_Stack();
stackJson.Type = json["type"].AsString();
stackJson.Value = json["value"].AsString();
return stackJson;
Expand Down
9 changes: 2 additions & 7 deletions neo/SDK/RPC/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private JObject RpcSend(string method, params JObject[] paraArgs)
Method = method,
Params = paraArgs.Select(p => p).ToArray()
};
return rpcHelper.Send(request);
return rpcHelper.Send(request).Result;
}

public string GetBestBlockHash()
Expand Down Expand Up @@ -148,7 +148,7 @@ public SDK_Version GetVersion()
return SDK_Version.FromJson(RpcSend("getversion"));
}

public SDK_InvokeScriptResult InvokeFunction(string address, string function, SDK_StackJson[] stacks)
public SDK_InvokeScriptResult InvokeFunction(string address, string function, SDK_Stack[] stacks)
{
return SDK_InvokeScriptResult.FromJson(RpcSend("invokefunction", address, function, stacks.Select(p => p.ToJson()).ToArray()));
}
Expand Down Expand Up @@ -178,10 +178,5 @@ public SDK_ValidateAddressResult ValidateAddress(string address)
return SDK_ValidateAddressResult.FromJson(RpcSend("validateaddress", address));
}

public SDK_Nep5Balances GetNep5Balances(string address)
{
return SDK_Nep5Balances.FromJson(RpcSend("getnep5balances", address));
}

}
}
12 changes: 7 additions & 5 deletions neo/UIntBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Neo
{

/// <summary>
/// Base class for little-endian unsigned integers. Two classes inherit from this: UInt160 and UInt256.
/// Only basic comparison/serialization are proposed for these classes. For arithmetic purposes, use BigInteger class.
Expand All @@ -15,7 +14,7 @@ public abstract class UIntBase : IEquatable<UIntBase>, ISerializable
/// <summary>
/// Storing unsigned int in a little-endian byte array.
/// </summary>
private byte[] data_bytes;
private readonly byte[] data_bytes;

/// <summary>
/// Number of bytes of the unsigned int.
Expand Down Expand Up @@ -44,7 +43,10 @@ protected UIntBase(int bytes, byte[] value)
/// </summary>
void ISerializable.Deserialize(BinaryReader reader)
{
reader.Read(data_bytes, 0, data_bytes.Length);
if (reader.Read(data_bytes, 0, data_bytes.Length) != data_bytes.Length)
{
throw new FormatException();
}
}

/// <summary>
Expand All @@ -53,7 +55,7 @@ void ISerializable.Deserialize(BinaryReader reader)
/// </summary>
public bool Equals(UIntBase other)
{
if (ReferenceEquals(other, null))
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
Expand All @@ -68,7 +70,7 @@ public bool Equals(UIntBase other)
/// </summary>
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
if (obj is null)
return false;
if (!(obj is UIntBase))
return false;
Expand Down

0 comments on commit 058819b

Please sign in to comment.