Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 26 additions & 13 deletions src/Neo/SmartContract/Native/ContractEventAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,48 @@ namespace Neo.SmartContract.Native
{
[DebuggerDisplay("{Descriptor.Name}")]
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = true)]
internal class ContractEventAttribute : Attribute
internal class ContractEventAttribute : Attribute, IHardforkActivable
{
public int Order { get; init; }
public ContractEventDescriptor Descriptor { get; set; }
public Hardfork? ActiveIn { get; init; } = null;
public Hardfork? DeprecatedIn { get; init; } = null;

public ContractEventAttribute(Hardfork activeIn, int order, string name,
string arg1Name, ContractParameterType arg1Value) : this(order, name, arg1Name, arg1Value)
{
ActiveIn = activeIn;
}

public ContractEventAttribute(Hardfork activeIn, int order, string name,
string arg1Name, ContractParameterType arg1Value, Hardfork deprecatedIn) : this(activeIn, order, name, arg1Name, arg1Value)
{
DeprecatedIn = deprecatedIn;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need all these constructors? Unless their required and absolutely needed than yes, otherwise just make properties your setting Public. Public for optional.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because enum doesn't work as arguments :(

public ContractEventAttribute(int order, string name, string arg1Name, ContractParameterType arg1Value)
{
Order = order;
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Type = arg1Value
}
}
]
};
}

public ContractEventAttribute(int order, string name, string arg1Name, ContractParameterType arg1Value, Hardfork deprecatedIn)
: this(order, name, arg1Name, arg1Value)
{
DeprecatedIn = deprecatedIn;
}

public ContractEventAttribute(Hardfork activeIn, int order, string name,
string arg1Name, ContractParameterType arg1Value,
string arg2Name, ContractParameterType arg2Value) : this(order, name, arg1Name, arg1Value, arg2Name, arg2Value)
Expand All @@ -61,8 +74,8 @@ public ContractEventAttribute(int order, string name,
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -73,7 +86,7 @@ public ContractEventAttribute(int order, string name,
Name = arg2Name,
Type = arg2Value
}
}
]
};
}

Expand All @@ -95,8 +108,8 @@ public ContractEventAttribute(int order, string name,
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -112,7 +125,7 @@ public ContractEventAttribute(int order, string name,
Name = arg3Name,
Type = arg3Value
}
}
]
};
}

Expand All @@ -136,8 +149,8 @@ public ContractEventAttribute(int order, string name,
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -158,7 +171,7 @@ public ContractEventAttribute(int order, string name,
Name = arg4Name,
Type = arg4Value
}
}
]
};
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Neo/SmartContract/Native/ContractMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Neo.SmartContract.Native
{
[DebuggerDisplay("{Name}")]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false)]
internal class ContractMethodAttribute : Attribute
internal class ContractMethodAttribute : Attribute, IHardforkActivable
{
public string Name { get; init; }
public CallFlags RequiredCallFlags { get; init; }
Expand All @@ -32,6 +32,11 @@ public ContractMethodAttribute(Hardfork activeIn)
ActiveIn = activeIn;
}

public ContractMethodAttribute(Hardfork activeIn, Hardfork deprecatedIn) : this(activeIn)
{
DeprecatedIn = deprecatedIn;
}

public ContractMethodAttribute(bool isDeprecated, Hardfork deprecatedIn)
{
if (!isDeprecated) throw new ArgumentException("isDeprecated must be true", nameof(isDeprecated));
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/ContractMethodMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Neo.SmartContract.Native
{
[DebuggerDisplay("{Name}")]
internal class ContractMethodMetadata
internal class ContractMethodMetadata : IHardforkActivable
{
public string Name { get; }
public MethodInfo Handler { get; }
Expand Down
19 changes: 19 additions & 0 deletions src/Neo/SmartContract/Native/IHardforkActivable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// IHardforkActivable.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

namespace Neo.SmartContract.Native
{
internal interface IHardforkActivable
{
public Hardfork? ActiveIn { get; }
public Hardfork? DeprecatedIn { get; }
}
}
23 changes: 13 additions & 10 deletions src/Neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ protected NativeContract()
_usedHardforks =
_methodDescriptors.Select(u => u.ActiveIn)
.Concat(_methodDescriptors.Select(u => u.DeprecatedIn))
.Concat(_eventsDescriptors.Select(u => u.DeprecatedIn))
.Concat(_eventsDescriptors.Select(u => u.ActiveIn))
.Concat([ActiveIn])
.Where(u => u is not null)
Expand All @@ -184,15 +185,7 @@ private NativeContractsCache.CacheEntry GetAllowedMethods(IsHardforkEnabledDeleg
byte[] script;
using (ScriptBuilder sb = new())
{
foreach (ContractMethodMetadata method in _methodDescriptors.Where(u
=>
// no hardfork is involved
u.ActiveIn is null && u.DeprecatedIn is null ||
// deprecated method hardfork is involved
u.DeprecatedIn is not null && hfChecker(u.DeprecatedIn.Value, blockHeight) == false ||
// active method hardfork is involved
u.ActiveIn is not null && hfChecker(u.ActiveIn.Value, blockHeight))
)
foreach (ContractMethodMetadata method in _methodDescriptors.Where(u => IsActive(u, hfChecker, blockHeight)))
{
method.Descriptor.Offset = sb.Length;
sb.EmitPush(0); //version
Expand All @@ -215,6 +208,16 @@ u.ActiveIn is null && u.DeprecatedIn is null ||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ContractState GetContractState(ProtocolSettings settings, uint blockHeight) => GetContractState(settings.IsHardforkEnabled, blockHeight);

private static bool IsActive(IHardforkActivable u, IsHardforkEnabledDelegate hfChecker, uint blockHeight)
{
return // no hardfork is involved
u.ActiveIn is null && u.DeprecatedIn is null ||
// deprecated method hardfork is involved
u.DeprecatedIn is not null && hfChecker(u.DeprecatedIn.Value, blockHeight) == false ||
// active method hardfork is involved
u.ActiveIn is not null && hfChecker(u.ActiveIn.Value, blockHeight);
}

/// <summary>
/// The <see cref="ContractState"/> of the native contract.
/// </summary>
Expand Down Expand Up @@ -245,7 +248,7 @@ public ContractState GetContractState(IsHardforkEnabledDelegate hfChecker, uint
Abi = new ContractAbi
{
Events = _eventsDescriptors
.Where(u => u.ActiveIn is null || hfChecker(u.ActiveIn.Value, blockHeight))
.Where(u => IsActive(u, hfChecker, blockHeight))
.Select(p => p.Descriptor).ToArray(),
Methods = allowedMethods.Methods.Values
.Select(p => p.Descriptor).ToArray()
Expand Down