From 0e9cb9ff354aaf8c608b6e5e4637466ce225e881 Mon Sep 17 00:00:00 2001 From: belane Date: Fri, 29 Sep 2023 19:03:03 +0200 Subject: [PATCH] Update CalculateNetworkFee (#2920) * Update CalculateNetworkFee * Clean code * Add argument * Fix MakeTransaction --------- Co-authored-by: Shargon --- src/Neo/Wallets/Wallet.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Neo/Wallets/Wallet.cs b/src/Neo/Wallets/Wallet.cs index 60a3b60891..7ca5aa8b7b 100644 --- a/src/Neo/Wallets/Wallet.cs +++ b/src/Neo/Wallets/Wallet.cs @@ -575,7 +575,7 @@ private Transaction MakeTransaction(DataCache snapshot, ReadOnlyMemory scr tx.SystemFee = engine.GasConsumed; } - tx.NetworkFee = CalculateNetworkFee(snapshot, tx); + tx.NetworkFee = CalculateNetworkFee(snapshot, tx, maxGas); if (value >= tx.SystemFee + tx.NetworkFee) return tx; } throw new InvalidOperationException("Insufficient GAS"); @@ -586,8 +586,9 @@ private Transaction MakeTransaction(DataCache snapshot, ReadOnlyMemory scr /// /// The snapshot used to read data. /// The transaction to calculate. + /// The maximum cost that can be spent when a contract is executed. /// The network fee of the transaction. - public long CalculateNetworkFee(DataCache snapshot, Transaction tx) + public long CalculateNetworkFee(DataCache snapshot, Transaction tx, long maxExecutionCost = ApplicationEngine.TestModeGas) { UInt160[] hashes = tx.GetScriptHashesForVerifying(snapshot); @@ -636,12 +637,14 @@ public long CalculateNetworkFee(DataCache snapshot, Transaction tx) size += Array.Empty().GetVarSize() + invSize; // Check verify cost - using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.CreateSnapshot(), settings: ProtocolSettings); + using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.CreateSnapshot(), settings: ProtocolSettings, gas: maxExecutionCost); engine.LoadContract(contract, md, CallFlags.ReadOnly); if (invocationScript != null) engine.LoadScript(invocationScript, configureState: p => p.CallFlags = CallFlags.None); if (engine.Execute() == VMState.FAULT) throw new ArgumentException($"Smart contract {contract.Hash} verification fault."); if (!engine.ResultStack.Pop().GetBoolean()) throw new ArgumentException($"Smart contract {contract.Hash} returns false."); + maxExecutionCost -= engine.GasConsumed; + if (maxExecutionCost <= 0) throw new InvalidOperationException("Insufficient GAS."); networkFee += engine.GasConsumed; } else if (IsSignatureContract(witness_script)) @@ -655,10 +658,7 @@ public long CalculateNetworkFee(DataCache snapshot, Transaction tx) size += IO.Helper.GetVarSize(size_inv) + size_inv + witness_script.GetVarSize(); networkFee += exec_fee_factor * MultiSignatureContractCost(m, n); } - else - { - //We can support more contract types in the future. - } + // We can support more contract types in the future. } networkFee += size * NativeContract.Policy.GetFeePerByte(snapshot); return networkFee;