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

m #8

Merged
merged 5 commits into from
Nov 16, 2020
Merged

m #8

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
86 changes: 74 additions & 12 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -37,30 +38,46 @@ public JObject GetApplicationLog(JArray _params)
UInt256 hash = UInt256.Parse(_params[0].AsString());
byte[] value = db.Get(ReadOptions.Default, hash.ToArray());
if (value is null)
throw new RpcException(-100, "Unknown transaction");
return JObject.Parse(Encoding.UTF8.GetString(value));
throw new RpcException(-100, "Unknown transaction/blockhash");

var raw = JObject.Parse(Utility.StrictUTF8.GetString(value));
//Additional optional "trigger" parameter to getapplicationlog for clients to be able to get just one execution result for a block.
if (_params.Count >= 2 && Enum.TryParse(_params[1].AsString(), true, out TriggerType trigger))
{
var executions = raw["executions"] as JArray;
for (int i = 0; i < executions.Count;)
{
if (!executions[i]["trigger"].AsString().Equals(trigger.ToString(), StringComparison.OrdinalIgnoreCase))
executions.RemoveAt(i);
else
i++;
}
}
return raw;
}

public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
WriteBatch writeBatch = new WriteBatch();

foreach (var appExec in applicationExecutedList)
//processing log for transactions
foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null))
{
JObject json = new JObject();
json["txid"] = appExec.Transaction?.Hash.ToString();
json["trigger"] = appExec.Trigger;
json["vmstate"] = appExec.VMState;
json["gasconsumed"] = appExec.GasConsumed.ToString();
var txJson = new JObject();
txJson["txid"] = appExec.Transaction.Hash.ToString();
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
json["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
trigger["stack"] = "error: recursive reference";
}
json["notifications"] = appExec.Notifications.Select(q =>
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
Expand All @@ -75,7 +92,52 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
}
return notification;
}).ToArray();
writeBatch.Put((appExec.Transaction?.Hash ?? snapshot.PersistingBlock.Hash).ToArray(), Encoding.UTF8.GetBytes(json.ToString()));

txJson["executions"] = new List<JObject>() { trigger }.ToArray();
writeBatch.Put(appExec.Transaction.Hash.ToArray(), Utility.StrictUTF8.GetBytes(txJson.ToString()));
}

//processing log for block
var blocks = applicationExecutedList.Where(p => p.Transaction == null);
if (blocks.Count() > 0)
{
var blockJson = new JObject();
var blockHash = snapshot.PersistingBlock.Hash.ToArray();
blockJson["blockhash"] = snapshot.PersistingBlock.Hash.ToString();
var triggerList = new List<JObject>();
foreach (var appExec in blocks)
{
JObject trigger = new JObject();
trigger["trigger"] = appExec.Trigger;
trigger["vmstate"] = appExec.VMState;
trigger["gasconsumed"] = appExec.GasConsumed.ToString();
try
{
trigger["stack"] = appExec.Stack.Select(q => q.ToJson()).ToArray();
}
catch (InvalidOperationException)
{
trigger["stack"] = "error: recursive reference";
}
trigger["notifications"] = appExec.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
notification["eventname"] = q.EventName;
try
{
notification["state"] = q.State.ToJson();
}
catch (InvalidOperationException)
{
notification["state"] = "error: recursive reference";
}
return notification;
}).ToArray();
triggerList.Add(trigger);
}
blockJson["executions"] = triggerList.ToArray();
writeBatch.Put(blockHash, Utility.StrictUTF8.GetBytes(blockJson.ToString()));
}
db.Write(WriteOptions.Default, writeBatch);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LevelDBStore/LevelDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01056" />
<PackageReference Include="Neo" Version="3.0.0-CI01066" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/RocksDBStore/RocksDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01056" />
<PackageReference Include="Neo" Version="3.0.0-CI01066" />
<PackageReference Include="RocksDbNative" Version="6.2.2" />
<PackageReference Include="RocksDbSharp" Version="6.2.2" />
</ItemGroup>
Expand Down
34 changes: 30 additions & 4 deletions src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ public class RpcApplicationLog
{
public UInt256 TxId { get; set; }

public UInt256 BlockHash { get; set; }

public List<Execution> Executions { get; set; }

public JObject ToJson()
{
JObject json = new JObject();
if (TxId != null)
json["txid"] = TxId.ToString();
if (BlockHash != null)
json["blockhash"] = BlockHash.ToString();
json["executions"] = Executions.Select(p => p.ToJson()).ToArray();
return json;
}

public static RpcApplicationLog FromJson(JObject json)
{
return new RpcApplicationLog
{
TxId = json["txid"] is null ? null : UInt256.Parse(json["txid"].AsString()),
BlockHash = json["blockhash"] is null ? null : UInt256.Parse(json["blockhash"].AsString()),
Executions = ((JArray)json["executions"]).Select(p => Execution.FromJson(p)).ToList(),
};
}
}

public class Execution
{
public TriggerType Trigger { get; set; }

public VMState VMState { get; set; }
Expand All @@ -24,7 +52,6 @@ public class RpcApplicationLog
public JObject ToJson()
{
JObject json = new JObject();
json["txid"] = TxId?.ToString();
json["trigger"] = Trigger;
json["vmstate"] = VMState;
json["gasconsumed"] = GasConsumed.ToString();
Expand All @@ -33,11 +60,10 @@ public JObject ToJson()
return json;
}

public static RpcApplicationLog FromJson(JObject json)
public static Execution FromJson(JObject json)
{
return new RpcApplicationLog
return new Execution
{
TxId = json["txid"] is null ? null : UInt256.Parse(json["txid"].AsString()),
Trigger = json["trigger"].TryGetEnum<TriggerType>(),
VMState = json["vmstate"].TryGetEnum<VMState>(),
GasConsumed = long.Parse(json["gasconsumed"].AsString()),
Expand Down
8 changes: 4 additions & 4 deletions src/RpcClient/PolicyAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.VM;
using System.Linq;
using System.Threading.Tasks;
using Neo.IO.Json;

namespace Neo.Network.RPC
{
Expand Down Expand Up @@ -52,11 +53,10 @@ public async Task<long> GetFeePerByteAsync()
/// Get Ploicy Blocked Accounts
/// </summary>
/// <returns></returns>
public async Task<UInt160[]> GetBlockedAccountsAsync()
public async Task<bool> IsBlockedAsync(UInt160 account)
{
var result = await TestInvokeAsync(scriptHash, "getBlockedAccounts").ConfigureAwait(false);
var array = (VM.Types.Array)result.Stack.Single();
return array.Select(p => new UInt160(p.GetSpan().ToArray())).ToArray();
var result = await TestInvokeAsync(scriptHash, "isBlocked", new object[] { account }).ConfigureAwait(false);
return result.Stack.Single().GetBoolean();
}
}
}
17 changes: 14 additions & 3 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -329,7 +330,7 @@ public async Task<RpcVersion> GetVersionAsync()
/// </summary>
public async Task<UInt256> SendRawTransactionAsync(byte[] rawTransaction)
{
var result = await RpcSendAsync(GetRpcName(), rawTransaction.ToHexString()).ConfigureAwait(false);
var result = await RpcSendAsync(GetRpcName(), Convert.ToBase64String(rawTransaction)).ConfigureAwait(false);
return UInt256.Parse(result["hash"].AsString());
}

Expand All @@ -346,7 +347,7 @@ public Task<UInt256> SendRawTransactionAsync(Transaction transaction)
/// </summary>
public async Task<UInt256> SubmitBlockAsync(byte[] block)
{
var result = await RpcSendAsync(GetRpcName(), block.ToHexString()).ConfigureAwait(false);
var result = await RpcSendAsync(GetRpcName(), Convert.ToBase64String(block)).ConfigureAwait(false);
return UInt256.Parse(result["hash"].AsString());
}

Expand Down Expand Up @@ -450,9 +451,9 @@ public async Task<string> GetNewAddressAsync()
/// <returns>new address as string</returns>
public async Task<BigDecimal> GetWalletBalanceAsync(string assetId)
{
byte decimals = await new Nep5API(this).DecimalsAsync(UInt160.Parse(assetId.AsScriptHash())).ConfigureAwait(false);
var result = await RpcSendAsync(GetRpcName(), assetId).ConfigureAwait(false);
BigInteger balance = BigInteger.Parse(result["balance"].AsString());
byte decimals = await new Nep5API(this).DecimalsAsync(UInt160.Parse(assetId.AsScriptHash())).ConfigureAwait(false);
return new BigDecimal(balance, decimals);
}

Expand Down Expand Up @@ -543,6 +544,16 @@ public async Task<RpcApplicationLog> GetApplicationLogAsync(string txHash)
return RpcApplicationLog.FromJson(result);
}

/// <summary>
/// Returns the contract log based on the specified txHash. The complete contract logs are stored under the ApplicationLogs directory.
/// This method is provided by the plugin ApplicationLogs.
/// </summary>
public async Task<RpcApplicationLog> GetApplicationLogAsync(string txHash, TriggerType triggerType)
{
var result = await RpcSendAsync(GetRpcName(), txHash, triggerType).ConfigureAwait(false);
return RpcApplicationLog.FromJson(result);
}

/// <summary>
/// Returns all the NEP-5 transaction information occurred in the specified address.
/// This method is provided by the plugin RpcNep5Tracker.
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/RpcClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01056" />
<PackageReference Include="Neo" Version="3.0.0-CI01066" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/RpcServer/RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using System;
using System.Linq;
using static Neo.Ledger.Blockchain;

Expand Down Expand Up @@ -71,15 +72,15 @@ protected virtual JObject GetVersion(JArray _params)
[RpcMethod]
protected virtual JObject SendRawTransaction(JArray _params)
{
Transaction tx = _params[0].AsString().HexToBytes().AsSerializable<Transaction>();
Transaction tx = Convert.FromBase64String(_params[0].AsString()).AsSerializable<Transaction>();
RelayResult reason = system.Blockchain.Ask<RelayResult>(tx).Result;
return GetRelayResult(reason.Result, tx.Hash);
}

[RpcMethod]
protected virtual JObject SubmitBlock(JArray _params)
{
Block block = _params[0].AsString().HexToBytes().AsSerializable<Block>();
Block block = Convert.FromBase64String(_params[0].AsString()).AsSerializable<Block>();
RelayResult reason = system.Blockchain.Ask<RelayResult>(block).Result;
return GetRelayResult(reason.Result, block.Hash);
}
Expand Down
8 changes: 7 additions & 1 deletion src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public void SerializeUnsigned(BinaryWriter writer)

private JObject GetInvokeResult(byte[] script, Signers signers = null)
{
using ApplicationEngine engine = ApplicationEngine.Run(script, container: signers, gas: settings.MaxGasInvoke);
Transaction tx = new Transaction
{
Signers = signers.GetSigners(),
Attributes = Array.Empty<TransactionAttribute>(),
Witnesses = signers.Witnesses,
};
using ApplicationEngine engine = ApplicationEngine.Run(script, container: tx, gas: settings.MaxGasInvoke);
JObject json = new JObject();
json["script"] = Convert.ToBase64String(script);
json["state"] = engine.State;
Expand Down
3 changes: 2 additions & 1 deletion src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ private void ProcessInvokeWithWallet(JObject result, Signers signers = null)
tx = null;
}
}
result["tx"] = tx?.ToArray().ToHexString();
if (tx != null)
result["tx"] = Convert.ToBase64String(tx.ToArray());
}

[RpcMethod]
Expand Down
4 changes: 2 additions & 2 deletions src/RpcServer/RpcServer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-preview3</Version>
Expand All @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" />
<PackageReference Include="Neo" Version="3.0.0-CI01056" />
<PackageReference Include="Neo" Version="3.0.0-CI01066" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/StatesDumper/StatesDumper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01056" />
<PackageReference Include="Neo" Version="3.0.0-CI01066" />
<PackageReference Include="Neo.ConsoleService" Version="1.0.0" />
</ItemGroup>

Expand Down
Loading