diff --git a/src/proto_alpha/lib_protocol/test/main.ml b/src/proto_alpha/lib_protocol/test/main.ml index fcfdc965fb33bd5e18b7318a4013fdca0e447078..ff5c62e2cc7d54c8b2af97bfd4e587d488d2a7f3 100644 --- a/src/proto_alpha/lib_protocol/test/main.ml +++ b/src/proto_alpha/lib_protocol/test/main.ml @@ -23,31 +23,38 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol + Invocation: dune build @src/proto_alpha/lib_protocol/runtest + Subject: Entrypoint +*) + let () = Alcotest_lwt.run "protocol_alpha" - [ ("transfer", Transfer.tests); - ("origination", Origination.tests); - ("activation", Activation.tests); - ("revelation", Reveal.tests); - ("endorsement", Endorsement.tests); - ("double endorsement", Double_endorsement.tests); - ("double baking", Double_baking.tests); - ("seed", Seed.tests); - ("baking", Baking.tests); - ("delegation", Delegation.tests); - ("rolls", Rolls.tests); - ("combined", Combined_operations.tests); - ("qty", Qty.tests); - ("voting", Voting.tests); - ("interpretation", Interpretation.tests); - ("typechecking", Typechecking.tests); - ("gas properties", Gas_properties.tests); - ("fixed point computation", Fixed_point.tests); - ("gas levels", Gas_levels.tests); - ("gas cost functions", Gas_costs.tests); - ("lazy storage diff", Lazy_storage_diff.tests); + [ ("transfer", Test_transfer.tests); + ("origination", Test_origination.tests); + ("activation", Test_activation.tests); + ("revelation", Test_reveal.tests); + ("endorsement", Test_endorsement.tests); + ("double endorsement", Test_double_endorsement.tests); + ("double baking", Test_double_baking.tests); + ("seed", Test_seed.tests); + ("baking", Test_baking.tests); + ("delegation", Test_delegation.tests); + ("rolls", Test_rolls.tests); + ("combined", Test_combined_operations.tests); + ("qty", Test_qty.tests); + ("voting", Test_voting.tests); + ("interpretation", Test_interpretation.tests); + ("typechecking", Test_typechecking.tests); + ("gas properties", Test_gas_properties.tests); + ("fixed point computation", Test_fixed_point.tests); + ("gas levels", Test_gas_levels.tests); + ("gas cost functions", Test_gas_costs.tests); + ("lazy storage diff", Test_lazy_storage_diff.tests); ("sapling", Test_sapling.tests); ("helpers rpcs", Test_helpers_rpcs.tests); - ("script deserialize gas", Script_gas.tests) ] + ("script deserialize gas", Test_script_gas.tests) ] |> Lwt_main.run diff --git a/src/proto_alpha/lib_protocol/test/activation.ml b/src/proto_alpha/lib_protocol/test/test_activation.ml similarity index 86% rename from src/proto_alpha/lib_protocol/test/activation.ml rename to src/proto_alpha/lib_protocol/test/test_activation.ml index 792d4b38efb6554ac45637a9cd662210759d3d73..8c73151e9a8a827accf093e438a2338969a617bd 100644 --- a/src/proto_alpha/lib_protocol/test/activation.ml +++ b/src/proto_alpha/lib_protocol/test/test_activation.ml @@ -23,16 +23,21 @@ (* *) (*****************************************************************************) -(** The activation operation creates an implicit contract from a - registered commitment present in the context. It is parametrized by - a public key hash (pkh) and a secret. +(** Testing + ------- + Component: Protocol (activation) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^activation$" + Subject: The activation operation creates an implicit contract from a + registered commitment present in the context. It is + parametrized by a public key hash (pkh) and a secret. - The commitments are composed of : - - a blinded pkh that can be revealed by the secret ; - - an amount. + The commitments are composed of : + - a blinded pkh that can be revealed by the secret ; + - an amount. - The commitments and the secrets are generated from - /scripts/create_genesis/create_genesis.py and should be coherent. + The commitments and the secrets are generated from + /scripts/create_genesis/create_genesis.py and should be + coherent. *) open Protocol @@ -302,17 +307,21 @@ let secrets () = "zknAl3lrX2", "ettilrvh.zsrqrbud@tezos.example.org" ) ] +(** Helper: Create a genesis block with predefined commitments, + accounts and balances. *) let activation_init () = Context.init ~with_commitments:true 1 >|=? fun (b, cs) -> secrets () |> fun ss -> (b, cs, ss) -let simple_init_with_commitments () = +(** Verify the genesis block created by [activation_init] can be + baked. *) +let test_simple_init_with_commitments () = activation_init () >>=? fun (blk, _contracts, _secrets) -> Block.bake blk >>=? fun _ -> return_unit (** A single activation *) -let single_activation () = +let test_single_activation () = activation_init () >>=? fun (blk, _contracts, secrets) -> let ({account; activation_code; amount = expected_amount; _} as _first_one) = @@ -336,8 +345,8 @@ let single_activation () = (Contract.implicit_contract account) expected_amount -(** 10 activations, one per bake *) -let multi_activation_1 () = +(** 10 activations, one per bake. *) +let test_multi_activation_1 () = activation_init () >>=? fun (blk, _contracts, secrets) -> List.fold_left_es @@ -356,8 +365,8 @@ let multi_activation_1 () = secrets >>=? fun _ -> return_unit -(** All in one bake *) -let multi_activation_2 () = +(** All of the 10 activations occur in one bake. *) +let test_multi_activation_2 () = activation_init () >>=? fun (blk, _contracts, secrets) -> List.fold_left_es @@ -378,8 +387,8 @@ let multi_activation_2 () = expected_amount) secrets -(** Transfer with activated account *) -let activation_and_transfer () = +(** Transfer with activated account. *) +let test_activation_and_transfer () = activation_init () >>=? fun (blk, contracts, secrets) -> let ({account; activation_code; _} as _first_one) = @@ -408,8 +417,8 @@ let activation_and_transfer () = activated_amount_before half_amount -(** Transfer to an unactivated account and then activating it *) -let transfer_to_unactivated_then_activate () = +(** Transfer to an unactivated account and then activating it. *) +let test_transfer_to_unactivated_then_activate () = activation_init () >>=? fun (blk, contracts, secrets) -> let ({account; activation_code; amount} as _first_one) = @@ -448,9 +457,9 @@ let transfer_to_unactivated_then_activate () = (* The following test scenarios are supposed to raise errors. *) (****************************************************************) -(** Invalid pkh activation : expected to fail as the context does not - contain any commitment *) -let invalid_activation_with_no_commitments () = +(** Invalid pkh activation: expected to fail as the context does not + contain any commitment. *) +let test_invalid_activation_with_no_commitments () = Context.init 1 >>=? fun (blk, _) -> let secrets = secrets () in @@ -467,8 +476,8 @@ let invalid_activation_with_no_commitments () = | _ -> false) -(** Wrong activation : wrong secret given in the operation *) -let invalid_activation_wrong_secret () = +(** Wrong activation: wrong secret given in the operation. *) +let test_invalid_activation_wrong_secret () = activation_init () >>=? fun (blk, _, secrets) -> let ({account; _} as _first_one) = Option.get @@ List.nth secrets 0 in @@ -486,8 +495,8 @@ let invalid_activation_wrong_secret () = false) (** Invalid pkh activation : expected to fail as the context does not - contain an associated commitment *) -let invalid_activation_inexistent_pkh () = + contain an associated commitment. *) +let test_invalid_activation_inexistent_pkh () = activation_init () >>=? fun (blk, _, secrets) -> let ({activation_code; _} as _first_one) = Option.get @@ List.hd secrets in @@ -506,8 +515,8 @@ let invalid_activation_inexistent_pkh () = false) (** Invalid pkh activation : expected to fail as the commitment has - already been claimed *) -let invalid_double_activation () = + already been claimed. *) +let test_invalid_double_activation () = activation_init () >>=? fun (blk, _, secrets) -> let ({account; activation_code; _} as _first_one) = @@ -529,8 +538,8 @@ let invalid_double_activation () = | _ -> false) -(** Transfer from an unactivated commitment account *) -let invalid_transfer_from_unactivated_account () = +(** Transfer from an unactivated commitment account. *) +let test_invalid_transfer_from_unactivated_account () = activation_init () >>=? fun (blk, contracts, secrets) -> let ({account; _} as _first_one) = Option.get @@ List.hd secrets in @@ -552,26 +561,32 @@ let invalid_transfer_from_unactivated_account () = false) let tests = - [ Test.tztest "init with commitments" `Quick simple_init_with_commitments; - Test.tztest "single activation" `Quick single_activation; - Test.tztest "multi-activation one-by-one" `Quick multi_activation_1; - Test.tztest "multi-activation all at a time" `Quick multi_activation_2; - Test.tztest "activation and transfer" `Quick activation_and_transfer; + [ Test.tztest "init with commitments" `Quick test_simple_init_with_commitments; + Test.tztest "single activation" `Quick test_single_activation; + Test.tztest "multi-activation one-by-one" `Quick test_multi_activation_1; + Test.tztest "multi-activation all at a time" `Quick test_multi_activation_2; + Test.tztest "activation and transfer" `Quick test_activation_and_transfer; Test.tztest "transfer to unactivated account then activate" `Quick - transfer_to_unactivated_then_activate; + test_transfer_to_unactivated_then_activate; Test.tztest "invalid activation with no commitments" `Quick - invalid_activation_with_no_commitments; + test_invalid_activation_with_no_commitments; Test.tztest "invalid activation with commitments" `Quick - invalid_activation_inexistent_pkh; - Test.tztest "invalid double activation" `Quick invalid_double_activation; - Test.tztest "wrong activation code" `Quick invalid_activation_wrong_secret; + test_invalid_activation_inexistent_pkh; + Test.tztest + "invalid double activation" + `Quick + test_invalid_double_activation; + Test.tztest + "wrong activation code" + `Quick + test_invalid_activation_wrong_secret; Test.tztest "invalid transfer from unactivated account" `Quick - invalid_transfer_from_unactivated_account ] + test_invalid_transfer_from_unactivated_account ] diff --git a/src/proto_alpha/lib_protocol/test/baking.ml b/src/proto_alpha/lib_protocol/test/test_baking.ml similarity index 88% rename from src/proto_alpha/lib_protocol/test/baking.ml rename to src/proto_alpha/lib_protocol/test/test_baking.ml index 9b750d2e33fec2367c35ae9acfa8f60f5eaeb1d6..29ac3327c67a6c032c37eb37c3717b955672ef0b 100644 --- a/src/proto_alpha/lib_protocol/test/baking.ml +++ b/src/proto_alpha/lib_protocol/test/test_baking.ml @@ -24,10 +24,28 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (baking) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^baking$" + Subject: Rewards and bakers. Tests based on RPCs. +*) + open Protocol open Alpha_context -(** Tests for [bake_n] and [bake_until_end_cycle]. *) +(** Verify the level is correctly computed when the first cycle is + passed and after baking a certain fixed number of blocks (10 for + the moment). The result should be [blocks_per_cycle + 10] where + [blocks_per_cycle] comes from the constants of the selected + protocol. + + IMPROVEMENTS: + - Randomize the number of cycle. + - Randomize the number of accounts created at the beginning + - Randomize the blocks per cycle. + - Randomize the number of blocks baked after the n cycles baked + previously. *) let test_cycle () = Context.init 5 >>=? fun (b, _) -> @@ -35,8 +53,6 @@ let test_cycle () = >>=? fun csts -> let blocks_per_cycle = csts.parametric.blocks_per_cycle in let pp fmt x = Format.fprintf fmt "%ld" x in - (* Tests that [bake_until_cycle_end] returns a block at - level [blocks_per_cycle]. *) Block.bake b >>=? fun b -> Block.bake_until_cycle_end b @@ -51,7 +67,6 @@ let test_cycle () = (Alpha_context.Raw_level.to_int32 curr_level) blocks_per_cycle >>=? fun () -> - (* Tests that [bake_n n] bakes [n] blocks. *) Context.get_level (B b) >>?= fun l -> Block.bake_n 10 b @@ -66,8 +81,8 @@ let test_cycle () = (Alpha_context.Raw_level.to_int32 curr_level) (Int32.add (Alpha_context.Raw_level.to_int32 l) 10l) -(** Check that after baking and/or endorsing a block the baker and the - endorsers get their reward *) +(** After baking and/or endorsing a block, the baker and the endorsers + get their reward. *) let test_rewards_retrieval () = Context.init 256 >>=? fun (b, _) -> @@ -165,8 +180,8 @@ let test_rewards_retrieval () = real_endorsers) ranges -(** Tests the baking and endorsing rewards formulas against a - precomputed table *) +(** Checks the baking and endorsing rewards formulas against a precomputed + table. *) let test_rewards_formulas () = Context.init 1 >>=? fun (b, _) -> @@ -197,8 +212,8 @@ let test_rewards_formulas () = let wrap e = Lwt.return (Environment.wrap_error e) -(* Check that the rewards formulas from Context are - equivalent with the ones from Baking *) +(** Check that the rewards formulas from Context are equivalent with + the ones from Baking. *) let test_rewards_formulas_equivalence () = Context.init 1 >>=? fun (b, _) -> @@ -237,6 +252,7 @@ let test_rewards_formulas_equivalence () = >>=? fun reward2 -> Assert.equal_tez ~loc:__LOC__ reward1 reward2) ranges +(** Test baking [n] cycles in a raw works smoothly. *) let test_bake_n_cycles n () = let open Block in let policy = By_priority 0 in @@ -244,11 +260,8 @@ let test_bake_n_cycles n () = >>=? fun (block, _contracts) -> Block.bake_until_n_cycle_end ~policy n block >>=? fun _block -> return () -(* gets the voting power *) -let get_voting_power block pkhash = - let ctxt = Context.B block in - Context.get_voting_power ctxt pkhash - +(** Check the voting power is constant between cycles when number of + rolls are constant and in presence of one account. *) let test_voting_power_cache () = let open Block in let policy = By_priority 0 in @@ -258,7 +271,8 @@ let test_voting_power_cache () = >>=? fun bakers -> let baker = Option.get @@ List.hd bakers in let assert_voting_power n block = - get_voting_power block baker + let ctxt = Context.B block in + Context.get_voting_power ctxt baker >>=? fun voting_power -> Assert.equal_int ~loc:__LOC__ n (Int32.to_int voting_power) in diff --git a/src/proto_alpha/lib_protocol/test/combined_operations.ml b/src/proto_alpha/lib_protocol/test/test_combined_operations.ml similarity index 89% rename from src/proto_alpha/lib_protocol/test/combined_operations.ml rename to src/proto_alpha/lib_protocol/test/test_combined_operations.ml index 84d9b38146c8a6b38deb56ed8b22575e8d392622..415eb6ec843e11b2171f3e04662605cfe9cb56b9 100644 --- a/src/proto_alpha/lib_protocol/test/combined_operations.ml +++ b/src/proto_alpha/lib_protocol/test/test_combined_operations.ml @@ -23,16 +23,23 @@ (* *) (*****************************************************************************) -(** Multiple operations can be grouped in one ensuring their - deterministic application. +(** Testing + ------- + Component: Protocol (combined operations) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^combined$" + Subject: Multiple operations can be grouped in one ensuring their + deterministic application. - If an invalid operation is present in this group of operation, the - previous applied operations are backtracked leaving the context - unchanged and the following operations are skipped. Fees attributed - to the operations are collected by the baker nonetheless. + If an invalid operation is present in this group of + operations, the previously applied operations are + backtracked leaving the context unchanged and the + following operations are skipped. Fees attributed to the + operations are collected by the baker nonetheless. - Only manager operations are allowed in multiple transactions. - They must all belong to the same manager as there is only one signature. *) + Only manager operations are allowed in multiple transactions. + They must all belong to the same manager as there is only one + signature. +*) open Protocol open Test_tez @@ -40,7 +47,7 @@ open Test_tez let ten_tez = Tez.of_int 10 (** Groups ten transactions between the same parties. *) -let multiple_transfers () = +let test_multiple_transfers () = Context.init 3 >>=? fun (blk, contracts) -> let (c1, c2, c3) = @@ -74,7 +81,7 @@ let multiple_transfers () = >>=? fun () -> return_unit (** Groups ten delegated originations. *) -let multiple_origination_and_delegation () = +let test_multiple_origination_and_delegation () = Context.init 2 >>=? fun (blk, contracts) -> let (c1, c2) = @@ -162,7 +169,7 @@ let expect_balance_too_low = function (** Groups three operations, the middle one failing. Checks that the receipt is consistent. Variant without fees. *) -let failing_operation_in_the_middle () = +let test_failing_operation_in_the_middle () = Context.init 2 >>=? fun (blk, contracts) -> let (c1, c2) = @@ -219,7 +226,7 @@ let failing_operation_in_the_middle () = (** Groups three operations, the middle one failing. Checks that the receipt is consistent. Variant with fees, that should be spent even in case of failure. *) -let failing_operation_in_the_middle_with_fees () = +let test_failing_operation_in_the_middle_with_fees () = Context.init 2 >>=? fun (blk, contracts) -> let (c1, c2) = @@ -294,7 +301,7 @@ let expect_wrong_signature list = "Packed operation has invalid source in the middle : operation expected \ to fail." -let wrong_signature_in_the_middle () = +let test_wrong_signature_in_the_middle () = Context.init 2 >>=? function | (_, []) | (_, [_]) -> @@ -330,20 +337,20 @@ let wrong_signature_in_the_middle () = >>=? fun _inc -> return_unit let tests = - [ Test.tztest "multiple transfers" `Quick multiple_transfers; + [ Test.tztest "multiple transfers" `Quick test_multiple_transfers; Test.tztest "multiple originations and delegations" `Quick - multiple_origination_and_delegation; + test_multiple_origination_and_delegation; Test.tztest "Failing operation in the middle" `Quick - failing_operation_in_the_middle; + test_failing_operation_in_the_middle; Test.tztest "Failing operation in the middle (with fees)" `Quick - failing_operation_in_the_middle_with_fees; + test_failing_operation_in_the_middle_with_fees; Test.tztest "Failing operation (wrong manager in the middle of a pack)" `Quick - wrong_signature_in_the_middle ] + test_wrong_signature_in_the_middle ] diff --git a/src/proto_alpha/lib_protocol/test/delegation.ml b/src/proto_alpha/lib_protocol/test/test_delegation.ml similarity index 85% rename from src/proto_alpha/lib_protocol/test/delegation.ml rename to src/proto_alpha/lib_protocol/test/test_delegation.ml index f9ddabc64207a49e144db822b268d8d7c861860c..34a349036a5ebb898eecb9d30be6bf542365ea38 100644 --- a/src/proto_alpha/lib_protocol/test/delegation.ml +++ b/src/proto_alpha/lib_protocol/test/test_delegation.ml @@ -23,16 +23,29 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (delegation) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^delegation$" + Subject: - Properties on bootstrap contracts (self-delegation, + cannot delete/change their delegate (as opposed to contracts + not-being-delegate which can do these), bootstrap manager + as delegate during origination). + - Properties on delegation depending on whether delegate + keys registration, through origination and delegation. +*) + open Protocol open Alpha_context open Test_tez -(**************************************************************************) -(* bootstrap contracts *) -(**************************************************************************) -(* Bootstrap contracts are heavily used in other tests. It is helpful - to test some properties of these contracts, so we can correctly - interpret the other tests that use them. *) +(*****************************************************************************) +(* Bootstrap contracts + ------------------- + Bootstrap contracts are heavily used in other tests. It is helpful to test + some properties of these contracts, so we can correctly interpret the other + tests that use them. *) +(*****************************************************************************) let expect_error err = function | err0 :: _ when err = err0 -> @@ -49,7 +62,7 @@ let expect_no_change_registered_delegate_pkh pkh = function | _ -> failwith "Delegate can not be deleted and operation should fail." -(** bootstrap contracts delegate to themselves *) +(** Bootstrap contracts delegate to themselves. *) let bootstrap_manager_is_bootstrap_delegate () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -59,7 +72,7 @@ let bootstrap_manager_is_bootstrap_delegate () = Context.Contract.manager (B b) bootstrap0 >>=? fun manager0 -> Assert.equal_pkh ~loc:__LOC__ delegate0 manager0.pkh -(** bootstrap contracts cannot change their delegate *) +(** Bootstrap contracts cannot change their delegate. *) let bootstrap_delegate_cannot_change ~fee () = Context.init 2 >>=? fun (b, bootstrap_contracts) -> @@ -102,7 +115,7 @@ let bootstrap_delegate_cannot_change ~fee () = (* fee has been debited *) Assert.balance_was_debited ~loc:__LOC__ (B b) bootstrap0 balance0 fee -(** bootstrap contracts cannot delete their delegation *) +(** Bootstrap contracts cannot delete their delegation. *) let bootstrap_delegate_cannot_be_removed ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -140,7 +153,8 @@ let bootstrap_delegate_cannot_be_removed ~fee () = (* fee has been debited *) Assert.balance_was_debited ~loc:__LOC__ (I i) bootstrap balance fee -(** contracts not registered as delegate can change their delegation *) +(** Contracts not registered as delegate can change their + delegation. *) let delegate_can_be_changed_from_unregistered_contract ~fee () = Context.init 2 >>=? fun (b, bootstrap_contracts) -> @@ -193,7 +207,8 @@ let delegate_can_be_changed_from_unregistered_contract ~fee () = (* fee has been debited *) Assert.balance_was_debited ~loc:__LOC__ (I i) unregistered credit fee -(** contracts not registered as delegate can delete their delegation *) +(** Contracts not registered as delegate can delete their + delegation. *) let delegate_can_be_removed_from_unregistered_contract ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -246,7 +261,7 @@ let delegate_can_be_removed_from_unregistered_contract ~fee () = (* fee has been debited *) Assert.balance_was_debited ~loc:__LOC__ (I i) unregistered credit fee -(** bootstrap keys are already registered as delegate keys *) +(** Bootstrap keys are already registered as delegate keys. *) let bootstrap_manager_already_registered_delegate ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -282,8 +297,8 @@ let bootstrap_manager_already_registered_delegate ~fee () = (* fee has been debited *) Assert.balance_was_debited ~loc:__LOC__ (I i) impl_contract balance fee -(** bootstrap manager can be set as delegate of an originated contract - (through origination operation) *) +(** Bootstrap manager can be set as delegate of an originated contract + (through origination operation). *) let delegate_to_bootstrap_by_origination ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -417,10 +432,10 @@ let tests_bootstrap_contracts = `Quick (delegate_to_bootstrap_by_origination ~fee:(Tez.of_int 10_000_000)) ] -(**************************************************************************) -(* delegate registration *) -(**************************************************************************) -(* A delegate is a pkh. Delegates must be registered. Registration is +(*****************************************************************************) +(* Delegate registration + --------------------- + A delegate is a pkh. Delegates must be registered. Registration is done via the self-delegation of the implicit contract corresponding to the pkh. The implicit contract must be credited when the self-delegation is done. Furthermore, trying to register an already @@ -431,7 +446,6 @@ let tests_bootstrap_contracts = 2- registered keys can be delegated to, 3- registering an already registered key raises an error. - We consider three scenarios for setting a delegate: - through origination, - through delegation when the implicit contract has no delegate yet, @@ -439,38 +453,36 @@ let tests_bootstrap_contracts = We also test that emptying the implicit contract linked to a registered delegate key does not unregister the delegate key. -*) -(* Valid registration - + ------------------ Unregistered key: - - contract not credited and no self-delegation - - contract credited but no self-delegation - - contract not credited and self-delegation + - contract not credited and no self-delegation, + - contract credited but no self-delegation, + - contract not credited and self-delegation. -Not credited: -- no credit operation -- credit operation of 1μꜩ and then debit operation of 1μꜩ + Not credited: + - no credit operation + - credit operation of 1μꜩ and then debit operation of 1μꜩ *) +(*****************************************************************************) -*) +(* Part A. + Unregistered delegate keys cannot be used for delegation -(** A- unregistered delegate keys cannot be used for delegation *) + Two main series of tests: without self-delegation and with a failed attempt at self-delegation: -(* Two main series of tests: without self-delegation, and with a failed attempt at self-delegation - 1- no self-delegation - a- no credit - - no token transfer - - credit of 1μꜩ and then debit of 1μꜩ - b- with credit of 1μꜩ. - For every scenario, we try three different ways of delegating: - - through origination (init origination) - - through delegation when no delegate was assigned (init delegation) - - through delegation when a delegate was assigned (switch delegation). + 1/ no self-delegation + a/ no credit + - no token transfer + - credit of 1μꜩ and then debit of 1μꜩ + b/ with credit of 1μꜩ. + For every scenario, we try three different ways of delegating: + - through origination (init origination) + - through delegation when no delegate was assigned (init delegation) + - through delegation when a delegate was assigned (switch delegation). - 2- Self-delegation fails if the contract has no credit. We try the - two possibilities of 1a for non-credited contracts. -*) + 2/ Self-delegation fails if the contract has no credit. We try the + two possibilities of 1a for non-credited contracts. *) let expect_unregistered_key pkh = function | Environment.Ecoproto_error (Roll_storage.Unregistered_delegate pkh0) :: _ @@ -479,9 +491,15 @@ let expect_unregistered_key pkh = function | _ -> failwith "Delegate key is not registered: operation should fail." -(* A1: no self-delegation *) -(* no token transfer, no self-delegation *) -let unregistered_delegate_key_init_origination ~fee () = +(* Part A. Section 1. + No self-delegation. *) + +(** No token transfer, no self-delegation. Originated account. If + fees are higher than balance, [Balance_too_low] is + raised. Otherwise, it checks the correct exception is raised + (unregistered key), and the fees are still debited. Using RPCs, we + verify the contract has not been originated. *) +let test_unregistered_delegate_key_init_origination ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -532,7 +550,11 @@ let unregistered_delegate_key_init_origination ~fee () = | _ -> false) -let unregistered_delegate_key_init_delegation ~fee () = +(** Delegation when delegate key is not assigned. Delegate account is + initialized. If fees are higher than initial credit (10 tez), + [Balance_too_low] is raised. Otherwise, fees are still debited. The + implicit contract has no delegate. *) +let test_unregistered_delegate_key_init_delegation ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -582,7 +604,11 @@ let unregistered_delegate_key_init_delegation ~fee () = | _ -> false) -let unregistered_delegate_key_switch_delegation ~fee () = +(** Re-delegation when a delegate key was already assigned. If fees + are higher than initial credit (10 tez), [Balance_too_low] is + raised. Otherwise, fees are not debited and the implicit contract + delegate remains unchanged. *) +let test_unregistered_delegate_key_switch_delegation ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -640,8 +666,9 @@ let unregistered_delegate_key_switch_delegation ~fee () = >>=? fun delegate_pkh_after -> Assert.equal_pkh ~loc:__LOC__ delegate_pkh delegate_pkh_after -(* credit of some amount, no self-delegation *) -let unregistered_delegate_key_init_origination_credit ~fee ~amount () = +(** Same as [unregistered_delegate_key_init_origination] and credits + [amount], no self-delegation. *) +let test_unregistered_delegate_key_init_origination_credit ~fee ~amount () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -692,7 +719,9 @@ let unregistered_delegate_key_init_origination_credit ~fee ~amount () = | _ -> false) -let unregistered_delegate_key_init_delegation_credit ~fee ~amount () = +(** Same as [unregistered_delegate_key_init_delegation] and credits + the amount [amount] of the implicit contract. *) +let test_unregistered_delegate_key_init_delegation_credit ~fee ~amount () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -750,7 +779,9 @@ let unregistered_delegate_key_init_delegation_credit ~fee ~amount () = | _ -> false) -let unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = +(** Same as in [unregistered_delegate_key_switch_delegation] and + credits the amount [amount] to the implicit contract. *) +let test_unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -817,8 +848,10 @@ let unregistered_delegate_key_switch_delegation_credit ~fee ~amount () = Assert.not_equal_pkh ~loc:__LOC__ delegate unregistered_delegate_pkh >>=? fun () -> Assert.equal_pkh ~loc:__LOC__ delegate bootstrap_pkh -(* a credit of some amount followed by a debit of the same amount, no self-delegation *) -let unregistered_delegate_key_init_origination_credit_debit ~fee ~amount () = +(** A credit of some amount followed by a debit of the same amount, + no self-delegation. *) +let test_unregistered_delegate_key_init_origination_credit_debit ~fee ~amount + () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -876,7 +909,10 @@ let unregistered_delegate_key_init_origination_credit_debit ~fee ~amount () = | _ -> false) -let unregistered_delegate_key_init_delegation_credit_debit ~amount ~fee () = +(** Same as in [unregistered_delegate_key_init_delegation] but credits + then debits the amount [amount] to the implicit contract. *) +let test_unregistered_delegate_key_init_delegation_credit_debit ~amount ~fee () + = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -939,7 +975,10 @@ let unregistered_delegate_key_init_delegation_credit_debit ~amount ~fee () = | _ -> false) -let unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount () = +(** Same as in [unregistered_delegate_key_switch_delegation] but + credits then debits the amount [amount] to the implicit contract. *) +let test_unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount + () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1010,8 +1049,11 @@ let unregistered_delegate_key_switch_delegation_credit_debit ~fee ~amount () = >>=? fun delegate -> Assert.not_equal_pkh ~loc:__LOC__ delegate unregistered_delegate_pkh -(* A2- self-delegation to an empty contract fails *) -let failed_self_delegation_no_transaction () = +(* Part A. Section 2. + Self-delegation to an empty contract fails. *) + +(** Self-delegation with zero-balance contract should fail. *) +let test_failed_self_delegation_no_transaction () = Context.init 1 >>=? fun (b, _) -> Incremental.begin_construction b @@ -1035,7 +1077,9 @@ let failed_self_delegation_no_transaction () = | _ -> false) -let failed_self_delegation_emptied_implicit_contract amount () = +(** Implicit contract is credited then debited of same amount (i.e., + is emptied). Self-delegation fails. *) +let test_failed_self_delegation_emptied_implicit_contract amount () = (* create an implicit contract *) Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -1070,7 +1114,10 @@ let failed_self_delegation_emptied_implicit_contract amount () = | _ -> false) -let emptying_delegated_implicit_contract_fails amount () = +(** Implicit contract is credited with a non-zero quantity [amount] + tz, then it is delegated. The operation of debit of [amount] tz + should fail as the contract is already delegated. *) +let test_emptying_delegated_implicit_contract_fails amount () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1104,12 +1151,16 @@ let emptying_delegated_implicit_contract_fails amount () = | _ -> false) -(** B- valid registration: - - credit implicit contract with some ꜩ + verification of balance - - self delegation + verification - - empty contract + verification of balance + verification of not being erased / self-delegation - - create delegator implicit contract w first implicit contract as delegate + verification of delegation *) -let valid_delegate_registration_init_delegation_credit amount () = +(* Part B. + - Valid registration: + - Credit implicit contract with some ꜩ + verification of balance + - Self delegation + verification + - Empty contract + verification of balance + verification of not being erased / self-delegation + - Create delegator implicit contract w first implicit contract as delegate + verification of delegation. *) + +(** Initialized account is credited of [amount] tz, then + self-delegated. *) +let test_valid_delegate_registration_init_delegation_credit amount () = (* create an implicit contract *) Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -1162,7 +1213,11 @@ let valid_delegate_registration_init_delegation_credit amount () = >>=? fun delegator_delegate -> Assert.equal_pkh ~loc:__LOC__ delegator_delegate delegate_pkh -let valid_delegate_registration_switch_delegation_credit amount () = +(** Create an implicit contract, credits with [amount] + tz. Self-delegates. Create another implicit contract with + bootstrap as delegate. Re-delegate it to the first implicit + contract. *) +let test_valid_delegate_registration_switch_delegation_credit amount () = (* create an implicit contract *) Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -1216,7 +1271,8 @@ let valid_delegate_registration_switch_delegation_credit amount () = >>=? fun delegator_delegate -> Assert.equal_pkh ~loc:__LOC__ delegator_delegate delegate_pkh -let valid_delegate_registration_init_delegation_credit_debit amount () = +(** Create an implicit contract. *) +let test_valid_delegate_registration_init_delegation_credit_debit amount () = (* create an implicit contract *) Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -1284,7 +1340,12 @@ let valid_delegate_registration_init_delegation_credit_debit amount () = >>=? fun delegator_delegate -> Assert.equal_pkh ~loc:__LOC__ delegator_delegate delegate_pkh -let valid_delegate_registration_switch_delegation_credit_debit amount () = +(** A created implicit contract is credited with [amount] tz, then is + self-delegated. It is emptied (fund back into bootstrap), and + should remain existing (as registered as delegate). Another created + implicit contract is delegated to bootstrap, then should be able to + be re-delegated to the latter contract. *) +let test_valid_delegate_registration_switch_delegation_credit_debit amount () = (* create an implicit contract *) Context.init 1 >>=? fun (b, bootstrap_contracts) -> @@ -1348,10 +1409,12 @@ let valid_delegate_registration_switch_delegation_credit_debit amount () = >>=? fun delegator_delegate -> Assert.equal_pkh ~loc:__LOC__ delegator_delegate delegate_pkh -(* with implicit contract with some credit *) +(* Part C. + A second self-delegation should raise an [Active_delegate] error. *) -(** C- a second self-delegation should raise an `Active_delegate` error *) -let double_registration () = +(** Second self-delegation should fail with implicit contract with + some credit. *) +let test_double_registration () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1383,8 +1446,9 @@ let double_registration () = | _ -> false) -(* with implicit contract emptied after first self-delegation *) -let double_registration_when_empty () = +(** Second self-delegation should fail with implicit contract emptied + after first self-delegation. *) +let test_double_registration_when_empty () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1423,8 +1487,9 @@ let double_registration_when_empty () = | _ -> false) -(* with implicit contract emptied then recredited after first self-delegation *) -let double_registration_when_recredited () = +(** Second self-delegation should fail with implicit contract emptied + then credited back after first self-delegation. *) +let test_double_registration_when_recredited () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1470,8 +1535,9 @@ let double_registration_when_recredited () = | _ -> false) -(* self-delegation on unrevealed contract *) -let unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee () = +(** Self-delegation on unrevealed contract. *) +let test_unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee () + = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1505,8 +1571,8 @@ let unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee () = >>=? fun i -> Assert.balance_was_debited ~loc:__LOC__ (I i) contract balance fee -(* self-delegation on revealed but not registered contract *) -let unregistered_and_revealed_self_delegate_key_init_delegation ~fee () = +(** Self-delegation on revealed but not registered contract. *) +let test_unregistered_and_revealed_self_delegate_key_init_delegation ~fee () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1544,8 +1610,8 @@ let unregistered_and_revealed_self_delegate_key_init_delegation ~fee () = >>=? fun i -> Assert.balance_was_debited ~loc:__LOC__ (I i) contract balance fee -(* self-delegation on revealed and registered contract *) -let registered_self_delegate_key_init_delegation () = +(** Self-delegation on revealed and registered contract. *) +let test_registered_self_delegate_key_init_delegation () = Context.init 1 >>=? fun (b, bootstrap_contracts) -> Incremental.begin_construction b @@ -1590,196 +1656,200 @@ let tests_delegate_registration = Test.tztest "unregistered delegate key (origination, small fee)" `Quick - (unregistered_delegate_key_init_origination ~fee:Tez.one_mutez); + (test_unregistered_delegate_key_init_origination ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key (origination, edge case fee)" `Quick - (unregistered_delegate_key_init_origination ~fee:(Tez.of_int 3_999_488)); + (test_unregistered_delegate_key_init_origination + ~fee:(Tez.of_int 3_999_488)); Test.tztest "unregistered delegate key (origination, large fee)" `Quick - (unregistered_delegate_key_init_origination ~fee:(Tez.of_int 10_000_000)); + (test_unregistered_delegate_key_init_origination + ~fee:(Tez.of_int 10_000_000)); Test.tztest "unregistered delegate key (init with delegation, small fee)" `Quick - (unregistered_delegate_key_init_delegation ~fee:Tez.one_mutez); + (test_unregistered_delegate_key_init_delegation ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key (init with delegation, max fee)" `Quick - (unregistered_delegate_key_init_delegation ~fee:Tez.max_tez); + (test_unregistered_delegate_key_init_delegation ~fee:Tez.max_tez); Test.tztest "unregistered delegate key (switch with delegation, small fee)" `Quick - (unregistered_delegate_key_switch_delegation ~fee:Tez.one_mutez); + (test_unregistered_delegate_key_switch_delegation ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key (switch with delegation, max fee)" `Quick - (unregistered_delegate_key_switch_delegation ~fee:Tez.max_tez); + (test_unregistered_delegate_key_switch_delegation ~fee:Tez.max_tez); (* credit/debit 1μꜩ, no self-delegation *) Test.tztest "unregistered delegate key - credit/debit 1μꜩ (origination, small fee)" `Quick - (unregistered_delegate_key_init_origination_credit_debit + (test_unregistered_delegate_key_init_origination_credit_debit ~fee:Tez.one_mutez ~amount:Tez.one_mutez); Test.tztest "unregistered delegate key - credit/debit 1μꜩ (origination, large fee)" `Quick - (unregistered_delegate_key_init_origination_credit_debit + (test_unregistered_delegate_key_init_origination_credit_debit ~fee:Tez.max_tez ~amount:Tez.one_mutez); Test.tztest "unregistered delegate key - credit/debit 1μꜩ (init with delegation, \ small fee)" `Quick - (unregistered_delegate_key_init_delegation_credit_debit + (test_unregistered_delegate_key_init_delegation_credit_debit ~amount:Tez.one_mutez ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key - credit/debit 1μꜩ (init with delegation, \ large fee)" `Quick - (unregistered_delegate_key_init_delegation_credit_debit + (test_unregistered_delegate_key_init_delegation_credit_debit ~amount:Tez.one_mutez ~fee:Tez.max_tez); Test.tztest "unregistered delegate key - credit/debit 1μꜩ (switch with \ delegation, small fee)" `Quick - (unregistered_delegate_key_switch_delegation_credit_debit + (test_unregistered_delegate_key_switch_delegation_credit_debit ~amount:Tez.one_mutez ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key - credit/debit 1μꜩ (switch with \ delegation, large fee)" `Quick - (unregistered_delegate_key_switch_delegation_credit_debit + (test_unregistered_delegate_key_switch_delegation_credit_debit ~amount:Tez.one_mutez ~fee:Tez.max_tez); (* credit 1μꜩ, no self-delegation *) Test.tztest "unregistered delegate key - credit 1μꜩ (origination, small fee)" `Quick - (unregistered_delegate_key_init_origination_credit + (test_unregistered_delegate_key_init_origination_credit ~fee:Tez.one_mutez ~amount:Tez.one_mutez); Test.tztest "unregistered delegate key - credit 1μꜩ (origination, edge case fee)" `Quick - (unregistered_delegate_key_init_origination_credit + (test_unregistered_delegate_key_init_origination_credit ~fee:(Tez.of_int 3_999_488) ~amount:Tez.one_mutez); Test.tztest "unregistered delegate key - credit 1μꜩ (origination, large fee)" `Quick - (unregistered_delegate_key_init_origination_credit + (test_unregistered_delegate_key_init_origination_credit ~fee:(Tez.of_int 10_000_000) ~amount:Tez.one_mutez); Test.tztest "unregistered delegate key - credit 1μꜩ (init with delegation, small \ fee)" `Quick - (unregistered_delegate_key_init_delegation_credit + (test_unregistered_delegate_key_init_delegation_credit ~amount:Tez.one_mutez ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key - credit 1μꜩ (init with delegation, large \ fee)" `Quick - (unregistered_delegate_key_init_delegation_credit + (test_unregistered_delegate_key_init_delegation_credit ~amount:Tez.one_mutez ~fee:Tez.max_tez); Test.tztest "unregistered delegate key - credit 1μꜩ (switch with delegation, \ small fee)" `Quick - (unregistered_delegate_key_switch_delegation_credit + (test_unregistered_delegate_key_switch_delegation_credit ~amount:Tez.one_mutez ~fee:Tez.one_mutez); Test.tztest "unregistered delegate key - credit 1μꜩ (switch with delegation, \ large fee)" `Quick - (unregistered_delegate_key_switch_delegation_credit + (test_unregistered_delegate_key_switch_delegation_credit ~amount:Tez.one_mutez ~fee:Tez.max_tez); (* self delegation on unrevealed and unregistered contract *) Test.tztest "unregistered and unrevealed self-delegation (small fee)" `Quick - (unregistered_and_unrevealed_self_delegate_key_init_delegation + (test_unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee:Tez.one_mutez); Test.tztest "unregistered and unrevealed self-delegation (large fee)" `Quick - (unregistered_and_unrevealed_self_delegate_key_init_delegation + (test_unregistered_and_unrevealed_self_delegate_key_init_delegation ~fee:Tez.max_tez); (* self delegation on unregistered contract *) Test.tztest "unregistered and revealed self-delegation (small fee)" `Quick - (unregistered_and_revealed_self_delegate_key_init_delegation + (test_unregistered_and_revealed_self_delegate_key_init_delegation ~fee:Tez.one_mutez); Test.tztest "unregistered and revealed self-delegation large fee)" `Quick - (unregistered_and_revealed_self_delegate_key_init_delegation + (test_unregistered_and_revealed_self_delegate_key_init_delegation ~fee:Tez.max_tez); (* self delegation on registered contract *) Test.tztest "registered and revealed self-delegation" `Quick - registered_self_delegate_key_init_delegation; + test_registered_self_delegate_key_init_delegation; (*** unregistered delegate key: failed self-delegation ***) (* no token transfer, self-delegation *) Test.tztest "failed self-delegation: no transaction" `Quick - failed_self_delegation_no_transaction; + test_failed_self_delegation_no_transaction; (* credit 1μtz, debit 1μtz, self-delegation *) Test.tztest "failed self-delegation: credit & debit 1μꜩ" `Quick - (failed_self_delegation_emptied_implicit_contract Tez.one_mutez); + (test_failed_self_delegation_emptied_implicit_contract Tez.one_mutez); (* credit 1μtz, delegate, debit 1μtz *) Test.tztest "empty delegated contract is not deleted: credit 1μꜩ, delegate & \ debit 1μꜩ" `Quick - (emptying_delegated_implicit_contract_fails Tez.one_mutez); + (test_emptying_delegated_implicit_contract_fails Tez.one_mutez); (*** valid registration ***) (* valid registration: credit 1 μꜩ, self delegation *) Test.tztest "valid delegate registration: credit 1μꜩ, self delegation (init with \ delegation)" `Quick - (valid_delegate_registration_init_delegation_credit Tez.one_mutez); + (test_valid_delegate_registration_init_delegation_credit Tez.one_mutez); Test.tztest "valid delegate registration: credit 1μꜩ, self delegation (switch \ with delegation)" `Quick - (valid_delegate_registration_switch_delegation_credit Tez.one_mutez); + (test_valid_delegate_registration_switch_delegation_credit Tez.one_mutez); (* valid registration: credit 1 μꜩ, self delegation, debit 1μꜩ *) Test.tztest "valid delegate registration: credit 1μꜩ, self delegation, debit \ 1μꜩ (init with delegation)" `Quick - (valid_delegate_registration_init_delegation_credit_debit Tez.one_mutez); + (test_valid_delegate_registration_init_delegation_credit_debit + Tez.one_mutez); Test.tztest "valid delegate registration: credit 1μꜩ, self delegation, debit \ 1μꜩ (switch with delegation)" `Quick - (valid_delegate_registration_switch_delegation_credit_debit Tez.one_mutez); + (test_valid_delegate_registration_switch_delegation_credit_debit + Tez.one_mutez); (*** double registration ***) - Test.tztest "double registration" `Quick double_registration; + Test.tztest "double registration" `Quick test_double_registration; Test.tztest "double registration when delegate account is emptied" `Quick - double_registration_when_empty; + test_double_registration_when_empty; Test.tztest "double registration when delegate account is emptied and then recredited" `Quick - double_registration_when_recredited ] + test_double_registration_when_recredited ] (******************************************************************************) (* Main *) diff --git a/src/proto_alpha/lib_protocol/test/double_baking.ml b/src/proto_alpha/lib_protocol/test/test_double_baking.ml similarity index 85% rename from src/proto_alpha/lib_protocol/test/double_baking.ml rename to src/proto_alpha/lib_protocol/test/test_double_baking.ml index f4aa22ca8bb0cd22d6a2641088680748d399ccea..ec7b4ad6ab09a4cfc5028a94a88010a10722da92 100644 --- a/src/proto_alpha/lib_protocol/test/double_baking.ml +++ b/src/proto_alpha/lib_protocol/test/test_double_baking.ml @@ -23,8 +23,13 @@ (* *) (*****************************************************************************) -(** Double baking evidence operation may happen when a baker - baked two different blocks on the same level. *) +(** Testing + ------- + Component: Protocol (double baking) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^double baking$" + Subject: Double baking evidence operation may happen when a baker + baked two different blocks on the same level. +*) open Protocol open Alpha_context @@ -53,7 +58,7 @@ let get_first_different_endorsers ctxt = Context.get_endorsers ctxt >|=? fun endorsers -> get_hd_hd endorsers (** Bake two block at the same level using the same policy (i.e. same - baker) *) + baker). *) let block_fork ?policy contracts b = let (contract_a, contract_b) = get_hd_hd contracts in Op.transaction (B b) contract_a contract_b Alpha_context.Tez.one_cent @@ -66,8 +71,8 @@ let block_fork ?policy contracts b = (****************************************************************) (** Simple scenario where two blocks are baked by a same baker and - exposed by a double baking evidence operation *) -let valid_double_baking_evidence () = + exposed by a double baking evidence operation. *) +let test_valid_double_baking_evidence () = Context.init 2 >>=? fun (b, contracts) -> Context.get_bakers (B b) @@ -92,8 +97,9 @@ let valid_double_baking_evidence () = (* The following test scenarios are supposed to raise errors. *) (****************************************************************) -(** Check that a double baking operation fails if it exposes the same two blocks *) -let same_blocks () = +(** Check that a double baking operation fails if it exposes the same two + blocks. *) +let test_same_blocks () = Context.init 2 >>=? fun (b, _contracts) -> Block.bake b @@ -110,8 +116,8 @@ let same_blocks () = >>=? fun () -> return_unit (** Check that a double baking operation exposing two blocks with - different levels fails *) -let different_levels () = + different levels fails. *) +let test_different_levels () = Context.init 2 >>=? fun (b, contracts) -> block_fork ~policy:(By_priority 0) contracts b @@ -128,9 +134,9 @@ let different_levels () = | _ -> false) -(** Check that a double baking operation exposing two yet to be baked - blocks fails *) -let too_early_double_baking_evidence () = +(** Check that a double baking operation exposing two yet-to-be-baked + blocks fails. *) +let test_too_early_double_baking_evidence () = Context.init 2 >>=? fun (b, contracts) -> block_fork ~policy:(By_priority 0) contracts b @@ -146,8 +152,8 @@ let too_early_double_baking_evidence () = false) (** Check that after [preserved_cycles + 1], it is not possible to - create a double baking operation anymore *) -let too_late_double_baking_evidence () = + create a double baking operation anymore. *) +let test_too_late_double_baking_evidence () = Context.init 2 >>=? fun (b, contracts) -> Context.get_constants (B b) @@ -169,9 +175,9 @@ let too_late_double_baking_evidence () = | _ -> false) -(** Check that an invalid double baking evidence that exposes two block - baking with same level made by different bakers fails *) -let different_delegates () = +(** Check that an invalid double baking evidence that exposes two + block baking with same level made by different bakers fails. *) +let test_different_delegates () = Context.init 2 >>=? fun (b, _) -> get_first_different_bakers (B b) @@ -190,8 +196,8 @@ let different_delegates () = | _ -> false) -let wrong_signer () = - (* Baker_2 bakes a block but baker signs it. *) +(** Baker_2 bakes a block but baker signs it. *) +let test_wrong_signer () = let header_custom_signer baker baker_2 b = Block.Forge.forge_header ~policy:(By_account baker_2) b >>=? fun header -> @@ -219,17 +225,17 @@ let tests = [ Test.tztest "valid double baking evidence" `Quick - valid_double_baking_evidence; + test_valid_double_baking_evidence; (* Should fail*) - Test.tztest "same blocks" `Quick same_blocks; - Test.tztest "different levels" `Quick different_levels; + Test.tztest "same blocks" `Quick test_same_blocks; + Test.tztest "different levels" `Quick test_different_levels; Test.tztest "too early double baking evidence" `Quick - too_early_double_baking_evidence; + test_too_early_double_baking_evidence; Test.tztest "too late double baking evidence" `Quick - too_late_double_baking_evidence; - Test.tztest "different delegates" `Quick different_delegates; - Test.tztest "wrong delegate" `Quick wrong_signer ] + test_too_late_double_baking_evidence; + Test.tztest "different delegates" `Quick test_different_delegates; + Test.tztest "wrong delegate" `Quick test_wrong_signer ] diff --git a/src/proto_alpha/lib_protocol/test/double_endorsement.ml b/src/proto_alpha/lib_protocol/test/test_double_endorsement.ml similarity index 90% rename from src/proto_alpha/lib_protocol/test/double_endorsement.ml rename to src/proto_alpha/lib_protocol/test/test_double_endorsement.ml index ca73e1b9be0f46bdf09a9fbb3927eef73677f474..a009f9522a667867d2de55297bff9eda5ada701d 100644 --- a/src/proto_alpha/lib_protocol/test/double_endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/test_double_endorsement.ml @@ -23,8 +23,13 @@ (* *) (*****************************************************************************) -(** Double endorsement evidence operation may happen when an endorser - endorsed two different blocks on the same level. *) +(** Testing + ------- + Component: Protocol (double endorsement) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^double endorsement$" + Subject: Double endorsement evidence operation may happen when an + endorser endorsed two different blocks on the same level. +*) open Protocol open Alpha_context @@ -66,7 +71,7 @@ let block_fork b = (** Simple scenario where two endorsements are made from the same delegate and exposed by a double_endorsement operation. Also verify that punishment is operated. *) -let valid_double_endorsement_evidence () = +let test_valid_double_endorsement_evidence () = Context.init 2 >>=? fun (b, _) -> block_fork b @@ -100,9 +105,9 @@ let valid_double_endorsement_evidence () = (* The following test scenarios are supposed to raise errors. *) (****************************************************************) -(** Check that an invalid double endorsement operation that exposes a valid - endorsement fails. *) -let invalid_double_endorsement () = +(** Check that an invalid double endorsement operation that exposes a + valid endorsement fails. *) +let test_invalid_double_endorsement () = Context.init 10 >>=? fun (b, _) -> Block.bake b @@ -123,7 +128,7 @@ let invalid_double_endorsement () = (** Check that a double endorsement added at the same time as a double endorsement operation fails. *) -let too_early_double_endorsement_evidence () = +let test_too_early_double_endorsement_evidence () = Context.init 2 >>=? fun (b, _) -> block_fork b @@ -146,7 +151,7 @@ let too_early_double_endorsement_evidence () = (** Check that after [preserved_cycles + 1], it is not possible to create a double_endorsement anymore. *) -let too_late_double_endorsement_evidence () = +let test_too_late_double_endorsement_evidence () = Context.init 2 >>=? fun (b, _) -> Context.get_constants (B b) @@ -174,9 +179,9 @@ let too_late_double_endorsement_evidence () = | _ -> false) -(** Check that an invalid double endorsement evidence that expose two +(** Check that an invalid double endorsement evidence that exposes two endorsements made by two different endorsers fails. *) -let different_delegates () = +let test_different_delegates () = Context.init 2 >>=? fun (b, _) -> Block.bake b @@ -210,7 +215,7 @@ let different_delegates () = (** Check that a double endorsement evidence that exposes a ill-formed endorsement fails. *) -let wrong_delegate () = +let test_wrong_delegate () = Context.init ~endorsers_per_block:1 2 >>=? fun (b, contracts) -> List.map_es (Context.Contract.manager (B b)) contracts @@ -245,18 +250,18 @@ let tests = [ Test.tztest "valid double endorsement evidence" `Quick - valid_double_endorsement_evidence; + test_valid_double_endorsement_evidence; Test.tztest "invalid double endorsement evidence" `Quick - invalid_double_endorsement; + test_invalid_double_endorsement; Test.tztest "too early double endorsement evidence" `Quick - too_early_double_endorsement_evidence; + test_too_early_double_endorsement_evidence; Test.tztest "too late double endorsement evidence" `Quick - too_late_double_endorsement_evidence; - Test.tztest "different delegates" `Quick different_delegates; - Test.tztest "wrong delegate" `Quick wrong_delegate ] + test_too_late_double_endorsement_evidence; + Test.tztest "different delegates" `Quick test_different_delegates; + Test.tztest "wrong delegate" `Quick test_wrong_delegate ] diff --git a/src/proto_alpha/lib_protocol/test/endorsement.ml b/src/proto_alpha/lib_protocol/test/test_endorsement.ml similarity index 89% rename from src/proto_alpha/lib_protocol/test/endorsement.ml rename to src/proto_alpha/lib_protocol/test/test_endorsement.ml index 58cb0354276761f9cf07b9e6305fa1038c6b30ce..54546503e2ab140fcb757e8c0b13cf0aebc666af 100644 --- a/src/proto_alpha/lib_protocol/test/endorsement.ml +++ b/src/proto_alpha/lib_protocol/test/test_endorsement.ml @@ -23,12 +23,18 @@ (* *) (*****************************************************************************) -(** Endorsing a block adds an extra layer of confidence to the Tezos' - PoS algorithm. The block endorsing operation must be included in - the following block. Each endorser possess a number of slots - corresponding to their priority. After [preserved_cycles], a reward - is given to the endorser. This reward depends on the priority of - the block that contains the endorsements. *) +(** Testing + ------- + Component: Protocol (endorsement) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^endorsement$" + Subject: Endorsing a block adds an extra layer of confidence to the + Tezos' PoS algorithm. The block endorsing operation must be + included in the following block. Each endorser possesses a + number of slots corresponding to their priority. After + [preserved_cycles], a reward is given to the endorser. This + reward depends on the priority of the block that contains + the endorsements. +*) open Protocol open Alpha_context @@ -97,8 +103,8 @@ let endorsing_power endorsers = (* Tests *) (****************************************************************) -(** Apply a single endorsement from the slot 0 endorser *) -let simple_endorsement () = +(** Apply a single endorsement from the slot 0 endorser. *) +let test_simple_endorsement () = Context.init 5 >>=? fun (b, _) -> Context.get_endorser (B b) @@ -122,7 +128,7 @@ let simple_endorsement () = (** Apply a maximum number of endorsements. An endorser can be selected twice. *) -let max_endorsement () = +let test_max_endorsement () = let endorsers_per_block = 16 in Context.init ~endorsers_per_block 32 >>=? fun (b, _) -> @@ -167,8 +173,9 @@ let max_endorsement () = delegates previous_balances -(** Check every that endorsers' balances are consistent with different priorities *) -let consistent_priorities () = +(** Check that every endorsers' balances are consistent with different + priorities. *) +let test_consistent_priorities () = let priorities = 0 -- 64 in Context.init 64 >>=? fun (b, _) -> @@ -221,8 +228,9 @@ let consistent_priorities () = priorities >>=? fun _b -> return_unit -(** Check that after [preserved_cycles] cycles the endorser gets his reward *) -let reward_retrieval () = +(** Check that after [preserved_cycles] number of cycles the endorser + gets his reward. *) +let test_reward_retrieval () = Context.init 5 >>=? fun (b, _) -> Context.get_constants (B b) @@ -258,10 +266,10 @@ let reward_retrieval () = balance reward -(** Check that after [preserved_cycles] cycles endorsers get their - reward. Two endorsers are used and they endorse in different +(** Check that after [preserved_cycles] number of cycles endorsers get + their reward. Two endorsers are used and they endorse in different cycles. *) -let reward_retrieval_two_endorsers () = +let test_reward_retrieval_two_endorsers () = Context.init 5 >>=? fun (b, _) -> Context.get_constants (B b) @@ -363,7 +371,7 @@ let reward_retrieval_two_endorsers () = balance2 security_deposit2 >>=? fun () -> - (* bake [preserved_cycles] cycles *) + (* bake [preserved_cycles] number of cycles *) List.fold_left_es (fun b _ -> Assert.balance_was_debited @@ -419,8 +427,8 @@ let reward_retrieval_two_endorsers () = (****************************************************************) (** Wrong endorsement predecessor : apply an endorsement with an - incorrect block predecessor *) -let wrong_endorsement_predecessor () = + incorrect block predecessor. *) +let test_wrong_endorsement_predecessor () = Context.init 5 >>=? fun (b, _) -> Context.get_endorser (B b) @@ -438,9 +446,9 @@ let wrong_endorsement_predecessor () = | _ -> false) -(** Invalid_endorsement_level : apply an endorsement with an incorrect - level (i.e. the predecessor level) *) -let invalid_endorsement_level () = +(** Invalid_endorsement_level: apply an endorsement with an incorrect + level (i.e. the predecessor level). *) +let test_invalid_endorsement_level () = Context.init 5 >>=? fun (b, _) -> Context.get_level (B b) @@ -458,8 +466,9 @@ let invalid_endorsement_level () = | _ -> false) -(** Duplicate endorsement : apply an endorsement that has already been done *) -let duplicate_endorsement () = +(** Duplicate endorsement : apply an endorsement that has already been + done. *) +let test_duplicate_endorsement () = Context.init 5 >>=? fun (b, _) -> Incremental.begin_construction b @@ -480,8 +489,8 @@ let duplicate_endorsement () = | _ -> false) -(** Apply a single endorsement from the slot 0 endorser *) -let not_enough_for_deposit () = +(** Apply a single endorsement from the slot 0 endorser. *) +let test_not_enough_for_deposit () = Context.init 5 ~endorsers_per_block:1 >>=? fun (b_init, contracts) -> List.map_es @@ -531,8 +540,8 @@ let not_enough_for_deposit () = | _ -> false) -(* check that a block with not enough endorsement cannot be baked *) -let endorsement_threshold () = +(** Check that a block with not enough endorsement cannot be baked. *) +let test_endorsement_threshold () = let initial_endorsers = 28 in let num_accounts = 100 in Context.init ~initial_endorsers num_accounts @@ -590,6 +599,7 @@ let endorsement_threshold () = b >>= fun _ -> return_unit +(** Fitness gap *) let test_fitness_gap () = let num_accounts = 5 in Context.init num_accounts @@ -619,21 +629,24 @@ let test_fitness_gap () = Assert.equal_int ~loc:__LOC__ res 1 >>=? fun () -> return_unit let tests = - [ Test.tztest "Simple endorsement" `Quick simple_endorsement; - Test.tztest "Maximum endorsement" `Quick max_endorsement; - Test.tztest "Consistent priorities" `Quick consistent_priorities; - Test.tztest "Reward retrieval" `Quick reward_retrieval; + [ Test.tztest "Simple endorsement" `Quick test_simple_endorsement; + Test.tztest "Maximum endorsement" `Quick test_max_endorsement; + Test.tztest "Consistent priorities" `Quick test_consistent_priorities; + Test.tztest "Reward retrieval" `Quick test_reward_retrieval; Test.tztest "Reward retrieval two endorsers" `Quick - reward_retrieval_two_endorsers; - Test.tztest "Endorsement threshold" `Quick endorsement_threshold; + test_reward_retrieval_two_endorsers; + Test.tztest "Endorsement threshold" `Quick test_endorsement_threshold; Test.tztest "Fitness gap" `Quick test_fitness_gap; (* Fail scenarios *) Test.tztest "Wrong endorsement predecessor" `Quick - wrong_endorsement_predecessor; - Test.tztest "Invalid endorsement level" `Quick invalid_endorsement_level; - Test.tztest "Duplicate endorsement" `Quick duplicate_endorsement; - Test.tztest "Not enough for deposit" `Quick not_enough_for_deposit ] + test_wrong_endorsement_predecessor; + Test.tztest + "Invalid endorsement level" + `Quick + test_invalid_endorsement_level; + Test.tztest "Duplicate endorsement" `Quick test_duplicate_endorsement; + Test.tztest "Not enough for deposit" `Quick test_not_enough_for_deposit ] diff --git a/src/proto_alpha/lib_protocol/test/fixed_point.ml b/src/proto_alpha/lib_protocol/test/test_fixed_point.ml similarity index 86% rename from src/proto_alpha/lib_protocol/test/fixed_point.ml rename to src/proto_alpha/lib_protocol/test/test_fixed_point.ml index 771c5238a7188da5b75c64339e266b4305b23aae..60cb8f18f4004f2679e2f103d34e0427a1a1a3a5 100644 --- a/src/proto_alpha/lib_protocol/test/fixed_point.ml +++ b/src/proto_alpha/lib_protocol/test/test_fixed_point.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (fixed-point decimals) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^fixed point computation$" + Subject: On fixed-point decimal numbers. +*) + open Protocol exception Fixed_point_test_error of string @@ -104,7 +111,9 @@ let arith_from_fp : (module Fixed_point_repr.Full) -> (module Arith) = end in (module Arith) -let integral_tests decimals () = +(** Roundtrips between [integral] and [Z.t] (for fixed-point + decimals). Floor and ceil preserve the integral part. *) +let test_integral_tests decimals () = let module FP = Fixed_point_repr.Make (struct let decimals = decimals end) in @@ -142,7 +151,8 @@ let integral_tests decimals () = (err "pp_integral(integral) = pp(fp(integral))") >>=? fun () -> basic_arith "integral arith" (arith_from_integral (module FP)) -let fp_zero () = +(** With zero decimal. *) +let test_fp_zero () = let decimals = 0 in let module FP = Fixed_point_repr.Make (struct let decimals = decimals @@ -164,7 +174,8 @@ let fp_zero () = >>=? fun () -> basic_arith "fp (0 decimals) arith" (arith_from_fp (module FP)) -let fp_nonzero decimals () = +(** With [decimals] decimal(s). *) +let test_fp_nonzero decimals () = let module FP = Fixed_point_repr.Make (struct let decimals = decimals end) in @@ -191,7 +202,10 @@ let fp_nonzero decimals () = FP.(ceil (add (fp (integral x)) (unsafe_fp Z.one)) = integral (Z.succ x)) (err "ceil (x + eps) = x + 1") -let fp_pp () = +(** Checking the output of the pretty-printer [FF.pp] such that + fixed-point decimal values are converted to their correct string + output according to the number of decimals. *) +let test_fp_pp () = let module FP = Fixed_point_repr.Make (struct let decimals = 3 end) in @@ -216,10 +230,10 @@ let fp_pp () = >>=? fun () -> fail_unless (FP.zero =:= "0") (err "0") let tests = - [ Test.tztest "Integral tests (0 decimals)" `Quick (integral_tests 0); - Test.tztest "Integral tests (1 decimals)" `Quick (integral_tests 1); - Test.tztest "Integral tests (10 decimals)" `Quick (integral_tests 10); - Test.tztest "FP tests (0 decimals)" `Quick fp_zero; - Test.tztest "FP tests (1 decimals)" `Quick (fp_nonzero 1); - Test.tztest "FP tests (3 decimals)" `Quick (fp_nonzero 3); - Test.tztest "FP pp tests (3 decimals)" `Quick fp_pp ] + [ Test.tztest "Integral tests (0 decimals)" `Quick (test_integral_tests 0); + Test.tztest "Integral tests (1 decimals)" `Quick (test_integral_tests 1); + Test.tztest "Integral tests (10 decimals)" `Quick (test_integral_tests 10); + Test.tztest "FP tests (0 decimals)" `Quick test_fp_zero; + Test.tztest "FP tests (1 decimals)" `Quick (test_fp_nonzero 1); + Test.tztest "FP tests (3 decimals)" `Quick (test_fp_nonzero 3); + Test.tztest "FP pp tests (3 decimals)" `Quick test_fp_pp ] diff --git a/src/proto_alpha/lib_protocol/test/gas_costs.ml b/src/proto_alpha/lib_protocol/test/test_gas_costs.ml similarity index 92% rename from src/proto_alpha/lib_protocol/test/gas_costs.ml rename to src/proto_alpha/lib_protocol/test/test_gas_costs.ml index 217c90086269da171d9bb3f94a052d40e7b2d50e..a61923572d7832794ec10fbdced9819f90586356 100644 --- a/src/proto_alpha/lib_protocol/test/gas_costs.ml +++ b/src/proto_alpha/lib_protocol/test/test_gas_costs.ml @@ -23,13 +23,18 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (gas costs) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^gas cost functions$" + Subject: Gas costs + Current limitations: for maps, sets & compare, we only test + integer comparable keys. +*) + open Protocol open Script_ir_translator -(* Basic tests related to costs. - Current limitations: for maps, sets & compare, we only test integer - comparable keys. *) - let dummy_list = list_cons 42 list_empty let forty_two = Alpha_context.Script_int.of_int 42 @@ -228,7 +233,8 @@ let cast_cost_to_z (c : Alpha_context.Gas.cost) : Z.t = Data_encoding.Binary.to_bytes_exn Alpha_context.Gas.cost_encoding c |> Data_encoding.Binary.of_bytes_exn Data_encoding.z -let check_cost_reprs_are_all_positive list () = +(** Checks that all costs are positive values. *) +let test_cost_reprs_are_all_positive list () = List.iter_es (fun (cost_name, cost) -> if Z.gt cost Z.zero then return_unit @@ -239,26 +245,27 @@ let check_cost_reprs_are_all_positive list () = (Failure (Format.asprintf "Gas cost test \"%s\" failed" cost_name)))) list -let check_costs_are_all_positive list () = +(** Checks that all costs are positive values. *) +let test_costs_are_all_positive list () = let list = List.map (fun (cost_name, cost) -> (cost_name, cast_cost_to_z cost)) list in - check_cost_reprs_are_all_positive list () + test_cost_reprs_are_all_positive list () let tests = [ Test.tztest "Positivity of interpreter costs" `Quick - (check_costs_are_all_positive all_interpreter_costs); + (test_costs_are_all_positive all_interpreter_costs); Test.tztest "Positivity of typechecking costs" `Quick - (check_costs_are_all_positive all_parsing_costs); + (test_costs_are_all_positive all_parsing_costs); Test.tztest "Positivity of unparsing costs" `Quick - (check_costs_are_all_positive all_unparsing_costs); + (test_costs_are_all_positive all_unparsing_costs); Test.tztest "Positivity of io costs" `Quick - (check_cost_reprs_are_all_positive all_io_costs) ] + (test_cost_reprs_are_all_positive all_io_costs) ] diff --git a/src/proto_alpha/lib_protocol/test/gas_levels.ml b/src/proto_alpha/lib_protocol/test/test_gas_levels.ml similarity index 80% rename from src/proto_alpha/lib_protocol/test/gas_levels.ml rename to src/proto_alpha/lib_protocol/test/test_gas_levels.ml index 7cfef33cb085ff4ddb4d67e21742803951aeb174..09ee74b21d60280f12903720d145e76b08be089c 100644 --- a/src/proto_alpha/lib_protocol/test/gas_levels.ml +++ b/src/proto_alpha/lib_protocol/test/test_gas_levels.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (Gas levels) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^gas levels$" + Subject: On gas consumption and exhaustion. +*) + open Test open Protocol open Raw_context @@ -46,7 +53,7 @@ let dummy_context () = (block.context : Environment_context.Context.t) >|= Environment.wrap_error -let detect_gas_exhaustion_in_fresh_context () = +let test_detect_gas_exhaustion_in_fresh_context () = dummy_context () >>=? fun context -> fail_unless @@ -60,14 +67,14 @@ let make_context initial_operation_gas = ( Gas_limit_repr.Arith.integral_of_int initial_operation_gas |> set_gas_limit context ) -let detect_gas_exhaustion_when_operation_gas_hits_zero () = +let test_detect_gas_exhaustion_when_operation_gas_hits_zero () = make_context 10 >>=? fun context -> fail_unless (consume_gas context (Z.of_int max_int) |> failed) (err "Fail when consuming more than the remaining operation gas.") -let detect_gas_exhaustion_when_block_gas_hits_zero () = +let test_detect_gas_exhaustion_when_block_gas_hits_zero () = make_context max_int >>=? fun context -> fail_unless @@ -95,21 +102,17 @@ let operation_gas_level context = (* because this function is called after [set_gas_limit]. *) assert false -(* - - Monitoring runs differently depending on the minimum between the +(* Monitoring runs differently depending on the minimum between the operation gas level and the block gas level. Hence, we check that - in both situations, the gas levels are correctly reported. - -*) -let monitor_operation_gas_level = monitor 100 operation_gas_level 90 + in both situations, the gas levels are correctly reported. *) +let test_monitor_operation_gas_level = monitor 100 operation_gas_level 90 -let monitor_operation_gas_level' = +let test_monitor_operation_gas_level' = monitor max_int operation_gas_level (max_int - 10) -let monitor_block_gas_level = monitor 100 block_gas_level 10399990 +let test_monitor_block_gas_level = monitor 100 block_gas_level 10399990 -let monitor_block_gas_level' = monitor max_int block_gas_level 10399990 +let test_monitor_block_gas_level' = monitor max_int block_gas_level 10399990 let quick (what, how) = tztest what `Quick how @@ -117,18 +120,18 @@ let tests = List.map quick [ ( "Detect gas exhaustion in fresh context", - detect_gas_exhaustion_in_fresh_context ); + test_detect_gas_exhaustion_in_fresh_context ); ( "Detect gas exhaustion when operation gas as hits zero", - detect_gas_exhaustion_when_operation_gas_hits_zero ); + test_detect_gas_exhaustion_when_operation_gas_hits_zero ); ( "Detect gas exhaustion when block gas as hits zero", - detect_gas_exhaustion_when_block_gas_hits_zero ); + test_detect_gas_exhaustion_when_block_gas_hits_zero ); ( "Each gas consumption impacts operation gas level (operation < block)", - monitor_operation_gas_level ); + test_monitor_operation_gas_level ); ( "Each gas consumption impacts operation gas level (block < operation)", - monitor_operation_gas_level' ); + test_monitor_operation_gas_level' ); ( "Each gas consumption has an impact on block gas level (operation < \ block)", - monitor_block_gas_level ); + test_monitor_block_gas_level ); ( "Each gas consumption has an impact on block gas level (block < \ operation)", - monitor_block_gas_level' ) ] + test_monitor_block_gas_level' ) ] diff --git a/src/proto_alpha/lib_protocol/test/gas_properties.ml b/src/proto_alpha/lib_protocol/test/test_gas_properties.ml similarity index 89% rename from src/proto_alpha/lib_protocol/test/gas_properties.ml rename to src/proto_alpha/lib_protocol/test/test_gas_properties.ml index fd2ccb48949d1f967e42d1b6374a302e4e64ef21..aa0b229af16ac0af6d5826f77fcad7e64163d5f1 100644 --- a/src/proto_alpha/lib_protocol/test/gas_properties.ml +++ b/src/proto_alpha/lib_protocol/test/test_gas_properties.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (gas properties) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^gas properties$" + Subject: Arithmetic properties around gas. +*) + open Protocol type cost_kind = @@ -75,7 +82,8 @@ let random_cost_of_kind (cost_kind : cost_kind) = let random_cost () = random_cost_of_kind (random_cost_kind ()) -let free_neutral since = +(** Consuming [Gas.free] is equivalent to consuming nothing. *) +let test_free_neutral since = let open Alpha_context in let open Environment.Error_monad in let cost = random_cost () in @@ -92,7 +100,9 @@ let free_neutral since = then ok_none else Ok (Some (cost, Gas.free)) -let consume_commutes since = +(** Consuming [cost1] then [cost2] is equivalent to consuming + [Gas.(cost1 +@ cost2)]. *) +let test_consume_commutes since = let open Alpha_context in let open Environment.Error_monad in let cost1 = random_cost () in @@ -154,8 +164,8 @@ let tests = [ Test.tztest "Gas.free is a neutral element" `Quick - (check_property (loop_check free_neutral 1000)); + (check_property (loop_check test_free_neutral 1000)); Test.tztest "Gas.consume commutes" `Quick - (check_property (loop_check consume_commutes 1000)) ] + (check_property (loop_check test_consume_commutes 1000)) ] diff --git a/src/proto_alpha/lib_protocol/test/test_helpers_rpcs.ml b/src/proto_alpha/lib_protocol/test/test_helpers_rpcs.ml index 11bf1dbb0dab1fc0cc446719ddd7c640e5d2d683..bcb017481254f2a2399ff9a0883d49be5cf60f8c 100644 --- a/src/proto_alpha/lib_protocol/test/test_helpers_rpcs.ml +++ b/src/proto_alpha/lib_protocol/test/test_helpers_rpcs.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (Helpers RPCs) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^helpers rpcs$" + Subject: On RPCs. +*) + open Protocol open Alpha_context diff --git a/src/proto_alpha/lib_protocol/test/interpretation.ml b/src/proto_alpha/lib_protocol/test/test_interpretation.ml similarity index 92% rename from src/proto_alpha/lib_protocol/test/interpretation.ml rename to src/proto_alpha/lib_protocol/test/test_interpretation.ml index da8ed08746186fb5e4ed6ab0bfe44ab3a0234d91..323100349eb12dcc1787e8f62280d6e07728f7fb 100644 --- a/src/proto_alpha/lib_protocol/test/interpretation.ml +++ b/src/proto_alpha/lib_protocol/test/test_interpretation.ml @@ -1,3 +1,11 @@ +(** Testing + ------- + Component: Protocol (interpretation) + Dependencies: src/proto_alpha/lib_protocol/script_interpreter.ml + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^interpretation$" + Subject: Interpretation of Michelson scripts +*) + open Protocol open Alpha_context open Script_interpreter @@ -68,7 +76,7 @@ let run_step ctxt code param = param (** Runs a script with an ill-typed parameter and verifies that a - Bad_contract_parameter error is returned *) + Bad_contract_parameter error is returned. *) let test_bad_contract_parameter () = test_context () >>=? fun ctx -> @@ -120,7 +128,6 @@ let test_stack_overflow () = Alcotest.failf "Unexpected error (%s)" __LOC__ (** Test the encoding/decoding of script_interpreter.ml specific errors *) - let test_json_roundtrip name testable enc v = let v' = Data_encoding.Json.destruct enc (Data_encoding.Json.construct enc v) @@ -132,6 +139,7 @@ let test_json_roundtrip name testable enc v = v' ; return_unit +(** Encoding/decoding of script_interpreter.ml specific errors. *) let test_json_roundtrip_err name e () = test_json_roundtrip name diff --git a/src/proto_alpha/lib_protocol/test/lazy_storage_diff.ml b/src/proto_alpha/lib_protocol/test/test_lazy_storage_diff.ml similarity index 100% rename from src/proto_alpha/lib_protocol/test/lazy_storage_diff.ml rename to src/proto_alpha/lib_protocol/test/test_lazy_storage_diff.ml diff --git a/src/proto_alpha/lib_protocol/test/origination.ml b/src/proto_alpha/lib_protocol/test/test_origination.ml similarity index 84% rename from src/proto_alpha/lib_protocol/test/origination.ml rename to src/proto_alpha/lib_protocol/test/test_origination.ml index efeec384e7e85b7cf9f51a88a41a051e7ac17cc2..c782e4b9801f0313eed972dd15799426de003f57 100644 --- a/src/proto_alpha/lib_protocol/test/origination.ml +++ b/src/proto_alpha/lib_protocol/test/test_origination.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (origination) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^origination$" + Subject: On originating contracts. +*) + open Protocol open Test_tez @@ -114,8 +121,7 @@ let test_origination_balances ~loc:_ ?(fee = Tez.zero) ?(credit = Tez.zero) () Assert.balance_is ~loc:__LOC__ (B b) new_contract credit (******************************************************) -(** Tests *) - +(* Tests *) (******************************************************) (** compute half of the balance and divided it by nth times *) @@ -125,27 +131,22 @@ let two_nth_of_balance incr contract nth = >>=? fun balance -> Lwt.return (Tez.( /? ) balance nth >>? fun res -> Tez.( *? ) res 2L) -(*******************) -(** Basic test *) - -(*******************) - -let balances_simple () = test_origination_balances ~loc:__LOC__ () +(** Basic test. A contract is created as well as the newly originated + contract (called from origination operation). The balance + before/after are checked. *) +let test_balances_simple () = test_origination_balances ~loc:__LOC__ () -let balances_credit () = +(** Same as [balances_simple] but credits 10 tez to the originated + contract (no fees). *) +let test_balances_credit () = test_origination_balances ~loc:__LOC__ ~credit:ten_tez () -let balances_credit_fee () = +(** Same as [balances_credit] with 10 tez fees. *) +let test_balances_credit_fee () = test_origination_balances ~loc:__LOC__ ~credit:(Tez.of_int 2) ~fee:ten_tez () -let balances_undelegatable () = test_origination_balances ~loc:__LOC__ () - -(*******************) -(** ask source contract to pay a fee when originating a contract *) - -(*******************) - -let pay_fee () = +(** Ask source contract to pay a fee when originating a contract. *) +let test_pay_fee () = register_origination ~credit:(Tez.of_int 2) ~fee:ten_tez () >>=? fun (_b, _contract, _new_contract) -> return_unit @@ -154,13 +155,9 @@ let pay_fee () = (******************************************************) -(*******************) -(** create an originate contract where the contract - does not have enough tez to pay for the fee *) - -(*******************) - -let not_tez_in_contract_to_pay_fee () = +(** Create an originate contract where the contract does not have + enough tez to pay for the fee. *) +let test_not_tez_in_contract_to_pay_fee () = Context.init 2 >>=? fun (b, contracts) -> let contract_1 = Option.get @@ List.nth contracts 0 in @@ -195,11 +192,8 @@ let not_tez_in_contract_to_pay_fee () = | _ -> false) -(***************************************************) -(* set the endorser of the block as manager/delegate of the originated - account *) -(***************************************************) - +(* Set the endorser of the block as manager/delegate of the originated + account. *) let register_contract_get_endorser () = Context.init 1 >>=? fun (b, contracts) -> @@ -209,12 +203,7 @@ let register_contract_get_endorser () = Context.get_endorser (I inc) >|=? fun (account_endorser, _slots) -> (inc, contract, account_endorser) -(*******************) -(** create multiple originated contracts and - ask contract to pay the fee *) - -(*******************) - +(* Create multiple originated contracts and ask contract to pay the fee. *) let n_originations n ?credit ?fee () = List.fold_left_es (fun new_contracts _ -> @@ -223,17 +212,14 @@ let n_originations n ?credit ?fee () = [] (1 -- n) -let multiple_originations () = +(** Create 100 originations. *) +let test_multiple_originations () = n_originations 100 ~credit:(Tez.of_int 2) ~fee:ten_tez () >>=? fun contracts -> Assert.equal_int ~loc:__LOC__ (List.length contracts) 100 -(*******************) -(** cannot originate two contracts with the same context's counter *) - -(*******************) - -let counter () = +(** Cannot originate two contracts with the same context's counter. *) +let test_counter () = Context.init 1 >>=? fun (b, contracts) -> let contract = Option.get @@ List.hd contracts in @@ -256,14 +242,13 @@ let counter () = (******************************************************) let tests = - [ Test.tztest "balances_simple" `Quick balances_simple; - Test.tztest "balances_credit" `Quick balances_credit; - Test.tztest "balances_credit_fee" `Quick balances_credit_fee; - Test.tztest "balances_undelegatable" `Quick balances_undelegatable; - Test.tztest "pay_fee" `Quick pay_fee; + [ Test.tztest "balances_simple" `Quick test_balances_simple; + Test.tztest "balances_credit" `Quick test_balances_credit; + Test.tztest "balances_credit_fee" `Quick test_balances_credit_fee; + Test.tztest "pay_fee" `Quick test_pay_fee; Test.tztest "not enough tez in contract to pay fee" `Quick - not_tez_in_contract_to_pay_fee; - Test.tztest "multiple originations" `Quick multiple_originations; - Test.tztest "counter" `Quick counter ] + test_not_tez_in_contract_to_pay_fee; + Test.tztest "multiple originations" `Quick test_multiple_originations; + Test.tztest "counter" `Quick test_counter ] diff --git a/src/proto_alpha/lib_protocol/test/qty.ml b/src/proto_alpha/lib_protocol/test/test_qty.ml similarity index 87% rename from src/proto_alpha/lib_protocol/test/qty.ml rename to src/proto_alpha/lib_protocol/test/test_qty.ml index 0aeea4bb4e8a1b9f3dc953e6e5a545c55a65f18e..94567cae4376eaab3b03bc727783447a1457c4cb 100644 --- a/src/proto_alpha/lib_protocol/test/qty.ml +++ b/src/proto_alpha/lib_protocol/test/test_qty.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (quantities) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^qty$" + Subject: On tez quantities. +*) + open Protocol let known_ok_tez_literals = @@ -73,13 +80,7 @@ let fail_msg fmt = Format.kasprintf (fail "" "") fmt let default_printer _ = "" -let equal ?(eq = ( = )) ?(prn = default_printer) ?(msg = "") x y = - if not (eq x y) then fail (prn x) (prn y) msg - -let is_none ?(msg = "") x = if x <> None then fail "None" "Some _" msg - -let is_some ?(msg = "") x = if x = None then fail "Some _" "None" msg - +(** Literals which are supposed to be parsed correctly. *) let test_known_tez_literals () = List.iter (fun (v, s) -> @@ -105,17 +106,19 @@ let test_known_tez_literals () = | Some vs' -> vs' in - equal ~prn:Tez_repr.to_string vv vs ; - equal ~prn:Tez_repr.to_string vv vs' ; - equal ~prn:(fun s -> s) (Tez_repr.to_string vv) s) + assert (vv = vs) ; + assert (vv = vs') ; + assert (Tez_repr.to_string vv = s)) known_ok_tez_literals ; List.iter (fun s -> let vs = Tez_repr.of_string s in - is_none ~msg:("Unexpected successful parsing of " ^ s) vs) + assert (vs = None)) known_bad_tez_literals ; return_unit +(** Randomly generated tez value which is printed into a string then + parsed again for their equality. *) let test_random_tez_literals () = for _ = 0 to 100_000 do let v = Random.int64 12L in @@ -127,20 +130,20 @@ let test_random_tez_literals () = let vs = Tez_repr.of_string s in let s' = String.concat "" (String.split_on_char ',' s) in let vs' = Tez_repr.of_string s' in - is_some ~msg:("Could not parse " ^ s ^ " back") vs ; - is_some ~msg:("Could not parse " ^ s ^ " back") vs' ; + assert (vs <> None) ; + assert (vs' <> None) ; ( match vs with | None -> assert false | Some vs -> let rev = Tez_repr.to_int64 vs in - equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev ) ; + assert (v = rev) ) ; match vs' with | None -> assert false | Some vs' -> let rev = Tez_repr.to_int64 vs' in - equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev + assert (v = rev) done ; return_unit diff --git a/src/proto_alpha/lib_protocol/test/reveal.ml b/src/proto_alpha/lib_protocol/test/test_reveal.ml similarity index 90% rename from src/proto_alpha/lib_protocol/test/reveal.ml rename to src/proto_alpha/lib_protocol/test/test_reveal.ml index 5833aa1aa4c32af108c3241459d9b11585129449..9579c9ad00da5494b6d09743417966b92237d7d5 100644 --- a/src/proto_alpha/lib_protocol/test/reveal.ml +++ b/src/proto_alpha/lib_protocol/test/test_reveal.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (revelation) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^revelation$" + Subject: On the reveal operation. +*) + (** Test for the [Reveal] operation. *) open Protocol @@ -30,7 +37,7 @@ open Test_tez let ten_tez = Tez.of_int 10 -let simple_reveal () = +let test_simple_reveal () = Context.init 1 >>=? fun (blk, contracts) -> let c = Option.get @@ List.hd contracts in @@ -54,7 +61,7 @@ let simple_reveal () = >|=? function | true -> () | false -> Stdlib.failwith "New contract revelation failed." -let empty_account_on_reveal () = +let test_empty_account_on_reveal () = Context.init 1 >>=? fun (blk, contracts) -> let c = Option.get @@ List.hd contracts in @@ -86,7 +93,7 @@ let empty_account_on_reveal () = | true -> Stdlib.failwith "Empty account still exists and is revealed." -let not_enough_found_for_reveal () = +let test_not_enough_found_for_reveal () = Context.init 1 >>=? fun (blk, contracts) -> let c = Option.get @@ List.hd contracts in @@ -113,9 +120,9 @@ let not_enough_found_for_reveal () = false) let tests = - [ Test.tztest "simple reveal" `Quick simple_reveal; - Test.tztest "empty account on reveal" `Quick empty_account_on_reveal; + [ Test.tztest "simple reveal" `Quick test_simple_reveal; + Test.tztest "empty account on reveal" `Quick test_empty_account_on_reveal; Test.tztest "not enough found for reveal" `Quick - not_enough_found_for_reveal ] + test_not_enough_found_for_reveal ] diff --git a/src/proto_alpha/lib_protocol/test/rolls.ml b/src/proto_alpha/lib_protocol/test/test_rolls.ml similarity index 76% rename from src/proto_alpha/lib_protocol/test/rolls.ml rename to src/proto_alpha/lib_protocol/test/test_rolls.ml index a8f2fbd418c6b5937ac2f7158fc55daffeeccafb..98f5b223fa45e8f5d3c42708348a4dbd62a51a65 100644 --- a/src/proto_alpha/lib_protocol/test/rolls.ml +++ b/src/proto_alpha/lib_protocol/test/test_rolls.ml @@ -23,6 +23,20 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (rolls) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^rolls$" + Subject: On rolls and baking rights. + A delegate has baking rights provided that it has at least + more than [token_per_rolls] tz of staking balance. This + balance corresponds to the quantity of tez that have been + delegated to it for baking rights. After a given number of + cycles where it has not made use of its baking rights, its + account will be deactivated for baker selection. To bake + again, it will have to re-activate its account. +*) + open Protocol open Alpha_context open Test_tez @@ -46,7 +60,13 @@ let get_rolls ctxt delegate = >>=? function | None -> return_nil | Some head_roll -> traverse_rolls ctxt head_roll -let check_rolls b (account : Account.t) = +(** Baking rights consistency. Assert that the number of rolls for + [account]'s pkh - equals to the number of expected rolls, i.e., + staking balance of [account] / (token_per_roll). As of protocol + version 007, token_per_roll = 8000. Note that the consistency is + verified against the value in the context, i.e. we are testing + Storage.Roll.Delegate_roll_list. We do not use RPCs here. *) +let check_rolls (b : Block.t) (account : Account.t) = Context.get_constants (B b) >>=? fun constants -> Context.Delegate.info (B b) account.pkh @@ -82,7 +102,11 @@ let check_no_rolls (b : Block.t) (account : Account.t) = get_rolls ctxt account.pkh >>=? fun rolls -> Assert.equal_int ~loc:__LOC__ (List.length rolls) 0 -let simple_staking_rights () = +(** Create a block with two initialized contracts/accounts. Assert + that the first account has a staking balance that is equal to its + own balance, and that its staking rights are consistent + (check_rolls). *) +let test_simple_staking_rights () = Context.init 2 >>=? fun (b, accounts) -> let (a1, _a2) = account_pair accounts in @@ -95,7 +119,11 @@ let simple_staking_rights () = Assert.equal_tez ~loc:__LOC__ balance info.staking_balance >>=? fun () -> check_rolls b m1 -let simple_staking_rights_after_baking () = +(** Create a block with two initialized contracts/accounts. Bake + five blocks. Assert that the staking balance of the first account + equals to its balance. Then both accounts have consistent staking + rights. *) +let test_simple_staking_rights_after_baking () = Context.init 2 >>=? fun (b, accounts) -> let (a1, a2) = account_pair accounts in @@ -151,7 +179,10 @@ let run_until_deactivation () = check_activate_staking_balance ~loc:__LOC__ ~deactivated:true b (a1, m1) >|=? fun () -> (b, ((a1, m1), balance_start), (a2, m2)) -let deactivation_then_bake () = +(** From an initialized block with two contracts/accounts, the first + one is active then deactivated. After baking, check that the + account is active again. Baking rights are ensured. *) +let test_deactivation_then_bake () = run_until_deactivation () >>=? fun ( b, ( ((_deactivated_contract, deactivated_account) as deactivated), @@ -162,7 +193,10 @@ let deactivation_then_bake () = check_activate_staking_balance ~loc:__LOC__ ~deactivated:false b deactivated >>=? fun () -> check_rolls b deactivated_account -let deactivation_then_self_delegation () = +(** A deactivated account, after baking with self-delegation, is + active again. Preservation of its balance is tested. Baking rights + are ensured. *) +let test_deactivation_then_self_delegation () = run_until_deactivation () >>=? fun ( b, ( ((deactivated_contract, deactivated_account) as deactivated), @@ -179,7 +213,10 @@ let deactivation_then_self_delegation () = Assert.equal_tez ~loc:__LOC__ start_balance balance >>=? fun () -> check_rolls b deactivated_account -let deactivation_then_empty_then_self_delegation () = +(** A deactivated account, which is emptied (into a newly created sink + account), then self-delegated, becomes activated. Its balance is + zero. Baking rights are ensured. *) +let test_deactivation_then_empty_then_self_delegation () = run_until_deactivation () >>=? fun ( b, ( ((deactivated_contract, deactivated_account) as deactivated), @@ -217,7 +254,10 @@ let deactivation_then_empty_then_self_delegation () = Assert.equal_tez ~loc:__LOC__ Tez.zero balance >>=? fun () -> check_rolls b deactivated_account -let deactivation_then_empty_then_self_delegation_then_recredit () = +(** A deactivated account, which is emptied, then self-delegated, then + re-credited of the sunk amount, becomes active again. Staking + rights remain consistent. *) +let test_deactivation_then_empty_then_self_delegation_then_recredit () = run_until_deactivation () >>=? fun ( b, ( ((deactivated_contract, deactivated_account) as deactivated), @@ -258,7 +298,13 @@ let deactivation_then_empty_then_self_delegation_then_recredit () = Assert.equal_tez ~loc:__LOC__ amount balance >>=? fun () -> check_rolls b deactivated_account -let delegation () = +(** Initialize a block with two contracts/accounts. A third new + account is also created. The first account is self-delegated. First + account sends to third one the amount of 0.5 tez. The third account + has no delegate and is consistent for baking rights. Then, it is + self-delegated and is supposed to be activated. Again, consistency + for baking rights are preserved for the first and third accounts. *) +let test_delegation () = Context.init 2 >>=? fun (b, accounts) -> let (a1, a2) = account_pair accounts in @@ -300,22 +346,22 @@ let delegation () = >>=? fun () -> check_rolls b m3 >>=? fun () -> check_rolls b m1 let tests = - [ Test.tztest "simple staking rights" `Quick simple_staking_rights; + [ Test.tztest "simple staking rights" `Quick test_simple_staking_rights; Test.tztest "simple staking rights after baking" `Quick - simple_staking_rights_after_baking; - Test.tztest "deactivation then bake" `Quick deactivation_then_bake; + test_simple_staking_rights_after_baking; + Test.tztest "deactivation then bake" `Quick test_deactivation_then_bake; Test.tztest "deactivation then self delegation" `Quick - deactivation_then_self_delegation; + test_deactivation_then_self_delegation; Test.tztest "deactivation then empty then self delegation" `Quick - deactivation_then_empty_then_self_delegation; + test_deactivation_then_empty_then_self_delegation; Test.tztest "deactivation then empty then self delegation then recredit" `Quick - deactivation_then_empty_then_self_delegation_then_recredit; - Test.tztest "delegation" `Quick delegation ] + test_deactivation_then_empty_then_self_delegation_then_recredit; + Test.tztest "delegation" `Quick test_delegation ] diff --git a/src/proto_alpha/lib_protocol/test/test_sapling.ml b/src/proto_alpha/lib_protocol/test/test_sapling.ml index dbc3db59ddd4520f9f711dc48aa0540d760cf955..727c532737e67e0d0fb7702bf9db099e13d2d473 100644 --- a/src/proto_alpha/lib_protocol/test/test_sapling.ml +++ b/src/proto_alpha/lib_protocol/test/test_sapling.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (Sapling) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^sapling$" + Subject: On the privacy-preserving library Sapling +*) + open Protocol module Raw_context_tests = struct diff --git a/src/proto_alpha/lib_protocol/test/script_gas.ml b/src/proto_alpha/lib_protocol/test/test_script_gas.ml similarity index 92% rename from src/proto_alpha/lib_protocol/test/script_gas.ml rename to src/proto_alpha/lib_protocol/test/test_script_gas.ml index 3bbd175040bd4d335d27df62aae44dce53adc203..aae948676c3057bdbe16ba2256c8f40f4cfa97a5 100644 --- a/src/proto_alpha/lib_protocol/test/script_gas.ml +++ b/src/proto_alpha/lib_protocol/test/test_script_gas.ml @@ -23,10 +23,15 @@ (* *) (*****************************************************************************) -open Protocol +(** Testing + ------- + Component: Protocol (Michelson serialization gas) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^script deserialize gas$" + Subject: Unit tests pertaining to the computation of serialization + and deserialization gas of Michelson terms. +*) -(* This file contains unit tests pertaining to the computation of - serialization and deserialization gas of Michelson terms. *) +open Protocol module Tested_terms () = struct open Micheline @@ -116,7 +121,7 @@ module Tested_terms () = struct let check_correctness () = Lwt.return @@ check_correctness () end -let check_property () = +let test_check_property () = let module T = Tested_terms () in T.check_correctness () @@ -125,4 +130,4 @@ let tests = "Script_repr.minimal_deserialize_cost is a lower bound for full \ deserialization cost" `Quick - check_property ] + test_check_property ] diff --git a/src/proto_alpha/lib_protocol/test/seed.ml b/src/proto_alpha/lib_protocol/test/test_seed.ml similarity index 91% rename from src/proto_alpha/lib_protocol/test/seed.ml rename to src/proto_alpha/lib_protocol/test/test_seed.ml index 1d18737b577f18f9c3e1140e51cd0bd7a60e6943..bf04ca8923def0f5231acc52d5c2e4542b44e3d2 100644 --- a/src/proto_alpha/lib_protocol/test/seed.ml +++ b/src/proto_alpha/lib_protocol/test/test_seed.ml @@ -23,18 +23,21 @@ (* *) (*****************************************************************************) -(** Tests about - - seed_nonce_hash included in some blocks - - revelation operation of seed_nonce that should correspond to each - seed_nonce_hash +(** Testing + ------- + Component: Protocol (seed) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^seed$" + Subject: - seed_nonce_hash included in some blocks + - revelation operation of seed_nonce that should correspond + to each seed_nonce_hash *) open Protocol open Test_tez -(** Tests that baking [blocks_per_commitment] blocks without a - [seed_nonce_hash] commitment fails with [Invalid_commitment] *) -let no_commitment () = +(** Baking [blocks_per_commitment] blocks without a [seed_nonce_hash] + commitment fails with [Invalid_commitment]. *) +let test_no_commitment () = Context.init 5 >>=? fun (b, _) -> Context.get_constants (B b) @@ -64,16 +67,16 @@ let baking_reward ctxt (b : Block.t) = Context.get_baking_reward ctxt ~priority ~endorsing_power (** Choose a baker, denote it by id. In the first cycle, make id bake only once. - Test that: - - after id bakes with a commitment the bond is frozen and the reward allocated + Check that: + - after id bakes with a commitment the bond is frozen and the reward + allocated - when id reveals the nonce too early, there's an error - when id reveals at the right time but the wrong value, there's an error - when another baker reveals correctly, it receives the tip - revealing twice produces an error - after [preserved cycles] a committer that correctly revealed - receives back the bond and the reward -*) -let revelation_early_wrong_right_twice () = + receives back the bond and the reward. *) +let test_revelation_early_wrong_right_twice () = let open Assert in Context.init 5 >>=? fun (b, _) -> @@ -218,12 +221,10 @@ let revelation_early_wrong_right_twice () = balance_is ~loc:__LOC__ (B b) id ~kind:Deposit Tez.zero >>=? fun () -> balance_is ~loc:__LOC__ (B b) id ~kind:Rewards Tez.zero -(** Tests that: - - a committer at cycle 0, which doesn't reveal at cycle 1, +(** - a committer at cycle 0, which doesn't reveal at cycle 1, at the end of the cycle 1 looses the bond and the reward - - revealing too late produces an error -*) -let revelation_missing_and_late () = + - revealing too late produces an error *) +let test_revelation_missing_and_late () = let open Context in let open Assert in Context.init 5 @@ -284,12 +285,12 @@ let revelation_missing_and_late () = false) let tests = - [ Test.tztest "no commitment" `Quick no_commitment; + [ Test.tztest "no commitment" `Quick test_no_commitment; Test.tztest "revelation_early_wrong_right_twice" `Quick - revelation_early_wrong_right_twice; + test_revelation_early_wrong_right_twice; Test.tztest "revelation_missing_and_late" `Quick - revelation_missing_and_late ] + test_revelation_missing_and_late ] diff --git a/src/proto_alpha/lib_protocol/test/transfer.ml b/src/proto_alpha/lib_protocol/test/test_transfer.ml similarity index 82% rename from src/proto_alpha/lib_protocol/test/transfer.ml rename to src/proto_alpha/lib_protocol/test/test_transfer.ml index 31ac4ab26a286a188ae92951c4990ca4c4e00720..cd8a87420a9c0da59a66ee25fab22a373b2557dc 100644 --- a/src/proto_alpha/lib_protocol/test/transfer.ml +++ b/src/proto_alpha/lib_protocol/test/test_transfer.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (transfer) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^transfer$" + Subject: Quantities transfer between contracts. +*) + open Protocol open Alpha_context open Test_tez @@ -45,8 +52,7 @@ open Test_tez This function returns a pair: - A block that added a valid operation - - a valid operation -*) + - a valid operation *) let transfer_and_check_balances ?(with_burn = false) ~loc b ?(fee = Tez.zero) ?expect_failure src dst amount = Tez.( +? ) fee amount @@ -90,8 +96,7 @@ let transfer_and_check_balances ?(with_burn = false) ~loc b ?(fee = Tez.zero) This function returns a pair: - a block that added the valid transaction - - an valid transaction -*) + - an valid transaction *) let transfer_to_itself_and_check_balances ~loc b ?(fee = Tez.zero) contract amount = Context.Contract.balance (I b) contract @@ -109,8 +114,7 @@ let transfer_to_itself_and_check_balances ~loc b ?(fee = Tez.zero) contract a destination contract and an amount one wants to transfer. This function will do a transaction from a source contract to - a destination contract with the amount "n" times. -*) + a destination contract with the amount "n" times. *) let n_transactions n b ?fee source dest amount = List.fold_left_es (fun b _ -> @@ -133,13 +137,11 @@ let register_two_contracts () = | (b, contract_1 :: contract_2 :: _) -> (b, contract_1, contract_2) -(** compute half of the balance and divided by nth - times *) - -let two_nth_of_balance incr contract nth = +(** Compute a fraction of 2/[n] of the balance of [contract] *) +let two_over_n_of_balance incr contract n = Context.Contract.balance (I incr) contract >>=? fun balance -> - Lwt.return (Tez.( /? ) balance nth >>? fun res -> Tez.( *? ) res 2L) + Lwt.return (Tez.( /? ) balance n >>? fun res -> Tez.( *? ) res 2L) (********************) (** Single transfer *) @@ -161,16 +163,15 @@ let single_transfer ?fee ?expect_failure amount = amount >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(** single transfer without fee *) -let block_with_a_single_transfer () = single_transfer Tez.one +(** Single transfer without fee. *) +let test_block_with_a_single_transfer () = single_transfer Tez.one -(** single transfer with fee *) -let block_with_a_single_transfer_with_fee () = +(** Single transfer with fee. *) +let test_block_with_a_single_transfer_with_fee () = single_transfer ~fee:Tez.one Tez.one -(** single transfer without fee *) - -let transfer_zero_tez () = +(** Single transfer without fee. *) +let test_transfer_zero_tez () = single_transfer ~expect_failure:(function | Environment.Ecoproto_error (Contract_storage.Empty_transaction _) :: _ @@ -180,12 +181,8 @@ let transfer_zero_tez () = failwith "Empty transaction should fail") Tez.zero -(********************) -(** Transfer zero tez from an implicit contract *) - -(********************) - -let transfer_zero_implicit () = +(** Transfer zero tez from an implicit contract. *) +let test_transfer_zero_implicit () = Context.init 1 >>=? fun (b, contracts) -> let dest = Option.get @@ List.nth contracts 0 in @@ -203,35 +200,27 @@ let transfer_zero_implicit () = | _ -> false) -(********************) -(** Transfer to originated contract *) - -(********************) - -let transfer_to_originate_with_fee () = +(** Transfer to originated contract. *) +let test_transfer_to_originate_with_fee () = Context.init 1 >>=? fun (b, contracts) -> let contract = Option.get @@ List.nth contracts 0 in Incremental.begin_construction b >>=? fun b -> - two_nth_of_balance b contract 10L + two_over_n_of_balance b contract 10L >>=? fun fee -> (* originated contract, paying a fee to originated this contract *) Op.origination (I b) ~fee:ten_tez contract ~script:Op.dummy_script >>=? fun (operation, new_contract) -> Incremental.add_operation b operation >>=? fun b -> - two_nth_of_balance b contract 3L + two_over_n_of_balance b contract 3L >>=? fun amount -> transfer_and_check_balances ~loc:__LOC__ b ~fee contract new_contract amount >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(********************) -(** Transfer from balance *) - -(********************) - -let transfer_amount_of_contract_balance () = +(** Transfer from balance. *) +let test_transfer_amount_of_contract_balance () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> Context.Contract.pkh contract_1 @@ -247,32 +236,24 @@ let transfer_amount_of_contract_balance () = transfer_and_check_balances ~loc:__LOC__ b contract_1 contract_2 balance >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(********************) -(** Transfer to itself *) - -(********************) - -let transfers_to_self () = +(** Transfer to oneself. *) +let test_transfers_to_self () = Context.init 1 >>=? fun (b, contracts) -> let contract = Option.get @@ List.nth contracts 0 in Incremental.begin_construction b >>=? fun b -> - two_nth_of_balance b contract 3L + two_over_n_of_balance b contract 3L >>=? fun amount -> transfer_to_itself_and_check_balances ~loc:__LOC__ b contract amount >>=? fun (b, _) -> - two_nth_of_balance b contract 5L + two_over_n_of_balance b contract 5L >>=? fun fee -> transfer_to_itself_and_check_balances ~loc:__LOC__ b ~fee contract ten_tez >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(********************) -(** Forgot to add the valid transaction into the block *) - -(********************) - -let missing_transaction () = +(** Forgot to add the valid transaction into the block. *) +let test_missing_transaction () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> (* given that contract_1 no longer has a sufficient balance to bake, @@ -281,9 +262,9 @@ let missing_transaction () = >>=? fun pkh1 -> Incremental.begin_construction b ~policy:(Block.Excluding [pkh1]) >>=? fun b -> - two_nth_of_balance b contract_1 6L + two_over_n_of_balance b contract_1 6L >>=? fun amount -> - (* do the transfer 3 times from source contract to destination contract *) + (* Do the transfer 3 times from source contract to destination contract *) n_transactions 3 b contract_1 contract_2 amount >>=? fun b -> (* do the fourth transfer from source contract to destination contract *) @@ -291,17 +272,16 @@ let missing_transaction () = >>=? fun _ -> Incremental.finalize_block b >>=? fun _ -> return_unit (********************) -(** These following tests are for different kind of contracts: - - implicit to implicit - - implicit to originated - - originated to implicit - - originated to originated *) +(* The following tests are for different kind of contracts: + - implicit to implicit + - implicit to originated + - originated to implicit + - originated to originated *) (********************) -(** Implicit to Implicit *) - -let transfer_from_implicit_to_implicit_contract () = +(** Implicit to Implicit. *) +let test_transfer_from_implicit_to_implicit_contract () = Context.init 1 >>=? fun (b, contracts) -> let bootstrap_contract = Option.get @@ List.nth contracts 0 in @@ -310,9 +290,9 @@ let transfer_from_implicit_to_implicit_contract () = Incremental.begin_construction b >>=? fun b -> let src = Contract.implicit_contract account_a.Account.pkh in - two_nth_of_balance b bootstrap_contract 3L + two_over_n_of_balance b bootstrap_contract 3L >>=? fun amount1 -> - two_nth_of_balance b bootstrap_contract 10L + two_over_n_of_balance b bootstrap_contract 10L >>=? fun fee1 -> transfer_and_check_balances ~with_burn:true @@ -323,13 +303,13 @@ let transfer_from_implicit_to_implicit_contract () = src amount1 >>=? fun (b, _) -> - (* create an implicit contract as a destination contract *) + (* Create an implicit contract as a destination contract. *) let dest = Contract.implicit_contract account_b.pkh in - two_nth_of_balance b bootstrap_contract 4L + two_over_n_of_balance b bootstrap_contract 4L >>=? fun amount2 -> - two_nth_of_balance b bootstrap_contract 10L + two_over_n_of_balance b bootstrap_contract 10L >>=? fun fee2 -> - (* transfer from implicit contract to another implicit contract *) + (* Transfer from implicit contract to another implicit contract. *) transfer_and_check_balances ~with_burn:true ~loc:__LOC__ @@ -340,9 +320,8 @@ let transfer_from_implicit_to_implicit_contract () = amount2 >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(** Implicit to originated *) - -let transfer_from_implicit_to_originated_contract () = +(** Implicit to originated. *) +let test_transfer_from_implicit_to_originated_contract () = Context.init 1 >>=? fun (b, contracts) -> let bootstrap_contract = Option.get @@ List.nth contracts 0 in @@ -351,7 +330,7 @@ let transfer_from_implicit_to_originated_contract () = let src = Contract.implicit_contract account.Account.pkh in Incremental.begin_construction b >>=? fun b -> - two_nth_of_balance b bootstrap_contract 3L + two_over_n_of_balance b bootstrap_contract 3L >>=? fun amount1 -> (* transfer the money to implicit contract *) transfer_and_check_balances @@ -367,14 +346,14 @@ let transfer_from_implicit_to_originated_contract () = >>=? fun (operation, new_contract) -> Incremental.add_operation b operation >>=? fun b -> - two_nth_of_balance b bootstrap_contract 4L + two_over_n_of_balance b bootstrap_contract 4L >>=? fun amount2 -> (* transfer from implicit contract to originated contract *) transfer_and_check_balances ~loc:__LOC__ b src new_contract amount2 >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit (********************) -(** Slow tests case *) +(* Slow tests case *) (********************) @@ -387,12 +366,14 @@ let multiple_transfer n ?fee amount = >>=? fun b -> Incremental.finalize_block b >>=? fun _ -> return_unit (** 1- Create a block with two contracts; - 2- Apply 100 transfers. *) -let block_with_multiple_transfers () = multiple_transfer 99 (Tez.of_int 1000) + 2- Apply 100 transfers. +*) +let test_block_with_multiple_transfers () = + multiple_transfer 99 (Tez.of_int 1000) (** 1- Create a block with two contracts; 2- Apply 100 transfers with 10tz fee. *) -let block_with_multiple_transfers_pay_fee () = +let test_block_with_multiple_transfers_pay_fee () = multiple_transfer 10 ~fee:ten_tez (Tez.of_int 1000) (* TODO : increase the number of operations and add a `Slow tag to it in `tests` *) @@ -400,7 +381,7 @@ let block_with_multiple_transfers_pay_fee () = (** 1- Create a block with 8 contracts; 2- Apply multiple transfers without fees; 3- Apply multiple transfers with fees. *) -let block_with_multiple_transfers_with_without_fee () = +let test_block_with_multiple_transfers_with_without_fee () = Context.init 8 >>=? fun (b, contracts) -> let contracts = Array.of_list contracts in @@ -438,12 +419,8 @@ let block_with_multiple_transfers_with_without_fee () = n_transactions 20 ~fee:ten b contracts.(1) contracts.(0) twenty >>=? fun b -> Incremental.finalize_block b >>=? fun _ -> return_unit -(********************) (** Build a chain that has 10 blocks. *) - -(********************) - -let build_a_chain () = +let test_build_a_chain () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> let ten = Tez.of_int 10 in @@ -461,12 +438,8 @@ let build_a_chain () = (* Expected error test cases *) (*********************************************************************) -(********************) -(** transfer zero tez is forbidden in implicit contract *) - -(********************) - -let empty_implicit () = +(** Transferring zero tez is forbidden in implicit contract. *) +let test_empty_implicit () = Context.init 1 >>=? fun (b, contracts) -> let dest = Option.get @@ List.nth contracts 0 in @@ -474,9 +447,9 @@ let empty_implicit () = Incremental.begin_construction b >>=? fun incr -> let src = Contract.implicit_contract account.Account.pkh in - two_nth_of_balance incr dest 3L + two_over_n_of_balance incr dest 3L >>=? fun amount -> - (* transfer zero tez from an implicit contract *) + (* Transfer zero tez from an implicit contract. *) Op.transaction (I incr) src dest amount >>=? fun op -> Incremental.add_operation incr op @@ -487,12 +460,8 @@ let empty_implicit () = | _ -> false) -(********************) -(** Balance is too low to transfer *) - -(********************) - -let balance_too_low fee () = +(** Balance is too low to transfer. *) +let test_balance_too_low fee () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> Incremental.begin_construction b @@ -529,7 +498,7 @@ let balance_too_low fee () = zero into this block; 3- Add another transfer that send tez from a zero balance contract; 4- Catch the expected error: Balance_too_low. *) -let balance_too_low_two_transfers fee () = +let test_balance_too_low_two_transfers fee () = Context.init 3 >>=? fun (b, contracts) -> let contract_1 = Option.get @@ List.nth contracts 0 in @@ -570,11 +539,7 @@ let balance_too_low_two_transfers fee () = (* contract_3 is not credited *) Assert.balance_was_credited ~loc:__LOC__ (I i) contract_3 balance3 Tez.zero -(********************) -(** The counter is already used for the previous operation *) - -(********************) - +(** The counter is already used for the previous operation. *) let invalid_counter () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> @@ -594,9 +559,9 @@ let invalid_counter () = | _ -> false) -(* same as before but different way to perform this error *) - -let add_the_same_operation_twice () = +(** Same as before but through a different way to perform this + error. *) +let test_add_the_same_operation_twice () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> Incremental.begin_construction b @@ -613,12 +578,8 @@ let add_the_same_operation_twice () = | _ -> false) -(********************) -(** check ownership *) - -(********************) - -let ownership_sender () = +(** Check ownership. *) +let test_ownership_sender () = register_two_contracts () >>=? fun (b, contract_1, contract_2) -> Incremental.begin_construction b @@ -632,9 +593,9 @@ let ownership_sender () = >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit (*********************************************************************) -(** Random transfer *) +(* Random transfer *) -(** Return a pair of minimum and maximum random number *) +(* Return a pair of minimum and maximum random number. *) let random_range (min, max) = let interv = max - min + 1 in let init = @@ -643,14 +604,14 @@ let random_range (min, max) = in init -(** Return a random contract *) +(* Return a random contract. *) let random_contract contract_array = let i = Random.int (Array.length contract_array) in contract_array.(i) (** Transfer by randomly choose amount 10 contracts, and randomly - choose the amount in the source contract *) -let random_transfer () = + choose the amount in the source contract. *) +let test_random_transfer () = Context.init 10 >>=? fun (b, contracts) -> let contracts = Array.of_list contracts in @@ -669,8 +630,8 @@ let random_transfer () = else transfer_and_check_balances ~loc:__LOC__ b source dest amount ) >>=? fun (b, _) -> Incremental.finalize_block b >>=? fun _ -> return_unit -(** Transfer random transactions *) -let random_multi_transactions () = +(** Transfer random transactions. *) +let test_random_multi_transactions () = let n = random_range (1, 100) in multiple_transfer n (Tez.of_int 100) @@ -678,81 +639,82 @@ let random_multi_transactions () = let tests = [ (* single transfer *) - Test.tztest "single transfer" `Quick block_with_a_single_transfer; + Test.tztest "single transfer" `Quick test_block_with_a_single_transfer; Test.tztest "single transfer with fee" `Quick - block_with_a_single_transfer_with_fee; + test_block_with_a_single_transfer_with_fee; (* transfer zero tez *) - Test.tztest "single transfer zero tez" `Quick transfer_zero_tez; + Test.tztest "single transfer zero tez" `Quick test_transfer_zero_tez; Test.tztest "transfer zero tez from implicit contract" `Quick - transfer_zero_implicit; + test_transfer_zero_implicit; (* transfer to originated contract *) Test.tztest "transfer to originated contract paying transaction fee" `Quick - transfer_to_originate_with_fee; + test_transfer_to_originate_with_fee; (* transfer by the balance of contract *) Test.tztest "transfer the amount from source contract balance" `Quick - transfer_amount_of_contract_balance; + test_transfer_amount_of_contract_balance; (* transfer to itself *) - Test.tztest "transfers to itself" `Quick transfers_to_self; + Test.tztest "transfers to itself" `Quick test_transfers_to_self; (* missing operation *) - Test.tztest "missing transaction" `Quick missing_transaction; + Test.tztest "missing transaction" `Quick test_missing_transaction; (* transfer from/to implicit/originated contracts*) Test.tztest "transfer from an implicit to implicit contract " `Quick - transfer_from_implicit_to_implicit_contract; + test_transfer_from_implicit_to_implicit_contract; Test.tztest "transfer from an implicit to an originated contract" `Quick - transfer_from_implicit_to_originated_contract; + test_transfer_from_implicit_to_originated_contract; (* Slow tests *) Test.tztest "block with multiple transfers" `Slow - block_with_multiple_transfers; + test_block_with_multiple_transfers; (* TODO increase the number of transaction times *) Test.tztest "block with multiple transfer paying fee" `Slow - block_with_multiple_transfers_pay_fee; + test_block_with_multiple_transfers_pay_fee; Test.tztest "block with multiple transfer without paying fee" `Slow - block_with_multiple_transfers_with_without_fee; + test_block_with_multiple_transfers_with_without_fee; (* build the chain *) - Test.tztest "build a chain" `Quick build_a_chain; + Test.tztest "build a chain" `Quick test_build_a_chain; (* Erroneous *) - Test.tztest "empty implicit" `Quick empty_implicit; + Test.tztest "empty implicit" `Quick test_empty_implicit; Test.tztest "balance too low - transfer zero" `Quick - (balance_too_low Tez.zero); - Test.tztest "balance too low" `Quick (balance_too_low Tez.one); + (test_balance_too_low Tez.zero); + Test.tztest "balance too low" `Quick (test_balance_too_low Tez.one); Test.tztest "balance too low (max fee)" `Quick - (balance_too_low Tez.max_tez); + (test_balance_too_low Tez.max_tez); Test.tztest "balance too low with two transfers - transfer zero" `Quick - (balance_too_low_two_transfers Tez.zero); + (test_balance_too_low_two_transfers Tez.zero); Test.tztest "balance too low with two transfers" `Quick - (balance_too_low_two_transfers Tez.one); + (test_balance_too_low_two_transfers Tez.one); Test.tztest "invalid_counter" `Quick invalid_counter; Test.tztest "add the same operation twice" `Quick - add_the_same_operation_twice; - Test.tztest "ownership sender" `Quick ownership_sender; + test_add_the_same_operation_twice; + Test.tztest "ownership sender" `Quick test_ownership_sender; (* Random tests *) - Test.tztest "random transfer" `Quick random_transfer; - Test.tztest "random multi transfer" `Quick random_multi_transactions ] + Test.tztest "random transfer" `Quick test_random_transfer; + Test.tztest "random multi transfer" `Quick test_random_multi_transactions + ] diff --git a/src/proto_alpha/lib_protocol/test/typechecking.ml b/src/proto_alpha/lib_protocol/test/test_typechecking.ml similarity index 98% rename from src/proto_alpha/lib_protocol/test/typechecking.ml rename to src/proto_alpha/lib_protocol/test/test_typechecking.ml index 992c2b143b78a300e8de63f08ee3e9747e62dc63..6a8d461044ad96a666a498f6a7e839ebc2639579 100644 --- a/src/proto_alpha/lib_protocol/test/typechecking.ml +++ b/src/proto_alpha/lib_protocol/test/test_typechecking.ml @@ -1,3 +1,10 @@ +(** Testing + ------- + Component: Protocol (type-checking) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^typechecking$" + Subject: Type-checking +*) + open Protocol open Alpha_context open Script_interpreter @@ -101,7 +108,8 @@ let read_file filename = let s = really_input_string ch (in_channel_length ch) in close_in ch ; s -(* Check that the custom stack overflow exception is triggered when it should be *) +(** Check that the custom stack overflow exception is triggered when + it should be. *) let test_typecheck_stack_overflow () = test_context () >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/test/voting.ml b/src/proto_alpha/lib_protocol/test/test_voting.ml similarity index 96% rename from src/proto_alpha/lib_protocol/test/voting.ml rename to src/proto_alpha/lib_protocol/test/test_voting.ml index f886f128c42618948fc05424cfe38471a626b077..8364d1a786c1c1fe6e710b2f78be04d3dcdbb48a 100644 --- a/src/proto_alpha/lib_protocol/test/voting.ml +++ b/src/proto_alpha/lib_protocol/test/test_voting.ml @@ -23,6 +23,13 @@ (* *) (*****************************************************************************) +(** Testing + ------- + Component: Protocol (voting) + Invocation: dune exec src/proto_alpha/lib_protocol/test/main.exe -- test "^voting$" + Subject: On the voting process. +*) + open Protocol (* missing stuff in Alpha_context.Vote *) @@ -195,6 +202,7 @@ let bake_until_first_block_of_next_period b = Context.Vote.get_current_period (B b) >>=? fun {remaining; _} -> Block.bake_n Int32.(add remaining one |> to_int) b +(** A normal and successful vote sequence. *) let test_successful_vote num_delegates () = let open Alpha_context in let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in @@ -536,8 +544,9 @@ let get_expected_participation_ema rolls voter_rolls old_participation_ema = in get_updated_participation_ema old_participation_ema participation -(* if not enough quorum -- get_updated_participation_ema < pr_ema_weight/den -- in testing vote, - go back to proposal period *) +(** If not enough quorum + -- get_updated_participation_ema < pr_ema_weight/den -- + in testing vote, go back to proposal period. *) let test_not_enough_quorum_in_testing_vote num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in Context.init ~min_proposal_quorum num_delegates @@ -600,8 +609,9 @@ let test_not_enough_quorum_in_testing_vote num_delegates () = (Int32.to_int new_participation_ema) >>=? fun () -> return_unit -(* if not enough quorum -- get_updated_participation_ema < pr_ema_weight/den -- in promotion vote, - go back to proposal period *) +(** If not enough quorum + -- get_updated_participation_ema < pr_ema_weight/den -- + In promotion vote, go back to proposal period. *) let test_not_enough_quorum_in_promotion_vote num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in Context.init ~min_proposal_quorum num_delegates @@ -690,6 +700,8 @@ let test_not_enough_quorum_in_promotion_vote num_delegates () = >>=? fun () -> assert_listings_not_empty b ~loc:__LOC__ >>=? fun () -> return_unit +(** Identical proposals (identified by their hash) must be counted as + one. *) let test_multiple_identical_proposals_count_as_one () = Context.init 1 >>=? fun (b, delegates) -> @@ -728,8 +740,8 @@ let test_multiple_identical_proposals_count_as_one () = | None -> failwith "%s - Missing proposal" __LOC__ -(* assumes the initial balance of allocated by Context.init is at - least 4 time the value of the tokens_per_roll constant *) +(** Assume the initial balance of allocated by Context.init is at + least 4 times the value of the tokens_per_roll constant. *) let test_supermajority_in_proposal there_is_a_winner () = let min_proposal_quorum = 0l in Context.init ~min_proposal_quorum ~initial_balances:[1L; 1L; 1L] 10 @@ -797,6 +809,9 @@ let test_supermajority_in_proposal there_is_a_winner () = else assert_period ~expected_kind:Proposal b __LOC__ ) >>=? fun () -> return_unit +(** After one voting period, if [has_quorum] then the period kind must + have been the testing vote. Otherwise, it should have remained in + place in the proposal period. *) let test_quorum_in_proposal has_quorum () = let total_tokens = 32_000_000_000_000L in let half_tokens = Int64.div total_tokens 2L in @@ -854,6 +869,8 @@ let test_quorum_in_proposal has_quorum () = else assert_period ~expected_kind:Proposal b __LOC__ ) >>=? fun () -> return_unit +(** If a supermajority is reached, then the voting period must be + reached. Otherwise, it remains in proposal period. *) let test_supermajority_in_testing_vote supermajority () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / 100)) in Context.init ~min_proposal_quorum 100 @@ -906,7 +923,8 @@ let test_supermajority_in_testing_vote supermajority () = else assert_period ~expected_kind:Proposal b __LOC__ ) >>=? fun () -> return_unit -(* test also how the selection scales: all delegates propose max proposals *) +(** Test also how the selection scales: all delegates propose max + proposals. *) let test_no_winning_proposal num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in Context.init ~min_proposal_quorum num_delegates @@ -930,9 +948,9 @@ let test_no_winning_proposal num_delegates () = (* we stay in the same proposal period because no winning proposal *) assert_period ~expected_kind:Proposal b __LOC__ >>=? fun () -> return_unit -(** Test that for the vote to pass with maximum possible participation_ema - (100%), it is sufficient for the vote quorum to be equal or greater than - the maximum quorum cap. *) +(** Vote to pass with maximum possible participation_ema (100%), it is + sufficient for the vote quorum to be equal or greater than the + maximum quorum cap. *) let test_quorum_capped_maximum num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in Context.init ~min_proposal_quorum num_delegates @@ -978,9 +996,9 @@ let test_quorum_capped_maximum num_delegates () = (* expect to move to testing because we have supermajority and enough quorum *) assert_period ~expected_kind:Testing b __LOC__ -(** Test that for the vote to pass with minimum possible participation_ema - (0%), it is sufficient for the vote quorum to be equal or greater than - the minimum quorum cap. *) +(** Vote to pass with minimum possible participation_ema (0%), it is + sufficient for the vote quorum to be equal or greater than the + minimum quorum cap. *) let test_quorum_capped_minimum num_delegates () = let min_proposal_quorum = Int32.(of_int @@ (100_00 / num_delegates)) in Context.init ~min_proposal_quorum num_delegates