Alpha/Tests:origination: typos, comments, add verifications

This commit is contained in:
bruno 2018-06-26 14:46:49 +02:00 committed by Benjamin Canou
parent 6ad461f9e2
commit 59fbdaf37b

View File

@ -13,15 +13,30 @@ open Test_tez
let ten_tez = Tez.of_int 10 let ten_tez = Tez.of_int 10
(* create a source contract, use it to create an originate contract (* create a source contract, use it to create an originate contract
with fee and credit as parameters (default to zero tez), this originate with fee and credit as parameters (default to zero tez), this originated
contract can also be spendable and/or delegatable. *) contract can also be spendable and/or delegatable. *)
let register_origination ?(fee=Tez.zero) ?(credit=Tez.zero) ?spendable ?delegatable () = let register_origination ?(fee=Tez.zero) ?(credit=Tez.zero) ?spendable ?delegatable () =
Context.init 1 >>=? fun (b, contracts) -> Context.init 1 >>=? fun (b, contracts) ->
let contract = List.hd contracts in let source = List.hd contracts in
Op.origination (B b) contract ~fee ~credit ?spendable ?delegatable Context.Contract.balance (B b) source >>=? fun source_balance ->
Op.origination (B b) source ~fee ~credit ?spendable ?delegatable
>>=? fun (operation, originated) -> >>=? fun (operation, originated) ->
Block.bake ~operation b >>=? fun b -> Block.bake ~operation b >>=? fun b ->
return (b, contract, originated) (* fee + credit + block security deposit were debited from source *)
Context.get_constants (B b) >>=? fun {parametric = {origination_burn ;
block_security_deposit}} ->
Lwt.return (
Tez.(+?) credit block_security_deposit >>?
Tez.(+?) fee >>?
Tez.(+?) origination_burn ) >>=? fun total_fee ->
Assert.balance_was_debited ~loc:__LOC__ (B b) source source_balance total_fee >>=? fun () ->
(* originated contract has been credited *)
Assert.balance_was_credited ~loc:__LOC__ (B b) originated Tez.zero credit >>=? fun () ->
(* TODO spendable or not and delegatable or not if relevant for some
test. Not the case at the moment, cf. uses of
register_origination *)
return (b, source, originated)
(* like register_origination but additionally tests that (* like register_origination but additionally tests that
@ -35,8 +50,13 @@ let test_origination_balances ~loc ?(fee=Tez.zero) ?(credit=Tez.zero)
Context.Contract.balance (B b) contract >>=? fun balance -> Context.Contract.balance (B b) contract >>=? fun balance ->
Op.origination (B b) contract ~fee ~credit ?spendable ?delegatable >>=? fun (operation, new_contract) -> Op.origination (B b) contract ~fee ~credit ?spendable ?delegatable >>=? fun (operation, new_contract) ->
(* The possible fees are: a given credit, an origination burn fee of 1 tez, (* The possible fees are: a given credit, an origination burn fee
a fee that is paid when creating an originate contract *) (constants_repr.default.origination_burn = 257 mtez),
a fee that is paid when creating an originate contract.
We also take into account a block security deposit. Note that it
is not related to origination but to the baking done in the
tests.*)
Context.get_constants (B b) >>=? fun {parametric = {origination_burn ; Context.get_constants (B b) >>=? fun {parametric = {origination_burn ;
block_security_deposit}} -> block_security_deposit}} ->
Lwt.return ( Lwt.return (
@ -126,8 +146,8 @@ let undelegatable fee () =
Context.get_endorser (B b) >>=? fun (account, _slots) -> Context.get_endorser (B b) >>=? fun (account, _slots) ->
Incremental.begin_construction b >>=? fun i -> Incremental.begin_construction b >>=? fun i ->
Context.Contract.balance (I i) new_contract >>=? fun balance -> Context.Contract.balance (I i) new_contract >>=? fun balance ->
(* FIXME need Context.Contract.delegate: cf. delegation tests Context.Contract.delegate_opt (I i) new_contract >>=? fun delegate_opt ->
Context.Contract.delegate (I i) new_contract >>=? fun delegate -> *) assert (delegate_opt = None) ;
Op.delegation ~fee (I i) new_contract (Some account) >>=? fun operation -> Op.delegation ~fee (I i) new_contract (Some account) >>=? fun operation ->
if fee > balance then if fee > balance then
(* fees cannot be paid *) (* fees cannot be paid *)
@ -146,16 +166,14 @@ let undelegatable fee () =
| Alpha_environment.Ecoproto_error (Delegate_storage.Non_delegatable_contract _) :: _ -> | Alpha_environment.Ecoproto_error (Delegate_storage.Non_delegatable_contract _) :: _ ->
return () return ()
| _ -> | _ ->
failwith "The contract is not delegatable, it fail !" failwith "The contract is not delegatable, it fails!"
in in
Incremental.add_operation ~expect_failure i operation >>=? fun i -> Incremental.add_operation ~expect_failure i operation >>=? fun i ->
(* new contracts loses the fee *) (* still no delegate *)
Context.Contract.delegate_opt (I i) new_contract >>=? fun new_delegate_opt ->
assert (new_delegate_opt = None) ;
(* new contract loses the fee *)
Assert.balance_was_debited ~loc:__LOC__ (I i) new_contract balance fee Assert.balance_was_debited ~loc:__LOC__ (I i) new_contract balance fee
(* TODO delegate has not changed : wait for delegation tests and Context.Contract.delegate
>>=? fun () ->
Context.Contract.delegate (I i) new_contract >>=? fun new_delegate ->
Assert.equal_account ~loc:__LOC__ delegate new_delegate
*)
end end
let credit fee () = let credit fee () =
@ -176,8 +194,13 @@ let credit fee () =
end end
else else
begin begin
Incremental.add_operation i operation >>=? fun i -> let not_enough_money = function
(* new contracts loses the fee *) | Alpha_environment.Ecoproto_error (Proto_alpha.Contract_storage.Balance_too_low _) :: _ ->
return ()
| _ -> failwith "The contract does not have enough money, it fails!"
in
Incremental.add_operation ~expect_failure:not_enough_money i operation >>=? fun i ->
(* new contract loses the fee *)
Assert.balance_was_debited ~loc:__LOC__ (I i) new_contract new_balance fee >>=? fun () -> Assert.balance_was_debited ~loc:__LOC__ (I i) new_contract new_balance fee >>=? fun () ->
(* contract is not credited *) (* contract is not credited *)
Assert.balance_was_credited ~loc:__LOC__ (I i) contract balance Tez.zero Assert.balance_was_credited ~loc:__LOC__ (I i) contract balance Tez.zero
@ -186,25 +209,25 @@ let credit fee () =
(* same as register_origination but for an incremental *) (* same as register_origination but for an incremental *)
let register_origination_inc ~credit () = let register_origination_inc ~credit () =
Context.init 1 >>=? fun (b, contracts) -> Context.init 1 >>=? fun (b, contracts) ->
let contract = List.hd contracts in let source_contract = List.hd contracts in
Incremental.begin_construction b >>=? fun inc -> Incremental.begin_construction b >>=? fun inc ->
Op.origination (I inc) ~credit contract >>=? fun (operation, new_contract) -> Op.origination (I inc) ~credit source_contract >>=? fun (operation, new_contract) ->
Incremental.add_operation inc operation >>=? fun inc -> Incremental.add_operation inc operation >>=? fun inc ->
return (inc, new_contract) return (inc, source_contract, new_contract)
(* Using the originate contract to create another (* Using the originate contract to create another
originate contract *) originate contract *)
let origination_contract_from_origination_contract_not_enough_fund fee () = let origination_contract_from_origination_contract_not_enough_fund fee () =
let amount = Tez.one in let amount = Tez.one in
register_origination_inc ~credit:amount () >>=? fun (inc, contract) -> register_origination_inc ~credit:amount () >>=? fun (inc, _, contract) ->
(* contract's balance is not enough to afford origination burn *) (* contract's balance is not enough to afford origination burn *)
Op.origination ~fee (I inc) ~credit:amount contract >>=? fun (operation, orig_contract) -> Op.origination ~fee (I inc) ~credit:amount contract >>=? fun (operation, orig_contract) ->
let expect_failure = function let expect_failure = function
| Alpha_environment.Ecoproto_error (Contract_storage.Balance_too_low _) :: _ -> | Alpha_environment.Ecoproto_error (Contract_storage.Balance_too_low _) :: _ ->
return () return ()
| _ -> | _ ->
failwith "The contract has not enought funds, it fail !" failwith "The contract has not enough funds, it fails!"
in in
Incremental.add_operation ~expect_failure inc operation >>=? fun inc -> Incremental.add_operation ~expect_failure inc operation >>=? fun inc ->
Context.Contract.balance (I inc) contract >>=? fun balance_aft -> Context.Contract.balance (I inc) contract >>=? fun balance_aft ->
@ -242,45 +265,49 @@ let not_tez_in_contract_to_pay_fee () =
end end
(******************************************************) (******************************************************)
(* change the manager/delegate of this account to the account (* set the endorser of the block as manager/delegate of the originated
of endorser *) account *)
let register_contract_get_ownership () = let register_contract_get_endorser () =
Context.init 1 >>=? fun (b, contracts) -> Context.init 1 >>=? fun (b, contracts) ->
let contract = List.hd contracts in let contract = List.hd contracts in
Incremental.begin_construction b >>=? fun inc -> Incremental.begin_construction b >>=? fun inc ->
Context.get_endorser (I inc) >>=? fun (account_endorser, _slots) -> Context.get_endorser (I inc) >>=? fun (account_endorser, _slots) ->
return (inc, contract, account_endorser) return (inc, contract, account_endorser)
let change_manager () = let set_manager () =
register_contract_get_ownership () >>=? fun (inc, contract, account_endorser) -> register_contract_get_endorser () >>=? fun (inc, contract, account_endorser) ->
Op.origination ~manager:account_endorser (I inc) ~credit:Tez.one contract >>=? fun (op, _) -> Op.origination ~manager:account_endorser (I inc) ~credit:Tez.one contract >>=? fun (op, orig_contract) ->
Incremental.add_operation inc op >>=? fun inc -> Incremental.add_operation inc op >>=? fun inc ->
Incremental.finalize_block inc >>=? fun _ -> Incremental.finalize_block inc >>=? fun b ->
return () (* the manager is indeed the endorser *)
Context.Contract.manager (B b) orig_contract >>=? fun manager ->
Assert.equal_pkh ~loc:__LOC__ manager.pkh account_endorser
let change_delegate () = let set_delegate () =
register_contract_get_ownership () >>=? fun (inc, contract, account_endorser) -> register_contract_get_endorser () >>=? fun (inc, contract, account_endorser) ->
Op.origination ~delegate:account_endorser (I inc) ~credit:Tez.one contract >>=? fun (op, _) -> Op.origination ~delegate:account_endorser (I inc) ~credit:Tez.one contract >>=? fun (op, orig_contract) ->
Incremental.add_operation inc op >>=? fun inc -> Incremental.add_operation inc op >>=? fun inc ->
Incremental.finalize_block inc >>=? fun _ -> Incremental.finalize_block inc >>=? fun b ->
return () (* the delegate is indeed the endorser *)
Context.Contract.delegate (B b) orig_contract >>=? fun delegate ->
Assert.equal_pkh ~loc:__LOC__ delegate account_endorser
(******************************************************) (******************************************************)
(* create a multiple originate contracts and (* create a multiple originate contracts and
ask contract to pay the fee ask contract to pay the fee *)
*)
let n_originations n ?credit ?fee ?spendable ?delegatable () = let n_originations n ?credit ?fee ?spendable ?delegatable () =
fold_left_s (fun new_contracts _ -> fold_left_s (fun new_contracts _ ->
register_origination ?fee ?credit ?spendable ?delegatable () >>=? fun (_, _, new_contract) -> register_origination ?fee ?credit ?spendable ?delegatable () >>=? fun (_b, _source, new_contract) ->
let contracts = new_contract :: new_contracts in let contracts = new_contract :: new_contracts in
return contracts return contracts
) [] (1 -- n) ) [] (1 -- n)
let multiple_originations () = let multiple_originations () =
n_originations 100 ~credit:(Tez.of_int 2) ~fee:ten_tez () >>=? fun _ -> n_originations 100 ~credit:(Tez.of_int 2) ~fee:ten_tez () >>=? fun contracts ->
return () Assert.equal_int ~loc:__LOC__ (List.length contracts) 100
(******************************************************) (******************************************************)
(* cannot originate two contracts with the same context's counter *) (* cannot originate two contracts with the same context's counter *)
@ -302,11 +329,16 @@ let counter () =
(* create an originate contract from an originate contract *) (* create an originate contract from an originate contract *)
let origination_contract_from_origination_contract () = let origination_contract_from_origination_contract () =
register_origination_inc ~credit:ten_tez () >>=? fun (inc, new_contract) -> register_origination_inc ~credit:ten_tez () >>=? fun (inc, _source_contract, new_contract) ->
Op.origination (I inc) ~credit:Tez.one new_contract >>=? fun (op2, _) -> let credit = Tez.one in
Op.origination (I inc) ~credit new_contract >>=? fun (op2, orig_contract) ->
Incremental.add_operation inc op2 >>=? fun inc -> Incremental.add_operation inc op2 >>=? fun inc ->
Incremental.finalize_block inc >>=? fun _ -> Incremental.finalize_block inc >>=? fun b ->
return () (* operation has been processed:
originated contract exists and has been credited with the right amount *)
Context.Contract.balance (B b) orig_contract >>=? fun credit0 ->
Assert.equal_tez ~loc:__LOC__ credit0 credit
(******************************************************) (******************************************************)
@ -327,8 +359,8 @@ let tests = [
Test.tztest "create origination from origination not enough fund" `Quick (origination_contract_from_origination_contract_not_enough_fund Tez.zero); Test.tztest "create origination from origination not enough fund" `Quick (origination_contract_from_origination_contract_not_enough_fund Tez.zero);
Test.tztest "not enough tez in contract to pay fee" `Quick not_tez_in_contract_to_pay_fee; Test.tztest "not enough tez in contract to pay fee" `Quick not_tez_in_contract_to_pay_fee;
Test.tztest "change manager" `Quick change_manager; Test.tztest "set manager" `Quick set_manager;
Test.tztest "change delegate" `Quick change_delegate; Test.tztest "set delegate" `Quick set_delegate;
Test.tztest "multiple originations" `Quick multiple_originations; Test.tztest "multiple originations" `Quick multiple_originations;