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

Dispose ApplicationEngine after using (2.x) #1350

Merged
merged 3 commits into from
Dec 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions neo/Network/RPC/RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ public void Dispose()

private JObject GetInvokeResult(byte[] script)
{
ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: MaxGasInvoke);
JObject json = new JObject();
json["script"] = script.ToHexString();
json["state"] = engine.State;
json["gas_consumed"] = engine.GasConsumed.ToString();
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()));
}
catch (InvalidOperationException)
using (ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: MaxGasInvoke))
{
json["stack"] = "error: recursive reference";
JObject json = new JObject();
json["script"] = script.ToHexString();
json["state"] = engine.State;
json["gas_consumed"] = engine.GasConsumed.ToString();
try
{
json["stack"] = new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()));
}
catch (InvalidOperationException)
{
json["stack"] = "error: recursive reference";
}
}
return json;
}
Expand Down
12 changes: 7 additions & 5 deletions neo/Wallets/AssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public AssetDescriptor(UIntBase asset_id)
sb.EmitAppCall(asset_id_160, "name");
script = sb.ToArray();
}
ApplicationEngine engine = ApplicationEngine.Run(script);
if (engine.State.HasFlag(VMState.FAULT)) throw new ArgumentException();
this.AssetId = asset_id;
this.AssetName = engine.ResultStack.Pop().GetString();
this.Decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
using (ApplicationEngine engine = ApplicationEngine.Run(script))
{
if (engine.State.HasFlag(VMState.FAULT)) throw new ArgumentException();
this.AssetId = asset_id;
this.AssetName = engine.ResultStack.Pop().GetString();
this.Decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
}
}
else
{
Expand Down
48 changes: 27 additions & 21 deletions neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ public BigDecimal GetAvailable(UIntBase asset_id)
sb.EmitAppCall(asset_id_160, "decimals");
script = sb.ToArray();
}
ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: Fixed8.FromDecimal(0.2m) * accounts.Length);
if (engine.State.HasFlag(VMState.FAULT))
return new BigDecimal(0, 0);
byte decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
BigInteger amount = engine.ResultStack.Pop().GetBigInteger();
return new BigDecimal(amount, decimals);
using (ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: Fixed8.FromDecimal(0.2m) * accounts.Length))
{
if (engine.State.HasFlag(VMState.FAULT))
return new BigDecimal(0, 0);
byte decimals = (byte)engine.ResultStack.Pop().GetBigInteger();
BigInteger amount = engine.ResultStack.Pop().GetBigInteger();
return new BigDecimal(amount, decimals);
}
}
else
{
Expand Down Expand Up @@ -323,11 +325,13 @@ public virtual WalletAccount Import(string nep2, string passphrase)
sb2.EmitAppCall(output.AssetId, "balanceOf", account);
script = sb2.ToArray();
}
ApplicationEngine engine = ApplicationEngine.Run(script);
if (engine.State.HasFlag(VMState.FAULT)) return null;
var result = engine.ResultStack.Pop().GetBigInteger();
if (result == 0) continue;
balances.Add((account, result));
using (ApplicationEngine engine = ApplicationEngine.Run(script))
{
if (engine.State.HasFlag(VMState.FAULT)) return null;
var result = engine.ResultStack.Pop().GetBigInteger();
if (result == 0) continue;
balances.Add((account, result));
}
}
BigInteger sum = balances.Aggregate(BigInteger.Zero, (x, y) => x + y.Value);
if (sum < output.Value) return null;
Expand Down Expand Up @@ -376,17 +380,19 @@ public virtual WalletAccount Import(string nep2, string passphrase)
tx.Witnesses = new Witness[0];
if (tx is InvocationTransaction itx)
{
ApplicationEngine engine = ApplicationEngine.Run(itx.Script, itx);
if (engine.State.HasFlag(VMState.FAULT)) return null;
tx = new InvocationTransaction
using (ApplicationEngine engine = ApplicationEngine.Run(itx.Script, itx))
{
Version = itx.Version,
Script = itx.Script,
Gas = InvocationTransaction.GetGas(engine.GasConsumed),
Attributes = itx.Attributes,
Inputs = itx.Inputs,
Outputs = itx.Outputs
};
if (engine.State.HasFlag(VMState.FAULT)) return null;
tx = new InvocationTransaction
{
Version = itx.Version,
Script = itx.Script,
Gas = InvocationTransaction.GetGas(engine.GasConsumed),
Attributes = itx.Attributes,
Inputs = itx.Inputs,
Outputs = itx.Outputs
};
}
}
tx = MakeTransaction(tx, from, change_address, fee);
return tx;
Expand Down