Skip to content

Commit

Permalink
Decouple Mina_block.{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 c013d0d commit 057ad38
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/app/archive/lib/diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ module Builder = struct
(Time.diff account_created_time accounts_accessed_time) ) )
] ;
Transition_frontier.Breadcrumb_added
{ block = block_with_hash
{ block =
With_hash.map ~f:Mina_block.read_all_proofs_from_disk block_with_hash
; accounts_accessed
; accounts_created
; tokens_used
Expand Down
3 changes: 3 additions & 0 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4708,6 +4708,9 @@ let run pool reader ~genesis_constants ~constraint_constants ~logger
{ block; accounts_accessed; accounts_created; tokens_used; _ } ) -> (
let add_block = Block.add_if_doesn't_exist ~constraint_constants in
let hash = State_hash.With_state_hashes.state_hash in
let block =
With_hash.map ~f:Mina_block.write_all_proofs_to_disk block
in
match%bind
add_block_aux ~logger ~genesis_constants ~pool ~delete_older_than
~hash ~add_block ~accounts_accessed ~accounts_created ~tokens_used
Expand Down
3 changes: 2 additions & 1 deletion src/app/cli/src/init/test_ledger_application.ml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ let test ~privkey_path ~ledger_path ?prev_block_path ~first_partition_slots
let prev_block =
Binable.of_string (module Mina_block.Stable.Latest) prev_block_data
in
Mina_block.header prev_block |> Mina_block.Header.protocol_state
Mina_block.Stable.Latest.header prev_block
|> Mina_block.Header.protocol_state
in
let consensus_constants =
Consensus.Constants.create ~constraint_constants
Expand Down
2 changes: 1 addition & 1 deletion src/app/delegation_verify/delegation_verify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Make_verifier (Source : Submission.Data_source) = struct
verify_snark_work ~verify_transaction_snarks ~proof ~message
else return ()
in
let header = Mina_block.header block in
let header = Mina_block.Stable.Latest.header block in
let protocol_state = Mina_block.Header.protocol_state header in
let consensus_state =
Mina_state.Protocol_state.consensus_state protocol_state
Expand Down
4 changes: 2 additions & 2 deletions src/app/delegation_verify/known_blocks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end

module Deferred_block = struct
type t =
{ block : Mina_block.t Deferred.Or_error.t
{ block : Mina_block.Stable.Latest.t Deferred.Or_error.t
; valid : unit Deferred.Or_error.t (* Raises if block is invalid. *)
}

Expand All @@ -26,7 +26,7 @@ module Deferred_block = struct
Deferred.create compute

let verify_block ~verify_blockchain_snarks block =
let header = Mina_block.header block in
let header = Mina_block.Stable.Latest.header block in
let open Mina_block.Header in
verify_blockchain_snarks
[ (protocol_state header, protocol_state_proof header) ]
Expand Down
16 changes: 9 additions & 7 deletions src/app/dump_blocks/encoding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Core
type t = Sexp | Json | Binary

type 'a content =
| Block : Mina_block.t content
| Block : Mina_block.Stable.Latest.t content
| Precomputed : Mina_block.Precomputed.t content

let append_newline s = s ^ "\n"
Expand All @@ -13,7 +13,7 @@ let block_of_breadcrumb ?with_parent_statehash breadcrumb =
let block = Frontier_base.Breadcrumb.block breadcrumb in
match with_parent_statehash with
| None ->
block
block |> Mina_block.read_all_proofs_from_disk
| Some hash ->
let previous_state_hash = Mina_base.State_hash.of_base58_check_exn hash in
let h = header block in
Expand All @@ -29,6 +29,7 @@ let block_of_breadcrumb ?with_parent_statehash breadcrumb =
()
in
Mina_block.create ~header ~body:(body block)
|> Mina_block.read_all_proofs_from_disk

module type S = sig
type t
Expand All @@ -43,8 +44,8 @@ module type S = sig
val of_string : string -> t
end

module Sexp_block : S with type t = Mina_block.t = struct
type t = Mina_block.t
module Sexp_block : S with type t = Mina_block.Stable.Latest.t = struct
type t = Mina_block.Stable.Latest.t

let name = "sexp"

Expand All @@ -56,8 +57,8 @@ module Sexp_block : S with type t = Mina_block.t = struct
let of_string s = Sexp.of_string s |> Mina_block.Stable.Latest.t_of_sexp
end

module Binary_block : S with type t = Mina_block.t = struct
type t = Mina_block.t
module Binary_block : S with type t = Mina_block.Stable.Latest.t = struct
type t = Mina_block.Stable.Latest.t

let name = "binary"

Expand All @@ -84,7 +85,8 @@ let precomputed_of_breadcrumb ?with_parent_statehash breadcrumb =
let block = block_of_breadcrumb ?with_parent_statehash breadcrumb in
let staged_ledger = Transition_frontier.Breadcrumb.staged_ledger breadcrumb in
let scheduled_time =
Mina_block.(Header.protocol_state @@ header block)
Mina_block.Stable.Latest.header block
|> Mina_block.Header.protocol_state
|> Mina_state.Protocol_state.blockchain_state
|> Mina_state.Blockchain_state.timestamp
in
Expand Down
16 changes: 10 additions & 6 deletions src/lib/best_tip_prover/wrap_for_block.ml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
let map ~f
let to_header_data ~to_header
{ Proof_carrying_data.proof = merkle_list, root_unverified
; data = best_tip_unverified
} =
{ Proof_carrying_data.proof = (merkle_list, to_header root_unverified)
; data = to_header best_tip_unverified
}

let map ~f
( { Proof_carrying_data.proof = _, root_unverified
; data = best_tip_unverified
} as pcd ) =
let%map.Async_kernel.Deferred.Or_error ( `Root root_header
, `Best_tip best_tip_header ) =
f
{ Proof_carrying_data.proof =
(merkle_list, Mina_block.header root_unverified)
; data = Mina_block.header best_tip_unverified
}
f (to_header_data ~to_header:Mina_block.header pcd)
in
let root =
Mina_block.Validation.with_body root_header
Expand Down
8 changes: 7 additions & 1 deletion src/lib/bootstrap_controller/bootstrap_controller.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,19 @@ let on_transition ({ context = (module Context); _ } as t) ~sender
$error" ;
Deferred.return `Ignored
| Ok peer_root_with_proof -> (
let pcd =
peer_root_with_proof.data
|> Proof_carrying_data.map ~f:Mina_block.write_all_proofs_to_disk
|> Proof_carrying_data.map_proof
~f:(Tuple2.map_snd ~f:Mina_block.write_all_proofs_to_disk)
in
match%bind
Best_tip_prover.Wrap_for_block.map
~f:
(Sync_handler.Root.verify
~context:(module Context)
~verifier:t.verifier candidate_consensus_state )
peer_root_with_proof.data
pcd
with
| Ok (`Root root, `Best_tip best_tip) ->
if done_syncing_root root_sync_ledger then return `Ignored
Expand Down
10 changes: 8 additions & 2 deletions src/lib/gossip_net/libp2p.ml
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,12 @@ module Make (Rpc_interface : RPC_INTERFACE) :
~fn:(fun (env, vc) ->
match Envelope.Incoming.data env with
| Message.Latest.T.New_state state ->
let state_cached =
Mina_block.write_all_proofs_to_disk state
in
Sinks.Block_sink.push sink_block
( `Block (Envelope.Incoming.map ~f:(const state) env)
( `Block
(Envelope.Incoming.map ~f:(const state_cached) env)
, `Time_received (Block_time.now config.time_controller)
, `Valid_cb vc )
| Message.Latest.T.Transaction_pool_diff
Expand Down Expand Up @@ -951,7 +955,9 @@ module Make (Rpc_interface : RPC_INTERFACE) :
guard_topic ?origin_topic v1_topic_block pfs.publish_v1_block
(Mina_block.header state)
in
guard_topic ?origin_topic v0_topic pfs.publish_v0 (Message.New_state state)
let state_unwrapped = Mina_block.read_all_proofs_from_disk state in
guard_topic ?origin_topic v0_topic pfs.publish_v0
(Message.New_state state_unwrapped)

let broadcast_transaction_pool_diff ?origin_topic ?(nonce = 0) t diff =
let pfs = !(t.publish_functions) in
Expand Down
3 changes: 1 addition & 2 deletions src/lib/gossip_net/message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ open Network_peer
module Master = struct
module T = struct
type msg =
| New_state of Mina_block.t
| New_state of Mina_block.Stable.Latest.t
| Snark_pool_diff of
Snark_pool.Resource_pool.Diff.t Network_pool.With_nonce.t
| Transaction_pool_diff of
Transaction_pool.Resource_pool.Diff.t Network_pool.With_nonce.t
[@@deriving to_yojson]

type snark_pool_diff_msg = Snark_pool.Resource_pool.Diff.t

Expand Down
8 changes: 7 additions & 1 deletion src/lib/ledger_catchup/normal_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ let download_transitions ~target_hash ~logger ~trust_system ~network
~hash_data:
(Fn.compose Mina_state.Protocol_state.hashes
(Fn.compose Mina_block.Header.protocol_state
Mina_block.header ) ) )
Mina_block.Stable.Latest.header ) ) )
in
if not @@ verify_against_hashes hashed_transitions hashes then (
let error_msg =
Expand Down Expand Up @@ -822,6 +822,12 @@ let run ~context:(module Context : CONTEXT) ~trust_system ~verifier ~network
else
download_transitions ~logger ~trust_system ~network
~preferred_peer ~hashes_of_missing_transitions ~target_hash
>>| List.map
~f:
(Envelope.Incoming.map
~f:
(With_hash.map
~f:Mina_block.write_all_proofs_to_disk ) )
in
[%log debug]
~metadata:[ ("target_hash", State_hash.to_yojson target_hash) ]
Expand Down
4 changes: 3 additions & 1 deletion src/lib/ledger_catchup/super_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,9 @@ let run_catchup ~context:(module Context : CONTEXT) ~trust_system ~verifier
in
Mina_networking.get_transition_chain
~heartbeat_timeout:(Time_ns.Span.of_sec sec)
~timeout:(Time.Span.of_sec sec) network peer (List.map hs ~f:fst) )
~timeout:(Time.Span.of_sec sec) network peer (List.map hs ~f:fst)
|> Deferred.Or_error.map
~f:(List.map ~f:Mina_block.write_all_proofs_to_disk) )
~peers:(fun () -> Mina_networking.peers network)
~knowledge_context:
(Broadcast_pipe.map best_tip_r
Expand Down
19 changes: 15 additions & 4 deletions src/lib/mina_block/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ open Mina_state

[%%versioned
module Stable = struct
[@@@no_toplevel_latest_type]

module V2 = struct
type t =
{ header : Header.Stable.V2.t
Expand All @@ -18,7 +20,8 @@ module Stable = struct

type nonrec t = t

type 'a creator = header:Header.t -> body:Staged_ledger_diff.Body.t -> 'a
type 'a creator =
header:Header.t -> body:Staged_ledger_diff.Body.Stable.Latest.t -> 'a

let map_creator c ~f ~header ~body = f (c ~header ~body)

Expand All @@ -40,6 +43,10 @@ module Stable = struct
end
end]

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

type with_hash = t State_hash.With_state_hashes.t

let to_yojson t =
Expand All @@ -60,7 +67,7 @@ let to_yojson t =
~default:"<None>" ~f:Protocol_version.to_string ) )
]

[%%define_locally Stable.Latest.(create, header, body)]
let create ~header ~body = { header; body }

let wrap_with_hash block =
With_hash.of_data block
Expand All @@ -74,10 +81,10 @@ let timestamp block =

let transactions ~constraint_constants block =
let consensus_state =
block |> header |> Header.protocol_state |> Protocol_state.consensus_state
Header.protocol_state block.header |> Protocol_state.consensus_state
in
let staged_ledger_diff =
block |> body |> Staged_ledger_diff.Body.staged_ledger_diff
Staged_ledger_diff.Body.staged_ledger_diff block.body
in
let coinbase_receiver =
Consensus.Data.Consensus_state.coinbase_receiver consensus_state
Expand All @@ -97,3 +104,7 @@ let account_ids_accessed ~constraint_constants t =
|> List.concat
|> List.dedup_and_sort
~compare:[%compare: Account_id.t * [ `Accessed | `Not_accessed ]]

let write_all_proofs_to_disk = Fn.id

let read_all_proofs_from_disk = Fn.id
12 changes: 10 additions & 2 deletions src/lib/mina_block/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ module Stable : sig
[@@@no_toplevel_latest_type]

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

val header : t -> Header.Stable.V2.t

val body : t -> Staged_ledger_diff.Body.Stable.V1.t
end
end]

type t = Stable.Latest.t [@@deriving to_yojson]
type t [@@deriving to_yojson]

type with_hash = t State_hash.With_state_hashes.t

Expand Down Expand Up @@ -39,3 +43,7 @@ val account_ids_accessed :
constraint_constants:Genesis_constants.Constraint_constants.t
-> t
-> (Account_id.t * [ `Accessed | `Not_accessed ]) list

val write_all_proofs_to_disk : Stable.Latest.t -> t

val read_all_proofs_from_disk : t -> Stable.Latest.t
3 changes: 2 additions & 1 deletion src/lib/mina_block/validated_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ let is_genesis t =
header t |> Header.protocol_state |> Mina_state.Protocol_state.consensus_state
|> Consensus.Data.Consensus_state.is_genesis_state

let read_all_proofs_from_disk = Fn.id
let read_all_proofs_from_disk ((b, v) : t) : Stable.Latest.t =
(With_hash.map ~f:Block.read_all_proofs_from_disk b, v)
20 changes: 12 additions & 8 deletions src/lib/mina_networking/mina_networking.mli
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Rpcs : sig
module Get_transition_chain : sig
type query = State_hash.t list

type response = Mina_block.t list option
type response = Mina_block.Stable.Latest.t list option
end

module Get_transition_chain_proof : sig
Expand All @@ -88,8 +88,8 @@ module Rpcs : sig
(Consensus.Data.Consensus_state.Value.t, State_hash.t) With_hash.t

type response =
( Mina_block.t
, State_body_hash.t list * Mina_block.t )
( Mina_block.Stable.Latest.t
, State_body_hash.t list * Mina_block.Stable.Latest.t )
Proof_carrying_data.t
option
end
Expand All @@ -105,8 +105,8 @@ module Rpcs : sig
type query = unit

type response =
( Mina_block.t
, State_body_hash.t list * Mina_block.t )
( Mina_block.Stable.Latest.t
, State_body_hash.t list * Mina_block.Stable.Latest.t )
Proof_carrying_data.t
option
end
Expand Down Expand Up @@ -189,7 +189,9 @@ val get_ancestry :
t
-> Peer.Id.t
-> (Consensus.Data.Consensus_state.Value.t, State_hash.t) With_hash.t
-> (Mina_block.t, State_body_hash.t list * Mina_block.t) Proof_carrying_data.t
-> ( Mina_block.Stable.Latest.t
, State_body_hash.t list * Mina_block.Stable.Latest.t )
Proof_carrying_data.t
Envelope.Incoming.t
Deferred.Or_error.t

Expand All @@ -198,7 +200,9 @@ val get_best_tip :
-> ?timeout:Time.Span.t
-> t
-> Network_peer.Peer.t
-> (Mina_block.t, State_body_hash.t list * Mina_block.t) Proof_carrying_data.t
-> ( Mina_block.Stable.Latest.t
, State_body_hash.t list * Mina_block.Stable.Latest.t )
Proof_carrying_data.t
Deferred.Or_error.t

val get_transition_chain_proof :
Expand All @@ -215,7 +219,7 @@ val get_transition_chain :
-> t
-> Network_peer.Peer.t
-> State_hash.t list
-> Mina_block.t list Deferred.Or_error.t
-> Mina_block.Stable.Latest.t list Deferred.Or_error.t

val get_staged_ledger_aux_and_pending_coinbases_at_hash :
t
Expand Down
Loading

0 comments on commit 057ad38

Please sign in to comment.