From ebd7bff0efecaf03086b247ea146a478e2de4733 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Tue, 10 Nov 2020 19:53:42 +0800 Subject: [PATCH 01/17] optimize tx.verify --- src/neo/Network/P2P/Payloads/Transaction.cs | 27 ++++++- src/neo/Network/P2P/Payloads/Witness.cs | 2 +- src/neo/SmartContract/Helper.cs | 90 ++++++++++----------- src/neo/SmartContract/WitnessFlag.cs | 15 ---- 4 files changed, 69 insertions(+), 65 deletions(-) delete mode 100644 src/neo/SmartContract/WitnessFlag.cs diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index fabb005c9a..ab1883a063 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -295,8 +295,23 @@ 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].IsStandardAccount) + { + if (!this.VerifyWitness(null, hashes[i], witnesses[i], net_fee, out long fee)) + return VerifyResult.Invalid; + net_fee -= fee; + } + else + { + // TODO Calculate signature fee + //net_fee -= fee; + } + } return VerifyResult.Succeed; } @@ -304,8 +319,12 @@ 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].IsStandardAccount) + if (!this.VerifyWitness(null, hashes[i], witnesses[i], NetworkFee, out _)) + return VerifyResult.Invalid; return VerifyResult.Succeed; } diff --git a/src/neo/Network/P2P/Payloads/Witness.cs b/src/neo/Network/P2P/Payloads/Witness.cs index 7ae2d51d70..a32b5cdba5 100644 --- a/src/neo/Network/P2P/Payloads/Witness.cs +++ b/src/neo/Network/P2P/Payloads/Witness.cs @@ -37,7 +37,7 @@ public virtual UInt160 ScriptHash } } - public bool StateDependent => VerificationScript.Length == 0; + public bool IsStandardAccount => VerificationScript.Length != 0 && (VerificationScript.IsStandardContract() || VerificationScript.IsMultiSigContract()); public int Size => InvocationScript.GetVarSize() + VerificationScript.GetVarSize(); diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index d3f841b84d..c716482221 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -130,7 +130,7 @@ public static UInt160 ToScriptHash(this ReadOnlySpan 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; @@ -145,56 +145,56 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap return false; } if (hashes.Length != verifiable.Witnesses.Length) return false; - for (int i = 0; i < hashes.Length; i++) + for (uint i = 0; i < hashes.Length; i++) { - WitnessFlag flag = verifiable.Witnesses[i].StateDependent ? WitnessFlag.StateDependent : WitnessFlag.StateIndependent; - if (!filter.HasFlag(flag)) - { - gas -= verifiable.Witnesses[i].GasConsumed; - if (gas < 0) return false; - continue; - } + if (!verifiable.VerifyWitness(snapshot, hashes[i], verifiable.Witnesses[i], gas, out long fee)) + return false; + gas -= fee; + } + return true; + } - int offset; - ContractMethodDescriptor init = null; - 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; - init = cs.Manifest.Abi.GetMethod("_initialize"); - } - else + public static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot, UInt160 hash, Witness witness, long gas, out long fee) + { + int offset; + fee = 0; + ContractMethodDescriptor init = null; + byte[] verification = witness.VerificationScript; + if (verification.Length == 0) + { + ContractState cs = snapshot.Contracts.TryGet(hash); + 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; + init = cs.Manifest.Abi.GetMethod("_initialize"); + } + else + { + if (NativeContract.IsNative(hash)) return false; + if (hash != witness.ScriptHash) return false; + offset = 0; + } + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) + { + CallFlags callFlags = !witness.IsStandardAccount ? CallFlags.AllowStates : CallFlags.None; + ExecutionContext context = engine.LoadScript(verification, callFlags, offset); + if (NativeContract.IsNative(hash)) { - if (NativeContract.IsNative(hashes[i])) return false; - if (hashes[i] != verifiable.Witnesses[i].ScriptHash) return false; - offset = 0; + using ScriptBuilder sb = new ScriptBuilder(); + sb.Emit(OpCode.DEPTH, OpCode.PACK); + sb.EmitPush("verify"); + engine.LoadScript(sb.ToArray(), CallFlags.None); } - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) + else if (init != null) { - CallFlags callFlags = verifiable.Witnesses[i].StateDependent ? CallFlags.AllowStates : CallFlags.None; - ExecutionContext context = engine.LoadScript(verification, callFlags, offset); - if (NativeContract.IsNative(hashes[i])) - { - using ScriptBuilder sb = new ScriptBuilder(); - sb.Emit(OpCode.DEPTH, OpCode.PACK); - sb.EmitPush("verify"); - engine.LoadScript(sb.ToArray(), CallFlags.None); - } - else if (init != null) - { - engine.LoadContext(context.Clone(init.Offset), false); - } - 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; + engine.LoadContext(context.Clone(init.Offset), false); } + engine.LoadScript(witness.InvocationScript, CallFlags.None); + if (engine.Execute() == VMState.FAULT) return false; + if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) return false; + fee = engine.GasConsumed; } return true; } diff --git a/src/neo/SmartContract/WitnessFlag.cs b/src/neo/SmartContract/WitnessFlag.cs deleted file mode 100644 index 528562a70f..0000000000 --- a/src/neo/SmartContract/WitnessFlag.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Neo.SmartContract -{ - [Flags] - internal enum WitnessFlag : byte - { - None = 0, - - StateIndependent = 0b00000001, - StateDependent = 0b00000010, - - All = StateIndependent | StateDependent - } -} From 61ef979785264eab2f8a6fdd7ae8f211cbcb2ed5 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 00:53:03 +0800 Subject: [PATCH 02/17] fix 1/2 --- src/neo/Network/P2P/Payloads/Transaction.cs | 20 +++++++++++++------- src/neo/Network/P2P/Payloads/Witness.cs | 4 ---- src/neo/SmartContract/Helper.cs | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index ab1883a063..f67aaedc5b 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -300,18 +300,24 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) { - if (!witnesses[i].IsStandardAccount) + if (witnesses[i].VerificationScript.IsSignatureContract()) { - if (!this.VerifyWitness(null, hashes[i], witnesses[i], net_fee, out long fee)) - return VerifyResult.Invalid; - net_fee -= fee; + // TODO fix fee + net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * 1); + } + else if (witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) + { + // TODO fix fee + net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * m + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * n); } else { - // TODO Calculate signature fee - //net_fee -= fee; + 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; } @@ -322,7 +328,7 @@ public virtual VerifyResult VerifyStateIndependent() UInt160[] hashes = GetScriptHashesForVerifying(null); if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) - if (witnesses[i].IsStandardAccount) + if (witnesses[i].VerificationScript.IsStandardContract()) if (!this.VerifyWitness(null, hashes[i], witnesses[i], NetworkFee, out _)) return VerifyResult.Invalid; return VerifyResult.Succeed; diff --git a/src/neo/Network/P2P/Payloads/Witness.cs b/src/neo/Network/P2P/Payloads/Witness.cs index a32b5cdba5..d6cfcf7470 100644 --- a/src/neo/Network/P2P/Payloads/Witness.cs +++ b/src/neo/Network/P2P/Payloads/Witness.cs @@ -22,8 +22,6 @@ public class Witness : ISerializable public byte[] InvocationScript; public byte[] VerificationScript; - internal long GasConsumed { get; set; } - private UInt160 _scriptHash; public virtual UInt160 ScriptHash { @@ -37,8 +35,6 @@ public virtual UInt160 ScriptHash } } - public bool IsStandardAccount => VerificationScript.Length != 0 && (VerificationScript.IsStandardContract() || VerificationScript.IsMultiSigContract()); - public int Size => InvocationScript.GetVarSize() + VerificationScript.GetVarSize(); void ISerializable.Deserialize(BinaryReader reader) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index c716482221..5806f2d28c 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -178,7 +178,7 @@ public static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot } using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) { - CallFlags callFlags = !witness.IsStandardAccount ? CallFlags.AllowStates : CallFlags.None; + CallFlags callFlags = !witness.VerificationScript.IsStandardContract() ? CallFlags.AllowStates : CallFlags.None; ExecutionContext context = engine.LoadScript(verification, callFlags, offset); if (NativeContract.IsNative(hash)) { From caa6f3c48cdd146012dc8fe83ed9ba5b7244a83c Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 11:15:33 +0800 Subject: [PATCH 03/17] fix fee 2/2 --- src/neo/Network/P2P/Payloads/Transaction.cs | 6 +-- .../SmartContract/UT_Contract.cs | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index f67aaedc5b..d24714a7bb 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -302,13 +302,11 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction { if (witnesses[i].VerificationScript.IsSignatureContract()) { - // TODO fix fee - net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * 1); + net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); } else if (witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) { - // TODO fix fee - net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * m + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * 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 { diff --git a/tests/neo.UnitTests/SmartContract/UT_Contract.cs b/tests/neo.UnitTests/SmartContract/UT_Contract.cs index 249018f69b..456959e060 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Contract.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Contract.cs @@ -1,5 +1,6 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.P2P.Payloads; using Neo.SmartContract; using Neo.VM; using Neo.Wallets; @@ -163,5 +164,53 @@ public void TestCreateSignatureRedeemScript() Array.Copy(BitConverter.GetBytes(ApplicationEngine.Neo_Crypto_VerifyWithECDsaSecp256r1), 0, expectedArray, 37, 4); CollectionAssert.AreEqual(expectedArray, script); } + + [TestMethod] + public void TestSignatureRedeemScriptFee() + { + byte[] privateKey = new byte[32]; + RandomNumberGenerator rng = RandomNumberGenerator.Create(); + rng.GetBytes(privateKey); + KeyPair key = new KeyPair(privateKey); + byte[] verification = Contract.CreateSignatureRedeemScript(key.PublicKey); + byte[] invocation = new ScriptBuilder().EmitPush(UInt160.Zero).ToArray(); + + var fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice; + + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + { + engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); + engine.Execute(); + engine.GasConsumed.Should().Be(fee); + } + } + + [TestMethod] + public void TestCreateMultiSigRedeemScriptFee() + { + byte[] privateKey1 = new byte[32]; + RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); + rng1.GetBytes(privateKey1); + KeyPair key1 = new KeyPair(privateKey1); + byte[] privateKey2 = new byte[32]; + RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); + rng2.GetBytes(privateKey2); + KeyPair key2 = new KeyPair(privateKey2); + Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; + publicKeys[0] = key1.PublicKey; + publicKeys[1] = key2.PublicKey; + publicKeys = publicKeys.OrderBy(p => p).ToArray(); + byte[] verification = Contract.CreateMultiSigRedeemScript(2, publicKeys); + byte[] invocation = new ScriptBuilder().EmitPush(UInt160.Zero).EmitPush(UInt160.Zero).ToArray(); + + long fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice * 2; + + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + { + engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); + engine.Execute(); + engine.GasConsumed.Should().Be(fee); + } + } } } From 9439df7771c586cbf9da3c657adb5d5451e4bdeb Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 11:17:29 +0800 Subject: [PATCH 04/17] optimize --- src/neo/Network/P2P/Payloads/Transaction.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index d24714a7bb..4e8bac1d41 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -301,21 +301,17 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction for (int i = 0; i < hashes.Length; i++) { if (witnesses[i].VerificationScript.IsSignatureContract()) - { net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); - } 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; } - if (net_fee < 0) return VerifyResult.InsufficientFunds; return VerifyResult.Succeed; } From 0641353cb1cd35cb236d06552429faf46ae7d84b Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 11:29:19 +0800 Subject: [PATCH 05/17] format --- src/neo/Network/P2P/Payloads/Transaction.cs | 2 +- tests/neo.UnitTests/SmartContract/UT_Contract.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 4e8bac1d41..53913579b4 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -303,7 +303,7 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction if (witnesses[i].VerificationScript.IsSignatureContract()) net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); 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); + 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)) diff --git a/tests/neo.UnitTests/SmartContract/UT_Contract.cs b/tests/neo.UnitTests/SmartContract/UT_Contract.cs index 456959e060..f541813ac6 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Contract.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Contract.cs @@ -177,7 +177,7 @@ public void TestSignatureRedeemScriptFee() var fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty() , Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); @@ -205,7 +205,7 @@ public void TestCreateMultiSigRedeemScriptFee() long fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice * 2; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty() , Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); From 4a6f3ae3985ad539ba5d17274f1cef9a8d4ee4d2 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 11:42:11 +0800 Subject: [PATCH 06/17] fix and format --- src/neo/Network/P2P/Payloads/Transaction.cs | 2 +- src/neo/SmartContract/Helper.cs | 2 +- tests/neo.UnitTests/SmartContract/UT_Contract.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 53913579b4..2461f46360 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -323,7 +323,7 @@ public virtual VerifyResult VerifyStateIndependent() if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) if (witnesses[i].VerificationScript.IsStandardContract()) - if (!this.VerifyWitness(null, hashes[i], witnesses[i], NetworkFee, out _)) + if (!this.VerifyWitness(null, hashes[i], witnesses[i], SmartContract.Helper.MaxVerificationGas, out _)) return VerifyResult.Invalid; return VerifyResult.Succeed; } diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 5806f2d28c..d2bc0f394d 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -14,7 +14,7 @@ namespace Neo.SmartContract { public static class Helper { - private const long MaxVerificationGas = 0_50000000; + public const long MaxVerificationGas = 0_50000000; public static UInt160 GetScriptHash(this ExecutionContext context) { diff --git a/tests/neo.UnitTests/SmartContract/UT_Contract.cs b/tests/neo.UnitTests/SmartContract/UT_Contract.cs index f541813ac6..3a97a88df2 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Contract.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Contract.cs @@ -177,7 +177,7 @@ public void TestSignatureRedeemScriptFee() var fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty(), Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); @@ -205,7 +205,7 @@ public void TestCreateMultiSigRedeemScriptFee() long fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice * 2; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty() , Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty(), Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); From 420ee5c266ff99179bf0b0b56c4b45780a1ca114 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 11 Nov 2020 11:45:53 +0800 Subject: [PATCH 07/17] format --- tests/neo.UnitTests/SmartContract/UT_Contract.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/neo.UnitTests/SmartContract/UT_Contract.cs b/tests/neo.UnitTests/SmartContract/UT_Contract.cs index 3a97a88df2..6c2f4b04f2 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Contract.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Contract.cs @@ -177,7 +177,7 @@ public void TestSignatureRedeemScriptFee() var fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty(), Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty(), Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); @@ -205,7 +205,7 @@ public void TestCreateMultiSigRedeemScriptFee() long fee = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice * 2; - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction{ Signers = Array.Empty(), Attributes = Array.Empty() }, null)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty(), Attributes = Array.Empty() }, null)) { engine.LoadScript(invocation.Concat(verification).ToArray(), CallFlags.None); engine.Execute(); From d69d84986695a083fd0893b047fc78ff7f9df8e3 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 25 Nov 2020 10:51:41 +0800 Subject: [PATCH 08/17] optimize --- src/neo/SmartContract/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 3c8d72a483..4f68a3d95f 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -144,7 +144,7 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap return false; } if (hashes.Length != verifiable.Witnesses.Length) return false; - for (uint i = 0; i < hashes.Length; i++) + for (int i = 0; i < hashes.Length; i++) { if (!verifiable.VerifyWitness(snapshot, hashes[i], verifiable.Witnesses[i], gas, out long fee)) return false; From 177cd44b568135db6e20d30906eb90c9381b2a5b Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 2 Dec 2020 20:10:51 +0800 Subject: [PATCH 09/17] fix --- src/neo/Network/P2P/Payloads/Transaction.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 2461f46360..ef16997ac7 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -300,9 +300,9 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) { - if (witnesses[i].VerificationScript.IsSignatureContract()) + if (witnesses[i].VerificationScript != null && witnesses[i].VerificationScript.IsSignatureContract()) net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); - else if (witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) + else if (witnesses[i].VerificationScript != null && 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 { @@ -322,7 +322,7 @@ public virtual VerifyResult VerifyStateIndependent() 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()) + if (witnesses[i].VerificationScript != null && witnesses[i].VerificationScript.IsStandardContract()) if (!this.VerifyWitness(null, hashes[i], witnesses[i], SmartContract.Helper.MaxVerificationGas, out _)) return VerifyResult.Invalid; return VerifyResult.Succeed; From 3fabce96ed46ac5bb7de806f8c9256a7ced213fa Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Wed, 2 Dec 2020 20:15:26 +0800 Subject: [PATCH 10/17] Revert --- src/neo/Network/P2P/Payloads/Transaction.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index ef16997ac7..2461f46360 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -300,9 +300,9 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) { - if (witnesses[i].VerificationScript != null && witnesses[i].VerificationScript.IsSignatureContract()) + if (witnesses[i].VerificationScript.IsSignatureContract()) net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); - else if (witnesses[i].VerificationScript != null && witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) + 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 { @@ -322,7 +322,7 @@ public virtual VerifyResult VerifyStateIndependent() UInt160[] hashes = GetScriptHashesForVerifying(null); if (hashes.Length != witnesses.Length) return VerifyResult.Invalid; for (int i = 0; i < hashes.Length; i++) - if (witnesses[i].VerificationScript != null && witnesses[i].VerificationScript.IsStandardContract()) + if (witnesses[i].VerificationScript.IsStandardContract()) if (!this.VerifyWitness(null, hashes[i], witnesses[i], SmartContract.Helper.MaxVerificationGas, out _)) return VerifyResult.Invalid; return VerifyResult.Succeed; From 43324e8fc399f52928cee9c6320c594f86e3c640 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 2 Dec 2020 16:23:23 +0100 Subject: [PATCH 11/17] Optimize --- src/neo/Network/P2P/Payloads/Transaction.cs | 2 +- src/neo/SmartContract/Helper.cs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 2461f46360..21afb2e2a8 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -301,7 +301,7 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction for (int i = 0; i < hashes.Length; i++) { if (witnesses[i].VerificationScript.IsSignatureContract()) - net_fee -= (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice); + 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 diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 3a97ef4af5..c290dde602 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -14,6 +14,11 @@ namespace Neo.SmartContract public static class Helper { public const long MaxVerificationGas = 0_50000000; + public static readonly long SignatureContractCost = + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + + ApplicationEngine.ECDsaVerifyPrice; public static UInt160 GetContractHash(UInt160 sender, byte[] script) { From 67ad7468690b7ed8a892db6713091b4ec8a34946 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 3 Dec 2020 18:30:41 +0800 Subject: [PATCH 12/17] Update src/neo/SmartContract/Helper.cs Co-authored-by: Shargon --- src/neo/SmartContract/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index c290dde602..59a8bdf8bb 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -192,7 +192,7 @@ public static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot engine.LoadScript(witness.InvocationScript, CallFlags.None); if (engine.Execute() == VMState.FAULT) return false; - if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) return false; + if (engine.ResultStack.Count != 1 || !engine.ResultStack.Peek().GetBoolean()) return false; fee = engine.GasConsumed; } return true; From fda5a2875096bdda459f5412e0d6c36a17f07cff Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Thu, 3 Dec 2020 18:40:53 +0800 Subject: [PATCH 13/17] Change to Internal --- src/neo/SmartContract/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 59a8bdf8bb..2951f911e2 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -168,7 +168,7 @@ internal static bool VerifyWitnesses(this IVerifiable verifiable, StoreView snap return true; } - public static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot, UInt160 hash, Witness witness, long gas, out long fee) + internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapshot, UInt160 hash, Witness witness, long gas, out long fee) { fee = 0; using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, verifiable, snapshot?.Clone(), gas)) From f09512b06d3e77b25839bc129c2e349da202594e Mon Sep 17 00:00:00 2001 From: Luchuan Date: Thu, 3 Dec 2020 19:43:08 +0800 Subject: [PATCH 14/17] Update src/neo/SmartContract/Helper.cs Co-authored-by: Shargon --- src/neo/SmartContract/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 2951f911e2..f97b59dd50 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -14,7 +14,7 @@ namespace Neo.SmartContract public static class Helper { public const long MaxVerificationGas = 0_50000000; - public static readonly long SignatureContractCost = + internal static readonly long SignatureContractCost = ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + From c340bfc3f5e6ffea6f64abab29b928a60b4480df Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Fri, 4 Dec 2020 10:27:58 +0800 Subject: [PATCH 15/17] add MultiSignatureContractCost --- src/neo/Network/P2P/Payloads/Transaction.cs | 2 +- src/neo/SmartContract/Helper.cs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 21afb2e2a8..b5fc216731 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -303,7 +303,7 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction 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); + net_fee -= SmartContract.Helper.MultiSignatureContractCost(m, n); else { if (!this.VerifyWitness(null, hashes[i], witnesses[i], net_fee, out long fee)) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index f97b59dd50..92e6961507 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -19,6 +19,12 @@ public static class Helper ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.ECDsaVerifyPrice; + internal static long MultiSignatureContractCost(int m, int n) => + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (m + n) + + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + + ApplicationEngine.ECDsaVerifyPrice * n; public static UInt160 GetContractHash(UInt160 sender, byte[] script) { From 7e62e797859608b2421c1b04c9e9395e3140f741 Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Fri, 4 Dec 2020 15:23:57 +0800 Subject: [PATCH 16/17] Move SignatureCost to tx --- src/neo/Network/P2P/Payloads/Transaction.cs | 16 ++++++++++++++-- src/neo/SmartContract/Helper.cs | 13 +------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index b5fc216731..11b5e0a3ef 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -35,6 +35,18 @@ public class Transaction : IEquatable, IInventory, IInteroperable private byte[] script; private Witness[] witnesses; + private static readonly long SignatureContractCost = + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + + ApplicationEngine.ECDsaVerifyPrice; + private static long MultiSignatureContractCost(int m, int n) => + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (m + n) + + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + + ApplicationEngine.ECDsaVerifyPrice * n; + public const int HeaderSize = sizeof(byte) + //Version sizeof(uint) + //Nonce @@ -301,9 +313,9 @@ public virtual VerifyResult VerifyStateDependent(StoreView snapshot, Transaction for (int i = 0; i < hashes.Length; i++) { if (witnesses[i].VerificationScript.IsSignatureContract()) - net_fee -= SmartContract.Helper.SignatureContractCost; + net_fee -= SignatureContractCost; else if (witnesses[i].VerificationScript.IsMultiSigContract(out int m, out int n)) - net_fee -= SmartContract.Helper.MultiSignatureContractCost(m, n); + net_fee -= MultiSignatureContractCost(m, n); else { if (!this.VerifyWitness(null, hashes[i], witnesses[i], net_fee, out long fee)) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 92e6961507..4be3016dbe 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -13,18 +13,7 @@ namespace Neo.SmartContract { public static class Helper { - public const long MaxVerificationGas = 0_50000000; - internal static readonly long SignatureContractCost = - ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * 2 + - ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + - ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + - ApplicationEngine.ECDsaVerifyPrice; - internal static long MultiSignatureContractCost(int m, int n) => - ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (m + n) + - ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + - ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + - ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + - ApplicationEngine.ECDsaVerifyPrice * n; + public const long MaxVerificationGas = 0_50000000; public static UInt160 GetContractHash(UInt160 sender, byte[] script) { From cd4969bcb05fd9972d486deb751a1a51740c4cfb Mon Sep 17 00:00:00 2001 From: Tommo-L Date: Fri, 4 Dec 2020 15:27:08 +0800 Subject: [PATCH 17/17] format --- src/neo/SmartContract/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Helper.cs b/src/neo/SmartContract/Helper.cs index 4be3016dbe..5a33a8a8db 100644 --- a/src/neo/SmartContract/Helper.cs +++ b/src/neo/SmartContract/Helper.cs @@ -13,7 +13,7 @@ namespace Neo.SmartContract { public static class Helper { - public const long MaxVerificationGas = 0_50000000; + public const long MaxVerificationGas = 0_50000000; public static UInt160 GetContractHash(UInt160 sender, byte[] script) {