package octez-protocol-018-Proxford-libs

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

Source file baking_simulator.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
181
182
183
184
185
186
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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 Protocol
open Alpha_context
open Baking_errors

type incremental = {
  predecessor : Baking_state.block_info;
  context : Tezos_protocol_environment.Context.t;
  state : Protocol.validation_state * Protocol.application_state option;
  rev_operations : Operation.packed list;
  header : Tezos_base.Block_header.shell_header;
}

let load_context ~context_path =
  let open Lwt_result_syntax in
  protect (fun () ->
      let*! index = Context.init ~readonly:true context_path in
      return (Abstract_context_index.abstract index))

let check_context_consistency (abstract_index : Abstract_context_index.t)
    context_hash =
  let open Lwt_result_syntax in
  protect (fun () ->
      (* Hypothesis : the version key exists *)
      let version_key = ["version"] in
      let*! context_opt = abstract_index.checkout_fun context_hash in
      match context_opt with
      | None -> tzfail Failed_to_checkout_context
      | Some context -> (
          let*! result = Context_ops.mem context version_key in
          match result with
          | true -> return_unit
          | false -> tzfail Invalid_context))

let begin_construction ~timestamp ~protocol_data ~force_apply
    ~pred_resulting_context_hash (abstract_index : Abstract_context_index.t)
    pred_block chain_id =
  let open Lwt_result_syntax in
  protect (fun () ->
      let {Baking_state.shell = pred_shell; hash = pred_hash; _} = pred_block in
      let*! context_opt =
        abstract_index.checkout_fun pred_resulting_context_hash
      in
      match context_opt with
      | None -> tzfail Failed_to_checkout_context
      | Some context ->
          let header : Tezos_base.Block_header.shell_header =
            Tezos_base.Block_header.
              {
                predecessor = pred_hash;
                proto_level = pred_shell.proto_level;
                validation_passes = 0;
                fitness = pred_shell.fitness;
                timestamp;
                level = pred_shell.level;
                context = Context_hash.zero (* fake context hash *);
                operations_hash =
                  Operation_list_list_hash.zero (* fake op hash *);
              }
          in
          let mode =
            Lifted_protocol.Construction
              {
                predecessor_hash = pred_hash;
                timestamp;
                block_header_data = protocol_data;
              }
          in
          let* validation_state =
            Lifted_protocol.begin_validation
              context
              chain_id
              mode
              ~predecessor:pred_shell
              ~cache:`Lazy
          in
          let* application_state =
            if force_apply then
              let* application_state =
                Lifted_protocol.begin_application
                  context
                  chain_id
                  mode
                  ~predecessor:pred_shell
                  ~cache:`Lazy
              in
              return_some application_state
            else return_none
          in
          let state = (validation_state, application_state) in
          return
            {
              predecessor = pred_block;
              context;
              state;
              rev_operations = [];
              header;
            })

let ( let** ) x k =
  let open Lwt_result_syntax in
  let*! x in
  let*? x = Environment.wrap_tzresult x in
  k x

let add_operation st (op : Operation.packed) =
  let open Lwt_result_syntax in
  protect (fun () ->
      let validation_state, application_state = st.state in
      let oph = Operation.hash_packed op in
      let** validation_state =
        Protocol.validate_operation
          ~check_signature:false
            (* We assume that the operation has already been validated in the
               node, therefore the signature has already been checked, but we
               still need to validate it again because the context may be
               different. *)
          validation_state
          oph
          op
      in
      let** application_state, receipt =
        match application_state with
        | Some application_state ->
            let* application_state, receipt =
              Protocol.apply_operation application_state oph op
            in
            return (Some application_state, Some receipt)
        | None -> return (None, None)
      in
      let state = (validation_state, application_state) in
      return ({st with state; rev_operations = op :: st.rev_operations}, receipt))

let finalize_construction inc =
  let open Lwt_result_syntax in
  protect (fun () ->
      let validation_state, application_state = inc.state in
      let** () = Protocol.finalize_validation validation_state in
      let** result =
        match application_state with
        | Some application_state -> (
            let*! result =
              Protocol.finalize_application application_state (Some inc.header)
            in
            match result with
            | Ok (vr, metadata) ->
                let new_vr =
                  Tezos_protocol_environment.
                    {
                      context = vr.context;
                      fitness = vr.fitness;
                      message = vr.message;
                      max_operations_ttl = vr.max_operations_ttl;
                      last_finalized_block_level = vr.last_allowed_fork_level;
                      last_preserved_block_level = vr.last_allowed_fork_level;
                    }
                in
                return_some (new_vr, metadata)
            | Error e -> Lwt.return (Error e))
        | None -> return_none
      in
      return result)
OCaml

Innovation. Community. Security.