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

DeprecatedIn for events #3362

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
54 changes: 41 additions & 13 deletions src/Neo/SmartContract/Native/ContractEventAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,62 @@ 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;
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved

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;
}
shargon marked this conversation as resolved.
Show resolved Hide resolved

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

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

public ContractEventAttribute(int order, string name,
string arg1Name, ContractParameterType arg1Value,
string arg2Name, ContractParameterType arg2Value)
Expand All @@ -61,8 +81,8 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -73,10 +93,18 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
Name = arg2Name,
Type = arg2Value
}
}
]
};
}

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


public ContractEventAttribute(Hardfork activeIn, int order, string name,
string arg1Name, ContractParameterType arg1Value,
string arg2Name, ContractParameterType arg2Value,
Expand All @@ -95,8 +123,8 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -112,7 +140,7 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
Name = arg3Name,
Type = arg3Value
}
}
]
};
}

Expand All @@ -136,8 +164,8 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
Descriptor = new ContractEventDescriptor()
{
Name = name,
Parameters = new ContractParameterDefinition[]
{
Parameters =
[
new ContractParameterDefinition()
{
Name = arg1Name,
Expand All @@ -158,7 +186,7 @@ public ContractEventAttribute(int order, string name, string arg1Name, ContractP
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)
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ private NativeContractsCache.CacheEntry GetAllowedMethods(IsHardforkEnabledDeleg
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ContractState GetContractState(ProtocolSettings settings, uint blockHeight) => GetContractState(settings.IsHardforkEnabled, blockHeight);

internal 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
22 changes: 22 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ public void Clean()
TestBlockchain.ResetStore();
}

class active : IHardforkActivable
{
public Hardfork? ActiveIn { get; init; }
public Hardfork? DeprecatedIn { get; init; }
}

[TestMethod]
public void TestActiveDeprecatedIn()
{
string json = UT_ProtocolSettings.CreateHKSettings("\"HF_Cockatrice\": 20");
shargon marked this conversation as resolved.
Show resolved Hide resolved
var file = Path.GetTempFileName();
File.WriteAllText(file, json);
ProtocolSettings settings = ProtocolSettings.Load(file, false);
File.Delete(file);

Assert.IsFalse(NativeContract.IsActive(new active() { ActiveIn = Hardfork.HF_Cockatrice, DeprecatedIn = null }, settings.IsHardforkEnabled, 1));
Assert.IsTrue(NativeContract.IsActive(new active() { ActiveIn = Hardfork.HF_Cockatrice, DeprecatedIn = null }, settings.IsHardforkEnabled, 20));

Assert.IsTrue(NativeContract.IsActive(new active() { ActiveIn = null, DeprecatedIn = Hardfork.HF_Cockatrice }, settings.IsHardforkEnabled, 1));
Assert.IsFalse(NativeContract.IsActive(new active() { ActiveIn = null, DeprecatedIn = Hardfork.HF_Cockatrice }, settings.IsHardforkEnabled, 20));
}

[TestMethod]
public void TestGetContract()
{
Expand Down
Loading