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

PR Correction2 #3

Merged
merged 6 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 29 additions & 22 deletions neo.UnitTests/SDK/UT_RpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Protected;
using Neo.IO;
Expand Down Expand Up @@ -105,13 +105,7 @@ public void TestGetBlock()
{
// create block
var block = new Block();
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
Transaction[] transactionsVal;
TestUtils.SetupBlockWithValues(block, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal, out transactionsVal, 0);
TestUtils.SetupBlockWithValues(block, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal, out Transaction[] transactionsVal, 0);

block.Transactions = new[]
{
Expand All @@ -121,20 +115,25 @@ public void TestGetBlock()
};

JObject json = block.ToJson();
json["confirmations"] = 20;
json["nextblockhash"] = "773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e";

JObject response = RpcServer.CreateResponse(1);
response["result"] = json;
MockResponse(response.ToString());

var result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString());
Assert.IsNull(result.Confirmations);
Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length);
Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString());

// verbose with confirmations
json["confirmations"] = 20;
json["nextblockhash"] = "773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e";
MockResponse(response.ToString());
result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString());
Assert.AreEqual(20, result.Confirmations);
Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length);
Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString());

}

[TestMethod]
Expand Down Expand Up @@ -177,18 +176,19 @@ public void TestGetBlockHeader()
TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal);

JObject json = header.ToJson();
json["confirmations"] = 20;

json["nextblockhash"] = "4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2";

JObject response = RpcServer.CreateResponse(1);
response["result"] = json;
MockResponse(response.ToString());

var result = rpc.GetBlockHeader("100");

Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString());
Assert.IsNull(result.Confirmations);

json["confirmations"] = 20;
json["nextblockhash"] = "4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2";
MockResponse(response.ToString());
result = rpc.GetBlockHeader("100");
Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString());
Assert.AreEqual(20, result.Confirmations);
}

Expand Down Expand Up @@ -283,7 +283,6 @@ public void TestGetRawMempool()
""0xb488ad25eb474f89d5ca3f985cc047ca96bc7373a6d3da8c0f192722896c1cd7"",
""0xf86f6f2c08fbf766ebe59dc84bc3b8829f1053f0a01deb26bf7960d99fa86cd6""
]");

MockResponse(response.ToString());

var result = rpc.GetRawMempool();
Expand All @@ -306,7 +305,6 @@ public void TestGetRawMempoolBoth()
Assert.AreEqual((uint)65535, result.Height);
Assert.AreEqual("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e", result.Verified[0]);
Assert.AreEqual("0xf86f6f2c08fbf766ebe59dc84bc3b8829f1053f0a01deb26bf7960d99fa86cd6", result.UnVerified[1]);

}

[TestMethod]
Expand All @@ -317,6 +315,8 @@ public void TestGetRawTransactionHex()
JObject response = RpcServer.CreateResponse(1);
response["result"] = json;
MockResponse(response.ToString());

//var result = rpc.GetRawTransactionHex("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e");
var result = rpc.GetRawTransactionHex(TestUtils.GetTransaction().Hash.ToString());
Assert.AreEqual(json, result);
}
Expand All @@ -331,8 +331,17 @@ public void TestGetRawTransaction()
MockResponse(response.ToString());

var result = rpc.GetRawTransaction("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e");
Assert.AreEqual(transaction.Hash, result.Transaction.Hash);
Assert.AreEqual(json.ToString(), result.ToJson().ToString());

json["blockhash"] = UInt256.Zero.ToString();
json["confirmations"] = 100;
json["blocktime"] = 10;
MockResponse(response.ToString());

Assert.AreEqual(transaction.Hash, result.Hash);
result = rpc.GetRawTransaction("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e");
Assert.AreEqual(transaction.Hash, result.Transaction.Hash);
Assert.AreEqual(100, result.Confirmations);
Assert.AreEqual(json.ToString(), result.ToJson().ToString());
}

Expand Down Expand Up @@ -424,7 +433,6 @@ public void TestInvokeFunction()
}
],
""tx"":""d101361426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf000000000000000000000000""

}");
JObject response = RpcServer.CreateResponse(1);
response["result"] = json;
Expand Down Expand Up @@ -515,5 +523,4 @@ public void TestValidateAddress()
Assert.AreEqual(json.ToString(), result.ToJson().ToString());
}
}

}
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ internal static void CheckValidator(ECPoint eCPoint, DataCache<StorageKey, Stora

internal static void CheckBalance(byte[] account, DataCache<StorageKey, StorageItem>.Trackable trackable, BigInteger balance, BigInteger height, ECPoint[] votes)
{
var st = (VM.Types.Struct)trackable.Item.Value.DeserializeStackItem(3);
var st = (VM.Types.Struct)trackable.Item.Value.DeserializeStackItem(3, 32);

st.Count.Should().Be(3);
st.Select(u => u.GetType()).ToArray().Should().BeEquivalentTo(new Type[] { typeof(VM.Types.Integer), typeof(VM.Types.Integer), typeof(VM.Types.ByteArray) }); // Balance
Expand Down
1 change: 1 addition & 0 deletions neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static byte[] Decode(string input)
}
byte[] tmp = new byte[bytes.Length - (stripSignByte ? 1 : 0) + leadingZeros];
Array.Copy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.Length - leadingZeros);
Array.Clear(bytes, 0, bytes.Length);
return tmp;
}

Expand Down
8 changes: 6 additions & 2 deletions neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public static byte[] Base58CheckDecode(this string input)
byte[] checksum = buffer.Sha256(0, buffer.Length - 4).Sha256();
if (!buffer.Skip(buffer.Length - 4).SequenceEqual(checksum.Take(4)))
throw new FormatException();
return buffer.Take(buffer.Length - 4).ToArray();
var ret = buffer.Take(buffer.Length - 4).ToArray();
Array.Clear(buffer, 0, buffer.Length);
return ret;
}

public static string Base58CheckEncode(this byte[] data)
Expand All @@ -88,7 +90,9 @@ public static string Base58CheckEncode(this byte[] data)
byte[] buffer = new byte[data.Length + 4];
Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
Buffer.BlockCopy(checksum, 0, buffer, data.Length, 4);
return Base58.Encode(buffer);
var ret = Base58.Encode(buffer);
Array.Clear(buffer, 0, buffer.Length);
return ret;
}

public static byte[] RIPEMD160(this IEnumerable<byte> value)
Expand Down
21 changes: 20 additions & 1 deletion neo/IO/Caching/FIFOSet.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace Neo.IO.Caching
{
internal class FIFOSet<T> where T : IEquatable<T>
internal class FIFOSet<T> : IEnumerable<T> where T : IEquatable<T>
{
private readonly int maxCapacity;
private readonly int removeCount;
Expand Down Expand Up @@ -37,5 +40,21 @@ public bool Add(T item)
dictionary.Add(item, null);
return true;
}

public void ExceptWith(IEnumerable<UInt256> hashes)
{
foreach (var hash in hashes)
{
dictionary.Remove(hash);
}
}

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

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
2 changes: 0 additions & 2 deletions neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ public JObject ToJson()
public static Transaction FromJson(JObject json)
{
Transaction tx = new Transaction();
//tx._hash = UInt256.Parse(json["hash"].AsString());
//tx.Size = int.Parse(json["size"].AsString());
tx.Version = byte.Parse(json["version"].AsString());
tx.Nonce = uint.Parse(json["nonce"].AsString());
tx.Sender = json["sender"].AsString().ToScriptHash();
Expand Down
9 changes: 8 additions & 1 deletion neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Akka.Actor;
using Akka.Configuration;
using Neo.IO.Actors;
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using System;
Expand All @@ -24,7 +25,13 @@ private class Timer { }

private readonly NeoSystem system;
private const int MaxConncurrentTasks = 3;
private readonly HashSet<UInt256> knownHashes = new HashSet<UInt256>();

/// <summary>
/// The limit `Blockchain.Singleton.MemPool.Capacity * 2` was the same value used in ProtocolHandler
/// </summary>
private static readonly int MaxCachedHashes = Blockchain.Singleton.MemPool.Capacity * 2;
private readonly FIFOSet<UInt256> knownHashes = new FIFOSet<UInt256>(MaxCachedHashes);

private readonly Dictionary<UInt256, int> globalTasks = new Dictionary<UInt256, int>();
private readonly Dictionary<IActorRef, TaskSession> sessions = new Dictionary<IActorRef, TaskSession>();
private readonly ICancelable timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimerInterval, TimerInterval, Context.Self, new Timer(), ActorRefs.NoSender);
Expand Down
2 changes: 1 addition & 1 deletion neo/SDK/RPC/HttpService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Neo.IO.Json;
using Neo.IO.Json;
using Neo.SDK.RPC.Model;
using System;
using System.Net.Http;
Expand Down
6 changes: 3 additions & 3 deletions neo/SDK/RPC/IRpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Neo.Ledger;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC;
using Neo.SDK.RPC.Model;
Expand Down Expand Up @@ -88,7 +88,7 @@ public interface IRpcClient
/// Returns the corresponding transaction information, based on the specified hash value.
/// verbose = true
/// </summary>
Transaction GetRawTransaction(string txid);
SDK_Transaction GetRawTransaction(string txid);

/// <summary>
/// Returns the stored value, according to the contract script hash and the stored key.
Expand All @@ -115,7 +115,7 @@ public interface IRpcClient
/// This RPC call does not affect the blockchain in any way.
/// </summary>
SDK_InvokeScriptResult InvokeFunction(string address, string function, SDK_StackJson[] stacks);

/// <summary>
/// Returns the result after passing a script through the VM.
/// This RPC call does not affect the blockchain in any way.
Expand Down
11 changes: 0 additions & 11 deletions neo/SDK/RPC/IRpcService.cs

This file was deleted.

114 changes: 0 additions & 114 deletions neo/SDK/RPC/Model/GetBlock.cs

This file was deleted.

Loading