Skip to content

Commit

Permalink
Merge pull request #32 from neo-project/fixes/CI01148
Browse files Browse the repository at this point in the history
Fixes/ci01148
  • Loading branch information
chenzhitong authored Jan 11, 2021
2 parents 865a000 + 6d24932 commit 6ceb3b4
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 89 deletions.
27 changes: 9 additions & 18 deletions src/ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using Neo.IO.Data.LevelDB;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.IO.Path;

namespace Neo.Plugins
Expand Down Expand Up @@ -96,14 +96,14 @@ public static JObject TxLogToJson(Blockchain.ApplicationExecuted appExec)
return txJson;
}

public static JObject BlockLogToJson(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
public static JObject BlockLogToJson(Block block, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
var blocks = applicationExecutedList.Where(p => p.Transaction == null);
if (blocks.Count() > 0)
var blocks = applicationExecutedList.Where(p => p.Transaction is null).ToArray();
if (blocks.Length > 0)
{
var blockJson = new JObject();
var blockHash = snapshot.PersistingBlock.Hash.ToArray();
blockJson["blockhash"] = snapshot.PersistingBlock.Hash.ToString();
var blockHash = block.Hash.ToArray();
blockJson["blockhash"] = block.Hash.ToString();
var triggerList = new List<JObject>();
foreach (var appExec in blocks)
{
Expand Down Expand Up @@ -143,7 +143,7 @@ public static JObject BlockLogToJson(StoreView snapshot, IReadOnlyList<Blockchai
return null;
}

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

Expand All @@ -155,23 +155,14 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
}

//processing log for block
var blockJson = BlockLogToJson(snapshot, applicationExecutedList);
var blockJson = BlockLogToJson(block, applicationExecutedList);
if (blockJson != null)
{
writeBatch.Put(snapshot.PersistingBlock.Hash.ToArray(), Neo.Utility.StrictUTF8.GetBytes(blockJson.ToString()));
writeBatch.Put(block.Hash.ToArray(), Neo.Utility.StrictUTF8.GetBytes(blockJson.ToString()));
}
db.Write(WriteOptions.Default, writeBatch);
}

public void OnCommit(StoreView snapshot)
{
}

public bool ShouldThrowExceptionFromCommit(Exception ex)
{
return false;
}

static string GetExceptionMessage(Exception exception)
{
if (exception == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI01130" />
<PackageReference Include="Neo" Version="3.0.0-CI01148" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/RpcClient/ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ContractClient(RpcClient rpc, uint? magic = null)
/// <returns></returns>
public Task<RpcInvokeResult> TestInvokeAsync(UInt160 scriptHash, string operation, params object[] args)
{
byte[] script = scriptHash.MakeScript(operation, true, args);
byte[] script = scriptHash.MakeScript(operation, args);
return rpcClient.InvokeScriptAsync(script);
}

Expand All @@ -57,7 +57,7 @@ public async Task<Transaction> CreateDeployContractTxAsync(byte[] nefFile, Contr
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", true, nefFile, manifest.ToString());
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nefFile, manifest.ToString());
script = sb.ToArray();
}
UInt160 sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();
Expand Down
4 changes: 3 additions & 1 deletion src/RpcClient/Models/RpcContractState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Neo;
using Neo.IO;
using Neo.IO.Json;
using Neo.Network.RPC.Models;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System;
Expand All @@ -22,7 +24,7 @@ public static RpcContractState FromJson(JObject json)
Id = (int)json["id"].AsNumber(),
UpdateCounter = (ushort)json["updatecounter"].AsNumber(),
Hash = UInt160.Parse(json["hash"].AsString()),
Script = Convert.FromBase64String(json["script"].AsString()),
Nef = RpcNefFile.FromJson(json["nef"]),
Manifest = ContractManifest.FromJson(json["manifest"])
}
};
Expand Down
25 changes: 25 additions & 0 deletions src/RpcClient/Models/RpcMethodToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo.Network.RPC.Models
{
class RpcMethodToken
{
public static MethodToken FromJson(JObject json)
{
return new MethodToken
{
Hash = UInt160.Parse(json["hash"].AsString()),
Method = json["method"].AsString(),
ParametersCount = (ushort)json["paramcount"].AsNumber(),
HasReturnValue = json["hasreturnvalue"].AsBoolean(),
CallFlags = (CallFlags)Enum.Parse(typeof(CallFlags), json["callflags"].AsString())
};
}
}
}
25 changes: 25 additions & 0 deletions src/RpcClient/Models/RpcNefFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo.Network.RPC.Models
{
class RpcNefFile
{
public static NefFile FromJson(JObject json)
{
return new NefFile
{
Compiler = json["compiler"].AsString(),
Version = json["version"].AsString(),
Tokens = ((JArray)json["tokens"]).Select(p => RpcMethodToken.FromJson(p)).ToArray(),
Script = Convert.FromBase64String(json["script"].AsString()),
CheckSum = (uint)json["checksum"].AsNumber()
};
}
}
}
34 changes: 25 additions & 9 deletions src/RpcClient/Nep17API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,32 @@ public async Task<BigInteger> TotalSupplyAsync(UInt160 scriptHash)
/// <returns></returns>
public async Task<RpcNep17TokenInfo> GetTokenInfoAsync(UInt160 scriptHash)
{
byte[] script = Concat(
scriptHash.MakeScript("symbol", true),
scriptHash.MakeScript("decimals", true),
scriptHash.MakeScript("totalSupply", true));

var contractState = await rpcClient.GetContractStateAsync(scriptHash.ToString()).ConfigureAwait(false);
byte[] script = Concat(
scriptHash.MakeScript("symbol"),
scriptHash.MakeScript("decimals"),
scriptHash.MakeScript("totalSupply"));
var name = contractState.Manifest.Name;
var result = await rpcClient.InvokeScriptAsync(script).ConfigureAwait(false);
var stack = result.Stack;

return new RpcNep17TokenInfo
{
Name = name,
Symbol = stack[0].GetString(),
Decimals = (byte)stack[1].GetInteger(),
TotalSupply = stack[2].GetInteger()
};
}

public async Task<RpcNep17TokenInfo> GetTokenInfoAsync(string contractName)
{
var contractState = await rpcClient.GetContractStateAsync(contractName).ConfigureAwait(false);
byte[] script = Concat(
contractState.Hash.MakeScript("symbol"),
contractState.Hash.MakeScript("decimals"),
contractState.Hash.MakeScript("totalSupply"));
var name = contractState.Manifest.Name;
var result = await rpcClient.InvokeScriptAsync(script).ConfigureAwait(false);
var stack = result.Stack;

Expand All @@ -109,8 +127,7 @@ public async Task<Transaction> CreateTransferTxAsync(UInt160 scriptHash, KeyPair
{
var sender = Contract.CreateSignatureRedeemScript(fromKey.PublicKey).ToScriptHash();
Signer[] signers = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = sender } };

byte[] script = scriptHash.MakeScript("transfer", true, sender, to, amount, data);
byte[] script = scriptHash.MakeScript("transfer", sender, to, amount, data);

TransactionManagerFactory factory = new TransactionManagerFactory(rpcClient, magic);
TransactionManager manager = await factory.MakeTransactionAsync(script, signers).ConfigureAwait(false);
Expand All @@ -136,8 +153,7 @@ public async Task<Transaction> CreateTransferTxAsync(UInt160 scriptHash, int m,
throw new ArgumentException($"Need at least {m} KeyPairs for signing!");
var sender = Contract.CreateMultiSigContract(m, pubKeys).ScriptHash;
Signer[] signers = new[] { new Signer { Scopes = WitnessScope.CalledByEntry, Account = sender } };

byte[] script = scriptHash.MakeScript("transfer", true, sender, to, amount, data);
byte[] script = scriptHash.MakeScript("transfer", sender, to, amount, data);

TransactionManagerFactory factory = new TransactionManagerFactory(rpcClient, magic);
TransactionManager manager = await factory.MakeTransactionAsync(script, signers).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static ContractState ContractStateFromJson(JObject json)
Id = (int)json["id"].AsNumber(),
UpdateCounter = (ushort)json["updatecounter"].AsNumber(),
Hash = UInt160.Parse(json["hash"].AsString()),
Script = Convert.FromBase64String(json["script"].AsString()),
Nef = RpcNefFile.FromJson(json["nef"]),
Manifest = ContractManifest.FromJson(json["manifest"])
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/RpcNep17Tracker/RpcNep17Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ public void OnPersist(StoreView snapshot, IReadOnlyList<Blockchain.ApplicationEx
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitDynamicCall(nep17BalancePair.Key.AssetScriptHash, "balanceOf", true,
nep17BalancePair.Key.UserScriptHash.ToArray());
sb.EmitDynamicCall(nep17BalancePair.Key.AssetScriptHash, "balanceOf", nep17BalancePair.Key.UserScriptHash.ToArray());
script = sb.ToArray();
}

Expand Down
4 changes: 2 additions & 2 deletions src/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private JObject GetVerificationResult(UInt160 scriptHash, ContractParameter[] ar
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
engine.LoadContract(contract, methodName, CallFlags.None, true, 0);

engine.LoadScript(new ScriptBuilder().EmitDynamicCall(scriptHash, methodName, true, args).ToArray());
engine.LoadScript(new ScriptBuilder().EmitDynamicCall(scriptHash, methodName, args).ToArray());

JObject json = new JObject();
json["script"] = Convert.ToBase64String(contract.Script);
Expand Down Expand Up @@ -169,7 +169,7 @@ protected virtual JObject InvokeFunction(JArray _params)
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
script = sb.EmitDynamicCall(script_hash, operation, true, args).ToArray();
script = sb.EmitDynamicCall(script_hash, operation, args).ToArray();
}
return GetInvokeResult(script, signers);
}
Expand Down
2 changes: 1 addition & 1 deletion src/RpcServer/RpcServer/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"TrustedAuthorities": [],
"RpcUser": "",
"RpcPass": "",
"MaxGasInvoke": 10,
"MaxGasInvoke": 20,
"MaxFee": 0.1,
"MaxConcurrentConnections": 40,
"DisabledMethods": [ "openwallet" ]
Expand Down
Loading

0 comments on commit 6ceb3b4

Please sign in to comment.