From 2f4fdec5ab2a94e1955111806057ef49fc64b878 Mon Sep 17 00:00:00 2001 From: Thomas Plisson Date: Tue, 25 Feb 2025 13:26:28 +0100 Subject: [PATCH] EVM: observer snapshot file --- etherlink/CHANGES_NODE.md | 3 ++ etherlink/bin_node/lib_dev/evm_context.ml | 24 +++++--------- etherlink/bin_node/lib_dev/snapshots.ml | 21 +++++++++++++ etherlink/bin_node/lib_dev/snapshots.mli | 15 ++++++++- etherlink/bin_node/main.ml | 31 +++++-------------- .../evm_sequencer.ml/EVM Node- man.out | 2 +- 6 files changed, 55 insertions(+), 41 deletions(-) diff --git a/etherlink/CHANGES_NODE.md b/etherlink/CHANGES_NODE.md index 27d070907146..ae5cee53cbe1 100644 --- a/etherlink/CHANGES_NODE.md +++ b/etherlink/CHANGES_NODE.md @@ -4,6 +4,9 @@ ### Configuration changes +- Observer `--init-from-snapshot` now also accepts a path to an existing + snapshot. (!16963) + ### RPCs changes ### Metrics changes diff --git a/etherlink/bin_node/lib_dev/evm_context.ml b/etherlink/bin_node/lib_dev/evm_context.ml index 133ee62c0322..7476489d493f 100644 --- a/etherlink/bin_node/lib_dev/evm_context.ml +++ b/etherlink/bin_node/lib_dev/evm_context.ml @@ -1230,22 +1230,14 @@ module State = struct let* () = match snapshot_url with | Some snapshot_url when not store_already_exists -> - Lwt_utils_unix.with_tempdir - ".download_snapshot_" - ~temp_dir:data_dir - (fun tmp_dir -> - let* snapshot_file = - Misc.download_file - ~keep_alive:configuration.Configuration.keep_alive - ~working_dir:tmp_dir - snapshot_url - in - let*! () = Events.importing_snapshot () in - Snapshots.import - ~cancellable:true - ~force:true - ~data_dir - ~snapshot_file) + Snapshots.import_from + ~cancellable:true + ~force:true + ~keep_alive:configuration.Configuration.keep_alive + ~data_dir + ~download_path:".download_snapshot_" + ~snapshot_file:snapshot_url + () | _ -> return_unit in Irmin_context.load diff --git a/etherlink/bin_node/lib_dev/snapshots.ml b/etherlink/bin_node/lib_dev/snapshots.ml index da95914e2e00..3def5ab2d211 100644 --- a/etherlink/bin_node/lib_dev/snapshots.ml +++ b/etherlink/bin_node/lib_dev/snapshots.ml @@ -386,6 +386,27 @@ let import ~cancellable ~force ~data_dir ~snapshot_file = if display_progress then extract_snapshot_archive else Lwt.pick [extract_snapshot_archive; periodic_emit ()] +let import_from ~cancellable ~force ~keep_alive ~data_dir ~download_path + ~snapshot_file () = + let open Lwt_result_syntax in + let with_snapshot k = + if + String.starts_with ~prefix:"http://" snapshot_file + || String.starts_with ~prefix:"https://" snapshot_file + then + Lwt_utils_unix.with_tempdir ~temp_dir:data_dir download_path + @@ fun working_dir -> + let* snapshot_file = + Misc.download_file ~keep_alive ~working_dir snapshot_file + in + k snapshot_file + else k snapshot_file + in + Data_dir.use ~data_dir @@ fun () -> + with_snapshot @@ fun snapshot_file -> + let*! () = Events.importing_snapshot () in + import ~cancellable ~force ~data_dir ~snapshot_file + let info ~snapshot_file = let compressed = is_compressed_snapshot snapshot_file in let reader = if compressed then gzip_reader else stdlib_reader in diff --git a/etherlink/bin_node/lib_dev/snapshots.mli b/etherlink/bin_node/lib_dev/snapshots.mli index 0bafcb5d9506..ba0b6824b2c4 100644 --- a/etherlink/bin_node/lib_dev/snapshots.mli +++ b/etherlink/bin_node/lib_dev/snapshots.mli @@ -48,7 +48,7 @@ val export : unit -> string tzresult Lwt.t -(** [import ~display_progress ~force ~data_dir ~snapshot_file] imports the +(** [import ~cancellable ~force ~data_dir ~snapshot_file] imports the snapshot at path [snapshot_file] into the data directory [data_dir]. Import will fail if [data_dir] is already populated unless [force] is set to [true]. Set [cancellable] if you want to be able to cancel the resulting @@ -60,6 +60,19 @@ val import : snapshot_file:string -> unit tzresult Lwt.t +(** [import_from ~cancellable ~force ~keep_alive ~data_dir ~download_path ~snapshot_file] + similar to [import] but also takes care of downloading the file if the provided string + is an url. *) +val import_from : + cancellable:bool -> + force:bool -> + keep_alive:bool -> + data_dir:string -> + download_path:string -> + snapshot_file:string -> + unit -> + unit tzresult Lwt.t + (** [info ~snapshot_file] returns information that can be used to inspect the snapshot file. *) val info : snapshot_file:string -> Header.t * [`Compressed | `Uncompressed] diff --git a/etherlink/bin_node/main.ml b/etherlink/bin_node/main.ml index a903d32bf181..e0dced0ed1d6 100644 --- a/etherlink/bin_node/main.ml +++ b/etherlink/bin_node/main.ml @@ -512,10 +512,10 @@ let init_from_snapshot_arg = "Automatically download and import a recent snapshot for supported \ networks on fresh data directories. If no snapshot provider is given \ e.g. `--init-from-snapshot` with no argument, then it uses the default \ - built-in provider `https://snapshotter-sandbox.nomadic-labs.eu`.\n\ - %r is replaced by the short form of the Smart Rollup address, %R by the \ + built-in provider `https://snapshotter-sandbox.nomadic-labs.eu`. %r is \ + replaced by the short form of the Smart Rollup address, %R by the \ complete Smart Rollup address, %n by the network (given by the argument \ - --network), and %% by %." + --network), and %% by %. Also accepts a path to an existing snapshot." ~default: "https://snapshotter-sandbox.nomadic-labs.eu/etherlink-%n/evm-snapshot-%r-latest.gz" ~placeholder:"snapshot url" @@ -2439,34 +2439,19 @@ let import_snapshot_command = (prefixes ["snapshot"; "import"] @@ Params.snapshot_file_or_url @@ stop) (fun (data_dir, force) snapshot_file () -> let open Lwt_result_syntax in - let with_snapshot k = - if - String.starts_with ~prefix:"http://" snapshot_file - || String.starts_with ~prefix:"https://" snapshot_file - then - Lwt_utils_unix.with_tempdir ~temp_dir:data_dir ".download_" - @@ fun working_dir -> - let* snapshot_file = - Evm_node_lib_dev.Misc.download_file - ~keep_alive:false - ~working_dir - snapshot_file - in - k snapshot_file - else k snapshot_file - in let*! () = let open Tezos_base_unix.Internal_event_unix in let config = make_with_defaults ~verbosity:Notice () in init ~config () in - Evm_node_lib_dev.Data_dir.use ~data_dir @@ fun () -> - with_snapshot @@ fun snapshot_file -> - Evm_node_lib_dev.Snapshots.import + Evm_node_lib_dev.Snapshots.import_from ~cancellable:true ~force + ~keep_alive:false ~data_dir - ~snapshot_file) + ~download_path:".download_" + ~snapshot_file + ()) let snapshot_info_command = let open Tezos_clic in diff --git a/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- man.out b/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- man.out index 53519429c6f4..8f2763f294dc 100644 --- a/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- man.out +++ b/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- man.out @@ -245,7 +245,7 @@ Miscellaneous commands: --initial-kernel : Path to the EVM kernel used to launch the PVM, it will be loaded from storage afterward --dont-track-rollup-node: Disable tracking the head of the rollup node. Tracking the state of a rollup node allows to confirm the blocks received from the upstream EVM node. --no-sync: Disable tracking the head of the EVM node endpoint - --init-from-snapshot [snapshot url]: Automatically download and import a recent snapshot for supported networks on fresh data directories. If no snapshot provider is given e.g. `--init-from-snapshot` with no argument, then it uses the default built-in provider `https://snapshotter-sandbox.nomadic-labs.eu`. + --init-from-snapshot [snapshot url]: Automatically download and import a recent snapshot for supported networks on fresh data directories. If no snapshot provider is given e.g. `--init-from-snapshot` with no argument, then it uses the default built-in provider `https://snapshotter-sandbox.nomadic-labs.eu`. %r is replaced by the short form of the Smart Rollup address, %R by the complete Smart Rollup address, %n by the network (given by the argument --network), and %% by %. Also accepts a path to an existing snapshot. --network : The network the EVM node will be connecting to. Can be `mainnet` or `testnet`. If set, additional sanity checks are performed on the node’s startup. octez-evm-node experimental run rpc [--data-dir ] -- GitLab