Skip to content

Commit

Permalink
update 3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashuaidehao committed Aug 8, 2021
1 parent 7d468ee commit 98c2790
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 214 deletions.
54 changes: 32 additions & 22 deletions neo3-gui/neo3-gui/Common/Analyzers/BlockAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class BlockAnalyzerResult
public readonly IDictionary<UInt160, AssetInfo> AssetInfos = new Dictionary<UInt160, AssetInfo>();

/// <summary>
///
/// accounts of balance changed
/// </summary>
public readonly HashSet<AccountAsset> BalanceChangeAccounts = new HashSet<AccountAsset>();
}
Expand Down Expand Up @@ -100,41 +100,37 @@ private void AnalysisAppExecuteResult(Blockchain.ApplicationExecuted appExec)
execResult.Notifications = appExec.Notifications.Select(n => n.ToNotificationInfo()).ToList();
Result.ExecuteResultInfos.Add(execResult);

foreach (var contract in execResult.Notifications.Select(n => n.Contract).Distinct())
{
var asset = AssetCache.GetAssetInfo(contract, _snapshot);
if (asset != null)
{
Result.AssetInfos[asset.Asset] = asset;
}
}

if (execResult.VMState.HasFlag(VMState.FAULT))
if (execResult.VMState.HasFlag(VMState.FAULT) || execResult.Notifications.IsEmpty())
{
//no need to track
return;
}

if (execResult.Notifications.IsEmpty())
{
//no need to track
return;
}

//foreach (var contract in execResult.Notifications.Select(n => n.Contract).Distinct())
//{
// var asset = AssetCache.GetAssetInfo(contract, _snapshot);
// if (asset != null)
// {
// Result.AssetInfos[asset.Asset] = asset;
// }
//}
foreach (var notification in appExec.Notifications)
{
switch (notification.EventName)
{
case "transfer":
case "Transfer":
ProcessTransfer(notification, appExec);
break;
case "Deploy":
ProcessDeploy(notification, appExec);
break;
case "Update":
ProcessUpdate(notification, appExec);
break;
case "Destory":
ProcessDestory(notification, appExec);
break;
case "transfer":
case "Transfer":
ProcessTransfer(notification, appExec);
break;
default:
break;
}
Expand Down Expand Up @@ -206,7 +202,21 @@ private void ProcessDeploy(NotifyEventArgs notification, Blockchain.ApplicationE

private void ProcessUpdate(NotifyEventArgs notification, Blockchain.ApplicationExecuted appExec)
{

if (notification.State.Count != 1) { return; }
var hash = notification.State[0].GetByteSafely();
if (hash == null || hash.Length != 20) { return; }
var contractHash = new UInt160(hash);
if (!Result.ContractChangeEvents.ContainsKey(appExec.Transaction.Hash))
{
Result.ContractChangeEvents[appExec.Transaction.Hash] = new List<ContractEventInfo>();
}
ContractState contract = NativeContract.ContractManagement.GetContract(_snapshot, contractHash);
Result.ContractChangeEvents[appExec.Transaction.Hash].Add(new ContractEventInfo() { Contract = contractHash, Name = contract?.Manifest.Name, Event = ContractEventType.Migrate });
var asset = AssetCache.GetAssetInfoFromChain(contractHash, _snapshot);
if (asset != null)
{
Result.AssetInfos[asset.Asset] = asset;
}
}

private void ProcessDestory(NotifyEventArgs notification, Blockchain.ApplicationExecuted appExec)
Expand Down
68 changes: 37 additions & 31 deletions neo3-gui/neo3-gui/Common/Scanners/ExecuteResultScanner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Neo.Common.Storage;
Expand All @@ -23,6 +24,7 @@ public class ExecuteResultScanner
private LevelDbContext _levelDb = new LevelDbContext();
private bool _running = true;
private uint _scanHeight = 0;

public async Task Start()
{
_running = true;
Expand Down Expand Up @@ -55,7 +57,6 @@ public void Stop()
}



/// <summary>
/// analysis block transaction execute result logs
/// </summary>
Expand All @@ -76,7 +77,6 @@ public async Task<bool> Sync(uint blockHeight)
var block = blockHeight.GetBlock();
var blockTime = block.Timestamp.FromTimestampMS();

//var balanceChanges = new HashSet<(UInt160 account, UInt160 asset)>();
foreach (var transaction in block.Transactions)
{
_db.AddTransaction(new TransactionInfo()
Expand All @@ -86,21 +86,18 @@ public async Task<bool> Sync(uint blockHeight)
Sender = transaction.Sender,
Time = blockTime,
});
//balanceChanges.Add((transaction.Sender, NativeContract.GAS.Hash));
var invokeMethods = GetInvokeMethods(transaction);
if (invokeMethods.NotEmpty())
{
foreach (var invokeMethod in invokeMethods)
{
_db.AddInvokeTransaction(transaction.Hash, invokeMethod.contract, string.Join(',', invokeMethod.methods));
}
}
//var invokeMethods = GetInvokeMethods(transaction);
//if (invokeMethods.NotEmpty())
//{
// foreach (var invokeMethod in invokeMethods)
// {
// _db.AddInvokeTransaction(transaction.Hash, invokeMethod.Key, string.Join(',', invokeMethod.Value));
// }
//}
}

SyncContracts(blockHeight, blockTime);



var transfers = new List<TransferInfo>();
var transferItems = _levelDb.GetTransfers(blockHeight);
if (transferItems.NotEmpty())
Expand All @@ -118,7 +115,6 @@ public async Task<bool> Sync(uint blockHeight)
Asset = item.Asset,
Trigger = item.Trigger,
});

}
}

Expand All @@ -139,7 +135,12 @@ public async Task<bool> Sync(uint blockHeight)

_db.AddSyncIndex(blockHeight);
_db.Commit();
Console.WriteLine($"Synced:{_scanHeight}");

if (block.Transactions.Length > 0)
{
//Console.WriteLine($"Synced:{_scanHeight}[{block.Transactions.Length}] cost:[{_sw.ElapsedMilliseconds}]");
Console.WriteLine($"Synced:{_scanHeight}[{block.Transactions.Length}]");
}
if (_db.LiveTime.TotalSeconds > 15)
{
//release memory
Expand All @@ -161,21 +162,23 @@ private void SyncContracts(uint blockHeight, DateTime blockTime)
{
foreach (var nativeContract in NativeContract.Contracts)
{
var entity = new Nep5ContractInfo()
var entity = new ContractEntity()
{
Name = nativeContract.Name,
Hash = nativeContract.Hash,
Hash = nativeContract.Hash.ToBigEndianHex(),
CreateTime = blockTime,
};
if (nativeContract is NeoToken neo)
{
entity.Symbol = neo.Symbol;
entity.Decimals = neo.Decimals;
entity.AssetType = AssetType.Nep17;
}
if (nativeContract is GasToken gas)
{
entity.Symbol = gas.Symbol;
entity.Decimals = gas.Decimals;
entity.AssetType = AssetType.Nep17;
}
_db.CreateContract(entity);
}
Expand Down Expand Up @@ -203,47 +206,50 @@ private void ProcessContractEvent(ContractEventInfo contractEvent, UInt256 txId,
{
case ContractEventType.Create:
{
var newContract = GenerateNewNep5ContractInfo(contractEvent.Contract, txId, blockTime);
newContract.Name = contractEvent.Name;
var newContract = GenerateContractEntity(contractEvent);
newContract.CreateTxId = txId.ToBigEndianHex();
newContract.CreateTime = blockTime;
_db.CreateContract(newContract);
break;
}
case ContractEventType.Destroy:
_db.DeleteContract(contractEvent.Contract, txId, blockTime);
break;
case ContractEventType.Migrate:
var migrateContract = GenerateNewNep5ContractInfo(contractEvent.MigrateToContract, txId, blockTime);
_db.MigrateContract(contractEvent.Contract, migrateContract, txId, blockTime);
var migrateContract = GenerateContractEntity(contractEvent);
migrateContract.MigrateTxId = txId.ToBigEndianHex();
migrateContract.MigrateTime = blockTime;
_db.MigrateContract(migrateContract);
break;
}
}



private Nep5ContractInfo GenerateNewNep5ContractInfo(UInt160 contract, UInt256 txId, DateTime blockTime)
private ContractEntity GenerateContractEntity(ContractEventInfo contractEvent)
{
var newContract = new Nep5ContractInfo()
var newContract = new ContractEntity()
{
Hash = contract,
CreateTxId = txId,
CreateTime = blockTime,
Hash = contractEvent.Contract.ToBigEndianHex(),
Name = contractEvent.Name,
};

var asset = AssetCache.GetAssetInfoFromDb(contract);
var asset = AssetCache.GetAssetInfoFromLevelDb(contractEvent.Contract);
if (asset != null)
{
newContract.Name = asset.Name;
newContract.Symbol = asset.Symbol;
newContract.Decimals = asset.Decimals;
newContract.AssetType = asset.Type;
}
return newContract;
}


/// <summary>
/// get invoke methods from transaction script
/// </summary>
/// <param name="tx"></param>
/// <returns></returns>
private List<(UInt160 contract, HashSet<string> methods)> GetInvokeMethods(Transaction tx)
private Dictionary<UInt160, HashSet<string>> GetInvokeMethods(Transaction tx)
{
var methodBox = new Dictionary<UInt160, HashSet<string>>();

Expand All @@ -267,7 +273,7 @@ private Nep5ContractInfo GenerateNewNep5ContractInfo(UInt160 contract, UInt256 t
}
}
}
return methodBox.Select(m => (m.Key, m.Value)).ToList();
return methodBox;
}


Expand Down
6 changes: 5 additions & 1 deletion neo3-gui/neo3-gui/Common/Storage/ContractEventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Neo.Common.Storage.SQLiteModules;

namespace Neo.Common.Storage
{
public class ContractEventInfo
{
public UInt160 Contract { get; set; }

/// <summary>
/// Contract name
/// </summary>
public string Name { get; set; }

public ContractEventType Event { get; set; }

public UInt160 MigrateToContract { get; set; }
}
}
39 changes: 0 additions & 39 deletions neo3-gui/neo3-gui/Common/Storage/Nep5ContractInfo.cs

This file was deleted.

21 changes: 17 additions & 4 deletions neo3-gui/neo3-gui/Common/Storage/SQLiteModules/ContractEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ public class ContractEntity
/// </summary>
public string Hash { get; set; }

#region Nep5Info
#region NepInfo

public string Name { get; set; }
public string Symbol { get; set; }
public byte Decimals { get; set; }


public AssetType AssetType { get; set; }

#endregion

Expand All @@ -41,8 +42,20 @@ public class ContractEntity
/// <summary>
/// Delete or migrate Contract transaction hash string, big-endian without "0x"
/// </summary>
public string DeleteOrMigrateTxId { get; set; }
public string MigrateTxId { get; set; }

/// <summary>
/// Delete or migrate Contract transaction hash string, big-endian without "0x"
/// </summary>
public string DeleteTxId { get; set; }

public string MigrateTo { get; set; }
}


public enum AssetType
{
None,
Nep11,
Nep17,
}
}
Loading

0 comments on commit 98c2790

Please sign in to comment.