From 5449cbbfe6ebc661e4aeb65688d471d7d61ab20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sun, 2 Apr 2017 16:07:19 +0200 Subject: [PATCH] Proto/Alpha: use record for mining slot. --- src/attacker/attacker_minimal.ml | 2 +- .../alpha/baker/client_mining_forge.ml | 3 +- src/proto/alpha/apply.ml | 6 ++-- src/proto/alpha/block_repr.ml | 28 +++++++++++++------ src/proto/alpha/block_repr.mli | 5 +++- src/proto/alpha/mining.ml | 4 +-- src/proto/alpha/services_registration.ml | 2 +- src/proto/alpha/tezos_context.mli | 5 +++- 8 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/attacker/attacker_minimal.ml b/src/attacker/attacker_minimal.ml index 0b22db84f..3f543f405 100644 --- a/src/attacker/attacker_minimal.ml +++ b/src/attacker/attacker_minimal.ml @@ -50,7 +50,7 @@ let block_forged ?prev ops = | Ok nonce -> nonce in Block_repr.forge_header (block ops) Block_repr.{ - mining_slot = Raw_level_repr.of_int32_exn 1l, 0l ; + mining_slot = {level = Raw_level_repr.of_int32_exn 1l ; priority = 0l } ; seed_nonce_hash = Proto.Nonce_storage.hash (generate_seed_nonce ()); proof_of_work_nonce = generate_proof_of_work_nonce () ; } diff --git a/src/client/embedded/alpha/baker/client_mining_forge.ml b/src/client/embedded/alpha/baker/client_mining_forge.ml index a276f787d..782409ae3 100644 --- a/src/client/embedded/alpha/baker/client_mining_forge.ml +++ b/src/client/embedded/alpha/baker/client_mining_forge.ml @@ -51,7 +51,8 @@ let inject_block cctxt block let shell = { Store.Block_header.net_id = bi.net ; predecessor = bi.hash ; timestamp ; fitness ; operations } in - let slot = level.level, Int32.of_int priority in + let slot = + { Block.level = level.level ; priority = Int32.of_int 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/proto/alpha/apply.ml b/src/proto/alpha/apply.ml index c8e3da27b..bd66499d0 100644 --- a/src/proto/alpha/apply.ml +++ b/src/proto/alpha/apply.ml @@ -260,7 +260,7 @@ let apply_main ctxt accept_failing_script block pred_timestamp operations = Mining.pay_mining_bond ctxt block delegate_pkh >>=? fun ctxt -> (* do effectful stuff *) Fitness.increase ctxt >>=? fun ctxt -> - let priority = snd block.proto.mining_slot in + let priority = block.proto.mining_slot.priority in fold_left_s (fun ctxt operation -> apply_operation ctxt accept_failing_script @@ -269,9 +269,7 @@ let apply_main ctxt accept_failing_script block pred_timestamp operations = >>=? fun (ctxt, _contracts) -> return ctxt) ctxt operations >>=? fun ctxt -> (* end of level (from this point nothing should fail) *) - let reward = - Mining.base_mining_reward ctxt - ~priority:(snd block.proto.mining_slot) in + let reward = Mining.base_mining_reward ctxt ~priority in Nonce.record_hash ctxt delegate_pkh reward block.proto.seed_nonce_hash >>=? fun ctxt -> Reward.pay_due_rewards ctxt >>=? fun ctxt -> diff --git a/src/proto/alpha/block_repr.ml b/src/proto/alpha/block_repr.ml index 7258883e1..88acd4f4c 100644 --- a/src/proto/alpha/block_repr.ml +++ b/src/proto/alpha/block_repr.ml @@ -21,25 +21,35 @@ type header = { and proto_header = { mining_slot: mining_slot ; seed_nonce_hash: Nonce_hash.t ; - proof_of_work_nonce: MBytes.t ;} + proof_of_work_nonce: MBytes.t ; +} -and mining_slot = Raw_level_repr.t * Int32.t +and mining_slot = { + level: Raw_level_repr.t ; + priority: Int32.t ; +} let mining_slot_encoding = let open Data_encoding in - tup2 Raw_level_repr.encoding int32 + conv + (fun { level ; priority } -> level, priority) + (fun (level, priority) -> { level ; priority }) + (obj2 + (req "level" Raw_level_repr.encoding) + (req "proprity" int32)) let proto_header_encoding = let open Data_encoding in conv (fun { mining_slot ; seed_nonce_hash ; proof_of_work_nonce } -> - (mining_slot, seed_nonce_hash, proof_of_work_nonce)) - (fun (mining_slot, seed_nonce_hash, proof_of_work_nonce) -> + (mining_slot, (seed_nonce_hash, proof_of_work_nonce))) + (fun (mining_slot, (seed_nonce_hash, proof_of_work_nonce)) -> { mining_slot ; seed_nonce_hash ; proof_of_work_nonce }) - (obj3 - (req "slot" mining_slot_encoding) - (req "seed_nonce_hash" Nonce_hash.encoding) - (req "proof_of_work_nonce" (Fixed.bytes Constants_repr.proof_of_work_nonce_size))) + (merge_objs + mining_slot_encoding + (obj2 + (req "seed_nonce_hash" Nonce_hash.encoding) + (req "proof_of_work_nonce" (Fixed.bytes Constants_repr.proof_of_work_nonce_size)))) let signed_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 b5b2a21c5..26da9e36e 100644 --- a/src/proto/alpha/block_repr.mli +++ b/src/proto/alpha/block_repr.mli @@ -22,7 +22,10 @@ and proto_header = { proof_of_work_nonce: MBytes.t ; } -and mining_slot = Raw_level_repr.t * Int32.t +and mining_slot = { + level: Raw_level_repr.t ; + priority: Int32.t ; +} val mining_slot_encoding: mining_slot Data_encoding.encoding diff --git a/src/proto/alpha/mining.ml b/src/proto/alpha/mining.ml index a43471d03..e1b1b7392 100644 --- a/src/proto/alpha/mining.ml +++ b/src/proto/alpha/mining.ml @@ -133,7 +133,7 @@ let check_timestamp c priority pred_timestamp = (Timestamp_too_early (minimal_time, timestamp)) let check_mining_rights c - { Block.proto = { mining_slot = (raw_level, priority) } } + { Block.proto = { mining_slot = { level = raw_level ; priority } } } pred_timestamp = Level.current c >>=? fun current_level -> fail_unless @@ -145,7 +145,7 @@ let check_mining_rights c return delegate let pay_mining_bond c - { Block.proto = { mining_slot = (_raw_level, priority) } } + { Block.proto = { mining_slot = { priority} } } id = if Compare.Int32.(priority >= Constants.first_free_mining_slot c) then return c diff --git a/src/proto/alpha/services_registration.ml b/src/proto/alpha/services_registration.ml index e2675e647..cdadd59a3 100644 --- a/src/proto/alpha/services_registration.ml +++ b/src/proto/alpha/services_registration.ml @@ -436,7 +436,7 @@ 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 = (raw_level, priority) in + let mining_slot = { Block.level = raw_level ; priority } in return (Block.forge_header { net_id ; predecessor ; timestamp ; fitness ; operations } { mining_slot ; seed_nonce_hash ; proof_of_work_nonce }) diff --git a/src/proto/alpha/tezos_context.mli b/src/proto/alpha/tezos_context.mli index ee3f3585d..7e37928c4 100644 --- a/src/proto/alpha/tezos_context.mli +++ b/src/proto/alpha/tezos_context.mli @@ -530,7 +530,10 @@ module Block : sig proof_of_work_nonce: MBytes.t ; } - and mining_slot = Raw_level.t * Int32.t + and mining_slot = { + level: Raw_level.t ; + priority: Int32.t ; + } val mining_slot_encoding: mining_slot Data_encoding.encoding