diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 4d02f0836..0a8be9e7a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -129,6 +129,9 @@ module Contract = struct Alpha_services.Contract.manager_key rpc_ctxt ctxt contract >>=? fun (_, res) -> return (res <> None) + let delegate_opt ctxt contract = + Alpha_services.Contract.delegate_opt rpc_ctxt ctxt contract + end module Delegate = struct diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index 04db8b6b8..56b4eb27a 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -42,6 +42,8 @@ module Contract : sig val manager: t -> Contract.t -> Account.t tzresult Lwt.t val is_manager_key_revealed: t -> Contract.t -> bool tzresult Lwt.t + val delegate_opt: t -> Contract.t -> public_key_hash option tzresult Lwt.t + end module Delegate : sig diff --git a/src/proto_alpha/lib_protocol/test/rolls.ml b/src/proto_alpha/lib_protocol/test/rolls.ml index 0a3a71d6f..899c0100c 100644 --- a/src/proto_alpha/lib_protocol/test/rolls.ml +++ b/src/proto_alpha/lib_protocol/test/rolls.ml @@ -39,6 +39,14 @@ let check_rolls b (account:Account.t) = get_rolls ctxt account.pkh >>=? fun rolls -> Assert.equal_int ~loc:__LOC__ (List.length rolls) (Int64.to_int expected_rolls) +let check_no_rolls (b : Block.t) (account:Account.t) = + Raw_context.prepare b.context + ~level:b.header.shell.level + ~timestamp:b.header.shell.timestamp + ~fitness:b.header.shell.fitness >>= wrap >>=? fun ctxt -> + get_rolls ctxt account.pkh >>=? fun rolls -> + Assert.equal_int ~loc:__LOC__ (List.length rolls) 0 + let simple_staking_rights () = Context.init 2 >>=? fun (b,accounts) -> let (a1, a2) = account_pair accounts in @@ -121,9 +129,56 @@ let deactivation_then_self_delegation () = Assert.equal_tez ~loc:__LOC__ start_balance balance >>=? fun () -> check_rolls b deactivated_account + +let delegation () = + Context.init 2 >>=? fun (b,accounts) -> + let (a1, a2) = account_pair accounts in + let m3 = Account.new_account () in + Account.add_account m3; + + Context.Contract.balance (B b) a1 >>=? fun balance -> + Context.Contract.manager (B b) a1 >>=? fun m1 -> + Context.Contract.manager (B b) a2 >>=? fun m2 -> + let a3 = Contract.implicit_contract m3.pkh in + + Context.Contract.delegate_opt (B b) a1 >>=? fun delegate -> + begin + match delegate with + | None -> assert false + | Some pkh -> + assert (Signature.Public_key_hash.equal pkh m1.pkh) + end; + + Op.transaction (B b) a1 a3 balance >>=? fun transact -> + + Block.bake ~policy:(By_account m2.pkh) b ~operation:transact >>=? fun b -> + + Context.Contract.delegate_opt (B b) a3 >>=? fun delegate -> + begin + match delegate with + | None -> () + | Some _ -> assert false + end; + check_no_rolls b m3 >>=? fun () -> + + Op.delegation (B b) a3 (Some m3.pkh) >>=? fun delegation -> + Block.bake ~policy:(By_account m2.pkh) b ~operation:delegation >>=? fun b -> + + Context.Contract.delegate_opt (B b) a3 >>=? fun delegate -> + begin + match delegate with + | None -> assert false + | Some pkh -> + assert (Signature.Public_key_hash.equal pkh m3.pkh) + end; + check_activate_staking_balance ~loc:__LOC__ ~deactivated:false b (a3,m3) >>=? 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 after baking" `Quick (simple_staking_rights_after_baking) ; Test.tztest "deactivation then bake" `Quick (deactivation_then_bake) ; Test.tztest "deactivation then self delegation" `Quick (deactivation_then_self_delegation) ; + Test.tztest "delegation" `Quick (delegation) ; ]