-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Some small optimization #1651
Some small optimization #1651
Conversation
Could you provide some benchmarks? linq it's slower. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past I remember that we needed to order it for verifying multisig.
If it is not used and needed I agree with the improvement.
…tps://github.com/Qiao-Jin/neo into optimize_transaction_GetScriptHashesForVerifying
This reverts commit a14ac6c.
return hashes.OrderBy(p => p).ToArray(); | ||
var hashes = Cosigners.Select(p => p.Account); | ||
hashes = hashes.Contains(Sender) ? hashes : hashes.Append(Sender); | ||
return hashes.ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order cannot be removed.
neo/src/neo/SmartContract/Helper.cs
Lines 126 to 168 in 3de1646
internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snapshot, long gas) | |
{ | |
if (gas < 0) return false; | |
UInt160[] hashes; | |
try | |
{ | |
hashes = verifiable.GetScriptHashesForVerifying(snapshot); | |
} | |
catch (InvalidOperationException) | |
{ | |
return false; | |
} | |
if (hashes.Length != verifiable.Witnesses.Length) return false; | |
for (int i = 0; i < hashes.Length; i++) | |
{ | |
int offset; | |
byte[] verification = verifiable.Witnesses[i].VerificationScript; | |
if (verification.Length == 0) | |
{ | |
ContractState cs = snapshot.Contracts.TryGet(hashes[i]); | |
if (cs is null) return false; | |
ContractMethodDescriptor md = cs.Manifest.Abi.GetMethod("verify"); | |
if (md is null) return false; | |
verification = cs.Script; | |
offset = md.Offset; | |
} | |
else | |
{ | |
if (hashes[i] != verifiable.Witnesses[i].ScriptHash) return false; | |
offset = 0; | |
} | |
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, verifiable, snapshot, gas)) | |
{ | |
engine.LoadScript(verification, CallFlags.ReadOnly).InstructionPointer = offset; | |
engine.LoadScript(verifiable.Witnesses[i].InvocationScript, CallFlags.None); | |
if (engine.Execute() == VMState.FAULT) return false; | |
if (!engine.ResultStack.TryPop(out StackItem result) || !result.ToBoolean()) return false; | |
gas -= engine.GasConsumed; | |
} | |
} | |
return true; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the order of witness is also by func GetScriptHashesForVerifying. So whether we order hashes or not, witnesses and hashes are one-to-one correspondence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are modifying GetScriptHashesForVerifying()
, aren't you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Witness[] witnesses = new Witness[ScriptHashes.Count]; |
and
_ScriptHashes = Verifiable.GetScriptHashesForVerifying(null); |
Actually the order of witness depends on func GetScriptHashesForVerifying, so witnesses and hashes are one-to-one correspondence whether ordered or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are talking ablout this implementation. But NEO is a protocol. You need to find a way to ensure the order between different implementations.
Closed due to conflict with implementation design. |
hashes.OrderBy(p => p)
should be deleted as this sorting is not used by any code.Besides func UnionWith's complexity is O(n) where n is the length of its parameter.