text
stringlengths
0
601k
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_...
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 . rende...
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 ; mes...
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 " ( In...
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_...
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 ) ; ( "...
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 . Transa...
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 v...
let await_timeout cb = if is_expired cb then Deferred . return ( ) else match cb . expiration with | None -> Deferred . never ( ) | Some expires_at -> after ( Time_ns . Span . to_span_float_round_nearest @@ Time_ns . diff expires_at ( Time_ns . now ( ) ) )
let await cb = if is_expired cb then ( record_timeout_metrics cb ; Deferred . return None ) None else match cb . expiration with | None -> Ivar . read cb . signal >>| Option . some | Some expires_at -> ( match % map Timeout . await ( ) ~ timeout_duration ( : Time_ns . diff expires_at ( Time_ns . now ( ) ) ) ( Ivar . re...
let await_exn cb = match % map await cb with None -> failwith " timeout " | Some result -> result
let fire_if_not_already_fired cb result = if not ( is_expired cb ) cb then ( if Ivar . is_full cb . signal then [ % log ' error ( Logger . create ( ) ) ] " Ivar . fill bug is here " ! ; Ivar . fill cb . signal result )
let set_message_type t x = t . message_type <- x
type error += Oversized_operation of { size : int ; max : int }
type error += | Future_block_header of { block : Block_hash . t ; block_time : Time . Protocol . t ; time : Time . System . t ; }
let ( ) = register_error_kind ` Permanent ~ id " : node . prevalidation . parse_error " ~ title " : Parsing error in prevalidation " ~ description : " Raised when an operation has not been parsed correctly during \ prevalidation . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Operation parsing error in prevalidation...
type error += Unknown_chain of Chain_id . t
type error += Block_not_invalid of Block_hash . t
let ( ) = register_error_kind ` Permanent ~ id " : node . state . unknown_chain " ~ title " : Unknown chain " ~ description : " The chain identifier could not be found in the chain identifiers table . " ~ pp ( : fun ppf id -> Format . fprintf ppf " Unknown chain % a " Chain_id . pp id ) Data_encoding . ( obj1 ( req " c...
type error += Inconsistent_hash of Context_hash . t * Context_hash . t
type error += Missing_block_metadata_hash of Block_hash . t
type error += Missing_operation_metadata_hashes of Block_hash . t
let ( ) = register_error_kind ` Permanent ~ id " : node . state . block . inconsistent_context_hash " ~ title " : Inconsistent commit hash " ~ description : " When committing the context of a block , the announced context hash was \ not the one computed at commit time . " ~ pp ( : fun ppf ( got , exp ) -> Format . fpri...
type error += Invalid_locator of P2p_peer . Id . t * Block_locator . t
type error += Too_short_locator of P2p_peer . Id . t * Block_locator . t
let ( ) = register_error_kind ` Permanent ~ id " : node . bootstrap_pipeline . invalid_locator " ~ title " : Invalid block locator " ~ description " : Block locator is invalid . " ~ pp ( : fun ppf ( id , locator ) -> Format . fprintf ppf " Invalid block locator on peer % a :\ n % a " P2p_peer . Id . pp id Block_locator...
type protocol_error = Compilation_failed | Dynlinking_failed
type error += | Invalid_protocol of { hash : Protocol_hash . t ; error : protocol_error }
let protocol_error_encoding = let open Data_encoding in union [ case ( Tag 0 ) ~ title " : Compilation failed " ( obj1 ( req " error " ( constant " compilation_failed " ) ) ) ( function Compilation_failed -> Some ( ) | _ -> None ) ( fun ( ) -> Compilation_failed ) ; case ( Tag 1 ) ~ title " : Dynlinking failed " ( obj1...
let pp_protocol_error ppf = function | Compilation_failed -> Format . fprintf ppf " compilation error " | Dynlinking_failed -> Format . fprintf ppf " dynlinking error "
let ( ) = register_error_kind ` Permanent ~ id " : node . protocol_validator . invalid_protocol " ~ title " : Invalid protocol " ~ description " : Invalid protocol . " ~ pp ( : fun ppf ( protocol , error ) -> Format . fprintf ppf " [ @< v 2 > Invalid protocol % a @ % a ] " @ Protocol_hash . pp_short protocol pp_protoco...
type error += Cannot_load_protocol of Protocol_hash . t
let ( ) = register_error_kind ` Permanent ~ id " : node . protocol_validator . cannot_load_protocol " ~ title " : Cannot load protocol " ~ description " : Cannot load protocol from disk " ~ pp ( : fun ppf protocol -> Format . fprintf ppf " Failed to load the protocol % a from disk : the corresponding files \ might be m...
type error += Unknown_ancestor | Known_invalid
let ( ) = register_error_kind ` Permanent ~ id " : node . peer_validator . unknown_ancestor " ~ title " : Unknown ancestor " ~ description " : Unknown ancestor block found in the peer ' s chain " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Unknown ancestor " ) Data_encoding . empty ( function Unknown_ancestor -> Som...
type error += Inactive_chain of Chain_id . t
type error += Checkpoint_error of Block_hash . t * P2p_peer . Id . t option
let ( ) = register_error_kind ` Branch ~ id " : node . validator . inactive_chain " ~ title " : Inactive chain " ~ description " : Attempted validation of a block from an inactive chain . " ~ pp ( : fun ppf chain -> Format . fprintf ppf " Tried to validate a block from chain % a , that is not currently \ considered act...
type ( ' time_received , ' genesis_state , ' proof , ' delta_block_chain , ' frontier_dependencies , ' staged_ledger_diff , ' protocol_versions ) t = ' time_received * ' genesis_state * ' proof * ' delta_block_chain * ' frontier_dependencies * ' staged_ledger_diff * ' protocol_versions constraint ' delta_block_chain = ...
type fully_invalid = ( [ ` Time_received ] * unit Truth . false_t , [ ` Genesis_state ] * unit Truth . false_t , [ ` Proof ] * unit Truth . false_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . false_t , [ ` Frontier_dependencies ] * unit Truth . false_t , [ ` Staged_ledger_diff ] * unit Truth ....
type initial_valid = ( [ ` Time_received ] * unit Truth . true_t , [ ` Genesis_state ] * unit Truth . true_t , [ ` Proof ] * unit Truth . true_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . true_t , [ ` Frontier_dependencies ] * unit Truth . false_t , [ ` Staged_ledger_diff ] * unit Truth . fal...
type almost_valid = ( [ ` Time_received ] * unit Truth . true_t , [ ` Genesis_state ] * unit Truth . true_t , [ ` Proof ] * unit Truth . true_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . true_t , [ ` Frontier_dependencies ] * unit Truth . true_t , [ ` Staged_ledger_diff ] * unit Truth . false...
type fully_valid = ( [ ` Time_received ] * unit Truth . true_t , [ ` Genesis_state ] * unit Truth . true_t , [ ` Proof ] * unit Truth . true_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . true_t , [ ` Frontier_dependencies ] * unit Truth . true_t , [ ` Staged_ledger_diff ] * unit Truth . true_t...
type ( ' time_received , ' genesis_state , ' proof , ' delta_block_chain , ' frontier_dependencies , ' staged_ledger_diff , ' protocol_versions ) with_block = Block . with_hash * ( ' time_received , ' genesis_state , ' proof , ' delta_block_chain , ' frontier_dependencies , ' staged_ledger_diff , ' protocol_versions ) ...
type fully_invalid_with_block = ( [ ` Time_received ] * unit Truth . false_t , [ ` Genesis_state ] * unit Truth . false_t , [ ` Proof ] * unit Truth . false_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . false_t , [ ` Frontier_dependencies ] * unit Truth . false_t , [ ` Staged_ledger_diff ] * u...
type initial_valid_with_block = ( [ ` Time_received ] * unit Truth . true_t , [ ` Genesis_state ] * unit Truth . true_t , [ ` Proof ] * unit Truth . true_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . true_t , [ ` Frontier_dependencies ] * unit Truth . false_t , [ ` Staged_ledger_diff ] * unit ...
type almost_valid_with_block = ( [ ` Time_received ] * unit Truth . true_t , [ ` Genesis_state ] * unit Truth . true_t , [ ` Proof ] * unit Truth . true_t , [ ` Delta_block_chain ] * State_hash . t Non_empty_list . t Truth . true_t , [ ` Frontier_dependencies ] * unit Truth . true_t , [ ` Staged_ledger_diff ] * unit Tr...
type fully_valid_with_block = Block . with_hash * fully_valid
let fully_invalid : fully_invalid = ( ( ` Time_received , Truth . False ) False , ( ` Genesis_state , Truth . False ) False , ( ` Proof , Truth . False ) False , ( ` Delta_block_chain , Truth . False ) False , ( ` Frontier_dependencies , Truth . False ) False , ( ` Staged_ledger_diff , Truth . False ) False , ( ` Proto...
type t = { state : Store . t ; db : Distributed_db . t ; block_validator : Block_validator . t ; chain_validator_limits : Chain_validator . limits ; peer_validator_limits : Peer_validator . limits ; block_validator_limits : Block_validator . limits ; prevalidator_limits : Prevalidator . limits ; start_testchain : bool ...
let create state db peer_validator_limits block_validator_limits block_validator_kind prevalidator_limits chain_validator_limits ~ start_testchain = Block_validator . create block_validator_limits db block_validator_kind ~ start_testchain >>=? fun block_validator -> let valid_block_input = Lwt_watcher . create_input ( ...
let activate v ~ start_prevalidator ~ validator_process chain_store = let chain_id = Store . Chain . chain_id chain_store in Validator_event . ( emit activate_chain ) chain_id >>= fun ( ) -> match Chain_id . Table . find v . active_chains chain_id with | Some chain -> return chain | None -> Chain_validator . create ~ s...
let get { active_chains ; _ } chain_id = match Chain_id . Table . find active_chains chain_id with | Some nv -> Ok nv | None -> error ( Validation_errors . Inactive_chain chain_id )
let get_active_chains { active_chains ; _ } = let l = Chain_id . Table . fold ( fun c _ acc -> c :: acc ) active_chains [ ] in List . rev l
let read_block store h = Store . all_chain_stores store >>= fun chain_stores -> Lwt_utils . find_map_s ( fun chain_store -> Store . Block . read_block_opt chain_store h >|= Option . map ( fun b -> ( Store . Chain . chain_id chain_store , b ) ) ) chain_stores
let read_block_header db h = read_block ( Distributed_db . store db ) h >|= Option . map ( fun ( chain_id , block ) -> ( chain_id , Store . Block . header block ) )
let validate_block v ( ? force = false ) ? chain_id bytes operations = let hash = Block_hash . hash_bytes [ bytes ] in match Block_header . of_bytes bytes with | None -> failwith " Cannot parse block header . " | Some block -> if not ( Clock_drift . is_not_too_far_in_the_future block . shell . timestamp ) then failwith...
let shutdown { active_chains ; block_validator ; _ } = let chain_validator_jobs = List . of_seq @@ Seq . map ( fun ( id , nv ) -> Validator_event . ( emit shutdown_chain_validator ) id >>= fun ( ) -> Chain_validator . shutdown nv ) ( Chain_id . Table . to_seq active_chains ) in Lwt . join chain_validator_jobs >>= fun (...
let watcher { valid_block_input ; _ } = Lwt_watcher . create_stream valid_block_input
let chains_watcher { chains_input ; _ } = Lwt_watcher . create_stream chains_input
let inject_operation v ? chain_id ~ force op = let inject_operation_on nv ~ handle_missing_prevalidator = match Chain_validator . prevalidator nv with | Some pv -> Prevalidator . inject_operation pv ~ force op | None -> handle_missing_prevalidator in let handle_missing_prevalidator = failwith " Prevalidator is not runn...
let distributed_db { db ; _ } = db
let section = [ " node " ; " validator " ]
let activate_chain = declare_1 ~ section ~ name " : activate_chain " ~ msg " : activate chain { chain } " ~ level : Notice ( " chain " , Chain_id . encoding )
let shutdown_block_validator = declare_0 ~ section ~ name " : shutdown_block_validator " ~ msg " : shutting down the block validator " ~ level : Notice ( )
let shutdown_chain_validator = declare_1 ~ section ~ name " : shutdown_chain_validator " ~ msg " : shutting down the chain validator { chain } " ~ level : Notice ( " chain " , Chain_id . encoding )
type t = | Empty | Node of t * key * t * int * Int64 . t
let height = function | Empty -> 0 | Node ( _ , _ , _ , h , _ ) -> h
let accval = function | Empty -> 0L | Node ( _ , _ , _ , _ , acc ) -> acc
let weight = function | Empty -> 0L | Node ( l , _ , r , _ , acc ) -> Int64 . sub ( Int64 . sub acc ( accval l ) ) ( accval r )
let rec print f = function | Empty -> Pp . empty_set f | Node ( l , k , r , _ , acc ) as x -> Format . fprintf f " [ @< hov 2 ><% d , % Li ( % Li ) , [ >@% a , @|% a , ] " @ k acc ( weight x ) print l print r