Skip to content

Commit

Permalink
Added Block structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajivhost committed Apr 7, 2019
1 parent 5e65dc8 commit 41e694d
Showing 8 changed files with 184 additions and 47 deletions.
7 changes: 7 additions & 0 deletions BlockChain.sln
Original file line number Diff line number Diff line change
@@ -10,18 +10,25 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E90D0681-1DA
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BlockChain.API", "src\BlockChain.API\BlockChain.API.fsproj", "{87551B2F-B3FE-4192-BF7E-7A903F9EA903}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BlockChain.Core", "src\BlockChain.Core\BlockChain.Core.fsproj", "{DF04CE25-21DE-4788-9B28-4896BE246FB9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{87551B2F-B3FE-4192-BF7E-7A903F9EA903} = {E90D0681-1DA8-4D03-A600-A0E6C41786E0}
{DF04CE25-21DE-4788-9B28-4896BE246FB9} = {E90D0681-1DA8-4D03-A600-A0E6C41786E0}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{87551B2F-B3FE-4192-BF7E-7A903F9EA903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87551B2F-B3FE-4192-BF7E-7A903F9EA903}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87551B2F-B3FE-4192-BF7E-7A903F9EA903}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87551B2F-B3FE-4192-BF7E-7A903F9EA903}.Release|Any CPU.Build.0 = Release|Any CPU
{DF04CE25-21DE-4788-9B28-4896BE246FB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF04CE25-21DE-4788-9B28-4896BE246FB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF04CE25-21DE-4788-9B28-4896BE246FB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF04CE25-21DE-4788-9B28-4896BE246FB9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
@@ -4,4 +4,5 @@ storage: none
source https://api.nuget.org/v3/index.json

nuget FSharp.Core
nuget Saturn.Extensions.Authorization
nuget Newtonsoft.Json
nuget Saturn.Extensions.Authorization
138 changes: 106 additions & 32 deletions paket.lock

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/BlockChain.API/Program.fs
Original file line number Diff line number Diff line change
@@ -12,14 +12,10 @@ open Microsoft.Extensions.Logging

module Program =
let exitCode = 0

let CreateWebHostBuilder args =
WebHost
.CreateDefaultBuilder(args)
.UseStartup<Startup>();
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>()

[<EntryPoint>]
let main args =
CreateWebHostBuilder(args).Build().Run()

exitCode
14 changes: 5 additions & 9 deletions src/BlockChain.API/Startup.fs
Original file line number Diff line number Diff line change
@@ -7,15 +7,11 @@ open Microsoft.AspNetCore.Http
open Microsoft.Extensions.DependencyInjection

type Startup() =

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
member this.ConfigureServices(services: IServiceCollection) =
()

member this.ConfigureServices(services : IServiceCollection) = ()
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment) =
if env.IsDevelopment() then
app.UseDeveloperExceptionPage() |> ignore

app.Run(fun context -> context.Response.WriteAsync("Hello World!")) |> ignore
member this.Configure(app : IApplicationBuilder, env : IHostingEnvironment) =
if env.IsDevelopment() then app.UseDeveloperExceptionPage() |> ignore
app.Run(fun context -> context.Response.WriteAsync("Hello World!"))
|> ignore
13 changes: 13 additions & 0 deletions src/BlockChain.Core/BlockChain.Core.fsproj
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>
48 changes: 48 additions & 0 deletions src/BlockChain.Core/Blocks.fs
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 }
2 changes: 2 additions & 0 deletions src/BlockChain.Core/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FSharp.Core
Newtonsoft.Json

0 comments on commit 41e694d

Please sign in to comment.