Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lock9 authored Dec 16, 2019
2 parents 281c76a + abeebd9 commit 9fe7805
Show file tree
Hide file tree
Showing 67 changed files with 1,978 additions and 1,569 deletions.
8 changes: 4 additions & 4 deletions src/neo/Cryptography/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static byte[] Sign(byte[] message, byte[] prikey, byte[] pubkey)
}
}

public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte> signature, byte[] pubkey)
public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte> signature, ReadOnlySpan<byte> pubkey)
{
if (pubkey.Length == 33 && (pubkey[0] == 0x02 || pubkey[0] == 0x03))
{
try
{
pubkey = ECC.ECPoint.DecodePoint(pubkey, ECC.ECCurve.Secp256r1).EncodePoint(false)[1..];
pubkey = ECC.ECPoint.DecodePoint(pubkey, ECC.ECCurve.Secp256r1).EncodePoint(false).AsSpan(1);
}
catch
{
Expand All @@ -58,8 +58,8 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
Curve = ECCurve.NamedCurves.nistP256,
Q = new ECPoint
{
X = pubkey[..32],
Y = pubkey[32..]
X = pubkey[..32].ToArray(),
Y = pubkey[32..].ToArray()
}
}))
{
Expand Down
12 changes: 12 additions & 0 deletions src/neo/IO/Json/JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteEndArray();
}

public override JObject Clone()
{
var cloned = new JArray();

foreach (JObject item in items)
{
cloned.Add(item.Clone());
}

return cloned;
}

public static implicit operator JArray(JObject[] value)
{
return new JArray(value);
Expand Down
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteBooleanValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JBoolean(bool value)
{
return new JBoolean(value);
Expand Down
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteNumberValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JNumber(double value)
{
return new JNumber(value);
Expand Down
31 changes: 24 additions & 7 deletions src/neo/IO/Json/JObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ private static JObject ReadObject(ref Utf8JsonReader reader)
throw new FormatException();
}

public override string ToString()
{
return ToString(false);
}

public string ToString(bool indented)
public byte[] ToByteArray(bool indented)
{
using MemoryStream ms = new MemoryStream();
using Utf8JsonWriter writer = new Utf8JsonWriter(ms, new JsonWriterOptions
Expand All @@ -153,7 +148,17 @@ public string ToString(bool indented)
});
Write(writer);
writer.Flush();
return Encoding.UTF8.GetString(ms.ToArray());
return ms.ToArray();
}

public override string ToString()
{
return ToString(false);
}

public string ToString(bool indented)
{
return Encoding.UTF8.GetString(ToByteArray(indented));
}

public virtual T TryGetEnum<T>(T defaultValue = default, bool ignoreCase = false) where T : Enum
Expand Down Expand Up @@ -199,5 +204,17 @@ public static implicit operator JObject(string value)
{
return (JString)value;
}

public virtual JObject Clone()
{
var cloned = new JObject();

foreach (KeyValuePair<string, JObject> pair in Properties)
{
cloned[pair.Key] = pair.Value != null ? pair.Value.Clone() : Null;
}

return cloned;
}
}
}
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteStringValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JString(Enum value)
{
return new JString(value.ToString());
Expand Down
8 changes: 4 additions & 4 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private class ParallelVerified { public Transaction Transaction; public bool Sho
Witness = new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = new[] { (byte)OpCode.PUSHT }
VerificationScript = new[] { (byte)OpCode.PUSH1 }
},
ConsensusData = new ConsensusData
{
Expand Down Expand Up @@ -157,14 +157,14 @@ private static Transaction DeployNativeContracts()
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitSysCall(InteropService.Neo_Native_Deploy);
sb.EmitSysCall(InteropService.Native.Deploy);
script = sb.ToArray();
}
return new Transaction
{
Version = 0,
Script = script,
Sender = (new[] { (byte)OpCode.PUSHT }).ToScriptHash(),
Sender = (new[] { (byte)OpCode.PUSH1 }).ToScriptHash(),
SystemFee = 0,
Attributes = new TransactionAttribute[0],
Cosigners = new Cosigner[0],
Expand All @@ -173,7 +173,7 @@ private static Transaction DeployNativeContracts()
new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = new[] { (byte)OpCode.PUSHT }
VerificationScript = new[] { (byte)OpCode.PUSH1 }
}
}
};
Expand Down
47 changes: 22 additions & 25 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Network.P2P
{
Expand All @@ -21,6 +22,7 @@ internal class SendDirectly { public IInventory Inventory; }

public const uint ProtocolVersion = 0;
private const int MaxCountFromSeedList = 5;
private readonly IPEndPoint[] SeedList = new IPEndPoint[ProtocolSettings.Default.SeedList.Length];

private static readonly object lockObj = new object();
private readonly NeoSystem system;
Expand Down Expand Up @@ -56,6 +58,14 @@ public LocalNode(NeoSystem system)
throw new InvalidOperationException();
this.system = system;
singleton = this;

// Start dns resolution in parallel

for (int i = 0; i < ProtocolSettings.Default.SeedList.Length; i++)
{
int index = i;
Task.Run(() => SeedList[index] = GetIpEndPoint(ProtocolSettings.Default.SeedList[index]));
}
}
}

Expand Down Expand Up @@ -97,33 +107,18 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in
return new IPEndPoint(ipAddress, port);
}

/// <summary>
/// Return an amount of random seeds nodes from the default SeedList file defined on <see cref="ProtocolSettings"/>.
/// </summary>
/// <param name="seedsToTake">Limit of random seed nodes to be obtained, also limited by the available seeds from file.</param>
private static IEnumerable<IPEndPoint> GetIPEndPointsFromSeedList(int seedsToTake)
internal static IPEndPoint GetIpEndPoint(string hostAndPort)
{
if (seedsToTake > 0)
if (string.IsNullOrEmpty(hostAndPort)) return null;

try
{
Random rand = new Random();
foreach (string hostAndPort in ProtocolSettings.Default.SeedList.OrderBy(p => rand.Next()))
{
if (seedsToTake == 0) break;
string[] p = hostAndPort.Split(':');
IPEndPoint seed;
try
{
seed = GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch (AggregateException)
{
continue;
}
if (seed == null) continue;
seedsToTake--;
yield return seed;
}
string[] p = hostAndPort.Split(':');
return GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch { }

return null;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
Expand Down Expand Up @@ -153,7 +148,9 @@ protected override void NeedMorePeers(int count)
{
// Will call AddPeers with default SeedList set cached on <see cref="ProtocolSettings"/>.
// It will try to add those, sequentially, to the list of currently uncconected ones.
AddPeers(GetIPEndPointsFromSeedList(count));

Random rand = new Random();
AddPeers(SeedList.Where(u => u != null).OrderBy(p => rand.Next()).Take(count));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/neo/Network/P2P/Payloads/Witness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ void ISerializable.Deserialize(BinaryReader reader)
{
// This is designed to allow a MultiSig 10/10 (around 1003 bytes) ~1024 bytes
// Invocation = 10 * 64 + 10 = 650 ~ 664 (exact is 653)
InvocationScript = reader.ReadVarBytes(664);
InvocationScript = reader.ReadVarBytes(663);
// Verification = 10 * 33 + 10 = 340 ~ 360 (exact is 351)
VerificationScript = reader.ReadVarBytes(360);
VerificationScript = reader.ReadVarBytes(361);
}

void ISerializable.Serialize(BinaryWriter writer)
Expand Down
15 changes: 11 additions & 4 deletions src/neo/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,23 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven
if (args.Name.Contains(".resources"))
return null;

AssemblyName an = new AssemblyName(args.Name);

Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
if (assembly != null)
return assembly;
if (assembly is null)
assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == an.Name);
if (assembly != null) return assembly;

AssemblyName an = new AssemblyName(args.Name);
string filename = an.Name + ".dll";
string path = filename;
if (!File.Exists(path)) path = Combine(GetDirectoryName(Assembly.GetEntryAssembly().Location), filename);
if (!File.Exists(path)) path = Combine(PluginsDirectory, filename);
if (!File.Exists(path)) path = Combine(PluginsDirectory, args.RequestingAssembly.GetName().Name, filename);
if (!File.Exists(path)) return null;

try
{
return Assembly.Load(File.ReadAllBytes(filename));
return Assembly.Load(File.ReadAllBytes(path));
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 9fe7805

Please sign in to comment.