Alpha: do not include duplicate endorsements

This commit is contained in:
Arthur B 2017-12-07 11:34:14 +01:00 committed by Benjamin Canou
parent 264b950f39
commit f480d730e1
5 changed files with 34 additions and 1 deletions

View File

@ -13,6 +13,7 @@ open Tezos_context
type error += Wrong_voting_period of Voting_period.t * Voting_period.t (* `Temporary *) type error += Wrong_voting_period of Voting_period.t * Voting_period.t (* `Temporary *)
type error += Wrong_endorsement_predecessor of Block_hash.t * Block_hash.t (* `Temporary *) type error += Wrong_endorsement_predecessor of Block_hash.t * Block_hash.t (* `Temporary *)
type error += Duplicate_endorsement of int (* `Permanent *)
type error += Bad_contract_parameter of Contract.t * Script.expr option * Script.expr option (* `Permanent *) type error += Bad_contract_parameter of Contract.t * Script.expr option * Script.expr option (* `Permanent *)
type error += Too_many_faucet type error += Too_many_faucet
@ -60,6 +61,16 @@ let () =
(function Bad_contract_parameter (c, expected, supplied) -> (function Bad_contract_parameter (c, expected, supplied) ->
Some (c, expected, supplied) | _ -> None) Some (c, expected, supplied) | _ -> None)
(fun (c, expected, supplied) -> Bad_contract_parameter (c, expected, supplied)) ; (fun (c, expected, supplied) -> Bad_contract_parameter (c, expected, supplied)) ;
register_error_kind
`Permanent
~id:"operation.duplicate_endorsement"
~title:"Duplicate endorsement"
~description:"Two endorsements received for the same slot"
~pp:(fun ppf k ->
Format.fprintf ppf "Duplicate endorsement for slot %d." k)
Data_encoding.(obj1 (req "slot" uint16))
(function Duplicate_endorsement k -> Some k | _ -> None)
(fun k -> Duplicate_endorsement k);
register_error_kind register_error_kind
`Temporary `Temporary
~id:"operation.too_many_faucet" ~id:"operation.too_many_faucet"
@ -79,7 +90,11 @@ let apply_delegate_operation_content
fail_unless fail_unless
(Block_hash.equal block pred_block) (Block_hash.equal block pred_block)
(Wrong_endorsement_predecessor (pred_block, block)) >>=? fun () -> (Wrong_endorsement_predecessor (pred_block, block)) >>=? fun () ->
fail_when
(endorsement_already_recorded ctxt slot)
(Duplicate_endorsement (slot)) >>=? fun () ->
Baking.check_signing_rights ctxt slot delegate >>=? fun () -> Baking.check_signing_rights ctxt slot delegate >>=? fun () ->
let ctxt = record_endorsement ctxt slot in
let ctxt = Fitness.increase ctxt in let ctxt = Fitness.increase ctxt in
Baking.pay_endorsement_bond ctxt delegate >>=? fun (ctxt, bond) -> Baking.pay_endorsement_bond ctxt delegate >>=? fun (ctxt, bond) ->
Baking.endorsement_reward ~block_priority >>=? fun reward -> Baking.endorsement_reward ~block_priority >>=? fun reward ->

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Int_set = Set.Make (Compare.Int)
type t = { type t = {
context: Context.t ; context: Context.t ;
constants: Constants_repr.constants ; constants: Constants_repr.constants ;
@ -16,7 +18,9 @@ type t = {
fitness: Int64.t ; fitness: Int64.t ;
roll_value: Tez_repr.t ; roll_value: Tez_repr.t ;
faucet_count: int; faucet_count: int;
endorsements_received: Int_set.t;
} }
type context = t type context = t
type root_context = t type root_context = t
@ -29,6 +33,9 @@ let constants ctxt = ctxt.constants
let roll_value ctxt = ctxt.roll_value let roll_value ctxt = ctxt.roll_value
let recover ctxt = ctxt.context let recover ctxt = ctxt.context
let record_endorsement ctxt k = { ctxt with endorsements_received = Int_set.add k ctxt.endorsements_received }
let endorsement_already_recorded ctxt k = Int_set.mem k ctxt.endorsements_received
let incr_faucet_count ctxt = { ctxt with faucet_count = ctxt.faucet_count + 1 } let incr_faucet_count ctxt = { ctxt with faucet_count = ctxt.faucet_count + 1 }
let set_current_fitness ctxt fitness = { ctxt with fitness } let set_current_fitness ctxt fitness = { ctxt with fitness }
@ -228,7 +235,7 @@ let prepare ~level ~timestamp ~fitness ctxt =
level in level in
return ({ context = ctxt ; constants ; level ; return ({ context = ctxt ; constants ; level ;
timestamp ; fitness ; first_level ; roll_value ; timestamp ; fitness ; first_level ; roll_value ;
faucet_count = 0 ; faucet_count = 0 ; endorsements_received = Int_set.empty ;
}, },
first_block) first_block)
@ -257,6 +264,7 @@ let register_resolvers enc resolve =
fitness = 0L ; fitness = 0L ;
roll_value = Tez_repr.zero ; roll_value = Tez_repr.zero ;
faucet_count = 0 ; faucet_count = 0 ;
endorsements_received = Int_set.empty ;
} in } in
resolve faked_context str in resolve faked_context str in
Context.register_resolver enc resolve Context.register_resolver enc resolve

View File

@ -134,6 +134,10 @@ end
include T with type t := t and type context := context include T with type t := t and type context := context
val record_endorsement: context -> int -> context
val endorsement_already_recorded: context -> int -> bool
(** HACK alphanet *) (** HACK alphanet *)
val double_roll_value: context -> int -> context tzresult Lwt.t val double_roll_value: context -> int -> context tzresult Lwt.t
val faucet_count: context -> int val faucet_count: context -> int

View File

@ -153,3 +153,6 @@ let fork_test_network = Raw_context.fork_test_network
let faucet_count = Raw_context.faucet_count let faucet_count = Raw_context.faucet_count
let incr_faucet_count = Raw_context.incr_faucet_count let incr_faucet_count = Raw_context.incr_faucet_count
let endorsement_already_recorded = Raw_context.endorsement_already_recorded
let record_endorsement = Raw_context.record_endorsement

View File

@ -726,6 +726,9 @@ val configure_sandbox:
val activate: context -> Protocol_hash.t -> context Lwt.t val activate: context -> Protocol_hash.t -> context Lwt.t
val fork_test_network: context -> Protocol_hash.t -> Time.t -> context Lwt.t val fork_test_network: context -> Protocol_hash.t -> Time.t -> context Lwt.t
val endorsement_already_recorded: context -> int -> bool
val record_endorsement: context -> int -> context
(**/**) (**/**)
(* HACK alphanet *) (* HACK alphanet *)