ligo/src/client/embedded/alpha/client_mining_main.ml

159 lines
6.5 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Cli_entries
open Client_commands
2016-09-08 21:13:10 +04:00
open Client_proto_contracts
let mine_block cctxt block
?force ?max_priority ?(free_mining=false) ?src_sk delegate =
2016-09-08 21:13:10 +04:00
begin
match src_sk with
| None ->
Client_keys.get_key cctxt delegate >>=? fun (_, _, src_sk) ->
2016-09-08 21:13:10 +04:00
return src_sk
| Some sk -> return sk
end >>=? fun src_sk ->
Client_proto_rpcs.Context.level cctxt.rpc_config block >>=? fun level ->
2016-09-08 21:13:10 +04:00
let level = Raw_level.succ level.level in
let seed_nonce = Client_mining_forge.generate_seed_nonce () in
let seed_nonce_hash = Nonce.hash seed_nonce in
Client_mining_forge.forge_block cctxt.rpc_config
2016-09-08 21:13:10 +04:00
~timestamp:(Time.now ())
2017-03-08 21:47:01 +04:00
?force
~seed_nonce_hash ~src_sk block
~priority:(`Auto (delegate, max_priority, free_mining)) () >>=? fun block_hash ->
Client_mining_forge.State.record_block cctxt level block_hash seed_nonce
2016-09-08 21:13:10 +04:00
|> trace_exn (Failure "Error while recording block") >>=? fun () ->
cctxt.message "Injected block %a" Block_hash.pp_short block_hash >>= fun () ->
2016-09-08 21:13:10 +04:00
return ()
let endorse_block cctxt ?force ?max_priority delegate =
Client_keys.get_key cctxt delegate >>=? fun (_src_name, src_pk, src_sk) ->
Client_mining_endorsement.forge_endorsement cctxt
cctxt.config.block ?force ?max_priority ~src_sk src_pk >>=? fun oph ->
cctxt.answer "Operation successfully injected in the node." >>= fun () ->
cctxt.answer "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
2016-09-08 21:13:10 +04:00
return ()
let get_predecessor_cycle cctxt cycle =
2016-09-08 21:13:10 +04:00
match Cycle.pred cycle with
| None ->
if Cycle.(cycle = root) then
cctxt.Client_commands.error "No predecessor for the first cycle"
2016-09-08 21:13:10 +04:00
else
cctxt.error
2016-09-08 21:13:10 +04:00
"Cannot compute the predecessor of cycle %a"
Cycle.pp cycle
| Some cycle -> Lwt.return cycle
let do_reveal cctxt ?force block blocks =
2016-09-08 21:13:10 +04:00
let nonces = List.map snd blocks in
Client_mining_revelation.forge_seed_nonce_revelation cctxt
2016-09-08 21:13:10 +04:00
block ?force nonces >>=? fun () ->
Client_proto_nonces.dels cctxt (List.map fst blocks) >>=? fun () ->
2016-09-08 21:13:10 +04:00
return ()
let reveal_block_nonces cctxt ?force block_hashes =
2016-09-08 21:13:10 +04:00
Lwt_list.filter_map_p
(fun hash ->
Lwt.catch
(fun () ->
Client_mining_blocks.info cctxt.rpc_config (`Hash hash) >>= function
2016-09-08 21:13:10 +04:00
| Ok bi -> Lwt.return (Some bi)
| Error _ ->
Lwt.fail Not_found)
(fun _ ->
cctxt.warning
"Cannot find block %a in the chain. (ignoring)@."
Block_hash.pp_short hash >>= fun () ->
2016-09-08 21:13:10 +04:00
Lwt.return_none))
block_hashes >>= fun block_infos ->
filter_map_s (fun (bi : Client_mining_blocks.block_info) ->
Client_proto_nonces.find cctxt bi.hash >>= function
2016-09-08 21:13:10 +04:00
| None ->
cctxt.warning "Cannot find nonces for block %a (ignoring)@."
Block_hash.pp_short bi.hash >>= fun () ->
2016-09-08 21:13:10 +04:00
return None
| Some nonce ->
return (Some (bi.hash, (bi.level.level, nonce))))
block_infos >>=? fun blocks ->
do_reveal cctxt ?force cctxt.config.block blocks
2016-09-08 21:13:10 +04:00
let reveal_nonces cctxt ?force () =
let block = Client_rpcs.last_mined_block cctxt.config.block in
Client_mining_forge.get_unrevealed_nonces
cctxt ?force block >>=? fun nonces ->
do_reveal cctxt ?force cctxt.config.block nonces
2016-09-08 21:13:10 +04:00
open Client_proto_args
let run_daemon cctxt max_priority endorsement_delay delegates ~endorsement ~mining ~denunciation =
Client_mining_daemon.run cctxt
?max_priority
~delay:endorsement_delay
2016-09-21 18:22:43 +04:00
~min_date:((Time.add (Time.now ()) (Int64.neg 1800L)))
~endorsement ~mining ~denunciation
2016-09-08 21:13:10 +04:00
(List.map snd delegates)
let group =
{ Cli_entries.name = "delegate" ;
title = "Commands related to delegate operations." }
2016-09-08 21:13:10 +04:00
let commands () =
let open Cli_entries in
[
command ~group ~desc: "Launch a daemon that handles delegate operations."
(args5 max_priority_arg endorsement_delay_arg
Daemon.mining_switch Daemon.endorsement_switch Daemon.denunciation_switch)
2016-09-08 21:13:10 +04:00
(prefixes [ "launch" ; "daemon" ]
@@ seq_of_param Client_keys.Public_key_hash.alias_param)
(fun (max_priority, endorsement_delay, mining, endorsement, denunciation) delegates cctxt ->
let (endorsement, mining, denunciation) =
if (not endorsement) && (not mining) && (not denunciation)
then (true, true, true)
else (endorsement, mining, denunciation) in
run_daemon cctxt max_priority endorsement_delay ~endorsement ~mining ~denunciation delegates) ;
command ~group ~desc: "Forge and inject an endorsement operation"
(args2 force_switch max_priority_arg)
2016-09-08 21:13:10 +04:00
(prefixes [ "endorse"; "for" ]
@@ Client_keys.Public_key_hash.alias_param
~name:"miner" ~desc: "name of the delegate owning the endorsement right"
2016-09-08 21:13:10 +04:00
@@ stop)
(fun (force, max_priority) (_, delegate) cctxt ->
endorse_block cctxt
~force ?max_priority delegate) ;
command ~group ~desc: "Forge and inject block using the delegate rights"
(args3 max_priority_arg force_switch free_mining_switch)
2016-09-08 21:13:10 +04:00
(prefixes [ "mine"; "for" ]
@@ Client_keys.Public_key_hash.alias_param
~name:"miner" ~desc: "name of the delegate owning the mining right"
2016-09-08 21:13:10 +04:00
@@ stop)
(fun (max_priority, force, free_mining) (_, delegate) cctxt ->
mine_block cctxt cctxt.config.block
~force ?max_priority ~free_mining delegate) ;
command ~group ~desc: "Forge and inject a seed-nonce revelation operation"
(args1 force_switch)
2016-09-08 21:13:10 +04:00
(prefixes [ "reveal"; "nonce"; "for" ]
@@ seq_of_param Block_hash.param)
(fun force block_hashes cctxt ->
reveal_block_nonces cctxt
~force block_hashes) ;
command ~group ~desc: "Forge and inject redemption operations"
(args1 force_switch)
2016-09-08 21:13:10 +04:00
(prefixes [ "reveal"; "nonces" ]
@@ stop)
(fun force cctxt ->
reveal_nonces cctxt ~force ()) ;
2016-09-08 21:13:10 +04:00
]
let () =
Client_commands.register Client_proto_main.protocol @@
2016-09-08 21:13:10 +04:00
commands ()