Split application and library.

This commit is contained in:
2020-09-02 00:03:14 +02:00
parent cd7a71bfe0
commit f97321d929
11 changed files with 123 additions and 19 deletions

4
.gitignore vendored
View File

@@ -1,4 +1,4 @@
/.vs/* /.vs/*
/BasicBlockChain/bin/* */bin/*
/BasicBlockChain/obj/* */obj/*
/packages/ /packages/

View File

@@ -0,0 +1,56 @@
<?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>{919BC116-C8FC-4B65-B742-226B38437C48}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BasicBlockChain.Core</RootNamespace>
<AssemblyName>BasicBlockChain.Core</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</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="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="VAR.Json, Version=1.1.1.36534, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VAR.Json.1.1.1.36534\lib\net461\VAR.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Block.cs" />
<Compile Include="BlockChain.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -2,8 +2,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using VAR.Json;
namespace BasicBlockChain namespace BasicBlockChain.Core
{ {
public class Block public class Block
{ {
@@ -26,8 +27,7 @@ namespace BasicBlockChain
private string GetData() private string GetData()
{ {
VAR.Json.JsonWriter jsonWriter = new VAR.Json.JsonWriter(1); return JsonWriter.WriteObject(Transactions);
return jsonWriter.Write(Transactions);
} }
public string CalculateHash(string data = null, SHA256 sha256 = null) public string CalculateHash(string data = null, SHA256 sha256 = null)

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace BasicBlockChain namespace BasicBlockChain.Core
{ {
public class BlockChain public class BlockChain
{ {

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("BasicBlockChain.Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BasicBlockChain.Core")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[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("919bc116-c8fc-4b65-b742-226b38437c48")]
// 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

@@ -1,6 +1,6 @@
using System; using System;
namespace BasicBlockChain namespace BasicBlockChain.Core
{ {
public class Transaction public class Transaction
{ {

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="VAR.Json" version="1.1.1.36534" targetFramework="net48" />
</packages>

View File

@@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.28803.452
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicBlockChain", "BasicBlockChain\BasicBlockChain.csproj", "{34581A96-29BE-4AB6-9298-BC1AD3E78369}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicBlockChain", "BasicBlockChain\BasicBlockChain.csproj", "{34581A96-29BE-4AB6-9298-BC1AD3E78369}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicBlockChain.Core", "BasicBlockChain.Core\BasicBlockChain.Core.csproj", "{919BC116-C8FC-4B65-B742-226B38437C48}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
{34581A96-29BE-4AB6-9298-BC1AD3E78369}.Debug|Any CPU.Build.0 = Debug|Any CPU {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.ActiveCfg = Release|Any CPU {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.Build.0 = Release|Any CPU {34581A96-29BE-4AB6-9298-BC1AD3E78369}.Release|Any CPU.Build.0 = Release|Any CPU
{919BC116-C8FC-4B65-B742-226B38437C48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{919BC116-C8FC-4B65-B742-226B38437C48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{919BC116-C8FC-4B65-B742-226B38437C48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{919BC116-C8FC-4B65-B742-226B38437C48}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -42,20 +42,22 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="VAR.Json, Version=1.0.6252.27492, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="VAR.Json, Version=1.1.1.36534, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VAR.Json.1.0.6252.27492\lib\net461\VAR.Json.dll</HintPath> <HintPath>..\packages\VAR.Json.1.1.1.36534\lib\net461\VAR.Json.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Block.cs" />
<Compile Include="BlockChain.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BasicBlockChain.Core\BasicBlockChain.Core.csproj">
<Project>{919bc116-c8fc-4b65-b742-226b38437c48}</Project>
<Name>BasicBlockChain.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@@ -1,4 +1,6 @@
using System; using System;
using BasicBlockChain.Core;
using VAR.Json;
namespace BasicBlockChain namespace BasicBlockChain
{ {
@@ -6,20 +8,18 @@ namespace BasicBlockChain
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
VAR.Json.JsonWriter jsonWriter = new VAR.Json.JsonWriter(1);
// Example BlockChain with some example data // Example BlockChain with some example data
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("#### Mining BlockChain with sample data"); Console.WriteLine("#### Mining BlockChain with sample data");
var startTime = DateTime.UtcNow; var startTime = DateTime.UtcNow;
BlockChain nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2); BlockChain nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 3);
nullCoin.AddTransaction(new Transaction("VAR", "NAM", 10_000_000, new DateTime(2000, 1, 2))); nullCoin.AddTransaction(new Transaction("VAR", "NAM", 10_000_000, new DateTime(2000, 1, 2)));
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 2), "Kable"); nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 2), "Kable");
nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 3))); nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 3)));
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 3), "Kable"); nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 3), "Kable");
nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 4))); nullCoin.AddTransaction(new Transaction("NAM", "VAR", 5_000_000, new DateTime(2000, 1, 4)));
nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 4), "Kable"); nullCoin.ProcessPendingTransactions(new DateTime(2000, 1, 4), "Kable");
Console.WriteLine(jsonWriter.Write(nullCoin)); Console.WriteLine(JsonWriter.WriteObject(nullCoin, indent: true));
var endTime = DateTime.UtcNow; var endTime = DateTime.UtcNow;
Console.WriteLine($"Duration: {endTime - startTime}"); Console.WriteLine($"Duration: {endTime - startTime}");
@@ -35,7 +35,7 @@ namespace BasicBlockChain
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("#### Tampering with the data"); Console.WriteLine("#### Tampering with the data");
nullCoin.Chain[1].Transactions[0].MicroCoinAmount = 1000_000_000; nullCoin.Chain[1].Transactions[0].MicroCoinAmount = 1000_000_000;
Console.WriteLine(jsonWriter.Write(nullCoin)); Console.WriteLine(JsonWriter.WriteObject(nullCoin, indent: true));
// Verify // Verify
Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False"); Console.WriteLine("BlockChain is Valid? {0}", nullCoin.Verify() ? "True" : "False");

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="VAR.Json" version="1.0.6252.27492" targetFramework="net472" /> <package id="VAR.Json" version="1.1.1.36534" targetFramework="net48" />
</packages> </packages>