From 6212af55f1049e563edff7eafe0d5723cf79527f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 10 Apr 2017 12:50:15 +0200 Subject: [PATCH] Proto/Alpha: use `uint16` for mining priority. --- .../alpha/baker/client_mining_forge.ml | 3 +-- .../embedded/alpha/client_proto_rpcs.mli | 2 +- src/proto/alpha/block_repr.ml | 4 +-- src/proto/alpha/block_repr.mli | 2 +- src/proto/alpha/constants_repr.ml | 10 ++++---- src/proto/alpha/main.ml | 4 +-- src/proto/alpha/mining.ml | 24 +++++++++--------- src/proto/alpha/mining.mli | 17 ++++++------- src/proto/alpha/roll_storage.ml | 4 +-- src/proto/alpha/roll_storage.mli | 2 +- src/proto/alpha/services.ml | 6 ++--- src/proto/alpha/services_registration.ml | 25 +++++++------------ src/proto/alpha/tezos_context.mli | 6 ++--- src/proto/environment/data_encoding.mli | 2 ++ test/proto_alpha/proto_alpha_helpers.ml | 6 ++--- 15 files changed, 55 insertions(+), 62 deletions(-) diff --git a/src/client/embedded/alpha/baker/client_mining_forge.ml b/src/client/embedded/alpha/baker/client_mining_forge.ml index a3c991391..dd0c0d1e4 100644 --- a/src/client/embedded/alpha/baker/client_mining_forge.ml +++ b/src/client/embedded/alpha/baker/client_mining_forge.ml @@ -53,8 +53,7 @@ let inject_block cctxt block let shell = { Store.Block_header.net_id = bi.net ; predecessor = bi.hash ; timestamp ; fitness ; operations } in - let slot = - { Block.level = level.level ; priority = Int32.of_int priority } in + let slot = { Block.level = level.level ; priority } in compute_stamp cctxt block src_sk shell slot seed_nonce_hash >>=? fun proof_of_work_nonce -> Client_proto_rpcs.Helpers.Forge.block cctxt diff --git a/src/client/embedded/alpha/client_proto_rpcs.mli b/src/client/embedded/alpha/client_proto_rpcs.mli index 1e76bf803..a81badd80 100644 --- a/src/client/embedded/alpha/client_proto_rpcs.mli +++ b/src/client/embedded/alpha/client_proto_rpcs.mli @@ -35,7 +35,7 @@ module Constants : sig block -> (Period.t list) tzresult Lwt.t val first_free_mining_slot: Client_rpcs.config -> - block -> int32 tzresult Lwt.t + block -> int tzresult Lwt.t val max_signing_slot: Client_rpcs.config -> block -> int tzresult Lwt.t diff --git a/src/proto/alpha/block_repr.ml b/src/proto/alpha/block_repr.ml index 88acd4f4c..1f67188fb 100644 --- a/src/proto/alpha/block_repr.ml +++ b/src/proto/alpha/block_repr.ml @@ -26,7 +26,7 @@ and proto_header = { and mining_slot = { level: Raw_level_repr.t ; - priority: Int32.t ; + priority: int ; } let mining_slot_encoding = @@ -36,7 +36,7 @@ let mining_slot_encoding = (fun (level, priority) -> { level ; priority }) (obj2 (req "level" Raw_level_repr.encoding) - (req "proprity" int32)) + (req "priority" uint16)) let proto_header_encoding = let open Data_encoding in diff --git a/src/proto/alpha/block_repr.mli b/src/proto/alpha/block_repr.mli index 26da9e36e..7967cc4d2 100644 --- a/src/proto/alpha/block_repr.mli +++ b/src/proto/alpha/block_repr.mli @@ -24,7 +24,7 @@ and proto_header = { and mining_slot = { level: Raw_level_repr.t ; - priority: Int32.t ; + priority: int ; } val mining_slot_encoding: mining_slot Data_encoding.encoding diff --git a/src/proto/alpha/constants_repr.ml b/src/proto/alpha/constants_repr.ml index 3c94e977a..b35f56347 100644 --- a/src/proto/alpha/constants_repr.ml +++ b/src/proto/alpha/constants_repr.ml @@ -38,7 +38,7 @@ type constants = { voting_period_length: int32 ; time_before_reward: Period_repr.t ; slot_durations: Period_repr.t list ; - first_free_mining_slot: int32 ; + first_free_mining_slot: int ; max_signing_slot: int ; instructions_per_transaction: int ; proof_of_work_threshold: int64 ; @@ -58,7 +58,7 @@ let default = { Int64.(mul 365L (mul 24L 3600L)) ; slot_durations = List.map Period_repr.of_seconds_exn [ 60L ] ; - first_free_mining_slot = 16l ; + first_free_mining_slot = 16 ; max_signing_slot = 15 ; instructions_per_transaction = 16 * 1024 ; proof_of_work_threshold = @@ -103,7 +103,7 @@ let constants_encoding = opt Compare_slot_durations.(=) default.slot_durations c.slot_durations and first_free_mining_slot = - opt Compare.Int32.(=) + opt Compare.Int.(=) default.first_free_mining_slot c.first_free_mining_slot and max_signing_slot = opt Compare.Int.(=) @@ -171,8 +171,8 @@ let constants_encoding = (opt "voting_period_length" int32) (opt "time_before_reward" int64) (opt "slot_durations" (list Period_repr.encoding)) - (opt "first_free_mining_slot" int32) - (opt "max_signing_slot" int31) + (opt "first_free_mining_slot" uint16) + (opt "max_signing_slot" uint16) (opt "instructions_per_transaction" int31) (opt "proof_of_work_threshold" int64) (opt "bootstrap_keys" (list Ed25519.Public_key.encoding)) diff --git a/src/proto/alpha/main.ml b/src/proto/alpha/main.ml index 7b3d632c4..e3f7ede80 100644 --- a/src/proto/alpha/main.ml +++ b/src/proto/alpha/main.ml @@ -71,7 +71,7 @@ let apply_operation ({ mode ; ctxt ; op_count } as data) operation = let pred_block, block_prio, miner_contract = match mode with | Construction { pred_block } -> - pred_block, 0l, None + pred_block, 0, None | Application (block, delegate) -> block.shell.predecessor, block.proto.mining_slot.priority, @@ -94,7 +94,7 @@ let finalize_block { mode ; ctxt ; op_count } = match mode with let fitness = Tezos_context.Fitness.current ctxt in let commit_message = Format.asprintf - "lvl %ld, fit %Ld, prio %ld, %d ops" + "lvl %ld, fit %Ld, prio %d, %d ops" level fitness priority op_count in let ctxt = Tezos_context.finalize ~commit_message ctxt in return ctxt diff --git a/src/proto/alpha/mining.ml b/src/proto/alpha/mining.ml index c5e8b0094..32c8e3d8d 100644 --- a/src/proto/alpha/mining.ml +++ b/src/proto/alpha/mining.ml @@ -110,6 +110,7 @@ let () = (fun () -> Cannot_pay_endorsement_bond) let minimal_time c priority pred_timestamp = + let priority = Int32.of_int priority in let rec cumsum_slot_durations acc durations p = if Compare.Int32.(<=) p 0l then ok acc @@ -147,7 +148,7 @@ let check_mining_rights c let pay_mining_bond c { Block.proto = { mining_slot = { priority} } } id = - if Compare.Int32.(priority >= Constants.first_free_mining_slot c) + if Compare.Int.(priority >= Constants.first_free_mining_slot c) then return c else Contract.spend c (Contract.default_contract id) Constants.mining_bond_cost @@ -168,7 +169,7 @@ let check_signing_rights c slot delegate = (Wrong_delegate (owning_delegate, delegate)) let paying_priorities c = - 0l ---> Constants.first_free_mining_slot c + 0 --> Constants.first_free_mining_slot c let bond_and_reward = match Tez.(Constants.mining_bond_cost +? Constants.mining_reward) with @@ -176,25 +177,25 @@ let bond_and_reward = | Error _ -> assert false let base_mining_reward c ~priority = - if Compare.Int32.(priority < Constants.first_free_mining_slot c) + if Compare.Int.(priority < Constants.first_free_mining_slot c) then bond_and_reward else Constants.mining_reward type error += Incorect_priority let endorsement_reward ~block_priority:prio = - if Compare.Int32.(prio >= 0l) + if Compare.Int.(prio >= 0) then Lwt.return - Tez.(Constants.endorsement_reward /? (Int64.(succ (of_int32 prio)))) + Tez.(Constants.endorsement_reward /? (Int64.(succ (of_int prio)))) else fail Incorect_priority let mining_priorities c level = let rec f priority = Roll.mining_rights_owner c level ~priority >>=? fun delegate -> - return (LCons (delegate, (fun () -> f (Int32.succ priority)))) + return (LCons (delegate, (fun () -> f (succ priority)))) in - f 0l + f 0 let endorsement_priorities c level = let rec f slot = @@ -205,7 +206,7 @@ let endorsement_priorities c level = let select_delegate delegate delegate_list max_priority = let rec loop acc l n = - if Compare.Int32.(n >= max_priority) + if Compare.Int.(n >= max_priority) then return (List.rev acc) else let LCons (pkh, t) = l in @@ -214,9 +215,9 @@ let select_delegate delegate delegate_list max_priority = then n :: acc else acc in t () >>=? fun t -> - loop acc t (Int32.succ n) + loop acc t (succ n) in - loop [] delegate_list 0l + loop [] delegate_list 0 let first_mining_priorities ctxt @@ -227,8 +228,7 @@ let first_mining_priorities let first_endorsement_slots ctxt - ?(max_priority = - Int32.of_int (Constants.max_signing_slot ctxt)) + ?(max_priority = Constants.max_signing_slot ctxt) delegate level = endorsement_priorities ctxt level >>=? fun delegate_list -> select_delegate delegate delegate_list max_priority diff --git a/src/proto/alpha/mining.mli b/src/proto/alpha/mining.mli index 3d2fa2141..efa312019 100644 --- a/src/proto/alpha/mining.mli +++ b/src/proto/alpha/mining.mli @@ -19,10 +19,9 @@ type error += Wrong_delegate of public_key_hash * public_key_hash (* `Permanent type error += Cannot_pay_mining_bond (* `Permanent *) type error += Cannot_pay_endorsement_bond (* `Permanent *) -val paying_priorities: context -> int32 list +val paying_priorities: context -> int list -val minimal_time: - context -> int32 -> Time.t -> Time.t tzresult Lwt.t +val minimal_time: context -> int -> Time.t -> Time.t tzresult Lwt.t (** [minimal_time ctxt priority pred_block_time] returns the minimal time, given the predecessor block timestamp [pred_block_time], after which a miner with priority [priority] is allowed to @@ -56,9 +55,9 @@ val check_signing_rights: (** If this priority should have payed the bond it is the base mining reward and the bond, or just the base reward otherwise *) -val base_mining_reward: context -> priority:int32 -> Tez.t +val base_mining_reward: context -> priority:int -> Tez.t -val endorsement_reward: block_priority:int32 -> Tez.t tzresult Lwt.t +val endorsement_reward: block_priority:int -> Tez.t tzresult Lwt.t val mining_priorities: context -> Level.t -> public_key_hash lazy_list @@ -70,10 +69,10 @@ val endorsement_priorities: val first_mining_priorities: context -> - ?max_priority:int32 -> + ?max_priority:int -> public_key_hash -> Level.t -> - int32 list tzresult Lwt.t + int list tzresult Lwt.t (** [first_mining_priorities ctxt ?max_priority contract_hash level] is a list of priorities of max [?max_priority] elements, where the delegate of [contract_hash] is allowed to mine for [level]. If @@ -82,9 +81,9 @@ val first_mining_priorities: val first_endorsement_slots: context -> - ?max_priority:int32 -> + ?max_priority:int -> public_key_hash -> - Level.t -> int32 list tzresult Lwt.t + Level.t -> int list tzresult Lwt.t val check_signature: context -> Block.header -> public_key_hash -> unit tzresult Lwt.t diff --git a/src/proto/alpha/roll_storage.ml b/src/proto/alpha/roll_storage.ml index 2a568b586..171aa5840 100644 --- a/src/proto/alpha/roll_storage.ml +++ b/src/proto/alpha/roll_storage.ml @@ -73,7 +73,7 @@ module Random = struct let cycle = level.Level_repr.cycle in Seed_storage.for_cycle c cycle >>=? fun random_seed -> let rd = level_random random_seed kind level in - let sequence = Seed_repr.sequence rd offset in + let sequence = Seed_repr.sequence rd (Int32.of_int offset) in Storage.Roll.Last_for_cycle.get c cycle >>=? fun bound -> let roll, _ = Roll_repr.random sequence bound in Storage.Roll.Owner_for_cycle.get c (cycle, roll) @@ -84,7 +84,7 @@ let mining_rights_owner c level ~priority = Random.owner c "mining" level priority let endorsement_rights_owner c level ~slot = - Random.owner c "endorsement" level (Int32.of_int slot) + Random.owner c "endorsement" level slot module Contract = struct diff --git a/src/proto/alpha/roll_storage.mli b/src/proto/alpha/roll_storage.mli index 066d55117..9fd798b73 100644 --- a/src/proto/alpha/roll_storage.mli +++ b/src/proto/alpha/roll_storage.mli @@ -35,7 +35,7 @@ val clear_cycle : Storage.t -> Cycle_repr.t -> Storage.t tzresult Lwt.t val mining_rights_owner : - Storage.t -> Level_repr.t -> priority:int32 -> + Storage.t -> Level_repr.t -> priority:int -> Ed25519.Public_key_hash.t tzresult Lwt.t val endorsement_rights_owner : diff --git a/src/proto/alpha/services.ml b/src/proto/alpha/services.ml index 0ed8a43a2..71ee0c4d1 100644 --- a/src/proto/alpha/services.ml +++ b/src/proto/alpha/services.ml @@ -73,7 +73,7 @@ module Constants = struct ~description: "First free mining slot" ~input: empty ~output: (wrap_tzerror @@ - describe ~title: "first free mining slot" int32) + describe ~title: "first free mining slot" uint16) RPC.Path.(custom_root / "constants" / "first_free_mining_slot") let max_signing_slot custom_root = @@ -81,7 +81,7 @@ module Constants = struct ~description: "Max signing slot" ~input: empty ~output: (wrap_tzerror @@ - describe ~title: "max signing slot" int31) + describe ~title: "max signing slot" uint16) RPC.Path.(custom_root / "constants" / "max_signing_slot") let instructions_per_transaction custom_root = @@ -563,7 +563,7 @@ module Helpers = struct (req "fitness" Fitness.encoding) (req "operations" Operation_list_list_hash.encoding) (req "level" Raw_level.encoding) - (req "priority" int31) + (req "priority" uint16) (req "nonce_hash" Nonce_hash.encoding) (req "proof_of_work_nonce" (Fixed.bytes Tezos_context.Constants.proof_of_work_nonce_size))) diff --git a/src/proto/alpha/services_registration.ml b/src/proto/alpha/services_registration.ml index 2d6bff8eb..ced5e6d8b 100644 --- a/src/proto/alpha/services_registration.ml +++ b/src/proto/alpha/services_registration.ml @@ -174,7 +174,7 @@ let () = (*-- Helpers -----------------------------------------------------------------*) let minimal_timestamp ctxt prio = - let prio = match prio with None -> 0l | Some p -> Int32.of_int p in + let prio = match prio with None -> 0 | Some p -> p in Mining.minimal_time ctxt prio let () = register1 @@ -196,7 +196,7 @@ let () = Tezos_context.Level.current ctxt >>=? fun level -> Mining.mining_priorities ctxt level >>=? fun (Misc.LCons (miner_pkh, _)) -> let miner_contract = Contract.default_contract miner_pkh in - let block_prio = 0l in + let block_prio = 0 in Apply.apply_operation ctxt (Some miner_contract) pred_block block_prio operation >>=? function @@ -281,11 +281,11 @@ let () = register2 Services.Helpers.levels levels let default_max_mining_priority ctxt arg = let default = Constants.first_free_mining_slot ctxt in match arg with - | None -> Int32.mul 2l default - | Some m -> Int32.of_int m + | None -> 2 * default + | Some m -> m let mining_rights ctxt level max = - let max = Int32.to_int (default_max_mining_priority ctxt max) in + let max = default_max_mining_priority ctxt max in Mining.mining_priorities ctxt level >>=? fun contract_list -> let rec loop l n = match n with @@ -309,8 +309,7 @@ let () = List.mapi (fun prio c -> let timestamp = Timestamp.current ctxt in - Mining.minimal_time - ctxt (Int32.of_int prio) timestamp >>= function + Mining.minimal_time ctxt prio timestamp >>= function | Error _ -> Lwt.return None | Ok minimal_timestamp -> Lwt.return (Some (c, minimal_timestamp))) slots @@ -348,7 +347,7 @@ let mining_rights_for_delegate (fun priority -> let timestamp = Timestamp.current ctxt in Mining.minimal_time ctxt priority timestamp >>=? fun time -> - return (raw_level, Int32.to_int priority, time)) + return (raw_level, priority, time)) priorities >>=? fun priorities -> return (priorities @ t) in @@ -391,10 +390,8 @@ let () = let endorsement_rights_for_delegate ctxt contract (max_priority, min_level, max_level) = - let max_priority = - Int32.of_int @@ - default_max_endorsement_priority ctxt max_priority in Level.current ctxt >>=? fun current_level -> + let max_priority = default_max_endorsement_priority ctxt max_priority in let max_level = match max_level with | None -> @@ -412,10 +409,7 @@ let endorsement_rights_for_delegate Mining.first_endorsement_slots ctxt ~max_priority contract level >>=? fun slots -> let raw_level = level.level in - let slots = - List.rev_map - (fun slot -> (raw_level, Int32.to_int slot)) - slots in + let slots = List.rev_map (fun slot -> (raw_level, slot)) slots in return (List.rev_append slots t) in loop min_level @@ -442,7 +436,6 @@ let () = register1 Services.Helpers.Forge.operations forge_operations let forge_block _ctxt (net_id, predecessor, timestamp, fitness, operations, raw_level, priority, seed_nonce_hash, proof_of_work_nonce) : MBytes.t tzresult Lwt.t = - let priority = Int32.of_int priority in let mining_slot = { Block.level = raw_level ; priority } in return (Block.forge_header { net_id ; predecessor ; timestamp ; fitness ; operations } diff --git a/src/proto/alpha/tezos_context.mli b/src/proto/alpha/tezos_context.mli index 3e8f33373..9ec73165b 100644 --- a/src/proto/alpha/tezos_context.mli +++ b/src/proto/alpha/tezos_context.mli @@ -172,7 +172,7 @@ module Constants : sig val voting_period_length: context -> int32 val time_before_reward: context -> Period.t val slot_durations: context -> Period.t list - val first_free_mining_slot: context -> int32 + val first_free_mining_slot: context -> int val max_signing_slot: context -> int val instructions_per_transaction: context -> int val proof_of_work_threshold: context -> int64 @@ -530,7 +530,7 @@ module Block : sig and mining_slot = { level: Raw_level.t ; - priority: Int32.t ; + priority: int ; } val mining_slot_encoding: mining_slot Data_encoding.encoding @@ -556,7 +556,7 @@ module Roll : sig val clear_cycle: context -> Cycle.t -> context tzresult Lwt.t val mining_rights_owner: - context -> Level.t -> priority:int32 -> public_key_hash tzresult Lwt.t + context -> Level.t -> priority:int -> public_key_hash tzresult Lwt.t val endorsement_rights_owner: context -> Level.t -> slot:int -> public_key_hash tzresult Lwt.t diff --git a/src/proto/environment/data_encoding.mli b/src/proto/environment/data_encoding.mli index d13ff0e01..716f74816 100644 --- a/src/proto/environment/data_encoding.mli +++ b/src/proto/environment/data_encoding.mli @@ -28,7 +28,9 @@ val empty : unit encoding val unit : unit encoding val constant : string -> unit encoding val int8 : int encoding +val uint8 : int encoding val int16 : int encoding +val uint16 : int encoding val int31 : int encoding val int32 : int32 encoding val int64 : int64 encoding diff --git a/test/proto_alpha/proto_alpha_helpers.ml b/test/proto_alpha/proto_alpha_helpers.ml index 3200188fc..dbe78360d 100644 --- a/test/proto_alpha/proto_alpha_helpers.ml +++ b/test/proto_alpha/proto_alpha_helpers.ml @@ -399,7 +399,7 @@ module Mining = struct let shell = { Store.Block_header.net_id = bi.net ; predecessor = bi.hash ; timestamp ; fitness ; operations } in - let slot = { Block.level = level.level ; priority = Int32.of_int priority } in + let slot = { Block.level = level.level ; priority } in mine_stamp block src_sk shell slot seed_nonce_hash >>=? fun proof_of_work_nonce -> Client_proto_rpcs.Helpers.Forge.block rpc_config @@ -410,7 +410,7 @@ module Mining = struct ~fitness ~operations ~level:level.level - ~priority:priority + ~priority ~seed_nonce_hash ~proof_of_work_nonce () >>=? fun unsigned_header -> @@ -453,7 +453,7 @@ module Mining = struct let endorsement_reward contract block = Client_mining_blocks.info rpc_config block >>=? fun bi -> get_first_priority bi.level.level contract block >>=? fun prio -> - Mining.endorsement_reward ~block_priority:(Int32.of_int prio) >|= + Mining.endorsement_reward ~block_priority:prio >|= Register_client_embedded_proto_alpha.wrap_error >>|? Tez.to_cents