text
stringlengths 12
786k
|
---|
type _ addr = | OpenSSL : Socket . Address . Inet . t * Ssl . Config . t -> Socket . Address . Inet . t addr | Inet : Socket . Address . Inet . t -> Socket . Address . Inet . t addr | Unix : Socket . Address . Unix . t -> Socket . Address . Unix . t addr |
type _ tcp_sock = | Inet_sock : ( [ ` Active ] , Socket . Address . Inet . t ) Socket . t -> Socket . Address . Inet . t tcp_sock | Unix_sock : ( [ ` Active ] , Socket . Address . Unix . t ) Socket . t -> Socket . Address . Unix . t tcp_sock |
let ssl_schemes = [ " https " ; " wss " ] |
let mem_scheme s = List . mem ssl_schemes ~ equal : String . equal s |
let resolve_uri ( ? options = [ ] ) uri = let host = Option . value_exn ~ here [ :% here ] ~ message " : no host in URL " ( Uri . host uri ) in let service = match ( Uri . port uri , Uri_services . tcp_port_of_uri uri ) with | Some p , _ -> Some ( string_of_int p ) | None , Some p -> Some ( string_of_int p ) | _ -> None in let options = Unix . Addr_info . AI_FAMILY PF_INET :: options in Unix . Addr_info . get ~ host ? service options >>= function | [ ] -> failwithf " unable to resolve % s " ( Uri . to_string uri ) ( ) | { ai_addr ; _ } :: _ -> ( match ( Uri . scheme uri , ai_addr ) with | _ , ADDR_UNIX _ -> invalid_arg " uri must resolve to inet address " | Some s , ADDR_INET ( h , p ) when mem_scheme s -> return ( OpenSSL ( ` Inet ( h , p ) , Ssl . Config . create ( ) ) ) | _ , ADDR_INET ( h , p ) -> return ( Inet ( ` Inet ( h , p ) ) ) ) |
let connect ( type a ) ? interrupt ( addr : a addr ) : ( a tcp_sock * Reader . t * Writer . t ) Deferred . t = match addr with | Inet addr -> Tcp . connect ? interrupt ( Tcp . Where_to_connect . of_inet_address addr ) >>| fun ( s , r , w ) -> ( Inet_sock s , r , w ) | OpenSSL ( addr , cfg ) -> Tcp . connect ? interrupt ( Tcp . Where_to_connect . of_inet_address addr ) >>= fun ( s , rd , wr ) -> Ssl . connect ~ cfg rd wr >>| fun ( rd , wr ) -> ( Inet_sock s , rd , wr ) | Unix addr -> Tcp . connect ? interrupt ( Tcp . Where_to_connect . of_unix_address addr ) >>| fun ( s , r , w ) -> ( Unix_sock s , r , w ) |
let with_connection ( type a ) ? interrupt ( addr : a addr ) ( f : a tcp_sock -> Reader . t -> Writer . t -> ' a Deferred . t ) = match addr with | Inet addr -> Tcp . with_connection ? interrupt ( Tcp . Where_to_connect . of_inet_address addr ) ( fun s rd wr -> f ( Inet_sock s ) rd wr ) | OpenSSL ( addr , cfg ) -> Tcp . with_connection ? interrupt ( Tcp . Where_to_connect . of_inet_address addr ) ( fun s rd wr -> Ssl . connect ~ cfg rd wr >>= fun ( rd , wr ) -> Monitor . protect ( fun ( ) -> f ( Inet_sock s ) rd wr ) ~ finally ( : fun ( ) -> Deferred . all_unit [ Reader . close rd ; Writer . close wr ] ) ) | Unix addr -> Tcp . with_connection ? interrupt ( Tcp . Where_to_connect . of_unix_address addr ) ( fun s rd wr -> f ( Unix_sock s ) rd wr ) |
let connect_uri ? options ? interrupt uri = resolve_uri ? options uri >>= fun addr -> connect ? interrupt addr |
let with_connection_uri ? options ? interrupt uri f = resolve_uri ? options uri >>= fun addr -> with_connection ? interrupt addr f |
type trust_chain = [ ` Ca_file of string | ` Ca_path of string | ` Search_file_first_then_path of [ ` File of string ] * [ ` Path of string ] ] |
type openssl = [ ` OpenSSL of [ ` Crt_file_path of string ] * [ ` Key_file_path of string ] ] |
type requires_async_ssl = [ openssl | ` OpenSSL_with_trust_chain of openssl * trust_chain ] |
type server = [ ` TCP | requires_async_ssl ] [ @@ deriving sexp ] |
let serve ? max_connections ? backlog ? buffer_age_limit ~ on_handler_error mode where_to_listen handle_request = let handle_client handle_request sock rd wr = match mode with | ` TCP -> handle_request sock rd wr | # requires_async_ssl as async_ssl -> let crt_file , key_file , ca_file , ca_path = match async_ssl with | ` OpenSSL ( ` Crt_file_path crt_file , ` Key_file_path key_file ) -> ( crt_file , key_file , None , None ) | ` OpenSSL_with_trust_chain ( ` OpenSSL ( ` Crt_file_path crt , ` Key_file_path key ) , trust_chain ) -> let ca_file , ca_path = match trust_chain with | ` Ca_file ca_file -> ( Some ca_file , None ) | ` Ca_path ca_path -> ( None , Some ca_path ) | ` Search_file_first_then_path ( ` File ca_file , ` Path ca_path ) -> ( Some ca_file , Some ca_path ) in ( crt , key , ca_file , ca_path ) in let cfg = Ssl . Config . create ? ca_file ? ca_path ~ crt_file ~ key_file ( ) in Ssl . listen cfg rd wr >>= fun ( rd , wr ) -> Monitor . protect ( fun ( ) -> handle_request sock rd wr ) ~ finally ( : fun ( ) -> Deferred . all_unit [ Reader . close rd ; Writer . close wr ] ) in Tcp . Server . create ? max_connections ? backlog ? buffer_age_limit ~ on_handler_error where_to_listen ( handle_client handle_request ) |
type ssl_version = Ssl . version [ @@ deriving sexp ] |
type ssl_opt = Ssl . opt [ @@ deriving sexp ] |
type ssl_conn = Ssl . connection [ @@ deriving sexp_of ] |
type allowed_ciphers = [ ` Only of string list | ` Openssl_default | ` Secure ] |
type verify_mode = Ssl . verify_mode [ @@ deriving sexp_of ] |
type session = Ssl . session [ @@ deriving sexp_of ] |
module Ssl = struct module Config = Ssl . Config end |
module VAE = struct type t = { fc1 : Layer . t ; fc21 : Layer . t ; fc22 : Layer . t ; fc3 : Layer . t ; fc4 : Layer . t } let create vs = { fc1 = Layer . linear vs ~ input_dim : 784 400 ; fc21 = Layer . linear vs ~ input_dim : 400 20 ; fc22 = Layer . linear vs ~ input_dim : 400 20 ; fc3 = Layer . linear vs ~ input_dim : 20 400 ; fc4 = Layer . linear vs ~ input_dim : 400 784 } let encode t xs = let h1 = Layer . forward t . fc1 xs |> Tensor . relu in Layer . forward t . fc21 h1 , Layer . forward t . fc22 h1 let decode t zs = Layer . forward t . fc3 zs |> Tensor . relu |> Layer . forward t . fc4 |> Tensor . sigmoid let forward t xs = let mu , logvar = encode t ( Tensor . view xs ~ size [ : - 1 ; 784 ] ) in let std_ = Tensor . ( exp ( logvar * f 0 . 5 ) ) in let eps = Tensor . randn_like std_ in decode t Tensor . ( mu + ( eps * std_ ) ) , mu , logvar end |
let loss ~ recon_x ~ x ~ mu ~ logvar = let bce = Tensor . bce_loss recon_x ~ targets ( : Tensor . view x ~ size [ : - 1 ; 784 ] ) ~ reduction : Sum in let kld = Tensor . ( f ( - 0 . 5 ) * ( f 1 . 0 + logvar - ( mu * mu ) - exp logvar ) |> sum ) in Tensor . ( + ) bce kld |
let write_samples samples ~ filename = let samples = Tensor . ( samples * f 256 . ) in List . init 8 ~ f ( : fun i -> List . init 8 ~ f ( : fun j -> Tensor . narrow samples ~ dim : 0 ~ start ( ( : 4 * i ) + j ) ~ length : 1 ) |> Tensor . cat ~ dim : 2 ) |> Tensor . cat ~ dim : 3 |> Torch_vision . Image . write_image ~ filename |
let ( ) = let device = Device . cuda_if_available ( ) in let mnist = Mnist_helper . read_files ( ) in let vs = Var_store . create ~ name " : vae " ~ device ( ) in let vae = VAE . create vs in let opt = Optimizer . adam vs ~ learning_rate : 1e - 3 in for epoch_idx = 1 to 20 do let train_loss = ref 0 . in let samples = ref 0 . in Dataset_helper . iter mnist ~ batch_size ~ device ~ f ( : fun _ ~ batch_images ~ batch_labels : _ -> let recon_x , mu , logvar = VAE . forward vae batch_images in let loss = loss ~ recon_x ~ x : batch_images ~ mu ~ logvar in Optimizer . backward_step ~ loss opt ; train_loss := ! train_loss . + Tensor . float_value loss ; samples := ! samples . + ( Tensor . shape batch_images |> List . hd_exn |> Float . of_int ) ) ; Stdio . printf " epoch % 4d loss : % 12 . 6f \ n " %! epoch_idx ( ! train_loss . / ! samples ) ; Tensor . randn [ 64 ; 20 ] ~ device |> VAE . decode vae |> Tensor . to_device ~ device : Cpu |> Tensor . view ~ size [ : - 1 ; 1 ; 28 ; 28 ] |> write_samples ~ filename ( : Printf . sprintf " s_ % d . png " epoch_idx ) done |
module QRCode = struct type t = { source : Uuidm . t ; value : string ; } let create ~ source ~ value = { source ; value ; } let check t s = t . value = s let compare = compare let source t = t . source let value t = t . value end |
module User = struct type _t = { id : string ; source : Uuidm . t ; } type t = [ ` Granted of _t | ` Denied of _t | ` Unknown ] let granted ~ id ~ source = ` Granted { id ; source ; } let denied ~ id ~ source = ` Denied { id ; source ; } end |
module Event : sig type kind = [ ` QRCode of QRCode . t | ` User of User . t | ` Nil ] type qrcode = [ ` QRCode of QRCode . t ] type user = [ ` User of User . t ] type nil = [ ` Nil ] type ' a t val event : ' a t -> ' a val qrcode : QRCode . t -> [ > qrcode ] t val user : User . t -> [ > user ] t val nil : unit -> [ > nil ] t type kind = [ ` QRCode of QRCode . t | ` User of User . t | ` Nil ] type qrcode = [ ` QRCode of QRCode . t ] type user = [ ` User of User . t ] type nil = [ ` Nil ] type ' a t = { event : ' a ; timestamp : float ; } let create event = { event = event ; timestamp = Unix . gettimeofday ( ) ; } let event t = t . event let qrcode qr = create @@ ` QRCode qr let user u = create @@ ` User u let nil ( ) = create ` Nil end |
module UserDB : sig val create : int -> string array * int SMap . t let random_string len = let true_len = len / 8 * 8 + 8 in let b = Bytes . create true_len in for i = 0 to true_len / 8 - 1 do EndianBytes . BigEndian . set_int64 b ( i * 8 ) @@ Random . int64 Int64 . max_int done ; Bytes . ( sub b 0 len |> unsafe_to_string ) let create n = let user_to_qr = Array . make n " " in let rec generate a = function | 0 -> a | n -> let qrcode = random_string 10 in user_to_qr . ( n - 1 ) <- qrcode ; generate ( SMap . add qrcode ( n - 1 ) a ) @@ pred n in user_to_qr , generate SMap . empty n end |
type ' a t = ( ' a , Error . t Preface . Nonempty_list . t ) Preface . Validation . t |
let valid x = Preface . Validation . Valid x |
let invalid x = Preface . Validation . Invalid x |
let error x = invalid ( Preface . Nonempty_list . create x ) |
let pp inner_pp = Preface . ( Validation . pp inner_pp ( Nonempty_list . pp Error . pp ) ) |
let equal inner_eq = Preface . ( Validation . equal inner_eq ( Nonempty_list . equal Error . equal ) ) ; ; |
let to_try = function | Preface . Validation . Valid x -> Ok x | Preface . Validation . Invalid errs -> Error ( Error . List errs ) ; ; |
let from_try = function | Ok x -> Preface . Validation . Valid x | Error err -> Preface . ( Validation . Invalid ( Nonempty_list . create err ) ) ; ; |
module Error_list = Preface . Make . Semigroup . From_alt ( Preface . Nonempty_list . Alt ) ( Error ) |
module Functor = Preface . Validation . Functor ( Error_list ) |
module Applicative = Preface . Validation . Applicative ( Error_list ) |
module Selective = Preface . Validation . Selective ( Error_list ) |
module Monad = Preface . Validation . Monad ( Error_list ) |
module Alt = Preface . Validation . Alt ( Error_list ) |
module Infix = struct type nonrec ' a t = ' a t include ( Alt . Infix : Preface . Specs . Alt . INFIX with type ' a t := ' a t ) include ( Selective . Infix : Preface . Specs . Selective . INFIX with type ' a t := ' a t ) include ( Monad . Infix : Preface . Specs . Monad . INFIX with type ' a t := ' a t ) end |
module Syntax = struct type nonrec ' a t = ' a t include ( Applicative . Syntax : Preface . Specs . Applicative . SYNTAX with type ' a t := ' a t ) include ( Monad . Syntax : Preface . Specs . Monad . SYNTAX with type ' a t := ' a t ) end |
module type Raw = Raw |
type ( ' raw , ' witness ) t = ' raw |
module type S = S with type ( ' a , ' b ) validated := ( ' a , ' b ) t |
module type S_bin_io = S_bin_io with type ( ' a , ' b ) validated := ( ' a , ' b ) t |
module type S_bin_io_compare_hash_sexp = S_bin_io_compare_hash_sexp with type ( ' a , ' b ) validated := ( ' a , ' b ) t |
let raw t = t |
module Make ( Raw : Raw ) = struct type witness type t = Raw . t [ @@ deriving sexp_of ] let validation_failed t error = Error . create " validation failed " ( t , error , Raw . here ) [ % sexp_of : Raw . t * Error . t * Source_code_position . t ] ; ; let create_exn t = match Validate . result ( Raw . validate t ) with | Ok ( ) -> t | Error error -> Error . raise ( validation_failed t error ) ; ; let create t = match Validate . result ( Raw . validate t ) with | Ok ( ) -> Ok t | Error error -> Error ( validation_failed t error ) ; ; let t_of_sexp sexp = create_exn ( Raw . t_of_sexp sexp ) let raw t = t end |
module Add_bin_io ( Raw : sig type t [ @@ deriving bin_io ] include Raw_bin_io with type t := t end ) ( Validated : S with type raw := Raw . t ) = struct include Binable . Of_binable_without_uuid [ @ alert " - legacy " ] ( Raw ) ( struct type t = Raw . t let of_binable raw = if Raw . validate_binio_deserialization then Validated . create_exn raw else raw ; ; let to_binable = Fn . id end ) end |
module Add_compare ( Raw : sig type t [ @@ deriving compare ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) = struct let compare t1 t2 = [ % compare : Raw . t ] ( raw t1 ) ( raw t2 ) end |
module Add_hash ( Raw : sig type t [ @@ deriving hash ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) = struct let hash_fold_t state t = Raw . hash_fold_t state ( Validated . raw t ) let hash t = Raw . hash ( Validated . raw t ) end |
module Add_typerep ( Raw : sig type t [ @@ deriving typerep ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) = struct type t = Raw . t [ @@ deriving typerep ] end |
module Make_binable ( Raw : Raw_bin_io ) = struct module T0 = Make ( Raw ) include T0 include Add_bin_io ( Raw ) ( T0 ) end |
module Make_bin_io_compare_hash_sexp ( Raw : sig type t [ @@ deriving compare , hash ] include Raw_bin_io with type t := t end ) = struct module T = Make_binable ( Raw ) include T include Add_compare ( Raw ) ( T ) include ( Add_hash ( Raw ) ( T ) : sig type t [ @@ deriving hash ] end with type t := t ) end |
type t = Block . t State_hash . With_state_hashes . t * State_hash . t Non_empty_list . t |
let to_yojson ( block_with_hashes , _ ) _ = State_hash . With_state_hashes . to_yojson Block . to_yojson block_with_hashes |
let lift ( b , v ) v = match v with | _ , _ , _ , ( ` Delta_block_chain , Truth . True delta_block_chain_proof ) delta_block_chain_proof , _ , _ , _ -> ( b , delta_block_chain_proof ) delta_block_chain_proof |
let forget ( b , _ ) _ = b |
let remember ( b , delta_block_chain_proof ) delta_block_chain_proof = ( b , ( ( ` Time_received , Truth . True ( ) ) , ( ` Genesis_state , Truth . True ( ) ) , ( ` Proof , Truth . True ( ) ) , ( ` Delta_block_chain , Truth . True delta_block_chain_proof ) delta_block_chain_proof , ( ` Frontier_dependencies , Truth . True ( ) ) , ( ` Staged_ledger_diff , Truth . True ( ) ) , ( ` Protocol_versions , Truth . True ( ) ) ) ) |
let delta_block_chain_proof ( _ , d ) d = d |
let valid_commands ( block , _ ) _ = block |> With_hash . data |> Block . body |> Body . staged_ledger_diff |> Staged_ledger_diff . commands |> List . map ~ f ( : fun cmd -> let ( ` If_this_is_used_it_should_have_a_comment_justifying_it data ) data = User_command . to_valid_unsafe cmd . data in { cmd with data } ) |
let unsafe_of_trusted_block ~ delta_block_chain_proof ( ` This_block_is_trusted_to_be_safe b ) b = ( b , delta_block_chain_proof ) delta_block_chain_proof |
let state_hash ( b , _ ) _ = State_hash . With_state_hashes . state_hash b |
let state_body_hash ( t , _ ) _ = State_hash . With_state_hashes . state_body_hash t ~ compute_hashes : ( Fn . compose Mina_state . Protocol_state . hashes ( Fn . compose Header . protocol_state Block . header ) header ) |
let header t = t |> forget |> With_hash . data |> Block . header |
let body t = t |> forget |> With_hash . data |> Block . body |
module type Raw = sig type t [ @@ deriving sexp ] val here : Source_code_position . t val validate : t Validate . check end |
module type Raw_bin_io = sig type t [ @@ deriving bin_io ] include Raw with type t := t val validate_binio_deserialization : bool end |
module type Raw_bin_io_compare_hash_sexp = sig type t [ @@ deriving compare , hash ] include Raw_bin_io with type t := t end |
module type S = sig type ( ' raw , ' witness ) validated type witness type raw type t = ( raw , witness ) validated [ @@ deriving sexp ] val create : raw -> t Or_error . t val create_exn : raw -> t val raw : t -> raw end |
module type S_bin_io = sig include S include sig type t = ( raw , witness ) validated [ @@ deriving bin_io ] end with type t := t end |
module type S_bin_io_compare_hash_sexp = sig include S include sig type t = ( raw , witness ) validated [ @@ deriving bin_io , compare , hash ] end with type t := t end |
module type Validated = sig type ( ' raw , ' witness ) t = private ' raw val raw : ( ' raw , _ ) t -> ' raw module type Raw = Raw module type S = S with type ( ' a , ' b ) validated := ( ' a , ' b ) t module type S_bin_io = S_bin_io with type ( ' a , ' b ) validated := ( ' a , ' b ) t module type S_bin_io_compare_hash_sexp = S_bin_io_compare_hash_sexp with type ( ' a , ' b ) validated := ( ' a , ' b ) t module Make ( Raw : Raw ) : S with type raw := Raw . t module Make_binable ( Raw : Raw_bin_io ) : S_bin_io with type raw := Raw . t module Make_bin_io_compare_hash_sexp ( Raw : Raw_bin_io_compare_hash_sexp ) : S_bin_io_compare_hash_sexp with type raw := Raw . t module Add_bin_io ( Raw : sig type t [ @@ deriving bin_io ] include Raw_bin_io with type t := t end ) ( Validated : S with type raw := Raw . t ) : sig type t [ @@ deriving bin_io ] end with type t := Validated . t module Add_compare ( Raw : sig type t [ @@ deriving compare ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) : sig type t [ @@ deriving compare ] end with type t := Validated . t module Add_hash ( Raw : sig type t [ @@ deriving hash ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) : sig type t [ @@ deriving hash ] end with type t := Validated . t module Add_typerep ( Raw : sig type t [ @@ deriving typerep ] include Raw with type t := t end ) ( Validated : S with type raw := Raw . t ) : sig type t [ @@ deriving typerep ] end with type t := Validated . t end |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2010 - 05 - 15 " ] ) ; ( " Action " , [ " ValidateTemplate " ] ) ] ( Util . drop_empty ( Uri . query_of_encoded ( Query . render ( ValidateTemplateInput . to_query req ) ) ) ) ) in ( ` POST , uri , [ ] ) |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Util . option_bind ( Xml . member " ValidateTemplateResponse " ( snd xml ) ) ( Xml . member " ValidateTemplateResult " ) in try Util . or_error ( Util . option_bind resp ValidateTemplateOutput . parse ) ( let open Error in BadResponse { body ; message = " Could not find well formed ValidateTemplateOutput . " } ) with | Xml . RequiredFieldMissing msg -> let open Error in ` Error ( BadResponse { body ; message = ( " Error parsing ValidateTemplateOutput - missing field in body or children : " ^ msg ) } ) with | Failure msg -> ` Error ( let open Error in BadResponse { body ; message = ( " Error parsing xml : " ^ msg ) } ) |
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 test = " validate " >::: [ " basic " >:: ( fun ( ) -> " bounds " @? ( let res = V . name_list " foo " [ V . name " ok " ( Int . validate_bound ~ min ( : Incl 0 ) ~ max ( : Incl 100 ) 5 ) ; V . name " incl_lower " ( Int . validate_bound ~ min ( : Incl 0 ) ~ max ( : Incl 100 ) ( - 1 ) ) ; V . name " incl_lower " ( Int . validate_bound ~ min ( : Incl 0 ) ~ max ( : Incl 100 ) ( 0 ) ) ; V . name " incl_upper " ( Int . validate_bound ~ min ( : Incl 0 ) ~ max ( : Incl 100 ) ( 101 ) ) ; V . name " incl_upper " ( Int . validate_bound ~ min ( : Incl 0 ) ~ max ( : Incl 100 ) ( 100 ) ) ; V . name " excl_lower " ( Int . validate_bound ~ min ( : Excl 0 ) ~ max ( : Excl 100 ) ( 0 ) ) ; V . name " excl_upper " ( Int . validate_bound ~ min ( : Excl 0 ) ~ max ( : Excl 100 ) ( 100 ) ) ; V . name " excl_lower " ( Int . validate_bound ~ min ( : Excl 0 ) ~ max ( : Excl 100 ) ( 1 ) ) ; V . name " excl_upper " ( Int . validate_bound ~ min ( : Excl 0 ) ~ max ( : Excl 100 ) ( 99 ) ) ; ] in let expected = [ " ( foo . incl_lower " \ value - 1 < bound 0 " ) " \ ; " ( foo . incl_upper " \ value 101 > bound 100 " ) " \ ; " ( foo . excl_lower " \ value 0 <= bound 0 " ) " \ ; " ( foo . excl_upper " \ value 100 >= bound 100 " ) " \ ] in List . sort ~ compare : Poly . ascending ( V . errors res ) = List . sort ~ compare : Poly . ascending expected ; ) ; " inf / nan " @? ( let res = V . name_list " bar " [ V . name " ok " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 5 . ) ; V . name " nan " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) Float . nan ) ; V . name " inf " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) Float . infinity ) ; ] in let expected = [ " ( bar . nan " \ value is NaN " ) " \ ; " ( bar . inf " \ value is infinite " ) " \ ] in List . sort ~ compare : Poly . ascending ( V . errors res ) = List . sort ~ compare : Poly . ascending expected ; ) ; " nesting " @? ( let res = V . name_list " nesting " [ V . name " ok " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 5 . ) ; V . name " top " ( Float . validate_ordinary Float . nan ) ; V . name_list " sub0 " [ V . name " sub1 " ( Float . validate_ordinary Float . nan ) ; V . name " sub2 " ( Float . validate_ordinary Float . nan ) ; V . name_list " sub3 " [ V . name " sub4 " ( Float . validate_ordinary Float . nan ) ; V . name " sub5 " ( Float . validate_ordinary Float . nan ) ; ] ] ] in let expected = [ " ( nesting . top " \ value is NaN " ) " \ ; " ( nesting . sub0 . sub1 " \ value is NaN " ) " \ ; " ( nesting . sub0 . sub2 " \ value is NaN " ) " \ ; " ( nesting . sub0 . sub3 . sub4 " \ value is NaN " ) " \ ; " ( nesting . sub0 . sub3 . sub5 " \ value is NaN " ) " \ ] in List . sort ~ compare : Poly . ascending ( V . errors res ) = List . sort ~ compare : Poly . ascending expected ; ) ; " empty " @? ( let res = V . name_list " " [ V . name " ok1 " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 5 . ) ; V . name " ok2 " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 6 . ) ; V . name_list " sub " [ V . name " ok3 " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 22 . ) ; V . name " ok4 " ( Float . validate_bound ~ min ( : Incl 0 . ) ~ max ( : Incl 100 . ) 36 . ) ; ] ] in let expected = [ ] in List . sort ~ compare : Poly . ascending ( V . errors res ) = List . sort ~ compare : Poly . ascending expected ) ; ) ] |
module Error = Preface_stdlib . Nonempty_list . Semigroup ( struct type t = Preface_stdlib . Exn . t end ) |
module Req = struct type ( ' a , ' b ) t = ( ' a , ' b ) Preface_stdlib . Validation . t let arbitrary x y = Preface_qcheck . Arbitrary . validation x y let observable x y = Preface_qcheck . Observable . validation x y let equal x y = Preface_stdlib . Validation . equal x y end |
module Req_with_exn = struct type ' a t = ( ' a , Error . t ) Preface_stdlib . Validation . t let arbitrary x = Preface_qcheck . Arbitrary . validation x Preface_qcheck . Arbitrary . ( nonempty_list ( exn ( ) ) ) ; ; let observable x = Preface_qcheck . Observable . validation x Preface_qcheck . Observable . ( nonempty_list exn ) ; ; let equal f = Preface_stdlib . Validation . equal f ( Preface_stdlib . Nonempty_list . equal Preface_stdlib . Exn . equal ) ; ; end |
module Functor = Preface_laws . Functor . Cases ( Preface_stdlib . Validation . Functor ( Error ) ) ( Req_with_exn ) ( Preface_qcheck . Sample . Pack1 ) |
module Alt = Preface_laws . Alt . Semigroup_cases ( Preface_stdlib . Validation . Alt ( Error ) ) ( Req_with_exn ) ( Preface_qcheck . Sample . Pack1 ) |
module Applicative = Preface_laws . Applicative . Cases ( Preface_stdlib . Validation . Applicative ( Error ) ) ( Req_with_exn ) ( Preface_qcheck . Sample . Pack1 ) |
module Monad = Preface_laws . Monad . Cases ( Preface_stdlib . Validation . Monad ( Error ) ) ( Req_with_exn ) ( Preface_qcheck . Sample . Pack1 ) |
module Bifunctor = Preface_laws . Bifunctor . Cases ( Preface_stdlib . Validation . Bifunctor ) ( Req ) ( Preface_qcheck . Sample . Pack1 ) |
module Selective = Preface_laws . Selective . Cases ( Preface_stdlib . Validation . Selective ( Error ) ) ( Req_with_exn ) ( Preface_qcheck . Sample . Pack1 ) |
let cases n = [ ( " Validation ( with error nonempty list as Error part ) Functor Laws " , Functor . cases n ) ; ( " Validation ( with error nonempty list as Error part ) Alt Semigroup Laws " , Alt . cases n ) ; ( " Validation ( with error nonempty list as Error part ) Applicative Laws " , Applicative . cases n ) ; ( " Validation ( with error nonempty list as Error part ) Monad Laws " , Monad . cases n ) ; ( " Validation Bifunctor Laws " , Bifunctor . cases n ) ; ( " Validation Selective ( with error nonempty list as Error part ) Laws " , Selective . cases n ) ] ; ; |
type validation_result = [ ` Accept | ` Reject | ` Ignore ] [ @@ deriving equal ] equal |
type t = { expiration : Time_ns . t option ; signal : validation_result Ivar . t ; mutable message_type : [ ` Unknown | ` Block | ` Snark_work | ` Transaction ] } |
let create expiration = { expiration = Some expiration ; signal = Ivar . create ( ) ; message_type = ` Unknown } |
let create_without_expiration ( ) = { expiration = None ; signal = Ivar . create ( ) ; message_type = ` Unknown } |
let is_expired cb = match cb . expiration with | None -> false | Some expires_at -> Time_ns ( . now ( ) >= expires_at ) expires_at |
module type Metric_intf = sig val validations_timed_out : Mina_metrics . Counter . t val rejected : Mina_metrics . Counter . t val ignored : Mina_metrics . Counter . t module Validation_time : sig val update : Time . Span . t -> unit end end |
let metrics_of_message_type m : ( module Metric_intf ) Metric_intf option = match m with | ` Unknown -> None | ` Block -> Some ( module Mina_metrics . Network . Block ) Block | ` Snark_work -> Some ( module Mina_metrics . Network . Snark_work ) Snark_work | ` Transaction -> Some ( module Mina_metrics . Network . Transaction ) Transaction |
let record_timeout_metrics cb = Mina_metrics ( . Counter . inc_one Network . validations_timed_out ) validations_timed_out ; match metrics_of_message_type cb . message_type with | None -> ( ) | Some ( module M ) M -> Mina_metrics . Counter . inc_one M . validations_timed_out |
let record_validation_metrics message_type ( result : validation_result ) validation_result validation_time = match metrics_of_message_type message_type with | None -> ( ) | Some ( module M ) M -> ( match result with | ` Ignore -> Mina_metrics . Counter . inc_one M . ignored | ` Accept -> M . Validation_time . update validation_time | ` Reject -> Mina_metrics . Counter . inc_one M . rejected ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.