2017-04-06 00:43:50 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2017-04-06 00:43:50 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
module Helpers = Proto_alpha_helpers
|
|
|
|
module Assert = Helpers.Assert
|
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
let { Helpers.Account.b1 ; b2 ; b3 ; b4 ; b5 } =
|
|
|
|
Helpers.Account.bootstrap_accounts
|
|
|
|
|
|
|
|
let default_account =
|
|
|
|
Helpers.Account.create "default_account"
|
|
|
|
|
2017-04-06 00:43:50 +04:00
|
|
|
let test_double_endorsement contract block =
|
|
|
|
|
|
|
|
(* Double endorsement for the same level *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block contract [] >>=? fun b1 ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* branch root *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b1) contract [] >>=? fun b2 ->
|
2017-04-06 00:43:50 +04:00
|
|
|
(* changing branch *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b1) contract [] >>=? fun b2' ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* branch root *)
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse contract (`Hash b2) >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b2) contract [ op ] >>=? fun _b3 ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse contract (`Hash b2') >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b2') contract [ op ] >>=? fun b3' ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse contract (`Hash b3') >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b3') contract [ op ] >>=? fun b4' ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* TODO: Inject double endorsement op ! *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash b4') contract []
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-11-01 19:42:37 +04:00
|
|
|
(* FIXME: Baking.Invalid_signature is unclassified *)
|
2017-04-06 00:43:50 +04:00
|
|
|
let test_invalid_signature block =
|
|
|
|
let public_key =
|
|
|
|
Environment.Ed25519.Public_key.of_b58check_exn
|
|
|
|
"edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n" in
|
|
|
|
let secret_key =
|
|
|
|
Environment.Ed25519.Secret_key.of_b58check_exn
|
|
|
|
"edskRuR1azSfboG86YPTyxrQgosh5zChf5bVDmptqLTb5EuXAm9rsn\
|
|
|
|
DYfTKhq7rDQujdn5WWzwUMeV3agaZ6J2vPQT58jJAJPi" in
|
|
|
|
let account =
|
|
|
|
Helpers.Account.create ~keys:(secret_key, public_key) "WRONG SIGNATURE" in
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block account [] >>= fun res ->
|
2017-04-06 00:43:50 +04:00
|
|
|
Assert.generic_economic_error ~msg:__LOC__ res ;
|
|
|
|
return ()
|
|
|
|
|
|
|
|
let contain_tzerror ?(msg="") ~f t =
|
|
|
|
t >>= function
|
|
|
|
| Ok _ -> failwith "%s: Expected error found success" msg
|
|
|
|
| Error error when not (List.exists f error) ->
|
|
|
|
failwith "@[<v 2>Unexpected error@ %a@]" pp_print_error error
|
|
|
|
| _ -> return ()
|
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
let test_wrong_delegate ~miner contract block =
|
2017-04-27 03:01:05 +04:00
|
|
|
begin
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:1 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block miner [ op ] >>=? fun _ ->
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:2 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block miner [ op ] >>=? fun _ ->
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:3 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block miner [ op ] >>=? fun _ ->
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:4 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block miner [ op ] >>=? fun _ ->
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:5 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block miner [ op ] >>=? fun _ ->
|
2017-04-17 22:53:23 +04:00
|
|
|
return ()
|
2017-04-27 03:01:05 +04:00
|
|
|
end >>= fun res ->
|
|
|
|
Assert.failed_to_preapply ~msg:__LOC__ begin function
|
2017-11-01 19:42:37 +04:00
|
|
|
| Baking.Wrong_delegate _ -> true
|
2017-04-27 03:01:05 +04:00
|
|
|
| _ -> false
|
|
|
|
end res ;
|
|
|
|
Lwt.return_unit
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
let test_invalid_endorsement_slot contract block =
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:~-1 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block contract [ op ] >>= fun res ->
|
2017-04-27 03:01:05 +04:00
|
|
|
Assert.failed_to_preapply ~msg:__LOC__ ~op:(Blob op) begin function
|
2017-11-01 19:42:37 +04:00
|
|
|
| Baking.Invalid_endorsement_slot _ -> true
|
2017-04-27 03:01:05 +04:00
|
|
|
| _ -> false
|
|
|
|
end res ;
|
2017-04-17 22:53:23 +04:00
|
|
|
Helpers.Endorse.endorse ~slot:16 contract block >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block contract [ op ] >>= fun res ->
|
2017-04-27 03:01:05 +04:00
|
|
|
Assert.failed_to_preapply ~msg:__LOC__ ~op:(Blob op) begin function
|
2017-11-01 19:42:37 +04:00
|
|
|
| Baking.Invalid_endorsement_slot _ -> true
|
2017-04-27 03:01:05 +04:00
|
|
|
| _ -> false
|
|
|
|
end res ;
|
2017-04-06 00:43:50 +04:00
|
|
|
return ()
|
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
let test_endorsement_rewards block0 =
|
|
|
|
|
2017-05-31 20:05:29 +04:00
|
|
|
let get_endorser_except bs accounts =
|
2017-04-06 00:43:50 +04:00
|
|
|
let account, cpt = ref accounts.(0), ref 0 in
|
2017-05-31 20:05:29 +04:00
|
|
|
while List.mem !account bs do
|
2017-04-06 00:43:50 +04:00
|
|
|
incr cpt ;
|
|
|
|
account := accounts.(!cpt)
|
|
|
|
done ;
|
|
|
|
return (!account, !cpt) in
|
|
|
|
|
|
|
|
let bond = Tez.to_cents Constants.endorsement_bond_cost in
|
|
|
|
|
|
|
|
(* Endorsement Rights *)
|
|
|
|
(* #1 endorse & inject in a block *)
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Endorse.endorsers_list block0 >>=? fun accounts ->
|
2017-05-31 20:05:29 +04:00
|
|
|
get_endorser_except [ b1 ] accounts >>=? fun (account0, slot0) ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Account.balance ~block:block0 account0 >>=? fun balance0 ->
|
|
|
|
Helpers.Endorse.endorse ~slot:slot0 account0 block0 >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block0 b1 [ op ] >>=? fun hash1 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash1) >>=? fun () ->
|
|
|
|
Assert.balance_equal ~block:(`Hash hash1) ~msg:__LOC__ account0
|
2017-04-06 00:43:50 +04:00
|
|
|
(Int64.sub (Tez.to_cents balance0) bond) >>=? fun () ->
|
|
|
|
|
|
|
|
(* #2 endorse & inject in a block *)
|
2017-04-20 10:49:14 +04:00
|
|
|
let block1 = `Hash hash1 in
|
|
|
|
Helpers.Endorse.endorsers_list block1 >>=? fun accounts ->
|
2017-05-31 20:05:29 +04:00
|
|
|
get_endorser_except [ b1 ; account0 ] accounts >>=? fun (account1, slot1) ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Account.balance ~block:block1 account1 >>=? fun balance1 ->
|
|
|
|
Helpers.Endorse.endorse ~slot:slot1 account1 block1 >>=? fun op ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine block1 b1 [ op ] >>=? fun hash2 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash2) >>=? fun () ->
|
|
|
|
Assert.balance_equal ~block:(`Hash hash2) ~msg:__LOC__ account1
|
2017-04-06 00:43:50 +04:00
|
|
|
(Int64.sub (Tez.to_cents balance1) bond) >>=? fun () ->
|
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
(* Check rewards after one cycle for account0 *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash2) b1 [] >>=? fun hash3 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash3) >>=? fun () ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash3) b1 [] >>=? fun hash4 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash4) >>=? fun () ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash4) b1 [] >>=? fun hash5 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash5) >>=? fun () ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.endorsement_reward block1 >>=? fun rw0 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Assert.balance_equal ~block:(`Hash hash5) ~msg:__LOC__ account0
|
2017-04-06 00:43:50 +04:00
|
|
|
(Int64.add (Tez.to_cents balance0) rw0) >>=? fun () ->
|
|
|
|
|
|
|
|
(* Check rewards after one cycle for account1 *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.endorsement_reward (`Hash hash2) >>=? fun rw1 ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Assert.balance_equal ~block:(`Hash hash5) ~msg:__LOC__ account1
|
2017-04-06 00:43:50 +04:00
|
|
|
(Int64.add (Tez.to_cents balance1) rw1) >>=? fun () ->
|
|
|
|
|
|
|
|
(* #2 endorse and check reward only on the good chain *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash5) b1 []>>=? fun hash6a ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash6a) >>=? fun () ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash5) b1 [] >>=? fun hash6b ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash6b) >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* working on head *)
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Endorse.endorsers_list (`Hash hash6a) >>=? fun accounts ->
|
2017-05-31 20:05:29 +04:00
|
|
|
get_endorser_except [ b1 ] accounts >>=? fun (account3, slot3) ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Account.balance ~block:(`Hash hash6a) account3 >>=? fun balance3 ->
|
2017-04-06 00:43:50 +04:00
|
|
|
Helpers.Endorse.endorse
|
2017-04-20 10:49:14 +04:00
|
|
|
~slot:slot3 account3 (`Hash hash6a) >>=? fun ops ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash6a) b1 [ ops ] >>=? fun hash7a ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash7a) >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* working on fork *)
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Endorse.endorsers_list (`Hash hash6b) >>=? fun accounts ->
|
2017-05-31 20:05:29 +04:00
|
|
|
get_endorser_except [ b1 ] accounts >>=? fun (account4, slot4) ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Account.balance ~block:(`Hash hash7a) account4 >>=? fun _balance4 ->
|
|
|
|
Helpers.Endorse.endorse ~slot:slot4 account4 (`Hash hash6b) >>=? fun ops ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash6b) b1 [ ops ] >>=? fun _new_fork ->
|
2017-04-10 15:01:22 +04:00
|
|
|
Helpers.display_level (`Hash _new_fork) >>=? fun () ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.Account.balance ~block:(`Hash hash7a) account4 >>=? fun balance4 ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash7a) b1 [] >>=? fun hash8a ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash8a) >>=? fun () ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.mine (`Hash hash8a) b1 [] >>=? fun hash9a ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Helpers.display_level (`Hash hash9a) >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* Check rewards after one cycle *)
|
2017-11-01 19:42:37 +04:00
|
|
|
Helpers.Baking.endorsement_reward (`Hash hash7a) >>=? fun reward ->
|
2017-04-20 10:49:14 +04:00
|
|
|
Assert.balance_equal ~block:(`Hash hash9a) ~msg:__LOC__ account3
|
2017-04-06 00:43:50 +04:00
|
|
|
(Int64.add (Tez.to_cents balance3) reward) >>=? fun () ->
|
|
|
|
|
|
|
|
(* Check no reward for the fork *)
|
|
|
|
begin
|
|
|
|
if account3 = account4 then return ()
|
|
|
|
(* if account4 is different from account3, we need to check that there
|
|
|
|
is no reward for him since the endorsement was in the fork branch *)
|
2017-04-20 10:49:14 +04:00
|
|
|
else Assert.balance_equal ~block:(`Hash hash9a) ~msg:__LOC__ account4 (Tez.to_cents balance4)
|
2017-04-17 22:53:23 +04:00
|
|
|
end >>=? fun () ->
|
2017-04-20 10:49:14 +04:00
|
|
|
return ()
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
let test_endorsement_rights contract block =
|
|
|
|
Helpers.Endorse.endorsement_rights contract block >>|? fun possibilities ->
|
|
|
|
possibilities <> []
|
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
let run genesis =
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
test_endorsement_rights
|
|
|
|
default_account genesis >>=? fun has_right_to_endorse ->
|
2017-04-06 00:43:50 +04:00
|
|
|
Assert.equal_bool ~msg:__LOC__ has_right_to_endorse false ;
|
2017-04-20 10:49:14 +04:00
|
|
|
test_endorsement_rights b1 genesis >>=? fun has_right_to_endorse ->
|
2017-04-06 00:43:50 +04:00
|
|
|
Assert.equal_bool ~msg:__LOC__ has_right_to_endorse true ;
|
2017-04-20 10:49:14 +04:00
|
|
|
test_endorsement_rights b1 genesis >>=? fun has_right_to_endorse ->
|
2017-04-06 00:43:50 +04:00
|
|
|
Assert.equal_bool ~msg:__LOC__ has_right_to_endorse true ;
|
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
Assert.balance_equal
|
|
|
|
~block:genesis ~msg:__LOC__ b1 4_000_000_00L >>=? fun () ->
|
|
|
|
Assert.balance_equal
|
|
|
|
~block:genesis ~msg:__LOC__ b2 4_000_000_00L >>=? fun () ->
|
|
|
|
Assert.balance_equal
|
|
|
|
~block:genesis ~msg:__LOC__ b3 4_000_000_00L >>=? fun () ->
|
|
|
|
Assert.balance_equal
|
|
|
|
~block:genesis ~msg:__LOC__ b4 4_000_000_00L >>=? fun () ->
|
|
|
|
Assert.balance_equal
|
|
|
|
~block:genesis ~msg:__LOC__ b5 4_000_000_00L >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* Check Rewards *)
|
2017-04-20 10:49:14 +04:00
|
|
|
test_endorsement_rewards genesis >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* Endorse with a contract with wrong delegate:
|
|
|
|
- contract with no endorsement rights
|
|
|
|
- contract which signs at every available slots *)
|
2017-04-20 10:49:14 +04:00
|
|
|
test_wrong_delegate ~miner:b1 default_account genesis >>= fun () ->
|
|
|
|
test_wrong_delegate ~miner:b1 b5 genesis >>= fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* Endorse with a wrong slot : -1 and max (16) *)
|
2017-04-20 10:49:14 +04:00
|
|
|
test_invalid_endorsement_slot b3 genesis >>=? fun () ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-11-01 19:42:37 +04:00
|
|
|
(* FIXME: Baking.Invalid_signature is still unclassified *)
|
2017-04-20 10:49:14 +04:00
|
|
|
test_invalid_signature genesis >>=? fun _ ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
(* FIXME: cannot inject double endorsement operation yet, but the
|
|
|
|
code is still here
|
|
|
|
Double endorsement *)
|
2017-04-20 10:49:14 +04:00
|
|
|
test_double_endorsement b4 genesis >>=? fun _ ->
|
2017-04-06 00:43:50 +04:00
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
return ()
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
let main () =
|
2017-10-28 22:32:39 +04:00
|
|
|
Helpers.init ~rpc_port:18100 () >>=? fun (_node_pid, genesis) ->
|
2017-04-20 10:49:14 +04:00
|
|
|
run (`Hash genesis)
|
2017-04-06 00:43:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
let tests = [
|
|
|
|
"main", (fun _ -> main ()) ;
|
|
|
|
]
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Test.run "endorsement." tests
|