Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 30 additions & 13 deletions src/Neo/Wallets/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using Neo.Cryptography;
using Neo.Extensions;
using Neo.IO;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static string ToAddress(this UInt160 scriptHash, byte version)
/// <returns>The converted script hash.</returns>
public static UInt160 ToScriptHash(this string address, byte version)
{
byte[] data = address.Base58CheckDecode();
var data = address.Base58CheckDecode();
if (data.Length != 21)
throw new FormatException();
if (data[0] != version)
Expand All @@ -74,12 +75,28 @@ public static UInt160 ToScriptHash(this string address, byte version)
internal static byte[] XOR(byte[] x, byte[] y)
{
if (x.Length != y.Length) throw new ArgumentException();
byte[] r = new byte[x.Length];
for (int i = 0; i < r.Length; i++)
var r = new byte[x.Length];
for (var i = 0; i < r.Length; i++)
r[i] = (byte)(x[i] ^ y[i]);
return r;
}

/// <summary>
/// Calculates the network fee for the specified transaction.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS
/// </summary>
/// <param name="tx">The transaction to calculate.</param>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="settings">Thr protocol settings to use.</param>
/// <param name="wallet">User wallet.</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 static long CalculateNetworkFee(this Transaction tx, DataCache snapshot, ProtocolSettings settings, Wallet? wallet = null, long maxExecutionCost = ApplicationEngine.TestModeGas)
{
Func<UInt160, byte[]?>? accountScript = wallet != null ? (scriptHash) => wallet.GetAccount(scriptHash)?.Contract?.Script : null;
return CalculateNetworkFee(tx, snapshot, settings, accountScript, maxExecutionCost);
}

/// <summary>
/// Calculates the network fee for the specified transaction.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS
Expand All @@ -90,24 +107,24 @@ internal static byte[] XOR(byte[] x, byte[] y)
/// <param name="accountScript">Function to retrive the script's account from a hash.</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 static long CalculateNetworkFee(this Transaction tx, DataCache snapshot, ProtocolSettings settings, Func<UInt160, byte[]> accountScript, long maxExecutionCost = ApplicationEngine.TestModeGas)
public static long CalculateNetworkFee(this Transaction tx, DataCache snapshot, ProtocolSettings settings, Func<UInt160, byte[]?>? accountScript, long maxExecutionCost = ApplicationEngine.TestModeGas)
{
UInt160[] hashes = tx.GetScriptHashesForVerifying(snapshot);
var hashes = tx.GetScriptHashesForVerifying(snapshot);

// base size for transaction: includes const_header + signers + attributes + script + hashes
int size = Transaction.HeaderSize + tx.Signers.GetVarSize() + tx.Attributes.GetVarSize() + tx.Script.GetVarSize() + UnsafeData.GetVarSize(hashes.Length), index = -1;
uint exec_fee_factor = NativeContract.Policy.GetExecFeeFactor(snapshot);
var exec_fee_factor = NativeContract.Policy.GetExecFeeFactor(snapshot);
long networkFee = 0;
foreach (UInt160 hash in hashes)
foreach (var hash in hashes)
{
index++;
byte[] witnessScript = accountScript(hash);
byte[] invocationScript = null;
var witnessScript = accountScript != null ? accountScript(hash) : null;
byte[]? invocationScript = null;

if (tx.Witnesses != null && witnessScript is null)
{
// Try to find the script in the witnesses
Witness witness = tx.Witnesses[index];
var witness = tx.Witnesses[index];
witnessScript = witness?.VerificationScript.ToArray();

if (witnessScript is null || witnessScript.Length == 0)
Expand Down Expand Up @@ -193,14 +210,14 @@ public static long CalculateNetworkFee(this Transaction tx, DataCache snapshot,
}
else if (IsMultiSigContract(witnessScript, out int m, out int n))
{
int size_inv = 66 * m;
var size_inv = 66 * m;
size += UnsafeData.GetVarSize(size_inv) + size_inv + witnessScript.GetVarSize();
networkFee += exec_fee_factor * MultiSignatureContractCost(m, n);
}
}
}
networkFee += size * NativeContract.Policy.GetFeePerByte(snapshot);
foreach (TransactionAttribute attr in tx.Attributes)
foreach (var attr in tx.Attributes)
{
networkFee += attr.CalculateNetworkFee(snapshot, tx);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ private Transaction MakeTransaction(DataCache snapshot, ReadOnlyMemory<byte> scr
tx.SystemFee = engine.FeeConsumed;
}

tx.NetworkFee = tx.CalculateNetworkFee(snapshot, ProtocolSettings, (a) => GetAccount(a)?.Contract?.Script, maxGas);
tx.NetworkFee = tx.CalculateNetworkFee(snapshot, ProtocolSettings, this, maxGas);
if (value >= tx.SystemFee + tx.NetworkFee) return tx;
}
throw new InvalidOperationException("Insufficient GAS");
Expand Down
4 changes: 1 addition & 3 deletions src/Plugins/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ protected internal virtual JToken CalculateNetworkFee(JArray _params)
var tx = Result.Ok_Or(() => Convert.FromBase64String(_params[0].AsString()), RpcError.InvalidParams.WithData($"Invalid tx: {_params[0]}")); ;

JObject account = new();
var networkfee = Wallets.Helper.CalculateNetworkFee(
tx.AsSerializable<Transaction>(), system.StoreView, system.Settings,
wallet is not null ? a => wallet.GetAccount(a).Contract.Script : _ => null);
var networkfee = Wallets.Helper.CalculateNetworkFee(tx.AsSerializable<Transaction>(), system.StoreView, system.Settings, wallet);
account["networkfee"] = networkfee.ToString();
return account;
}
Expand Down
Loading