Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc0247 committed Jun 24, 2018
1 parent 376ab95 commit 134efcd
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bad_node_demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void Main(string[] args)
{
var addr = Console.ReadLine();

peers.AddPeer(addr);
peers.ConnectPeer(addr);
}
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion src/mcore/DataType/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial struct Hash

private static readonly int ValidAddrLength = -1;

public int Length => addr.Length;
public int Length => addr != null ? addr.Length : 0;
public string str => addr;

private string addr;
Expand Down
27 changes: 24 additions & 3 deletions src/mcore/Serialize/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,39 @@ static Serializer()

public static string Serialize(this object o, bool indented = false)
{
return JsonConvert.SerializeObject(o,
indented ? Formatting.Indented : Formatting.None,
converters);
var setting = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
ContractResolver = new NonPublicPropertiesResolver(),
Converters = converters,
Formatting = indented ? Formatting.Indented : Formatting.None
};

return JsonConvert.SerializeObject(o, setting);
}
public static T Deserialize<T>(this string json)
{
var setting = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
ContractResolver = new NonPublicPropertiesResolver(),
Converters = converters
};

return JsonConvert.DeserializeObject<T>(json, setting);
}
public static object Deserialize(this string json)
{
var setting = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
ContractResolver = new NonPublicPropertiesResolver(),
Converters = converters
};

return JsonConvert.DeserializeObject(json, setting);
}
}
11 changes: 11 additions & 0 deletions src/minichain.test/mcore/Hash_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@ public void HashToString()

Assert.AreEqual(hash.ToString(), hash.str);
}

[TestMethod]
public void Serialize()
{
var hash = Hash.Calc("a");

var json = Serializer.Serialize(hash);
var back = Serializer.Deserialize<Hash>(json);

Assert.AreEqual(hash.ToString(), back.ToString());
}
}
}
2 changes: 2 additions & 0 deletions src/minichain/Chain/ChainState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ internal bool PushBlock(Block block, bool isSync = false)

onBlockConfirmed?.Invoke(block);

Console.WriteLine(" * Block accepted: " + currentBlock.blockNo);

return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/minichain/Net/Packet/Packets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public static PacketBase FromJson(string json)
{
try
{
return (PacketBase)JsonConvert.DeserializeObject(json, SerializeSetting);
return (PacketBase)Serializer.Deserialize(json);
}
catch(Exception e)
{
Console.WriteLine(e);
return null;
}
}
Expand All @@ -52,7 +53,7 @@ public PacketBase()
/// <returns></returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, SerializeSetting);
return Serializer.Serialize(this);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/minichain/Net/PeerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ public PeerPool(NodeBase _nodeBase)
Console.WriteLine("RUNNING on port " + listeningPort);

foreach (var addr in HardCodedSeeds.Addrs)
AddPeer(addr);
ConnectPeer(addr);

onPeerConnected += p =>
{
Console.WriteLine("[PEER] New peer " + p.address);
};
}

public void AddPeer(string addr)
public void ConnectPeer(string addr)
{
if (peers.Count >= MaxPeers) return;
if (peers.Any(x => x.Key.address == addr)) return;
Expand All @@ -63,8 +68,6 @@ public void AddPeer(string addr)
var ws = new WebSocket(addr);
var peer = new Peer(this, ws);
ws.Connect();

onPeerConnected?.Invoke(peer);
}
public void AddPeer(Peer peer)
{
Expand All @@ -90,6 +93,7 @@ public void SendPacketToAllPeers(PacketBase packet)
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/minichain/POW/Miner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,11 @@ private void MinerHQ()

PublishBlock(chain.currentBlock);

/*
Console.WriteLine(
$" * FindBlock#{chain.currentBlock.blockNo}, elapsed {elapsed} sec(s)\r\n" +
$" nonce: {solution} \r\n" +
$" prevBlock: {chain.currentBlock.prevBlockHash} \r\n" +
$" txs: {chain.currentBlock.txs.Length}");
*/
}
}
}
Expand All @@ -125,11 +123,9 @@ private void OnNewBlockDiscovered(Block block)

private void PrepareWorkers(Block vblock, int nThread)
{
/*
Console.WriteLine(
"------------------------------------------------------------\r\n" +
"Preparing job, block#" + vblock.blockNo + " with " + nThread + " thread(s).");
*/

Interlocked.Exchange(ref hashCounter, 0);
for (int i = 0; i < nThread; i++)
Expand Down
6 changes: 3 additions & 3 deletions src/minichain/State/EndpointNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void OnFoundLocalPeer(Int64 id, string payload, IPEndPoint ep)
if (payload == peers.listeningPort.ToString())
return;

peers.AddPeer(ep.Address.ToString().Split(':')[0] + ":" + payload);
peers.ConnectPeer(ep.Address.ToString().Split(':')[0] + ":" + payload);
}

/// <summary>
Expand Down Expand Up @@ -125,7 +125,7 @@ private void OnResponsePeers(Peer sender, PktResponsePeers pkt)
if (pkt.addrs == null) return;

foreach (var addr in pkt.addrs)
peers.AddPeer(addr);
peers.ConnectPeer(addr);
}

private void OnRequestBlock(Peer sender, PktRequestBlock pkt)
Expand Down Expand Up @@ -180,7 +180,7 @@ protected virtual void OnNewBlock(Peer sender, PktBroadcastNewBlock pkt)
if (state != NodeState.OK) return;

// Peer sent invalid block.
if (Block.IsValidBlockLight(pkt.block, pkt.block.nonce) == false) { Console.WriteLine("Invalid block"); return; }
if (Block.IsValidBlockLight(pkt.block, pkt.block.nonce) == false) return;
// My block is longer than received
if (chain.currentBlock.blockNo >= pkt.block.blockNo) return;

Expand Down

0 comments on commit 134efcd

Please sign in to comment.