ligo/src/client/embedded/demo/client_proto_main.ml

88 lines
3.1 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
let protocol =
Protocol_hash.of_b58check
"ProtoDemoDemoDemoDemoDemoDemoDemoDemoDemoDemoD3c8k9"
2016-09-08 21:13:10 +04:00
let demo cctxt =
2016-09-08 21:13:10 +04:00
let block = Client_config.block () in
cctxt.Client_commands.message "Calling the 'echo' RPC." >>= fun () ->
2016-09-08 21:13:10 +04:00
let msg = "test" in
Client_proto_rpcs.echo cctxt block msg >>= fun reply ->
2016-09-08 21:13:10 +04:00
fail_unless (reply = msg) (Unclassified "...") >>=? fun () ->
begin
cctxt.message "Calling the 'failing' RPC." >>= fun () ->
Client_proto_rpcs.failing cctxt block 3 >>= function
2016-09-08 21:13:10 +04:00
| Error [Ecoproto_error [Error.Demo_error 3]] ->
return ()
| _ -> failwith "..."
end >>=? fun () ->
cctxt.message "Direct call to `demo_error`." >>= fun () ->
2016-09-08 21:13:10 +04:00
begin Error.demo_error 101010 >|= wrap_error >>= function
| Error [Ecoproto_error [Error.Demo_error 101010]] ->
return ()
| _ -> failwith "...."
end >>=? fun () ->
cctxt.answer "All good!" >>= fun () ->
2016-09-08 21:13:10 +04:00
return ()
let mine cctxt =
2016-09-08 21:13:10 +04:00
let block =
match Client_config.block () with
| `Prevalidation -> `Head 0
| `Test_prevalidation -> `Test_head 0
| b -> b in
Client_node_rpcs.Blocks.info cctxt block >>= fun bi ->
2016-09-08 21:13:10 +04:00
let fitness =
match bi.fitness with
| [ v ; b ] ->
let f = MBytes.get_int64 b 0 in
MBytes.set_int64 b 0 (Int64.succ f) ;
[ v ; b ]
| _ ->
Lwt.ignore_result
(cctxt.message "Cannot parse fitness: %a" Fitness.pp bi.fitness);
2016-09-08 21:13:10 +04:00
exit 2 in
Client_node_rpcs.forge_block cctxt
2016-09-08 21:13:10 +04:00
~net:bi.net ~predecessor:bi.hash
fitness [] (MBytes.create 0) >>= fun bytes ->
Client_node_rpcs.inject_block cctxt ~wait:true bytes >>=? fun hash ->
cctxt.answer "Injected %a" Block_hash.pp_short hash >>= fun () ->
2016-09-08 21:13:10 +04:00
return ()
let handle_error cctxt = function
2016-09-08 21:13:10 +04:00
| Ok res ->
Lwt.return res
| Error exns ->
pp_print_error Format.err_formatter exns ;
cctxt.Client_commands.error "%s" "cannot continue"
2016-09-08 21:13:10 +04:00
let commands () =
let open Cli_entries in
let group = {name = "demo" ; title = "Some demo command" } in
2016-09-08 21:13:10 +04:00
[
command ~group ~desc: "A demo command"
2016-09-08 21:13:10 +04:00
(fixed [ "demo" ])
(fun cctxt -> demo cctxt >>= handle_error cctxt) ;
command ~group ~desc: "A failing command"
2016-09-08 21:13:10 +04:00
(fixed [ "fail" ])
(fun cctxt ->
2016-09-08 21:13:10 +04:00
Error.demo_error 101010
>|= wrap_error
>>= handle_error cctxt) ;
command ~group ~desc: "Mine an empty block"
2016-09-08 21:13:10 +04:00
(fixed [ "mine" ])
(fun cctxt -> mine cctxt >>= handle_error cctxt) ;
2016-09-08 21:13:10 +04:00
]
let () =
Client_commands.register protocol @@
2016-09-08 21:13:10 +04:00
commands ()