package tezt-tezos

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file demo_client.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 G.B. Fefe  <gb.fefe@protonmail.com>                    *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module Time = Tezos_base.Time.System

let default_delay = Time.Span.of_seconds_exn (3600. *. 24. *. 365.)

let spawn_activate_protocol ?endpoint ?(fitness = 1)
    ?(key = Constant.activator.alias) ?(timestamp = Client.Ago default_delay)
    ~parameter_file client =
  let timestamp = Client.time_of_timestamp timestamp in
  Client.spawn_command
    ?endpoint
    client
    [
      "activate";
      "protocol";
      Protocol.demo_counter_hash;
      "with";
      "fitness";
      string_of_int fitness;
      "and";
      "key";
      key;
      "and";
      "parameters";
      parameter_file;
      "--timestamp";
      Time.to_notation timestamp;
    ]

let activate ?endpoint ?fitness ?key ?timestamp client =
  Lwt_io.with_temp_file (fun (parameter_file, ch) ->
      let* () = Lwt_io.write_line ch "{}" in
      spawn_activate_protocol
        ?endpoint
        ?fitness
        ?key
        ?timestamp
        ~parameter_file
        client
      |> Process.check)

let spawn_demo_bake ?(msg = "hello world") client =
  Client.spawn_command client ["bake"; msg]

let bake ?msg client = spawn_demo_bake ?msg client |> Process.check

let spawn_get client name = Client.spawn_command client ["get"; name]

let get client name =
  let extract_key (client_output : string) : int =
    let value =
      client_output =~* rex "The counter value is ?(\\w*)" |> mandatory "value"
    in
    match int_of_string_opt value with
    | Some i -> i
    | None -> failwith "Unexpected counter value."
  in
  let* output = spawn_get client name |> Process.check_and_read_stdout in
  return @@ extract_key output

let get_a client = get client "a"

let get_b client = get client "b"

let spawn_increment client name =
  Client.spawn_command client ["increment"; name]

let increment client name = spawn_increment client name |> Process.check

let increment_a client = increment client "a"

let increment_b client = increment client "b"

let spawn_transfer client amount =
  Client.spawn_command client ["transfer"; string_of_int amount]

let transfer client amount = spawn_transfer client amount |> Process.check
OCaml

Innovation. Community. Security.