Skip to content

Commit

Permalink
P2P: move NotaryAssisted transaction attribute under D hardfork
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Jun 11, 2024
1 parent 042440f commit eded6bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/Neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,15 @@ public virtual VerifyResult VerifyStateDependent(ProtocolSettings settings, Data
if (!(context?.CheckTransaction(this, conflictsList, snapshot) ?? true)) return VerifyResult.InsufficientFunds;
long attributesFee = 0;
foreach (TransactionAttribute attribute in Attributes)
{
if (!attribute.Verify(snapshot, this))
return VerifyResult.InvalidAttribute;
else
attributesFee += attribute.CalculateNetworkFee(snapshot, this);
if (attribute.Type == TransactionAttributeType.NotaryAssisted && !settings.IsHardforkEnabled(Hardfork.HF_Domovoi, height))
return VerifyResult.InvalidAttribute;
attributesFee += attribute.CalculateNetworkFee(snapshot, this);
}
long netFeeDatoshi = NetworkFee - (Size * NativeContract.Policy.GetFeePerByte(snapshot)) - attributesFee;
if (netFeeDatoshi < 0) return VerifyResult.InsufficientFunds;

if (netFeeDatoshi > MaxVerificationGas) netFeeDatoshi = MaxVerificationGas;
uint execFeeFactor = NativeContract.Policy.GetExecFeeFactor(snapshot);
for (int i = 0; i < hashes.Length; i++)
Expand Down
77 changes: 71 additions & 6 deletions src/Neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ internal override ContractTask InitializeAsync(ApplicationEngine engine, Hardfor
engine.Snapshot.Add(CreateStorageKey(Prefix_FeePerByte), new StorageItem(DefaultFeePerByte));
engine.Snapshot.Add(CreateStorageKey(Prefix_ExecFeeFactor), new StorageItem(DefaultExecFeeFactor));
engine.Snapshot.Add(CreateStorageKey(Prefix_StoragePrice), new StorageItem(DefaultStoragePrice));
}
if (hardfork == Hardfork.HF_Domovoi)
{
engine.Snapshot.Add(CreateStorageKey(Prefix_AttributeFee).Add((byte)TransactionAttributeType.NotaryAssisted), new StorageItem(DefaultNotaryAssistedAttributeFee));
}
return ContractTask.CompletedTask;
Expand Down Expand Up @@ -118,21 +121,48 @@ public uint GetStoragePrice(DataCache snapshot)
}

/// <summary>
/// Gets the fee for attribute.
/// Gets the fee for attribute before Domovoi hardfork.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="attributeType">Attribute type excluding <see cref="TransactionAttributeType.NotaryAssisted"/></param>
/// <returns>The fee for attribute.</returns>
[ContractMethod(Hardfork.HF_Domovoi, CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates, Name = "getAttributeFee")]
public uint GetAttributeFeeV0(DataCache snapshot, byte attributeType)
{
return GetAttributeFee(snapshot, attributeType, false);
}

/// <summary>
/// Gets the fee for attribute after Domovoi hardfork.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="attributeType">Attribute type</param>
/// <returns>The fee for attribute.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
[ContractMethod(true, Hardfork.HF_Domovoi, CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public uint GetAttributeFee(DataCache snapshot, byte attributeType)
{
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType)) throw new InvalidOperationException();
return GetAttributeFee(snapshot, attributeType, true);
}

/// <summary>
/// Generic handler for GetAttributeFeeV0 and GetAttributeFeeV1 that
/// gets the fee for attribute.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="attributeType">Attribute type</param>
/// <param name="allowNotaryAssisted">Whether to support <see cref="TransactionAttributeType.NotaryAssisted"/> attribute type.</param>
/// <returns>The fee for attribute.</returns>
private uint GetAttributeFee(DataCache snapshot, byte attributeType, bool allowNotaryAssisted)
{
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType) || (!allowNotaryAssisted && attributeType == (byte)(TransactionAttributeType.NotaryAssisted)))
throw new InvalidOperationException();
StorageItem entry = snapshot.TryGet(CreateStorageKey(Prefix_AttributeFee).Add(attributeType));
if (entry == null) return DefaultAttributeFee;

return (uint)(BigInteger)entry;
}


/// <summary>
/// Determines whether the specified account is blocked.
/// </summary>
Expand All @@ -145,10 +175,45 @@ public bool IsBlocked(DataCache snapshot, UInt160 account)
return snapshot.Contains(CreateStorageKey(Prefix_BlockedAccount).Add(account));
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetAttributeFee(ApplicationEngine engine, byte attributeType, uint value)
/// <summary>
/// Sets the fee for attribute before Domovoi hardfork.
/// </summary>
/// <param name="engine">The engine used to check committee witness and read data.</param>
/// <param name="attributeType">Attribute type excluding <see cref="TransactionAttributeType.NotaryAssisted"/></param>
/// <param name="value">Attribute fee value</param>
/// <returns>The fee for attribute.</returns>
[ContractMethod(Hardfork.HF_Domovoi, CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States, Name = "setAttributeFee")]
private void SetAttributeFeeV0(ApplicationEngine engine, byte attributeType, uint value)
{
SetAttributeFee(engine, attributeType, value, false);
}

/// <summary>
/// Sets the fee for attribute after Domovoi hardfork.
/// </summary>
/// <param name="engine">The engine used to check committee witness and read data.</param>
/// <param name="attributeType">Attribute type excluding <see cref="TransactionAttributeType.NotaryAssisted"/></param>
/// <param name="value">Attribute fee value</param>
/// <returns>The fee for attribute.</returns>
[ContractMethod(true, Hardfork.HF_Domovoi, CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States, Name = "setAttributeFee")]
private void SetAttributeFeeV1(ApplicationEngine engine, byte attributeType, uint value)
{
SetAttributeFee(engine, attributeType, value, true);
}

/// <summary>
/// Generic handler for SetAttributeFeeV0 and SetAttributeFeeV1 that
/// gets the fee for attribute.
/// </summary>
/// <param name="engine">The engine used to check committee witness and read data.</param>
/// <param name="attributeType">Attribute type</param>
/// <param name="value">Attribute fee value</param>
/// <param name="allowNotaryAssisted">Whether to support <see cref="TransactionAttributeType.NotaryAssisted"/> attribute type.</param>
/// <returns>The fee for attribute.</returns>
private void SetAttributeFee(ApplicationEngine engine, byte attributeType, uint value, bool allowNotaryAssisted)
{
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType)) throw new InvalidOperationException();
if (!Enum.IsDefined(typeof(TransactionAttributeType), attributeType) || (!allowNotaryAssisted && attributeType == (byte)(TransactionAttributeType.NotaryAssisted)))
throw new InvalidOperationException();
if (value > MaxAttributeFee) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();

Expand Down

0 comments on commit eded6bd

Please sign in to comment.