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_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 ) |
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 . read cb . signal ) signal with | ` Ok result -> let validation_time = Time_ns . abs_diff expires_at ( Time_ns . now ( ) ) |> Time_ns . Span . to_ms |> Time . Span . of_ms in record_validation_metrics cb . message_type result validation_time ; Some result | ` Timeout -> record_timeout_metrics cb ; None ) |
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 . " ) Data_encoding . empty ( function Parse_error -> Some ( ) | _ -> None ) ( fun ( ) -> Parse_error ) ; register_error_kind ` Temporary ~ id " : node . prevalidation . too_many_operations " ~ title " : Too many pending operations in prevalidation " ~ description " : The prevalidation context is full . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Too many operations in prevalidation context . " ) Data_encoding . empty ( function Too_many_operations -> Some ( ) | _ -> None ) ( fun ( ) -> Too_many_operations ) ; register_error_kind ` Permanent ~ id " : node . prevalidation . oversized_operation " ~ title " : Oversized operation " ~ description " : The operation size is bigger than allowed . " ~ pp ( : fun ppf ( size , max ) -> Format . fprintf ppf " Oversized operation ( size : % d , max : % d ) " size max ) Data_encoding . ( obj2 ( req " size " int31 ) ( req " max_size " int31 ) ) ( function Oversized_operation { size ; max } -> Some ( size , max ) | _ -> None ) ( fun ( size , max ) -> Oversized_operation { size ; max } ) ; register_error_kind ` Temporary ~ id " : node . prevalidation . future_block_header " ~ title " : Future block header " ~ description " : The block was annotated with a time too far in the future . " ~ pp ( : fun ppf ( block , block_time , time ) -> Format . fprintf ppf " Future block header ( block : % a , block_time : % a , time : % a ) " Block_hash . pp block Time . System . pp_hum ( Time . System . of_protocol_exn block_time ) Time . System . pp_hum time ) Data_encoding . ( obj3 ( req " block " Block_hash . encoding ) ( req " block_time " Time . Protocol . encoding ) ( req " time " Time . System . encoding ) ) ( function | Future_block_header { block ; block_time ; time } -> Some ( block , block_time , time ) | _ -> None ) ( fun ( block , block_time , time ) -> Future_block_header { block ; block_time ; time } ) ; Error_monad . register_error_kind ` Permanent ~ id " : block_validation . cannot_serialize_metadata " ~ title " : Cannot serialize metadata " ~ description " : Unable to serialize metadata " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Unable to serialize the metadata for an operation . " ) Data_encoding . empty ( function Cannot_serialize_operation_metadata -> Some ( ) | _ -> None ) ( fun ( ) -> Cannot_serialize_operation_metadata ) |
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 " chain " Chain_id . encoding ) ) ( function Unknown_chain x -> Some x | _ -> None ) ( fun x -> Unknown_chain x ) ; register_error_kind ` Permanent ~ id " : node . state . bad_data_dir " ~ title " : Bad data directory " ~ description : " The data directory could not be read . This could be because it was \ generated with an old version of the tezos - node program . Deleting and \ regenerating this directory may fix the problem . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Bad data directory . " ) Data_encoding . empty ( function Bad_data_dir -> Some ( ) | _ -> None ) ( fun ( ) -> Bad_data_dir ) ; register_error_kind ` Permanent ~ id " : node . state . block_not_invalid " ~ title " : Block not invalid " ~ description " : The invalid block to be unmarked was not actually invalid . " ~ pp ( : fun ppf block -> Format . fprintf ppf " Block % a was expected to be invalid , but was not actually invalid . " Block_hash . pp block ) Data_encoding . ( obj1 ( req " block " Block_hash . encoding ) ) ( function Block_not_invalid block -> Some block | _ -> None ) ( fun block -> Block_not_invalid block ) |
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 . fprintf ppf " [ @< v 2 > Inconsistent hash :@ got : % a @ expected : % a " Context_hash . pp got Context_hash . pp exp ) Data_encoding . ( obj2 ( req " wrong_context_hash " Context_hash . encoding ) ( req " expected_context_hash " Context_hash . encoding ) ) ( function Inconsistent_hash ( got , exp ) -> Some ( got , exp ) | _ -> None ) ( fun ( got , exp ) -> Inconsistent_hash ( got , exp ) ) ; register_error_kind ` Permanent ~ id " : node . state . block . missing_block_metadata_hash " ~ title " : Missing block metadata hash " ~ description : " A block was expected to commit to a block metadata hash , however none \ was given . " ~ pp ( : fun ppf block -> Format . fprintf ppf " [ @< v 2 > Missing block metadata hash at block : % a " Block_hash . pp block ) Data_encoding . ( obj1 ( req " block " Block_hash . encoding ) ) ( function Missing_block_metadata_hash block -> Some block | _ -> None ) ( fun block -> Missing_block_metadata_hash block ) ; register_error_kind ` Permanent ~ id " : node . state . block . missing_operation_metadata_hashes " ~ title " : Missing operation metadata hashes " ~ description : " A block was expected to commit to operation metadata hashes , however \ none were given . " ~ pp ( : fun ppf block -> Format . fprintf ppf " [ @< v 2 > Missing operation metadata hashes at block : % a " Block_hash . pp block ) Data_encoding . ( obj1 ( req " block " Block_hash . encoding ) ) ( function | Missing_operation_metadata_hashes block -> Some block | _ -> None ) ( fun block -> Missing_operation_metadata_hashes block ) |
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 . pp locator ) Data_encoding . ( obj2 ( req " id " P2p_peer . Id . encoding ) ( req " locator " Block_locator . encoding ) ) ( function Invalid_locator ( id , loc ) -> Some ( id , loc ) | _ -> None ) ( fun ( id , loc ) -> Invalid_locator ( id , loc ) ) ; register_error_kind ` Permanent ~ id " : node . bootstrap_pipeline . too_short_locator " ~ title " : Too short locator " ~ description " : Block locator is too short . " ~ pp ( : fun ppf ( id , locator ) -> Format . fprintf ppf " Too short locator on peer % a :\ n % a " P2p_peer . Id . pp id Block_locator . pp locator ) Data_encoding . ( obj2 ( req " id " P2p_peer . Id . encoding ) ( req " locator " Block_locator . encoding ) ) ( function Too_short_locator ( id , loc ) -> Some ( id , loc ) | _ -> None ) ( fun ( id , loc ) -> Too_short_locator ( id , loc ) ) |
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 ( req " error " ( constant " dynlinking_failed " ) ) ) ( function Dynlinking_failed -> Some ( ) | _ -> None ) ( fun ( ) -> Dynlinking_failed ) ; ] |
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_protocol_error error ) Data_encoding . ( merge_objs ( obj1 ( req " invalid_protocol " Protocol_hash . encoding ) ) protocol_error_encoding ) ( function | Invalid_protocol { hash ; error } -> Some ( hash , error ) | _ -> None ) ( fun ( hash , error ) -> Invalid_protocol { hash ; error } ) |
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 missing or corrupted . " Protocol_hash . pp protocol ) Data_encoding . ( obj1 ( req " protocol " Protocol_hash . encoding ) ) ( function Cannot_load_protocol protocol -> Some protocol | _ -> None ) ( fun protocol -> Cannot_load_protocol protocol ) |
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 -> Some ( ) | _ -> None ) ( fun ( ) -> Unknown_ancestor ) ; register_error_kind ` Permanent ~ id " : node . peer_validator . known_invalid " ~ title " : Known invalid " ~ description " : Known invalid block found in the peer ' s chain " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Known invalid " ) Data_encoding . empty ( function Known_invalid -> Some ( ) | _ -> None ) ( fun ( ) -> Known_invalid ) |
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 active . " Chain_id . pp chain ) Data_encoding . ( obj1 ( req " inactive_chain " Chain_id . encoding ) ) ( function Inactive_chain chain -> Some chain | _ -> None ) ( fun chain -> Inactive_chain chain ) ; register_error_kind ` Branch ~ id " : node . validator . checkpoint_error " ~ title " : Block incompatible with the current checkpoint . " ~ description : " The block belongs to a branch that is not compatible with the current \ checkpoint . " ~ pp ( : fun ppf ( block , peer ) -> match peer with | None -> Format . fprintf ppf " The block % a is incompatible with the current checkpoint . " Block_hash . pp_short block | Some peer -> Format . fprintf ppf " The peer % a send us a block which is a sibling of the configured \ checkpoint ( % a ) . " P2p_peer . Id . pp peer Block_hash . pp_short block ) Data_encoding . ( obj2 ( req " block " Block_hash . encoding ) ( opt " peer " P2p_peer . Id . encoding ) ) ( function | Checkpoint_error ( block , peer ) -> Some ( block , peer ) | _ -> None ) ( fun ( block , peer ) -> Checkpoint_error ( block , peer ) ) |
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 = [ ` Delta_block_chain ] * ( State_hash . t Non_empty_list . t , _ ) _ Truth . t constraint ' frontier_dependencies = [ ` Frontier_dependencies ] * ( unit , _ ) _ Truth . t constraint ' staged_ledger_diff = [ ` Staged_ledger_diff ] * ( unit , _ ) _ Truth . t constraint ' protocol_versions = [ ` Protocol_versions ] * ( unit , _ ) _ Truth . t ) * |
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 . false_t , [ ` Protocol_versions ] * unit Truth . false_t ) t |
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 . false_t , [ ` Protocol_versions ] * unit Truth . true_t ) t |
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_t , [ ` Protocol_versions ] * unit Truth . true_t ) t |
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 , [ ` Protocol_versions ] * unit Truth . true_t ) 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 ) t |
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 ] * unit Truth . false_t , [ ` Protocol_versions ] * unit Truth . false_t ) with_block |
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 Truth . false_t , [ ` Protocol_versions ] * unit Truth . true_t ) with_block |
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 Truth . false_t , [ ` Protocol_versions ] * unit Truth . true_t ) with_block |
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 , ( ` Protocol_versions , Truth . False ) False ) |
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 ; valid_block_input : Store . Block . t Lwt_watcher . input ; chains_input : ( Chain_id . t * bool ) Lwt_watcher . input ; active_chains : Chain_validator . t Chain_id . Table . t ; } |
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 ( ) in let chains_input = Lwt_watcher . create_input ( ) in return { state ; db ; start_testchain ; block_validator ; block_validator_limits ; prevalidator_limits ; peer_validator_limits ; chain_validator_limits ; valid_block_input ; chains_input ; active_chains = Chain_id . Table . create 7 ; } |
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 ~ start_prevalidator ~ start_testchain : v . start_testchain ~ active_chains : v . active_chains ~ block_validator_process : validator_process v . peer_validator_limits v . prevalidator_limits v . block_validator v . valid_block_input v . chains_input v . db chain_store v . chain_validator_limits |
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 " Block in the future . " else ( match chain_id with | None -> ( read_block_header v . db block . shell . predecessor >>= function | None -> failwith " Unknown predecessor ( % a ) , cannot inject the block . " Block_hash . pp_short block . shell . predecessor | Some ( chain_id , _bh ) -> Lwt . return ( get v chain_id ) ) | Some chain_id -> ( Lwt . return ( get v chain_id ) >>=? fun nv -> if force then return nv else Distributed_db . Block_header . known ( Chain_validator . chain_db nv ) block . shell . predecessor >>= function | true -> return nv | false -> failwith " Unknown predecessor ( % a ) , cannot inject the block . " Block_hash . pp_short block . shell . predecessor ) ) >>=? fun nv -> let validation = Chain_validator . validate_block nv ~ force hash block operations in return ( hash , validation ) |
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 ( ) -> Validator_event . ( emit shutdown_block_validator ) ( ) >>= fun ( ) -> Block_validator . shutdown block_validator |
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 running , cannot inject the operation . " in match chain_id with | None -> ( read_block_header v . db op . Operation . shell . branch >>= function | None -> if force then Chain_id . Table . iter_es ( fun _chain_id chain -> inject_operation_on chain ~ handle_missing_prevalidator : return_unit ) v . active_chains else failwith " Unknown branch ( % a ) , cannot inject the operation . " Block_hash . pp_short op . shell . branch | Some ( chain_id , _bh ) -> get v chain_id >>?= fun nv -> inject_operation_on nv ~ handle_missing_prevalidator ) | Some chain_id -> get v chain_id >>?= fun nv -> inject_operation_on nv ~ handle_missing_prevalidator |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.