-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init * check balance * Update src/TokensTracker/Storage/Nep17BalanceKey.cs * Update src/TokensTracker/Storage/Nep11BalanceKey.cs * fix * update Neo to v3.0.3-CI01319 * nep-17 check * nep17 check * Update OracleService.csproj * merge * Fix csproj * Add classes * refac * format * transferaddress * Add _enabledTrackers * Move namespace * refac * format * Optimize * Clean code * move to one line Co-authored-by: Shargon <shargon@gmail.com> Co-authored-by: superboyiii <573504781@qq.com> Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> Co-authored-by: Erik Zhang <erik@neo.org>
- Loading branch information
1 parent
01db34e
commit f949f2d
Showing
15 changed files
with
1,206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using Neo.IO; | ||
using Neo.Persistence; | ||
using Neo.VM.Types; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
public static class Extensions | ||
{ | ||
public static bool NotNull(this StackItem item) | ||
{ | ||
return !item.IsNull; | ||
} | ||
|
||
public static string ToBase64(this ReadOnlySpan<byte> item) | ||
{ | ||
return item == null ? String.Empty : Convert.ToBase64String(item); | ||
} | ||
|
||
public static int GetVarSize(this ByteString item) | ||
{ | ||
var length = item.GetSpan().Length; | ||
return IO.Helper.GetVarSize(length) + length; | ||
} | ||
|
||
public static int GetVarSize(this BigInteger item) | ||
{ | ||
var length = item.GetByteCount(); | ||
return IO.Helper.GetVarSize(length) + length; | ||
} | ||
|
||
public static IEnumerable<(TKey, TValue)> FindPrefix<TKey, TValue>(this IStore db, byte[] prefix) | ||
where TKey : ISerializable, new() | ||
where TValue : class, ISerializable, new() | ||
{ | ||
foreach (var (key, value) in db.Seek(prefix, SeekDirection.Forward)) | ||
{ | ||
if (!key.AsSpan().StartsWith(prefix)) break; | ||
yield return (key.AsSerializable<TKey>(1), value.AsSerializable<TValue>()); | ||
} | ||
} | ||
|
||
public static IEnumerable<(TKey, TValue)> FindRange<TKey, TValue>(this IStore db, byte[] startKey, byte[] endKey) | ||
where TKey : ISerializable, new() | ||
where TValue : class, ISerializable, new() | ||
{ | ||
foreach (var (key, value) in db.Seek(startKey, SeekDirection.Forward)) | ||
{ | ||
if (key.AsSpan().SequenceCompareTo(endKey) > 0) break; | ||
yield return (key.AsSerializable<TKey>(1), value.AsSerializable<TValue>()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Neo.IO; | ||
using Neo.Ledger; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.Persistence; | ||
using Neo.Plugins.Trackers; | ||
using static System.IO.Path; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
public class TokensTracker : Plugin, IPersistencePlugin | ||
{ | ||
private string _dbPath; | ||
private bool _shouldTrackHistory; | ||
private uint _maxResults; | ||
private uint _network; | ||
private string[] _enabledTrackers; | ||
private IStore _db; | ||
private NeoSystem neoSystem; | ||
private readonly List<TrackerBase> trackers = new(); | ||
|
||
public override string Description => "Enquiries balances and transaction history of accounts through RPC"; | ||
|
||
protected override void Configure() | ||
{ | ||
IConfigurationSection config = GetConfiguration(); | ||
_dbPath = config.GetValue("DBPath", "TokensBalanceData"); | ||
_shouldTrackHistory = config.GetValue("TrackHistory", true); | ||
_maxResults = config.GetValue("MaxResults", 1000u); | ||
_network = config.GetValue("Network", 860833102u); | ||
_enabledTrackers = config.GetSection("EnabledTrackers").GetChildren().Select(p => p.Value).ToArray(); | ||
} | ||
|
||
protected override void OnSystemLoaded(NeoSystem system) | ||
{ | ||
if (system.Settings.Network != _network) return; | ||
neoSystem = system; | ||
string path = string.Format(_dbPath, neoSystem.Settings.Network.ToString("X8")); | ||
_db = neoSystem.LoadStore(GetFullPath(path)); | ||
if (_enabledTrackers.Contains("NEP-11")) | ||
trackers.Add(new Trackers.NEP_11.Nep11Tracker(_db, _maxResults, _shouldTrackHistory, neoSystem)); | ||
if (_enabledTrackers.Contains("NEP-17")) | ||
trackers.Add(new Trackers.NEP_17.Nep17Tracker(_db, _maxResults, _shouldTrackHistory, neoSystem)); | ||
foreach (TrackerBase tracker in trackers) | ||
RpcServerPlugin.RegisterMethods(tracker, _network); | ||
} | ||
|
||
private void ResetBatch() | ||
{ | ||
foreach (var tracker in trackers) | ||
{ | ||
tracker.ResetBatch(); | ||
} | ||
} | ||
|
||
void IPersistencePlugin.OnPersist(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList) | ||
{ | ||
if (system.Settings.Network != _network) return; | ||
// Start freshly with a new DBCache for each block. | ||
ResetBatch(); | ||
foreach (var tracker in trackers) | ||
{ | ||
tracker.OnPersist(system, block, snapshot, applicationExecutedList); | ||
} | ||
} | ||
|
||
void IPersistencePlugin.OnCommit(NeoSystem system, Block block, DataCache snapshot) | ||
{ | ||
if (system.Settings.Network != _network) return; | ||
foreach (var tracker in trackers) | ||
{ | ||
tracker.Commit(); | ||
} | ||
} | ||
|
||
bool IPersistencePlugin.ShouldThrowExceptionFromCommit(Exception ex) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Neo.Plugins.TokensTracker</PackageId> | ||
<RootNamespace>Neo.Plugins</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RpcServer\RpcServer.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="TokensTracker\config.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"PluginConfiguration": { | ||
"DBPath": "TokenBalanceData", | ||
"TrackHistory": true, | ||
"MaxResults": 1000, | ||
"Network": 860833102, | ||
"EnabledTrackers": [ "NEP-11", "NEP-17" ] | ||
}, | ||
"Dependency": [ | ||
"RpcServer" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.IO; | ||
using Neo.IO; | ||
using Neo.VM.Types; | ||
|
||
namespace Neo.Plugins.Trackers.NEP_11 | ||
{ | ||
public class Nep11BalanceKey : IComparable<Nep11BalanceKey>, IEquatable<Nep11BalanceKey>, ISerializable | ||
{ | ||
public readonly UInt160 UserScriptHash; | ||
public readonly UInt160 AssetScriptHash; | ||
public ByteString Token; | ||
public int Size => UInt160.Length + UInt160.Length + Token.GetVarSize(); | ||
|
||
public Nep11BalanceKey() : this(new UInt160(), new UInt160(), ByteString.Empty) | ||
{ | ||
} | ||
|
||
public Nep11BalanceKey(UInt160 userScriptHash, UInt160 assetScriptHash, ByteString tokenId) | ||
{ | ||
if (userScriptHash == null || assetScriptHash == null || tokenId == null) | ||
throw new ArgumentNullException(); | ||
UserScriptHash = userScriptHash; | ||
AssetScriptHash = assetScriptHash; | ||
Token = tokenId; | ||
} | ||
|
||
public int CompareTo(Nep11BalanceKey other) | ||
{ | ||
if (other is null) return 1; | ||
if (ReferenceEquals(this, other)) return 0; | ||
int result = UserScriptHash.CompareTo(other.UserScriptHash); | ||
if (result != 0) return result; | ||
result = AssetScriptHash.CompareTo(other.AssetScriptHash); | ||
if (result != 0) return result; | ||
return (Token.GetInteger() - other.Token.GetInteger()).Sign; | ||
} | ||
|
||
public bool Equals(Nep11BalanceKey other) | ||
{ | ||
if (other is null) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return UserScriptHash.Equals(other.UserScriptHash) && AssetScriptHash.Equals(AssetScriptHash) && Token.Equals(other.Token); | ||
} | ||
|
||
public override bool Equals(Object other) | ||
{ | ||
return other is Nep11BalanceKey otherKey && Equals(otherKey); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(UserScriptHash.GetHashCode(), AssetScriptHash.GetHashCode(), Token.GetHashCode()); | ||
} | ||
|
||
public void Serialize(BinaryWriter writer) | ||
{ | ||
writer.Write(UserScriptHash); | ||
writer.Write(AssetScriptHash); | ||
writer.WriteVarBytes(Token.GetSpan()); | ||
} | ||
|
||
public void Deserialize(BinaryReader reader) | ||
{ | ||
((ISerializable)UserScriptHash).Deserialize(reader); | ||
((ISerializable)AssetScriptHash).Deserialize(reader); | ||
Token = new ByteString(reader.ReadVarBytes()); | ||
} | ||
} | ||
} |
Oops, something went wrong.