Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New protocol version #14227

Merged
merged 14 commits into from
Oct 2, 2023
6 changes: 3 additions & 3 deletions src/app/archive/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ CREATE TABLE epoch_data

CREATE TABLE protocol_versions
( id serial PRIMARY KEY
, major int NOT NULL
, minor int NOT NULL
, transaction int NOT NULL
, network int NOT NULL
, patch int NOT NULL
, UNIQUE (major,minor,patch)
, UNIQUE (transaction,network)
);

CREATE TYPE chain_status_type AS ENUM ('canonical', 'orphaned', 'pending');
Expand Down
4 changes: 2 additions & 2 deletions src/app/archive/lib/extensional.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ module Block = struct
; user_cmds : User_command.Stable.V2.t list
; internal_cmds : Internal_command.Stable.V2.t list
; zkapp_cmds : Zkapp_command.Stable.V1.t list
; protocol_version : Protocol_version.Stable.V1.t
; proposed_protocol_version : Protocol_version.Stable.V1.t option
; protocol_version : Protocol_version.Stable.V2.t
; proposed_protocol_version : Protocol_version.Stable.V2.t option
; chain_status : Chain_status.Stable.V1.t
; accounts_accessed : (int * Account.Stable.V2.t) list
; accounts_created :
Expand Down
41 changes: 19 additions & 22 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2641,16 +2641,18 @@ module Accounts_created = struct
end

module Protocol_versions = struct
type t = { major : int; minor : int; patch : int } [@@deriving hlist, fields]
type t = { transaction : int; network : int; patch : int }
[@@deriving hlist, fields]

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

let table_name = "protocol_versions"

let add_if_doesn't_exist (module Conn : CONNECTION) ~major ~minor ~patch =
let t = { major; minor; patch } in
let add_if_doesn't_exist (module Conn : CONNECTION) ~transaction ~network
~patch =
let t = { transaction; network; patch } in
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
~table_name ~cols:(Fields.names, typ)
(module Conn)
Expand Down Expand Up @@ -2811,23 +2813,23 @@ module Block = struct
|> Unsigned.UInt32.to_int64
in
let%bind protocol_version_id =
let major = Protocol_version.major protocol_version in
let minor = Protocol_version.minor protocol_version in
let transaction = Protocol_version.transaction protocol_version in
let network = Protocol_version.network protocol_version in
let patch = Protocol_version.patch protocol_version in
Protocol_versions.add_if_doesn't_exist
(module Conn)
~major ~minor ~patch
~transaction ~network ~patch
in
let%bind proposed_protocol_version_id =
Option.value_map proposed_protocol_version ~default:(return None)
~f:(fun ppv ->
let major = Protocol_version.major ppv in
let minor = Protocol_version.minor ppv in
let transaction = Protocol_version.transaction ppv in
let network = Protocol_version.network ppv in
let patch = Protocol_version.patch ppv in
let%map id =
Protocol_versions.add_if_doesn't_exist
(module Conn)
~major ~minor ~patch
~transaction ~network ~patch
in
Some id )
in
Expand Down Expand Up @@ -3188,23 +3190,25 @@ module Block = struct
Epoch_data.add_if_doesn't_exist (module Conn) block.next_epoch_data
in
let%bind protocol_version_id =
let major = Protocol_version.major block.protocol_version in
let minor = Protocol_version.minor block.protocol_version in
let transaction =
Protocol_version.transaction block.protocol_version
in
let network = Protocol_version.network block.protocol_version in
let patch = Protocol_version.patch block.protocol_version in
Protocol_versions.add_if_doesn't_exist
(module Conn)
~major ~minor ~patch
~transaction ~network ~patch
in
let%bind proposed_protocol_version_id =
Option.value_map block.proposed_protocol_version
~default:(return None) ~f:(fun ppv ->
let major = Protocol_version.major ppv in
let minor = Protocol_version.minor ppv in
let transaction = Protocol_version.transaction ppv in
let network = Protocol_version.network ppv in
let patch = Protocol_version.patch ppv in
let%map id =
Protocol_versions.add_if_doesn't_exist
(module Conn)
~major ~minor ~patch
~transaction ~network ~patch
in
Some id )
in
Expand Down Expand Up @@ -3808,13 +3812,6 @@ let add_genesis_accounts ~logger ~(runtime_config_opt : Runtime_config.t option)
"Runtime config does not contain a ledger, could not add genesis \
accounts"
| Some runtime_config_ledger -> (
(* blocks depend on having the protocol version set, which the daemon does on startup;
the actual value doesn't affect the block state hash, which is how we
identify a block in the archive db

here, we just set the protocol version to a dummy value
*)
Protocol_version.(set_current zero) ;
let proof_level = Genesis_constants.Proof_level.compiled in
let%bind precomputed_values =
match%map
Expand Down
46 changes: 20 additions & 26 deletions src/app/cli/src/cli_entrypoint/mina_cli_entrypoint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,34 @@ type mina_initialization =
; itn_graphql_port : int option
}

(* keep this code in sync with Client.chain_id_inputs, Mina_commands.chain_id_inputs, and
Daemon_rpcs.Chain_id_inputs
*)
let chain_id ~constraint_system_digests ~genesis_state_hash ~genesis_constants
~protocol_major_version =
~protocol_transaction_version ~protocol_network_version =
(* if this changes, also change Mina_commands.chain_id_inputs *)
let genesis_state_hash = State_hash.to_base58_check genesis_state_hash in
let genesis_constants_hash = Genesis_constants.hash genesis_constants in
let all_snark_keys =
List.map constraint_system_digests ~f:(fun (_, digest) -> Md5.to_hex digest)
|> String.concat ~sep:""
in
let protocol_version_digest =
Int.to_string protocol_major_version |> Md5.digest_string |> Md5.to_hex
let version_digest v = Int.to_string v |> Md5.digest_string |> Md5.to_hex in
let protocol_transaction_version_digest =
version_digest protocol_transaction_version
in
let protocol_network_version_digest =
version_digest protocol_network_version
in
let b2 =
Blake2.digest_string
( genesis_state_hash ^ all_snark_keys ^ genesis_constants_hash
^ protocol_version_digest )
^ protocol_transaction_version_digest ^ protocol_network_version_digest )
in
Blake2.to_hex b2

[%%inject "daemon_expiry", daemon_expiry]

[%%inject "compile_time_current_protocol_version", current_protocol_version]

[%%if plugins]

let plugin_flag =
Expand Down Expand Up @@ -364,13 +369,6 @@ let setup_daemon logger =
flag "--peer-list-url" ~aliases:[ "peer-list-url" ]
~doc:"URL URL of seed peer list file. Will be polled periodically."
(optional string)
and curr_protocol_version =
flag "--current-protocol-version"
~aliases:[ "current-protocol-version" ]
(optional string)
~doc:
"NN.NN.NN Current protocol version, only blocks with the same version \
accepted"
and proposed_protocol_version =
flag "--proposed-protocol-version"
~aliases:[ "proposed-protocol-version" ]
Expand Down Expand Up @@ -1213,18 +1211,21 @@ let setup_daemon logger =

Pass one of -peer, -peer-list-file, -seed, -peer-list-url.|} ;
let chain_id =
let protocol_major_version =
Protocol_version.of_string_exn
compile_time_current_protocol_version
|> Protocol_version.major
let protocol_transaction_version =
Protocol_version.(transaction current)
in
let protocol_network_version =
Protocol_version.(transaction current)
in
chain_id ~genesis_state_hash
~genesis_constants:precomputed_values.genesis_constants
~constraint_system_digests:
(Lazy.force precomputed_values.constraint_system_digests)
~protocol_major_version
~protocol_transaction_version ~protocol_network_version
in
[%log info] "Daemon will use chain id %s" chain_id ;
[%log info] "Daemon running protocol version %s"
Protocol_version.(to_string current) ;
let gossip_net_params =
Gossip_net.Libp2p.Config.
{ timeout = Time.Span.of_sec 3.
Expand Down Expand Up @@ -1276,11 +1277,6 @@ Pass one of -peer, -peer-list-file, -seed, -peer-list-url.|} ;
Option.value_map coinbase_receiver_flag ~default:`Producer
~f:(fun pk -> `Other pk)
in
let current_protocol_version =
Mina_run.get_current_protocol_version
~compile_time_current_protocol_version ~conf_dir ~logger
curr_protocol_version
in
let proposed_protocol_version_opt =
Mina_run.get_proposed_protocol_version_opt ~conf_dir ~logger
proposed_protocol_version
Expand Down Expand Up @@ -1352,9 +1348,7 @@ Pass one of -peer, -peer-list-file, -seed, -peer-list-url.|} ;
(Mina_lib.Config.make ~logger ~pids ~trust_system ~conf_dir
~chain_id ~is_seed ~super_catchup:(not no_super_catchup)
~disable_node_status ~demo_mode ~coinbase_receiver ~net_config
~gossip_net_params
~initial_protocol_version:current_protocol_version
~proposed_protocol_version_opt
~gossip_net_params ~proposed_protocol_version_opt
~work_selection_method:
(Cli_lib.Arg_type.work_selection_method_to_module
work_selection_method )
Expand Down
8 changes: 5 additions & 3 deletions src/app/cli/src/init/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,8 @@ let chain_id_inputs =
( genesis_state_hash
, genesis_constants
, snark_keys
, protocol_major_version ) ->
, protocol_transaction_version
, protocol_network_version ) ->
let open Format in
printf
"@[<v>Genesis state hash: %s@,\
Expand All @@ -2144,7 +2145,8 @@ let chain_id_inputs =
@]@,\
@[<v 2>Snark keys:@,\
%a@]@,\
Protocol major version: %d@]@."
Protocol transaction version: %u@,\
Protocol network version: %u@]@."
(State_hash.to_base58_check genesis_state_hash)
Yojson.Safe.pp
(Genesis_constants.Protocol.to_yojson genesis_constants.protocol)
Expand All @@ -2154,7 +2156,7 @@ let chain_id_inputs =
pp_print_int )
genesis_constants.num_accounts
(pp_print_list ~pp_sep:pp_print_cut pp_print_string)
snark_keys protocol_major_version
snark_keys protocol_transaction_version protocol_network_version
| Error err ->
Format.eprintf "Could not get chain id inputs: %s@."
(Error.to_string_hum err) ) )
Expand Down
60 changes: 0 additions & 60 deletions src/app/cli/src/init/mina_run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,66 +31,6 @@ let make_conf_dir_item_io ~conf_dir ~filename =
in
(read_item, write_item)

let get_current_protocol_version ~compile_time_current_protocol_version
~conf_dir ~logger =
let read_protocol_version, write_protocol_version =
make_conf_dir_item_io ~conf_dir ~filename:"current_protocol_version"
in
function
| None -> (
try
(* not provided on command line, try to read from config dir *)
let protocol_version = read_protocol_version () in
[%log info]
"Setting current protocol version to $protocol_version from config"
~metadata:[ ("protocol_version", `String protocol_version) ] ;
Protocol_version.of_string_exn protocol_version
with Sys_error _ ->
(* not on command-line, not in config dir, use compile-time value *)
[%log info]
"Setting current protocol version to $protocol_version from \
compile-time config"
~metadata:
[ ("protocol_version", `String compile_time_current_protocol_version)
] ;
Protocol_version.of_string_exn compile_time_current_protocol_version )
| Some protocol_version -> (
try
(* it's an error if the command line value disagrees with the value in the config *)
let config_protocol_version = read_protocol_version () in
if String.equal config_protocol_version protocol_version then (
[%log info]
"Using current protocol version $protocol_version from command \
line, which matches the one in the config"
~metadata:[ ("protocol_version", `String protocol_version) ] ;
Protocol_version.of_string_exn config_protocol_version )
else (
[%log fatal]
"Current protocol version $protocol_version from the command line \
disagrees with $config_protocol_version from the Coda config"
~metadata:
[ ("protocol_version", `String protocol_version)
; ("config_protocol_version", `String config_protocol_version)
] ;
failwith
"Current protocol version from command line disagrees with \
protocol version in Coda config; please delete your Coda config \
if you wish to use a new protocol version" )
with Sys_error _ -> (
(* use value provided on command line, write to config dir *)
match Protocol_version.of_string_opt protocol_version with
| None ->
[%log fatal] "Protocol version provided on command line is invalid"
~metadata:[ ("protocol_version", `String protocol_version) ] ;
failwith "Protocol version from command line is invalid"
| Some pv ->
write_protocol_version protocol_version ;
[%log info]
"Using current protocol_version $protocol_version from command \
line, writing to config"
~metadata:[ ("protocol_version", `String protocol_version) ] ;
pv ) )

let get_proposed_protocol_version_opt ~conf_dir ~logger =
let read_protocol_version, write_protocol_version =
make_conf_dir_item_io ~conf_dir ~filename:"proposed_protocol_version"
Expand Down
11 changes: 7 additions & 4 deletions src/app/extract_blocks/extract_blocks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ let fill_in_block pool (block : Archive_lib.Processor.Block.t) :
let timestamp = Block_time.of_string_exn block.timestamp in
let chain_status = Chain_status.of_string block.chain_status in
let%bind protocol_version =
let%map { major; minor; patch } =
let%map { transaction; network; patch } =
query_db ~f:(fun db ->
Processor.Protocol_versions.load db block.protocol_version_id )
in
Protocol_version.create_exn ~major ~minor ~patch
Protocol_version.create ~transaction ~network ~patch
in
let%bind proposed_protocol_version =
Option.value_map block.proposed_protocol_version_id ~default:(return None)
~f:(fun id ->
let%map { major; minor; patch } =
let%map { transaction; network; patch } =
query_db ~f:(fun db -> Processor.Protocol_versions.load db id)
in
Some (Protocol_version.create_exn ~major ~minor ~patch) )
let protocol_version =
Protocol_version.create ~transaction ~network ~patch
in
Some protocol_version )
in
(* commands, accounts_accessed, accounts_created, tokens_used to be filled in later *)
return
Expand Down
5 changes: 4 additions & 1 deletion src/config/protocol_version/current.mlh
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
[%%define current_protocol_version "3.0.0"]
(* transaction >= 1, network >= 1, patch >= 0 *)
[%%define protocol_version_transaction 3]
[%%define protocol_version_network 1]
[%%define protocol_version_patch 0]
2 changes: 1 addition & 1 deletion src/lib/daemon_rpcs/daemon_rpcs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module Chain_id_inputs = struct
type query = unit [@@deriving bin_io_unversioned]

type response =
State_hash.Stable.Latest.t * Genesis_constants.t * string list * int
State_hash.Stable.Latest.t * Genesis_constants.t * string list * int * int
[@@deriving bin_io_unversioned]

let rpc : (query, response) Rpc.Rpc.t =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ledger_catchup/normal_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ let verify_transition ~context:(module Context : CONTEXT) ~trust_system
(Mina_block.header transition)
|> Protocol_version.to_string ) )
; ( "daemon_current_protocol_version"
, `String Protocol_version.(get_current () |> to_string) )
, `String Protocol_version.(to_string current) )
] ) )
in
Error (Error.of_string "mismatched protocol version")
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ledger_catchup/super_catchup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ let verify_transition ~context:(module Context : CONTEXT) ~trust_system
(Mina_block.header transition)
|> Protocol_version.to_string ) )
; ( "daemon_current_protocol_version"
, `String Protocol_version.(get_current () |> to_string) )
, `String Protocol_version.(to_string current) )
] ) )
in
Error (Error.of_string "mismatched protocol version")
Expand Down
Loading