Alphanet: trivial faucet

This commit is contained in:
Grégoire Henry 2017-02-28 02:48:51 +01:00
parent 768cf91cd6
commit 2b938802f3
8 changed files with 97 additions and 15 deletions

View File

@ -89,14 +89,16 @@ let transfer cctxt
cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () -> cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
return contracts 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 () -> 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 -> 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 let oph = Operation_hash.hash_bytes [ signed_bytes ] in
Client_proto_rpcs.Helpers.apply_operation cctxt block Client_proto_rpcs.Helpers.apply_operation cctxt block
predecessor oph bytes (Some signature) >>=? function predecessor oph bytes signature >>=? function
| [ contract ] -> | [ contract ] ->
Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun injected_oph -> Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun injected_oph ->
assert (Operation_hash.equal oph 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 ~net ~source ~sourcePubKey:src_pk ~managerPubKey:manager_pkh
~counter ~balance ?spendable ~counter ~balance ?spendable
?delegatable ?delegatePubKey:delegate ~fee () >>=? fun bytes -> ?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 let originate_contract cctxt
block ?force block ?force
@ -136,7 +139,14 @@ let originate_contract cctxt
~counter ~balance ~spendable:!spendable ~counter ~balance ~spendable:!spendable
?delegatable ?delegatePubKey ?delegatable ?delegatePubKey
~script:(code, init) ~fee () >>=? fun bytes -> ~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 = let group =
{ Cli_entries.name = "context" ; { Cli_entries.name = "context" ;
@ -246,6 +256,20 @@ let commands () =
~delegatable:!delegatable ?delegatePubKey:delegate ~code ~init:!init ~delegatable:!delegatable ?delegatePubKey:delegate ~code ~init:!init
()) >>= Client_proto_rpcs.handle_error cctxt >>= fun contract -> ()) >>= Client_proto_rpcs.handle_error cctxt >>= fun contract ->
RawContractAlias.add cctxt neu 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" command ~group ~desc: "transfer tokens"
~args: [ fee_arg ; arg_arg ; force_arg ] ~args: [ fee_arg ; arg_arg ; force_arg ]
(prefixes [ "transfer" ] (prefixes [ "transfer" ]

View File

@ -232,6 +232,10 @@ module Helpers = struct
let seed_nonce_revelation cctxt let seed_nonce_revelation cctxt
block ~net ~level ~nonce () = block ~net ~level ~nonce () =
operations cctxt block ~net [Seed_nonce_revelation { 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 end
let block cctxt let block cctxt
block ~net ~predecessor ~timestamp ~fitness ~operations block ~net ~predecessor ~timestamp ~fitness ~operations

View File

@ -272,6 +272,12 @@ module Helpers : sig
level:Raw_level.t -> level:Raw_level.t ->
nonce:Nonce.t -> nonce:Nonce.t ->
unit -> MBytes.t tzresult Lwt.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 end
val block: val block:
Client_commands.context -> Client_commands.context ->

View File

@ -154,7 +154,7 @@ let apply_sourced_operation
ctxt contents >>=? fun ctxt -> ctxt contents >>=? fun ctxt ->
return (ctxt, origination_nonce) 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 match kind with
| Seed_nonce_revelation { level ; nonce } -> | Seed_nonce_revelation { level ; nonce } ->
let level = Level.from_raw ctxt level in let level = Level.from_raw ctxt level in
@ -162,19 +162,37 @@ let apply_anonymous_operation ctxt miner_contract kind =
reward_amount) -> reward_amount) ->
Reward.record ctxt Reward.record ctxt
delegate_to_reward level.cycle reward_amount >>=? fun ctxt -> delegate_to_reward level.cycle reward_amount >>=? fun ctxt ->
(match miner_contract with begin
| None -> return ctxt match miner_contract with
| Some contract -> | None -> return (ctxt, origination_nonce)
Contract.credit ctxt contract Constants.seed_nonce_revelation_tip) | 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 let apply_operation
ctxt accept_failing_script miner_contract pred_block block_prio operation = ctxt accept_failing_script miner_contract pred_block block_prio operation =
match operation.contents with match operation.contents with
| Anonymous_operations ops -> | Anonymous_operations ops ->
let origination_nonce = Contract.initial_origination_nonce operation.hash in
fold_left_s fold_left_s
(fun ctxt -> apply_anonymous_operation ctxt miner_contract) (fun (ctxt, origination_nonce) ->
ctxt ops >>=? fun ctxt -> apply_anonymous_operation ctxt miner_contract origination_nonce)
return (ctxt, []) (ctxt, origination_nonce) ops >>=? fun (ctxt, origination_nonce) ->
return (ctxt, Contract.originated_contracts origination_nonce)
| Sourced_operations op -> | Sourced_operations op ->
let origination_nonce = Contract.initial_origination_nonce operation.hash in let origination_nonce = Contract.initial_origination_nonce operation.hash in
apply_sourced_operation apply_sourced_operation

View File

@ -30,6 +30,8 @@ let mining_reward =
Tez_repr.of_cents_exn 150_00L Tez_repr.of_cents_exn 150_00L
let endorsement_reward = let endorsement_reward =
Tez_repr.of_cents_exn 150_00L Tez_repr.of_cents_exn 150_00L
let faucet_credit =
Tez_repr.of_cents_exn 100_000_00L
type constants = { type constants = {
cycle_length: int32 ; cycle_length: int32 ;

View File

@ -25,6 +25,10 @@ and anonymous_operation =
level: Raw_level_repr.t ; level: Raw_level_repr.t ;
nonce: Seed_repr.nonce ; nonce: Seed_repr.nonce ;
} }
| Faucet of {
id: Ed25519.Public_key_hash.t ;
nonce: MBytes.t ;
}
and sourced_operations = and sourced_operations =
| Manager_operations of { | Manager_operations of {
@ -251,10 +255,24 @@ module Encoding = struct
case ~tag seed_nonce_revelation_encoding case ~tag seed_nonce_revelation_encoding
(function (function
| Seed_nonce_revelation { level ; nonce } -> Some ((), level, nonce) | Seed_nonce_revelation { level ; nonce } -> Some ((), level, nonce)
(* | _ -> None *) | _ -> None
) )
(fun ((), level, nonce) -> Seed_nonce_revelation { level ; nonce }) (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 = let unsigned_operation_case tag =
case ~tag case ~tag
(obj1 (obj1
@ -262,6 +280,7 @@ module Encoding = struct
(list (list
(union [ (union [
seed_nonce_revelation_case 0 ; seed_nonce_revelation_case 0 ;
faucet_case 1 ;
])))) ]))))
(function Anonymous_operations ops -> Some ops | _ -> None) (function Anonymous_operations ops -> Some ops | _ -> None)
(fun ops -> Anonymous_operations ops) (fun ops -> Anonymous_operations ops)

View File

@ -25,6 +25,10 @@ and anonymous_operation =
level: Raw_level_repr.t ; level: Raw_level_repr.t ;
nonce: Seed_repr.nonce ; nonce: Seed_repr.nonce ;
} }
| Faucet of {
id: Ed25519.Public_key_hash.t ;
nonce: MBytes.t ;
}
and sourced_operations = and sourced_operations =
| Manager_operations of { | Manager_operations of {

View File

@ -175,6 +175,7 @@ module Constants : sig
val origination_burn: Tez.t val origination_burn: Tez.t
val mining_bond_cost: Tez.t val mining_bond_cost: Tez.t
val endorsement_bond_cost: Tez.t val endorsement_bond_cost: Tez.t
val faucet_credit: Tez.t
val cycle_length: context -> int32 val cycle_length: context -> int32
val voting_period_length: context -> int32 val voting_period_length: context -> int32
@ -444,6 +445,10 @@ and anonymous_operation =
level: Raw_level.t ; level: Raw_level.t ;
nonce: Nonce.t ; nonce: Nonce.t ;
} }
| Faucet of {
id: Ed25519.Public_key_hash.t ;
nonce: MBytes.t ;
}
and sourced_operations = and sourced_operations =
| Manager_operations of { | Manager_operations of {