Skip to content

Commit

Permalink
Merge branch 'berkeley' into test/staged-ledger-nonce-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost-not-in-the-shell authored Sep 25, 2023
2 parents 9a93042 + f611438 commit 8b3eb4c
Show file tree
Hide file tree
Showing 49 changed files with 491 additions and 589 deletions.
4 changes: 0 additions & 4 deletions src/app/archive/drop_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ DROP TABLE zkapp_network_precondition;

DROP TABLE zkapp_account_precondition;

DROP TABLE zkapp_account_precondition_values;

DROP TABLE zkapp_accounts;

DROP TABLE zkapp_epoch_data;
Expand Down Expand Up @@ -88,8 +86,6 @@ DROP TABLE zkapp_token_id_bounds;

DROP TYPE zkapp_auth_required_type;

DROP TYPE zkapp_precondition_type;

DROP TABLE snarked_ledger_hashes;

DROP TABLE timing_info;
Expand Down
229 changes: 104 additions & 125 deletions src/app/archive/lib/load_data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -462,139 +462,118 @@ let get_account_update_body ~pool body_id =
protocol_state_precondition_of_id pool zkapp_network_precondition_id
in
let%bind account_precondition =
let%bind ({ kind; account_precondition_values_id; nonce }
let%bind ({ balance_id
; nonce_id
; receipt_chain_hash
; delegate_id
; state_id
; action_state_id
; proved_state
; is_new
}
: Processor.Zkapp_account_precondition.t ) =
query_db ~f:(fun db ->
Processor.Zkapp_account_precondition.load db
zkapp_account_precondition_id )
in
match kind with
| Nonce ->
assert (Option.is_some nonce) ;
let nonce =
Option.value_exn nonce |> Unsigned.UInt32.of_int64
|> Mina_numbers.Account_nonce.of_uint32
in
return @@ Account_update.Account_precondition.Nonce nonce
| Accept ->
return Account_update.Account_precondition.Accept
| Full ->
assert (Option.is_some account_precondition_values_id) ;
let%bind { balance_id
; nonce_id
; receipt_chain_hash
; delegate_id
; state_id
; action_state_id
; proved_state
; is_new
} =
query_db ~f:(fun db ->
Processor.Zkapp_account_precondition_values.load db
(Option.value_exn account_precondition_values_id) )
in
let%bind balance =
let%map balance_opt =
Option.value_map balance_id ~default:(return None) ~f:(fun id ->
let%map { balance_lower_bound; balance_upper_bound } =
query_db ~f:(fun db ->
Processor.Zkapp_balance_bounds.load db id )
in
let lower = Currency.Balance.of_string balance_lower_bound in
let upper = Currency.Balance.of_string balance_upper_bound in
Some ({ lower; upper } : _ Zkapp_precondition.Closed_interval.t) )
in
Or_ignore.of_option balance_opt
in
let%bind nonce =
let%map nonce_opt =
Option.value_map nonce_id ~default:(return None) ~f:(fun id ->
let%map { nonce_lower_bound; nonce_upper_bound } =
query_db ~f:(fun db ->
Processor.Zkapp_nonce_bounds.load db id )
in
let balance_of_int64 int64 =
int64 |> Unsigned.UInt32.of_int64
|> Mina_numbers.Account_nonce.of_uint32
in
let lower = balance_of_int64 nonce_lower_bound in
let upper = balance_of_int64 nonce_upper_bound in
Some ({ lower; upper } : _ Zkapp_precondition.Closed_interval.t) )
in
Or_ignore.of_option nonce_opt
in
let receipt_chain_hash =
Option.map receipt_chain_hash
~f:Receipt.Chain_hash.of_base58_check_exn
|> Or_ignore.of_option
in
let get_pk id =
let%map pk_opt =
Option.value_map id ~default:(return None) ~f:(fun id ->
let%map pk = pk_of_id id in
Some pk )
in
Or_ignore.of_option pk_opt
in
let%bind delegate = get_pk delegate_id in
let%bind state =
let%bind { element0
; element1
; element2
; element3
; element4
; element5
; element6
; element7
} =
query_db ~f:(fun db ->
Processor.Zkapp_states_nullable.load db state_id )
in
let elements =
[ element0
; element1
; element2
; element3
; element4
; element5
; element6
; element7
]
in
let%map fields =
Deferred.List.map elements ~f:(fun id_opt ->
Option.value_map id_opt ~default:(return None) ~f:(fun id ->
let%map field_str =
query_db ~f:(fun db -> Processor.Zkapp_field.load db id)
in
Some (Zkapp_basic.F.of_string field_str) ) )
in
List.map fields ~f:Or_ignore.of_option |> Zkapp_state.V.of_list_exn
in
let%bind action_state =
let%map action_state_opt =
Option.value_map action_state_id ~default:(return None)
~f:(fun id ->
let%bind balance =
let%map balance_opt =
Option.value_map balance_id ~default:(return None) ~f:(fun id ->
let%map { balance_lower_bound; balance_upper_bound } =
query_db ~f:(fun db -> Processor.Zkapp_balance_bounds.load db id)
in
let lower = Currency.Balance.of_string balance_lower_bound in
let upper = Currency.Balance.of_string balance_upper_bound in
Some ({ lower; upper } : _ Zkapp_precondition.Closed_interval.t) )
in
Or_ignore.of_option balance_opt
in
let%bind nonce =
let%map nonce_opt =
Option.value_map nonce_id ~default:(return None) ~f:(fun id ->
let%map { nonce_lower_bound; nonce_upper_bound } =
query_db ~f:(fun db -> Processor.Zkapp_nonce_bounds.load db id)
in
let balance_of_int64 int64 =
int64 |> Unsigned.UInt32.of_int64
|> Mina_numbers.Account_nonce.of_uint32
in
let lower = balance_of_int64 nonce_lower_bound in
let upper = balance_of_int64 nonce_upper_bound in
Some ({ lower; upper } : _ Zkapp_precondition.Closed_interval.t) )
in
Or_ignore.of_option nonce_opt
in
let receipt_chain_hash =
Option.map receipt_chain_hash ~f:Receipt.Chain_hash.of_base58_check_exn
|> Or_ignore.of_option
in
let get_pk id =
let%map pk_opt =
Option.value_map id ~default:(return None) ~f:(fun id ->
let%map pk = pk_of_id id in
Some pk )
in
Or_ignore.of_option pk_opt
in
let%bind delegate = get_pk delegate_id in
let%bind state =
let%bind { element0
; element1
; element2
; element3
; element4
; element5
; element6
; element7
} =
query_db ~f:(fun db ->
Processor.Zkapp_states_nullable.load db state_id )
in
let elements =
[ element0
; element1
; element2
; element3
; element4
; element5
; element6
; element7
]
in
let%map fields =
Deferred.List.map elements ~f:(fun id_opt ->
Option.value_map id_opt ~default:(return None) ~f:(fun id ->
let%map field_str =
query_db ~f:(fun db -> Processor.Zkapp_field.load db id)
in
Some (Zkapp_basic.F.of_string field_str) )
in
Or_ignore.of_option action_state_opt
in
let proved_state = Or_ignore.of_option proved_state in
let is_new = Or_ignore.of_option is_new in
return
(Account_update.Account_precondition.Full
{ balance
; nonce
; receipt_chain_hash
; delegate
; state
; action_state
; proved_state
; is_new
} )
Some (Zkapp_basic.F.of_string field_str) ) )
in
List.map fields ~f:Or_ignore.of_option |> Zkapp_state.V.of_list_exn
in
let%bind action_state =
let%map action_state_opt =
Option.value_map action_state_id ~default:(return None) ~f:(fun id ->
let%map field_str =
query_db ~f:(fun db -> Processor.Zkapp_field.load db id)
in
Some (Zkapp_basic.F.of_string field_str) )
in
Or_ignore.of_option action_state_opt
in
let proved_state = Or_ignore.of_option proved_state in
let is_new = Or_ignore.of_option is_new in
return
( { balance
; nonce
; receipt_chain_hash
; delegate
; state
; action_state
; proved_state
; is_new
}
: Zkapp_precondition.Account.t )
in
let%bind valid_while_precondition =
get_global_slot_bounds pool zkapp_valid_while_precondition_id
Expand Down
73 changes: 2 additions & 71 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ module Zkapp_nonce_bounds = struct
id
end

module Zkapp_account_precondition_values = struct
module Zkapp_account_precondition = struct
type t =
{ balance_id : int option
; nonce_id : int option
Expand All @@ -882,7 +882,7 @@ module Zkapp_account_precondition_values = struct
; option bool
]

let table_name = "zkapp_account_precondition_values"
let table_name = "zkapp_account_precondition"

let add_if_doesn't_exist (module Conn : CONNECTION)
(acct : Zkapp_precondition.Account.t) =
Expand Down Expand Up @@ -940,75 +940,6 @@ module Zkapp_account_precondition_values = struct
id
end

module Zkapp_account_precondition = struct
type t =
{ kind : Account_update.Account_precondition.Tag.t
; account_precondition_values_id : int option
; nonce : int64 option
}
[@@deriving fields, hlist]

let zkapp_account_precondition_kind_typ =
let encode = function
| Account_update.Account_precondition.Tag.Full ->
"full"
| Nonce ->
"nonce"
| Accept ->
"accept"
in
let decode = function
| "full" ->
Result.return Account_update.Account_precondition.Tag.Full
| "nonce" ->
Result.return Account_update.Account_precondition.Tag.Nonce
| "accept" ->
Result.return Account_update.Account_precondition.Tag.Accept
| _ ->
Result.failf "Failed to decode zkapp_account_precondition_kind_typ"
in
Caqti_type.enum "zkapp_precondition_type" ~encode ~decode

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

let table_name = "zkapp_account_precondition"

let add_if_doesn't_exist (module Conn : CONNECTION)
(account_precondition : Account_update.Account_precondition.t) =
let open Deferred.Result.Let_syntax in
let%bind account_precondition_values_id =
match account_precondition with
| Account_update.Account_precondition.Full acct ->
Zkapp_account_precondition_values.add_if_doesn't_exist
(module Conn)
acct
>>| Option.some
| _ ->
return None
in
let kind = Account_update.Account_precondition.tag account_precondition in
let nonce =
match account_precondition with
| Account_update.Account_precondition.Nonce nonce ->
Option.some @@ Unsigned.UInt32.to_int64 nonce
| _ ->
None
in
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
~table_name ~cols:(Fields.names, typ)
(module Conn)
{ kind; account_precondition_values_id; nonce }

let load (module Conn : CONNECTION) id =
Conn.find
(Caqti_request.find Caqti_type.int typ
(Mina_caqti.select_cols_from_id ~table_name ~cols:Fields.names) )
id
end

module Zkapp_token_id_bounds = struct
type t = { token_id_lower_bound : string; token_id_upper_bound : string }
[@@deriving fields, hlist]
Expand Down
Loading

0 comments on commit 8b3eb4c

Please sign in to comment.