Skip to content

Commit

Permalink
Update CalculateNetworkFee (neo-project#2920)
Browse files Browse the repository at this point in the history
* Update CalculateNetworkFee

* Clean code

* Add argument

* Fix MakeTransaction

---------

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
belane and shargon authored Sep 29, 2023
1 parent f6d578b commit 0e9cb9f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ private Transaction MakeTransaction(DataCache snapshot, ReadOnlyMemory<byte> 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");
Expand All @@ -586,8 +586,9 @@ private Transaction MakeTransaction(DataCache snapshot, ReadOnlyMemory<byte> scr
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="tx">The transaction to calculate.</param>
/// <param name="maxExecutionCost">The maximum cost that can be spent when a contract is executed.</param>
/// <returns>The network fee of the transaction.</returns>
public long CalculateNetworkFee(DataCache snapshot, Transaction tx)
public long CalculateNetworkFee(DataCache snapshot, Transaction tx, long maxExecutionCost = ApplicationEngine.TestModeGas)
{
UInt160[] hashes = tx.GetScriptHashesForVerifying(snapshot);

Expand Down Expand Up @@ -636,12 +637,14 @@ public long CalculateNetworkFee(DataCache snapshot, Transaction tx)
size += Array.Empty<byte>().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))
Expand All @@ -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;
Expand Down

0 comments on commit 0e9cb9f

Please sign in to comment.