Skip to content

Commit db16e16

Browse files
author
davetoland
committed
Bug fixes, plus config driven host and port
1 parent 839fd24 commit db16e16

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

csharp/BlockChain.Console/App.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
55
</startup>
6+
<appSettings>
7+
<add key="host" value="localhost" />
8+
<add key="port" value="12345" />
9+
</appSettings>
610
</configuration>

csharp/BlockChain/BlockChain.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,24 @@ private bool ResolveConflicts()
6666

6767
foreach (Node node in _nodes)
6868
{
69-
var request = (HttpWebRequest)WebRequest.Create(node.Address);
69+
var url = new Uri(node.Address, "/chain");
70+
var request = (HttpWebRequest)WebRequest.Create(url);
7071
var response = (HttpWebResponse)request.GetResponse();
7172

7273
if (response.StatusCode == HttpStatusCode.OK)
7374
{
75+
var model = new
76+
{
77+
chain = new List<Block>(),
78+
length = 0
79+
};
7480
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
75-
List<Block> chain = JsonConvert.DeserializeObject<List<Block>>(json);
81+
var data = JsonConvert.DeserializeAnonymousType(json, model);
7682

77-
if (chain.Count > _chain.Count && IsValidChain(chain))
83+
if (data.chain.Count > _chain.Count && IsValidChain(data.chain))
7884
{
79-
maxLength = chain.Count;
80-
newChain = chain;
85+
maxLength = data.chain.Count;
86+
newChain = data.chain;
8187
}
8288
}
8389
}
@@ -179,7 +185,7 @@ internal string RegisterNodes(string[] nodes)
179185
var builder = new StringBuilder();
180186
foreach (string node in nodes)
181187
{
182-
string url = $"https://{node}";
188+
string url = $"http://{node}";
183189
RegisterNode(url);
184190
builder.Append($"{url}, ");
185191
}

csharp/BlockChain/BlockChain.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
4040
</Reference>
4141
<Reference Include="System" />
42+
<Reference Include="System.configuration" />
4243
<Reference Include="System.Core" />
4344
<Reference Include="System.Xml.Linq" />
4445
<Reference Include="System.Data.DataSetExtensions" />

csharp/BlockChain/WebServer.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json;
2+
using System.Configuration;
23
using System.IO;
34
using System.Net;
45
using System.Net.Http;
@@ -9,6 +10,10 @@ public class WebServer
910
{
1011
public WebServer(BlockChain chain)
1112
{
13+
var settings = ConfigurationManager.AppSettings;
14+
string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
15+
string port = settings["port"]?.Length > 1 ? settings["port"] : "12345";
16+
1217
var server = new TinyWebServer.WebServer(request =>
1318
{
1419
string path = request.Url.PathAndQuery.ToLower();
@@ -53,18 +58,18 @@ public WebServer(BlockChain chain)
5358
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
5459
return chain.RegisterNodes(obj.Urls);
5560

56-
//GET: http://localhost:12345/nodes/register
61+
//GET: http://localhost:12345/nodes/resolve
5762
case "/nodes/resolve":
5863
return chain.Consensus();
5964
}
6065

6166
return "";
6267
},
63-
"http://localhost:12345/mine/",
64-
"http://localhost:12345/transactions/new/",
65-
"http://localhost:12345/chain/",
66-
"http://localhost:12345/nodes/register/",
67-
"http://localhost:12345/nodes/resolve/"
68+
$"http://{host}:{port}/mine/",
69+
$"http://{host}:{port}/transactions/new/",
70+
$"http://{host}:{port}/chain/",
71+
$"http://{host}:{port}/nodes/register/",
72+
$"http://{host}:{port}/nodes/resolve/"
6873
);
6974

7075
server.Run();

0 commit comments

Comments
 (0)