Bug fixes, plus config driven host and port
This commit is contained in:
parent
839fd24b14
commit
db16e16ab0
|
@ -3,4 +3,8 @@
|
||||||
<startup>
|
<startup>
|
||||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
|
||||||
</startup>
|
</startup>
|
||||||
|
<appSettings>
|
||||||
|
<add key="host" value="localhost" />
|
||||||
|
<add key="port" value="12345" />
|
||||||
|
</appSettings>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -66,18 +66,24 @@ namespace BlockChainDemo
|
||||||
|
|
||||||
foreach (Node node in _nodes)
|
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();
|
var response = (HttpWebResponse)request.GetResponse();
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.OK)
|
if (response.StatusCode == HttpStatusCode.OK)
|
||||||
{
|
{
|
||||||
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
var model = new
|
||||||
List<Block> chain = JsonConvert.DeserializeObject<List<Block>>(json);
|
|
||||||
|
|
||||||
if (chain.Count > _chain.Count && IsValidChain(chain))
|
|
||||||
{
|
{
|
||||||
maxLength = chain.Count;
|
chain = new List<Block>(),
|
||||||
newChain = chain;
|
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();
|
var builder = new StringBuilder();
|
||||||
foreach (string node in nodes)
|
foreach (string node in nodes)
|
||||||
{
|
{
|
||||||
string url = $"https://{node}";
|
string url = $"http://{node}";
|
||||||
RegisterNode(url);
|
RegisterNode(url);
|
||||||
builder.Append($"{url}, ");
|
builder.Append($"{url}, ");
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.configuration" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using System.Configuration;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
@ -9,6 +10,10 @@ namespace BlockChainDemo
|
||||||
{
|
{
|
||||||
public WebServer(BlockChain chain)
|
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 =>
|
var server = new TinyWebServer.WebServer(request =>
|
||||||
{
|
{
|
||||||
string path = request.Url.PathAndQuery.ToLower();
|
string path = request.Url.PathAndQuery.ToLower();
|
||||||
|
@ -53,18 +58,18 @@ namespace BlockChainDemo
|
||||||
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
|
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
|
||||||
return chain.RegisterNodes(obj.Urls);
|
return chain.RegisterNodes(obj.Urls);
|
||||||
|
|
||||||
//GET: http://localhost:12345/nodes/register
|
//GET: http://localhost:12345/nodes/resolve
|
||||||
case "/nodes/resolve":
|
case "/nodes/resolve":
|
||||||
return chain.Consensus();
|
return chain.Consensus();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
"http://localhost:12345/mine/",
|
$"http://{host}:{port}/mine/",
|
||||||
"http://localhost:12345/transactions/new/",
|
$"http://{host}:{port}/transactions/new/",
|
||||||
"http://localhost:12345/chain/",
|
$"http://{host}:{port}/chain/",
|
||||||
"http://localhost:12345/nodes/register/",
|
$"http://{host}:{port}/nodes/register/",
|
||||||
"http://localhost:12345/nodes/resolve/"
|
$"http://{host}:{port}/nodes/resolve/"
|
||||||
);
|
);
|
||||||
|
|
||||||
server.Run();
|
server.Run();
|
||||||
|
|
Loading…
Reference in New Issue