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

Extend CalculateNetworkFee with contract verification #3385

Merged
merged 30 commits into from
Nov 5, 2024
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
099f7be
Calculate fee
shargon Jul 2, 2024
3a332fa
change values
shargon Jul 2, 2024
ad3d477
remove using
shargon Jul 2, 2024
7b6ccb9
fix using
shargon Jul 2, 2024
78ddbb1
Remove comments
shargon Jul 2, 2024
eccec43
Merge branch 'master' into core-calculate-fee
cschuchardt88 Jul 4, 2024
8a8af47
Remove map
shargon Jul 5, 2024
20935fd
Merge branch 'master' into core-calculate-fee
shargon Jul 9, 2024
58cab0d
Anna's feedback
shargon Jul 11, 2024
e2cc49f
ensure only one result
shargon Jul 11, 2024
1d071a5
Merge branch 'master' into core-calculate-fee
cschuchardt88 Jul 11, 2024
dabcd58
Merge branch 'master' into core-calculate-fee
Jim8y Jul 15, 2024
45588a9
Merge branch 'master' into core-calculate-fee
Jim8y Jul 22, 2024
373e1d0
Merge branch 'master' into core-calculate-fee
shargon Jul 23, 2024
e4fa517
Merge branch 'master' into core-calculate-fee
cschuchardt88 Jul 23, 2024
56f39ed
Merge branch 'master' into core-calculate-fee
Jim8y Jul 30, 2024
3812a45
Update src/Neo/Wallets/Helper.cs
shargon Jul 30, 2024
c0f03b1
Merge branch 'master' into core-calculate-fee
shargon Jul 30, 2024
15a563d
Merge branch 'master' into core-calculate-fee
shargon Jul 30, 2024
8ad1acf
Remove error
shargon Jul 31, 2024
6fa6ef9
Merge branch 'master' into core-calculate-fee
shargon Aug 1, 2024
2745757
Merge branch 'master' into core-calculate-fee
shargon Aug 6, 2024
149f910
Merge branch 'master' into core-calculate-fee
shargon Aug 9, 2024
590e83d
Merge branch 'master' into core-calculate-fee
Jim8y Sep 5, 2024
ea69990
Merge branch 'master' into core-calculate-fee
shargon Sep 16, 2024
e7154ef
Merge branch 'master' into core-calculate-fee
shargon Oct 14, 2024
fca78e8
Merge branch 'master' into core-calculate-fee
shargon Oct 17, 2024
f4234d1
Merge branch 'master' into core-calculate-fee
Jim8y Oct 24, 2024
256e1e2
Fix compilation
shargon Oct 24, 2024
d682870
Merge branch 'master' into core-calculate-fee
Jim8y Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions src/Neo/Wallets/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using static Neo.SmartContract.Helper;

Expand Down Expand Up @@ -118,6 +119,7 @@ public static long CalculateNetworkFee(this Transaction tx, DataCache snapshot,

if (witnessScript is null || witnessScript.Length == 0)
{
// Contract-based verification
var contract = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (contract is null)
throw new ArgumentException($"The smart contract or address {hash} ({hash.ToAddress(settings.AddressVersion)}) is not found. " +
Expand All @@ -128,35 +130,74 @@ public static long CalculateNetworkFee(this Transaction tx, DataCache snapshot,
if (md.ReturnType != ContractParameterType.Boolean)
throw new ArgumentException("The verify method doesn't return boolean value.");
if (md.Parameters.Length > 0 && invocationScript is null)
throw new ArgumentException("The verify method requires parameters that need to be passed via the witness' invocation script.");
{
var script = new ScriptBuilder();
foreach (var par in md.Parameters)
{
switch (par.Type)
{
case ContractParameterType.Any:
case ContractParameterType.Signature:
case ContractParameterType.String:
case ContractParameterType.ByteArray:
script.EmitPush(new byte[64]);
break;
case ContractParameterType.Boolean:
script.EmitPush(true);
break;
case ContractParameterType.Integer:
script.Emit(OpCode.PUSHINT256, new byte[Integer.MaxSize]);
break;
case ContractParameterType.Hash160:
script.EmitPush(new byte[UInt160.Length]);
break;
case ContractParameterType.Hash256:
script.EmitPush(new byte[UInt256.Length]);
break;
case ContractParameterType.PublicKey:
script.EmitPush(new byte[33]);
break;
case ContractParameterType.Array:
script.Emit(OpCode.NEWARRAY0);
break;
}
}
invocationScript = script.ToArray();
}

// Empty verification and non-empty invocation scripts
var invSize = invocationScript?.GetVarSize() ?? Array.Empty<byte>().GetVarSize();
size += Array.Empty<byte>().GetVarSize() + invSize;
var invSize = invocationScript?.GetVarSize() ?? System.Array.Empty<byte>().GetVarSize();
size += System.Array.Empty<byte>().GetVarSize() + invSize;

// Check verify cost
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.CloneCache(), settings: settings, 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.");

if (engine.Execute() == VMState.HALT)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
// https://github.com/neo-project/neo/issues/2805
if (engine.ResultStack.Count != 1) throw new ArgumentException($"Smart contract {contract.Hash} verification fault.");
_ = engine.ResultStack.Pop().GetBoolean(); // Ensure that the result is boolean
}
maxExecutionCost -= engine.FeeConsumed;
if (maxExecutionCost <= 0) throw new InvalidOperationException("Insufficient GAS.");
networkFee += engine.FeeConsumed;
}
else if (IsSignatureContract(witnessScript))
else
{
size += 67 + witnessScript.GetVarSize();
networkFee += exec_fee_factor * SignatureContractCost();
}
else if (IsMultiSigContract(witnessScript, out int m, out int n))
{
int size_inv = 66 * m;
size += UnsafeData.GetVarSize(size_inv) + size_inv + witnessScript.GetVarSize();
networkFee += exec_fee_factor * MultiSignatureContractCost(m, n);
// Regular signature verification.
if (IsSignatureContract(witnessScript))
{
size += 67 + witnessScript.GetVarSize();
networkFee += exec_fee_factor * SignatureContractCost();
}
else if (IsMultiSigContract(witnessScript, out int m, out int n))
{
int size_inv = 66 * m;
size += UnsafeData.GetVarSize(size_inv) + size_inv + witnessScript.GetVarSize();
networkFee += exec_fee_factor * MultiSignatureContractCost(m, n);
}
}
// We can support more contract types in the future.
}
networkFee += size * NativeContract.Policy.GetFeePerByte(snapshot);
foreach (TransactionAttribute attr in tx.Attributes)
Expand Down
Loading