-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
184 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Blocks.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="paket.references" /> | ||
</ItemGroup> | ||
<Import Project="..\..\.paket\Paket.Restore.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace BlockChain | ||
|
||
open System | ||
open System.Text | ||
open System.Security.Cryptography | ||
open Newtonsoft.Json | ||
|
||
type Transaction = | ||
{ SenderAddress : string | ||
RecipientAddress : string | ||
Amount : decimal } | ||
|
||
type Block = | ||
{ Index : int | ||
TimeStamp : DateTime | ||
PreviousHash : string | ||
Hash : string | ||
Transactions : Transaction list | ||
Nonce : int } | ||
|
||
module Block = | ||
let empty = | ||
{ Index = 0 | ||
TimeStamp = DateTime.Now | ||
PreviousHash = String.Empty | ||
Hash = String.Empty | ||
Transactions = List.Empty | ||
Nonce = 0 } | ||
|
||
let calculateHash block = | ||
let sha256 = SHA256.Create() | ||
let input = | ||
sprintf "%A-%s-%s-%i" block.TimeStamp block.PreviousHash | ||
(JsonConvert.SerializeObject(block.Transactions)) block.Nonce | ||
let inputBytes = input |> Encoding.ASCII.GetBytes | ||
let outputBytes = sha256.ComputeHash(inputBytes) | ||
Convert.ToBase64String(outputBytes) | ||
|
||
let mine (block, difficulty) = | ||
let leadingZeros = new string('0', difficulty) | ||
let mutable nonce = block.Nonce | ||
let mutable hash = block.Hash | ||
|
||
while Object.ReferenceEquals(hash, null) || hash.Substring(0, difficulty) <> leadingZeros do | ||
nonce <- nonce + 1 | ||
hash <- { block with Nonce = nonce; Hash = hash } |> calculateHash | ||
|
||
{ block with Nonce = nonce; Hash = hash } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FSharp.Core | ||
Newtonsoft.Json |