0 people like it.
Like the snippet!
More parallel option for nonce checking in NBitcoin block-chain` for http://www.fssnip.net/7RY
More parallel calculating alternative option for ``mine to correct`` for
http://www.fssnip.net/7RY/title/Using-NBitcoin-to-create-private-BlockChain-with-F-FSharp
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
|
let ``mine to correct`` checkType (chain:ConcurrentChain) (block:Block) =
let validation =
match checkType with
| NoWork -> fun (cb:ChainedBlock) -> true
| EasyWork -> fun cb -> cb.ValidateEasy network
| CorrectWork -> fun cb -> cb.Validate network
let processors = uint32(Environment.ProcessorCount)
let prevBlock = chain.GetBlock(block.Header.HashPrevBlock)
let rec mine nonce (blockHead:BlockHeader) =
let bh = blockHead.Clone()
bh.Nonce <- nonce
let headerBlock =
ChainedBlock(bh, bh.GetHash(), prevBlock)
if validation headerBlock then nonce
else mine (nonce+processors) bh
let tasks =
[|0u .. processors-1u|] |> Array.map(fun i ->
System.Threading.Tasks.Task.Run(fun t ->
mine i block.Header
)
)
let found =
tasks
|> Array.map (fun t -> t :> System.Threading.Tasks.Task)
|> System.Threading.Tasks.Task.WaitAny
let r = tasks.[found].Result
printfn "%d" r
block.Header.Nonce <- r
|
val ( mine to correct ) : checkType:'a -> chain:'b -> block:'c -> 'd
Full name: Script.( mine to correct )
val checkType : 'a
val chain : 'b
val block : 'c
val validation : ('e -> bool)
val NoWork : 'a
val cb : 'e
val EasyWork : 'a
val CorrectWork : 'a
val processors : uint32
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint32
--------------------
type uint32 = System.UInt32
Full name: Microsoft.FSharp.Core.uint32
val prevBlock : obj
val mine : (uint32 -> 'e -> uint32)
val nonce : uint32
val blockHead : 'e
val bh : 'e
val headerBlock : obj
val tasks : System.Threading.Tasks.Task []
module Array
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
val i : uint32
namespace System
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type Task<'TResult> =
inherit Task
new : function:Func<'TResult> -> Task<'TResult> + 7 overloads
member ContinueWith : continuationAction:Action<Task<'TResult>> -> Task + 9 overloads
member Result : 'TResult with get, set
static member Factory : TaskFactory<'TResult>
Full name: System.Threading.Tasks.Task<_>
--------------------
type Task =
new : action:Action -> Task + 7 overloads
member AsyncState : obj
member ContinueWith : continuationAction:Action<Task> -> Task + 9 overloads
member CreationOptions : TaskCreationOptions
member Dispose : unit -> unit
member Exception : AggregateException
member Id : int
member IsCanceled : bool
member IsCompleted : bool
member IsFaulted : bool
...
Full name: System.Threading.Tasks.Task
--------------------
System.Threading.Tasks.Task(function: System.Func<'TResult>) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
--------------------
System.Threading.Tasks.Task(action: System.Action) : unit
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(action: System.Action, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj) : unit
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
val found : int
val t : System.Threading.Tasks.Task
System.Threading.Tasks.Task.WaitAny([<System.ParamArray>] tasks: System.Threading.Tasks.Task []) : int
System.Threading.Tasks.Task.WaitAny(tasks: System.Threading.Tasks.Task [], millisecondsTimeout: int) : int
System.Threading.Tasks.Task.WaitAny(tasks: System.Threading.Tasks.Task [], cancellationToken: System.Threading.CancellationToken) : int
System.Threading.Tasks.Task.WaitAny(tasks: System.Threading.Tasks.Task [], timeout: System.TimeSpan) : int
System.Threading.Tasks.Task.WaitAny(tasks: System.Threading.Tasks.Task [], millisecondsTimeout: int, cancellationToken: System.Threading.CancellationToken) : int
val r : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information