Split application and library.
This commit is contained in:
56
BasicBlockChain.Core/BasicBlockChain.Core.csproj
Normal file
56
BasicBlockChain.Core/BasicBlockChain.Core.csproj
Normal 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>
|
||||
63
BasicBlockChain.Core/Block.cs
Normal file
63
BasicBlockChain.Core/Block.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using VAR.Json;
|
||||
|
||||
namespace BasicBlockChain.Core
|
||||
{
|
||||
public class Block
|
||||
{
|
||||
public int Index { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public string PreviousHash { get; set; }
|
||||
public List<Transaction> Transactions { get; } = new List<Transaction>();
|
||||
public string Hash { get; set; }
|
||||
public int Nonce { get; set; }
|
||||
|
||||
public Block(DateTime date, Block previousBlock, IList<Transaction> transactions)
|
||||
{
|
||||
Index = (previousBlock?.Index ?? -1) + 1;
|
||||
Date = date;
|
||||
PreviousHash = previousBlock?.Hash;
|
||||
if (transactions != null) { Transactions.AddRange(transactions); }
|
||||
Nonce = 0;
|
||||
Hash = CalculateHash();
|
||||
}
|
||||
|
||||
private string GetData()
|
||||
{
|
||||
return JsonWriter.WriteObject(Transactions);
|
||||
}
|
||||
|
||||
public string CalculateHash(string data = null, SHA256 sha256 = null)
|
||||
{
|
||||
if (sha256 == null) { sha256 = SHA256.Create(); }
|
||||
if (data == null)
|
||||
{
|
||||
data = GetData();
|
||||
}
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(string.Format("{0}-{1}-{2}-{3}",
|
||||
Date,
|
||||
PreviousHash,
|
||||
data,
|
||||
Nonce));
|
||||
byte[] hashBytes = sha256.ComputeHash(dataBytes);
|
||||
|
||||
string hash = Convert.ToBase64String(hashBytes);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public void Mine(int difficulty)
|
||||
{
|
||||
SHA256 sha256 = SHA256.Create();
|
||||
var leadingZeros = new string('0', difficulty);
|
||||
string data = GetData();
|
||||
while (Hash.Substring(0, difficulty) != leadingZeros)
|
||||
{
|
||||
Nonce++;
|
||||
Hash = CalculateHash(data, sha256);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
87
BasicBlockChain.Core/BlockChain.cs
Normal file
87
BasicBlockChain.Core/BlockChain.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BasicBlockChain.Core
|
||||
{
|
||||
public class BlockChain
|
||||
{
|
||||
private List<Transaction> _pendingTransactions = new List<Transaction>();
|
||||
public List<Block> Chain { get; } = new List<Block>();
|
||||
public int Difficulty { get; set; } = 2;
|
||||
public int Reward { get; set; } = 1_000_000;
|
||||
|
||||
public BlockChain(DateTime? genesisDate = null, int difficulty = 2, int reward = 1_000_000)
|
||||
{
|
||||
Block genesisBlock = new Block(genesisDate ?? DateTime.UtcNow, null, null);
|
||||
genesisBlock.Mine(difficulty);
|
||||
Difficulty = difficulty;
|
||||
Reward = 1_000_000;
|
||||
Chain.Add(genesisBlock);
|
||||
}
|
||||
|
||||
public void AddBlock(DateTime date, IList<Transaction> transactions)
|
||||
{
|
||||
Block lastBlock = Chain.Last();
|
||||
Block newBlock = new Block(date, lastBlock, transactions);
|
||||
newBlock.Mine(Difficulty);
|
||||
Chain.Add(newBlock);
|
||||
}
|
||||
|
||||
public void AddTransaction(Transaction transaction)
|
||||
{
|
||||
_pendingTransactions.Add(transaction);
|
||||
}
|
||||
|
||||
public void ProcessPendingTransactions(DateTime date, string miner)
|
||||
{
|
||||
Block lastBlock = Chain.Last();
|
||||
Block newBlock = new Block(date, lastBlock, _pendingTransactions);
|
||||
newBlock.Transactions.Add(new Transaction(null, miner, Reward, date));
|
||||
newBlock.Mine(Difficulty);
|
||||
Chain.Add(newBlock);
|
||||
_pendingTransactions.Clear();
|
||||
}
|
||||
|
||||
public bool Verify()
|
||||
{
|
||||
for (int i = 1; i < Chain.Count; i++)
|
||||
{
|
||||
Block currentBlock = Chain[i];
|
||||
Block previousBlock = Chain[i - 1];
|
||||
|
||||
string currentBlockHashRecalculated = currentBlock.CalculateHash();
|
||||
if (currentBlock.Hash != currentBlockHashRecalculated || currentBlock.PreviousHash != previousBlock.Hash)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public long GetMicroCoinBalance(string receiver)
|
||||
{
|
||||
long microCoinBalance = 0;
|
||||
|
||||
for (int i = 0; i < Chain.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < Chain[i].Transactions.Count; j++)
|
||||
{
|
||||
var transaction = Chain[i].Transactions[j];
|
||||
|
||||
if (transaction.Sender == receiver)
|
||||
{
|
||||
microCoinBalance -= transaction.MicroCoinAmount;
|
||||
}
|
||||
|
||||
if (transaction.Receiver == receiver)
|
||||
{
|
||||
microCoinBalance += transaction.MicroCoinAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return microCoinBalance;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
BasicBlockChain.Core/Properties/AssemblyInfo.cs
Normal file
36
BasicBlockChain.Core/Properties/AssemblyInfo.cs
Normal 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")]
|
||||
20
BasicBlockChain.Core/Transaction.cs
Normal file
20
BasicBlockChain.Core/Transaction.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace BasicBlockChain.Core
|
||||
{
|
||||
public class Transaction
|
||||
{
|
||||
public string Sender { get; set; }
|
||||
public string Receiver { get; set; }
|
||||
public long MicroCoinAmount { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
public Transaction(string sender, string receiver, long microCoinAmount, DateTime date)
|
||||
{
|
||||
Sender = sender;
|
||||
Receiver = receiver;
|
||||
MicroCoinAmount = microCoinAmount;
|
||||
Date = date;
|
||||
}
|
||||
}
|
||||
}
|
||||
4
BasicBlockChain.Core/packages.config
Normal file
4
BasicBlockChain.Core/packages.config
Normal 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>
|
||||
Reference in New Issue
Block a user