From bb82702fd620e3ac6818af25a0634c9a540a2993 Mon Sep 17 00:00:00 2001 From: Vincent Botbol Date: Wed, 8 Aug 2018 13:35:12 +0200 Subject: [PATCH] Alpha/Baker: rename threshold into fee_threshold and include it in the baker's state --- .../lib_delegate/client_baking_forge.ml | 69 ++++++++++++------- .../lib_delegate/client_baking_forge.mli | 8 +-- .../lib_delegate/client_baking_lib.ml | 4 +- .../lib_delegate/client_baking_lib.mli | 2 +- src/proto_alpha/lib_delegate/client_daemon.ml | 4 +- .../lib_delegate/client_daemon.mli | 2 +- .../lib_delegate/delegate_commands.ml | 8 +-- 7 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/proto_alpha/lib_delegate/client_baking_forge.ml b/src/proto_alpha/lib_delegate/client_baking_forge.ml index 644a86b20..cbf16f07e 100644 --- a/src/proto_alpha/lib_delegate/client_baking_forge.ml +++ b/src/proto_alpha/lib_delegate/client_baking_forge.ml @@ -48,18 +48,22 @@ type state = { (* lazy-initialisation with retry-on-error *) constants: Constants.t tzlazy ; + (* Minimum operation fee required to include in a block *) + fee_threshold : Tez.t ; + (* truly mutable *) mutable best: Client_baking_blocks.block_info ; mutable future_slots: (Time.t * (Client_baking_blocks.block_info * int * public_key_hash)) list ; } -let create_state genesis context_path index delegates constants best = +let create_state genesis context_path index delegates constants ?(fee_threshold = Tez.zero) best = { genesis ; context_path ; index ; delegates ; constants ; + fee_threshold ; best ; future_slots = [] ; } @@ -163,7 +167,7 @@ let get_manager_operation_gas_and_fee op = let sort_manager_operations ~max_size ~hard_gas_limit_per_block - ?(threshold = Tez.zero) + ~fee_threshold (operations : Proto_alpha.operation list) = let compute_weight op (fee, gas) = @@ -178,7 +182,7 @@ let sort_manager_operations filter_map_s (fun op -> get_manager_operation_gas_and_fee op >>=? fun (fee, gas) -> - if Tez.(<) fee threshold then + if Tez.(<) fee fee_threshold then return_none else return (Some (op, (compute_weight op (fee, gas)))) @@ -230,9 +234,9 @@ let trim_manager_operations ~max_size ~hard_gas_limit_per_block manager_operatio (* Hypothesis : we suppose that the received manager operations have a valid gas_limit *) let classify_operations (cctxt : #Proto_alpha.full) - ?threshold ~block ~hard_gas_limit_per_block + ~fee_threshold (ops: Proto_alpha.operation list) = Alpha_block_services.live_blocks cctxt ~chain:`Main ~block () >>=? fun live_blocks -> @@ -255,7 +259,7 @@ let classify_operations let manager_operations = t.(managers_index) in let { Alpha_environment.Updater.max_size } = List.nth Proto_alpha.Main.validation_passes managers_index in - sort_manager_operations ~max_size ~hard_gas_limit_per_block ?threshold manager_operations + sort_manager_operations ~max_size ~hard_gas_limit_per_block ~fee_threshold manager_operations >>=? fun ordered_operations -> (* Greedy heuristic *) trim_manager_operations ~max_size ~hard_gas_limit_per_block (List.map fst ordered_operations) @@ -369,9 +373,11 @@ let error_of_op (result: error Preapply_result.t) op = with Not_found -> None let forge_block cctxt ?(chain = `Main) block - ?threshold ?force - ?operations ?(best_effort = operations = None) ?(sort = best_effort) + ?operations + ?(best_effort = operations = None) + ?(sort = best_effort) + ?(fee_threshold = Tez.zero) ?timestamp ~priority ?seed_nonce_hash ~src_sk () = @@ -385,7 +391,7 @@ let forge_block cctxt ?(chain = `Main) block let protocol_data = forge_faked_protocol_data ~priority ~seed_nonce_hash in Alpha_services.Constants.all cctxt (`Main, block) >>=? fun Constants.{ parametric = { hard_gas_limit_per_block ; endorsers_per_block } } -> - classify_operations cctxt ~hard_gas_limit_per_block ~block:block ?threshold operations_arg >>=? fun operations -> + classify_operations cctxt ~hard_gas_limit_per_block ~block:block ~fee_threshold operations_arg >>=? fun operations -> (* Ensure that we retain operations up to the quota *) let quota : Alpha_environment.Updater.quota list = Main.validation_passes in let endorsements = List.sub @@ -757,10 +763,30 @@ let shell_prevalidation return (Some (bi, priority, shell_header, raw_ops, delegate, seed_nonce_hash)) +(** Retrieve the operations present in the node's mempool and classify + them in 5 lists indexed as : + - 0 -> Endorsements + - 1 -> Votes and proposals + - 2 -> Anonymous operations + - 3 -> High-priority manager operations + - 4 -> Low-priority manager operations +*) +let fetch_operations + (cctxt : #Proto_alpha.full) + state + ~chain + ~block + = + (* Retrieve pending operations *) + Alpha_block_services.Mempool.pending_operations cctxt ~chain () >>=? fun mpool -> + let operations = ops_of_mempool mpool in + tzforce state.constants >>=? fun Constants.{ parametric = { hard_gas_limit_per_block } } -> + classify_operations cctxt + ~hard_gas_limit_per_block ~fee_threshold:state.fee_threshold ~block operations + let bake_slot cctxt state - ?threshold seed_nonce_hash ((timestamp, (bi, priority, delegate)) as slot) = @@ -768,6 +794,11 @@ let bake_slot let block = `Hash (bi.hash, 0) in Alpha_services.Helpers.current_level cctxt ~offset:1l (chain, block) >>=? fun next_level -> + let seed_nonce_hash = + if next_level.Level.expected_commitment then + Some seed_nonce_hash + else + None in let timestamp = if Block_hash.equal bi.Client_baking_blocks.hash state.genesis then Time.now () @@ -781,16 +812,7 @@ let bake_slot -% s bake_priorty_tag priority -% s Client_keys.Logging.tag name -% a timestamp_tag timestamp) >>= fun () -> - (* Retrieve pending operations *) - Alpha_block_services.Mempool.pending_operations cctxt ~chain () >>=? fun mpool -> - let operations = ops_of_mempool mpool in - let seed_nonce_hash = - if next_level.expected_commitment then - Some seed_nonce_hash - else - None in - tzforce state.constants >>=? fun Constants.{ parametric = { hard_gas_limit_per_block } } -> - classify_operations cctxt ?threshold ~hard_gas_limit_per_block ~block operations >>=? fun operations -> + fetch_operations cctxt state ~chain ~block >>=? fun operations -> let next_version = match Tezos_base.Block_header.get_forced_protocol_upgrade ~level:(Raw_level.to_int32 next_level.Level.level) with | None -> bi.next_protocol @@ -852,7 +874,6 @@ let pp_operation_list_list = (* [bake] create a single block when woken up to do so. All the necessary information (e.g., slot) is available in the [state]. *) let bake - ?threshold () (cctxt : #Proto_alpha.full) state @@ -868,7 +889,7 @@ let bake (* baking for each slot *) filter_map_s - (bake_slot cctxt ?threshold state seed_nonce_hash) + (bake_slot cctxt state seed_nonce_hash) slots >>=? fun candidates -> (* FIXME: pick one block per-delegate *) @@ -925,7 +946,7 @@ let bake the [delegates] *) let create (cctxt : #Proto_alpha.full) - ?threshold + ?fee_threshold ?max_priority ~(context_path: string) (delegates: public_key_hash list) @@ -936,7 +957,7 @@ let create let constants = tzlazy (fun () -> Alpha_services.Constants.all cctxt (`Main, `Head 0)) in Client_baking_simulator.load_context ~context_path >>= fun index -> - let state = create_state genesis_hash context_path index delegates constants bi in + let state = create_state genesis_hash context_path index delegates constants ?fee_threshold bi in return state in @@ -947,5 +968,5 @@ let create ~state_maker ~pre_loop:(insert_block ?max_priority ()) ~compute_timeout - ~timeout_k:(bake ?threshold ()) + ~timeout_k:(bake ()) ~event_k:(insert_block ?max_priority ()) diff --git a/src/proto_alpha/lib_delegate/client_baking_forge.mli b/src/proto_alpha/lib_delegate/client_baking_forge.mli index b9a1a8aed..c4906df94 100644 --- a/src/proto_alpha/lib_delegate/client_baking_forge.mli +++ b/src/proto_alpha/lib_delegate/client_baking_forge.mli @@ -55,18 +55,18 @@ val forge_block: #Proto_alpha.full -> ?chain:Chain_services.chain -> Block_services.block -> - ?threshold:Tez.t -> ?force:bool -> ?operations: Operation.packed list -> ?best_effort:bool -> ?sort:bool -> + ?fee_threshold:Tez.t -> ?timestamp:Time.t -> priority:[`Set of int | `Auto of (public_key_hash * int option)] -> ?seed_nonce_hash:Nonce_hash.t -> src_sk:Client_keys.sk_uri -> unit -> Block_hash.t tzresult Lwt.t -(** [forge_block cctxt parent_blk ?threshold ?force ?operations ?best_effort +(** [forge_block cctxt parent_blk ?fee_threshold ?force ?operations ?best_effort ?sort ?timestamp ?max_priority ?priority ~seed_nonce ~src_sk pk_hash] injects a block in the node. In addition of inject_block, it will: @@ -83,7 +83,7 @@ val forge_block: computed baking priority, it will be used. Otherwise, it will be set at the best baking priority. - * Threshold: If [?threshold] is given, operations with fees lower than it + * Fee Threshold: If [?fee_threshold] is given, operations with fees lower than it are not added to the block. *) @@ -103,7 +103,7 @@ end val create: #Proto_alpha.full -> - ?threshold:Tez.t -> + ?fee_threshold:Tez.t -> ?max_priority: int -> context_path: string -> public_key_hash list -> diff --git a/src/proto_alpha/lib_delegate/client_baking_lib.ml b/src/proto_alpha/lib_delegate/client_baking_lib.ml index 7c58ed58f..bfecdf65d 100644 --- a/src/proto_alpha/lib_delegate/client_baking_lib.ml +++ b/src/proto_alpha/lib_delegate/client_baking_lib.ml @@ -28,7 +28,7 @@ open Alpha_context let bake_block (cctxt : #Proto_alpha.full) ?(chain = `Main) block - ?threshold + ?fee_threshold ?force ?max_priority ?(minimal_timestamp=false) ?src_sk ?src_pk delegate = begin @@ -56,7 +56,7 @@ let bake_block (cctxt : #Proto_alpha.full) None, None in Client_baking_forge.forge_block cctxt ?timestamp:(if minimal_timestamp then None else Some (Time.now ())) - ?threshold + ?fee_threshold ?force ?seed_nonce_hash ~src_sk block ~priority:(`Auto (delegate, max_priority)) () >>=? fun block_hash -> diff --git a/src/proto_alpha/lib_delegate/client_baking_lib.mli b/src/proto_alpha/lib_delegate/client_baking_lib.mli index c3258b012..d338f27c2 100644 --- a/src/proto_alpha/lib_delegate/client_baking_lib.mli +++ b/src/proto_alpha/lib_delegate/client_baking_lib.mli @@ -31,7 +31,7 @@ val bake_block: #Proto_alpha.full -> ?chain:Chain_services.chain -> Block_services.block -> - ?threshold:Tez.t -> + ?fee_threshold:Tez.t -> ?force:bool -> ?max_priority: int -> ?minimal_timestamp: bool -> diff --git a/src/proto_alpha/lib_delegate/client_daemon.ml b/src/proto_alpha/lib_delegate/client_daemon.ml index a3063b1a0..f64f77975 100644 --- a/src/proto_alpha/lib_delegate/client_daemon.ml +++ b/src/proto_alpha/lib_delegate/client_daemon.ml @@ -37,12 +37,12 @@ end module Baker = struct - let run (cctxt : #Proto_alpha.full) ?threshold ?max_priority ?min_date ~context_path delegates = + let run (cctxt : #Proto_alpha.full) ?fee_threshold ?max_priority ?min_date ~context_path delegates = Client_baking_blocks.monitor_heads ~next_protocols:(Some [Proto_alpha.hash]) cctxt `Main >>=? fun block_stream -> Client_baking_forge.create cctxt - ?threshold ?max_priority ~context_path delegates block_stream >>=? fun () -> + ?fee_threshold ?max_priority ~context_path delegates block_stream >>=? fun () -> ignore min_date; return_unit diff --git a/src/proto_alpha/lib_delegate/client_daemon.mli b/src/proto_alpha/lib_delegate/client_daemon.mli index 8df8b415f..86e427797 100644 --- a/src/proto_alpha/lib_delegate/client_daemon.mli +++ b/src/proto_alpha/lib_delegate/client_daemon.mli @@ -37,7 +37,7 @@ end module Baker : sig val run: #Proto_alpha.full -> - ?threshold: Tez.tez -> + ?fee_threshold: Tez.tez -> ?max_priority: int -> ?min_date: Time.t -> context_path: string -> diff --git a/src/proto_alpha/lib_delegate/delegate_commands.ml b/src/proto_alpha/lib_delegate/delegate_commands.ml index 2cd95c965..257b941a8 100644 --- a/src/proto_alpha/lib_delegate/delegate_commands.ml +++ b/src/proto_alpha/lib_delegate/delegate_commands.ml @@ -46,9 +46,9 @@ let delegate_commands () = @@ Client_keys.Public_key_hash.source_param ~name:"baker" ~desc: "name of the delegate owning the baking right" @@ stop) - (fun (max_priority, threshold, force, minimal_timestamp) delegate cctxt -> + (fun (max_priority, fee_threshold, force, minimal_timestamp) delegate cctxt -> bake_block cctxt cctxt#block - ?threshold ~force ?max_priority ~minimal_timestamp delegate) ; + ?fee_threshold ~force ?max_priority ~minimal_timestamp delegate) ; command ~group ~desc: "Forge and inject a seed-nonce revelation operation." no_options (prefixes [ "reveal"; "nonce"; "for" ] @@ -85,11 +85,11 @@ let baker_commands () = ~desc:"Path to the node data directory (e.g. $HOME/.tezos-node)" directory_parameter @@ seq_of_param Client_keys.Public_key_hash.alias_param) - (fun (max_priority, threshold) node_path delegates cctxt -> + (fun (max_priority, fee_threshold) node_path delegates cctxt -> Tezos_signer_backends.Encrypted.decrypt_list cctxt (List.map fst delegates) >>=? fun () -> Client_daemon.Baker.run cctxt - ?threshold + ?fee_threshold ?max_priority ~min_date:((Time.add (Time.now ()) (Int64.neg 1800L))) ~context_path:(Filename.concat node_path "context")