text
stringlengths
12
786k
module type S = sig include Verification . S val constraint_constants : Genesis_constants . Constraint_constants . t val cache_handle : Pickles . Cache_handle . t val of_non_parties_transaction : statement : Statement . With_sok . t -> init_stack : Pending_coinbase . Stack . t -> Transaction . Valid ...
let check_transaction_union ( ? preeval = false ) false ~ constraint_constants sok_message source target init_stack pending_coinbase_stack_state transaction state_body handler = if preeval then failwith " preeval currently disabled " ; let sok_digest = Sok_message . digest sok_message in let handler = ...
let check_transaction ? preeval ~ constraint_constants ~ sok_message ~ source ~ target ~ init_stack ~ pending_coinbase_stack_state ~ zkapp_account1 : _ ~ zkapp_account2 : _ ( transaction_in_block : Transaction . Valid . t Transaction_protocol_state . t ) t handler = let transaction = Transaction_...
let check_user_command ~ constraint_constants ~ sok_message ~ source ~ target ~ init_stack ~ pending_coinbase_stack_state t_in_block handler = let user_command = Transaction_protocol_state . transaction t_in_block in check_transaction ~ constraint_constants ~ sok_message ~ source ~ target ~ init_stack ~ ...
let generate_transaction_union_witness ( ? preeval = false ) false ~ constraint_constants sok_message source target transaction_in_block init_stack pending_coinbase_stack_state handler = if preeval then failwith " preeval currently disabled " ; let transaction = Transaction_protocol_state . transaction ...
let generate_transaction_witness ? preeval ~ constraint_constants ~ sok_message ~ source ~ target ~ init_stack ~ pending_coinbase_stack_state ~ zkapp_account1 : _ ~ zkapp_account2 : _ ( transaction_in_block : Transaction . Valid . t Transaction_protocol_state . t ) t handler = match to_preunion ( ...
let verify ( ts : ( t * _ ) _ list ) list ~ key = if List . for_all ts ~ f ( : fun ( { statement ; _ } , message ) message -> Sok_message . Digest . equal ( Sok_message . digest message ) message statement . sok_digest ) then Pickles . verify ( module Nat . N2 ) N2 ( ...
let constraint_system_digests ~ constraint_constants ( ) = let digest = Tick . R1CS_constraint_system . digest in [ ( " transaction - merge " , digest Merge ( . Tick . constraint_system ~ exposing [ : Statement . With_sok . typ ] ~ return_typ ( : Snarky_backendless . Typ . unit ( ...
type local_state = ( Stack_frame . value , Stack_frame . value list , Token_id . t , Currency . Amount . Signed . t , Sparse_ledger . t , bool , unit , Transaction_status . Failure . Collection . t ) Mina_transaction_logic . Parties_logic . Local_state . t
module Parties_intermediate_state = struct type state = { global : global_state ; local : local_state } type t = { kind : [ ` Same | ` New | ` Two_new ] ; spec : Parties_segment . Basic . t ; state_before : state ; state_after : state ; use_full_commitment : [ ` Others | ` ...
let group_by_parties_rev ( partiess : Party . t list list ) list ( stmtss : ( global_state * local_state ) local_state list list ) list : Parties_intermediate_state . t list = let use_full_commitment ( p : Party . t ) t = match p . authorization with | Proof _ -> ` Proved_use_full_commi...
let rec accumulate_call_stack_hashes ( ~ hash_frame : ' frame -> Stack_frame . Digest . t ) t ( frames : ' frame list ) list : ( ' frame , Call_stack_digest . t ) t With_stack_hash . t list = match frames with | [ ] -> [ ] | f :: fs -> let h_f = hash_frame f in let tl = accu...
let parties_witnesses_exn ~ constraint_constants ~ state_body ~ fee_excess ledger ( partiess : ( [ ` Pending_coinbase_init_stack of Pending_coinbase . Stack . t ] * [ ` Pending_coinbase_of_statement of Pending_coinbase_stack_state . t ] * Parties . t ) list ) = let sparse_ledger = match l...
module Make ( Inputs : sig val constraint_constants : Genesis_constants . Constraint_constants . t val proof_level : Genesis_constants . Proof_level . t struct open Inputs let constraint_constants = constraint_constants let ( tag , cache_handle , p , Pickles . Provers . [ base ; merge ; opt_sign...
module For_tests = struct module Spec = struct type t = { fee : Currency . Fee . t ; sender : Signature_lib . Keypair . t * Mina_base . Account . Nonce . t ; fee_payer : ( Signature_lib . Keypair . t * Mina_base . Account . Nonce . t ) t option ; receivers : ( Signature_lib . Publi...
module Sparse_ledger = struct include Mina_ledger . Sparse_ledger let merkle_root t = Frozen_ledger_hash . of_ledger_hash @@ merkle_root t end
let create_ledger_and_transactions num_transitions = let num_accounts = 4 in let constraint_constants = Genesis_constants . Constraint_constants . compiled in let ledger = Mina_ledger . Ledger . create ~ depth : constraint_constants . ledger_depth ( ) in let keys = Array . init num_accounts ~ f ( :...
let time thunk = let start = Time . now ( ) in let x = thunk ( ) in let stop = Time . now ( ) in ( Time . diff stop start , x ) x
let rec pair_up = function | [ ] -> [ ] | x :: y :: xs -> ( x , y ) y :: pair_up xs | _ -> failwith " Expected even length list "
let state_body = Mina_state ( . Lazy . map precomputed_values ~ f ( : fun values -> values . protocol_state_with_hashes . data |> Protocol_state . body ) )
let curr_state_view = Lazy . map state_body ~ f : Mina_state . Protocol_state . Body . view
let state_body_hash = Lazy . map ~ f : Mina_state . Protocol_state . Body . hash state_body
let pending_coinbase_stack_target ( t : Transaction . t ) t stack = let stack_with_state = Pending_coinbase . Stack ( . push_state ( Lazy . force state_body_hash ) state_body_hash stack ) stack in let target = match t with | Coinbase c -> Pending_coinbase ( . Stack . push_coinbase c stac...
let profile ( module T : Transaction_snark . S ) S sparse_ledger0 ( transitions : Transaction . Valid . t list ) list _ = let constraint_constants = Genesis_constants . Constraint_constants . compiled in let txn_state_view = Lazy . force curr_state_view in let ( base_proof_time , _ , _ ) _ ...
let check_base_snarks sparse_ledger0 ( transitions : Transaction . Valid . t list ) list preeval = let constraint_constants = Genesis_constants . Constraint_constants . compiled in ignore ( let sok_message = Sok_message . create ~ fee : Currency . Fee . zero ~ prover : Public_key ( . compress (...
let generate_base_snarks_witness sparse_ledger0 ( transitions : Transaction . Valid . t list ) list preeval = let constraint_constants = Genesis_constants . Constraint_constants . compiled in ignore ( let sok_message = Sok_message . create ~ fee : Currency . Fee . zero ~ prover : Public_key ( . ...
let run profiler num_transactions repeats preeval = let ledger , transactions = create_ledger_and_transactions num_transactions in let sparse_ledger = Mina_ledger . Sparse_ledger . of_ledger_subset_exn ledger ( List . fold ~ init [ ] : transactions ~ f ( : fun participants t -> List . rev_append (...
let main num_transactions repeats preeval ( ) = Test_util . with_randomness 123456789 ( fun ( ) -> let module T = Transaction_snark . Make ( struct let constraint_constants = Genesis_constants . Constraint_constants . compiled let proof_level = Genesis_constants . Proof_level . Full end ) end in...
let dry num_transactions repeats preeval ( ) = Test_util . with_randomness 123456789 ( fun ( ) -> run check_base_snarks num_transactions repeats preeval )
let witness num_transactions repeats preeval ( ) = Test_util . with_randomness 123456789 ( fun ( ) -> run generate_base_snarks_witness num_transactions repeats preeval )
let command = let open Command . Let_syntax in Command . basic ~ summary " : transaction snark profiler " ( let % map_open n = flag " - k " ~ doc : " count count = log_2 ( log_2number of transactions to snark ) snark or none for \ the mocked ones " ( optional int ) int and repeats = flag ...
let map2_or_error xs ys ~ f = let rec go xs ys acc = match ( xs , ys ) ys with | [ ] , [ ] -> Ok ( List . rev acc ) acc | x :: xs , y :: ys -> ( match f x y with Error e -> Error e | Ok z -> go xs ys ( z :: acc ) acc ) | _ , _ -> Or_error . error_string " Length mis...
module type Monad_with_Or_error_intf = sig type ' a t include Monad . S with type ' a t := ' a t module Or_error : sig type nonrec ' a t = ' a Or_error . t t include Monad . S with type ' a t := ' a t end end
module Transaction_with_witness = struct [ %% versioned module Stable = struct module V2 = struct type t = { transaction_with_info : Mina_transaction_logic . Transaction_applied . Stable . V2 . t ; state_hash : State_hash . Stable . V1 . t * State_body_hash . Stable . V1 . t ; statement : Transactio...
module Ledger_proof_with_sok_message = struct [ %% versioned module Stable = struct module V2 = struct type t = Ledger_proof . Stable . V2 . t * Sok_message . Stable . V1 . t [ @@ deriving sexp ] sexp let to_latest = Fn . id end end ] end end
module Available_job = struct type t = ( Ledger_proof_with_sok_message . t , Transaction_with_witness . t ) Parallel_scan . Available_job . t [ @@ deriving sexp ] sexp end
module Job_view = struct type t = Transaction_snark . Statement . t Parallel_scan . Job_view . t [ @@ deriving sexp ] sexp let to_yojson ( { value ; position } : t ) t : Yojson . Safe . t = let module R = struct type t = ( Frozen_ledger_hash . t , Pending_coinbase . Stack_versioned . t , ...
type job = Available_job . t [ @@ deriving sexp ] sexp [ %% versioned
module Stable = struct module V2 = struct type t = ( Ledger_proof_with_sok_message . Stable . V2 . t , Transaction_with_witness . Stable . V2 . t ) Parallel_scan . State . Stable . V1 . t [ @@ deriving sexp ] sexp let to_latest = Fn . id let hash ( t : t ) t = let state_hash = Parallel_scan . ...
let create_expected_statement ~ constraint_constants ( ~ get_state : State_hash . t -> Mina_state . Protocol_state . value Or_error . t ) t { Transaction_with_witness . transaction_with_info ; state_hash ; ledger_witness ; init_stack ; statement } = let open Or_error . Let_syntax in let sourc...
let completed_work_to_scanable_work ( job : job ) job ( fee , current_proof , prover ) prover : Ledger_proof_with_sok_message . t Or_error . t = let sok_digest = Ledger_proof . sok_digest current_proof and proof = Ledger_proof . underlying_proof current_proof in match job with | Base { statement...
let total_proofs ( works : Transaction_snark_work . t list ) list = List . sum ( module Int ) Int works ~ f ( : fun w -> One_or_two . length w . proofs ) proofs
module P = struct type t = Ledger_proof_with_sok_message . t end
module Make_statement_scanner ( Verifier : sig type t val verify : verifier : t -> P . t list -> bool Deferred . Or_error . t struct module Fold = Parallel_scan . State . Make_foldable ( Deferred ) Deferred let logger = lazy ( Logger . create ( ) ) module Timer = struct module Info = struct ...
let statement_of_job : job -> Transaction_snark . Statement . t option = function | Base { statement ; _ } -> Some statement | Merge ( ( p1 , _ ) _ , ( p2 , _ ) _ ) _ -> Transaction_snark . Statement . merge ( Ledger_proof . statement p1 ) p1 ( Ledger_proof . statement p...
let create ~ work_delay ~ transaction_capacity_log_2 = let k = Int . pow 2 transaction_capacity_log_2 in Parallel_scan . empty ~ delay : work_delay ~ max_base_jobs : k
let empty ( ~ constraint_constants : Genesis_constants . Constraint_constants . t ) t ( ) = create ~ work_delay : constraint_constants . work_delay ~ transaction_capacity_log_2 : constraint_constants . transaction_capacity_log_2
let extract_txns txns_with_witnesses = List . map txns_with_witnesses ~ f ( : fun ( txn_with_witness : Transaction_with_witness . t ) t -> let txn = Ledger . Transaction_applied . transaction txn_with_witness . transaction_with_info in let state_hash = fst txn_with_witness . state_hash in ( txn , ...
let latest_ledger_proof t = let open Option . Let_syntax in let % map proof , txns_with_witnesses = Parallel_scan . last_emitted_value t in ( proof , extract_txns txns_with_witnesses ) txns_with_witnesses
let staged_transactions t = List . map ~ f ( : fun ( t : Transaction_with_witness . t ) t -> t . transaction_with_info |> Ledger . Transaction_applied . transaction ) @@ Parallel_scan . pending_data t
let staged_transactions_with_protocol_states t ( ~ get_state : State_hash . t -> Mina_state . Protocol_state . value Or_error . t ) t = let open Or_error . Let_syntax in List . map ~ f ( : fun ( t : Transaction_with_witness . t ) t -> let txn = t . transaction_with_info |> Ledger . Transact...
let partition_if_overflowing t = let bundle_count work_count = ( work_count + 1 ) 1 / 2 in let { Space_partition . first = slots , job_count ; second } = Parallel_scan . partition_if_overflowing t in { Space_partition . first = ( slots , bundle_count job_count ) job_count ; second = ...
let extract_from_job ( job : job ) job = match job with | Parallel_scan . Available_job . Base d -> First ( d . transaction_with_info , d . statement , d . state_hash , d . ledger_witness , d . init_stack ) | Merge ( ( p1 , _ ) _ , ( p2 , _ ) _ ) _ -> Second ( p1 , ...
let snark_job_list_json t = let all_jobs : Job_view . t list list = let fa ( a : Ledger_proof_with_sok_message . t ) t = Ledger_proof . statement ( fst a ) a in let fd ( d : Transaction_with_witness . t ) t = d . statement in Parallel_scan . view_jobs_with_position t fa fd in Yojson . Safe . ...
let all_work_statements_exn t : Transaction_snark_work . Statement . t list = let work_seqs = all_jobs t in List . concat_map work_seqs ~ f ( : fun work_seq -> One_or_two . group_list ( List . map work_seq ~ f ( : fun job -> match statement_of_job job with | None -> assert false | Some stmt -> s...
let required_work_pairs t ~ slots = let work_list = Parallel_scan . jobs_for_slots t ~ slots in List . concat_map work_list ~ f ( : fun works -> One_or_two . group_list works ) works
let k_work_pairs_for_new_diff t ~ k = let work_list = Parallel_scan . jobs_for_next_update t in List ( . take ( concat_map work_list ~ f ( : fun works -> One_or_two . group_list works ) works ) works k ) k
let work_statements_for_new_diff t : Transaction_snark_work . Statement . t list = let work_list = Parallel_scan . jobs_for_next_update t in List . concat_map work_list ~ f ( : fun work_seq -> One_or_two . group_list ( List . map work_seq ~ f ( : fun job -> match statement_of_job job with | None ...
let all_work_pairs t ( ~ get_state : State_hash . t -> Mina_state . Protocol_state . value Or_error . t ) t : ( Transaction_witness . t , Ledger_proof . t ) t Snark_work_lib . Work . Single . Spec . t One_or_two . t list Or_error . t = let all_jobs = all_jobs t in let module A = Available_jo...
let fill_work_and_enqueue_transactions t transactions work = let open Or_error . Let_syntax in let fill_in_transaction_snark_work t ( works : Transaction_snark_work . t list ) list : ( Ledger_proof . t * Sok_message . t ) t list Or_error . t = let next_jobs = List ( . take ( concat @@ Parall...
let required_state_hashes t = List . fold ~ init : State_hash . Set . empty ~ f ( : fun acc ( t : Transaction_with_witness . t ) t -> Set . add acc ( fst t . state_hash ) state_hash ) ( Parallel_scan . pending_data t ) t
let check_required_protocol_states t ~ protocol_states = let open Or_error . Let_syntax in let required_state_hashes = required_state_hashes t in let check_length states = let required = State_hash . Set . length required_state_hashes in let received = List . length states in if required = received then Or_er...
module Statement = struct module Arg = struct [ %% versioned module Stable = struct module V2 = struct type t = Transaction_snark . Statement . Stable . V2 . t One_or_two . Stable . V1 . t [ @@ deriving hash , sexp , compare ] compare let to_latest = Fn . id end end ] end end [ %% versioned mo...
module Info = struct [ %% versioned module Stable = struct [ @@@ no_toplevel_latest_type ] no_toplevel_latest_type module V2 = struct type t = { statements : Statement . Stable . V2 . t ; work_ids : int One_or_two . Stable . V1 . t ; fee : Fee . Stable . V1 . t ; prover : Public_key . Compres...
module T = struct [ %% versioned module Stable = struct [ @@@ no_toplevel_latest_type ] no_toplevel_latest_type module V2 = struct type t = { fee : Fee . Stable . V1 . t ; proofs : Ledger_proof . Stable . V2 . t One_or_two . Stable . V1 . t ; prover : Public_key . Compressed . Stable . V1 . t }...
module Checked = struct include T let create_unsafe = Fn . id end
let fee { fee ; _ } = fee
module Failure = struct [ %% versioned module Stable = struct module V2 = struct type t = | Predicate [ @ value 1 ] 1 | Source_not_present | Receiver_not_present | Amount_insufficient_to_create_account | Cannot_pay_creation_fee_in_token | Source_insufficient_balance | Source_minimum_balance_violati...
module Stable = struct module V2 = struct type t = Applied | Failed of Failure . Collection . Stable . V1 . t [ @@ deriving sexp , yojson , equal , compare ] compare let to_latest = Fn . id end end ] end
let state_body_hash = U . genesis_state_body_hash ( module struct module Ledger = struct include Ledger let merkle_root t = Frozen_ledger_hash . of_ledger_hash @@ merkle_root t let merkle_root_after_user_command_exn t ~ txn_global_slot txn = let hash = merkle_root_after_user_command_exn ~ constraint_constan...
module Body = struct type ( ' tag , ' public_key , ' token_id , ' amount , ' bool ) ' bool t_ = { tag : ' tag ; source_pk : ' public_key ; receiver_pk : ' public_key ; token_id : ' token_id ; amount : ' amount ; token_locked : ' bool } [ @@ deriving sexp , hlist ] hl...
module Payload_common = struct module Poly = struct type ( ' fee , ' public_key , ' token_id , ' nonce , ' global_slot , ' memo ) ' memo t = { fee : ' fee ; fee_token : ' token_id ; fee_payer_pk : ' public_key ; nonce : ' nonce ; valid_until : ' global_slot ; memo : ' memo...
type t = ( Payload_common . t , Body . t ) t Signed_command_payload . Poly . t
type payload = t [ @@ deriving sexp ] sexp
let of_user_command_payload ( { common = { memo ; fee ; fee_payer_pk ; nonce ; valid_until } ; body } : Signed_command_payload . t ) : t = { common = { fee ; fee_token = Token_id . default ; fee_payer_pk ; nonce ; valid_until ; memo } ; body = Body . of_user_command_payl...
let gen = let open Quickcheck . Generator . Let_syntax in let % bind common = Signed_command_payload . Common . gen in let % map body = Body . gen ~ fee : common . fee in Signed_command_payload . Poly { . common ; body }
type var = ( Payload_common . Checked . t , Body . var ) var Signed_command_payload . Poly . t
let typ : ( var , t ) t Typ . t = let to_hlist = Signed_command_payload . Poly . to_hlist in let of_hlist = Signed_command_payload . Poly . of_hlist in Typ . of_hlistable [ Payload_common . typ ; Body . typ ] ~ var_to_hlist : to_hlist ~ var_of_hlist : of_hlist ~ value_to_hlist : to_hlist ~...
module Checked = struct let to_input_legacy ( { common ; body } : var ) var = let % map common = Signed_command_payload . Common . Checked . to_input_legacy ( Payload_common . to_signed_command_payload_common common ) common and body = Body . Checked . to_input_legacy body in Random_oracle . Inp...
let to_input_legacy ( { common ; body } : t ) t = Random_oracle . Input . Legacy . append ( Signed_command_payload . Common . to_input_legacy ( Payload_common . to_signed_command_payload_common common ) common ) ( Body . to_input_legacy body ) body
let excess ( payload : t ) t : Amount . Signed . t = let tag = payload . body . tag in let fee = payload . common . fee in let amount = payload . body . amount in match tag with | Payment | Stake_delegation | Create_account | Mint_tokens -> Amount . Signed . of_unsigned ( Amount . of_fee fee ) ...
let fee_excess ( { body = { tag ; amount ; _ } ; common = { fee ; _ } } : t ) t = match tag with | Payment | Stake_delegation | Create_account | Mint_tokens -> Fee_excess . of_single ( Token_id . default , Fee . Signed . of_unsigned fee ) fee | Fee_transfer -> let excess ...
let supply_increase ( payload : payload ) payload = let tag = payload . body . tag in match tag with | Coinbase -> payload . body . amount | Payment | Stake_delegation | Create_account | Mint_tokens | Fee_transfer -> Amount . zero
type t = | Payment | Stake_delegation | Create_account | Mint_tokens | Fee_transfer | Coinbase
let to_string = function | Payment -> " payment " | Stake_delegation -> " delegation " | Create_account -> " create_account " | Mint_tokens -> " mint_tokens " | Fee_transfer -> " fee - transfer " | Coinbase -> " coinbase "
let gen = Quickcheck . Generator . map ( Int . gen_incl min max ) max ~ f ( : fun i -> Option . value_exn ( of_enum i ) i )
module Bits = struct type t = bool * bool * bool [ @@ deriving equal ] equal let of_int i : t = let test_mask mask = i land mask = mask in ( test_mask 0b100 , test_mask 0b10 , test_mask 0b1 ) 0b1 let of_t x = of_int ( to_enum x ) x let payment = of_t Payment let stake_delegation = of_t S...
module Unpacked = struct module Poly = struct type ' bool t = { is_payment : ' bool ; is_stake_delegation : ' bool ; is_create_account : ' bool ; is_mint_tokens : ' bool ; is_fee_transfer : ' bool ; is_coinbase : ' bool ; is_user_command : ' bool } [ @@ deriving equal , hlist ] ...
let unpacked_t_of_t = function | Payment -> Unpacked . payment | Stake_delegation -> Unpacked . stake_delegation | Create_account -> Unpacked . create_account | Mint_tokens -> Unpacked . mint_tokens | Fee_transfer -> Unpacked . fee_transfer | Coinbase -> Unpacked . coinbase
let to_bits tag = Bits . to_bits ( Unpacked . to_bits_t ( unpacked_t_of_t tag ) tag ) tag
let to_input_legacy tag = Random_oracle . Input . Legacy . bitstring ( to_bits tag ) tag
let t_of_unpacked_t ( unpacked : Unpacked . t ) t : t = match List . Assoc . find ~ equal : Unpacked . equal [ ( Unpacked . payment , Payment ) Payment ; ( Unpacked . stake_delegation , Stake_delegation ) Stake_delegation ; ( Unpacked . create_account , Create_account ) Creat...
let bits_t_of_t tag = Unpacked . to_bits_t ( unpacked_t_of_t tag ) tag
let t_of_bits_t tag = t_of_unpacked_t ( Unpacked . of_bits_t tag ) tag
let unpacked_of_t tag = Unpacked . constant ( unpacked_t_of_t tag ) tag
let bits_of_t tag = Bits . constant ( bits_t_of_t tag ) tag
let unpacked_typ = Typ . transport Unpacked . typ ~ there : unpacked_t_of_t ~ back : t_of_unpacked_t
let bits_typ = Typ . transport Bits . typ ~ there : bits_t_of_t ~ back : t_of_bits_t ( module struct let test_predicate checked unchecked = let checked x = Checked . return ( checked x ) x in for i = min to max do Test_util . test_equal unpacked_typ Boolean . typ checked unchecked ( Option . valu...
module Hashless_ledger = struct type t = { base : Ledger . t ; overlay : ( Account . Identifier . t , Account . t ) t Hashtbl . t } type location = Ours of Account . Identifier . t | Theirs of Ledger . Location . t let msg s = s ^ " : somehow we got a location that isn ' t present in th...
let apply_user_command ~ constraint_constants ~ txn_global_slot l uc = Result . map ~ f ( : fun applied_txn -> applied_txn . Transaction_applied . Signed_command_applied . common . user_command . status ) ( apply_user_command l ~ constraint_constants ~ txn_global_slot uc ) uc