text
stringlengths
0
601k
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None
let file_equal f1 f2 = String . equal ( Format . asprintf " % a " Sihl_storage . pp_file f1 ) ( Format . asprintf " % a " Sihl_storage . pp_file f2 ) ; ;
let alco_file = Alcotest . testable Sihl_storage . pp_file file_equal
module Make ( StorageService : Sihl . Contract . Storage . Sig ) = struct let fetch_uploaded_file _ ( ) = let % lwt ( ) = Sihl . Cleaner . clean_all ( ) in let file_id = Uuidm . v ` V4 |> Uuidm . to_string in let file = Sihl . Contract . Storage . { id = file_id ; filename = " diploma . pdf " ; filesize = 123 ; mime = ...
module StringMap = Map . Make ( String )
type ' key t = ' key desc_with_path rev_path : string list ; mutable dir : ' key description ; } | Empty : ' key description | Value : { get : ' key -> ' a option tzresult Lwt . t ; encoding : ' a Data_encoding . t ; } -> ' key description | NamedDir : ' key t StringMap . t -> ' key description | IndexedDir : { arg : '...
let pp_rev_path ppf path = Format . fprintf ppf " [ % a ] " Format . ( pp_print_list ~ pp_sep ( : fun ppf ( ) -> pp_print_string ppf " / " ) pp_print_string ) ( List . rev path )
let rec register_named_subcontext : type r . r t -> string list -> r t = fun desc names -> match ( desc . dir , names ) with | ( _ , [ ] ) -> desc | ( Value _ , _ ) | ( IndexedDir _ , _ ) -> Format . kasprintf invalid_arg " Could not register a named subcontext at % a because of an existing % a . " pp_rev_path desc . r...
type ( _ , _ , _ ) args = | One : { rpc_arg : ' a RPC_arg . t ; encoding : ' a Data_encoding . t ; compare : ' a -> ' a -> int ; } -> ( ' key , ' a , ' key * ' a ) args | Pair : ( ' key , ' a , ' inter_key ) args * ( ' inter_key , ' b , ' sub_key ) args -> ( ' key , ' a * ' b , ' sub_key ) args
let rec unpack : type a b c . ( a , b , c ) args -> c -> a * b = function | One _ -> fun x -> x | Pair ( l , r ) -> let unpack_l = unpack l in let unpack_r = unpack r in fun x -> let ( c , d ) = unpack_r x in let ( b , a ) = unpack_l c in ( b , ( a , d ) ) [ @@ coq_axiom_with_reason " gadt " ]
let rec pack : type a b c . ( a , b , c ) args -> a -> b -> c = function | One _ -> fun b a -> ( b , a ) | Pair ( l , r ) -> let pack_l = pack l in let pack_r = pack r in fun b ( a , d ) -> let c = pack_l b a in pack_r c d [ @@ coq_axiom_with_reason " gadt " ]
let rec compare : type a b c . ( a , b , c ) args -> b -> b -> int = function | One { compare ; _ } -> compare | Pair ( l , r ) -> ( let compare_l = compare l in let compare_r = compare r in fun ( a1 , b1 ) ( a2 , b2 ) -> match compare_l a1 a2 with 0 -> compare_r b1 b2 | x -> x ) [ @@ coq_axiom_with_reason " gadt " ]
let destutter equal l = match l with | [ ] -> [ ] | ( i , _ ) :: l -> let rec loop acc i = function | [ ] -> acc | ( j , _ ) :: l -> if equal i j then loop acc i l else loop ( j :: acc ) j l in loop [ i ] i l
let rec register_indexed_subcontext : type r a b . r t -> list ( : r -> a list tzresult Lwt . t ) -> ( r , a , b ) args -> b t = fun desc ~ list path -> match path with | Pair ( left , right ) -> let compare_left = compare left in let equal_left x y = Compare . Int . ( compare_left x y = 0 ) in let list_left r = list r...
let register_value : type a b . a t -> get ( : a -> b option tzresult Lwt . t ) -> b Data_encoding . t -> unit = fun desc ~ get encoding -> match desc . dir with | Empty -> desc . dir <- Value { get ; encoding } | _ -> Format . kasprintf invalid_arg " Could not register a value at % a because of an existing % a . " pp_...
let create ( ) = { rev_path = [ ] ; dir = Empty }
module type INDEX = sig type t include Path_encoding . S with type t := t val rpc_arg : t RPC_arg . t val encoding : t Data_encoding . t val compare : t -> t -> int end
type _ handler = | Handler : { encoding : ' a Data_encoding . t ; get : ' key -> int -> ' a tzresult Lwt . t ; } -> ' key handler
type _ opt_handler = | Opt_handler : { encoding : ' a Data_encoding . t ; get : ' key -> int -> ' a option tzresult Lwt . t ; } -> ' key opt_handler
let rec combine_object = function | [ ] -> Handler { encoding = Data_encoding . unit ; get = ( fun _ _ -> return_unit ) } | ( name , Opt_handler handler ) :: fields -> let ( Handler handlers ) = combine_object fields in Handler { encoding = Data_encoding . merge_objs Data_encoding . ( obj1 ( opt name ( dynamic_size han...
type query = { depth : int }
let depth_query = let open RPC_query in query ( fun depth -> { depth } ) |+ field " depth " RPC_arg . uint 0 ( fun t -> t . depth ) |> seal
let build_directory : type key . key t -> key RPC_directory . t = fun dir -> let rpc_dir = ref ( RPC_directory . empty : key RPC_directory . t ) in let register : type ikey . chunked : bool -> ( key , ikey ) RPC_path . t -> ikey opt_handler -> unit = fun ~ chunked path ( Opt_handler { encoding ; get } ) -> let service ...
module Registered = struct let ghost = false end
module Ghost = struct let ghost = true end
module type ENCODER = sig type t val of_bytes : key ( : unit -> string list ) -> bytes -> t tzresult val to_bytes : t -> bytes end
module Make_encoder ( V : VALUE ) : ENCODER with type t := V . t = struct let of_bytes ~ key b = match Data_encoding . Binary . of_bytes_opt V . encoding b with | None -> error ( Raw_context . Storage_error ( Corrupted_data ( key ( ) ) ) ) | Some v -> Ok v let to_bytes v = match Data_encoding . Binary . to_bytes_opt V ...
let encode_len_value bytes = let length = Bytes . length bytes in Data_encoding . ( Binary . to_bytes_exn int31 ) length
let decode_len_value key len = match Data_encoding . ( Binary . of_bytes_opt int31 ) len with | None -> error ( Raw_context . Storage_error ( Corrupted_data key ) ) | Some len -> ok len
module Make_subcontext ( R : REGISTER ) ( C : Raw_context . T ) ( N : NAME ) : Raw_context . T with type t = C . t = struct type t = C . t let to_key k = N . name @ k let mem t k = C . mem t ( to_key k ) let mem_tree t k = C . mem_tree t ( to_key k ) let get t k = C . get t ( to_key k ) let get_tree t k = C . get_tree ...
module Make_single_data_storage ( R : REGISTER ) ( C : Raw_context . T ) ( N : NAME ) ( V : VALUE ) : Single_data_storage with type t = C . t and type value = V . t = struct type t = C . t type context = t type value = V . t let mem t = C . mem t N . name include Make_encoder ( V ) let get t = C . get t N . name >>=? f...
module type INDEX = sig type t include Path_encoding . S with type t := t type ' a ipath val args : ( ' a , t , ' a ipath ) Storage_description . args end
module Pair ( I1 : INDEX ) ( I2 : INDEX ) : INDEX with type t = I1 . t * I2 . t = struct type t = I1 . t * I2 . t let path_length = I1 . path_length + I2 . path_length let to_path ( x , y ) l = I1 . to_path x ( I2 . to_path y l ) let of_path l = match Misc . take I1 . path_length l with | None -> None | Some ( l1 , l2 ...
module Make_data_set_storage ( C : Raw_context . T ) ( I : INDEX ) : Data_set_storage with type t = C . t and type elt = I . t = struct type t = C . t type context = t type elt = I . t let inited = Bytes . of_string " inited " let mem s i = C . mem s ( I . to_path i [ ] ) let add s i = C . add s ( I . to_path i [ ] ) i...
module Make_indexed_data_storage ( C : Raw_context . T ) ( I : INDEX ) ( V : VALUE ) : Indexed_data_storage with type t = C . t and type key = I . t and type value = V . t = struct type t = C . t type context = t type key = I . t type value = V . t include Make_encoder ( V ) let mem s i = C . mem s ( I . to_path i [ ] ...
module Make_indexed_carbonated_data_storage_INTERNAL ( C : Raw_context . T ) ( I : INDEX ) ( V : VALUE ) : Non_iterable_indexed_carbonated_data_storage_INTERNAL with type t = C . t and type key = I . t and type value = V . t = struct type t = C . t type context = t type key = I . t type value = V . t include Make_encod...
module Make_indexed_carbonated_data_storage : functor ( C : Raw_context . T ) ( I : INDEX ) ( V : VALUE ) -> Non_iterable_indexed_carbonated_data_storage_with_values with type t = C . t and type key = I . t and type value = V . t = Make_indexed_carbonated_data_storage_INTERNAL
module Make_carbonated_data_set_storage ( C : Raw_context . T ) ( I : INDEX ) : Carbonated_data_set_storage with type t = C . t and type elt = I . t = struct module V = struct type t = unit let encoding = Data_encoding . unit end module M = Make_indexed_carbonated_data_storage_INTERNAL ( C ) ( I ) ( V ) type t = M . t ...
module Make_indexed_data_snapshotable_storage ( C : Raw_context . T ) ( Snapshot_index : INDEX ) ( I : INDEX ) ( V : VALUE ) : Indexed_data_snapshotable_storage with type t = C . t and type snapshot = Snapshot_index . t and type key = I . t and type value = V . t = struct type snapshot = Snapshot_index . t let data_nam...
module Make_indexed_subcontext ( C : Raw_context . T ) ( I : INDEX ) : Indexed_raw_context with type t = C . t and type key = I . t and type ' a ipath = ' a I . ipath = struct type t = C . t type context = t type key = I . t type ' a ipath = ' a I . ipath let clear t = C . remove t [ ] >|= fun t -> C . project t let fo...
module type WRAPPER = sig type t type key val wrap : t -> key val unwrap : key -> t option end
module Wrap_indexed_data_storage ( C : Indexed_data_storage ) ( K : WRAPPER with type key := C . key ) : Indexed_data_storage with type t = C . t and type key = K . t and type value = C . value = struct type t = C . t type context = C . t type key = K . t type value = C . value let mem ctxt k = C . mem ctxt ( K . wrap ...
module type STORAGE = sig type state val state_encoding : state Data_encoding . t val empty : memo_size : int -> state val size : state -> int64 * int64 val get_memo_size : state -> int val get_root : state -> Core . Validator . Hash . t val mem_root : state -> Core . Validator . Hash . t -> bool val mem : state -> int...
module type Single_data_storage = sig type t type context = t type value val mem : context -> bool Lwt . t val get : context -> value tzresult Lwt . t val find : context -> value option tzresult Lwt . t val init : context -> value -> Raw_context . t tzresult Lwt . t val update : context -> value -> Raw_context . t tzre...
module type Non_iterable_indexed_data_storage = sig type t type context = t type key type value val mem : context -> key -> bool Lwt . t val get : context -> key -> value tzresult Lwt . t val find : context -> key -> value option tzresult Lwt . t val update : context -> key -> value -> Raw_context . t tzresult Lwt . t ...
module type Non_iterable_indexed_carbonated_data_storage = sig type t type context = t type key type value val mem : context -> key -> ( Raw_context . t * bool ) tzresult Lwt . t val get : context -> key -> ( Raw_context . t * value ) tzresult Lwt . t val find : context -> key -> ( Raw_context . t * value option ) tzre...
module type Non_iterable_indexed_carbonated_data_storage_with_values = sig include Non_iterable_indexed_carbonated_data_storage val list_values : ? offset : int -> ? length : int -> t -> ( Raw_context . t * value list ) tzresult Lwt . t end
module type Non_iterable_indexed_carbonated_data_storage_INTERNAL = sig include Non_iterable_indexed_carbonated_data_storage_with_values val fold_keys_unaccounted : context -> order [ ` : Sorted | ` Undefined ] -> init ' : a -> f ( : key -> ' a -> ' a Lwt . t ) -> ' a Lwt . t end
module type Indexed_data_storage = sig include Non_iterable_indexed_data_storage val clear : context -> Raw_context . t Lwt . t val keys : context -> key list Lwt . t val bindings : context -> ( key * value ) list Lwt . t val fold : context -> order [ ` : Sorted | ` Undefined ] -> init ' : a -> f ( : key -> value -> ' ...
module type Indexed_data_snapshotable_storage = sig type snapshot type key include Indexed_data_storage with type key := key module Snapshot : Indexed_data_storage with type key = snapshot * key and type value = value and type t = t val snapshot_exists : context -> snapshot -> bool Lwt . t val snapshot : context -> sna...
module type Data_set_storage = sig type t type context = t type elt val mem : context -> elt -> bool Lwt . t val add : context -> elt -> Raw_context . t Lwt . t val remove : context -> elt -> Raw_context . t Lwt . t val elements : context -> elt list Lwt . t val fold : context -> order [ ` : Sorted | ` Undefined ] -> i...
module type Carbonated_data_set_storage = sig type t type context = t type elt val mem : context -> elt -> ( Raw_context . t * bool ) tzresult Lwt . t val init : context -> elt -> ( Raw_context . t * int ) tzresult Lwt . t val add : context -> elt -> ( Raw_context . t * int * bool ) tzresult Lwt . t val remove : contex...
module type NAME = sig val name : Raw_context . key end
module type VALUE = sig type t val encoding : t Data_encoding . t end
module type REGISTER = sig val ghost : bool end
module type Indexed_raw_context = sig type t type context = t type key type ' a ipath val clear : context -> Raw_context . t Lwt . t val fold_keys : context -> order [ ` : Sorted | ` Undefined ] -> init ' : a -> f ( : key -> ' a -> ' a Lwt . t ) -> ' a Lwt . t val keys : context -> key list Lwt . t val remove : context...
module Shared = struct type ' a t = { mutable data : ' a ; lock : Lwt_idle_waiter . t } let create data = { data ; lock = Lwt_idle_waiter . create ( ) } let use { data ; lock } f = Lwt_idle_waiter . task lock ( fun ( ) -> f data ) let locked_use { data ; lock } f = Lwt_idle_waiter . force_idle lock ( fun ( ) -> f data ...
type store = { store_dir : [ ` Store_dir ] Naming . directory ; mutable main_chain_store : chain_store option ; context_index : Context . index ; protocol_store : Protocol_store . t ; allow_testchains : bool ; protocol_watcher : Protocol_hash . t Lwt_watcher . input ; global_block_watcher : ( chain_store * block ) Lwt_...
let current_head chain_store = Shared . use chain_store . chain_state ( fun { current_head ; _ } -> Lwt . return current_head )
let caboose chain_store = Block_store . caboose chain_store . block_store
let checkpoint chain_store = Shared . use chain_store . chain_state ( fun { checkpoint_data ; _ } -> Stored_data . get checkpoint_data )
let target chain_store = Shared . use chain_store . chain_state ( fun { target_data ; _ } -> Stored_data . get target_data )
let savepoint chain_store = Block_store . savepoint chain_store . block_store
let genesis chain_store = chain_store . chain_config . genesis
let history_mode chain_store = chain_store . chain_config . history_mode
let read_ancestor_hash { block_store ; _ } ~ distance hash = Block_store . get_hash block_store ( Block ( hash , distance ) )
let read_ancestor_hash_by_level chain_store head level = let open Lwt_syntax in let distance = Int32 . ( to_int ( sub ( Block_repr . level head ) level ) ) in let * ro = read_ancestor_hash chain_store ~ distance ( Block_repr . hash head ) in match ro with Ok ( Some x ) -> Lwt . return_some x | _ -> Lwt . return_none
let locked_is_acceptable_block chain_state ( hash , level ) = let open Lwt_syntax in let * ( _checkpoint_hash , checkpoint_level ) = Stored_data . get chain_state . checkpoint_data in if Compare . Int32 . ( checkpoint_level >= level ) then Lwt . return_false else let * o = Stored_data . get chain_state . target_data in...
let create_lockfile chain_dir = let open Lwt_syntax in protect ( fun ( ) -> let * fd = Lwt_unix . openfile ( Naming . lock_file chain_dir |> Naming . file_path ) [ Unix . O_CREAT ; O_RDWR ; O_CLOEXEC ; O_SYNC ] 0o644 in return_ok fd )
let lock_for_write lockfile = Lwt_unix . lockf lockfile Unix . F_LOCK 0
let lock_for_read lockfile = Lwt_unix . lockf lockfile Unix . F_RLOCK 0
let unlock lockfile = Lwt_unix . lockf lockfile Unix . F_ULOCK 0
let try_lock_for_write lockfile = let open Lwt_syntax in Lwt . catch ( fun ( ) -> let * ( ) = Lwt_unix . lockf lockfile Unix . F_TLOCK 0 in Lwt . return_true ) ( fun _ -> Lwt . return_false )
let may_unlock lockfile = Unit . catch_s ( fun ( ) -> unlock lockfile )
module Block = struct type nonrec block = block type t = block type metadata = Block_repr . metadata = { message : string option ; max_operations_ttl : int ; last_allowed_fork_level : Int32 . t ; block_metadata : Bytes . t ; operations_metadata : Bytes . t list list ; } let equal b b ' = Block_hash . equal ( Block_repr...
module Chain_traversal = struct let path chain_store ~ from_block ~ to_block = let open Lwt_syntax in if not Compare . Int32 . ( Block . level from_block <= Block . level to_block ) then invalid_arg " Chain_traversal . path " ; let rec loop acc current = if Block . equal from_block current then Lwt . return_some acc el...
module Chain = struct type nonrec chain_store = chain_store type t = chain_store type nonrec testchain = testchain type block_identifier = Block_services . block let global_store { global_store ; _ } = global_store let chain_id chain_store = chain_store . chain_id let chain_dir chain_store = chain_store . chain_dir let...
module Protocol = struct let all { protocol_store ; _ } = Protocol_store . all protocol_store let store { protocol_store ; protocol_watcher ; _ } protocol_hash protocol = let open Lwt_syntax in let * o = Protocol_store . store protocol_store protocol_hash protocol in match o with | None -> Lwt . return_none | p -> Lwt_...
let create_store ? block_cache_limit ~ context_index ~ chain_id ~ genesis ~ genesis_context ( ? history_mode = History_mode . default ) ~ allow_testchains store_dir = let open Lwt_tzresult_syntax in let store_dir_path = Naming . dir_path store_dir in let *! ( ) = Lwt_utils_unix . create_dir store_dir_path in let *! pro...
let load_store ? history_mode ? block_cache_limit store_dir ~ context_index ~ genesis ~ chain_id ~ allow_testchains ~ readonly ( ) = let open Lwt_tzresult_syntax in let chain_dir = Naming . chain_dir store_dir chain_id in let * ( ) = protect ( fun ( ) -> let * ( ) = Consistency . check_consistency chain_dir genesis in ...
let main_chain_store store = WithExceptions . Option . get ~ loc : __LOC__ store . main_chain_store
let init ? patch_context ? commit_genesis ? history_mode ( ? readonly = false ) ? block_cache_limit ~ store_dir ~ context_dir ~ allow_testchains genesis = let open Lwt_tzresult_syntax in let store_dir = Naming . store_dir ~ dir_path : store_dir in let chain_id = Chain_id . of_block_hash genesis . Genesis . block in let...
let close_store global_store = let open Lwt_syntax in Lwt_watcher . shutdown_input global_store . protocol_watcher ; Lwt_watcher . shutdown_input global_store . global_block_watcher ; let main_chain_store = WithExceptions . Option . get ~ loc : __LOC__ global_store . main_chain_store in let * ( ) = Chain . close_chain_...
let may_switch_history_mode ~ store_dir ~ context_dir genesis ~ new_history_mode = let open Lwt_tzresult_syntax in let store_dir = Naming . store_dir ~ dir_path : store_dir in let chain_id = Chain_id . of_block_hash genesis . Genesis . block in let chain_dir = Naming . chain_dir store_dir chain_id in let chain_dir_path...
let get_chain_store store chain_id = let chain_store = main_chain_store store in let rec loop chain_store = if Chain_id . equal ( Chain . chain_id chain_store ) chain_id then return chain_store else Shared . use chain_store . chain_state ( fun { active_testchain ; _ } -> match active_testchain with | None -> fail ( Val...
let get_chain_store_opt store chain_id = let open Lwt_syntax in let * r = get_chain_store store chain_id in match r with | Ok chain_store -> Lwt . return_some chain_store | Error _ -> Lwt . return_none
let all_chain_stores store = let chain_store = main_chain_store store in let rec loop acc chain_store = let acc = chain_store :: acc in Shared . use chain_store . chain_state ( fun { active_testchain ; _ } -> match active_testchain with | None -> Lwt . return acc | Some { testchain_store ; _ } -> loop acc testchain_sto...
let directory store = store . store_dir
let context_index store = store . context_index
let allow_testchains { allow_testchains ; _ } = allow_testchains
let global_block_watcher { global_block_watcher ; _ } = Lwt_watcher . create_stream global_block_watcher
let option_pp ~ default pp fmt = function | None -> Format . fprintf fmt " % s " default | Some x -> Format . fprintf fmt " % a " pp x
let rec make_pp_chain_store ( chain_store : chain_store ) = let open Lwt_syntax in let { chain_id ; chain_dir ; chain_config ; chain_state ; block_store ; _ } = chain_store in let chain_config_json = Data_encoding . Json . construct chain_config_encoding chain_config in let * ( current_head , alternate_heads , cementin...
let make_pp_store ( store : store ) = let open Lwt_syntax in let { store_dir ; allow_testchains ; main_chain_store ; _ } = store in let * pp_testchain_store = make_pp_chain_store ( WithExceptions . Option . get ~ loc : __LOC__ main_chain_store ) in Lwt . return ( fun fmt ( ) -> Format . fprintf fmt " [ @< v 2 > Store s...
module Unsafe = struct let repr_of_block b = b let block_of_repr b = b let get_block_store chain_store = chain_store . block_store let set_head chain_store new_head = let open Lwt_result_syntax in Shared . update_with chain_store . chain_state ( fun chain_state -> let * ( ) = Stored_data . write chain_state . current_h...
type _ t = | Stored_data : { mutable cache : ' a ; file : ( _ , ' a ) encoded_file ; scheduler : Lwt_idle_waiter . t ; } -> ' a t
let read_json_file file = let open Lwt_syntax in Option . catch_os ( fun ( ) -> let * r = Lwt_utils_unix . Json . read_file ( Naming . encoded_file_path file ) in match r with | Ok json -> let encoding = Naming . file_encoding file in Lwt . return_some ( Data_encoding . Json . destruct encoding json ) | _ -> Lwt . retu...
let read_file file = Lwt . try_bind ( fun ( ) -> let path = Naming . encoded_file_path file in Lwt_utils_unix . read_file path ) ( fun str -> let encoding = Naming . file_encoding file in Lwt . return ( Data_encoding . Binary . of_string_opt encoding str ) ) ( fun _ -> Lwt . return_none )
let get ( Stored_data v ) = Lwt_idle_waiter . task v . scheduler ( fun ( ) -> Lwt . return v . cache )
let write_file encoded_file data = let open Lwt_syntax in protect ( fun ( ) -> let encoding = Naming . file_encoding encoded_file in let path = Naming . encoded_file_path encoded_file in let encoder data = if Naming . is_json_file encoded_file then Data_encoding . Json . construct encoding data |> Data_encoding . Json ...