text
stringlengths 0
601k
|
---|
let monad_sequence_how ( ? how = ` Sequential ) ~ f = stage ( match how with | ` Parallel -> f | ( ` Sequential | ` Max_concurrent_jobs _ ) as how -> let max_concurrent_jobs = match how with | ` Sequential -> 1 | ` Max_concurrent_jobs max_concurrent_jobs -> max_concurrent_jobs in let t = create ~ continue_on_error : false ~ max_concurrent_jobs in fun a -> enqueue t ( fun ( ) -> f a ) ) ; ;
|
let monad_sequence_how2 ( ? how = ` Sequential ) ~ f = stage ( match how with | ` Parallel -> f | ( ` Sequential | ` Max_concurrent_jobs _ ) as how -> let max_concurrent_jobs = match how with | ` Sequential -> 1 | ` Max_concurrent_jobs max_concurrent_jobs -> max_concurrent_jobs in let t = create ~ continue_on_error : false ~ max_concurrent_jobs in fun a1 a2 -> enqueue t ( fun ( ) -> f a1 a2 ) ) ; ;
|
let prior_jobs_done t = Deferred . create ( fun all_dummy_jobs_running -> let dummy_jobs_running = ref 0 in for _ = 1 to t . max_concurrent_jobs do don ' t_wait_for ( enqueue t ( fun _ -> incr dummy_jobs_running ; if ! dummy_jobs_running = t . max_concurrent_jobs then Ivar . fill all_dummy_jobs_running ( ) ; Ivar . read all_dummy_jobs_running ) ) done ) ; ;
|
let capacity_available t = if num_jobs_running t < max_concurrent_jobs t then return ( ) else ( match t . capacity_available with | Some ivar -> Ivar . read ivar | None -> Deferred . create ( fun ivar -> t . capacity_available <- Some ivar ) ) ; ;
|
let null = null ( ) " java is_null " " java is_not_null "
|
type t = ( $ ocaml_short_name ) ocaml_short_name java_instance
|
let make ( ? cause = null ) null ( ? message = JavaString . null ) null ( ) = if JavaString . is_null message then Java . make ( " $ java_short_name ) java_short_name ( java_short_nameThrowable ) java_short_nameThrowable " cause else Java . make ( " $ java_short_name ) java_short_name ( java_short_nameString , Throwable ) Throwable " message cause
|
let get_cause th = Java . call " Throwable . getCause ( getCause ) getCause " th
|
let get_message th = Java . call " Throwable . getMessage ( getMessage ) getMessage " th
|
let get_stack_trace th = Java . call " Throwable . getStackTrace ( getStackTrace ) getStackTrace " th
|
let print_stack_trace th = Java . call " Throwable . printStackTrace ( printStackTrace ) printStackTrace " th
|
let wrap x = if is_null x then None else Some x
|
let unwrap = function | Some x -> x | None -> null
|
let string_to_size = function | " xs " -> ` Xs | " s " -> ` S | " m " -> ` M | " l " -> ` L | " xl " -> ` Xl | _ -> invalid_arg " Size must be xs , s , m , l or xl . "
|
let string_to_format = function | " jpeg " -> ` Jpeg | " jpg " -> ` Jpeg | " png " -> ` Png | " bmp " -> ` Bmp | _ -> invalid_arg " Format must be jpeg , jpg , png or bmp . "
|
let download t ( ? format " = jpeg " ) ( ? size " = s " ) fn = D . thumbnails t ~ size ( : string_to_size size ) ~ format ( : string_to_format format ) fn >>= function | None -> Lwt_io . printlf " No image named % S . " fn | Some ( metadata , stream ) -> let fn = Filename . basename fn in let fname = " thumbnail_of_ " ^ ( Filename . chop_extension fn ) ^ " . " ^ format in Lwt_unix . ( openfile fname [ O_WRONLY ; O_CREAT ; O_TRUNC ] 0o664 ) >>= fun fd -> let write s = let s = Bytes . of_string s in Lwt_unix . write fd s 0 ( Bytes . length s ) >>= fun _ -> return_unit in Lwt_stream . iter_s write stream >>= fun ( ) -> Lwt_unix . close fd >>= fun ( ) -> Lwt_io . printlf " Wrote a thumbnail as % S \ n % s " fname ( Dropbox_j . string_of_metadata metadata )
|
let main t args = match args with | [ ] -> Lwt_io . printlf " No file specified . " | [ fn ] -> download t fn | [ fn ; size ] -> download t ~ size fn | [ fn ; size ; format ] -> download t ~ format ~ size fn | _ -> Lwt_io . printlf " % s < path > [ size ] [ format ] " Sys . argv . ( 0 )
|
let ( ) = Common . run main
|
[ %% error mechanism ] " [ %% endif ] endif [ %% else ] else [ %% endif ] endif
|
module Field = struct open Core_kernel [ %% versioned_asserted module Stable = struct [ @@@ no_toplevel_latest_type ] no_toplevel_latest_type module V1 = struct type t = Pasta . Fp . t [ @@ deriving equal , compare , yojson , sexp , hash ] hash let to_latest x = x end module Tests = struct end end ] end include Pasta . Fp let size = order |> Snarkette . Nat . to_string |> Bigint . of_string let size_in_bits = length_in_bits let unpack t = to_bits t let project bits = Core_kernel . Option . value_exn ~ message " : project : invalid bits " ( of_bits bits ) bits end
|
module Tock = struct module Field = struct type t = Pasta . Fq . t let unpack ( t : t ) t = Pasta . Fq . to_bits t let size_in_bits = Pasta . Fq . length_in_bits let project bits = Core_kernel . Option . value_exn ~ message " : Snark_params_nonconsensus . Tock . Field . project " ( Pasta . Fq . of_bits bits ) bits end end
|
module Inner_curve = struct module C = Pasta . Pallas type t = C . t [ @@ deriving sexp ] sexp module Coefficients = C . Coefficients let find_y x = let open Field in let y2 = ( x * square x ) x + ( Coefficients . a * x ) x + Coefficients . b in if is_square y2 then Some ( sqrt y2 ) y2 else None [ %% define_locally C ( . of_affine , to_affine , to_affine_exn , one , ( + ) , negate ) negate ] negate module Scalar = struct type t = Pasta . Fq . t [ @@ deriving bin_io_unversioned , sexp ] sexp type _unused = unit constraint t = Tock . Field . t let size = Pasta . Fq . order [ %% define_locally Pasta . Fq . ( to_string , of_string , equal , compare , size , zero , one , ( + ) , ( - ) , ( * ) , gen_uniform_incl , negate , hash_fold_t ) ] let gen = Pasta . Fq ( . gen_incl one ( zero - one ) one ) one let gen_uniform = gen_uniform_incl one ( zero - one ) one let unpack t = Tock . Field . unpack t let of_bits bits = Tock . Field . project bits let project = of_bits end let scale ( t : t ) t ( scalar : Scalar . t ) t = C . scale t ( scalar :> Nat . t ) t let scale_field ( t : t ) t x = scale t ( Pasta . Fq . of_bigint x :> Scalar . t ) t end
|
let ticket_balance_key ctxt ~ owner ( Ticket_token . Ex_token { ticketer ; contents_type ; contents } ) = let loc = Micheline . dummy_location in Script_ir_translator . unparse_comparable_ty ~ loc ctxt contents_type >>?= fun ( cont_ty_unstripped , ctxt ) -> Gas . consume ctxt ( Script . strip_annotations_cost cont_ty_unstripped ) >>?= fun ctxt -> let typ = Script . strip_annotations cont_ty_unstripped in let ticketer = Destination . Contract ticketer in let ticketer_address = Script_typed_ir . { destination = ticketer ; entrypoint = Entrypoint . default } in let owner = Destination . Contract owner in let owner_address = Script_typed_ir . { destination = owner ; entrypoint = Entrypoint . default } in Script_ir_translator . unparse_data ctxt Script_ir_translator . Optimized_legacy Script_typed_ir . address_t ticketer_address >>=? fun ( ticketer , ctxt ) -> Script_ir_translator . unparse_comparable_data ~ loc ctxt Script_ir_translator . Optimized_legacy contents_type contents >>=? fun ( contents , ctxt ) -> Script_ir_translator . unparse_data ctxt Script_ir_translator . Optimized_legacy Script_typed_ir . address_t owner_address >>=? fun ( owner , ctxt ) -> Lwt . return ( Ticket_hash . make ctxt ~ ticketer ~ typ ~ contents ~ owner )
|
let add_ticket_balance contract ctxt ticket = let ( token , amount ) = Ticket_token . token_and_amount_of_ex_ticket ticket in Ticket_balance_key . ticket_balance_key ctxt ~ owner : contract token >>=? fun ( hash , ctxt ) -> Ticket_balance . adjust_balance ctxt hash ~ delta ( : Script_int . to_zint amount ) >|=? fun ( ( _added_size : Z . t ) , ctxt ) -> ctxt
|
let update_contract_tickets ctxt contract = Contract . get_script ctxt contract >>=? fun ( ctxt , script ) -> match script with | None -> return ctxt | Some script -> Script_ir_translator . parse_script ctxt ~ legacy : true ~ allow_forged_in_storage : true script >>=? fun ( ex_script , ctxt ) -> let ( Ex_script { storage_type ; storage ; _ } ) = ex_script in Ticket_scanner . type_has_tickets ctxt storage_type >>?= fun ( has_tickets , ctxt ) -> Ticket_scanner . tickets_of_value ctxt ~ include_lazy : true has_tickets storage >>=? fun ( tickets , ctxt ) -> List . fold_left_es ( add_ticket_balance contract ) ctxt tickets
|
let is_originated contract = match Contract . is_originated contract with Some _ -> true | _ -> false
|
let init ctxt = Contract . list ctxt >>= fun contracts -> let contracts = List . filter is_originated contracts in List . fold_left_es update_contract_tickets ctxt contracts
|
module Constants = struct let cost_collect_tickets_step = S . safe_int 360 let cost_has_tickets_of_ty type_size = S . mul ( S . safe_int 20 ) type_size let cost_token_and_amount_of_ticket = S . safe_int 30 let cost_compare_ticket_hash = S . safe_int 100 let cost_compare_key_contract = S . safe_int 100 end
|
let consume_gas_steps ctxt ~ step_cost ~ num_steps = let ( * ) = S . mul in if Compare . Int . ( num_steps <= 0 ) then Ok ctxt else let gas = Gas . atomic_step_cost ( step_cost * Saturation_repr . safe_int num_steps ) in Gas . consume ctxt gas
|
let has_tickets_of_ty_cost ty = Constants . cost_has_tickets_of_ty Script_typed_ir . ( ty_size ty |> Type_size . to_int )
|
let negate_cost z = let size = ( 7 + Z . numbits z ) / 8 in Gas . ( S . safe_int 25 +@ S . shift_right ( S . safe_int size ) 4 )
|
let ( ) = register_error_kind ` Branch ~ id " : Failed_to_hash_node " ~ title " : Failed to hash node " ~ description " : Failed to hash node for a key in the ticket - balance table " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Failed to hash node for a key in the ticket - balance table " ) Data_encoding . empty ( function Failed_to_hash_node -> Some ( ) | _ -> None ) ( fun ( ) -> Failed_to_hash_node )
|
let hash_bytes_cost bytes = let module S = Saturation_repr in let ( + ) = S . add in let v0 = S . safe_int @@ Bytes . length bytes in let ( lsr ) = S . shift_right in S . safe_int 200 + ( v0 + ( v0 lsr 2 ) ) |> Gas_limit_repr . atomic_step_cost
|
let hash_of_node ctxt node = Raw_context . consume_gas ctxt ( Script_repr . strip_locations_cost node ) >>? fun ctxt -> let node = Micheline . strip_locations node in match Data_encoding . Binary . to_bytes_opt Script_repr . expr_encoding node with | Some bytes -> Raw_context . consume_gas ctxt ( hash_bytes_cost bytes ) >|? fun ctxt -> ( Ticket_hash_repr . of_script_expr_hash @@ Script_expr_hash . hash_bytes [ bytes ] , ctxt ) | None -> error Failed_to_hash_node
|
let make ctxt ~ ticketer ~ typ ~ contents ~ owner = hash_of_node ctxt @@ Micheline . Seq ( Micheline . dummy_location , [ ticketer ; typ ; contents ; owner ] )
|
type error += Failed_to_load_big_map_value_type of Big_map . Id . t
|
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : Failed_to_load_big_map_value_type " ~ title " : Failed to load big - map value type " ~ description : " Failed to load big - map value type when computing ticket diffs . " ~ pp ( : fun ppf big_map_id -> Format . fprintf ppf " Failed to load big - map value type for big - map - id : ' % a ' " Z . pp_print ( Big_map . Id . unparse_to_z big_map_id ) ) ( obj1 ( req " big_map_id " Big_map . Id . encoding ) ) ( function | Failed_to_load_big_map_value_type big_map_id -> Some big_map_id | _ -> None ) ( fun big_map_id -> Failed_to_load_big_map_value_type big_map_id )
|
let token_and_amount ctxt ex_ticket = Gas . consume ctxt Ticket_costs . Constants . cost_token_and_amount_of_ticket >|? fun ctxt -> let ( token , amount ) = Ticket_token . token_and_amount_of_ex_ticket ex_ticket in ( ( token , Script_int . to_zint amount ) , ctxt )
|
let neg_token_and_amount ctxt ex_ticket = token_and_amount ctxt ex_ticket >>? fun ( ( token , amount ) , ctxt ) -> Gas . consume ctxt ( Ticket_costs . negate_cost amount ) >|? fun ctxt -> ( ( token , Z . neg amount ) , ctxt )
|
let parse_value_type ctxt value_type = Script_ir_translator . parse_big_map_value_ty ctxt ~ legacy : true ( Micheline . root value_type )
|
let collect_token_diffs_of_node ctxt has_tickets node ~ get_token_and_amount acc = Ticket_scanner . tickets_of_node ctxt ~ include_lazy : true has_tickets ( Micheline . root node ) >>=? fun ( ex_tickets , ctxt ) -> List . fold_left_e ( fun ( acc , ctxt ) ticket -> get_token_and_amount ctxt ticket >|? fun ( item , ctxt ) -> ( item :: acc , ctxt ) ) ( acc , ctxt ) ex_tickets >>?= return
|
module Key_hash_map = Carbonated_map . Make ( struct type t = Script_expr_hash . t let compare = Script_expr_hash . compare let compare_cost _ = Ticket_costs . Constants . cost_compare_ticket_hash end )
|
let collect_token_diffs_of_big_map_update ctxt ~ big_map_id has_tickets { Lazy_storage_kind . Big_map . key = _ ; key_hash ; value } already_updated acc = let collect_token_diffs_of_node_option ctxt ~ get_token_and_amount expr_opt acc = match expr_opt with | Some expr -> collect_token_diffs_of_node ctxt has_tickets expr ~ get_token_and_amount acc | None -> return ( acc , ctxt ) in ( Key_hash_map . find ctxt key_hash already_updated >>?= fun ( val_opt , ctxt ) -> match val_opt with | Some updated_value -> return ( updated_value , ctxt ) | None -> Big_map . get_opt ctxt big_map_id key_hash >|=? fun ( ctxt , old_value ) -> ( old_value , ctxt ) ) >>=? fun ( old_value , ctxt ) -> collect_token_diffs_of_node_option ctxt ~ get_token_and_amount : neg_token_and_amount old_value acc >>=? fun ( acc , ctxt ) -> Key_hash_map . update ctxt key_hash ( fun ctxt _ -> ok ( Some value , ctxt ) ) already_updated >>?= fun ( already_updated , ctxt ) -> collect_token_diffs_of_node_option ctxt ~ get_token_and_amount : token_and_amount value acc >|=? fun ( tickets , ctxt ) -> ( tickets , already_updated , ctxt )
|
let collect_token_diffs_of_big_map_updates ctxt big_map_id ~ value_type updates acc = parse_value_type ctxt value_type >>?= fun ( Script_ir_translator . Ex_ty value_type , ctxt ) -> Ticket_scanner . type_has_tickets ctxt value_type >>?= fun ( has_tickets , ctxt ) -> List . fold_left_es ( fun ( acc , already_updated , ctxt ) update -> collect_token_diffs_of_big_map_update ctxt ~ big_map_id has_tickets update already_updated acc ) ( acc , Key_hash_map . empty , ctxt ) updates >|=? fun ( acc , _already_updated , ctxt ) -> ( acc , ctxt )
|
let collect_token_diffs_of_big_map ctxt ~ get_token_and_amount big_map_id acc = Gas . consume ctxt Ticket_costs . Constants . cost_collect_tickets_step >>?= fun ctxt -> Big_map . exists ctxt big_map_id >>=? fun ( ctxt , key_val_tys ) -> match key_val_tys with | Some ( _key_ty , value_ty ) -> parse_value_type ctxt value_ty >>?= fun ( Script_ir_translator . Ex_ty value_type , ctxt ) -> Ticket_scanner . type_has_tickets ctxt value_type >>?= fun ( has_tickets , ctxt ) -> Big_map . list_values ctxt big_map_id >>=? fun ( ctxt , exprs ) -> List . fold_left_es ( fun ( acc , ctxt ) node -> collect_token_diffs_of_node ctxt has_tickets node ~ get_token_and_amount acc ) ( acc , ctxt ) exprs | None -> fail ( Failed_to_load_big_map_value_type big_map_id )
|
let collect_token_diffs_of_big_map_and_updates ctxt big_map_id updates acc = Gas . consume ctxt Ticket_costs . Constants . cost_collect_tickets_step >>?= fun ctxt -> Big_map . exists ctxt big_map_id >>=? fun ( ctxt , key_val_opt ) -> match key_val_opt with | Some ( _val , value_type ) -> collect_token_diffs_of_big_map_updates ctxt big_map_id ~ value_type updates acc | None -> fail ( Failed_to_load_big_map_value_type big_map_id )
|
let collect_token_diffs_of_big_map_diff ctxt diff_item acc = Gas . consume ctxt Ticket_costs . Constants . cost_collect_tickets_step >>?= fun ctxt -> match diff_item with | Lazy_storage . Item ( Lazy_storage_kind . Big_map , big_map_id , Remove ) -> collect_token_diffs_of_big_map ctxt ~ get_token_and_amount : neg_token_and_amount big_map_id acc | Item ( Lazy_storage_kind . Big_map , big_map_id , Update { init ; updates } ) -> ( match init with | Lazy_storage . Existing -> collect_token_diffs_of_big_map_and_updates ctxt big_map_id updates acc | Copy { src } -> collect_token_diffs_of_big_map ctxt ~ get_token_and_amount : token_and_amount src acc >>=? fun ( acc , ctxt ) -> collect_token_diffs_of_big_map_and_updates ctxt src updates acc | Alloc { key_type = _ ; value_type } -> collect_token_diffs_of_big_map_updates ctxt big_map_id ~ value_type updates acc ) | Item ( Sapling_state , _ , _ ) -> return ( acc , ctxt )
|
let ticket_diffs_of_lazy_storage_diff ctxt diffs_items = List . fold_left_es ( fun ( acc , ctxt ) diff_item -> collect_token_diffs_of_big_map_diff ctxt diff_item acc ) ( [ ] , ctxt ) diffs_items
|
type ticket_transfer = { destination : Contract . t ; tickets : Ticket_scanner . ex_ticket list ; }
|
type ticket_token_diff = { ticket_token : Ticket_token . ex_token ; total_amount : Script_int . n Script_int . num ; destinations : ( Contract . t * Script_int . n Script_int . num ) list ; }
|
type error += Failed_to_get_script of Contract . t | Contract_not_originated
|
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : Failed_to_get_script " ~ title " : Failed to get script for contract " ~ description : " Failed to get script for contract when scanning operations for tickets " ~ pp ( : fun ppf contract -> Format . fprintf ppf " Failed to get script for contract % a " Contract . pp contract ) ( obj1 ( req " contract " Contract . encoding ) ) ( function Failed_to_get_script c -> Some c | _ -> None ) ( fun c -> Failed_to_get_script c ) ; register_error_kind ` Permanent ~ id " : contract_not_originated " ~ title " : Contract not originated " ~ description " : Non originated contract detected in ticket update . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Contract was not pre - originated " ) unit ( function Contract_not_originated -> Some ( ) | _ -> None ) ( fun ( ) -> Contract_not_originated )
|
module Contract_map = Carbonated_map . Make ( struct type t = Contract . t let compare = Contract . compare let compare_cost _ = Ticket_costs . Constants . cost_compare_key_contract end )
|
module Ticket_token_map = struct include Ticket_token_map let add ctxt ~ ticket_token ~ destination ~ amount map = Ticket_token_map . update ctxt ticket_token ( fun ctxt old_val -> match old_val with | None -> let map = Contract_map . singleton destination amount in ok ( Some map , ctxt ) | Some destination_map -> let update ctxt prev_amt_opt = match prev_amt_opt with | Some prev_amount -> Gas . consume ctxt ( Ticket_costs . add_cost prev_amount amount ) >|? fun ctxt -> ( Some ( Script_int . add_n prev_amount amount ) , ctxt ) | None -> ok ( Some amount , ctxt ) in Contract_map . update ctxt destination update destination_map >|? fun ( destination_map , ctxt ) -> ( Some destination_map , ctxt ) ) map end
|
let parse_and_cache_script ctxt ~ destination ~ get_non_cached_script = Script_cache . find ctxt destination >>=? fun ( ctxt , _cache_key , cached ) -> match cached with | Some ( _script , ex_script ) -> return ( ex_script , ctxt ) | None -> get_non_cached_script ctxt >>=? fun ( script , ctxt ) -> Script_ir_translator . parse_script ctxt ~ legacy : true ~ allow_forged_in_storage : true script >>=? fun ( ex_script , ctxt ) -> let ( size , cost ) = Script_ir_translator . script_size ex_script in Gas . consume ctxt cost >>?= fun ctxt -> Script_cache . insert ctxt destination ( script , ex_script ) size >>?= fun ctxt -> return ( ex_script , ctxt )
|
let tickets_of_transaction ctxt ~ destination ~ parameters = match Contract . is_implicit destination with | Some _ -> return ( None , ctxt ) | None -> parse_and_cache_script ctxt ~ destination ~ get_non_cached_script ( : fun ctxt -> Contract . get_script ctxt destination >>=? fun ( ctxt , script_opt ) -> match script_opt with | None -> fail ( Failed_to_get_script destination ) | Some script -> return ( script , ctxt ) ) >>=? fun ( Script_ir_translator . Ex_script { arg_type ; _ } , ctxt ) -> Ticket_scanner . type_has_tickets ctxt arg_type >>?= fun ( has_tickets , ctxt ) -> Script . force_decode_in_context ctxt ~ consume_deserialization_gas : When_needed parameters >>?= fun ( expr , ctxt ) -> Ticket_scanner . tickets_of_node ~ include_lazy : true ctxt has_tickets ( Micheline . root expr ) >>=? fun ( tickets , ctxt ) -> return ( Some { destination ; tickets } , ctxt )
|
let tickets_of_origination ctxt ~ preorigination script = match preorigination with | None -> fail Contract_not_originated | Some destination -> parse_and_cache_script ctxt ~ destination ~ get_non_cached_script ( : fun ctxt -> return ( script , ctxt ) ) >>=? fun ( Script_ir_translator . Ex_script { storage ; storage_type ; code = _ ; arg_type = _ ; views = _ ; entrypoints = _ ; code_size = _ ; } , ctxt ) -> Ticket_scanner . type_has_tickets ctxt storage_type >>?= fun ( has_tickets , ctxt ) -> Ticket_scanner . tickets_of_value ctxt ~ include_lazy : true has_tickets storage >|=? fun ( tickets , ctxt ) -> ( Some { tickets ; destination } , ctxt )
|
let tickets_of_operation ctxt ( Internal_operation { source = _ ; operation ; nonce = _ } ) = match operation with | Reveal _ -> return ( None , ctxt ) | Transaction { amount = _ ; parameters ; entrypoint = _ ; destination = Destination . Contract destination ; } -> tickets_of_transaction ctxt ~ destination ~ parameters | Origination { delegate = _ ; script ; credit = _ ; preorigination } -> tickets_of_origination ctxt ~ preorigination script | Delegation _ -> return ( None , ctxt ) | Register_global_constant _ -> return ( None , ctxt ) | Set_deposits_limit _ -> return ( None , ctxt ) | Tx_rollup_origination -> return ( None , ctxt ) | Tx_rollup_submit_batch _ -> return ( None , ctxt ) | Sc_rollup_originate { kind = _ ; boot_sector = _ } -> return ( None , ctxt ) | Sc_rollup_add_messages { rollup = _ ; messages = _ } -> return ( None , ctxt )
|
let add_transfer_to_token_map ctxt token_map { destination ; tickets } = List . fold_left_es ( fun ( token_map , ctxt ) ticket -> let ( ticket_token , amount ) = Ticket_token . token_and_amount_of_ex_ticket ticket in Ticket_token_map . add ctxt ~ ticket_token ~ destination ~ amount token_map ) ( token_map , ctxt ) tickets
|
let ticket_token_map_of_operations ctxt ops = List . fold_left_es ( fun ( token_map , ctxt ) op -> tickets_of_operation ctxt op >>=? fun ( res , ctxt ) -> match res with | Some ticket_trans -> add_transfer_to_token_map ctxt token_map ticket_trans | None -> return ( token_map , ctxt ) ) ( Ticket_token_map . empty , ctxt ) ops
|
let ticket_diffs_of_operations ctxt { Script_typed_ir . elements ; length = _ } = ticket_token_map_of_operations ctxt elements >>=? fun ( token_map , ctxt ) -> Ticket_token_map . fold ctxt ( fun ctxt acc ticket_token destination_map -> Contract_map . fold ctxt ( fun ctxt total_amount _destination amount -> Gas . consume ctxt ( Ticket_costs . add_cost total_amount amount ) >|? fun ctxt -> ( Script_int . add_n total_amount amount , ctxt ) ) Script_int . zero_n destination_map >>? fun ( total_amount , ctxt ) -> Contract_map . to_list ctxt destination_map >|? fun ( destinations , ctxt ) -> ( { ticket_token ; total_amount ; destinations } :: acc , ctxt ) ) [ ] token_map |> Lwt . return
|
type error += Unsupported_non_empty_overlay | Unsupported_type_operation
|
let ( ) = register_error_kind ` Branch ~ id " : Unsupported_non_empty_overlay " ~ title " : Unsupported non empty overlay " ~ description " : Unsupported big - map value with non - empty overlay " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Unsupported big - map value with non - empty overlay " ) Data_encoding . empty ( function Unsupported_non_empty_overlay -> Some ( ) | _ -> None ) ( fun ( ) -> Unsupported_non_empty_overlay ) ; register_error_kind ` Branch ~ id " : Unsupported_type_operation " ~ title " : Unsupported type operation " ~ description " : Types embedding operations are not supported " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Types embedding operations are not supported " ) Data_encoding . empty ( function Unsupported_type_operation -> Some ( ) | _ -> None ) ( fun ( ) -> Unsupported_type_operation )
|
type ex_ticket = | Ex_ticket : ' a Script_typed_ir . comparable_ty * ' a Script_typed_ir . ticket -> ex_ticket
|
module Ticket_inspection = struct type ' a has_tickets = | True_ht : _ Script_typed_ir . ticket has_tickets | False_ht : _ has_tickets | Pair_ht : ' a has_tickets * ' b has_tickets -> ( ' a , ' b ) Script_typed_ir . pair has_tickets | Union_ht : ' a has_tickets * ' b has_tickets -> ( ' a , ' b ) Script_typed_ir . union has_tickets | Option_ht : ' a has_tickets -> ' a option has_tickets | List_ht : ' a has_tickets -> ' a Script_typed_ir . boxed_list has_tickets | Set_ht : ' k has_tickets -> ' k Script_typed_ir . set has_tickets | Map_ht : ' k has_tickets * ' v has_tickets -> ( ' k , ' v ) Script_typed_ir . map has_tickets | Big_map_ht : ' k has_tickets * ' v has_tickets -> ( ' k , ' v ) Script_typed_ir . big_map has_tickets let has_tickets_of_comparable : type a ret . a Script_typed_ir . comparable_ty -> ( a has_tickets -> ret ) -> ret = fun key_ty k -> let open Script_typed_ir in match key_ty with | Unit_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Never_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Int_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Nat_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Signature_key -> ( k [ @ ocaml . tailcall ] ) False_ht | String_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Bytes_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Mutez_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Bool_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Key_hash_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Key_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Timestamp_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Chain_id_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Address_key -> ( k [ @ ocaml . tailcall ] ) False_ht | Pair_key ( _ , _ , _ ) -> ( k [ @ ocaml . tailcall ] ) False_ht | Union_key ( _ , _ , _ ) -> ( k [ @ ocaml . tailcall ] ) False_ht | Option_key ( _ , _ ) -> ( k [ @ ocaml . tailcall ] ) False_ht let pair_has_tickets pair ht1 ht2 = match ( ht1 , ht2 ) with ( False_ht , False_ht ) -> False_ht | _ -> pair ht1 ht2 let map_has_tickets map ht = match ht with False_ht -> False_ht | _ -> map ht type ( ' a , ' r ) continuation = ' a has_tickets -> ' r tzresult let rec has_tickets_of_ty : type a ret . a Script_typed_ir . ty -> ( a , ret ) continuation -> ret tzresult = fun ty k -> let open Script_typed_ir in match ty with | Ticket_t _ -> ( k [ @ ocaml . tailcall ] ) True_ht | Unit_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Int_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Nat_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Signature_t -> ( k [ @ ocaml . tailcall ] ) False_ht | String_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Bytes_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Mutez_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Key_hash_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Key_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Timestamp_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Address_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Bool_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Pair_t ( ty1 , ty2 , _ ) -> ( has_tickets_of_pair [ @ ocaml . tailcall ] ) ty1 ty2 ~ pair ( : fun ht1 ht2 -> Pair_ht ( ht1 , ht2 ) ) k | Union_t ( ty1 , ty2 , _ ) -> ( has_tickets_of_pair [ @ ocaml . tailcall ] ) ty1 ty2 ~ pair ( : fun ht1 ht2 -> Union_ht ( ht1 , ht2 ) ) k | Lambda_t ( _ , _ , _ ) -> ( k [ @ ocaml . tailcall ] ) False_ht | Option_t ( ty , _ ) -> ( has_tickets_of_ty [ @ ocaml . tailcall ] ) ty ( fun ht -> let opt_hty = map_has_tickets ( fun ht -> Option_ht ht ) ht in ( k [ @ ocaml . tailcall ] ) opt_hty ) | List_t ( ty , _ ) -> ( has_tickets_of_ty [ @ ocaml . tailcall ] ) ty ( fun ht -> let list_hty = map_has_tickets ( fun ht -> List_ht ht ) ht in ( k [ @ ocaml . tailcall ] ) list_hty ) | Set_t ( key_ty , _ ) -> ( has_tickets_of_comparable [ @ ocaml . tailcall ] ) key_ty ( fun ht -> let set_hty = map_has_tickets ( fun ht -> Set_ht ht ) ht in ( k [ @ ocaml . tailcall ] ) set_hty ) | Map_t ( key_ty , val_ty , _ ) -> ( has_tickets_of_key_and_value [ @ ocaml . tailcall ] ) key_ty val_ty ~ pair ( : fun ht1 ht2 -> Map_ht ( ht1 , ht2 ) ) k | Big_map_t ( key_ty , val_ty , _ ) -> ( has_tickets_of_key_and_value [ @ ocaml . tailcall ] ) key_ty val_ty ~ pair ( : fun ht1 ht2 -> Big_map_ht ( ht1 , ht2 ) ) k | Contract_t _ -> ( k [ @ ocaml . tailcall ] ) False_ht | Sapling_transaction_t _ -> ( k [ @ ocaml . tailcall ] ) False_ht | Sapling_state_t _ -> ( k [ @ ocaml . tailcall ] ) False_ht | Operation_t -> error Unsupported_type_operation | Chain_id_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Never_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Bls12_381_g1_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Bls12_381_g2_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Bls12_381_fr_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Chest_t -> ( k [ @ ocaml . tailcall ] ) False_ht | Chest_key_t -> ( k [ @ ocaml . tailcall ] ) False_ht and has_tickets_of_pair : type a b c ret . a Script_typed_ir . ty -> b Script_typed_ir . ty -> pair ( : a has_tickets -> b has_tickets -> c has_tickets ) -> ( c , ret ) continuation -> ret tzresult = fun ty1 ty2 ~ pair k -> ( has_tickets_of_ty [ @ ocaml . tailcall ] ) ty1 ( fun ht1 -> ( has_tickets_of_ty [ @ ocaml . tailcall ] ) ty2 ( fun ht2 -> ( k [ @ ocaml . tailcall ] ) ( pair_has_tickets pair ht1 ht2 ) ) ) and has_tickets_of_key_and_value : type k v t ret . k Script_typed_ir . comparable_ty -> v Script_typed_ir . ty -> pair ( : k has_tickets -> v has_tickets -> t has_tickets ) -> ( t , ret ) continuation -> ret tzresult = fun key_ty val_ty ~ pair k -> ( has_tickets_of_comparable [ @ ocaml . tailcall ] ) key_ty ( fun ht1 -> ( has_tickets_of_ty [ @ ocaml . tailcall ] ) val_ty ( fun ht2 -> ( k [ @ ocaml . tailcall ] ) ( pair_has_tickets pair ht1 ht2 ) ) ) let has_tickets_of_ty ctxt ty = Gas . consume ctxt ( Ticket_costs . has_tickets_of_ty_cost ty ) >>? fun ctxt -> has_tickets_of_ty ty ok >|? fun ht -> ( ht , ctxt ) end
|
module Ticket_collection = struct let consume_gas_steps = Ticket_costs . consume_gas_steps ~ step_cost : Ticket_costs . Constants . cost_collect_tickets_step type accumulator = ex_ticket list type ' a continuation = Alpha_context . context -> accumulator -> ' a tzresult Lwt . t let tickets_of_comparable : type a ret . Alpha_context . context -> a Script_typed_ir . comparable_ty -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ctxt comp_ty acc k -> let open Script_typed_ir in match comp_ty with | Unit_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Never_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Int_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Nat_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Signature_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | String_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Bytes_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Mutez_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Bool_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Key_hash_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Key_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Timestamp_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Chain_id_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Address_key -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Pair_key ( _ , _ , _ ) -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Union_key ( _ , _ , _ ) -> ( k [ @ ocaml . tailcall ] ) ctxt acc | Option_key ( _ , _ ) -> ( k [ @ ocaml . tailcall ] ) ctxt acc let tickets_of_set : type a ret . Alpha_context . context -> a Script_typed_ir . comparable_ty -> a Script_typed_ir . set -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ctxt key_ty _set acc k -> consume_gas_steps ctxt ~ num_steps : 1 >>?= fun ctxt -> ( tickets_of_comparable [ @ ocaml . tailcall ] ) ctxt key_ty acc k let rec tickets_of_value : type a ret . include_lazy : bool -> Alpha_context . context -> a Ticket_inspection . has_tickets -> a Script_typed_ir . ty -> a -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ~ include_lazy ctxt hty ty x acc k -> let open Script_typed_ir in consume_gas_steps ctxt ~ num_steps : 1 >>?= fun ctxt -> match ( hty , ty ) with | ( False_ht , _ ) -> ( k [ @ ocaml . tailcall ] ) ctxt acc | ( Pair_ht ( hty1 , hty2 ) , Pair_t ( ty1 , ty2 , _ ) ) -> let ( l , r ) = x in ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt hty1 ty1 l acc ( fun ctxt acc -> ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt hty2 ty2 r acc k ) | ( Union_ht ( htyl , htyr ) , Union_t ( tyl , tyr , _ ) ) -> ( match x with | L v -> ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt htyl tyl v acc k | R v -> ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt htyr tyr v acc k ) | ( Option_ht el_hty , Option_t ( el_ty , _ ) ) -> ( match x with | Some x -> ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt el_hty el_ty x acc k | None -> ( k [ @ ocaml . tailcall ] ) ctxt acc ) | ( List_ht el_hty , List_t ( el_ty , _ ) ) -> let { elements ; _ } = x in ( tickets_of_list [ @ ocaml . tailcall ] ) ctxt ~ include_lazy el_hty el_ty elements acc k | ( Set_ht _ , Set_t ( key_ty , _ ) ) -> ( tickets_of_set [ @ ocaml . tailcall ] ) ctxt key_ty x acc k | ( Map_ht ( _ , val_hty ) , Map_t ( key_ty , val_ty , _ ) ) -> ( tickets_of_comparable [ @ ocaml . tailcall ] ) ctxt key_ty acc ( fun ctxt acc -> ( tickets_of_map [ @ ocaml . tailcall ] ) ctxt ~ include_lazy val_hty val_ty x acc k ) | ( Big_map_ht ( _ , val_hty ) , Big_map_t ( key_ty , _ , _ ) ) -> if include_lazy then ( tickets_of_big_map [ @ ocaml . tailcall ] ) ctxt val_hty key_ty x acc k else ( k [ @ ocaml . tailcall ] ) ctxt acc | ( True_ht , Ticket_t ( comp_ty , _ ) ) -> ( k [ @ ocaml . tailcall ] ) ctxt ( Ex_ticket ( comp_ty , x ) :: acc ) and tickets_of_list : type a ret . Alpha_context . context -> include_lazy : bool -> a Ticket_inspection . has_tickets -> a Script_typed_ir . ty -> a list -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ctxt ~ include_lazy el_hty el_ty elements acc k -> consume_gas_steps ctxt ~ num_steps : 1 >>?= fun ctxt -> match elements with | elem :: elems -> ( tickets_of_value [ @ ocaml . tailcall ] ) ~ include_lazy ctxt el_hty el_ty elem acc ( fun ctxt acc -> ( tickets_of_list [ @ ocaml . tailcall ] ) ~ include_lazy ctxt el_hty el_ty elems acc k ) | [ ] -> ( k [ @ ocaml . tailcall ] ) ctxt acc and tickets_of_map : type k v ret . include_lazy : bool -> Alpha_context . context -> v Ticket_inspection . has_tickets -> v Script_typed_ir . ty -> ( k , v ) Script_typed_ir . map -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ~ include_lazy ctxt val_hty val_ty map acc k -> let ( module M ) = Script_map . get_module map in consume_gas_steps ctxt ~ num_steps : 1 >>?= fun ctxt -> consume_gas_steps ctxt ~ num_steps : M . size >>?= fun ctxt -> let values = M . OPS . fold ( fun _ v vs -> v :: vs ) M . boxed [ ] in ( tickets_of_list [ @ ocaml . tailcall ] ) ~ include_lazy ctxt val_hty val_ty values acc k and tickets_of_big_map : type k v ret . Alpha_context . context -> v Ticket_inspection . has_tickets -> k Script_typed_ir . comparable_ty -> ( k , v ) Script_typed_ir . big_map -> accumulator -> ret continuation -> ret tzresult Lwt . t = fun ctxt val_hty key_ty { Script_typed_ir . id ; diff = { map = _ ; size } ; key_type = _ ; value_type } acc k -> consume_gas_steps ctxt ~ num_steps : 1 >>?= fun ctxt -> if Compare . Int . ( size > 0 ) then fail Unsupported_non_empty_overlay else ( tickets_of_comparable [ @ ocaml . tailcall ] ) ctxt key_ty acc ( fun ctxt acc -> match id with | Some id -> let accum ( values , ctxt ) exp = Script_ir_translator . parse_data ~ legacy : true ctxt ~ allow_forged : true value_type ( Micheline . root exp ) >|=? fun ( v , ctxt ) -> ( v :: values , ctxt ) in Big_map . list_values ctxt id >>=? fun ( ctxt , exps ) -> List . fold_left_es accum ( [ ] , ctxt ) exps >>=? fun ( values , ctxt ) -> ( tickets_of_list [ @ ocaml . tailcall ] ) ~ include_lazy : true ctxt val_hty value_type values acc k | None -> ( k [ @ ocaml . tailcall ] ) ctxt acc ) let tickets_of_value ctxt ~ include_lazy ht ty x = tickets_of_value ctxt ~ include_lazy ht ty x [ ] ( fun ctxt ex_tickets -> return ( ex_tickets , ctxt ) ) end
|
type ' a has_tickets = ' a Ticket_inspection . has_tickets * ' a Script_typed_ir . ty
|
let type_has_tickets ctxt ty = Ticket_inspection . has_tickets_of_ty ctxt ty >|? fun ( has_tickets , ctxt ) -> ( ( has_tickets , ty ) , ctxt )
|
let tickets_of_value ctxt ~ include_lazy ( ht , ty ) = Ticket_collection . tickets_of_value ctxt ~ include_lazy ht ty
|
let tickets_of_node ctxt ~ include_lazy ( ht , ty ) expr = match ht with | Ticket_inspection . False_ht -> return ( [ ] , ctxt ) | _ -> Script_ir_translator . parse_data ctxt ~ legacy : true ~ allow_forged : true ty expr >>=? fun ( value , ctxt ) -> tickets_of_value ctxt ~ include_lazy ( ht , ty ) value
|
type error += | Negative_ticket_balance of { key : Ticket_hash_repr . t ; balance : Z . t }
|
let ( ) = let open Data_encoding in register_error_kind ` Permanent ~ id " : Negative_ticket_balance " ~ title " : Negative ticket balance " ~ description " : Attempted to set a negative ticket balance value " ~ pp ( : fun ppf ( key , balance ) -> Format . fprintf ppf " Attempted to set negative ticket balance value ' % a ' for key % a . " Z . pp_print balance Ticket_hash_repr . pp key ) ( obj2 ( req " key " Ticket_hash_repr . encoding ) ( req " balance " Data_encoding . z ) ) ( function | Negative_ticket_balance { key ; balance } -> Some ( key , balance ) | _ -> None ) ( fun ( key , balance ) -> Negative_ticket_balance { key ; balance } )
|
let get_balance ctxt key = Storage . Ticket_balance . Table . find ctxt key >|=? fun ( ctxt , res ) -> ( res , ctxt )
|
let set_balance ctxt key balance = let cost_of_key = Z . of_int 65 in fail_when Compare . Z . ( balance < Z . zero ) ( Negative_ticket_balance { key ; balance } ) >>=? fun ( ) -> if Compare . Z . ( balance = Z . zero ) then Storage . Ticket_balance . Table . remove ctxt key >|=? fun ( ctxt , freed , existed ) -> let freed = if existed then Z . neg @@ Z . add cost_of_key ( Z . of_int freed ) else Z . zero in ( freed , ctxt ) else Storage . Ticket_balance . Table . add ctxt key balance >|=? fun ( ctxt , size_diff , existed ) -> let size_diff = let z_diff = Z . of_int size_diff in if existed then z_diff else Z . add cost_of_key z_diff in ( size_diff , ctxt )
|
let adjust_balance ctxt key ~ delta = get_balance ctxt key >>=? fun ( res , ctxt ) -> let old_balance = Option . value ~ default : Z . zero res in set_balance ctxt key ( Z . add old_balance delta )
|
module Ticket_token_map = Carbonated_map . Make ( struct type t = Ticket_hash . t let compare = Ticket_hash . compare let compare_cost _ = Ticket_costs . Constants . cost_compare_ticket_hash end )
|
type ' a t = ( Ticket_token . ex_token * ' a ) Ticket_token_map . t
|
let key_of_ticket_token ctxt ( Ticket_token . Ex_token { ticketer ; _ } as token ) = Ticket_balance_key . ticket_balance_key ctxt ~ owner : ticketer token
|
let update ctxt key f m = key_of_ticket_token ctxt key >>=? fun ( key_hash , ctxt ) -> let f ctxt val_opt = ( match val_opt with | Some ( _tkn , value ) -> f ctxt ( Some value ) | None -> f ctxt None ) >|? fun ( val_opt , ctxt ) -> ( Option . map ( fun v -> ( key , v ) ) val_opt , ctxt ) in Ticket_token_map . update ctxt key_hash f m |> Lwt . return
|
let fold ctxt f = Ticket_token_map . fold ctxt ( fun ctxt acc _key_hash ( tkn , value ) -> f ctxt acc tkn value )
|
let tick_stories f conf save_progress_bar ( init , last , counter , n_stories ) = let ( ) = if not init then let c = ref conf . Configuration . progressSize in let ( ) = Loggers . print_newline f in while ! c > 0 do Loggers . fprintf f " _ " ; c :=! c - 1 done ; Loggers . print_newline f in let n = if n_stories <= 0 && counter = 0 then conf . Configuration . progressSize else if counter > n_stories then 0 else let nc = ( counter * conf . Configuration . progressSize ) / n_stories in let nl = ( last * conf . Configuration . progressSize ) / n_stories in nc - nl in let rec aux n = if n <= 0 then ( ) else let ( ) = Loggers . fprintf f " % c " ( conf . Configuration . progressChar ) in aux ( n - 1 ) in let ( ) = aux n in let ( ) = Loggers . flush_logger f in let ( ) = if counter = n_stories then Loggers . print_newline f in let bar = ( true , counter , counter + 1 , n_stories ) in let ( ) = save_progress_bar bar in bar
|
type area = Area1 | Area0
|
type t = { tiles : Color_id . t array array array ; start_addr : uint16 ; end_addr : uint16 ; }
|
let create ~ start_addr ~ end_addr = let tiles = Array . init 384 ( fun _ -> Array . make_matrix 8 8 Color_id . ID_00 ) in { tiles ; start_addr ; end_addr ; }
|
let get_row_pixels t ~ area ( ~ index : uint8 ) ~ row = let index = match area with | Area1 -> Uint8 . to_int index | Area0 -> let signed_index = index |> Int8 . of_byte |> Int8 . to_int in signed_index + 256 in if row >= 8 then t . tiles . ( index + 1 ) . ( row - 8 ) else t . tiles . ( index ) . ( row )
|
let get_pixel t ~ area ( ~ index : uint8 ) ~ row ~ col = let row = get_row_pixels t ~ area ~ index ~ row in row . ( col )
|
let get_full_pixels t ~ area ~ index = [ | 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ] | |> Array . map ( fun row -> get_row_pixels t ~ area ~ index ~ row )
|
let print_full_pixels t ~ area ~ index = get_full_pixels t ~ area ~ index |> Array . iter ( fun color_ids -> color_ids |> Array . map ( Color_id . to_int ) |> Array . iter ( print_int ) ; print_newline ( ) )
|
let accepts t addr = Uint16 . ( t . start_addr <= addr && addr <= t . end_addr )
|
let read_byte t addr = let offset = Uint16 . ( addr - t . start_addr ) |> Uint16 . to_int in let index = offset / 16 in let row = ( offset mod 16 ) / 2 in let hi_or_lo = if offset mod 2 = 0 then ` Lo else ` Hi in t . tiles . ( index ) . ( row ) |> Array . map ( fun id -> Color_id . get_bit id hi_or_lo ) |> Bit_util . byte_of_bitarray
|
let write_byte t ~ addr ~ data = let data_bits = Bit_util . bitarray_of_byte data in let offset = Uint16 . ( addr - t . start_addr ) |> Uint16 . to_int in let index = offset / 16 in let row = ( offset mod 16 ) / 2 in let colors_in_row = t . tiles . ( index ) . ( row ) in let hi_or_lo = if offset mod 2 = 0 then ` Lo else ` Hi in t . tiles . ( index ) . ( row ) <- data_bits |> Array . mapi ( fun i b -> let id = colors_in_row . ( i ) in if b then Color_id . set_bit id hi_or_lo else Color_id . clear_bit id hi_or_lo )
|
type t = { area0_map : uint8 array array ; area1_map : uint8 array array ; area0_start_addr : uint16 ; area0_end_addr : uint16 ; area1_start_addr : uint16 ; area1_end_addr : uint16 ; }
|
type area = | Area0 | Area1
|
let create ~ area0_start_addr ~ area0_end_addr ~ area1_start_addr ~ area1_end_addr = { area0_map = Array . make_matrix 32 32 Uint8 . zero ; area1_map = Array . make_matrix 32 32 Uint8 . zero ; area0_start_addr ; area0_end_addr ; area1_start_addr ; area1_end_addr ; }
|
let get_tile_index t ~ area ~ y ~ x = match area with | Area0 -> t . area0_map . ( y / 8 ) . ( x / 8 ) | Area1 -> t . area1_map . ( y / 8 ) . ( x / 8 )
|
let accepts t addr = Uint16 . ( t . area0_start_addr <= addr && addr <= t . area0_end_addr || t . area1_start_addr <= addr && addr <= t . area1_end_addr )
|
let read_byte t addr = if Uint16 . ( t . area0_start_addr <= addr && addr <= t . area0_end_addr ) then let offset = Uint16 . ( to_int ( addr - t . area0_start_addr ) ) in t . area0_map . ( offset / 32 ) . ( offset mod 32 ) else if Uint16 . ( t . area1_start_addr <= addr && addr <= t . area1_end_addr ) then let offset = Uint16 . ( to_int ( addr - t . area1_start_addr ) ) in t . area1_map . ( offset / 32 ) . ( offset mod 32 ) else raise @@ Invalid_argument ( Printf . sprintf " Address out of range : % s " ( Uint16 . show addr ) )
|
let write_byte t ~ addr ~ data = if Uint16 . ( t . area0_start_addr <= addr && addr <= t . area0_end_addr ) then let offset = Uint16 . ( to_int ( addr - t . area0_start_addr ) ) in t . area0_map . ( offset / 32 ) . ( offset mod 32 ) <- data else if Uint16 . ( t . area1_start_addr <= addr && addr <= t . area1_end_addr ) then let offset = Uint16 . ( to_int ( addr - t . area1_start_addr ) ) in t . area1_map . ( offset / 32 ) . ( offset mod 32 ) <- data else raise @@ Invalid_argument ( Printf . sprintf " Address out of range : % s " ( Uint16 . show addr ) )
|
let var i = mk_var ( 1 + i )
|
let m = Array . make_matrix n 64 false
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.