Bug fixes, plus config driven host and port

This commit is contained in:
davetoland 2017-11-19 01:36:34 +00:00
parent 839fd24b14
commit db16e16ab0
4 changed files with 30 additions and 14 deletions

View File

@ -3,4 +3,8 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
<appSettings>
<add key="host" value="localhost" />
<add key="port" value="12345" />
</appSettings>
</configuration>

View File

@ -66,18 +66,24 @@ namespace BlockChainDemo
foreach (Node node in _nodes)
{
var request = (HttpWebRequest)WebRequest.Create(node.Address);
var url = new Uri(node.Address, "/chain");
var request = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
List<Block> chain = JsonConvert.DeserializeObject<List<Block>>(json);
if (chain.Count > _chain.Count && IsValidChain(chain))
var model = new
{
maxLength = chain.Count;
newChain = chain;
chain = new List<Block>(),
length = 0
};
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
var data = JsonConvert.DeserializeAnonymousType(json, model);
if (data.chain.Count > _chain.Count && IsValidChain(data.chain))
{
maxLength = data.chain.Count;
newChain = data.chain;
}
}
}
@ -179,7 +185,7 @@ namespace BlockChainDemo
var builder = new StringBuilder();
foreach (string node in nodes)
{
string url = $"https://{node}";
string url = $"http://{node}";
RegisterNode(url);
builder.Append($"{url}, ");
}

View File

@ -39,6 +39,7 @@
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
@ -9,6 +10,10 @@ namespace BlockChainDemo
{
public WebServer(BlockChain chain)
{
var settings = ConfigurationManager.AppSettings;
string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
string port = settings["port"]?.Length > 1 ? settings["port"] : "12345";
var server = new TinyWebServer.WebServer(request =>
{
string path = request.Url.PathAndQuery.ToLower();
@ -53,18 +58,18 @@ namespace BlockChainDemo
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
return chain.RegisterNodes(obj.Urls);
//GET: http://localhost:12345/nodes/register
//GET: http://localhost:12345/nodes/resolve
case "/nodes/resolve":
return chain.Consensus();
}
return "";
},
"http://localhost:12345/mine/",
"http://localhost:12345/transactions/new/",
"http://localhost:12345/chain/",
"http://localhost:12345/nodes/register/",
"http://localhost:12345/nodes/resolve/"
$"http://{host}:{port}/mine/",
$"http://{host}:{port}/transactions/new/",
$"http://{host}:{port}/chain/",
$"http://{host}:{port}/nodes/register/",
$"http://{host}:{port}/nodes/resolve/"
);
server.Run();