From 065d629e7c4e1added9eb3f0659297c4198a0523 Mon Sep 17 00:00:00 2001 From: Marco Stronati Date: Fri, 23 Nov 2018 17:11:00 +0100 Subject: [PATCH] Alpha/Vote: faster refusal of too many proposals --- src/proto_alpha/lib_protocol/src/amendment.ml | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/amendment.ml b/src/proto_alpha/lib_protocol/src/amendment.ml index f12b26618..75ad7c65e 100644 --- a/src/proto_alpha/lib_protocol/src/amendment.ml +++ b/src/proto_alpha/lib_protocol/src/amendment.ml @@ -199,6 +199,16 @@ let () = (function Empty_proposal -> Some () | _ -> None) (fun () -> Empty_proposal) +(* @return [true] if [List.length l] > [n] w/o computing length *) +let rec longer_than l n = + if Compare.Int.(n < 0) then assert false else + match l with + | [] -> false + | _ :: rest -> + if Compare.Int.(n = 0) then true + else (* n > 0 *) + longer_than rest (n-1) + let record_proposals ctxt delegate proposals = begin match proposals with | [] -> fail Empty_proposal @@ -208,14 +218,15 @@ let record_proposals ctxt delegate proposals = | Proposal -> Vote.in_listings ctxt delegate >>= fun in_listings -> if in_listings then + Vote.recorded_proposal_count_for_delegate ctxt delegate >>=? fun count -> + fail_when + (longer_than proposals (Constants.max_proposals_per_delegate - count)) + Too_many_proposals >>=? fun () -> fold_left_s (fun ctxt proposal -> Vote.record_proposal ctxt proposal delegate) ctxt proposals >>=? fun ctxt -> - Vote.recorded_proposal_count_for_delegate ctxt delegate >>=? fun count -> - if Compare.Int.(count > Constants.max_proposals_per_delegate) then - fail Too_many_proposals - else return ctxt + return ctxt else fail Unauthorized_proposal | Testing_vote | Testing | Promotion_vote ->