diff --git a/tezt/tests/cloud/layer1.ml b/tezt/tests/cloud/layer1.ml index d528a52ed3daee2c533ce3e41ab4749bec254814..7ac8bd25d1405a2976885b6ff3dc7279d35bfd8e 100644 --- a/tezt/tests/cloud/layer1.ml +++ b/tezt/tests/cloud/layer1.ml @@ -18,21 +18,13 @@ not requiring this data *) -open Tezos open Scenarios_helpers +open Tezos +open Yes_crypto (** A baker account is its public key (pk) and its hash (pkh) *) type baker_account = {pk : string; pkh : string} -let add_prometheus_source node cloud agent = - Scenarios_helpers.add_prometheus_source ~node cloud agent (Agent.name agent) - -let yes_crypto_env = - String_map.add - Tezos_crypto.Helpers.yes_crypto_environment_variable - "y" - String_map.empty - let wait_next_level ?(offset = 1) node = Lwt.bind (Network.get_level (Node.as_rpc_endpoint node)) @@ -55,63 +47,6 @@ module Node = struct open Snapshot_helpers include Node - (** We are running a private network with yes-crypto enabled. - We don't want to connect with the real network. - *) - let isolated_config ~peers ~network ~delay = - [ - No_bootstrap_peers; - Connections (List.length peers); - Synchronisation_threshold (if List.length peers < 2 then 1 else 2); - Network Network.(to_octez_network_options @@ to_public network); - Expected_pow 0; - Cors_origin "*"; - Storage_maintenance_delay (string_of_int delay); - ] - - (** [--private-mode] is mainly useful for the bootstrap node - because it is used first to bootstrap a node with real network peers - before being disconnected. - For the other node, it's an extra security but their ip/identity should - not be advertised to the external world anyway. - *) - let isolated_args peers = - Private_mode - :: List.fold_left - (fun acc peer -> Peer peer :: acc) - [Allow_yes_crypto; Force_history_mode_switch] - peers - - (** [add_migration_offset_to_config node snapshot ~migration_offset ~network] adds an - entry in the configuration file of [node] to trigger a UAU at level [~migration_offset] - to upgrade to the next protocol of [~network]. This entry is is parametrised by the - information obtained from [snapshot]. *) - let add_migration_offset_to_config node snapshot ~migration_offset ~network = - let* level = get_snapshot_info_level node snapshot in - match migration_offset with - | None -> Lwt.return_unit - | Some migration_offset -> - let* network_config = - match network with - | `Mainnet -> Lwt.return Node.Config_file.mainnet_network_config - | `Ghostnet -> Lwt.return Node.Config_file.ghostnet_network_config - | _ -> - Lwt.fail_with - "Migration scenarios are only supported for Mainnet and \ - Ghostnet." - in - let migration_level = level + migration_offset in - toplog "Add UAU entry for level : %d" migration_level ; - Node.Config_file.update node (fun json -> - JSON.put - ( "network", - JSON.annotate - ~origin:"add_migration_offset_to_config" - network_config ) - json - |> Node.Config_file.update_network_with_user_activated_upgrades - [(migration_level, Protocol.Alpha)]) - (* If trying to only bootstrap the network from a snapshot, you will have errors about missing block metadata, which is likely (I guess?) to be because of data not included in the snapshot. @@ -158,12 +93,16 @@ module Node = struct Lwt.return_unit in toplog "Reset node config for private a yes-crypto network" ; - let config = isolated_config ~peers ~network ~delay:0 in + let config = Node_helpers.isolated_config ~peers ~network ~delay:0 in let* () = Node.config_reset node config in let* () = - add_migration_offset_to_config node snapshot ~migration_offset ~network + Node_helpers.may_add_migration_offset_to_config + node + snapshot + ~migration_offset + ~network in - let arguments = isolated_args peers in + let arguments = Node_helpers.isolated_args peers in let* () = run ~env:yes_crypto_env node arguments in wait_for_ready node @@ -177,13 +116,17 @@ module Node = struct let* snapshot = ensure_snapshot ~agent ~name ~network:(Network.to_public network) snapshot in - let config = isolated_config ~peers ~network ~delay in + let config = Node_helpers.isolated_config ~peers ~network ~delay in let* () = Node.config_init node config in let* () = - add_migration_offset_to_config node snapshot ~migration_offset ~network + Node_helpers.may_add_migration_offset_to_config + node + snapshot + ~migration_offset + ~network in let* () = import_snapshot ~no_check:true ~name node snapshot in - let arguments = isolated_args peers in + let arguments = Node_helpers.isolated_args peers in let* () = run ~env:yes_crypto_env node arguments in let* () = wait_for_ready node in (* As we are playing with dates in the past, @@ -196,10 +139,6 @@ module Node = struct let name = Tezt_cloud.Agent.name agent ^ "-client" in Client.Agent.create ~name ~endpoint:(Client.Node node) agent - let yes_wallet agent = - let name = Tezt_cloud.Agent.name agent ^ "-yes-wallet" in - Yes_wallet.Agent.create ~name agent - (** Initialize the node, create the associated client, create the yes-wallet. @@ -216,7 +155,7 @@ module Node = struct migration_offset in let* client = client ~node agent in - let* yes_wallet = yes_wallet agent in + let* yes_wallet = Node_helpers.yes_wallet agent in let* _filename = Yes_wallet.create_from_context ~node @@ -247,7 +186,7 @@ module Node = struct (agent, node, name) in let* client = client ~node agent in - let* yes_wallet = yes_wallet agent in + let* yes_wallet = Node_helpers.yes_wallet agent in let* () = Lwt_list.iter_s (fun {pkh = alias; pk = public_key} -> @@ -270,7 +209,7 @@ module Node = struct (agent, node, name) in let* client = client ~node agent in - let* yes_wallet = yes_wallet agent in + let* yes_wallet = Node_helpers.yes_wallet agent in let* () = Client.forget_all_keys client in let* () = Client.import_public_key ~alias:pkh ~public_key:pk client in let* () = Yes_wallet.convert_wallet_inplace ~client yes_wallet in @@ -612,12 +551,14 @@ let number_of_bakers ~snapshot ~network cloud agent name = snapshot in let* () = - Node.config_init node (Node.isolated_config ~peers:[] ~network ~delay:0) + Node.config_init + node + (Node_helpers.isolated_config ~peers:[] ~network ~delay:0) in let* () = Snapshot_helpers.import_snapshot ~no_check:true ~name node snapshot in - let* () = Node.Agent.run node (Node.isolated_args []) in + let* () = Node.Agent.run node (Node_helpers.isolated_args []) in let* () = Node.wait_for_ready node in let* client = Client.Agent.create ~name:"tmp-client" ~endpoint:(Node node) agent @@ -813,16 +754,19 @@ let init ~(configuration : configuration) cloud next_agent = in Lwt.return stresstesters in - let* () = add_prometheus_source bootstrap_node cloud bootstrap_agent in + let* () = + add_prometheus_source ~node:bootstrap_node cloud bootstrap_agent "bootstrap" + in let* () = Lwt_list.iter_s - (fun ({agent; node; _} : baker) -> add_prometheus_source node cloud agent) + (fun ({agent; node; _} : baker) -> + add_prometheus_source ~node cloud agent (Agent.name agent)) bakers in let* () = Lwt_list.iter_s (fun ({agent; node; _} : stresstester) -> - add_prometheus_source node cloud agent) + add_prometheus_source ~node cloud agent (Agent.name agent)) stresstesters in Lwt.return {cloud; configuration; bootstrap; bakers; stresstesters} diff --git a/tezt/tests/cloud/network.ml b/tezt/tests/cloud/network.ml index c124fde40d80d49efc5136e14c35704bd807f7e7..24824859ffbd84dd66718f065e34bf558848a0ff 100644 --- a/tezt/tests/cloud/network.ml +++ b/tezt/tests/cloud/network.ml @@ -130,6 +130,8 @@ let stake_repartition_encoding = (fun (network, max_nb_bakers) -> Mimic {network; max_nb_bakers}); ] +let is_public = function #public -> true | _ -> false + let to_public = function | `Sandbox -> failwith "Sandbox is not public" | #public as p -> p @@ -173,6 +175,10 @@ let block_time : t -> int = function | `Ghostnet -> 5 | _ -> failwith "Block times are only available for Mainnet and Ghostnet." +let next_protocol : t -> Protocol.t = function + | `Mainnet | `Ghostnet | `Rionet -> S023 + | _ -> Alpha + let public_rpc_endpoint testnet = Endpoint.make ~scheme:"https" diff --git a/tezt/tests/cloud/network.mli b/tezt/tests/cloud/network.mli index fd31070bb349073ed71807948b7e09c688b2f142..c0182eab8b84b604b3f52c3880e1a2ec27f75c2d 100644 --- a/tezt/tests/cloud/network.mli +++ b/tezt/tests/cloud/network.mli @@ -34,6 +34,8 @@ type stake_repartition = val stake_repartition_encoding : stake_repartition Data_encoding.t +val is_public : t -> bool + val to_public : t -> public (** Parse the given [string] into an available network option. *) @@ -45,6 +47,8 @@ val default_protocol : t -> Protocol.t (** Block time value for the network *) val block_time : t -> int +val next_protocol : t -> Protocol.t + (** Endpoint publicly available with RPC opened *) val public_rpc_endpoint : public -> Endpoint.t diff --git a/tezt/tests/cloud/node_helpers.ml b/tezt/tests/cloud/node_helpers.ml index c4f277fb7483bfb340e48d7bb531ca75bda82e24..edd1c109bbfd9a1799e0416d7d98764eab5a1bc9 100644 --- a/tezt/tests/cloud/node_helpers.ml +++ b/tezt/tests/cloud/node_helpers.ml @@ -6,6 +6,7 @@ (* *) (*****************************************************************************) +open Network open Scenarios_helpers open Snapshot_helpers open Tezos @@ -23,107 +24,79 @@ let yes_wallet agent = let name = Tezt_cloud.Agent.name agent ^ "-yes-wallet" in Yes_wallet.Agent.create ~name agent -let init ?(arguments = []) ?data_dir ?identity_file ?dal_config ?env - ~rpc_external ~name network ~with_yes_crypto ~snapshot ?ppx_profiling cloud - agent = - toplog "Initializing an L1 node for %s" name ; - match network with - | #Network.public -> ( - let network = Network.to_public network in - (* for public networks deployments, we listen on all interfaces on both - ipv4 and ipv6 *) - let net_addr = "[::]" in - match data_dir with - | Some data_dir -> - let* node = - Node.Agent.create - ~rpc_external - ~net_addr - ~arguments - ~data_dir - ~name - cloud - agent - in - let* () = may_copy_node_identity_file agent node identity_file in - let* () = - Node.Agent.run - ?ppx_profiling - ?env - node - [Network (Network.to_octez_network_options network)] - in - let* () = Node.wait_for_ready node in - Lwt.return node - | None -> - toplog - "No data dir given, we will attempt to bootstrap the node from a \ - rolling snapshot." ; - toplog "Creating the agent %s." name ; - let* node = - Node.Agent.create - ~rpc_external - ~net_addr - ~arguments: - [ - Network (Network.to_octez_network_options network); - Expected_pow 26; - Cors_origin "*"; - ] - ?data_dir - ~name - cloud - agent - in - let* () = may_copy_node_identity_file agent node identity_file in - toplog "Initializing node configuration for %s" name ; - let* () = Node.config_init node [] in - let* snapshot_file_path = - ensure_snapshot ~agent ~name ~network snapshot - in - let* () = - import_snapshot - ~delete_snapshot_file:(snapshot = No_snapshot) - ~no_check:true - ~name - node - snapshot_file_path - in - toplog "Launching the node %s." name ; - let* () = - Node.Agent.run - ?ppx_profiling - ?env - node - (* We've just imported a rolling snapshot keeping few history. - To switch to the configured history mode, which may have - longer history, we need the --force-history-mode-switch - option. *) - (Force_history_mode_switch :: Synchronisation_threshold 1 - :: arguments) - in - toplog "Waiting for the node %s to be ready." name ; - let* () = Node.wait_for_ready node in - toplog "Node %s is ready." name ; - let* () = Node.wait_for_synchronisation ~statuses:["synced"] node in - toplog "Node %s is bootstrapped" name ; - Lwt.return node) - | _ (* private network *) -> ( - (* For sandbox deployments, we only listen on local interface, hence - no connection could be made to us from outside networks *) - let net_addr = "127.0.0.1" in - let yes_crypto_arg = - if with_yes_crypto then [Node.Allow_yes_crypto] else [] +(** We are running a private network with yes-crypto enabled. + We don't want to connect with the real network. *) +let isolated_config ~peers ~network ~delay = + Node. + [ + No_bootstrap_peers; + Connections (List.length peers); + Synchronisation_threshold (if List.length peers < 2 then 1 else 2); + Network Network.(to_octez_network_options @@ to_public network); + Expected_pow 0; + Cors_origin "*"; + Storage_maintenance_delay (string_of_int delay); + ] + +(** [--private-mode] is mainly useful for the bootstrap node + because it is used first to bootstrap a node with real network peers + before being disconnected. + For the other node, it's an extra security but their ip/identity should + not be advertised to the external world anyway. *) +let isolated_args peers = + Node.( + Private_mode + :: List.fold_left + (fun acc peer -> Peer peer :: acc) + [Allow_yes_crypto; Force_history_mode_switch] + peers) + +let may_add_migration_offset_to_config node snapshot ~migration_offset ~network + = + let* level = get_snapshot_info_level node snapshot in + match migration_offset with + | None -> Lwt.return_unit + | Some migration_offset -> + let* network_config = + match network with + | `Mainnet -> Lwt.return Node.Config_file.mainnet_network_config + | `Ghostnet -> Lwt.return Node.Config_file.ghostnet_network_config + | _ -> + Lwt.fail_with + "Migration scenarios are only supported for Mainnet and Ghostnet." in - match data_dir with - | None -> - let* node = - Node.Agent.create ~net_addr ~rpc_external ~name cloud agent + let migration_level = level + migration_offset in + toplog "Add UAU entry for level : %d" migration_level ; + Node.Config_file.update node (fun json -> + JSON.put + ( "network", + JSON.annotate + ~origin:"add_migration_offset_to_config" + network_config ) + json + |> Node.Config_file.update_network_with_user_activated_upgrades + [(migration_level, Network.next_protocol network)]) + +let may_init_from_snapshot node ?data_dir ?dal_config ~network ~snapshot + ?migration_offset ~name agent = + match data_dir with + | Some _ -> Lwt.return_unit + | None -> ( + toplog + "No data dir given, we will attempt to bootstrap the node from a \ + rolling snapshot." ; + toplog "Initializing node configuration for %s" name ; + let* () = Node.config_init node [Cors_origin "*"] in + let* snapshot_file_path = + if is_public network then + let* snapshot_file_path = + ensure_snapshot ~agent ~name ~network:(to_public network) snapshot in - let* () = Node.config_init node [Cors_origin "*"] in - let* snapshot_path = ensure_snapshot_opt ~agent ~name snapshot in + Lwt.return_some snapshot_file_path + else + let* snapshot_file_path = ensure_snapshot_opt ~agent ~name snapshot in let* snapshot_network = - match snapshot_path with + match snapshot_file_path with | Some path -> let* network = get_snapshot_info_network node path in Lwt.return_some network @@ -131,67 +104,110 @@ let init ?(arguments = []) ?data_dir ?identity_file ?dal_config ?env in (* Set network *) let* () = - Node.Config_file.update - node - (match snapshot_network with - | Some "mainnet" -> Node.Config_file.set_mainnet_network () - | Some "ghostnet" -> Node.Config_file.set_ghostnet_network () - | Some "rionet" -> Node.Config_file.set_rionet_network () - | Some "seoulnet" -> Node.Config_file.set_seoulnet_network () - | _ -> Node.Config_file.set_sandbox_network) + Node.Config_file.( + update + node + (match snapshot_network with + | Some "mainnet" -> set_mainnet_network () + | Some "ghostnet" -> set_ghostnet_network () + | Some "rionet" -> set_rionet_network () + | Some "seoulnet" -> set_seoulnet_network () + | _ -> set_sandbox_network)) in let* () = match dal_config with | None -> Lwt.return_unit | Some config -> - Node.Config_file.update - node - (Node.Config_file.set_network_with_dal_config config) - in - let* () = may_copy_node_identity_file agent node identity_file in - let* () = - match snapshot_path with - | Some snapshot_path -> - import_snapshot ~no_check:true ~name node snapshot_path - | None -> Lwt.return_unit + Node.Config_file.( + update node (set_network_with_dal_config config)) in + Lwt.return snapshot_file_path + in + match snapshot_file_path with + | Some snapshot_file_path -> let* () = - Node.Agent.run - ?ppx_profiling - ?env + may_add_migration_offset_to_config + ~migration_offset + ~network node - (Node. - [ - No_bootstrap_peers; - Synchronisation_threshold 0; - Cors_origin "*"; - (* We've just imported a rolling snapshot keeping few - history. To switch to the configured history mode, which - may have longer history, we need the - --force-history-mode-switch option. *) - Force_history_mode_switch; - ] - @ yes_crypto_arg @ arguments) - in - let* () = Node.wait_for_ready node in - Lwt.return node - | Some data_dir -> - let arguments = - Node. - [No_bootstrap_peers; Synchronisation_threshold 0; Cors_origin "*"] - @ yes_crypto_arg @ arguments - in - let* node = - Node.Agent.create - ~rpc_external - ~net_addr - ~arguments - ~data_dir - ~name - cloud - agent + snapshot_file_path in - let* () = may_copy_node_identity_file agent node identity_file in - let* () = Node.Agent.run ?env ?ppx_profiling node arguments in - let* () = Node.wait_for_ready node in - Lwt.return node) + import_snapshot + ~delete_snapshot_file:(snapshot = No_snapshot) + ~no_check:true + ~name + node + snapshot_file_path + | None -> Lwt.return_unit) + +let init ?(arguments = []) ?data_dir ?identity_file ?dal_config ?env + ?migration_offset ~rpc_external ~name network ~with_yes_crypto ~snapshot + ?ppx_profiling cloud agent = + toplog "Initializing an L1 node for %s" name ; + let net_addr = + if is_public network then + (* for public networks deployments, we listen on all interfaces on both + ipv4 and ipv6 *) + "[::]" + (* For sandbox deployments, we only listen on local interface, hence + no connection could be made to us from outside networks. *) + else "127.0.0.1" + in + let arguments = + if is_public network then + Node.Network (to_octez_network_options @@ to_public network) :: arguments + else arguments + in + toplog "Creating the agent %s." name ; + let* node = + Node.Agent.create + ~rpc_external + ~net_addr + ~arguments + ?data_dir + ~name + cloud + agent + in + let* () = + may_init_from_snapshot + node + ?data_dir + ?dal_config + ~network + ~snapshot + ?migration_offset + ~name + agent + in + let* () = may_copy_node_identity_file agent node identity_file in + let arguments = + if Option.is_none data_dir then + (* We've just imported a rolling snapshot keeping few history. + To switch to the configured history mode, which may have + longer history, we need the --force-history-mode-switch + option. *) Node.Force_history_mode_switch :: arguments + else arguments + in + let arguments = + if is_public network then + Node.[Synchronisation_threshold 1; Cors_origin "*"; Expected_pow 26] + @ arguments + else + let yes_crypto_arg = + if with_yes_crypto then Node.[Allow_yes_crypto] else [] + in + Node.[No_bootstrap_peers; Synchronisation_threshold 0; Cors_origin "*"] + @ yes_crypto_arg @ arguments + in + toplog "Launching the node %s." name ; + let* () = Node.Agent.run ?ppx_profiling ?env node arguments in + toplog "Waiting for the node %s to be ready." name ; + let* () = Node.wait_for_ready node in + toplog "Node %s is ready." name ; + let* () = + if is_public network && Option.is_none data_dir then + Node.wait_for_synchronisation ~statuses:["synced"] node + else Lwt.return_unit + in + Lwt.return node diff --git a/tezt/tests/cloud/node_helpers.mli b/tezt/tests/cloud/node_helpers.mli index d6627ff08f067e2969a2c99d87de08b8ceaf5fab..c814404858ef259c38f92c2bf8a5d48f781fad50 100644 --- a/tezt/tests/cloud/node_helpers.mli +++ b/tezt/tests/cloud/node_helpers.mli @@ -18,6 +18,11 @@ (** Shortcut to create a "yes-wallet" for the given agent. *) val yes_wallet : Agent.t -> Yes_wallet.t Lwt.t +val isolated_config : + peers:string list -> network:Network.t -> delay:int -> Node.argument list + +val isolated_args : string list -> Node.argument list + (** Initialize an L1 node for the given configuration. If a [~snapshot] is provided and [?data_dir] is omitted, the node will be @@ -28,12 +33,24 @@ val yes_wallet : Agent.t -> Yes_wallet.t Lwt.t whereas for private networks, it binds to [127.0.0.1]. *) +(** [may_add_migration_offset_to_config node snapshot ~migration_offset ~network] may add an + entry in the configuration file of [node] to trigger a UAU at level [~migration_offset] + to upgrade to the next protocol of [~network]. This entry is is parametrised by the + information obtained from [snapshot]. *) +val may_add_migration_offset_to_config : + Node.t -> + string -> + migration_offset:int option -> + network:Network.t -> + unit Lwt.t + val init : ?arguments:Node.argument list -> ?data_dir:string -> ?identity_file:string -> ?dal_config:Tezos_crypto_dal_octez_dal_config.Dal_config.t -> ?env:string String_map.t -> + ?migration_offset:int -> rpc_external:bool -> name:string -> Network.t -> diff --git a/tezt/tests/cloud/yes_crypto.mli b/tezt/tests/cloud/yes_crypto.mli index d20798d390f582c36c6bd93a31349fd9760e126f..ee88fd6a84c450792a33cbe7313d64a2a2032f3f 100644 --- a/tezt/tests/cloud/yes_crypto.mli +++ b/tezt/tests/cloud/yes_crypto.mli @@ -12,6 +12,8 @@ open Scenarios_configuration +val yes_crypto_env : string String_map.t + (** [should_enable_yes_crypto config] returns a flag indicating whether "yes-crypto" mode should be enabled (for [Scatter] and [Map]) or disabled (for [Disabled]). *)