Skip to content

Commit

Permalink
Some optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jan 28, 2021
1 parent d0cf078 commit 1036a62
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 79 deletions.
8 changes: 6 additions & 2 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,16 @@ private void OnInvMessageReceived(InvPayload payload)
switch (payload.Type)
{
case InventoryType.Block:
using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
{
DataCache snapshot = Blockchain.Singleton.View;
hashes = hashes.Where(p => !NativeContract.Ledger.ContainsBlock(snapshot, p)).ToArray();
}
break;
case InventoryType.TX:
using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
{
DataCache snapshot = Blockchain.Singleton.View;
hashes = hashes.Where(p => !NativeContract.Ledger.ContainsTransaction(snapshot, p)).ToArray();
}
break;
}
if (hashes.Length == 0) return;
Expand Down
11 changes: 4 additions & 7 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,10 @@ private void RequestTasks(bool sendPing)

private void SendPingMessage()
{
TrimmedBlock block;
using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
{
block = NativeContract.Ledger.GetTrimmedBlock(snapshot, NativeContract.Ledger.CurrentHash(snapshot));
}

uint currentHeight = NativeContract.Ledger.CurrentIndex(Blockchain.Singleton.View);
DataCache snapshot = Blockchain.Singleton.View;
uint currentHeight = NativeContract.Ledger.CurrentIndex(snapshot);
UInt256 currentHash = NativeContract.Ledger.CurrentHash(snapshot);
TrimmedBlock block = NativeContract.Ledger.GetTrimmedBlock(snapshot, currentHash);
foreach (KeyValuePair<IActorRef, TaskSession> item in sessions)
{
var node = item.Key;
Expand Down
11 changes: 3 additions & 8 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,9 @@ internal static void ResetApplicationEngineProvider()

public static ApplicationEngine Run(byte[] script, DataCache snapshot = null, IVerifiable container = null, Block persistingBlock = null, int offset = 0, long gas = TestModeGas)
{
SnapshotCache disposable = null;
if (snapshot is null)
{
disposable = Blockchain.Singleton.GetSnapshot();
snapshot = disposable;
}
ApplicationEngine engine = Create(TriggerType.Application, container, snapshot, persistingBlock ?? CreateDummyBlock(snapshot), gas);
if (disposable != null) engine.Disposables.Add(disposable);
snapshot ??= Blockchain.Singleton.View;
persistingBlock ??= CreateDummyBlock(snapshot);
ApplicationEngine engine = Create(TriggerType.Application, container, snapshot, persistingBlock, gas);
engine.LoadScript(script, initialPosition: offset);
engine.Execute();
return engine;
Expand Down
13 changes: 3 additions & 10 deletions src/neo/SmartContract/ContractParametersContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.VM;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -84,19 +83,13 @@ public IReadOnlyList<UInt160> ScriptHashes
{
get
{
if (_ScriptHashes == null)
if (_ScriptHashes is null)
{
// snapshot is not necessary for Transaction
if (Verifiable is Transaction)
{
_ScriptHashes = Verifiable.GetScriptHashesForVerifying(null);
return _ScriptHashes;
}

using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
{
_ScriptHashes = Verifiable.GetScriptHashesForVerifying(snapshot);
}
else
_ScriptHashes = Verifiable.GetScriptHashesForVerifying(Blockchain.Singleton.View);
}
return _ScriptHashes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Wallets/AssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AssetDescriptor

public AssetDescriptor(UInt160 asset_id)
{
using SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot();
DataCache snapshot = Blockchain.Singleton.View;
var contract = NativeContract.ContractManagement.GetContract(snapshot, asset_id);
if (contract is null) throw new ArgumentException();

Expand Down
98 changes: 47 additions & 51 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,64 +252,62 @@ public Transaction MakeTransaction(TransferOutput[] outputs, UInt160 from = null
{
accounts = new[] { from };
}
using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
DataCache snapshot = Blockchain.Singleton.View;
Dictionary<UInt160, Signer> cosignerList = cosigners?.ToDictionary(p => p.Account) ?? new Dictionary<UInt160, Signer>();
byte[] script;
List<(UInt160 Account, BigInteger Value)> balances_gas = null;
using (ScriptBuilder sb = new ScriptBuilder())
{
Dictionary<UInt160, Signer> cosignerList = cosigners?.ToDictionary(p => p.Account) ?? new Dictionary<UInt160, Signer>();
byte[] script;
List<(UInt160 Account, BigInteger Value)> balances_gas = null;
using (ScriptBuilder sb = new ScriptBuilder())
foreach (var (assetId, group, sum) in outputs.GroupBy(p => p.AssetId, (k, g) => (k, g, g.Select(p => p.Value.Value).Sum())))
{
foreach (var (assetId, group, sum) in outputs.GroupBy(p => p.AssetId, (k, g) => (k, g, g.Select(p => p.Value.Value).Sum())))
{
var balances = new List<(UInt160 Account, BigInteger Value)>();
foreach (UInt160 account in accounts)
using (ScriptBuilder sb2 = new ScriptBuilder())
var balances = new List<(UInt160 Account, BigInteger Value)>();
foreach (UInt160 account in accounts)
using (ScriptBuilder sb2 = new ScriptBuilder())
{
sb2.EmitDynamicCall(assetId, "balanceOf", account);
using (ApplicationEngine engine = ApplicationEngine.Run(sb2.ToArray(), snapshot))
{
sb2.EmitDynamicCall(assetId, "balanceOf", account);
using (ApplicationEngine engine = ApplicationEngine.Run(sb2.ToArray(), snapshot))
{
if (engine.State.HasFlag(VMState.FAULT))
throw new InvalidOperationException($"Execution for {assetId.ToString()}.balanceOf('{account.ToString()}' fault");
BigInteger value = engine.ResultStack.Pop().GetInteger();
if (value.Sign > 0) balances.Add((account, value));
}
if (engine.State.HasFlag(VMState.FAULT))
throw new InvalidOperationException($"Execution for {assetId}.balanceOf('{account}' fault");
BigInteger value = engine.ResultStack.Pop().GetInteger();
if (value.Sign > 0) balances.Add((account, value));
}
BigInteger sum_balance = balances.Select(p => p.Value).Sum();
if (sum_balance < sum)
throw new InvalidOperationException($"It does not have enough balance, expected: {sum.ToString()} found: {sum_balance.ToString()}");
foreach (TransferOutput output in group)
}
BigInteger sum_balance = balances.Select(p => p.Value).Sum();
if (sum_balance < sum)
throw new InvalidOperationException($"It does not have enough balance, expected: {sum} found: {sum_balance}");
foreach (TransferOutput output in group)
{
balances = balances.OrderBy(p => p.Value).ToList();
var balances_used = FindPayingAccounts(balances, output.Value.Value);
foreach (var (account, value) in balances_used)
{
balances = balances.OrderBy(p => p.Value).ToList();
var balances_used = FindPayingAccounts(balances, output.Value.Value);
foreach (var (account, value) in balances_used)
if (cosignerList.TryGetValue(account, out Signer signer))
{
if (cosignerList.TryGetValue(account, out Signer signer))
{
if (signer.Scopes != WitnessScope.Global)
signer.Scopes |= WitnessScope.CalledByEntry;
}
else
if (signer.Scopes != WitnessScope.Global)
signer.Scopes |= WitnessScope.CalledByEntry;
}
else
{
cosignerList.Add(account, new Signer
{
cosignerList.Add(account, new Signer
{
Account = account,
Scopes = WitnessScope.CalledByEntry
});
}
sb.EmitDynamicCall(output.AssetId, "transfer", account, output.ScriptHash, value, output.Data);
sb.Emit(OpCode.ASSERT);
Account = account,
Scopes = WitnessScope.CalledByEntry
});
}
sb.EmitDynamicCall(output.AssetId, "transfer", account, output.ScriptHash, value, output.Data);
sb.Emit(OpCode.ASSERT);
}
if (assetId.Equals(NativeContract.GAS.Hash))
balances_gas = balances;
}
script = sb.ToArray();
if (assetId.Equals(NativeContract.GAS.Hash))
balances_gas = balances;
}
if (balances_gas is null)
balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();

return MakeTransaction(snapshot, script, cosignerList.Values.ToArray(), Array.Empty<TransactionAttribute>(), balances_gas);
script = sb.ToArray();
}
if (balances_gas is null)
balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();

return MakeTransaction(snapshot, script, cosignerList.Values.ToArray(), Array.Empty<TransactionAttribute>(), balances_gas);
}

public Transaction MakeTransaction(byte[] script, UInt160 sender = null, Signer[] cosigners = null, TransactionAttribute[] attributes = null)
Expand All @@ -323,11 +321,9 @@ public Transaction MakeTransaction(byte[] script, UInt160 sender = null, Signer[
{
accounts = new[] { sender };
}
using (SnapshotCache snapshot = Blockchain.Singleton.GetSnapshot())
{
var balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();
return MakeTransaction(snapshot, script, cosigners ?? Array.Empty<Signer>(), attributes ?? Array.Empty<TransactionAttribute>(), balances_gas);
}
DataCache snapshot = Blockchain.Singleton.View;
var balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();
return MakeTransaction(snapshot, script, cosigners ?? Array.Empty<Signer>(), attributes ?? Array.Empty<TransactionAttribute>(), balances_gas);
}

private Transaction MakeTransaction(DataCache snapshot, byte[] script, Signer[] cosigners, TransactionAttribute[] attributes, List<(UInt160 Account, BigInteger Value)> balances_gas)
Expand Down

0 comments on commit 1036a62

Please sign in to comment.