package octez-smart-rollup-node-lib

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

Source file refutation_player.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Refutation_player_types
open Refutation_game

module Types = struct
  type state = {
    node_ctxt : Node_context.rw;
    self : Signature.public_key_hash;
    opponent : Signature.public_key_hash;
    mutable last_move_cache :
      (Octez_smart_rollup.Game.game_state * int32) option;
  }

  type parameters = {
    node_ctxt : Node_context.rw;
    self : Signature.public_key_hash;
    conflict : Octez_smart_rollup.Game.conflict;
  }
end

module Name = struct
  let base = Refutation_game_event.Player.section @ ["worker"]

  include Signature.Public_key_hash
end

module Worker = Worker.MakeSingle (Name) (Request) (Types)

type worker = Worker.infinite Worker.queue Worker.t

let table = Worker.create_table Queue

let on_play game Types.{node_ctxt; self; opponent; _} =
  play node_ctxt ~self game opponent

let on_play_opening conflict (Types.{node_ctxt; self; _} : Types.state) =
  play_opening_move node_ctxt self conflict

module Handlers = struct
  type self = worker

  let on_request :
      type r request_error.
      worker -> (r, request_error) Request.t -> (r, request_error) result Lwt.t
      =
   fun w request ->
    let state = Worker.state w in
    match request with
    | Request.Play game -> protect @@ fun () -> on_play game state
    | Request.Play_opening conflict ->
        protect @@ fun () -> on_play_opening conflict state

  type launch_error = error trace

  let on_launch _w _name Types.{node_ctxt; self; conflict} =
    Lwt_result.return
      Types.{node_ctxt; self; opponent = conflict.other; last_move_cache = None}

  let on_error (type a b) _w st (r : (a, b) Request.t) (errs : b) :
      unit tzresult Lwt.t =
    let open Lwt_result_syntax in
    let request_view = Request.view r in
    let emit_and_return_errors errs =
      let*! () =
        Refutation_game_event.Player.request_failed request_view st errs
      in
      return_unit
    in
    match r with
    | Request.Play _ -> emit_and_return_errors errs
    | Request.Play_opening _ -> emit_and_return_errors errs

  let on_completion _w r _ st =
    Refutation_game_event.Player.request_completed (Request.view r) st

  let on_no_request _ = Lwt.return_unit

  let on_close w =
    let open Lwt_syntax in
    let state = Worker.state w in
    let* () = Refutation_game_event.Player.stopped state.opponent in
    return_unit
end

let init node_ctxt ~self ~conflict =
  let open Lwt_result_syntax in
  let*! () =
    Refutation_game_event.Player.started
      conflict.Game.other
      conflict.Game.our_commitment
  in
  let worker_promise, worker_waker = Lwt.task () in
  let* worker =
    trace Rollup_node_errors.Refutation_player_failed_to_start
    @@ Worker.launch
         table
         conflict.other
         {node_ctxt; self; conflict}
         (module Handlers)
  in
  let () = Lwt.wakeup worker_waker worker in
  let worker =
    let open Result_syntax in
    match Lwt.state worker_promise with
    | Lwt.Return worker -> return worker
    | Lwt.Fail _ | Lwt.Sleep ->
        tzfail Rollup_node_errors.Refutation_player_failed_to_start
  in
  Lwt.return worker

(* Play if:
    - There's a new game state to play against or
    - The current level is past the buffer for re-playing in the
      same game state.
*)
let should_move ~level game last_move_cache =
  match last_move_cache with
  | None -> true
  | Some (last_move_game_state, last_move_level) ->
      (not (Game.game_state_equal game.Game.game_state last_move_game_state))
      || Int32.(
           sub level last_move_level
           > of_int Configuration.refutation_player_buffer_levels)

let play w game ~(level : int32) =
  let open Lwt_syntax in
  let state = Worker.state w in
  if should_move ~level game state.last_move_cache then (
    let* pushed = Worker.Queue.push_request w (Request.Play game) in
    if pushed then state.last_move_cache <- Some (game.Game.game_state, level) ;
    return_unit)
  else return_unit

let play_opening w conflict =
  let open Lwt_syntax in
  let* (_pushed : bool) =
    Worker.Queue.push_request w (Request.Play_opening conflict)
  in
  return_unit

let init_and_play node_ctxt ~self ~conflict ~game ~level =
  let open Lwt_result_syntax in
  let* worker = init node_ctxt ~self ~conflict in
  let*! () =
    match game with
    | None -> play_opening worker conflict
    | Some game -> play worker game ~level
  in
  return_unit

let current_games () =
  List.map
    (fun (_name, worker) -> ((Worker.state worker).opponent, worker))
    (Worker.list table)

let shutdown = Worker.shutdown
OCaml

Innovation. Community. Security.