Alpha: forbids trivial double baking evidence

This commit is contained in:
Grégoire Henry 2018-05-05 19:19:02 +02:00 committed by Benjamin Canou
parent 3dce646b18
commit a51c912722
2 changed files with 29 additions and 12 deletions

View File

@ -28,7 +28,10 @@ type error += Outdated_double_endorsement_evidence
of { level: Raw_level.t ; last: Raw_level.t } (* `Permanent *) of { level: Raw_level.t ; last: Raw_level.t } (* `Permanent *)
type error += Invalid_double_baking_evidence type error += Invalid_double_baking_evidence
of { level1: Int32.t ; level2: Int32.t } (* `Permanent *) of { hash1: Block_hash.t ;
level1: Int32.t ;
hash2: Block_hash.t ;
level2: Int32.t } (* `Permanent *)
type error += Inconsistent_double_baking_evidence type error += Inconsistent_double_baking_evidence
of { delegate1: Signature.Public_key_hash.t ; delegate2: Signature.Public_key_hash.t } (* `Permanent *) of { delegate1: Signature.Public_key_hash.t ; delegate2: Signature.Public_key_hash.t } (* `Permanent *)
type error += Unrequired_double_baking_evidence (* `Branch*) type error += Unrequired_double_baking_evidence (* `Branch*)
@ -201,17 +204,22 @@ let () =
~title:"Invalid double baking evidence" ~title:"Invalid double baking evidence"
~description:"A double-baking evidence is inconsistent \ ~description:"A double-baking evidence is inconsistent \
\ (two distinct level)" \ (two distinct level)"
~pp:(fun ppf (level1, level2) -> ~pp:(fun ppf (hash1, level1, hash2, level2) ->
Format.fprintf ppf Format.fprintf ppf
"Inconsistent double-baking evidence (levels: %ld and %ld)" "Invalid double-baking evidence (hash: %a and %a, levels: %ld and %ld)"
Block_hash.pp hash1 Block_hash.pp hash2
level1 level2) level1 level2)
Data_encoding.(obj2 Data_encoding.(obj4
(req "hash1" Block_hash.encoding)
(req "level1" int32) (req "level1" int32)
(req "hash2" Block_hash.encoding)
(req "level2" int32)) (req "level2" int32))
(function (function
| Invalid_double_baking_evidence { level1 ; level2 } -> Some (level1, level2) | Invalid_double_baking_evidence { hash1 ; level1 ; hash2 ; level2 } ->
Some (hash1, level1, hash2, level2)
| _ -> None) | _ -> None)
(fun (level1, level2) -> Invalid_double_baking_evidence { level1 ; level2 }) ; (fun (hash1, level1, hash2, level2) ->
Invalid_double_baking_evidence { hash1 ; level1 ; hash2 ; level2 }) ;
register_error_kind register_error_kind
`Permanent `Permanent
~id:"block.inconsistent_double_baking_evidence" ~id:"block.inconsistent_double_baking_evidence"
@ -677,10 +685,17 @@ let apply_contents_list
| _, _ -> fail Invalid_double_endorsement_evidence | _, _ -> fail Invalid_double_endorsement_evidence
end end
| Single (Double_baking_evidence { bh1 ; bh2 }) -> | Single (Double_baking_evidence { bh1 ; bh2 }) ->
fail_unless Compare.Int32.(bh1.shell.level = bh2.shell.level) let hash1 = Block_header.hash bh1 in
let hash2 = Block_header.hash bh2 in
fail_unless
(Compare.Int32.(bh1.shell.level = bh2.shell.level) &&
not (Block_hash.equal hash1 hash2))
(Invalid_double_baking_evidence (Invalid_double_baking_evidence
{ level1 = bh1.shell.level ; { hash1 ;
level2 = bh2.shell.level }) >>=? fun () -> level1 = bh1.shell.level ;
hash2 ;
level2 = bh2.shell.level ;
}) >>=? fun () ->
Lwt.return (Raw_level.of_int32 bh1.shell.level) >>=? fun raw_level -> Lwt.return (Raw_level.of_int32 bh1.shell.level) >>=? fun raw_level ->
let oldest_level = Level.last_allowed_fork_level ctxt in let oldest_level = Level.last_allowed_fork_level ctxt in
fail_unless Raw_level.(raw_level < (Level.current ctxt).level) fail_unless Raw_level.(raw_level < (Level.current ctxt).level)

View File

@ -87,8 +87,10 @@ let same_blocks () =
Context.init 2 >>=? fun (b, _contracts) -> Context.init 2 >>=? fun (b, _contracts) ->
Block.bake b >>=? fun ba -> Block.bake b >>=? fun ba ->
Op.double_baking (B ba) ba.header ba.header >>=? fun operation -> Op.double_baking (B ba) ba.header ba.header >>=? fun operation ->
Block.bake ~operation ba >>=? fun _ -> Block.bake ~operation ba >>= fun res ->
(* TODO: should fail *) Assert.proto_error ~loc:__LOC__ res begin function
| Apply.Invalid_double_baking_evidence _ -> true
| _ -> false end >>=? fun () ->
return () return ()
(** Check that a double baking operation exposing two blocks with (** Check that a double baking operation exposing two blocks with
@ -174,7 +176,7 @@ let tests = [
Test.tztest "valid double baking evidence" `Quick valid_double_baking_evidence ; Test.tztest "valid double baking evidence" `Quick valid_double_baking_evidence ;
(* Should fail*) (* Should fail*)
(* Test.tztest "same blocks" `Quick same_blocks ; *) Test.tztest "same blocks" `Quick same_blocks ;
Test.tztest "different levels" `Quick different_levels ; Test.tztest "different levels" `Quick different_levels ;
Test.tztest "too early double baking evidence" `Quick too_early_double_baking_evidence ; Test.tztest "too early double baking evidence" `Quick too_early_double_baking_evidence ;
Test.tztest "too late double baking evidence" `Quick too_late_double_baking_evidence ; Test.tztest "too late double baking evidence" `Quick too_late_double_baking_evidence ;