Skip to content

Commit

Permalink
Merge pull request #1 from neo-project/master
Browse files Browse the repository at this point in the history
merge neo-project
  • Loading branch information
zhangtao authored Jan 20, 2020
2 parents d9d54b5 + 35c73ce commit 3f083d2
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 461 deletions.
1 change: 1 addition & 0 deletions src/neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class Base58

public static byte[] Base58CheckDecode(this string input)
{
if (input is null) throw new ArgumentNullException(nameof(input));
byte[] buffer = Decode(input);
if (buffer.Length < 4) throw new FormatException();
byte[] checksum = buffer.Sha256(0, buffer.Length - 4).Sha256();
Expand Down
2 changes: 2 additions & 0 deletions src/neo/Cryptography/BloomFilter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Linq;

Expand All @@ -16,6 +17,7 @@ public class BloomFilter

public BloomFilter(int m, int k, uint nTweak, byte[] elements = null)
{
if (k < 0 || m < 0) throw new ArgumentOutOfRangeException();
this.seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray();
this.bits = elements == null ? new BitArray(m) : new BitArray(elements);
this.bits.Length = m;
Expand Down
107 changes: 0 additions & 107 deletions src/neo/Cryptography/ECC/ECDsa.cs

This file was deleted.

5 changes: 4 additions & 1 deletion src/neo/Cryptography/ECC/ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class ECFieldElement : IComparable<ECFieldElement>, IEquatable<ECFieldE

public ECFieldElement(BigInteger value, ECCurve curve)
{
if (curve is null)
throw new ArgumentNullException(nameof(curve));
if (value >= curve.Q)
throw new ArgumentException("x value too large in field element");
this.Value = value;
Expand All @@ -19,6 +21,7 @@ public ECFieldElement(BigInteger value, ECCurve curve)
public int CompareTo(ECFieldElement other)
{
if (ReferenceEquals(this, other)) return 0;
if (!curve.Equals(other.curve)) throw new InvalidOperationException("Invalid comparision for points with different curves");
return Value.CompareTo(other.Value);
}

Expand All @@ -35,7 +38,7 @@ public override bool Equals(object obj)

public bool Equals(ECFieldElement other)
{
return Value.Equals(other.Value);
return Value.Equals(other.Value) && curve.Equals(other.curve);
}

private static BigInteger[] FastLucasSequence(BigInteger p, BigInteger P, BigInteger Q, BigInteger k)
Expand Down
3 changes: 2 additions & 1 deletion src/neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ECPoint() : this(null, null, ECCurve.Secp256r1) { }

internal ECPoint(ECFieldElement x, ECFieldElement y, ECCurve curve)
{
if ((x != null && y == null) || (x == null && y != null))
if ((x is null ^ y is null) || (curve is null))
throw new ArgumentException("Exactly one of the field elements is null");
this.X = x;
this.Y = y;
Expand All @@ -31,6 +31,7 @@ internal ECPoint(ECFieldElement x, ECFieldElement y, ECCurve curve)

public int CompareTo(ECPoint other)
{
if (!Curve.Equals(other.Curve)) throw new InvalidOperationException("Invalid comparision for points with different curves");
if (ReferenceEquals(this, other)) return 0;
int result = X.CompareTo(other.X);
if (result != 0) return result;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Cryptography/MerkleTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MerkleTree

internal MerkleTree(UInt256[] hashes)
{
if (hashes.Length == 0) throw new ArgumentException();
if (hashes is null || hashes.Length == 0) throw new ArgumentException();
this.root = Build(hashes.Select(p => new MerkleTreeNode { Hash = p }).ToArray());
int depth = 1;
for (MerkleTreeNode i = root; i.LeftChild != null; i = i.LeftChild)
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal static void Remove<T>(this HashSet<T> set, ISet<T> other)
}
}

internal static void Remove<T>(this HashSet<T> set, FIFOSet<T> other)
internal static void Remove<T>(this HashSet<T> set, HashSetCache<T> other)
where T : IEquatable<T>
{
if (set.Count > other.Count)
Expand Down
67 changes: 0 additions & 67 deletions src/neo/IO/Caching/FIFOSet.cs

This file was deleted.

106 changes: 106 additions & 0 deletions src/neo/IO/Caching/HashSetCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace Neo.IO.Caching
{
public class HashSetCache<T> : IReadOnlyCollection<T> where T : IEquatable<T>
{
/// <summary>
/// Sets where the Hashes are stored
/// </summary>
private readonly LinkedList<HashSet<T>> sets = new LinkedList<HashSet<T>>();

/// <summary>
/// Maximum capacity of each bucket inside each HashSet of <see cref="sets"/>.
/// </summary>
private readonly int bucketCapacity;

/// <summary>
/// Maximum number of buckets for the LinkedList, meaning its maximum cardinality.
/// </summary>
private readonly int maxBucketCount;

/// <summary>
/// Entry count
/// </summary>
public int Count { get; private set; }

public HashSetCache(int bucketCapacity, int maxBucketCount = 10)
{
if (bucketCapacity <= 0) throw new ArgumentOutOfRangeException($"{nameof(bucketCapacity)} should be greater than 0");
if (maxBucketCount <= 0) throw new ArgumentOutOfRangeException($"{nameof(maxBucketCount)} should be greater than 0");

this.Count = 0;
this.bucketCapacity = bucketCapacity;
this.maxBucketCount = maxBucketCount;
sets.AddFirst(new HashSet<T>());
}

public bool Add(T item)
{
if (Contains(item)) return false;
Count++;
if (sets.First.Value.Count < bucketCapacity) return sets.First.Value.Add(item);
var newSet = new HashSet<T>
{
item
};
sets.AddFirst(newSet);
if (sets.Count > maxBucketCount)
{
Count -= sets.Last.Value.Count;
sets.RemoveLast();
}
return true;
}

public bool Contains(T item)
{
foreach (var set in sets)
{
if (set.Contains(item)) return true;
}
return false;
}

public void ExceptWith(IEnumerable<T> items)
{
List<HashSet<T>> removeList = null;
foreach (var item in items)
{
foreach (var set in sets)
{
if (set.Remove(item))
{
Count--;
if (set.Count == 0)
{
removeList ??= new List<HashSet<T>>();
removeList.Add(set);
}
break;
}
}
}
if (removeList == null) return;
foreach (var set in removeList)
{
sets.Remove(set);
}
}

public IEnumerator<T> GetEnumerator()
{
foreach (var set in sets)
{
foreach (var item in set)
{
yield return item;
}
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
Loading

0 comments on commit 3f083d2

Please sign in to comment.