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

Preview5 to RC1 Comparison #6

Open
wants to merge 63 commits into
base: preview5-untouched
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
8a5e33c
Allow call in verification (#2293)
erikzhang Feb 4, 2021
2554cb9
Allow larger ExtensiblePayload (#2295)
erikzhang Feb 4, 2021
c3c395c
Transaction optimization (#2290)
shargon Feb 4, 2021
7f799b8
Fix CallFlags (#2292)
erikzhang Feb 5, 2021
e2c6877
Fix IPv4 and IPv6 Regex (#2285)
shargon Feb 5, 2021
b690466
Remove BlockBase and ConsensusData (#2296)
erikzhang Feb 5, 2021
c290e31
Optimize MemoryPool (#2297)
erikzhang Feb 5, 2021
11afccb
Call native contracts like a normal contract (#2301)
erikzhang Feb 6, 2021
e3cf64e
Update TrimmedBlock.cs (#2304)
shargon Feb 7, 2021
8934c02
Remove singletons (#2302)
erikzhang Feb 7, 2021
130529b
Add ProtocolSettings to ApplicationEngine (#2308)
erikzhang Feb 8, 2021
6b13370
Add GetInstance to LocalNode (#2310)
shargon Feb 8, 2021
9fe018c
Add native contract: StdLib and CryptoLib (#2298)
erikzhang Feb 8, 2021
6cb8cc9
Change GetScriptContainer returns (#2312)
shargon Feb 9, 2021
16499e2
Optimize UT (#2324)
erikzhang Feb 10, 2021
c3c94f0
Eliminate the use of ProtocolSettings.Default (#2314)
erikzhang Feb 10, 2021
d73d190
Clean EmitDynamicCall code (#2321)
shargon Feb 10, 2021
b58f652
Set max verification gas for state dependant signatures (#2315)
shargon Feb 10, 2021
8b9f1d2
Allow reverify inventories (#2326)
erikzhang Feb 11, 2021
f92772a
Fix compile (#2328)
shargon Feb 11, 2021
d517a85
Remove Utility.LoadConfig (#2330)
erikzhang Feb 11, 2021
e2fee70
Allow plugins work with multipe NeoSystems (#2323)
erikzhang Feb 11, 2021
1dcda66
Remove some settings from policy contract (#2300)
erikzhang Feb 12, 2021
0d46328
Remove NFTState.Description (#2333)
erikzhang Feb 13, 2021
cddf6e8
Fix CreateDummyBlock (#2334)
erikzhang Feb 13, 2021
ba6eef4
Fix some merge errors (#2337)
erikzhang Feb 14, 2021
4fd5ce0
Adjust native contract fee (#2331)
erikzhang Feb 15, 2021
7b24f8e
Add Transaction.Verify() (#2338)
erikzhang Feb 15, 2021
fcc0a45
Add NativeUpdateHistory to ProtocolSettings (#2332)
erikzhang Feb 15, 2021
3230717
Remove BitConverter (#2341)
shargon Feb 16, 2021
b828b43
Add ServiceAdded event (#2344)
bettybao1209 Feb 16, 2021
b11d7d2
Use system ECDsa (#2340)
erikzhang Feb 17, 2021
116d5a8
Add CreateStandardMultisigAccount (#2343)
shargon Feb 17, 2021
9f21f3c
Transaction.Verify returns VerifyResult (#2348)
erikzhang Feb 17, 2021
79d462e
Fix CallFlags for native contracts and syscalls (#2339)
erikzhang Feb 17, 2021
da622ed
Fix FromJson (#2350)
erikzhang Feb 17, 2021
b1a6ed0
Remove StorageItem.IsConstant (#2351)
erikzhang Feb 19, 2021
0c83d45
Set DefaultFeePerByte as a variable (#2353)
vncoelho Feb 19, 2021
ee898bf
Fix types in native contracts (#2356)
erikzhang Feb 21, 2021
b60a8d3
Update NeoVM (#2359)
shargon Feb 24, 2021
b77690f
Allow native contracts to be disabled on private net (#2361)
erikzhang Feb 24, 2021
48835b4
Fix Header's hash (#2367)
erikzhang Feb 27, 2021
865de26
Stricter settings
erikzhang Feb 28, 2021
49b3874
fix dead lock with state service (#2368)
Mar 1, 2021
b6a769e
Make CallFromNativeContract debuggable (#2358)
erikzhang Mar 4, 2021
3d6b5f7
Fix CalculateNetworkFee (#2375)
erikzhang Mar 4, 2021
bdb612b
Load settings from IConfigurationSection (#2376)
erikzhang Mar 4, 2021
0ea094c
Move AES methods and mark them as private (#2360)
erikzhang Mar 5, 2021
d1c3cd1
Add GetSignatures to ContractParametersContext (#2370)
shargon Mar 5, 2021
e6dadb2
Role: add NeoFS alphabet node role (#2377)
roman-khimov Mar 5, 2021
26c3adf
StorageItem: optimize binary representation (#2379)
roman-khimov Mar 8, 2021
9ef9541
factor genesis block creation logic into separate CreateGenesisBlock …
devhawk Mar 9, 2021
3a89b8d
Turn InteropDescriptor into record (#2382)
erikzhang Mar 9, 2021
4454e54
Redefine prices (#2384)
shargon Mar 10, 2021
c6f4c00
Change get validator prices (#2392)
shargon Mar 12, 2021
8365f71
SmartContract: rename hex in ContractParametersContext (#2394)
roman-khimov Mar 12, 2021
3626b59
Make ContractManagement.Check available for blockchain toolkit (#2396)
devhawk Mar 13, 2021
df0e755
Fix Blockchain initialization (#2397)
erikzhang Mar 15, 2021
3dac7b2
optimize transaction route (#2398)
Qiao-Jin Mar 15, 2021
d763437
Remove IsStandardContract (#2400)
shargon Mar 16, 2021
39e82ac
UT for SignatureCost (#2389)
shargon Mar 17, 2021
d1f015c
Add XML comments (#2390)
erikzhang Mar 17, 2021
12b7984
RC1 (#2402)
erikzhang Mar 17, 2021
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: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
end_of_line = lf

dotnet_diagnostic.CS1591.severity = silent
67 changes: 52 additions & 15 deletions src/neo/BigDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,69 @@

namespace Neo
{
/// <summary>
/// Represents a fixed-point number of arbitrary precision.
/// </summary>
public struct BigDecimal
{
private readonly BigInteger value;
private readonly byte decimals;

/// <summary>
/// The <see cref="BigInteger"/> value of the number.
/// </summary>
public BigInteger Value => value;

/// <summary>
/// The number of decimal places for this number.
/// </summary>
public byte Decimals => decimals;

/// <summary>
/// The sign of the number.
/// </summary>
public int Sign => value.Sign;

/// <summary>
/// Create BigDecimal from BigInteger
/// Initializes a new instance of the <see cref="BigDecimal"/> struct.
/// </summary>
/// <param name="value">Value</param>
/// <param name="decimals">Decimals</param>
/// <param name="value">The <see cref="BigInteger"/> value of the number.</param>
/// <param name="decimals">The number of decimal places for this number.</param>
public BigDecimal(BigInteger value, byte decimals)
{
this.value = value;
this.decimals = decimals;
}

/// <summary>
/// Create BigDecimal from decimal
/// Initializes a new instance of the <see cref="BigDecimal"/> struct with the value of <see cref="decimal"/>.
/// </summary>
/// <param name="value">Value</param>
/// <param name="value">The value of the number.</param>
public unsafe BigDecimal(decimal value)
{
ReadOnlySpan<byte> buffer = new ReadOnlySpan<byte>(&value, sizeof(decimal));
ReadOnlySpan<byte> buffer = new(&value, sizeof(decimal));
this.decimals = buffer[14];
this.value = new BigInteger(decimal.Multiply((decimal)Math.Pow(10, decimals), value));
}

/// <summary>
/// Create BigDecimal from decimal
/// Initializes a new instance of the <see cref="BigDecimal"/> struct with the value of <see cref="decimal"/>.
/// </summary>
/// <param name="value">Value</param>
/// <param name="decimals">Decimals</param>
/// <param name="value">The value of the number.</param>
/// <param name="decimals">The number of decimal places for this number.</param>
public unsafe BigDecimal(decimal value, byte decimals)
{
ReadOnlySpan<byte> buffer = new ReadOnlySpan<byte>(&value, sizeof(decimal));
ReadOnlySpan<byte> buffer = new(&value, sizeof(decimal));
if (buffer[14] > decimals) throw new ArgumentException(null, nameof(value));
this.value = new BigInteger(decimal.Multiply((decimal)Math.Pow(10, decimals), value));
this.decimals = decimals;
}

/// <summary>
/// Changes the decimals of the <see cref="BigDecimal"/>.
/// </summary>
/// <param name="decimals">The new decimals field.</param>
/// <returns>The <see cref="BigDecimal"/> that has the new number of decimal places.</returns>
public BigDecimal ChangeDecimals(byte decimals)
{
if (this.decimals == decimals) return this;
Expand All @@ -60,18 +79,29 @@ public BigDecimal ChangeDecimals(byte decimals)
BigInteger divisor = BigInteger.Pow(10, this.decimals - decimals);
value = BigInteger.DivRem(this.value, divisor, out BigInteger remainder);
if (remainder > BigInteger.Zero)
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(decimals));
}
return new BigDecimal(value, decimals);
}

/// <summary>
/// Parses a <see cref="BigDecimal"/> from the specified <see cref="string"/>.
/// </summary>
/// <param name="s">A number represented by a <see cref="string"/>.</param>
/// <param name="decimals">The number of decimal places for this number.</param>
/// <returns>The parsed <see cref="BigDecimal"/>.</returns>
/// <exception cref="FormatException"><paramref name="s"/> is not in the correct format.</exception>
public static BigDecimal Parse(string s, byte decimals)
{
if (!TryParse(s, decimals, out BigDecimal result))
throw new FormatException();
return result;
}

/// <summary>
/// Gets a <see cref="string"/> representing the number.
/// </summary>
/// <returns>The <see cref="string"/> representing the number.</returns>
public override string ToString()
{
BigInteger divisor = BigInteger.Pow(10, decimals);
Expand All @@ -80,15 +110,22 @@ public override string ToString()
return $"{result}.{remainder.ToString("d" + decimals)}".TrimEnd('0');
}

/// <summary>
/// Parses a <see cref="BigDecimal"/> from the specified <see cref="string"/>.
/// </summary>
/// <param name="s">A number represented by a <see cref="string"/>.</param>
/// <param name="decimals">The number of decimal places for this number.</param>
/// <param name="result">The parsed <see cref="BigDecimal"/>.</param>
/// <returns><see langword="true"/> if a number is successfully parsed; otherwise, <see langword="false"/>.</returns>
public static bool TryParse(string s, byte decimals, out BigDecimal result)
{
int e = 0;
int index = s.IndexOfAny(new[] { 'e', 'E' });
if (index >= 0)
{
if (!sbyte.TryParse(s.Substring(index + 1), out sbyte e_temp))
if (!sbyte.TryParse(s[(index + 1)..], out sbyte e_temp))
{
result = default(BigDecimal);
result = default;
return false;
}
e = e_temp;
Expand All @@ -104,14 +141,14 @@ public static bool TryParse(string s, byte decimals, out BigDecimal result)
int ds = e + decimals;
if (ds < 0)
{
result = default(BigDecimal);
result = default;
return false;
}
if (ds > 0)
s += new string('0', ds);
if (!BigInteger.TryParse(s, out BigInteger value))
{
result = default(BigDecimal);
result = default;
return false;
}
result = new BigDecimal(value, decimals);
Expand Down
28 changes: 27 additions & 1 deletion src/neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

namespace Neo.Cryptography
{
/// <summary>
/// A helper class for base-58 encoder.
/// </summary>
public static class Base58
{
/// <summary>
/// Represents the alphabet of the base-58 encoder.
/// </summary>
public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

/// <summary>
/// Converts the specified <see cref="string"/>, which encodes binary data as base-58 digits, to an equivalent byte array. The encoded <see cref="string"/> contains the checksum of the binary data.
/// </summary>
/// <param name="input">The <see cref="string"/> to convert.</param>
/// <returns>A byte array that is equivalent to <paramref name="input"/>.</returns>
public static byte[] Base58CheckDecode(this string input)
{
if (input is null) throw new ArgumentNullException(nameof(input));
Expand All @@ -23,6 +34,11 @@ public static byte[] Base58CheckDecode(this string input)
return ret;
}

/// <summary>
/// Converts a byte array to its equivalent <see cref="string"/> representation that is encoded with base-58 digits. The encoded <see cref="string"/> contains the checksum of the binary data.
/// </summary>
/// <param name="data">The byte array to convert.</param>
/// <returns>The <see cref="string"/> representation, in base-58, of the contents of <paramref name="data"/>.</returns>
public static string Base58CheckEncode(this ReadOnlySpan<byte> data)
{
byte[] checksum = data.Sha256().Sha256();
Expand All @@ -34,6 +50,11 @@ public static string Base58CheckEncode(this ReadOnlySpan<byte> data)
return ret;
}

/// <summary>
/// Converts the specified <see cref="string"/>, which encodes binary data as base-58 digits, to an equivalent byte array.
/// </summary>
/// <param name="input">The <see cref="string"/> to convert.</param>
/// <returns>A byte array that is equivalent to <paramref name="input"/>.</returns>
public static byte[] Decode(string input)
{
// Decode Base58 string to BigInteger
Expand All @@ -55,10 +76,15 @@ public static byte[] Decode(string input)
return Concat(leadingZeros, bytesWithoutLeadingZeros);
}

/// <summary>
/// Converts a byte array to its equivalent <see cref="string"/> representation that is encoded with base-58 digits.
/// </summary>
/// <param name="input">The byte array to convert.</param>
/// <returns>The <see cref="string"/> representation, in base-58, of the contents of <paramref name="input"/>.</returns>
public static string Encode(ReadOnlySpan<byte> input)
{
// Decode byte[] to BigInteger
BigInteger value = new BigInteger(input, isUnsigned: true, isBigEndian: true);
BigInteger value = new(input, isUnsigned: true, isBigEndian: true);

// Encode BigInteger to Base58 string
var sb = new StringBuilder();
Expand Down
32 changes: 32 additions & 0 deletions src/neo/Cryptography/BloomFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@

namespace Neo.Cryptography
{
/// <summary>
/// Represents a bloom filter.
/// </summary>
public class BloomFilter
{
private readonly uint[] seeds;
private readonly BitArray bits;

/// <summary>
/// The number of hash functions used by the bloom filter.
/// </summary>
public int K => seeds.Length;

/// <summary>
/// The size of the bit array used by the bloom filter.
/// </summary>
public int M => bits.Length;

/// <summary>
/// Used to generate the seeds of the murmur hash functions.
/// </summary>
public uint Tweak { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BloomFilter"/> class.
/// </summary>
/// <param name="m">The size of the bit array used by the bloom filter.</param>
/// <param name="k">The number of hash functions used by the bloom filter.</param>
/// <param name="nTweak">Used to generate the seeds of the murmur hash functions.</param>
/// <param name="elements">The initial elements contained in this <see cref="BloomFilter"/> object.</param>
public BloomFilter(int m, int k, uint nTweak, byte[] elements = null)
{
if (k < 0 || m < 0) throw new ArgumentOutOfRangeException();
Expand All @@ -24,12 +43,21 @@ public BloomFilter(int m, int k, uint nTweak, byte[] elements = null)
this.Tweak = nTweak;
}

/// <summary>
/// Adds an element to the <see cref="BloomFilter"/>.
/// </summary>
/// <param name="element">The object to add to the <see cref="BloomFilter"/>.</param>
public void Add(byte[] element)
{
foreach (uint i in seeds.AsParallel().Select(s => element.Murmur32(s)))
bits.Set((int)(i % (uint)bits.Length), true);
}

/// <summary>
/// Determines whether the <see cref="BloomFilter"/> contains a specific element.
/// </summary>
/// <param name="element">The object to locate in the <see cref="BloomFilter"/>.</param>
/// <returns><see langword="true"/> if <paramref name="element"/> is found in the <see cref="BloomFilter"/>; otherwise, <see langword="false"/>.</returns>
public bool Check(byte[] element)
{
foreach (uint i in seeds.AsParallel().Select(s => element.Murmur32(s)))
Expand All @@ -38,6 +66,10 @@ public bool Check(byte[] element)
return true;
}

/// <summary>
/// Gets the bit array in this <see cref="BloomFilter"/>.
/// </summary>
/// <param name="newBits">The byte array to store the bits.</param>
public void GetBits(byte[] newBits)
{
bits.CopyTo(newBits, 0);
Expand Down
Loading