Skip to content

Commit

Permalink
Move zkapp_uri to Zkapp_account
Browse files Browse the repository at this point in the history
  • Loading branch information
psteckler committed Oct 26, 2022
1 parent 1f335b7 commit a01109d
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 130 deletions.
22 changes: 12 additions & 10 deletions src/app/archive/lib/load_data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ let get_account_accessed ~pool (account : Processor.Accounts_accessed.t) :
; zkapp_version
; sequence_state_id
; last_sequence_slot
; proved_state (* TODO : we'll have this at some point *)
; zkapp_uri_id = _
; proved_state
; zkapp_uri_id
}
->
let%bind { element0
Expand Down Expand Up @@ -838,7 +838,7 @@ let get_account_accessed ~pool (account : Processor.Accounts_accessed.t) :
Processor.Zkapp_sequence_states.load db sequence_state_id )
in
let elements = [ element0; element1; element2; element3; element4 ] in
let%map sequence_state =
let%bind sequence_state =
let%map field_strs =
Deferred.List.map elements ~f:(fun id ->
query_db ~f:(fun db -> Processor.Zkapp_state_data.load db id) )
Expand All @@ -850,22 +850,25 @@ let get_account_accessed ~pool (account : Processor.Accounts_accessed.t) :
last_sequence_slot |> Unsigned.UInt32.of_int64
|> Mina_numbers.Global_slot.of_uint32
in
let%map zkapp_uri =
Option.value_map zkapp_uri_id ~default:(return None)
~f:(fun zkapp_uri_id ->
let%map uri =
query_db ~f:(fun db -> Processor.Zkapp_uri.load db zkapp_uri_id)
in
Some uri )
in
Some
( { app_state
; verification_key
; zkapp_version
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
}
: Mina_base.Zkapp_account.t ) )
in
(* TODO: the URI will be moved to the zkApp, no longer in the account *)
let%bind zkapp_uri =
Option.value_map zkapp_db ~default:(return "https://dummy.com")
~f:(fun zkapp ->
query_db ~f:(fun db -> Processor.Zkapp_uri.load db zkapp.zkapp_uri_id) )
in
(* TODO: token permissions is going away *)
let account =
( { public_key
Expand All @@ -881,7 +884,6 @@ let get_account_accessed ~pool (account : Processor.Accounts_accessed.t) :
; timing
; permissions
; zkapp
; zkapp_uri
}
: Mina_base.Account.t )
in
Expand Down
15 changes: 8 additions & 7 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2445,25 +2445,25 @@ module Zkapp_account = struct
; sequence_state_id : int
; last_sequence_slot : int64
; proved_state : bool
; zkapp_uri_id : int
; zkapp_uri_id : int option
}
[@@deriving fields, hlist]

let typ =
Mina_caqti.Type_spec.custom_type ~to_hlist ~of_hlist
Caqti_type.[ int; option int; int64; int; int64; bool; int ]
Caqti_type.[ int; option int; int64; int; int64; bool; option int ]

let table_name = "zkapp_accounts"

(* TODO: when zkapp_uri moved to Zkapp.Account in OCaml, no need to pass it in separately *)
let add_if_doesn't_exist (module Conn : CONNECTION) zkapp_uri zkapp_account =
let add_if_doesn't_exist (module Conn : CONNECTION) zkapp_account =
let open Deferred.Result.Let_syntax in
let ({ app_state
; verification_key
; zkapp_version
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
}
: Mina_base.Zkapp_account.t ) =
zkapp_account
Expand All @@ -2487,7 +2487,9 @@ module Zkapp_account = struct
|> Unsigned.UInt32.to_int64
in
let%bind zkapp_uri_id =
Zkapp_uri.add_if_doesn't_exist (module Conn) zkapp_uri
Option.value_map zkapp_uri ~default:(return None) ~f:(fun zkapp_uri ->
let%map id = Zkapp_uri.add_if_doesn't_exist (module Conn) zkapp_uri in
Some id )
in
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
~table_name ~cols:(Fields.names, typ)
Expand Down Expand Up @@ -2599,9 +2601,8 @@ module Accounts_accessed = struct
account.permissions
in
let%bind zkapp_id =
(* TODO: when zkapp_uri part of Zkapp.Account.t, don't pass it separately here *)
Mina_caqti.add_if_some
(Zkapp_account.add_if_doesn't_exist (module Conn) account.zkapp_uri)
(Zkapp_account.add_if_doesn't_exist (module Conn))
account.zkapp
in
let account_accessed : t =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module Accounts = struct
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
} ->
let%bind app_state =
if
Expand Down Expand Up @@ -157,6 +158,7 @@ module Accounts = struct
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
}
in
( { public_key = account.public_key
Expand All @@ -177,7 +179,6 @@ module Accounts = struct
~f:Mina_base.State_hash.of_base58_check_exn t.voting_for
; zkapp
; permissions
; zkapp_uri = Option.value ~default:"" t.zkapp_uri
}
: Mina_base.Account.t )

Expand Down Expand Up @@ -268,6 +269,7 @@ module Accounts = struct
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
}
->
let state = Zkapp_state.V.to_list app_state in
Expand All @@ -284,6 +286,7 @@ module Accounts = struct
; sequence_state
; last_sequence_slot
; proved_state
; zkapp_uri
} )
in
{ pk =
Expand All @@ -308,7 +311,6 @@ module Accounts = struct
; zkapp
; permissions
; token_symbol = Some account.token_symbol
; zkapp_uri = Some account.zkapp_uri
}
end

Expand Down
60 changes: 7 additions & 53 deletions src/lib/mina_base/account.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ module Poly = struct
, 'state_hash
, 'timing
, 'permissions
, 'zkapp_opt
, 'zkapp_uri )
, 'zkapp_opt )
t =
{ public_key : 'pk
; token_id : 'id
Expand All @@ -249,7 +248,6 @@ module Poly = struct
; timing : 'timing
; permissions : 'permissions
; zkapp : 'zkapp_opt
; zkapp_uri : 'zkapp_uri
}
[@@deriving sexp, equal, compare, hash, yojson, fields, hlist]

Expand Down Expand Up @@ -322,8 +320,7 @@ module Binable_arg = struct
, State_hash.Stable.V1.t
, Timing.Stable.V1.t
, Permissions.Stable.V2.t
, Zkapp_account.Stable.V2.t option
, string )
, Zkapp_account.Stable.V2.t option )
(* TODO: Cache the digest of this? *)
Poly.Stable.V2.t
[@@deriving sexp, equal, hash, compare, yojson]
Expand Down Expand Up @@ -377,8 +374,7 @@ type value =
, State_hash.t
, Timing.t
, Permissions.t
, Zkapp_account.t option
, string )
, Zkapp_account.t option )
Poly.t
[@@deriving sexp]

Expand All @@ -403,7 +399,6 @@ let initialize account_id : t =
; timing = Timing.Untimed
; permissions = Permissions.user_default
; zkapp = None
; zkapp_uri = ""
}

let hash_zkapp_account_opt = function
Expand All @@ -412,38 +407,6 @@ let hash_zkapp_account_opt = function
| Some (a : Zkapp_account.t) ->
Zkapp_account.digest a

(* This preimage cannot be attained by any string, due to the trailing [true]
added below.
*)
let zkapp_uri_non_preimage =
lazy (Random_oracle_input.Chunked.field_elements [| Field.zero; Field.zero |])

let hash_zkapp_uri_opt (zkapp_uri_opt : string option) =
let input =
match zkapp_uri_opt with
| Some zkapp_uri ->
(* We use [length*8 + 1] to pass a final [true] after the end of the
string, to ensure that trailing null bytes don't alias in the hash
preimage.
*)
let bits = Array.create ~len:((String.length zkapp_uri * 8) + 1) true in
String.foldi zkapp_uri ~init:() ~f:(fun i () c ->
let c = Char.to_int c in
(* Insert the bits into [bits], LSB order. *)
for j = 0 to 7 do
(* [Int.test_bit c j] *)
bits.((i * 8) + j) <- Int.bit_and c (1 lsl j) <> 0
done ) ;
Random_oracle_input.Chunked.packeds
(Array.map ~f:(fun b -> (field_of_bool b, 1)) bits)
| None ->
Lazy.force zkapp_uri_non_preimage
in
Random_oracle.pack_input input
|> Random_oracle.hash ~init:Hash_prefix_states.zkapp_uri

let hash_zkapp_uri (zkapp_uri : string) = hash_zkapp_uri_opt (Some zkapp_uri)

let delegate_opt = Option.value ~default:Public_key.Compressed.empty

let to_input (t : t) =
Expand All @@ -459,7 +422,6 @@ let to_input (t : t) =
~voting_for:(f State_hash.to_input) ~timing:(f Timing.to_input)
~zkapp:(f (Fn.compose field hash_zkapp_account_opt))
~permissions:(f Permissions.to_input)
~zkapp_uri:(f (Fn.compose field hash_zkapp_uri))
|> List.reduce_exn ~f:append

let crypto_hash_prefix = Hash_prefix.account
Expand All @@ -482,9 +444,8 @@ type var =
, State_hash.var
, Timing.var
, Permissions.Checked.t
, Field.Var.t * Zkapp_account.t option As_prover.Ref.t
(* TODO: This is a hack that lets us avoid unhashing zkApp accounts when we don't need to *)
, string Data_as_hash.t )
(* TODO: This is a hack that lets us avoid unhashing zkApp accounts when we don't need to *)
, Field.Var.t * Zkapp_account.t option As_prover.Ref.t )
Poly.t

let identifier_of_var ({ public_key; token_id; _ } : var) =
Expand All @@ -507,7 +468,6 @@ let typ' zkapp =
; Timing.typ
; Permissions.typ
; zkapp
; Data_as_hash.typ ~hash:hash_zkapp_uri
]
~var_to_hlist:Poly.to_hlist ~var_of_hlist:Poly.of_hlist
~value_to_hlist:Poly.to_hlist ~value_of_hlist:Poly.of_hlist
Expand Down Expand Up @@ -539,7 +499,6 @@ let var_of_t
; timing
; permissions
; zkapp
; zkapp_uri
} :
value ) =
{ Poly.public_key = Public_key.Compressed.var_of_t public_key
Expand All @@ -554,7 +513,6 @@ let var_of_t
; timing = Timing.var_of_t timing
; permissions = Permissions.Checked.constant permissions
; zkapp = Field.Var.constant (hash_zkapp_account_opt zkapp)
; zkapp_uri = Field.Var.constant (hash_zkapp_uri zkapp_uri)
}

module Checked = struct
Expand All @@ -571,8 +529,7 @@ module Checked = struct
, State_hash.var
, Timing.var
, Permissions.Checked.t
, Zkapp_account.Checked.t
, string Data_as_hash.t )
, Zkapp_account.Checked.t )
Poly.t

let typ : (t, Stable.Latest.t) Typ.t =
Expand All @@ -597,7 +554,7 @@ module Checked = struct
~receipt_chain_hash:(f Receipt.Chain_hash.var_to_input)
~delegate:(f Public_key.Compressed.Checked.to_input)
~voting_for:(f State_hash.var_to_input)
~timing:(f Timing.var_to_input) ~zkapp_uri:(f Data_as_hash.to_input) )
~timing:(f Timing.var_to_input) )

let digest t =
make_checked (fun () ->
Expand Down Expand Up @@ -695,7 +652,6 @@ let empty =
Permissions.user_default
(* TODO: This should maybe be Permissions.empty *)
; zkapp = None
; zkapp_uri = ""
}

let empty_digest = digest empty
Expand All @@ -719,7 +675,6 @@ let create account_id balance =
; timing = Timing.Untimed
; permissions = Permissions.user_default
; zkapp = None
; zkapp_uri = ""
}

let create_timed account_id balance ~initial_minimum_balance ~cliff_time
Expand Down Expand Up @@ -756,7 +711,6 @@ let create_timed account_id balance ~initial_minimum_balance ~cliff_time
; vesting_period
; vesting_increment
}
; zkapp_uri = ""
}

(* no vesting after cliff time + 1 slot *)
Expand Down
Loading

0 comments on commit a01109d

Please sign in to comment.