Alpha/Baker: outsource mining
The (small) proof-of-work operation is handled separately.
This commit is contained in:
parent
bde05da36a
commit
14ee040e97
@ -45,9 +45,6 @@ let create_state genesis index delegates constants best =
|
|||||||
future_slots = [] ;
|
future_slots = [] ;
|
||||||
}
|
}
|
||||||
|
|
||||||
let generate_proof_of_work_nonce () =
|
|
||||||
Rand.generate Constants.proof_of_work_nonce_size
|
|
||||||
|
|
||||||
let generate_seed_nonce () =
|
let generate_seed_nonce () =
|
||||||
match Nonce.of_bytes @@
|
match Nonce.of_bytes @@
|
||||||
Rand.generate Constants.nonce_length with
|
Rand.generate Constants.nonce_length with
|
||||||
@ -57,32 +54,23 @@ let generate_seed_nonce () =
|
|||||||
let forge_block_header
|
let forge_block_header
|
||||||
(cctxt : #Proto_alpha.full)
|
(cctxt : #Proto_alpha.full)
|
||||||
?(chain = `Main) block delegate_sk shell priority seed_nonce_hash =
|
?(chain = `Main) block delegate_sk shell priority seed_nonce_hash =
|
||||||
Alpha_services.Constants.all cctxt
|
Client_baking_pow.mine
|
||||||
(chain, block) >>=? fun { parametric = {
|
cctxt chain block shell
|
||||||
proof_of_work_threshold = stamp_threshold ;
|
(fun proof_of_work_nonce ->
|
||||||
} } ->
|
{ Block_header.priority ;
|
||||||
let rec loop () =
|
seed_nonce_hash ;
|
||||||
let proof_of_work_nonce = generate_proof_of_work_nonce () in
|
proof_of_work_nonce ;
|
||||||
let contents =
|
}) >>=? fun contents ->
|
||||||
{ Block_header.priority ; seed_nonce_hash ; proof_of_work_nonce } in
|
|
||||||
if Baking.check_header_proof_of_work_stamp shell contents stamp_threshold then
|
|
||||||
let unsigned_header =
|
let unsigned_header =
|
||||||
Data_encoding.Binary.to_bytes_exn
|
Data_encoding.Binary.to_bytes_exn
|
||||||
Alpha_context.Block_header.unsigned_encoding
|
Alpha_context.Block_header.unsigned_encoding
|
||||||
(shell, contents) in
|
(shell, contents) in
|
||||||
Client_keys.append cctxt delegate_sk ~watermark:Block_header unsigned_header
|
Client_keys.append cctxt delegate_sk ~watermark:Block_header unsigned_header
|
||||||
else
|
|
||||||
loop () in
|
|
||||||
loop ()
|
|
||||||
|
|
||||||
let empty_proof_of_work_nonce =
|
|
||||||
MBytes.of_string
|
|
||||||
(String.make Constants_repr.proof_of_work_nonce_size '\000')
|
|
||||||
|
|
||||||
let forge_faked_protocol_data ~priority ~seed_nonce_hash =
|
let forge_faked_protocol_data ~priority ~seed_nonce_hash =
|
||||||
Alpha_context.Block_header.{
|
Alpha_context.Block_header.{
|
||||||
contents = { priority ; seed_nonce_hash ;
|
contents = { priority ; seed_nonce_hash ;
|
||||||
proof_of_work_nonce = empty_proof_of_work_nonce } ;
|
proof_of_work_nonce = Client_baking_pow.empty_proof_of_work_nonce } ;
|
||||||
signature = Signature.zero
|
signature = Signature.zero
|
||||||
}
|
}
|
||||||
|
|
||||||
|
29
src/proto_alpha/lib_delegate/client_baking_pow.ml
Normal file
29
src/proto_alpha/lib_delegate/client_baking_pow.ml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
(**************************************************************************)
|
||||||
|
(* *)
|
||||||
|
(* Copyright (c) 2014 - 2018. *)
|
||||||
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
||||||
|
(* *)
|
||||||
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
||||||
|
(* *)
|
||||||
|
(**************************************************************************)
|
||||||
|
|
||||||
|
open Proto_alpha
|
||||||
|
|
||||||
|
let generate_proof_of_work_nonce () =
|
||||||
|
Rand.generate Alpha_context.Constants.proof_of_work_nonce_size
|
||||||
|
|
||||||
|
let empty_proof_of_work_nonce =
|
||||||
|
MBytes.of_string
|
||||||
|
(String.make Constants_repr.proof_of_work_nonce_size '\000')
|
||||||
|
|
||||||
|
let mine cctxt chain block shell builder =
|
||||||
|
Alpha_services.Constants.all cctxt (chain, block) >>=? fun constants ->
|
||||||
|
let threshold = constants.parametric.proof_of_work_threshold in
|
||||||
|
let rec loop () =
|
||||||
|
let block = builder (generate_proof_of_work_nonce ()) in
|
||||||
|
if Baking.check_header_proof_of_work_stamp shell block threshold then
|
||||||
|
return block
|
||||||
|
else
|
||||||
|
loop ()
|
||||||
|
in
|
||||||
|
loop ()
|
28
src/proto_alpha/lib_delegate/client_baking_pow.mli
Normal file
28
src/proto_alpha/lib_delegate/client_baking_pow.mli
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
(**************************************************************************)
|
||||||
|
(* *)
|
||||||
|
(* Copyright (c) 2014 - 2018. *)
|
||||||
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
||||||
|
(* *)
|
||||||
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
||||||
|
(* *)
|
||||||
|
(**************************************************************************)
|
||||||
|
|
||||||
|
|
||||||
|
(** A null proof-of-work nonce. This should only be used to non-sensical blocks
|
||||||
|
of the correct size and shape. *)
|
||||||
|
val empty_proof_of_work_nonce: Cstruct.buffer
|
||||||
|
|
||||||
|
(** [mine cctxt chain block header builder] returns a block with a valid
|
||||||
|
proof-of-work nonce. The function [builder], provided by the caller, is used
|
||||||
|
to make the block. All the internal logic of generating nonces and checking
|
||||||
|
for the proof-of-work threshold is handled by [mine]. *)
|
||||||
|
val mine:
|
||||||
|
#Proto_alpha.full ->
|
||||||
|
Shell_services.chain ->
|
||||||
|
Block_services.block ->
|
||||||
|
Block_header.shell_header ->
|
||||||
|
(Cstruct.buffer -> Proto_alpha.Alpha_context.Block_header.contents) ->
|
||||||
|
Proto_alpha.Alpha_context.Block_header.contents tzresult Lwt.t
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user