Alpha/Client: use block metadata

This commit is contained in:
Grégoire Henry 2018-04-19 18:10:28 +02:00 committed by Benjamin Canou
parent d6f79edae2
commit acc76d22a8
16 changed files with 74 additions and 134 deletions

View File

@ -9,7 +9,7 @@ client using this command ``tezos-admin-client rpc (get|post) <url>``.
For instance, if you wish to request the current balance of a given
block and contract, you can call the associated RPC via the command :
``$ tezos-admin-client rpc post
``$ tezos-admin-client rpc get
/blocks/<block_id>/proto/context/contracts/<contract_id>/balance``.
A RPC may takes an *input* and generates an *output* both in JSON

View File

@ -382,7 +382,7 @@ run_shell() {
display_head() {
assert_node_uptodate
exec_docker tezos-client rpc get /chains/main/blocks/head
exec_docker tezos-client rpc post /chains/main/blocks/head/context/level with {}
exec_docker tezos-client rpc get /chains/main/blocks/head/metadata/protocol_data/level
}
## Main ####################################################################

View File

@ -40,8 +40,8 @@ assert_propagation_level() {
level=$1
printf "\n\nAsserting all nodes have reached level %s\n" "$level"
for client in "${client_instances[@]}"; do
( $client rpc post /chains/main/blocks/head/context/level with {} \
| assert_in_output "\"level\": $level" ) \
( $client rpc get /chains/main/blocks/head/header/shell/level \
| assert_in_output "$level" ) \
|| exit 2
done
}

View File

@ -29,7 +29,7 @@ let info cctxt ?(chain = `Main) block =
cctxt ~chain ~block () >>=? fun next_protocol ->
Block_services.Metadata.protocol_hash
cctxt ~chain ~block () >>=? fun protocol ->
Alpha_services.Context.level cctxt (chain, block) >>=? fun level ->
Block_services.Metadata.protocol_data cctxt ~chain ~block () >>=? fun { level } ->
let { Tezos_base.Block_header.predecessor ; fitness ; timestamp ; _ } = header in
return { hash ; chain_id ; predecessor ; fitness ;
timestamp ; protocol ; next_protocol ; level }
@ -50,7 +50,7 @@ let monitor_heads cctxt ?next_protocols chain =
let blocks_from_cycle cctxt ?(chain = `Main) block cycle =
Block_services.hash cctxt ~chain ~block () >>=? fun hash ->
Alpha_services.Context.level cctxt (chain, block) >>=? fun level ->
Block_services.Metadata.protocol_data cctxt ~chain ~block () >>=? fun { level } ->
Alpha_services.Helpers.levels cctxt (chain, block) cycle >>=? fun (first, last) ->
let length = Int32.to_int (Raw_level.diff level.level first) in
Chain_services.Blocks.list cctxt ~heads:[hash] ~length () >>=? fun blocks ->

View File

@ -132,7 +132,8 @@ let forge_endorsement (cctxt : #Proto_alpha.full)
?(chain = `Main) block
~src_sk ?slots ?max_priority src_pk =
let src_pkh = Signature.Public_key.hash src_pk in
Alpha_services.Context.level cctxt (chain, block) >>=? fun { level } ->
Block_services.Metadata.protocol_data
cctxt ~chain ~block () >>=? fun { level = { level } } ->
begin
match slots with
| Some slots -> return slots

View File

@ -156,7 +156,7 @@ let forge_block cctxt ?(chain = `Main) block
return (priority, time)
end
| `Auto (src_pkh, max_priority, free_baking) ->
Alpha_services.Context.next_level cctxt (chain, block) >>=? fun { level } ->
Alpha_services.Helpers.next_level cctxt (chain, block) >>=? fun { level } ->
Alpha_services.Delegate.Baker.rights_for_delegate cctxt
?max_priority
~first_level:level
@ -393,7 +393,7 @@ let compute_timeout { future_slots } =
let get_unrevealed_nonces
(cctxt : #Proto_alpha.full) ?(force = false) ?(chain = `Main) block =
Alpha_services.Context.next_level cctxt (chain, block) >>=? fun level ->
Alpha_services.Helpers.next_level cctxt (chain, block) >>=? fun level ->
let cur_cycle = level.cycle in
match Cycle.pred cur_cycle with
| None -> return []
@ -404,8 +404,8 @@ let get_unrevealed_nonces
Client_baking_nonces.find cctxt hash >>=? function
| None -> return None
| Some nonce ->
Alpha_services.Context.level
cctxt (chain, `Hash (hash, 0)) >>=? fun level ->
Block_services.Metadata.protocol_data
cctxt ~chain ~block:(`Hash (hash, 0)) () >>=? fun { level } ->
if force then
return (Some (hash, (level.level, nonce)))
else
@ -492,7 +492,7 @@ let bake (cctxt : #Proto_alpha.full) state =
(fun (timestamp, (bi, priority, delegate)) ->
let chain = `Hash bi.Client_baking_blocks.chain_id in
let block = `Hash (bi.hash, 0) in
Alpha_services.Context.next_level cctxt (chain, block) >>=? fun next_level ->
Alpha_services.Helpers.next_level cctxt (chain, block) >>=? fun next_level ->
let timestamp =
if Block_hash.equal bi.Client_baking_blocks.hash state.genesis then
Time.now ()

View File

@ -21,7 +21,7 @@ let bake_block (cctxt : #Proto_alpha.full)
return src_sk
| Some sk -> return sk
end >>=? fun src_sk ->
Alpha_services.Context.next_level cctxt (chain, block) >>=? fun level ->
Alpha_services.Helpers.next_level cctxt (chain, block) >>=? fun level ->
let seed_nonce, seed_nonce_hash =
if level.expected_commitment then
let seed_nonce = Client_baking_forge.generate_seed_nonce () in

View File

@ -146,8 +146,9 @@ let init ?exe ?vote ?rpc_port () =
activate_alpha ?vote () >>=? fun hash ->
return (pid, hash)
let level block =
Alpha_services.Context.level !rpc_ctxt block
let level (chain, block) =
Block_services.Metadata.protocol_data !rpc_ctxt ~chain ~block () >>=? fun { level } ->
return level
let rpc_raw_context block path depth =
Block_services.Context.Raw.read !rpc_ctxt ~block ~depth path
@ -335,11 +336,13 @@ module Protocol = struct
open Account
let voting_period_kind ?(block = `Head 0) () =
Alpha_services.Context.voting_period_kind !rpc_ctxt (`Main, block)
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { voting_period_kind } ->
return voting_period_kind
let proposals ?(block = `Head 0) ~src:({ pkh; sk } : Account.t) proposals =
Block_services.hash !rpc_ctxt ~block () >>=? fun hash ->
Alpha_services.Context.next_level
Alpha_services.Helpers.next_level
!rpc_ctxt (`Main, block) >>=? fun next_level ->
let shell = { Tezos_base.Operation.branch = hash } in
let contents =
@ -351,7 +354,7 @@ module Protocol = struct
let ballot ?(block = `Head 0) ~src:({ pkh; sk } : Account.t) ~proposal ballot =
Block_services.hash !rpc_ctxt ~block () >>=? fun hash ->
Alpha_services.Context.next_level
Alpha_services.Helpers.next_level
!rpc_ctxt (`Main, block) >>=? fun next_level ->
let shell = { Tezos_base.Operation.branch = hash } in
let contents =
@ -495,11 +498,12 @@ module Assert = struct
block_proto h
let check_voting_period_kind ?msg ~block kind =
Alpha_services.Context.voting_period_kind !rpc_ctxt (`Main, block)
>>=? fun current_kind ->
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { voting_period_kind } ->
return @@ equal
?msg
current_kind kind
voting_period_kind
kind
let is_none ?(msg="") x =
if x <> None then fail "None" "Some _" msg
@ -512,7 +516,7 @@ module Baking = struct
let bake block (contract: Account.t) operations =
let ctxt = (new wrap_full (no_write_context ~block !rpc_config)) in
Alpha_services.Context.next_level ctxt (`Main, block) >>=? fun level ->
Alpha_services.Helpers.next_level ctxt (`Main, block) >>=? fun level ->
let seed_nonce_hash =
if level.Level.expected_commitment then
let seed_nonce =
@ -546,7 +550,9 @@ module Endorse = struct
src_sk
slot =
Block_services.hash !rpc_ctxt ~block () >>=? fun hash ->
Alpha_services.Context.level !rpc_ctxt (`Main, block) >>=? fun { level } ->
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { level } ->
let level = level.level in
let shell = { Tezos_base.Operation.branch = hash } in
let contents =
Consensus_operation
@ -570,7 +576,9 @@ module Endorse = struct
?slot
(contract : Account.t)
block =
Alpha_services.Context.level !rpc_ctxt (`Main, block) >>=? fun { level } ->
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { level } ->
let level = level.level in
begin
match slot with
| Some slot -> return slot
@ -597,7 +605,8 @@ module Endorse = struct
in
let { Account.b1 ; b2 ; b3 ; b4 ; b5 } = Account.bootstrap_accounts in
let result = Array.make 16 b1 in
Alpha_services.Context.level !rpc_ctxt (`Main, block) >>=? fun level ->
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { level } ->
let level = level.level in
get_endorser_list result b1 level block >>=? fun () ->
get_endorser_list result b2 level block >>=? fun () ->
@ -609,9 +618,10 @@ module Endorse = struct
let endorsement_rights
?(max_priority = 1024)
(contract : Account.t) block =
Alpha_services.Context.level !rpc_ctxt (`Main, block) >>=? fun level ->
let delegate = contract.pkh in
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { level } ->
let level = level.level in
let delegate = contract.pkh in
Alpha_services.Delegate.Endorser.rights_for_delegate
!rpc_ctxt
~max_priority
@ -622,8 +632,9 @@ module Endorse = struct
end
let display_level block =
Alpha_services.Context.level !rpc_ctxt (`Main, block) >>=? fun lvl ->
Format.eprintf "Level: %a@." Level.pp_full lvl ;
Block_services.Metadata.protocol_data
!rpc_ctxt ~chain:`Main ~block () >>=? fun { level } ->
Format.eprintf "Level: %a@." Level.pp_full level ;
return ()
let endorsement_security_deposit block =

View File

@ -19,7 +19,8 @@ val init :
forked Tezos node and the block info of the block from where the
tests will begin. *)
val level : Chain_services.chain * Block_services.block -> Alpha_context.Level.t tzresult Lwt.t
val level:
Chain_services.chain * Block_services.block -> Alpha_context.Level.t tzresult Lwt.t
(** Calls the rpc service raw_context using the right rpc context *)
val rpc_raw_context : Block_services.block -> string list -> int ->

View File

@ -11,62 +11,6 @@ open Alpha_context
let custom_root = RPC_path.open_root
module Context = struct
module S = struct
open Data_encoding
let level =
RPC_service.post_service
~description: "Detailled level information for the current block"
~query: RPC_query.empty
~input: empty
~output: Level.encoding
RPC_path.(custom_root / "context" / "level")
let next_level =
RPC_service.post_service
~description: "Detailled level information for the next block"
~query: RPC_query.empty
~input: empty
~output: Level.encoding
RPC_path.(custom_root / "context" / "next_level")
let voting_period_kind =
RPC_service.post_service
~description: "Voting period kind for the current block"
~query: RPC_query.empty
~input: empty
~output:
(obj1 (req "voting_period_kind" Voting_period.kind_encoding))
RPC_path.(custom_root / "context" / "voting_period_kind")
end
let () =
let open Services_registration in
register0 S.level begin fun ctxt () () ->
return (Level.current ctxt)
end ;
register0 S.next_level begin fun ctxt () () ->
return (Level.succ ctxt (Level.current ctxt))
end ;
register0 S.voting_period_kind begin fun ctxt () () ->
Vote.get_current_period_kind ctxt
end
let level ctxt block =
RPC_context.make_call0 S.level ctxt block () ()
let next_level ctxt block =
RPC_context.make_call0 S.next_level ctxt block () ()
let voting_period_kind ctxt block =
RPC_context.make_call0 S.voting_period_kind ctxt block () ()
end
module Nonce = struct
type info =
@ -93,23 +37,12 @@ module Nonce = struct
module S = struct
open Data_encoding
let get =
RPC_service.post_service
RPC_service.get_service
~description: "Info about the nonce of a previous block."
~query: RPC_query.empty
~input: empty
~output: info_encoding
RPC_path.(custom_root / "context" / "nonce" /: Raw_level.arg)
let hash =
RPC_service.post_service
~description: "Hash of the current block's nonce."
~query: RPC_query.empty
~input: empty
~output: Nonce_hash.encoding
RPC_path.(custom_root / "context" / "nonce")
RPC_path.(custom_root / "context" / "nonces" /: Raw_level.arg)
end
@ -122,20 +55,11 @@ module Nonce = struct
| Ok (Unrevealed { nonce_hash ; _ }) ->
return (Missing nonce_hash)
| Error _ -> return Forgotten
end ;
register0 S.hash begin fun ctxt () () ->
let level = Level.current ctxt in
Nonce.get ctxt level >>=? function
| Unrevealed { nonce_hash ; _ } -> return nonce_hash
| _ -> assert false
end
let get ctxt block level =
RPC_context.make_call1 S.get ctxt block level () ()
let hash ctxt block =
RPC_context.make_call0 S.hash ctxt block () ()
end
module Contract = Contract_services

View File

@ -9,31 +9,8 @@
open Alpha_context
module Context : sig
val level:
'a #RPC_context.simple -> 'a -> Level.t shell_tzresult Lwt.t
(** [level cctxt blk] returns the (protocol view of the) level of
[blk]. *)
val next_level:
'a #RPC_context.simple -> 'a -> Level.t shell_tzresult Lwt.t
(** [next_level cctxt blk] returns the (protocol view of the) level
of the successor of [blk]. *)
val voting_period_kind:
'a #RPC_context.simple -> 'a -> Voting_period.kind shell_tzresult Lwt.t
(** [voting_period_kind cctxt blk] returns the voting period kind
of [blk]. *)
end
module Nonce : sig
val hash:
'a #RPC_context.simple ->
'a -> Nonce_hash.t shell_tzresult Lwt.t
type info =
| Revealed of Nonce.t
| Missing of Nonce_hash.t

View File

@ -40,6 +40,14 @@ module S = struct
let custom_root = RPC_path.(open_root / "helpers")
let next_level =
RPC_service.post_service
~description: "Detailled level information for the next block"
~query: RPC_query.empty
~input: empty
~output: Level.encoding
RPC_path.(custom_root / "next_level")
let minimal_timestamp =
RPC_service.post_service
~description: "Minimal timestamp for the next block."
@ -173,6 +181,9 @@ end
let () =
let open Services_registration in
register0 S.next_level begin fun ctxt () () ->
return (Level.succ ctxt (Level.current ctxt))
end ;
register0 S.minimal_timestamp begin fun ctxt () slot ->
let timestamp = Alpha_context.Timestamp.current ctxt in
let slot = match slot with None -> 0 | Some p -> p in
@ -243,6 +254,9 @@ let () =
return (first.level, last.level)
end
let next_level ctxt block =
RPC_context.make_call0 S.next_level ctxt block () ()
let minimal_time ctxt ?priority block =
RPC_context.make_call0 S.minimal_timestamp ctxt block () priority

View File

@ -13,6 +13,11 @@ type error +=
| Cannot_parse_operation (* `Branch *)
| Cant_parse_block_header
val next_level:
'a #RPC_context.simple -> 'a -> Level.t shell_tzresult Lwt.t
(** [next_level cctxt blk] returns the (protocol view of the) level
of the successor of [blk]. *)
val minimal_time:
'a #RPC_context.simple ->
?priority:int -> 'a -> Time.t shell_tzresult Lwt.t

View File

@ -139,7 +139,7 @@ module Forge = struct
| Error _ -> assert false
end >>=? fun fitness ->
begin
Alpha_services.Context.next_level (rpc_ctxt) pred >>|? function
Alpha_services.Helpers.next_level (rpc_ctxt) pred >>|? function
| { expected_commitment = true } -> Some (fst (Proto_Nonce.generate ()))
| { expected_commitment = false } -> None
end >>=? fun seed_nonce_hash ->

View File

@ -4,12 +4,14 @@
((name tezos_alpha_test_helpers)
(libraries (tezos-base
tezos-stdlib-unix
tezos-shell-services
tezos-protocol-environment
tezos-protocol-alpha
alcotest-lwt))
(flags (:standard -w -9-32 -safe-string
-open Tezos_base__TzPervasives
-open Tezos_stdlib_unix))))
-open Tezos_stdlib_unix
-open Tezos_shell_services))))
(alias
((name runtest_indent)

View File

@ -13,6 +13,11 @@ module Alpha_environment = Tezos_protocol_environment_memory.MakeV1(Name)()
type alpha_error = Alpha_environment.Error_monad.error
type 'a alpha_tzresult = 'a Alpha_environment.Error_monad.tzresult
include Tezos_protocol_alpha.Functor.Make(Alpha_environment)
module Proto = Tezos_protocol_alpha.Functor.Make(Alpha_environment)
module Block_services = struct
include Block_services
include Block_services.Make(Proto)(Proto)
end
include Proto
module M = Alpha_environment.Lift(Main)