-
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
Optimize VerifyWitness and tx.verify #2054
Changes from 20 commits
ebd7bff
61ef979
caa6f3c
9439df7
0641353
4a6f3ae
420ee5c
ae11bca
d69d849
c602f45
91cf47c
52520d2
ff53a7d
b675ac4
177cd44
3fabce9
43324e8
c963241
67ad746
fda5a28
f09512b
c340bfc
7e62e79
cd4969b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,17 +295,36 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction | |
if (!attribute.Verify(snapshot, this)) | ||
return VerifyResult.Invalid; | ||
long net_fee = NetworkFee - Size * NativeContract.Policy.GetFeePerByte(snapshot); | ||
if (!this.VerifyWitnesses(snapshot, net_fee, WitnessFlag.StateDependent)) | ||
return VerifyResult.Invalid; | ||
|
||
UInt160[] hashes = GetScriptHashesForVerifying(snapshot); | ||
if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; | ||
for (int i = 0; i < hashes.Length; i++) | ||
{ | ||
if (witnesses[i].VerificationScript.IsSignatureContract()) | ||
net_fee -= SmartContract.Helper.SignatureContractCost; | ||
else if (witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) | ||
net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (m + n) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice * n); | ||
else | ||
{ | ||
if (!this.VerifyWitness(null, hashes[i], witnesses[i], net_fee, out long fee)) | ||
return VerifyResult.Invalid; | ||
net_fee -= fee; | ||
} | ||
if (net_fee < 0) return VerifyResult.InsufficientFunds; | ||
} | ||
return VerifyResult.Succeed; | ||
} | ||
|
||
public virtual VerifyResult VerifyStateIndependent() | ||
{ | ||
if (Size > MaxTransactionSize) | ||
return VerifyResult.Invalid; | ||
if (!this.VerifyWitnesses(null, NetworkFee, WitnessFlag.StateIndependent)) | ||
return VerifyResult.Invalid; | ||
UInt160[] hashes = GetScriptHashesForVerifying(null); | ||
if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; | ||
for (int i = 0; i < hashes.Length; i++) | ||
if (witnesses[i].VerificationScript.IsStandardContract()) | ||
erikzhang marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can cache if it's standar inside the witness, otherwise we check it at least twice. |
||
if (!this.VerifyWitness(null, hashes[i], witnesses[i], SmartContract.Helper.MaxVerificationGas, out _)) | ||
erikzhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return VerifyResult.Invalid; | ||
return VerifyResult.Succeed; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,12 @@ namespace Neo.SmartContract | |
{ | ||
public static class Helper | ||
{ | ||
private const long MaxVerificationGas = 0_50000000; | ||
public const long MaxVerificationGas = 0_50000000; | ||
public static readonly long SignatureContractCost = | ||
Tommo-L marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + | ||
ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + | ||
ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + | ||
ApplicationEngine.ECDsaVerifyPrice; | ||
|
||
public static UInt160 GetContractHash(UInt160 sender, byte[] script) | ||
{ | ||
|
@@ -139,7 +144,7 @@ public static UInt160 ToScriptHash(this ReadOnlySpan<byte> script) | |
return new UInt160(Crypto.Hash160(script)); | ||
} | ||
|
||
internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snapshot, long gas, WitnessFlag filter = WitnessFlag.All) | ||
internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snapshot, long gas) | ||
{ | ||
if (gas < 0) return false; | ||
if (gas > MaxVerificationGas) gas = MaxVerificationGas; | ||
|
@@ -156,39 +161,39 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap | |
if (hashes.Length != verifiable.Witnesses.Length) return false; | ||
for (int i = 0; i < hashes.Length; i++) | ||
{ | ||
WitnessFlag flag = verifiable.Witnesses[i].StateDependent ? WitnessFlag.StateDependent : WitnessFlag.StateIndependent; | ||
if (!filter.HasFlag(flag)) | ||
if (!verifiable.VerifyWitness(snapshot, hashes[i], verifiable.Witnesses[i], gas, out long fee)) | ||
return false; | ||
gas -= fee; | ||
} | ||
return true; | ||
} | ||
|
||
internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot, UInt160 hash, Witness witness, long gas, out long fee) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
fee = 0; | ||
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) | ||
{ | ||
CallFlags callFlags = !witness.VerificationScript.IsStandardContract() ? CallFlags.AllowStates : CallFlags.None; | ||
byte[] verification = witness.VerificationScript; | ||
|
||
if (verification.Length == 0) | ||
{ | ||
gas -= verifiable.Witnesses[i].GasConsumed; | ||
if (gas < 0) return false; | ||
continue; | ||
ContractState cs = snapshot.Contracts.TryGet(hash); | ||
if (cs is null) return false; | ||
if (engine.LoadContract(cs, "verify", callFlags, true) is null) | ||
return false; | ||
} | ||
|
||
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) | ||
else | ||
{ | ||
CallFlags callFlags = verifiable.Witnesses[i].StateDependent ? CallFlags.AllowStates : CallFlags.None; | ||
byte[] verification = verifiable.Witnesses[i].VerificationScript; | ||
|
||
if (verification.Length == 0) | ||
{ | ||
ContractState cs = snapshot.Contracts.TryGet(hashes[i]); | ||
if (cs is null) return false; | ||
if (engine.LoadContract(cs, "verify", callFlags, true) is null) | ||
return false; | ||
} | ||
else | ||
{ | ||
if (NativeContract.IsNative(hashes[i])) return false; | ||
if (hashes[i] != verifiable.Witnesses[i].ScriptHash) return false; | ||
engine.LoadScript(verification, callFlags, hashes[i], 0); | ||
} | ||
|
||
engine.LoadScript(verifiable.Witnesses[i].InvocationScript, CallFlags.None); | ||
if (engine.Execute() == VMState.FAULT) return false; | ||
if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) return false; | ||
gas -= engine.GasConsumed; | ||
verifiable.Witnesses[i].GasConsumed = engine.GasConsumed; | ||
if (NativeContract.IsNative(hash)) return false; | ||
if (hash != witness.ScriptHash) return false; | ||
engine.LoadScript(verification, callFlags, hash, 0); | ||
} | ||
|
||
engine.LoadScript(witness.InvocationScript, CallFlags.None); | ||
if (engine.Execute() == VMState.FAULT) return false; | ||
if (engine.ResultStack.Count != 1 || !engine.ResultStack.Peek().GetBoolean()) return false; | ||
fee = engine.GasConsumed; | ||
} | ||
return true; | ||
} | ||
|
This file was deleted.
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.
It could be any kind of push, same price but just to take care..