From 2b938802f38c0a26a9827d9076adaf36e5e72d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 28 Feb 2017 02:48:51 +0100 Subject: [PATCH] Alphanet: trivial faucet --- .../embedded/alpha/client_proto_context.ml | 36 +++++++++++++++---- .../embedded/alpha/client_proto_rpcs.ml | 4 +++ .../embedded/alpha/client_proto_rpcs.mli | 6 ++++ src/proto/alpha/apply.ml | 34 +++++++++++++----- src/proto/alpha/constants_repr.ml | 2 ++ src/proto/alpha/operation_repr.ml | 21 ++++++++++- src/proto/alpha/operation_repr.mli | 4 +++ src/proto/alpha/tezos_context.mli | 5 +++ 8 files changed, 97 insertions(+), 15 deletions(-) diff --git a/src/client/embedded/alpha/client_proto_context.ml b/src/client/embedded/alpha/client_proto_context.ml index 1692d9454..9de5d12eb 100644 --- a/src/client/embedded/alpha/client_proto_context.ml +++ b/src/client/embedded/alpha/client_proto_context.ml @@ -89,14 +89,16 @@ let transfer cctxt cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () -> return contracts -let originate cctxt ?force ~block ~src_sk bytes = +let originate cctxt ?force ~block ?signature bytes = cctxt.Client_commands.message "Forged the raw origination frame." >>= fun () -> + let signed_bytes = + match signature with + | None -> bytes + | Some signature -> MBytes.concat bytes signature in Client_node_rpcs.Blocks.predecessor cctxt block >>= fun predecessor -> - let signature = Ed25519.sign src_sk bytes in - let signed_bytes = MBytes.concat bytes signature in let oph = Operation_hash.hash_bytes [ signed_bytes ] in Client_proto_rpcs.Helpers.apply_operation cctxt block - predecessor oph bytes (Some signature) >>=? function + predecessor oph bytes signature >>=? function | [ contract ] -> Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun injected_oph -> assert (Operation_hash.equal oph injected_oph) ; @@ -118,7 +120,8 @@ let originate_account cctxt ~net ~source ~sourcePubKey:src_pk ~managerPubKey:manager_pkh ~counter ~balance ?spendable ?delegatable ?delegatePubKey:delegate ~fee () >>=? fun bytes -> - originate cctxt ?force ~block ~src_sk bytes + let signature = Ed25519.sign src_sk bytes in + originate cctxt ?force ~block ~signature bytes let originate_contract cctxt block ?force @@ -136,7 +139,14 @@ let originate_contract cctxt ~counter ~balance ~spendable:!spendable ?delegatable ?delegatePubKey ~script:(code, init) ~fee () >>=? fun bytes -> - originate cctxt ?force ~block ~src_sk bytes + let signature = Ed25519.sign src_sk bytes in + originate cctxt ?force ~block ~signature bytes + +let faucet cctxt block ?force ~manager_pkh () = + Client_node_rpcs.Blocks.net cctxt block >>= fun net -> + Client_proto_rpcs.Helpers.Forge.Anonymous.faucet cctxt block + ~net ~id:manager_pkh () >>=? fun bytes -> + originate cctxt ?force ~block bytes let group = { Cli_entries.name = "context" ; @@ -246,6 +256,20 @@ let commands () = ~delegatable:!delegatable ?delegatePubKey:delegate ~code ~init:!init ()) >>= Client_proto_rpcs.handle_error cctxt >>= fun contract -> RawContractAlias.add cctxt neu contract) ; + command ~group ~desc: "open a new (free) account" + ~args: ([ fee_arg ; delegate_arg ; force_arg ] + @ delegatable_args @ spendable_args) + (prefixes [ "originate" ; "free" ; "account" ] + @@ RawContractAlias.fresh_alias_param + ~name: "new" ~desc: "name of the new contract" + @@ prefix "for" + @@ Public_key_hash.alias_param + ~name: "mgr" ~desc: "manager of the new contract" + @@ stop) + (fun neu (_, manager) cctxt -> + check_contract cctxt neu >>= fun () -> + faucet cctxt (block ()) ~force:!force ~manager_pkh:manager () >>= Client_proto_rpcs.handle_error cctxt >>= fun contract -> + RawContractAlias.add cctxt neu contract) ; command ~group ~desc: "transfer tokens" ~args: [ fee_arg ; arg_arg ; force_arg ] (prefixes [ "transfer" ] diff --git a/src/client/embedded/alpha/client_proto_rpcs.ml b/src/client/embedded/alpha/client_proto_rpcs.ml index fb6bd4f26..791a6c2c1 100644 --- a/src/client/embedded/alpha/client_proto_rpcs.ml +++ b/src/client/embedded/alpha/client_proto_rpcs.ml @@ -232,6 +232,10 @@ module Helpers = struct let seed_nonce_revelation cctxt block ~net ~level ~nonce () = operations cctxt block ~net [Seed_nonce_revelation { level ; nonce }] + let faucet cctxt + block ~net ~id () = + let nonce = Sodium.Random.Bigbytes.generate 16 in + operations cctxt block ~net [Faucet { id ; nonce }] end let block cctxt block ~net ~predecessor ~timestamp ~fitness ~operations diff --git a/src/client/embedded/alpha/client_proto_rpcs.mli b/src/client/embedded/alpha/client_proto_rpcs.mli index ea74062af..b7dd91853 100644 --- a/src/client/embedded/alpha/client_proto_rpcs.mli +++ b/src/client/embedded/alpha/client_proto_rpcs.mli @@ -272,6 +272,12 @@ module Helpers : sig level:Raw_level.t -> nonce:Nonce.t -> unit -> MBytes.t tzresult Lwt.t + val faucet: + Client_commands.context -> + block -> + net:Updater.Net_id.t -> + id:public_key_hash -> + unit -> MBytes.t tzresult Lwt.t end val block: Client_commands.context -> diff --git a/src/proto/alpha/apply.ml b/src/proto/alpha/apply.ml index aaaf39595..4b48ecf37 100644 --- a/src/proto/alpha/apply.ml +++ b/src/proto/alpha/apply.ml @@ -154,7 +154,7 @@ let apply_sourced_operation ctxt contents >>=? fun ctxt -> return (ctxt, origination_nonce) -let apply_anonymous_operation ctxt miner_contract kind = +let apply_anonymous_operation ctxt miner_contract origination_nonce kind = match kind with | Seed_nonce_revelation { level ; nonce } -> let level = Level.from_raw ctxt level in @@ -162,19 +162,37 @@ let apply_anonymous_operation ctxt miner_contract kind = reward_amount) -> Reward.record ctxt delegate_to_reward level.cycle reward_amount >>=? fun ctxt -> - (match miner_contract with - | None -> return ctxt - | Some contract -> - Contract.credit ctxt contract Constants.seed_nonce_revelation_tip) + begin + match miner_contract with + | None -> return (ctxt, origination_nonce) + | Some contract -> + Contract.credit + ctxt contract Constants.seed_nonce_revelation_tip >>=? fun ctxt -> + return (ctxt, origination_nonce) + end + | Faucet { id = manager } -> + (* Free tez for all! *) + begin + match miner_contract with + | None -> return None + | Some contract -> Contract.get_delegate_opt ctxt contract + end >>=? fun delegate -> + Contract.originate ctxt + origination_nonce + ~manager ~delegate ~balance:Constants.faucet_credit ~script:No_script + ~spendable:true ~delegatable:true >>=? fun (ctxt, _, origination_nonce) -> + return (ctxt, origination_nonce) let apply_operation ctxt accept_failing_script miner_contract pred_block block_prio operation = match operation.contents with | Anonymous_operations ops -> + let origination_nonce = Contract.initial_origination_nonce operation.hash in fold_left_s - (fun ctxt -> apply_anonymous_operation ctxt miner_contract) - ctxt ops >>=? fun ctxt -> - return (ctxt, []) + (fun (ctxt, origination_nonce) -> + apply_anonymous_operation ctxt miner_contract origination_nonce) + (ctxt, origination_nonce) ops >>=? fun (ctxt, origination_nonce) -> + return (ctxt, Contract.originated_contracts origination_nonce) | Sourced_operations op -> let origination_nonce = Contract.initial_origination_nonce operation.hash in apply_sourced_operation diff --git a/src/proto/alpha/constants_repr.ml b/src/proto/alpha/constants_repr.ml index b69d0da09..091db1f79 100644 --- a/src/proto/alpha/constants_repr.ml +++ b/src/proto/alpha/constants_repr.ml @@ -30,6 +30,8 @@ let mining_reward = Tez_repr.of_cents_exn 150_00L let endorsement_reward = Tez_repr.of_cents_exn 150_00L +let faucet_credit = + Tez_repr.of_cents_exn 100_000_00L type constants = { cycle_length: int32 ; diff --git a/src/proto/alpha/operation_repr.ml b/src/proto/alpha/operation_repr.ml index 9fead8956..1d28081c7 100644 --- a/src/proto/alpha/operation_repr.ml +++ b/src/proto/alpha/operation_repr.ml @@ -25,6 +25,10 @@ and anonymous_operation = level: Raw_level_repr.t ; nonce: Seed_repr.nonce ; } + | Faucet of { + id: Ed25519.Public_key_hash.t ; + nonce: MBytes.t ; + } and sourced_operations = | Manager_operations of { @@ -251,10 +255,24 @@ module Encoding = struct case ~tag seed_nonce_revelation_encoding (function | Seed_nonce_revelation { level ; nonce } -> Some ((), level, nonce) - (* | _ -> None *) + | _ -> None ) (fun ((), level, nonce) -> Seed_nonce_revelation { level ; nonce }) + let faucet_encoding = + (obj3 + (req "kind" (constant "faucet")) + (req "id" Ed25519.Public_key_hash.encoding) + (req "nonce" (Fixed.bytes 16))) + + let faucet_case tag = + case ~tag faucet_encoding + (function + | Faucet { id ; nonce } -> Some ((), id, nonce) + | _ -> None + ) + (fun ((), id, nonce) -> Faucet { id ; nonce }) + let unsigned_operation_case tag = case ~tag (obj1 @@ -262,6 +280,7 @@ module Encoding = struct (list (union [ seed_nonce_revelation_case 0 ; + faucet_case 1 ; ])))) (function Anonymous_operations ops -> Some ops | _ -> None) (fun ops -> Anonymous_operations ops) diff --git a/src/proto/alpha/operation_repr.mli b/src/proto/alpha/operation_repr.mli index 089325228..0cd6e5779 100644 --- a/src/proto/alpha/operation_repr.mli +++ b/src/proto/alpha/operation_repr.mli @@ -25,6 +25,10 @@ and anonymous_operation = level: Raw_level_repr.t ; nonce: Seed_repr.nonce ; } + | Faucet of { + id: Ed25519.Public_key_hash.t ; + nonce: MBytes.t ; + } and sourced_operations = | Manager_operations of { diff --git a/src/proto/alpha/tezos_context.mli b/src/proto/alpha/tezos_context.mli index 95116d2d2..cdd50ba01 100644 --- a/src/proto/alpha/tezos_context.mli +++ b/src/proto/alpha/tezos_context.mli @@ -175,6 +175,7 @@ module Constants : sig val origination_burn: Tez.t val mining_bond_cost: Tez.t val endorsement_bond_cost: Tez.t + val faucet_credit: Tez.t val cycle_length: context -> int32 val voting_period_length: context -> int32 @@ -444,6 +445,10 @@ and anonymous_operation = level: Raw_level.t ; nonce: Nonce.t ; } + | Faucet of { + id: Ed25519.Public_key_hash.t ; + nonce: MBytes.t ; + } and sourced_operations = | Manager_operations of {