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

Fix RpcNep5Tracker #125

Merged
merged 9 commits into from
Aug 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions RpcNep5Tracker/Nep5TransferKey.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using Neo.IO;
using System;
using System.IO;
using Neo.IO;

namespace Neo.Plugins
{
public class Nep5TransferKey : IComparable<Nep5TransferKey>, IEquatable<Nep5TransferKey>, ISerializable
{
public readonly UInt160 UserScriptHash;
public ulong Timestamp { get; private set; }
public ulong TimestampMS { get; private set; }
public readonly UInt160 AssetScriptHash;
public ushort BlockXferNotificationIndex { get; private set; }

public int Size => 20 + sizeof(uint) + 20 + sizeof(ushort);
public int Size =>
UInt160.Length + //UserScriptHash
sizeof(ulong) + //TimestampMS
UInt160.Length + //AssetScriptHash
sizeof(ushort); //BlockXferNotificationIndex

public Nep5TransferKey() : this(new UInt160(), 0, new UInt160(), 0)
{
Expand All @@ -22,7 +26,7 @@ public Nep5TransferKey(UInt160 userScriptHash, ulong timestamp, UInt160 assetScr
if (userScriptHash is null || assetScriptHash is null)
throw new ArgumentNullException();
UserScriptHash = userScriptHash;
Timestamp = timestamp;
TimestampMS = timestamp;
AssetScriptHash = assetScriptHash;
BlockXferNotificationIndex = xferIndex;
}
Expand All @@ -33,7 +37,7 @@ public int CompareTo(Nep5TransferKey other)
if (ReferenceEquals(this, other)) return 0;
int result = UserScriptHash.CompareTo(other.UserScriptHash);
if (result != 0) return result;
int result2 = Timestamp.CompareTo(other.Timestamp);
int result2 = TimestampMS.CompareTo(other.TimestampMS);
if (result2 != 0) return result2;
int result3 = AssetScriptHash.CompareTo(other.AssetScriptHash);
if (result3 != 0) return result3;
Expand All @@ -45,7 +49,7 @@ public bool Equals(Nep5TransferKey other)
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return UserScriptHash.Equals(other.UserScriptHash)
&& Timestamp.Equals(other.Timestamp) && AssetScriptHash.Equals(other.AssetScriptHash)
&& TimestampMS.Equals(other.TimestampMS) && AssetScriptHash.Equals(other.AssetScriptHash)
&& BlockXferNotificationIndex.Equals(other.BlockXferNotificationIndex);
}

Expand All @@ -59,7 +63,7 @@ public override int GetHashCode()
unchecked
{
var hashCode = UserScriptHash.GetHashCode();
hashCode = (hashCode * 397) ^ Timestamp.GetHashCode();
hashCode = (hashCode * 397) ^ TimestampMS.GetHashCode();
hashCode = (hashCode * 397) ^ AssetScriptHash.GetHashCode();
hashCode = (hashCode * 397) ^ BlockXferNotificationIndex.GetHashCode();
return hashCode;
Expand All @@ -69,7 +73,7 @@ public override int GetHashCode()
public void Serialize(BinaryWriter writer)
{
writer.Write(UserScriptHash);
var timestampBytes = BitConverter.GetBytes(Timestamp);
var timestampBytes = BitConverter.GetBytes(TimestampMS);
if (BitConverter.IsLittleEndian) Array.Reverse(timestampBytes);
writer.Write(timestampBytes);
writer.Write(AssetScriptHash);
Expand All @@ -79,12 +83,12 @@ public void Serialize(BinaryWriter writer)
public void Deserialize(BinaryReader reader)
{
((ISerializable)UserScriptHash).Deserialize(reader);
byte[] timestampBytes = new byte[sizeof(uint)];
reader.Read(timestampBytes, 0, sizeof(uint));
byte[] timestampBytes = new byte[sizeof(ulong)];
reader.Read(timestampBytes, 0, timestampBytes.Length);
if (BitConverter.IsLittleEndian) Array.Reverse(timestampBytes);
Timestamp = BitConverter.ToUInt64(timestampBytes, 0);
TimestampMS = BitConverter.ToUInt64(timestampBytes, 0);
((ISerializable)AssetScriptHash).Deserialize(reader);
BlockXferNotificationIndex = reader.ReadUInt16();
}
}
}
}
23 changes: 12 additions & 11 deletions RpcNep5Tracker/RpcNep5Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ private void ResetBatch()
private void RecordTransferHistory(Snapshot snapshot, UInt160 scriptHash, UInt160 from, UInt160 to, BigInteger amount, UInt256 txHash, ref ushort transferIndex)
{
if (!_shouldTrackHistory) return;

Header header = snapshot.GetHeader(snapshot.Height);

if (_recordNullAddressHistory || from != UInt160.Zero)
{
_transfersSent.Add(new Nep5TransferKey(from,
snapshot.GetHeader(snapshot.Height).Timestamp, scriptHash, transferIndex),
_transfersSent.Add(new Nep5TransferKey(from, header.Timestamp, scriptHash, transferIndex),
new Nep5Transfer
{
Amount = amount,
Expand All @@ -81,8 +83,7 @@ private void RecordTransferHistory(Snapshot snapshot, UInt160 scriptHash, UInt16

if (_recordNullAddressHistory || to != UInt160.Zero)
{
_transfersReceived.Add(new Nep5TransferKey(to,
snapshot.GetHeader(snapshot.Height).Timestamp, scriptHash, transferIndex),
_transfersReceived.Add(new Nep5TransferKey(to, header.Timestamp, scriptHash, transferIndex),
new Nep5Transfer
{
Amount = amount,
Expand All @@ -102,7 +103,7 @@ private void RecordTransferHistory(Snapshot snapshot, UInt160 scriptHash, UInt16
// Event name should be encoded as a byte array.
if (!(stateItems[0] is VM.Types.ByteArray)) return;
var eventName = Encoding.UTF8.GetString(stateItems[0].GetByteArray());
if (eventName != "transfer") return;
if (eventName != "Transfer") return;
if (stateItems.Count < 4) return;

if (!(stateItems[1] is null) && !(stateItems[1] is VM.Types.ByteArray))
Expand Down Expand Up @@ -166,7 +167,7 @@ public void OnPersist(Snapshot snapshot, IReadOnlyList<Blockchain.ApplicationExe
script = sb.ToArray();
}

ApplicationEngine engine = ApplicationEngine.Run(script, snapshot);
ApplicationEngine engine = ApplicationEngine.Run(script, snapshot, extraGAS: 100000000);
if (engine.State.HasFlag(VMState.FAULT)) continue;
if (engine.ResultStack.Count <= 0) continue;
nep5BalancePair.Value.Balance = engine.ResultStack.Pop().GetBigInteger();
Expand Down Expand Up @@ -199,7 +200,7 @@ public bool ShouldThrowExceptionFromCommit(Exception ex)
return true;
}

private void AddTransfers(byte dbPrefix, UInt160 userScriptHash, uint startTime, uint endTime,
private void AddTransfers(byte dbPrefix, UInt160 userScriptHash, ulong startTime, ulong endTime,
JArray parentJArray)
{
var prefix = new[] { dbPrefix }.Concat(userScriptHash.ToArray()).ToArray();
Expand All @@ -220,7 +221,7 @@ public bool ShouldThrowExceptionFromCommit(Exception ex)
{
if (++resultCount > _maxResults) break;
JObject transfer = new JObject();
transfer["timestamp"] = transferPair.Key.Timestamp;
transfer["timestamp"] = transferPair.Key.TimestampMS;
transfer["asset_hash"] = transferPair.Key.AssetScriptHash.ToArray().Reverse().ToHexString();
transfer["transfer_address"] = transferPair.Value.UserScriptHash.ToAddress();
transfer["amount"] = transferPair.Value.Amount.ToString();
Expand All @@ -240,9 +241,9 @@ private JObject GetNep5Transfers(JArray _params)
{
UInt160 userScriptHash = GetScriptHashFromParam(_params[0].AsString());
// If start time not present, default to 1 week of history.
uint startTime = _params.Count > 1 ? (uint)_params[1].AsNumber() :
(DateTime.UtcNow - TimeSpan.FromDays(7)).ToTimestamp();
uint endTime = _params.Count > 2 ? (uint)_params[2].AsNumber() : DateTime.UtcNow.ToTimestamp();
ulong startTime = _params.Count > 1 ? (ulong)_params[1].AsNumber() :
(DateTime.UtcNow - TimeSpan.FromDays(7)).ToTimestampMS();
ulong endTime = _params.Count > 2 ? (ulong)_params[2].AsNumber() : DateTime.UtcNow.ToTimestampMS();

if (endTime < startTime) throw new RpcException(-32602, "Invalid params");

Expand Down
2 changes: 1 addition & 1 deletion RpcNep5Tracker/RpcNep5Tracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00151" />
<PackageReference Include="Neo" Version="3.0.0-CI00160" />
</ItemGroup>
</Project>