2017-04-06 00:40:02 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2017-04-06 00:40:02 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-01-29 04:06:47 +04:00
|
|
|
open Proto_alpha
|
|
|
|
open Tezos_context
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
let (//) = Filename.concat
|
|
|
|
|
2017-04-27 03:01:05 +04:00
|
|
|
let () = Random.self_init ()
|
|
|
|
|
2017-10-28 22:32:39 +04:00
|
|
|
let rpc_config = ref {
|
2017-11-13 19:34:00 +04:00
|
|
|
Client_rpcs.host = "localhost" ;
|
|
|
|
port = 8192 + Random.int 8192 ;
|
|
|
|
tls = false ;
|
2017-12-09 01:08:29 +04:00
|
|
|
logger = RPC_client.null_logger ;
|
2017-11-13 19:34:00 +04:00
|
|
|
}
|
2017-04-06 00:40:02 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
(* Context that does not write to alias files *)
|
|
|
|
let no_write_context config block : Client_commands.full_context = object
|
2017-12-09 01:08:29 +04:00
|
|
|
inherit Client_rpcs.http_ctxt config
|
2017-11-07 20:38:11 +04:00
|
|
|
inherit Client_commands.logger (fun _ _ -> Lwt.return_unit)
|
|
|
|
method load : type a. string -> default:a -> a Data_encoding.encoding -> a Error_monad.tzresult Lwt.t =
|
|
|
|
fun _ ~default _ -> return default
|
|
|
|
method write : type a. string ->
|
|
|
|
a ->
|
|
|
|
a Data_encoding.encoding -> unit Error_monad.tzresult Lwt.t =
|
|
|
|
fun _ _ _ -> return ()
|
|
|
|
method block = block
|
|
|
|
end
|
|
|
|
|
2017-04-06 00:40:02 +04:00
|
|
|
let activate_alpha () =
|
|
|
|
let fitness = Fitness_repr.from_int64 0L in
|
2018-02-01 20:31:08 +04:00
|
|
|
let dictator_sk = Client_keys.Secret_key_locator.create
|
|
|
|
~scheme:"unencrypted"
|
|
|
|
~location:"edsk31vznjHSSpGExDMHYASz45VZqXN4DPxvsa4hAyY8dHM28cZzp6" in
|
2018-01-29 04:16:58 +04:00
|
|
|
Tezos_client_genesis.Client_proto_main.bake
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config) (`Head 0)
|
2017-12-05 18:18:02 +04:00
|
|
|
(Activate { protocol = Client_proto_main.protocol ; validation_passes = 1 ;
|
|
|
|
fitness })
|
|
|
|
dictator_sk
|
2017-04-06 00:40:02 +04:00
|
|
|
|
2017-10-28 22:32:39 +04:00
|
|
|
let init ?(sandbox = "sandbox.json") ?rpc_port () =
|
|
|
|
begin
|
|
|
|
match rpc_port with
|
|
|
|
| None -> ()
|
|
|
|
| Some port -> rpc_config := { !rpc_config with port }
|
|
|
|
end ;
|
2017-10-02 17:12:55 +04:00
|
|
|
(* Handles relative path on OSX *)
|
|
|
|
let executable_path =
|
|
|
|
if Filename.is_relative Sys.argv.(0)
|
|
|
|
then Filename.concat (Sys.getcwd ()) Sys.argv.(0)
|
|
|
|
else Sys.argv.(0) in
|
|
|
|
Unix.chdir (Filename.dirname executable_path) ;
|
|
|
|
Unix.chdir ".." ;
|
2017-04-06 00:40:02 +04:00
|
|
|
let pid =
|
|
|
|
Node_helpers.fork_node
|
2017-10-28 22:32:39 +04:00
|
|
|
~port:!rpc_config.port
|
2017-10-02 17:12:55 +04:00
|
|
|
~sandbox:(Filename.dirname executable_path // sandbox)
|
2017-04-06 00:40:02 +04:00
|
|
|
() in
|
|
|
|
activate_alpha () >>=? fun hash ->
|
|
|
|
return (pid, hash)
|
|
|
|
|
2017-04-12 20:53:23 +04:00
|
|
|
let level block =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.level (new Client_rpcs.http_ctxt !rpc_config) block
|
2017-04-12 20:53:23 +04:00
|
|
|
|
2017-04-06 00:40:02 +04:00
|
|
|
module Account = struct
|
|
|
|
|
|
|
|
type t = {
|
|
|
|
alias : string ;
|
|
|
|
sk : secret_key ;
|
|
|
|
pk : public_key ;
|
|
|
|
pkh : public_key_hash ;
|
|
|
|
contract : Contract.t ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun { alias ; sk ; pk ; pkh ; contract } ->
|
|
|
|
(alias, sk, pk, pkh, contract)
|
|
|
|
)
|
|
|
|
(fun (alias, sk, pk, pkh, contract) ->
|
|
|
|
{ alias ; sk ; pk ; pkh ; contract })
|
|
|
|
(obj5
|
|
|
|
(req "alias" string)
|
2017-04-20 17:21:10 +04:00
|
|
|
(req "sk" Ed25519.Secret_key.encoding)
|
|
|
|
(req "pk" Ed25519.Public_key.encoding)
|
|
|
|
(req "pkh" Ed25519.Public_key_hash.encoding)
|
2017-04-06 00:40:02 +04:00
|
|
|
(req "contract" Contract.encoding))
|
|
|
|
|
|
|
|
let pp_account ppf account =
|
|
|
|
let json = Data_encoding.Json.construct encoding account in
|
|
|
|
Format.fprintf ppf "%s" (Data_encoding_ezjsonm.to_string json)
|
|
|
|
|
|
|
|
let create ?keys alias =
|
|
|
|
let sk, pk = match keys with
|
|
|
|
| Some keys -> keys
|
2017-11-27 09:13:12 +04:00
|
|
|
| None -> let _, pk, sk = Ed25519.generate_key () in sk, pk in
|
2017-04-20 17:21:10 +04:00
|
|
|
let pkh = Ed25519.Public_key.hash pk in
|
2017-04-06 00:40:02 +04:00
|
|
|
let contract = Contract.default_contract pkh in
|
|
|
|
{ alias ; contract ; pkh ; pk ; sk }
|
|
|
|
|
|
|
|
type destination = {
|
|
|
|
alias : string ;
|
|
|
|
contract : Contract.t ;
|
|
|
|
pk : public_key ;
|
|
|
|
pkh : public_key_hash ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let destination_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun { alias ; pk ; pkh ; contract } ->
|
|
|
|
(alias, pk, pkh, contract))
|
|
|
|
(fun (alias, pk, pkh, contract) ->
|
|
|
|
{ alias ; pk ; pkh ; contract })
|
|
|
|
(obj4
|
|
|
|
(req "alias" string)
|
2017-04-20 17:21:10 +04:00
|
|
|
(req "pk" Ed25519.Public_key.encoding)
|
|
|
|
(req "pkh" Ed25519.Public_key_hash.encoding)
|
2017-04-06 00:40:02 +04:00
|
|
|
(req "contract" Contract.encoding))
|
|
|
|
|
|
|
|
let pp_destination ppf destination =
|
|
|
|
let json = Data_encoding.Json.construct destination_encoding destination in
|
|
|
|
Format.fprintf ppf "%s" (Data_encoding_ezjsonm.to_string json)
|
|
|
|
|
|
|
|
let create_destination ~alias ~contract ~pk =
|
2017-04-20 17:21:10 +04:00
|
|
|
let pkh = Ed25519.Public_key.hash pk in
|
2017-04-06 00:40:02 +04:00
|
|
|
{ alias ; contract ; pk ; pkh }
|
|
|
|
|
|
|
|
type bootstrap_accounts = { b1 : t ; b2 : t ; b3 : t ; b4 : t ; b5 : t ; }
|
|
|
|
|
|
|
|
let bootstrap_accounts =
|
|
|
|
let bootstrap1_sk =
|
2018-02-01 20:31:08 +04:00
|
|
|
"edsk3gUfUPyBSfrS9CCgmCiQsTCHGkviBDusMxDJstFtojtc1zcpsh" in
|
2017-04-06 00:40:02 +04:00
|
|
|
let bootstrap2_sk =
|
2018-02-01 20:31:08 +04:00
|
|
|
"edsk39qAm1fiMjgmPkw1EgQYkMzkJezLNewd7PLNHTkr6w9XA2zdfo" in
|
2017-04-06 00:40:02 +04:00
|
|
|
let bootstrap3_sk =
|
2018-02-01 20:31:08 +04:00
|
|
|
"edsk4ArLQgBTLWG5FJmnGnT689VKoqhXwmDPBuGx3z4cvwU9MmrPZZ" in
|
2017-04-06 00:40:02 +04:00
|
|
|
let bootstrap4_sk =
|
2018-02-01 20:31:08 +04:00
|
|
|
"edsk2uqQB9AY4FvioK2YMdfmyMrer5R8mGFyuaLLFfSRo8EoyNdht3" in
|
2017-04-06 00:40:02 +04:00
|
|
|
let bootstrap5_sk =
|
2018-02-01 20:31:08 +04:00
|
|
|
"edsk4QLrcijEffxV31gGdN2HU7UpyJjA8drFoNcmnB28n89YjPNRFm" in
|
2017-04-06 00:40:02 +04:00
|
|
|
let cpt = ref 0 in
|
2018-02-01 20:31:08 +04:00
|
|
|
match List.map begin fun sk ->
|
2017-04-06 00:40:02 +04:00
|
|
|
incr cpt ;
|
2018-02-01 20:31:08 +04:00
|
|
|
let sk = Ed25519.Secret_key.of_b58check_exn sk in
|
2017-04-06 00:40:02 +04:00
|
|
|
let alias = Printf.sprintf "bootstrap%d" !cpt in
|
2018-02-01 20:31:08 +04:00
|
|
|
let pk = Ed25519.Secret_key.to_public_key sk in
|
2017-04-20 17:21:10 +04:00
|
|
|
let pkh = Ed25519.Public_key.hash pk in
|
2017-04-06 00:40:02 +04:00
|
|
|
{ alias ; contract = Contract.default_contract pkh; pkh ; pk ; sk }
|
2018-02-01 20:31:08 +04:00
|
|
|
end [ bootstrap1_sk; bootstrap2_sk; bootstrap3_sk;
|
|
|
|
bootstrap4_sk; bootstrap5_sk; ]
|
2017-04-06 00:40:02 +04:00
|
|
|
with
|
|
|
|
| [ b1 ; b2 ; b3 ; b4 ; b5 ] -> { b1 ; b2 ; b3 ; b4 ; b5 }
|
|
|
|
| _ -> assert false
|
|
|
|
|
|
|
|
let transfer
|
|
|
|
?(block = `Prevalidation)
|
2017-11-29 21:06:17 +04:00
|
|
|
?(fee = Tez.fifty_cents)
|
2017-04-06 00:40:02 +04:00
|
|
|
~(account:t)
|
|
|
|
~destination
|
|
|
|
~amount () =
|
2018-02-01 20:31:08 +04:00
|
|
|
let src_sk = Client_keys.Secret_key_locator.create
|
|
|
|
~scheme:"unencrypted"
|
|
|
|
~location:(Ed25519.Secret_key.to_b58check account.sk) in
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_context.transfer (new Client_rpcs.http_ctxt !rpc_config)
|
2017-04-06 00:40:02 +04:00
|
|
|
block
|
|
|
|
~source:account.contract
|
|
|
|
~src_pk:account.pk
|
2018-02-01 20:31:08 +04:00
|
|
|
~src_sk
|
2017-04-06 00:40:02 +04:00
|
|
|
~destination
|
|
|
|
~amount
|
|
|
|
~fee ()
|
|
|
|
|
|
|
|
let originate
|
|
|
|
?(block = `Prevalidation)
|
|
|
|
?delegate
|
2017-11-29 21:06:17 +04:00
|
|
|
?(fee = Tez.fifty_cents)
|
2017-04-06 00:40:02 +04:00
|
|
|
~(src:t)
|
|
|
|
~manager_pkh
|
|
|
|
~balance
|
2017-11-13 19:34:00 +04:00
|
|
|
() =
|
2017-04-06 00:40:02 +04:00
|
|
|
let delegatable, delegate = match delegate with
|
|
|
|
| None -> false, None
|
|
|
|
| Some delegate -> true, Some delegate in
|
2018-02-01 20:31:08 +04:00
|
|
|
let src_sk = Client_keys.Secret_key_locator.create
|
|
|
|
~scheme:"unencrypted"
|
|
|
|
~location:(Ed25519.Secret_key.to_b58check src.sk) in
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_proto_context.originate_account
|
2017-04-06 00:40:02 +04:00
|
|
|
~source:src.contract
|
|
|
|
~src_pk:src.pk
|
2018-02-01 20:31:08 +04:00
|
|
|
~src_sk
|
2017-04-06 00:40:02 +04:00
|
|
|
~manager_pkh
|
|
|
|
~balance
|
|
|
|
~delegatable
|
|
|
|
?delegate
|
2017-11-07 20:38:11 +04:00
|
|
|
~fee
|
|
|
|
block
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config)
|
2017-11-07 20:38:11 +04:00
|
|
|
()
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
let set_delegate
|
|
|
|
?(block = `Prevalidation)
|
2017-11-29 21:06:17 +04:00
|
|
|
?(fee = Tez.fifty_cents)
|
2017-04-06 00:40:02 +04:00
|
|
|
~contract
|
|
|
|
~manager_sk
|
2017-11-07 20:38:11 +04:00
|
|
|
~src_pk
|
2017-04-06 00:40:02 +04:00
|
|
|
delegate_opt =
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_proto_context.set_delegate
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config)
|
2017-11-07 20:38:11 +04:00
|
|
|
block
|
2017-04-06 00:40:02 +04:00
|
|
|
~fee
|
2017-11-07 20:38:11 +04:00
|
|
|
contract
|
|
|
|
~src_pk
|
|
|
|
~manager_sk
|
2017-04-06 00:40:02 +04:00
|
|
|
delegate_opt
|
|
|
|
|
|
|
|
let balance ?(block = `Prevalidation) (account : t) =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.balance (new Client_rpcs.http_ctxt !rpc_config)
|
2017-04-06 00:40:02 +04:00
|
|
|
block account.contract
|
|
|
|
|
|
|
|
(* TODO: gather contract related functions in a Contract module? *)
|
|
|
|
let delegate ?(block = `Prevalidation) (contract : Contract.t) =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.delegate (new Client_rpcs.http_ctxt !rpc_config)
|
2017-04-06 00:40:02 +04:00
|
|
|
block contract
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-04-12 20:53:23 +04:00
|
|
|
module Protocol = struct
|
|
|
|
|
|
|
|
open Account
|
|
|
|
|
2017-04-20 13:07:50 +04:00
|
|
|
let voting_period_kind ?(block = `Prevalidation) () =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.voting_period_kind (new Client_rpcs.http_ctxt !rpc_config) block
|
2017-04-20 13:07:50 +04:00
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
let proposals ?(block = `Prevalidation) ~src:({ pk; sk } : Account.t) proposals =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_node_rpcs.Blocks.info (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun block_info ->
|
|
|
|
Client_proto_rpcs.Context.next_level (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun next_level ->
|
|
|
|
Client_proto_rpcs.Helpers.Forge.Delegate.proposals (new Client_rpcs.http_ctxt !rpc_config) block
|
2017-04-20 10:49:14 +04:00
|
|
|
~branch:block_info.hash
|
2017-04-12 20:53:23 +04:00
|
|
|
~source:pk
|
|
|
|
~period:next_level.voting_period
|
|
|
|
~proposals
|
|
|
|
() >>=? fun bytes ->
|
2017-04-20 17:21:10 +04:00
|
|
|
let signed_bytes = Ed25519.Signature.append sk bytes in
|
2017-11-27 09:13:12 +04:00
|
|
|
return (Tezos_base.Operation.of_bytes_exn signed_bytes)
|
2017-04-12 20:53:23 +04:00
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
let ballot ?(block = `Prevalidation) ~src:({ pk; sk } : Account.t) ~proposal ballot =
|
2017-12-09 01:08:29 +04:00
|
|
|
let rpc = new Client_rpcs.http_ctxt !rpc_config in
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_node_rpcs.Blocks.info rpc block >>=? fun block_info ->
|
|
|
|
Client_proto_rpcs.Context.next_level rpc block >>=? fun next_level ->
|
|
|
|
Client_proto_rpcs.Helpers.Forge.Delegate.ballot rpc block
|
2017-04-20 10:49:14 +04:00
|
|
|
~branch:block_info.hash
|
2017-04-12 20:53:23 +04:00
|
|
|
~source:pk
|
|
|
|
~period:next_level.voting_period
|
|
|
|
~proposal
|
|
|
|
~ballot
|
|
|
|
() >>=? fun bytes ->
|
2017-04-20 17:21:10 +04:00
|
|
|
let signed_bytes = Ed25519.Signature.append sk bytes in
|
2017-11-27 09:13:12 +04:00
|
|
|
return (Tezos_base.Operation.of_bytes_exn signed_bytes)
|
2017-04-12 20:53:23 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-04-06 00:40:02 +04:00
|
|
|
module Assert = struct
|
|
|
|
|
|
|
|
include Assert
|
|
|
|
|
|
|
|
let equal_pkh ?msg pkh1 pkh2 =
|
|
|
|
let msg = Assert.format_msg msg in
|
|
|
|
let eq pkh1 pkh2 =
|
|
|
|
match pkh1, pkh2 with
|
|
|
|
| None, None -> true
|
|
|
|
| Some pkh1, Some pkh2 ->
|
2017-04-20 17:21:10 +04:00
|
|
|
Ed25519.Public_key_hash.equal pkh1 pkh2
|
2017-04-06 00:40:02 +04:00
|
|
|
| _ -> false in
|
|
|
|
let prn = function
|
|
|
|
| None -> "none"
|
2017-04-20 17:21:10 +04:00
|
|
|
| Some pkh -> Ed25519.Public_key_hash.to_hex pkh in
|
2017-04-06 00:40:02 +04:00
|
|
|
Assert.equal ?msg ~prn ~eq pkh1 pkh2
|
|
|
|
|
|
|
|
let equal_tez ?msg tz1 tz2 =
|
|
|
|
let msg = Assert.format_msg msg in
|
2017-11-29 21:06:17 +04:00
|
|
|
let eq tz1 tz2 = Int64.equal (Tez.to_mutez tz1) (Tez.to_mutez tz2) in
|
2017-04-06 00:40:02 +04:00
|
|
|
let prn = Tez.to_string in
|
|
|
|
Assert.equal ?msg ~prn ~eq tz1 tz2
|
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
let balance_equal ?block ~msg account expected_balance =
|
|
|
|
Account.balance ?block account >>=? fun actual_balance ->
|
2017-11-29 21:06:17 +04:00
|
|
|
match Tez.of_mutez expected_balance with
|
2017-04-06 00:40:02 +04:00
|
|
|
| None ->
|
|
|
|
failwith "invalid tez constant"
|
|
|
|
| Some expected_balance ->
|
2017-11-29 21:06:17 +04:00
|
|
|
return (equal_tez ~msg expected_balance actual_balance)
|
2017-04-06 00:40:02 +04:00
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
let delegate_equal ?block ~msg contract expected_delegate =
|
|
|
|
Account.delegate ?block contract >>|? fun actual_delegate ->
|
2017-11-29 21:06:17 +04:00
|
|
|
equal_pkh ~msg expected_delegate actual_delegate
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
let ecoproto_error f = function
|
2017-10-27 20:53:07 +04:00
|
|
|
| Environment.Ecoproto_error errors ->
|
2017-04-06 00:40:02 +04:00
|
|
|
List.exists f errors
|
|
|
|
| _ -> false
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
let hash op = Tezos_base.Operation.hash op
|
2017-04-27 03:01:05 +04:00
|
|
|
|
|
|
|
let failed_to_preapply ~msg ?op f =
|
|
|
|
Assert.contain_error ~msg ~f:begin function
|
2017-11-01 15:13:23 +04:00
|
|
|
| Client_baking_forge.Failed_to_preapply (op', err) ->
|
2017-11-13 19:34:00 +04:00
|
|
|
begin
|
|
|
|
match op with
|
|
|
|
| None -> true
|
|
|
|
| Some op ->
|
|
|
|
let h = hash op and h' = hash op' in
|
|
|
|
Operation_hash.equal h h'
|
|
|
|
end && List.exists (ecoproto_error f) err
|
2017-04-27 03:01:05 +04:00
|
|
|
| _ -> false
|
|
|
|
end
|
|
|
|
|
2017-04-06 00:40:02 +04:00
|
|
|
let generic_economic_error ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:(ecoproto_error (fun _ -> true))
|
|
|
|
|
|
|
|
let unknown_contract ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
2017-11-16 19:45:22 +04:00
|
|
|
| Raw_context.Storage_error _ -> true
|
2017-04-06 00:40:02 +04:00
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let non_existing_contract ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract_storage.Non_existing_contract _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let balance_too_low ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract.Balance_too_low _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let non_spendable ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract_storage.Unspendable_contract _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let inconsistent_pkh ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
2017-11-09 14:50:42 +04:00
|
|
|
| Contract_storage.Inconsistent_hash _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let inconsistent_public_key ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract_storage.Inconsistent_public_key _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let missing_public_key ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract_storage.Missing_public_key _ -> true
|
2017-04-06 00:40:02 +04:00
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let initial_amount_too_low ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract.Initial_amount_too_low _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let non_delegatable ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
|
|
|
| Contract_storage.Non_delegatable_contract _ -> true
|
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
|
|
|
let wrong_delegate ~msg =
|
|
|
|
Assert.contain_error ~msg ~f:begin ecoproto_error (function
|
2017-11-01 19:42:37 +04:00
|
|
|
| Baking.Wrong_delegate _ -> true
|
2017-04-06 00:40:02 +04:00
|
|
|
| _ -> false)
|
|
|
|
end
|
|
|
|
|
2017-04-12 20:53:23 +04:00
|
|
|
let check_protocol ?msg ~block h =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_node_rpcs.Blocks.protocol (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun block_proto ->
|
2017-04-12 20:53:23 +04:00
|
|
|
return @@ Assert.equal
|
|
|
|
?msg:(Assert.format_msg msg)
|
|
|
|
~prn:Protocol_hash.to_b58check
|
|
|
|
~eq:Protocol_hash.equal
|
|
|
|
block_proto h
|
|
|
|
|
2017-04-20 13:07:50 +04:00
|
|
|
let check_voting_period_kind ?msg ~block kind =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.voting_period_kind (new Client_rpcs.http_ctxt !rpc_config) block
|
2017-04-20 13:07:50 +04:00
|
|
|
>>=? fun current_kind ->
|
|
|
|
return @@ Assert.equal
|
|
|
|
?msg:(Assert.format_msg msg)
|
|
|
|
current_kind kind
|
|
|
|
|
2017-04-06 00:40:02 +04:00
|
|
|
end
|
|
|
|
|
2017-11-01 19:42:37 +04:00
|
|
|
module Baking = struct
|
2017-04-06 00:40:02 +04:00
|
|
|
|
2017-11-20 06:37:06 +04:00
|
|
|
let bake block (contract: Account.t) operations =
|
2017-04-27 03:01:05 +04:00
|
|
|
let seed_nonce =
|
|
|
|
match Nonce.of_bytes @@
|
|
|
|
Sodium.Random.Bigbytes.generate Constants.nonce_length with
|
|
|
|
| Error _ -> assert false
|
|
|
|
| Ok nonce -> nonce in
|
2017-04-06 00:40:02 +04:00
|
|
|
let seed_nonce_hash = Nonce.hash seed_nonce in
|
2018-02-01 20:31:08 +04:00
|
|
|
let src_sk = Client_keys.Secret_key_locator.create
|
|
|
|
~scheme:"unencrypted"
|
|
|
|
~location:(Ed25519.Secret_key.to_b58check contract.sk) in
|
2017-11-01 15:13:23 +04:00
|
|
|
Client_baking_forge.forge_block
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config)
|
2017-04-06 00:40:02 +04:00
|
|
|
block
|
2017-04-27 03:01:05 +04:00
|
|
|
~operations
|
|
|
|
~force:true
|
|
|
|
~best_effort:false
|
|
|
|
~sort:false
|
|
|
|
~priority:(`Auto (contract.pkh, Some 1024, false))
|
2017-04-06 00:40:02 +04:00
|
|
|
~seed_nonce_hash
|
2018-02-01 20:31:08 +04:00
|
|
|
~src_sk
|
2017-04-27 03:01:05 +04:00
|
|
|
()
|
|
|
|
|
|
|
|
let endorsement_reward block =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Header.priority (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun prio ->
|
2017-11-01 19:42:37 +04:00
|
|
|
Baking.endorsement_reward ~block_priority:prio >|=
|
2017-10-27 20:53:07 +04:00
|
|
|
Environment.wrap_error >>|?
|
2017-11-29 21:06:17 +04:00
|
|
|
Tez.to_mutez
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Endorse = struct
|
|
|
|
|
2017-04-17 22:53:23 +04:00
|
|
|
let forge_endorsement
|
2017-04-06 00:40:02 +04:00
|
|
|
block
|
|
|
|
src_sk
|
|
|
|
source
|
|
|
|
slot =
|
2017-11-20 06:37:06 +04:00
|
|
|
let block = Client_rpcs.last_baked_block block in
|
2017-12-09 01:08:29 +04:00
|
|
|
let rpc = new Client_rpcs.http_ctxt !rpc_config in
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_node_rpcs.Blocks.info rpc block >>=? fun { hash ; _ } ->
|
|
|
|
Client_proto_rpcs.Helpers.Forge.Delegate.endorsement rpc
|
2017-04-06 00:40:02 +04:00
|
|
|
block
|
2017-04-20 10:49:14 +04:00
|
|
|
~branch:hash
|
2017-04-06 00:40:02 +04:00
|
|
|
~source
|
2017-04-17 22:53:23 +04:00
|
|
|
~block:hash
|
2017-04-06 00:40:02 +04:00
|
|
|
~slot:slot
|
|
|
|
() >>=? fun bytes ->
|
2017-04-20 17:21:10 +04:00
|
|
|
let signed_bytes = Ed25519.Signature.append src_sk bytes in
|
2017-11-27 09:13:12 +04:00
|
|
|
return (Tezos_base.Operation.of_bytes_exn signed_bytes)
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
let signing_slots
|
|
|
|
?(max_priority = 1024)
|
|
|
|
block
|
|
|
|
delegate
|
|
|
|
level =
|
|
|
|
Client_proto_rpcs.Helpers.Rights.endorsement_rights_for_delegate
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config) ~max_priority ~first_level:level ~last_level:level
|
2017-04-06 00:40:02 +04:00
|
|
|
block delegate () >>=? fun possibilities ->
|
|
|
|
let slots =
|
|
|
|
List.map (fun (_,slot) -> slot)
|
|
|
|
@@ List.filter (fun (l, _) -> l = level) possibilities in
|
|
|
|
return slots
|
|
|
|
|
|
|
|
let endorse
|
|
|
|
?slot
|
|
|
|
(contract : Account.t)
|
|
|
|
block =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.next_level (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun { level } ->
|
2017-04-06 00:40:02 +04:00
|
|
|
begin
|
|
|
|
match slot with
|
|
|
|
| Some slot -> return slot
|
|
|
|
| None -> begin
|
|
|
|
signing_slots
|
|
|
|
block contract.Account.pkh
|
|
|
|
level >>=? function
|
|
|
|
| slot::_ -> return slot
|
|
|
|
| [] ->
|
|
|
|
failwith "No slot found at level %a" Raw_level.pp level
|
|
|
|
end
|
|
|
|
end >>=? fun slot ->
|
2017-04-17 22:53:23 +04:00
|
|
|
forge_endorsement block contract.sk contract.pk slot
|
2017-04-06 00:40:02 +04:00
|
|
|
|
|
|
|
(* FIXME @vb: I don't understand this function, copied from @cago. *)
|
2017-04-20 10:49:14 +04:00
|
|
|
let endorsers_list block =
|
2017-04-06 00:40:02 +04:00
|
|
|
let get_endorser_list result (account : Account.t) level block =
|
|
|
|
Client_proto_rpcs.Helpers.Rights.endorsement_rights_for_delegate
|
2017-12-09 01:08:29 +04:00
|
|
|
(new Client_rpcs.http_ctxt !rpc_config) block account.pkh
|
2017-04-06 00:40:02 +04:00
|
|
|
~max_priority:16
|
|
|
|
~first_level:level
|
|
|
|
~last_level:level () >>|? fun slots ->
|
|
|
|
List.iter (fun (_,slot) -> result.(slot) <- account) slots
|
|
|
|
in
|
2017-04-20 10:49:14 +04:00
|
|
|
let { Account.b1 ; b2 ; b3 ; b4 ; b5 } = Account.bootstrap_accounts in
|
2017-04-06 00:40:02 +04:00
|
|
|
let result = Array.make 16 b1 in
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.level (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun level ->
|
2017-04-06 00:40:02 +04:00
|
|
|
let level = Raw_level.succ @@ level.level in
|
|
|
|
get_endorser_list result b1 level block >>=? fun () ->
|
|
|
|
get_endorser_list result b2 level block >>=? fun () ->
|
|
|
|
get_endorser_list result b3 level block >>=? fun () ->
|
|
|
|
get_endorser_list result b4 level block >>=? fun () ->
|
|
|
|
get_endorser_list result b5 level block >>=? fun () ->
|
|
|
|
return result
|
|
|
|
|
|
|
|
let endorsement_rights
|
2017-11-13 19:34:00 +04:00
|
|
|
?(max_priority = 1024)
|
|
|
|
(contract : Account.t) block =
|
2017-12-09 01:08:29 +04:00
|
|
|
let rpc = new Client_rpcs.http_ctxt !rpc_config in
|
2017-11-07 20:38:11 +04:00
|
|
|
Client_proto_rpcs.Context.level rpc block >>=? fun level ->
|
2017-04-06 00:40:02 +04:00
|
|
|
let delegate = contract.pkh in
|
|
|
|
let level = level.level in
|
|
|
|
Client_proto_rpcs.Helpers.Rights.endorsement_rights_for_delegate
|
2017-11-07 20:38:11 +04:00
|
|
|
rpc
|
2017-04-06 00:40:02 +04:00
|
|
|
~max_priority
|
|
|
|
~first_level:level
|
|
|
|
~last_level:level
|
|
|
|
block delegate ()
|
|
|
|
|
|
|
|
end
|
2017-04-10 15:01:22 +04:00
|
|
|
|
|
|
|
let display_level block =
|
2017-12-09 01:08:29 +04:00
|
|
|
Client_proto_rpcs.Context.level (new Client_rpcs.http_ctxt !rpc_config) block >>=? fun lvl ->
|
2017-04-10 15:01:22 +04:00
|
|
|
Format.eprintf "Level: %a@." Level.pp_full lvl ;
|
|
|
|
return ()
|