Skip to content

Commit

Permalink
Merge branch 'master' into adding-comments-peers
Browse files Browse the repository at this point in the history
  • Loading branch information
vncoelho committed Nov 28, 2019
2 parents 1850780 + 92eb258 commit 44d25a4
Show file tree
Hide file tree
Showing 94 changed files with 1,095 additions and 946 deletions.
12 changes: 6 additions & 6 deletions src/neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class ConsensusContext : IDisposable, ISerializable
/// <summary>
/// Key for saving consensus state.
/// </summary>
private static readonly byte[] ConsensusStateKey = { 0xf4 };
private const byte ConsensusStatePrefix = 0xf4;

public Block Block;
public byte ViewNumber;
Expand All @@ -42,11 +42,11 @@ internal class ConsensusContext : IDisposable, ISerializable
/// </summary>
public SendersFeeMonitor SendersFeeMonitor = new SendersFeeMonitor();

public Snapshot Snapshot { get; private set; }
public SnapshotView Snapshot { get; private set; }
private KeyPair keyPair;
private int _witnessSize;
private readonly Wallet wallet;
private readonly Store store;
private readonly IStore store;

public int F => (Validators.Length - 1) / 3;
public int M => Validators.Length - F;
Expand Down Expand Up @@ -74,7 +74,7 @@ internal class ConsensusContext : IDisposable, ISerializable

public int Size => throw new NotImplementedException();

public ConsensusContext(Wallet wallet, Store store)
public ConsensusContext(Wallet wallet, IStore store)
{
this.wallet = wallet;
this.store = store;
Expand Down Expand Up @@ -146,7 +146,7 @@ public uint GetPrimaryIndex(byte viewNumber)

public bool Load()
{
byte[] data = store.Get(ConsensusStateKey);
byte[] data = store.TryGet(ConsensusStatePrefix, null);
if (data is null || data.Length == 0) return false;
using (MemoryStream ms = new MemoryStream(data, false))
using (BinaryReader reader = new BinaryReader(ms))
Expand Down Expand Up @@ -409,7 +409,7 @@ public void Reset(byte viewNumber)

public void Save()
{
store.PutSync(ConsensusStateKey, this.ToArray());
store.PutSync(ConsensusStatePrefix, null, this.ToArray());
}

public void Serialize(BinaryWriter writer)
Expand Down
2 changes: 2 additions & 0 deletions src/neo/Consensus/ConsensusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract class ConsensusMessage : ISerializable

protected ConsensusMessage(ConsensusMessageType type)
{
if (!Enum.IsDefined(typeof(ConsensusMessageType), type))
throw new ArgumentOutOfRangeException(nameof(type));
this.Type = type;
}

Expand Down
4 changes: 2 additions & 2 deletions src/neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class Timer { public uint Height; public byte ViewNumber; }
/// </summary>
private bool isRecovering = false;

public ConsensusService(IActorRef localNode, IActorRef taskManager, Store store, Wallet wallet)
public ConsensusService(IActorRef localNode, IActorRef taskManager, IStore store, Wallet wallet)
: this(localNode, taskManager, new ConsensusContext(wallet, store))
{
}
Expand Down Expand Up @@ -601,7 +601,7 @@ protected override void PostStop()
base.PostStop();
}

public static Props Props(IActorRef localNode, IActorRef taskManager, Store store, Wallet wallet)
public static Props Props(IActorRef localNode, IActorRef taskManager, IStore store, Wallet wallet)
{
return Akka.Actor.Props.Create(() => new ConsensusService(localNode, taskManager, store, wallet)).WithMailbox("consensus-service-mailbox");
}
Expand Down
7 changes: 3 additions & 4 deletions src/neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ public static byte[] Decode(string input)
// Leading zero bytes get encoded as leading `1` characters
int leadingZeroCount = input.TakeWhile(c => c == Alphabet[0]).Count();
var leadingZeros = new byte[leadingZeroCount];
var bytesWithoutLeadingZeros = bi.ToByteArray()
.Reverse()// to big endian
.SkipWhile(b => b == 0);//strip sign byte
if (bi.IsZero) return leadingZeros;
var bytesWithoutLeadingZeros = bi.ToByteArray(isUnsigned: true, isBigEndian: true);
return leadingZeros.Concat(bytesWithoutLeadingZeros).ToArray();
}

public static string Encode(byte[] input)
{
// Decode byte[] to BigInteger
BigInteger value = new BigInteger(new byte[1].Concat(input).Reverse().ToArray());
BigInteger value = new BigInteger(input, isUnsigned: true, isBigEndian: true);

// Encode BigInteger to Base58 string
var sb = new StringBuilder();
Expand Down
5 changes: 2 additions & 3 deletions src/neo/Cryptography/ECC/ECDsa.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;

Expand All @@ -26,7 +25,7 @@ public ECDsa(ECPoint publicKey)
private BigInteger CalculateE(BigInteger n, byte[] message)
{
int messageBitLength = message.Length * 8;
BigInteger trunc = new BigInteger(message.Reverse().Concat(new byte[1]).ToArray());
BigInteger trunc = new BigInteger(message, isUnsigned: true, isBigEndian: true);
if (n.GetBitLength() < messageBitLength)
{
trunc >>= messageBitLength - n.GetBitLength();
Expand All @@ -38,7 +37,7 @@ public BigInteger[] GenerateSignature(byte[] message)
{
if (privateKey == null) throw new InvalidOperationException();
BigInteger e = CalculateE(curve.N, message);
BigInteger d = new BigInteger(privateKey.Reverse().Concat(new byte[1]).ToArray());
BigInteger d = new BigInteger(privateKey, isUnsigned: true, isBigEndian: true);
BigInteger r, s;
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
Expand Down
8 changes: 3 additions & 5 deletions src/neo/Cryptography/ECC/ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ public ECFieldElement Square()

public byte[] ToByteArray()
{
byte[] data = Value.ToByteArray();
byte[] data = Value.ToByteArray(isUnsigned: true, isBigEndian: true);
if (data.Length == 32)
return data.Reverse().ToArray();
if (data.Length > 32)
return data.Take(32).Reverse().ToArray();
return Enumerable.Repeat<byte>(0, 32 - data.Length).Concat(data.Reverse()).ToArray();
return data;
return Enumerable.Repeat<byte>(0, 32 - data.Length).Concat(data).ToArray();
}

public static ECFieldElement operator -(ECFieldElement x)
Expand Down
12 changes: 6 additions & 6 deletions src/neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public static ECPoint DecodePoint(byte[] encoded, ECCurve curve)
if (encoded.Length != (expectedLength + 1))
throw new FormatException("Incorrect length for compressed encoding");
int yTilde = encoded[0] & 1;
BigInteger X1 = new BigInteger(encoded.Skip(1).Reverse().Concat(new byte[1]).ToArray());
BigInteger X1 = new BigInteger(encoded.AsSpan(1), isUnsigned: true, isBigEndian: true);
p = DecompressPoint(yTilde, X1, curve);
break;
}
case 0x04: // uncompressed
{
if (encoded.Length != (2 * expectedLength + 1))
throw new FormatException("Incorrect length for uncompressed/hybrid encoding");
BigInteger X1 = new BigInteger(encoded.Skip(1).Take(expectedLength).Reverse().Concat(new byte[1]).ToArray());
BigInteger Y1 = new BigInteger(encoded.Skip(1 + expectedLength).Reverse().Concat(new byte[1]).ToArray());
BigInteger X1 = new BigInteger(encoded.AsSpan(1, expectedLength), isUnsigned: true, isBigEndian: true);
BigInteger Y1 = new BigInteger(encoded.AsSpan(1 + expectedLength), isUnsigned: true, isBigEndian: true);
p = new ECPoint(new ECFieldElement(X1, curve), new ECFieldElement(Y1, curve), curve);
break;
}
Expand Down Expand Up @@ -143,10 +143,10 @@ public byte[] EncodePoint(bool commpressed)
else
{
data = new byte[65];
byte[] yBytes = Y.Value.ToByteArray().Reverse().ToArray();
byte[] yBytes = Y.Value.ToByteArray(isUnsigned: true, isBigEndian: true);
Buffer.BlockCopy(yBytes, 0, data, 65 - yBytes.Length, yBytes.Length);
}
byte[] xBytes = X.Value.ToByteArray().Reverse().ToArray();
byte[] xBytes = X.Value.ToByteArray(isUnsigned: true, isBigEndian: true);
Buffer.BlockCopy(xBytes, 0, data, 33 - xBytes.Length, xBytes.Length);
data[0] = commpressed ? Y.Value.IsEven ? (byte)0x02 : (byte)0x03 : (byte)0x04;
return data;
Expand Down Expand Up @@ -379,7 +379,7 @@ private static sbyte[] WindowNaf(sbyte width, BigInteger k)
throw new ArgumentException();
if (p.IsInfinity)
return p;
BigInteger k = new BigInteger(n.Reverse().Concat(new byte[1]).ToArray());
BigInteger k = new BigInteger(n, isUnsigned: true, isBigEndian: true);
if (k.Sign == 0)
return p.Curve.Infinity;
return Multiply(p, k);
Expand Down
8 changes: 4 additions & 4 deletions src/neo/IO/Caching/CloneCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ protected override void AddInternal(TKey key, TValue value)
innerCache.Add(key, value);
}

public override void DeleteInternal(TKey key)
protected override void DeleteInternal(TKey key)
{
innerCache.Delete(key);
}

protected override IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix)
protected override IEnumerable<(TKey, TValue)> FindInternal(byte[] key_prefix)
{
foreach (KeyValuePair<TKey, TValue> pair in innerCache.Find(key_prefix))
yield return new KeyValuePair<TKey, TValue>(pair.Key, pair.Value.Clone());
foreach (var (key, value) in innerCache.Find(key_prefix))
yield return (key, value.Clone());
}

protected override TValue GetInternal(TKey key)
Expand Down
10 changes: 5 additions & 5 deletions src/neo/IO/Caching/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void Delete(TKey key)
}
}

public abstract void DeleteInternal(TKey key);
protected abstract void DeleteInternal(TKey key);

public void DeleteWhere(Func<TKey, TValue, bool> predicate)
{
Expand All @@ -123,7 +123,7 @@ public void DeleteWhere(Func<TKey, TValue, bool> predicate)
/// </summary>
/// <param name="key_prefix">Must maintain the deserialized format of TKey</param>
/// <returns>Entries found with the desired prefix</returns>
public IEnumerable<KeyValuePair<TKey, TValue>> Find(byte[] key_prefix = null)
public IEnumerable<(TKey Key, TValue Value)> Find(byte[] key_prefix = null)
{
IEnumerable<(byte[], TKey, TValue)> cached;
lock (dictionary)
Expand Down Expand Up @@ -159,21 +159,21 @@ public void DeleteWhere(Func<TKey, TValue, bool> predicate)
{
if (!c2 || (c1 && ByteArrayComparer.Default.Compare(i1.KeyBytes, i2.KeyBytes) < 0))
{
yield return new KeyValuePair<TKey, TValue>(i1.Key, i1.Item);
yield return (i1.Key, i1.Item);
c1 = e1.MoveNext();
i1 = c1 ? e1.Current : default;
}
else
{
yield return new KeyValuePair<TKey, TValue>(i2.Key, i2.Item);
yield return (i2.Key, i2.Item);
c2 = e2.MoveNext();
i2 = c2 ? e2.Current : default;
}
}
}
}

protected abstract IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix);
protected abstract IEnumerable<(TKey Key, TValue Value)> FindInternal(byte[] key_prefix);

public IEnumerable<Trackable> GetChangeSet()
{
Expand Down
9 changes: 6 additions & 3 deletions src/neo/IO/Data/LevelDB/SliceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ public SliceBuilder Add(long value)

public SliceBuilder Add(IEnumerable<byte> value)
{
data.AddRange(value);
if (value != null)
data.AddRange(value);
return this;
}

public SliceBuilder Add(string value)
{
data.AddRange(Encoding.UTF8.GetBytes(value));
if (value != null)
data.AddRange(Encoding.UTF8.GetBytes(value));
return this;
}

public SliceBuilder Add(ISerializable value)
{
data.AddRange(value.ToArray());
if (value != null)
data.AddRange(value.ToArray());
return this;
}

Expand Down
Loading

0 comments on commit 44d25a4

Please sign in to comment.