Skip to content

Commit

Permalink
fix hardfork issues (#3234)
Browse files Browse the repository at this point in the history
* fix hardfork issues

* fix hardfork for 3172

* fix 3195

* set custom setting if load from config.

* fix hf index

* fix UT

* Revert "fix 3195"

This reverts commit c1af443.

* revert role fix

* fix manifest

* format

* revert role UT
  • Loading branch information
Jim8y authored May 16, 2024
1 parent 1dec0c7 commit d56d4eb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static ProtocolSettings Load(string path, bool optional = true)
/// <returns>The loaded <see cref="ProtocolSettings"/>.</returns>
public static ProtocolSettings Load(IConfigurationSection section)
{
return new ProtocolSettings
Custom = new ProtocolSettings
{
Network = section.GetValue("Network", Default.Network),
AddressVersion = section.GetValue("AddressVersion", Default.AddressVersion),
Expand All @@ -164,6 +164,7 @@ public static ProtocolSettings Load(IConfigurationSection section)
? EnsureOmmitedHardforks(section.GetSection("Hardforks").GetChildren().ToDictionary(p => Enum.Parse<Hardfork>(p.Key, true), p => uint.Parse(p.Value))).ToImmutableDictionary()
: Default.Hardforks
};
return Custom;
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Neo/SmartContract/Native/ContractMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ internal class ContractMethodAttribute : Attribute
public long CpuFee { get; init; }
public long StorageFee { get; init; }
public Hardfork? ActiveIn { get; init; } = null;
public Hardfork? DeprecatedIn { get; init; } = null;

public ContractMethodAttribute() { }

public ContractMethodAttribute(Hardfork activeIn)
{
ActiveIn = activeIn;
}

public ContractMethodAttribute(bool isDeprecated, Hardfork deprecatedIn)
{
if (!isDeprecated) throw new ArgumentException("isDeprecated must be true", nameof(isDeprecated));
DeprecatedIn = deprecatedIn;
}
}
}
2 changes: 2 additions & 0 deletions src/Neo/SmartContract/Native/ContractMethodMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal class ContractMethodMetadata
public CallFlags RequiredCallFlags { get; }
public ContractMethodDescriptor Descriptor { get; }
public Hardfork? ActiveIn { get; init; } = null;
public Hardfork? DeprecatedIn { get; init; } = null;

public ContractMethodMetadata(MemberInfo member, ContractMethodAttribute attribute)
{
Expand All @@ -60,6 +61,7 @@ public ContractMethodMetadata(MemberInfo member, ContractMethodAttribute attribu
StorageFee = attribute.StorageFee;
RequiredCallFlags = attribute.RequiredCallFlags;
ActiveIn = attribute.ActiveIn;
DeprecatedIn = attribute.DeprecatedIn;
Descriptor = new ContractMethodDescriptor
{
Name = Name,
Expand Down
22 changes: 21 additions & 1 deletion src/Neo/SmartContract/Native/CryptoLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace Neo.SmartContract.Native
/// </summary>
public sealed partial class CryptoLib : NativeContract
{
private static readonly Dictionary<NamedCurve, ECCurve> curves = new()

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 24 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'
{
[NamedCurve.secp256k1] = ECCurve.Secp256k1,

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 26 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve.secp256k1' is obsolete: 'secp256k1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256k1SHA256 for compatible behaviour.'
[NamedCurve.secp256r1] = ECCurve.Secp256r1,

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve.secp256r1' is obsolete: 'secp256r1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256r1SHA256 for compatible behaviour.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve.secp256r1' is obsolete: 'secp256r1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256r1SHA256 for compatible behaviour.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve.secp256r1' is obsolete: 'secp256r1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256r1SHA256 for compatible behaviour.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve.secp256r1' is obsolete: 'secp256r1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256r1SHA256 for compatible behaviour.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 27 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve.secp256r1' is obsolete: 'secp256r1 value is obsolete and will be removed in future versions. Please, use NamedCurveHash.secp256r1SHA256 for compatible behaviour.'
};

private static readonly Dictionary<NamedCurveHash, (ECCurve Curve, Hasher Hasher)> s_curves = new()
{
[NamedCurveHash.secp256k1SHA256] = (ECCurve.Secp256k1, Hasher.SHA256),
Expand Down Expand Up @@ -85,7 +91,7 @@ public static byte[] Keccak256(byte[] data)
/// <param name="signature">The signature to be verified.</param>
/// <param name="curveHash">A pair of the curve to be used by the ECDSA algorithm and the hasher function to be used to hash message.</param>
/// <returns><see langword="true"/> if the signature is valid; otherwise, <see langword="false"/>.</returns>
[ContractMethod(CpuFee = 1 << 15)]
[ContractMethod(Hardfork.HF_Cockatrice, CpuFee = 1 << 15)]
public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signature, NamedCurveHash curveHash)
{
try
Expand All @@ -98,5 +104,19 @@ public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signatu
return false;
}
}

// This is for solving the hardfork issue in https://github.com/neo-project/neo/pull/3209
[ContractMethod(true, Hardfork.HF_Cockatrice, CpuFee = 1 << 15)]
public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signature, NamedCurve curve)

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'

Check warning on line 110 in src/Neo/SmartContract/Native/CryptoLib.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

'NamedCurve' is obsolete: 'NamedCurve enum is obsolete and will be removed in future versions. Please, use an extended NamedCurveHash instead.'
{
try
{
return Crypto.VerifySignature(message, signature, pubkey, curves[curve]);
}
catch (ArgumentException)
{
return false;
}
}
}
}
12 changes: 11 additions & 1 deletion src/Neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ private NativeContractsCache.CacheEntry GetAllowedMethods(IsHardforkEnabledDeleg
byte[] script;
using (ScriptBuilder sb = new())
{
foreach (ContractMethodMetadata method in methodDescriptors.Where(u => u.ActiveIn is null || hfChecker(u.ActiveIn.Value, index)))
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, index) == false ||
// active method hardfork is involved
u.ActiveIn is not null && hfChecker(u.ActiveIn.Value, index))
)
{
method.Descriptor.Offset = sb.Length;
sb.EmitPush(0); //version
Expand Down Expand Up @@ -366,6 +374,8 @@ internal async void Invoke(ApplicationEngine engine, byte version)
ContractMethodMetadata method = currentAllowedMethods.Methods[context.InstructionPointer];
if (method.ActiveIn is not null && !engine.IsHardforkEnabled(method.ActiveIn.Value))
throw new InvalidOperationException($"Cannot call this method before hardfork {method.ActiveIn}.");
if (method.DeprecatedIn is not null && engine.IsHardforkEnabled(method.DeprecatedIn.Value))
throw new InvalidOperationException($"Cannot call this method after hardfork {method.DeprecatedIn}.");
ExecutionContextState state = context.GetState<ExecutionContextState>();
if (!state.CallFlags.HasFlag(method.RequiredCallFlags))
throw new InvalidOperationException($"Cannot call this method with the flag {state.CallFlags}.");
Expand Down
22 changes: 14 additions & 8 deletions src/Neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class NeoToken : FungibleToken<NeoToken.NeoAccountState>
"from", ContractParameterType.PublicKey,
"to", ContractParameterType.PublicKey,
"amount", ContractParameterType.Integer)]
[ContractEvent(3, name: "CommitteeChanged",
[ContractEvent(Hardfork.HF_Cockatrice, 3, name: "CommitteeChanged",
"old", ContractParameterType.Array,
"new", ContractParameterType.Array)]
internal NeoToken() : base()
Expand Down Expand Up @@ -203,14 +203,20 @@ internal override ContractTask OnPersistAsync(ApplicationEngine engine)
cachedCommittee.Clear();
cachedCommittee.AddRange(ComputeCommitteeMembers(engine.Snapshot, engine.ProtocolSettings));

var newCommittee = cachedCommittee.Select(u => u.PublicKey).ToArray();

if (!newCommittee.SequenceEqual(prevCommittee))
// Hardfork check for https://github.com/neo-project/neo/pull/3158
// New notification will case 3.7.0 and 3.6.0 have different behavior
var index = engine.PersistingBlock?.Index ?? Ledger.CurrentIndex(engine.Snapshot);
if (engine.ProtocolSettings.IsHardforkEnabled(Hardfork.HF_Cockatrice, index))
{
engine.SendNotification(Hash, "CommitteeChanged", new VM.Types.Array(engine.ReferenceCounter) {
new VM.Types.Array(engine.ReferenceCounter, prevCommittee.Select(u => (ByteString)u.ToArray())) ,
new VM.Types.Array(engine.ReferenceCounter, newCommittee.Select(u => (ByteString)u.ToArray()))
});
var newCommittee = cachedCommittee.Select(u => u.PublicKey).ToArray();

if (!newCommittee.SequenceEqual(prevCommittee))
{
engine.SendNotification(Hash, "CommitteeChanged", new VM.Types.Array(engine.ReferenceCounter) {
new VM.Types.Array(engine.ReferenceCounter, prevCommittee.Select(u => (ByteString)u.ToArray())) ,
new VM.Types.Array(engine.ReferenceCounter, newCommittee.Select(u => (ByteString)u.ToArray()))
});
}
}
}
return ContractTask.CompletedTask;
Expand Down

0 comments on commit d56d4eb

Please sign in to comment.