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 () ->
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" ]

View File

@ -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

View File

@ -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 ->

View File

@ -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

View File

@ -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 ;

View File

@ -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)

View File

@ -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 {

View File

@ -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 {