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

Some small optimization #1651

Conversation

Qiao-Jin
Copy link
Contributor

@Qiao-Jin Qiao-Jin commented May 19, 2020

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.

@shargon
Copy link
Member

shargon commented May 19, 2020

Could you provide some benchmarks? linq it's slower.

Copy link
Member

@vncoelho vncoelho left a 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.

@Qiao-Jin Qiao-Jin changed the title Optimize Transaction.GetScriptHashesForVerifying Some small optimization May 20, 2020
return hashes.OrderBy(p => p).ToArray();
var hashes = Cosigners.Select(p => p.Account);
hashes = hashes.Contains(Sender) ? hashes : hashes.Append(Sender);
return hashes.ToArray();
Copy link
Member

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.

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;
}

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@Qiao-Jin
Copy link
Contributor Author

Closed due to conflict with implementation design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants