Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions DltNode/DltNode.Blockchain/Block.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DltNode.Hash;

namespace DltNode.Blockchain
{
public class Block
{
public readonly Byte[] blockHash;

public readonly Byte[] previousBlockHash;

public readonly List<Transaction> transactions;

public readonly Byte[] zero;
/// <summary>
///
/// </summary>
/// <param name="transactions"></param>
/// <param name="previousBlock">Put null in to first block constructor</param>
public Block(List<Transaction> transactions, Block previousBlock)
{
zero = new byte[32];
this.previousBlockHash = previousBlock?.blockHash ?? zero;
this.transactions = transactions;

List<Byte[]> hashes = new List<Byte[]>();
hashes.Add(previousBlockHash);
foreach(var tx in transactions)
{

hashes.Add(tx.GetHash());
}
blockHash = computeMerkleTreeHash(hashes);
}
public Byte[] computeMerkleTreeHash(List<Byte[]> hashes)
{
if (hashes.Count == 1)
return hashes[0];
else if (hashes.Count == 2)
return PureHash.computeHash(hashes[0].Concat(hashes[1]).ToArray());
else
{
List<Byte[]> firstPart = hashes.GetRange(0, hashes.Count / 2);
List<Byte[]> secondPart = hashes.GetRange(hashes.Count / 2, hashes.Count - 1);
var hashOfLeft = computeMerkleTreeHash(firstPart);
var hashOfRight = computeMerkleTreeHash(secondPart);
return PureHash.computeHash(hashOfLeft.Concat(hashOfRight).ToArray());
}

return Array.Empty<Byte>();
}
}
}
33 changes: 33 additions & 0 deletions DltNode/DltNode.Blockchain/Blockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DltNode.Blockchain
{
public class Blockchain
{
public List<Block> blockchain;

public Blockchain(Block genesisBlock)
{
blockchain = new List<Block>();
blockchain.Add(genesisBlock);
}
public Boolean AddNewBlock(Block newBlock)
{
if (newBlock.previousBlockHash != blockchain[blockchain.Count() - 1].blockHash)
{
return false;
}
else
{
blockchain.Add(newBlock);
return true;
}
}

public Int32 Height => blockchain.Count();
}
}
9 changes: 0 additions & 9 deletions DltNode/DltNode.Blockchain/Class1.cs

This file was deleted.

6 changes: 5 additions & 1 deletion DltNode/DltNode.Blockchain/DltNode.Blockchain.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DltNode.Hash\DltNode.Hash.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions DltNode/DltNode.Blockchain/Transaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using DltNode.Hash;
using System.Text;
namespace DltNode.Blockchain
{
public class Transaction
{
public readonly String info;
public Transaction(string info)
{
this.info = info;
}

public Byte[] GetHash() => PureHash.computeHash(Encoding.UTF8.GetBytes(info));
}
}
7 changes: 7 additions & 0 deletions DltNode/DltNode.Hash/DltNode.Hash.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

</Project>
20 changes: 20 additions & 0 deletions DltNode/DltNode.Hash/PureHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Security.Cryptography;
using System.Collections;
namespace DltNode.Hash
{
public static class PureHash
{

//public
public static byte[] computeHash(byte[] input)
{
var hashing = SHA256.Create();
//input.Length


return hashing.ComputeHash(input);
}

}
}
2 changes: 1 addition & 1 deletion DltNode/DltNode.Main/DltNode.Main.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
28 changes: 24 additions & 4 deletions DltNode/DltNode.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
using System;

using System.Collections.Generic;
using System.Text;
using DltNode.Blockchain;
namespace DltNode.Main
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine();
Console.WriteLine(1);
Transaction tx1 = new Transaction("Product X has moved from A to B");
Transaction tx2 = new Transaction("Product Y has moved from C to D");

List<Transaction> transactions = new List<Transaction>();
transactions.Add(tx1);
transactions.Add(tx2);

Block firstBlock = new Block(transactions, null);
Console.WriteLine(BitConverter.ToString(firstBlock.blockHash));
Transaction tx0 = new Transaction("Product Z has moved from E to F");
List<Transaction> transactions1 = new List<Transaction>();
transactions1.Add(tx0);

Block secondBlock = new Block(transactions1, firstBlock);
Console.WriteLine(BitConverter.ToString(secondBlock.previousBlockHash));
Console.WriteLine(BitConverter.ToString(secondBlock.blockHash));

Blockchain.Blockchain blockchain = new Blockchain.Blockchain(firstBlock);
Console.WriteLine(blockchain.Height);
Console.WriteLine(blockchain.AddNewBlock(secondBlock));
Console.WriteLine(blockchain.Height);
}
}
}
10 changes: 8 additions & 2 deletions DltNode/DltNode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DltNode.Main", "DltNode.Main\DltNode.Main.csproj", "{B8E636E8-B130-42C2-A2D9-24E95AAB97AA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DltNode.Main", "DltNode.Main\DltNode.Main.csproj", "{B8E636E8-B130-42C2-A2D9-24E95AAB97AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DltNode.Blockchain", "DltNode.Blockchain\DltNode.Blockchain.csproj", "{15EB2A24-0420-4CB0-9D46-EE8E84534BF1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DltNode.Blockchain", "DltNode.Blockchain\DltNode.Blockchain.csproj", "{15EB2A24-0420-4CB0-9D46-EE8E84534BF1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DltNode.Hash", "DltNode.Hash\DltNode.Hash.csproj", "{5E5F9D32-2481-4688-9C9C-0F9D2BCB6C67}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{15EB2A24-0420-4CB0-9D46-EE8E84534BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15EB2A24-0420-4CB0-9D46-EE8E84534BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15EB2A24-0420-4CB0-9D46-EE8E84534BF1}.Release|Any CPU.Build.0 = Release|Any CPU
{5E5F9D32-2481-4688-9C9C-0F9D2BCB6C67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E5F9D32-2481-4688-9C9C-0F9D2BCB6C67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E5F9D32-2481-4688-9C9C-0F9D2BCB6C67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E5F9D32-2481-4688-9C9C-0F9D2BCB6C67}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down