C# Implementation

Ported from blockchain.py
This commit is contained in:
davetoland 2017-11-18 20:45:28 +00:00
parent 5c6f5109c6
commit 81aede25ec
14 changed files with 665 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
</configuration>

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D0C795A0-6F20-4A8E-BE44-801678754DA4}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BlockChainDemo.Console</RootNamespace>
<AssemblyName>BlockChainDemo.Console</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BlockChain\BlockChain.csproj">
<Project>{e06fc4ce-77d0-4a64-94a6-32a08920e481}</Project>
<Name>BlockChain</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,12 @@
namespace BlockChainDemo.Console
{
class Program
{
static void Main(string[] args)
{
var chain = new BlockChain();
var server = new WebServer(chain);
System.Console.Read();
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BlockChain.Console")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BlockChain.Console")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d0c795a0-6f20-4a8e-be44-801678754da4")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

43
csharp/BlockChain.sln Normal file
View File

@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2008
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain", "BlockChain\BlockChain.csproj", "{E06FC4CE-77D0-4A64-94A6-32A08920E481}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain.Console", "BlockChain.Console\BlockChain.Console.csproj", "{D0C795A0-6F20-4A8E-BE44-801678754DA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.Build.0 = Release|Any CPU
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FB3650B-CAFB-4A71-940B-0CA6F0377422}
EndGlobalSection
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 3
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
SccTeamFoundationServer = https://devfire.visualstudio.com/
SccLocalPath0 = .
SccProjectUniqueName1 = BlockChain\\BlockChain.csproj
SccProjectName1 = BlockChain
SccLocalPath1 = BlockChain
SccProjectUniqueName2 = BlockChain.Console\\BlockChain.Console.csproj
SccProjectName2 = BlockChain.Console
SccLocalPath2 = BlockChain.Console
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
namespace BlockChainDemo
{
public class Block
{
public int Index { get; set; }
public DateTime Timestamp { get; set; }
public List<Transaction> Transactions { get; set; }
public int Proof { get; set; }
public string PreviousHash { get; set; }
public override string ToString()
{
return $"{Index} [{Timestamp.ToString("yyyy-MM-dd HH:mm:ss")}] Proof: {Proof} | PrevHash: {PreviousHash} | Trx: {Transactions.Count}";
}
}
}

View File

@ -0,0 +1,220 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace BlockChainDemo
{
public class BlockChain
{
private List<Transaction> _currentTransactions = new List<Transaction>();
private List<Block> _chain = new List<Block>();
private List<Node> _nodes = new List<Node>();
private Block _lastBlock => _chain.Last();
public string NodeId { get; private set; }
//ctor
public BlockChain()
{
NodeId = Guid.NewGuid().ToString().Replace("-", "");
CreateNewBlock(proof: 100, previousHash: "1"); //genesis block
}
//private functionality
private void RegisterNode(string address)
{
_nodes.Add(new Node { Address = new Uri(address) });
}
private bool IsValidChain(List<Block> chain)
{
Block block = null;
Block lastBlock = chain.First();
int currentIndex = 1;
while (currentIndex < chain.Count)
{
block = chain.ElementAt(currentIndex);
Debug.WriteLine($"{lastBlock}");
Debug.WriteLine($"{block}");
Debug.WriteLine("----------------------------");
//Check that the hash of the block is correct
if (block.PreviousHash != GetHash(lastBlock))
return false;
//Check that the Proof of Work is correct
if (!IsValidProof(lastBlock.Proof, block.Proof, lastBlock.PreviousHash))
return false;
lastBlock = block;
currentIndex++;
}
return true;
}
private bool ResolveConflicts()
{
List<Block> newChain = null;
int maxLength = _chain.Count;
foreach (Node node in _nodes)
{
var request = (HttpWebRequest)WebRequest.Create(node.Address);
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))
{
maxLength = chain.Count;
newChain = chain;
}
}
}
if (newChain != null)
{
_chain = newChain;
return true;
}
return false;
}
private Block CreateNewBlock(int proof, string previousHash = null)
{
var block = new Block
{
Index = _chain.Count,
Timestamp = DateTime.UtcNow,
Transactions = _currentTransactions.ToList(),
Proof = proof,
PreviousHash = previousHash ?? GetHash(_chain.Last())
};
_currentTransactions.Clear();
_chain.Add(block);
return block;
}
private int CreateProofOfWork(int lastProof, string previousHash)
{
int proof = 0;
while (!IsValidProof(lastProof, proof, previousHash))
proof++;
return proof;
}
private bool IsValidProof(int lastProof, int proof, string previousHash)
{
string guess = $"{lastProof}{proof}{previousHash}";
string result = GetSha256(guess);
return result.StartsWith("0000");
}
private string GetHash(Block block)
{
string blockText = JsonConvert.SerializeObject(block);
return GetSha256(blockText);
}
private string GetSha256(string data)
{
var sha256 = new SHA256Managed();
var hashBuilder = new StringBuilder();
byte[] bytes = Encoding.Unicode.GetBytes(data);
byte[] hash = sha256.ComputeHash(bytes);
foreach (byte x in hash)
hashBuilder.Append($"{x:x2}");
return hashBuilder.ToString();
}
//web server calls
internal string Mine()
{
int proof = CreateProofOfWork(_lastBlock.Proof, _lastBlock.PreviousHash);
CreateTransaction(sender: "0", recipient: NodeId, amount: 1);
Block block = CreateNewBlock(proof /*, _lastBlock.PreviousHash*/);
var response = new
{
Message = "New Block Forged",
Index = block.Index,
Transactions = block.Transactions.ToArray(),
Proof = block.Proof,
PreviousHash = block.PreviousHash
};
return JsonConvert.SerializeObject(response);
}
internal string GetFullChain()
{
var response = new
{
chain = _chain.ToArray(),
length = _chain.Count
};
return JsonConvert.SerializeObject(response);
}
internal string RegisterNodes(string[] nodes)
{
var builder = new StringBuilder();
foreach (string node in nodes)
{
string url = $"https://{node}";
RegisterNode(url);
builder.Append($"{url}, ");
}
builder.Insert(0, $"{nodes.Count()} new nodes have been added: ");
string result = builder.ToString();
return result.Substring(0, result.Length - 2);
}
internal string Consensus()
{
bool replaced = ResolveConflicts();
string message = replaced ? "was replaced" : "is authoritive";
var response = new
{
Message = $"Our chain {message}",
Chain = _chain
};
return JsonConvert.SerializeObject(response);
}
internal int CreateTransaction(string sender, string recipient, int amount)
{
var transaction = new Transaction
{
Sender = sender,
Recipient = recipient,
Amount = amount
};
_currentTransactions.Add(transaction);
return _lastBlock != null ? _lastBlock.Index + 1 : 0;
}
}
}

View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E06FC4CE-77D0-4A64-94A6-32A08920E481}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BlockChainDemo</RootNamespace>
<AssemblyName>BlockChainDemo</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TinyWebServer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\TinyWebServer.dll.1.0.1\lib\net40\TinyWebServer.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Block.cs" />
<Compile Include="BlockChain.cs" />
<Compile Include="Node.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" />
<Compile Include="WebServer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,9 @@
using System;
namespace BlockChainDemo
{
public class Node
{
public Uri Address { get; set; }
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BlockChain")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BlockChain")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e06fc4ce-77d0-4a64-94a6-32a08920e481")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,9 @@
namespace BlockChainDemo
{
public class Transaction
{
public int Amount { get; set; }
public string Recipient { get; set; }
public string Sender { get; set; }
}
}

View File

@ -0,0 +1,72 @@
using Newtonsoft.Json;
using System.IO;
using System.Net;
using System.Net.Http;
namespace BlockChainDemo
{
public class WebServer
{
public WebServer(BlockChain chain)
{
var server = new TinyWebServer.WebServer(request =>
{
string path = request.Url.PathAndQuery.ToLower();
string query = "";
string json = "";
if (path.Contains("?"))
{
string[] parts = path.Split('?');
path = parts[0];
query = parts[1];
}
switch (path)
{
//GET: http://localhost:12345/mine
case "/mine":
return chain.Mine();
//POST: http://localhost:12345/transactions/new
//{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" }
case "/transactions/new":
if (request.HttpMethod != HttpMethod.Post.Method)
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
json = new StreamReader(request.InputStream).ReadToEnd();
Transaction trx = JsonConvert.DeserializeObject<Transaction>(json);
int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount);
return $"Your transaction will be included in block {blockId}";
//GET: http://localhost:12345/chain
case "/chain":
return chain.GetFullChain();
//POST: http://localhost:12345/nodes/register
case "/nodes/register":
if (request.HttpMethod != HttpMethod.Post.Method)
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
json = new StreamReader(request.InputStream).ReadToEnd();
var urlList = new { Urls = new string[0] };
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
return chain.RegisterNodes(obj.Urls);
//GET: http://localhost:12345/nodes/register
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/"
);
server.Run();
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net451" />
<package id="TinyWebServer.dll" version="1.0.1" targetFramework="net451" />
</packages>