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

[Move] Part-7 Classes into Different Library - Neo.Extensions #3446

Merged
merged 39 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
bf93412
Part-1 `Neo.IO` - move
cschuchardt88 Jul 2, 2024
4d08154
Part-2
cschuchardt88 Jul 2, 2024
597c9ca
Merge branch 'master' into rebuild/the-split-2
cschuchardt88 Jul 5, 2024
9881579
Added `BigInteger` to `Neo.Extensions`
cschuchardt88 Jul 5, 2024
8779151
Found more `BigInteger`
cschuchardt88 Jul 5, 2024
ed34707
Added `ByteArray` to `Neo.Extensions`
cschuchardt88 Jul 5, 2024
5b12304
Added `DateTime` Extensions to `Neo.Extensions`
cschuchardt88 Jul 7, 2024
0f6627a
Added `HashSetExtensions`, `HashSetExtensions2`, `IpAddressExtensions…
cschuchardt88 Jul 7, 2024
95f8a2d
Merge branch 'master' into rebuild/the-split-4
cschuchardt88 Jul 7, 2024
721ce58
Added `ICollection`, `Memory`, `String`, `Unsafe` extensions
cschuchardt88 Jul 7, 2024
129a32f
Adding `using`
cschuchardt88 Jul 7, 2024
397cc1f
dotnet format
cschuchardt88 Jul 7, 2024
a127386
Added more Extensions
cschuchardt88 Jul 7, 2024
9a84436
Move some methods
cschuchardt88 Jul 7, 2024
dbbf5b3
Merge branch 'master' into rebuild/the-split-2
shargon Jul 8, 2024
b64435b
Added Tests
cschuchardt88 Jul 9, 2024
583e610
Merge branch 'rebuild/the-split-2' of https://github.com/cschuchardt8…
cschuchardt88 Jul 9, 2024
6a50385
Merge branch 'rebuild/the-split-2' into rebuild/the-split-3
cschuchardt88 Jul 9, 2024
f381b89
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 Jul 9, 2024
6261f70
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
4a0fb7a
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 Jul 9, 2024
58f7fee
Added `tests` from `Part-2`
cschuchardt88 Jul 9, 2024
7f74300
Merge branch 'rebuild/the-split-3' into rebuild/the-split-4
cschuchardt88 Jul 9, 2024
c790166
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
26d0515
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 Jul 9, 2024
e644bd3
Added `tests` for `PART-4`
cschuchardt88 Jul 9, 2024
79a3578
Merge branch 'rebuild/the-split-4' into rebuild/the-split-5
cschuchardt88 Jul 9, 2024
00ce5ae
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 Jul 9, 2024
6fa0fda
Added `tests` for `PART-5`
cschuchardt88 Jul 9, 2024
708ce84
Merge branch 'rebuild/the-split-5' into rebuild/the-split-6
cschuchardt88 Jul 9, 2024
bb9992e
Merge Master
cschuchardt88 Aug 3, 2024
ff3c158
Added the `Part` 7
cschuchardt88 Aug 3, 2024
d761585
Merge Master into `the-split-7`
cschuchardt88 Nov 25, 2024
9ecd77f
revert `OrCondition.cs`
cschuchardt88 Nov 25, 2024
15c092d
revert `OrCondition.cs`
cschuchardt88 Nov 25, 2024
b333474
Merge branch 'master' into rebuild/the-split-7
shargon Nov 26, 2024
0b24c62
Update tests/Neo.Extensions.Tests/UT_BigIntegerExtensions.cs
shargon Nov 26, 2024
b241941
Merge branch 'master' into rebuild/the-split-7
shargon Nov 27, 2024
8e1ff35
Merge branch 'master' into rebuild/the-split-7
Jim8y Nov 28, 2024
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
2 changes: 1 addition & 1 deletion src/Neo.GUI/GUI/ElectionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Extensions;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.GUI/GUI/VotingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Extensions;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Extensions/Collections/ICollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Extensions;
using Neo.IO;
using System.Collections.Generic;
using System.IO;
Expand Down
306 changes: 306 additions & 0 deletions src/Neo/Extensions/VM/ScriptBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ScriptBuilderExtensions.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.

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Neo.Extensions
{
public static class ScriptBuilderExtensions
{
/// <summary>
/// Emits the opcodes for creating an array.
/// </summary>
/// <typeparam name="T">The type of the elements of the array.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="list">The elements of the array.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateArray<T>(this ScriptBuilder builder, IReadOnlyList<T> list = null)
{
if (list is null || list.Count == 0)
return builder.Emit(OpCode.NEWARRAY0);
for (var i = list.Count - 1; i >= 0; i--)
builder.EmitPush(list[i]);
builder.EmitPush(list.Count);
return builder.Emit(OpCode.PACK);
}

/// <summary>
/// Emits the opcodes for creating a map.
/// </summary>
/// <typeparam name="TKey">The type of the key of the map.</typeparam>
/// <typeparam name="TValue">The type of the value of the map.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="map">The key/value pairs of the map.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IEnumerable<KeyValuePair<TKey, TValue>> map)
where TKey : notnull
where TValue : notnull
{
var count = map.Count();

if (count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
}
builder.EmitPush(count);
return builder.Emit(OpCode.PACKMAP);
}

/// <summary>
/// Emits the opcodes for creating a map.
/// </summary>
/// <typeparam name="TKey">The type of the key of the map.</typeparam>
/// <typeparam name="TValue">The type of the value of the map.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="map">The key/value pairs of the map.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IReadOnlyDictionary<TKey, TValue> map)
where TKey : notnull
where TValue : notnull
{
if (map.Count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
}
builder.EmitPush(map.Count);
return builder.Emit(OpCode.PACKMAP);
}

/// <summary>
/// Emits the opcodes for creating a struct.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="array">The list of properties.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateStruct<T>(this ScriptBuilder builder, IReadOnlyList<T> array)
where T : notnull
{
if (array.Count == 0)
return builder.Emit(OpCode.NEWSTRUCT0);
for (var i = array.Count - 1; i >= 0; i--)
builder.EmitPush(array[i]);
builder.EmitPush(array.Count);
return builder.Emit(OpCode.PACKSTRUCT);
}

/// <summary>
/// Emits the specified opcodes.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="ops">The opcodes to emit.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder Emit(this ScriptBuilder builder, params OpCode[] ops)
{
foreach (var op in ops)
builder.Emit(op);
return builder;
}

/// <summary>
/// Emits the opcodes for calling a contract dynamically.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="scriptHash">The hash of the contract to be called.</param>
/// <param name="method">The method to be called in the contract.</param>
/// <param name="args">The arguments for calling the contract.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitDynamicCall(this ScriptBuilder builder, UInt160 scriptHash, string method, params object[] args)
{
return EmitDynamicCall(builder, scriptHash, method, CallFlags.All, args);
}

/// <summary>
/// Emits the opcodes for calling a contract dynamically.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="scriptHash">The hash of the contract to be called.</param>
/// <param name="method">The method to be called in the contract.</param>
/// <param name="flags">The <see cref="CallFlags"/> for calling the contract.</param>
/// <param name="args">The arguments for calling the contract.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitDynamicCall(this ScriptBuilder builder, UInt160 scriptHash, string method, CallFlags flags, params object[] args)
{
builder.CreateArray(args);
builder.EmitPush(flags);
builder.EmitPush(method);
builder.EmitPush(scriptHash);
builder.EmitSysCall(ApplicationEngine.System_Contract_Call);
return builder;
}

/// <summary>
/// Emits the opcodes for pushing the specified data onto the stack.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="data">The data to be pushed.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitPush(this ScriptBuilder builder, ISerializable data)
{
return builder.EmitPush(data.ToArray());
}

/// <summary>
/// Emits the opcodes for pushing the specified data onto the stack.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="parameter">The data to be pushed.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitPush(this ScriptBuilder builder, ContractParameter parameter)
{
if (parameter.Value is null)
builder.Emit(OpCode.PUSHNULL);
else
switch (parameter.Type)
{
case ContractParameterType.Signature:
case ContractParameterType.ByteArray:
builder.EmitPush((byte[])parameter.Value);
break;
case ContractParameterType.Boolean:
builder.EmitPush((bool)parameter.Value);
break;
case ContractParameterType.Integer:
if (parameter.Value is BigInteger bi)
builder.EmitPush(bi);
else
builder.EmitPush((BigInteger)typeof(BigInteger).GetConstructor([parameter.Value.GetType()]).Invoke([parameter.Value]));
Copy link
Member

Choose a reason for hiding this comment

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

Can we change this ugly old code?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Nov 26, 2024

Choose a reason for hiding this comment

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

I rather leave for another pr.

break;
case ContractParameterType.Hash160:
builder.EmitPush((UInt160)parameter.Value);
break;
case ContractParameterType.Hash256:
builder.EmitPush((UInt256)parameter.Value);
break;
case ContractParameterType.PublicKey:
builder.EmitPush((ECPoint)parameter.Value);
break;
case ContractParameterType.String:
builder.EmitPush((string)parameter.Value);
break;
case ContractParameterType.Array:
{
var parameters = (IList<ContractParameter>)parameter.Value;
for (var i = parameters.Count - 1; i >= 0; i--)
builder.EmitPush(parameters[i]);
builder.EmitPush(parameters.Count);
builder.Emit(OpCode.PACK);
}
break;
case ContractParameterType.Map:
{
var pairs = (IList<KeyValuePair<ContractParameter, ContractParameter>>)parameter.Value;
builder.CreateMap(pairs);
}
break;
default:
throw new ArgumentException(null, nameof(parameter));
}
return builder;
}

/// <summary>
/// Emits the opcodes for pushing the specified data onto the stack.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="obj">The data to be pushed.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitPush(this ScriptBuilder builder, object obj)
{
switch (obj)
{
case bool data:
builder.EmitPush(data);
break;
case byte[] data:
builder.EmitPush(data);
break;
case string data:
builder.EmitPush(data);
break;
case BigInteger data:
builder.EmitPush(data);
break;
case ISerializable data:
builder.EmitPush(data);
break;
case sbyte data:
builder.EmitPush(data);
break;
case byte data:
builder.EmitPush(data);
break;
case short data:
builder.EmitPush(data);
break;
case char data:
builder.EmitPush(data);
break;
case ushort data:
builder.EmitPush(data);
break;
case int data:
builder.EmitPush(data);
break;
case uint data:
builder.EmitPush(data);
break;
case long data:
builder.EmitPush(data);
break;
case ulong data:
builder.EmitPush(data);
break;
case Enum data:
builder.EmitPush(BigInteger.Parse(data.ToString("d")));
break;
case ContractParameter data:
builder.EmitPush(data);
break;
case null:
builder.Emit(OpCode.PUSHNULL);
break;
default:
throw new ArgumentException(null, nameof(obj));
}
return builder;
}

/// <summary>
/// Emits the opcodes for invoking an interoperable service.
/// </summary>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="method">The hash of the interoperable service.</param>
/// <param name="args">The arguments for calling the interoperable service.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder EmitSysCall(this ScriptBuilder builder, uint method, params object[] args)
{
for (var i = args.Length - 1; i >= 0; i--)
EmitPush(builder, args[i]);
return builder.EmitSysCall(method);
}
}
}
1 change: 1 addition & 0 deletions src/Neo/SmartContract/ContractParametersContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
Expand Down
1 change: 1 addition & 0 deletions src/Neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
Expand Down
Loading
Loading