Skip to content

Commit

Permalink
Decouple Staged_ledger_diff.{Stable.Latest.t, t}
Browse files Browse the repository at this point in the history
Remove `Stable.Latest.t = t` from interfaces. This is a preparation step
for removing the equality in the implementation as well.
  • Loading branch information
georgeee committed Feb 6, 2025
1 parent dc75457 commit 174ee4c
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3305,8 +3305,11 @@ module Block = struct
~hash ~v1_transaction_hash:false

let add_from_precomputed conn ~constraint_constants (t : Precomputed.t) =
let staged_ledger_diff =
Staged_ledger_diff.write_all_proofs_to_disk t.staged_ledger_diff
in
add_parts_if_doesn't_exist conn ~constraint_constants
~protocol_state:t.protocol_state ~staged_ledger_diff:t.staged_ledger_diff
~protocol_state:t.protocol_state ~staged_ledger_diff
~protocol_version:t.protocol_version
~proposed_protocol_version:t.proposed_protocol_version
~hash:(Protocol_state.hashes t.protocol_state).state_hash
Expand Down
17 changes: 14 additions & 3 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ let generate_next_state ~proof_cache_db ~commit_id ~zkapp_cmd_limit
(Staged_ledger.Staged_ledger_error.to_string e) )
; ( "diff"
, Staged_ledger_diff.Stable.Latest.to_yojson
@@ Staged_ledger_diff.read_all_proofs_from_disk
@@ Staged_ledger_diff.forget diff )
]
"Error applying the diff $diff: $error"
Expand All @@ -338,6 +339,10 @@ let generate_next_state ~proof_cache_db ~commit_id ~zkapp_cmd_limit
, ledger_proof_opt
, is_new_stack
, pending_coinbase_update ) ->
let diff_unwrapped =
Staged_ledger_diff.read_all_proofs_from_disk
@@ Staged_ledger_diff.forget diff
in
let%bind protocol_state, consensus_transition_data =
lift_sync (fun () ->
let previous_ledger_hash =
Expand Down Expand Up @@ -368,7 +373,7 @@ let generate_next_state ~proof_cache_db ~commit_id ~zkapp_cmd_limit
let body_reference =
Staged_ledger_diff.Body.compute_reference
~tag:Mina_net2.Bitswap_tag.(to_enum Body)
(Body.create @@ Staged_ledger_diff.forget diff)
(Body.Stable.Latest.create diff_unwrapped)
in
let blockchain_state =
(* We use the time of the beginning of the slot because if things
Expand Down Expand Up @@ -916,7 +921,10 @@ let produce ~genesis_breadcrumb ~context:(module Context : CONTEXT) ~prover
~transition_receipt_time () )
|> Deferred.Result.map_error ~f:(function
| `Invalid_staged_ledger_diff e ->
`Invalid_staged_ledger_diff (e, staged_ledger_diff)
`Invalid_staged_ledger_diff
( e
, Staged_ledger_diff.read_all_proofs_from_disk
staged_ledger_diff )
| ( `Fatal_error _
| `Invalid_genesis_protocol_state
| `Invalid_staged_ledger_hash _
Expand Down Expand Up @@ -1438,7 +1446,10 @@ let run_precomputed ~context:(module Context : CONTEXT) ~verifier ~trust_system
Header.create ~protocol_state ~protocol_state_proof
~delta_block_chain_proof ()
in
let body = Body.create staged_ledger_diff in
let body =
Body.create
(Staged_ledger_diff.write_all_proofs_to_disk staged_ledger_diff)
in
let%bind transition =
let open Result.Let_syntax in
Validation.wrap_header
Expand Down
11 changes: 7 additions & 4 deletions src/lib/mina_block/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ module Stable = struct
end
end]

type t = Stable.Latest.t =
{ header : Header.t; body : Staged_ledger_diff.Body.t }
type t = { header : Header.t; body : Staged_ledger_diff.Body.t }
[@@deriving fields]

type with_hash = t State_hash.With_state_hashes.t
Expand Down Expand Up @@ -105,6 +104,10 @@ let account_ids_accessed ~constraint_constants t =
|> List.dedup_and_sort
~compare:[%compare: Account_id.t * [ `Accessed | `Not_accessed ]]

let write_all_proofs_to_disk = Fn.id
let write_all_proofs_to_disk { Stable.Latest.header; body } =
{ header; body = Staged_ledger_diff.Body.write_all_proofs_to_disk body }

let read_all_proofs_from_disk = Fn.id
let read_all_proofs_from_disk { header; body } =
{ Stable.Latest.header
; body = Staged_ledger_diff.Body.read_all_proofs_from_disk body
}
8 changes: 2 additions & 6 deletions src/lib/mina_block/internal_transition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Stable = struct
end]

(* bin_io, version omitted *)
type t = Stable.Latest.t =
type t =
{ snark_transition : Snark_transition.Value.t
; ledger_proof : Ledger_proof.t option
; prover_state : Consensus.Data.Prover_state.t
Expand All @@ -55,8 +55,4 @@ type t = Stable.Latest.t =
[@@deriving fields]

let create ~snark_transition ~ledger_proof ~prover_state ~staged_ledger_diff =
{ Stable.Latest.snark_transition
; ledger_proof
; staged_ledger_diff
; prover_state
}
{ snark_transition; ledger_proof; staged_ledger_diff; prover_state }
1 change: 1 addition & 0 deletions src/lib/mina_block/precomputed_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ let of_block ~logger
; protocol_state_proof = Header.protocol_state_proof header
; staged_ledger_diff =
Staged_ledger_diff.Body.staged_ledger_diff (Block.body block)
|> Staged_ledger_diff.read_all_proofs_from_disk
; delta_transition_chain_proof = Header.delta_block_chain_proof header
; protocol_version
; proposed_protocol_version
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_block/precomputed_block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type t = Stable.Latest.t =
{ scheduled_time : Block_time.Time.t
; protocol_state : Protocol_state.value
; protocol_state_proof : Proof.t
; staged_ledger_diff : Staged_ledger_diff.t
; staged_ledger_diff : Staged_ledger_diff.Stable.Latest.t
; delta_transition_chain_proof :
Frozen_ledger_hash.t * Frozen_ledger_hash.t list
; protocol_version : Protocol_version.t
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_block/validation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ let validate_staged_ledger_diff ?skip_staged_ledger_verification ~proof_cache_db
let body_ref_computed =
Staged_ledger_diff.Body.compute_reference
~tag:Mina_net2.Bitswap_tag.(to_enum Body)
body
@@ Staged_ledger_diff.Body.read_all_proofs_from_disk body
in
let%bind.Deferred.Result () =
if Blake2.equal body_ref_computed body_ref_from_header then
Expand Down
5 changes: 4 additions & 1 deletion src/lib/mina_lmdb_storage/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ let%test_module "Block storage tests" =
f conf_dir helper )

let send_and_receive ~helper ~reader ~db breadcrumb =
let body = Breadcrumb.block breadcrumb |> Mina_block.body in
let body =
Breadcrumb.block breadcrumb
|> Mina_block.body |> Mina_block.Body.read_all_proofs_from_disk
in
let body_ref =
Staged_ledger_diff.Body.compute_reference
~tag:Mina_net2.Bitswap_tag.(to_enum Body)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_lmdb_storage/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ val get_status :
val read_body :
t
-> Consensus.Body_reference.t
-> ( Mina_block.Body.t
-> ( Mina_block.Body.Stable.Latest.t
, [> `Invalid_structure of Error.t | `Non_full | `Tx_failed ] )
Result.t
7 changes: 3 additions & 4 deletions src/lib/staged_ledger/staged_ledger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3771,7 +3771,8 @@ let%test_module "staged ledger tests" =
assert_same_fee single work.fee )
| _ ->
failwith @@ "Incorrect coinbase in the diff "
^ ( Staged_ledger_diff.Stable.Latest.to_yojson diff
^ ( Staged_ledger_diff.read_all_proofs_from_disk diff
|> Staged_ledger_diff.Stable.Latest.to_yojson
|> Yojson.Safe.to_string )
in
(diff, List.tl_exn proofs_available_left) )
Expand Down Expand Up @@ -4725,9 +4726,7 @@ let%test_module "staged ledger tests" =
*)
let diff : Staged_ledger_diff.t =
let pre_diff :
Staged_ledger_diff.Pre_diff_with_at_most_two_coinbase.Stable
.V2
.t =
Staged_ledger_diff.Pre_diff_with_at_most_two_coinbase.t =
{ completed_works = []
; commands = cmds
; coinbase = Zero
Expand Down
14 changes: 12 additions & 2 deletions src/lib/staged_ledger_diff/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ module Make_str (A : Wire_types.Concrete) = struct
end
end]

type t = Stable.Latest.t
type t = { staged_ledger_diff : Diff.t } [@@deriving fields]

[%%define_locally Stable.Latest.(create, staged_ledger_diff)]
let create staged_ledger_diff = { staged_ledger_diff }

let to_binio_bigstring b =
let sz = Stable.V1.bin_size_t b in
Expand All @@ -60,6 +60,16 @@ module Make_str (A : Wire_types.Concrete) = struct
@@ Fn.compose
(Bitswap_block.blocks_of_data ~max_block_size:262144)
(serialize_with_len_and_tag ~tag)

let write_all_proofs_to_disk t =
{ staged_ledger_diff =
Diff.write_all_proofs_to_disk t.Stable.Latest.staged_ledger_diff
}

let read_all_proofs_from_disk t =
{ Stable.Latest.staged_ledger_diff =
Diff.read_all_proofs_from_disk t.staged_ledger_diff
}
end

include Wire_types.Make (Make_sig) (Make_str)
14 changes: 11 additions & 3 deletions src/lib/staged_ledger_diff/body_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ module type Full = sig

module V1 : sig
type t [@@deriving equal, sexp]

val create : Diff.Stable.V2.t -> t

val staged_ledger_diff : t -> Diff.Stable.V2.t
end
end]

type t = Stable.Latest.t
type t

val create : Diff.t -> t

val staged_ledger_diff : t -> Diff.t

val to_binio_bigstring : t -> Core_kernel.Bigstring.t
val to_binio_bigstring : Stable.V1.t -> Core_kernel.Bigstring.t

val compute_reference : tag:int -> Stable.V1.t -> Consensus.Body_reference.t

val write_all_proofs_to_disk : Stable.Latest.t -> t

val compute_reference : tag:int -> t -> Consensus.Body_reference.t
val read_all_proofs_from_disk : t -> Stable.Latest.t
end
56 changes: 51 additions & 5 deletions src/lib/staged_ledger_diff/diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Make_sig (A : Wire_types.Types.S) = struct
and type ('a, 'b) Pre_diff_one.Stable.V2.t = ('a, 'b) A.Pre_diff_one.V2.t
and type Pre_diff_with_at_most_one_coinbase.Stable.V2.t =
A.Pre_diff_with_at_most_one_coinbase.V2.t
and type t = A.V2.t
and type Diff.Stable.V2.t = A.Diff.V2.t
and type Stable.V2.t = A.V2.t
end

Expand Down Expand Up @@ -175,7 +175,12 @@ module Make_str (A : Wire_types.Concrete) = struct
end
end]

type t = Stable.Latest.t
type t =
(Transaction_snark_work.t, User_command.t With_status.t) Pre_diff_two.t

let write_all_proofs_to_disk : Stable.Latest.t -> t = Fn.id

let read_all_proofs_from_disk : t -> Stable.Latest.t = Fn.id
end

module Pre_diff_with_at_most_one_coinbase = struct
Expand All @@ -194,7 +199,12 @@ module Make_str (A : Wire_types.Concrete) = struct
end
end]

type t = Stable.Latest.t
type t =
(Transaction_snark_work.t, User_command.t With_status.t) Pre_diff_one.t

let write_all_proofs_to_disk : Stable.Latest.t -> t = Fn.id

let read_all_proofs_from_disk : t -> Stable.Latest.t = Fn.id
end

module Diff = struct
Expand All @@ -212,7 +222,27 @@ module Make_str (A : Wire_types.Concrete) = struct
end
end]

type t = Stable.Latest.t
type t =
Pre_diff_with_at_most_two_coinbase.t
* Pre_diff_with_at_most_one_coinbase.t option

let write_all_proofs_to_disk
(( pre_diff_with_at_most_two_coinbase
, pre_diff_with_at_most_one_coinbase_opt ) :
Stable.Latest.t ) : t =
( Pre_diff_with_at_most_two_coinbase.write_all_proofs_to_disk
pre_diff_with_at_most_two_coinbase
, Option.map pre_diff_with_at_most_one_coinbase_opt
~f:Pre_diff_with_at_most_one_coinbase.write_all_proofs_to_disk )

let read_all_proofs_from_disk
(( pre_diff_with_at_most_two_coinbase
, pre_diff_with_at_most_one_coinbase_opt ) :
t ) : Stable.Latest.t =
( Pre_diff_with_at_most_two_coinbase.read_all_proofs_from_disk
pre_diff_with_at_most_two_coinbase
, Option.map pre_diff_with_at_most_one_coinbase_opt
~f:Pre_diff_with_at_most_one_coinbase.read_all_proofs_from_disk )
end

[%%versioned
Expand All @@ -224,10 +254,26 @@ module Make_str (A : Wire_types.Concrete) = struct
[@@deriving equal, sexp, yojson]

let to_latest = Fn.id

let empty_diff : t =
{ diff =
( { completed_works = []
; commands = []
; coinbase = At_most_two.Zero
; internal_command_statuses = []
}
, None )
}
end
end]

type t = Stable.Latest.t = { diff : Diff.t } [@@deriving fields]
type t = { diff : Diff.t } [@@deriving fields]

let write_all_proofs_to_disk t =
{ diff = Diff.write_all_proofs_to_disk t.Stable.Latest.diff }

let read_all_proofs_from_disk t =
{ Stable.Latest.diff = Diff.read_all_proofs_from_disk t.diff }

module With_valid_signatures_and_proofs = struct
type pre_diff_with_at_most_two_coinbase =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/staged_ledger_diff/diff.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ include
('a, 'b) Mina_wire_types.Staged_ledger_diff.Pre_diff_one.V2.t
and type Pre_diff_with_at_most_one_coinbase.Stable.V2.t =
Mina_wire_types.Staged_ledger_diff.Pre_diff_with_at_most_one_coinbase.V2.t
and type t = Mina_wire_types.Staged_ledger_diff.V2.t
and type Diff.Stable.V2.t = Mina_wire_types.Staged_ledger_diff.Diff.V2.t
and type Stable.V2.t = Mina_wire_types.Staged_ledger_diff.V2.t
25 changes: 19 additions & 6 deletions src/lib/staged_ledger_diff/diff_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ module type Full = sig
module Stable : sig
module V2 : sig
type t

val to_latest : t -> t
end

module Latest = V2
end
with type V2.t = t
end

module Pre_diff_with_at_most_one_coinbase : sig
Expand All @@ -83,9 +86,12 @@ module type Full = sig
module Stable : sig
module V2 : sig
type t

val to_latest : t -> t
end

module Latest = V2
end
with type V2.t = t
end

module Diff : sig
Expand All @@ -96,24 +102,31 @@ module type Full = sig
module Stable : sig
module V2 : sig
type t

val to_latest : t -> t
end

module Latest = V2
end
with type V2.t = t
end

type t = { diff : Diff.t } [@@deriving fields]

module Stable : sig
module V2 : sig
type t = { diff : Diff.Stable.V2.t }
[@@deriving bin_io, equal, sexp, version, yojson]
type t [@@deriving bin_io, equal, sexp, version, yojson]

val to_latest : t -> t

val empty_diff : t
end

module Latest = V2
end
with type V2.t = t

val write_all_proofs_to_disk : Stable.Latest.t -> t

val read_all_proofs_from_disk : t -> Stable.Latest.t

module With_valid_signatures_and_proofs : sig
type pre_diff_with_at_most_two_coinbase =
Expand Down
Loading

0 comments on commit 174ee4c

Please sign in to comment.