From 02992ba5686dedb6c66748e557d3592c35c56d24 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Mon, 12 Aug 2024 22:41:51 +0200 Subject: [PATCH] EVM Node: Print SQLite schemas It can be difficult to keep track of the current SQLite tables, considering we only keep successive migration scripts. Arguably, it is possible to infer the current schemas by reading the SQLite statements in `evm_store.ml`. We propose a more direct way, by introducing a new command `debug print store schemas`. This one is not intented for end-users, but rather as a development tool. We add a regression test which can be used to review the current schemas without having to remember the command. --- etherlink/CHANGES_NODE.md | 2 + etherlink/bin_node/lib_dev/evm_store.ml | 20 ++++++++++ etherlink/bin_node/lib_dev/evm_store.mli | 6 +++ etherlink/bin_node/main.ml | 17 ++++++++ etherlink/tezt/lib/evm_node.ml | 6 +++ etherlink/tezt/lib/evm_node.mli | 3 ++ etherlink/tezt/tests/evm_sequencer.ml | 16 +++++++- .../EVM Node- debug print store schemas.out | 39 +++++++++++++++++++ 8 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- debug print store schemas.out diff --git a/etherlink/CHANGES_NODE.md b/etherlink/CHANGES_NODE.md index 6980d8375764..07ef79498bd5 100644 --- a/etherlink/CHANGES_NODE.md +++ b/etherlink/CHANGES_NODE.md @@ -81,6 +81,8 @@ estimation of DA fees no longer require to run the kernel. (!14165) - Add a private RPC method to by-pass node validation when injecting a transaction to the TX pool. (!13856). +- Add a debug command to print the SQLite schemas currently is use by the node. + (!14509) ## Version for ec7c3b349624896b269e179384d0a45cf39e1145 diff --git a/etherlink/bin_node/lib_dev/evm_store.ml b/etherlink/bin_node/lib_dev/evm_store.ml index 1ffe16096eb4..254bfd874a7e 100644 --- a/etherlink/bin_node/lib_dev/evm_store.ml +++ b/etherlink/bin_node/lib_dev/evm_store.ml @@ -200,6 +200,13 @@ module Q = struct let vacuum_request = (string ->. unit) @@ {|VACUUM main INTO ?|} + module Schemas = struct + let get_all = + (unit ->* string) + @@ {|SELECT sql FROM sqlite_schema WHERE name + NOT LIKE 'sqlite_%' AND name != 'migrations'|} + end + module Migrations = struct let create_table = (unit ->. unit) @@ -225,7 +232,14 @@ module Q = struct (with leading 0s) followed by the name of the migration (e.g. [005_create_blueprints_table.sql]) - Run [etherlink/scripts/check_evm_store_migrations.sh promote] + - Regenerate the schemas, using [[ + dune exec -- etherlink/tezt/tests/main.exe --file evm_sequencer.ml \ + evm store schemas regression --reset-regressions + ]] - Increment [version] + + You can review the result at + [etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- debug print store schemas.out]. *) let version = 9 @@ -408,6 +422,12 @@ module Journal_mode = struct return_unit end +module Schemas = struct + let get_all store = + with_connection store @@ fun conn -> + Db.collect_list conn Q.Schemas.get_all () +end + module Migrations = struct let create_table store = with_connection store @@ fun conn -> diff --git a/etherlink/bin_node/lib_dev/evm_store.mli b/etherlink/bin_node/lib_dev/evm_store.mli index 71a031cfd4da..e6d00cbf18cb 100644 --- a/etherlink/bin_node/lib_dev/evm_store.mli +++ b/etherlink/bin_node/lib_dev/evm_store.mli @@ -45,6 +45,12 @@ val with_transaction : conn -> (conn -> 'a tzresult Lwt.t) -> 'a tzresult Lwt.t @raise Assert_failure *) val assert_in_transaction : conn -> unit +module Schemas : sig + (** [get_all conn] returns the list of SQL statements allowing to recreate + the tables in the current store. *) + val get_all : conn -> string list tzresult Lwt.t +end + module Blueprints : sig val store : conn -> Blueprint_types.t -> unit tzresult Lwt.t diff --git a/etherlink/bin_node/main.ml b/etherlink/bin_node/main.ml index 1638bce01488..4d3193573421 100644 --- a/etherlink/bin_node/main.ml +++ b/etherlink/bin_node/main.ml @@ -2460,6 +2460,22 @@ let preemptive_kernel_download_command = ~preimages_endpoint ~root_hash) +let debug_print_store_schemas_command = + let open Tezos_clic in + command + ~desc:"Print SQL statements describing the tables created in the store" + no_options + (prefixes ["debug"; "print"; "store"; "schemas"] @@ stop) + (fun () () -> + let open Lwt_result_syntax in + let open Evm_node_lib_dev in + Lwt_utils_unix.with_tempdir "store" @@ fun data_dir -> + let* store = Evm_store.init ~data_dir ~perm:`Read_write () in + let* schemas = Evm_store.(use store Schemas.get_all) in + let output = String.concat ";\n\n" schemas in + Format.printf "%s\n" output ; + return_unit) + (* List of program commands *) let commands = [ @@ -2486,6 +2502,7 @@ let commands = export_snapshot_named_command; patch_state_command; preemptive_kernel_download_command; + debug_print_store_schemas_command; ] let global_options = Tezos_clic.no_options diff --git a/etherlink/tezt/lib/evm_node.ml b/etherlink/tezt/lib/evm_node.ml index 565d10a3fff3..513cbd714a0c 100644 --- a/etherlink/tezt/lib/evm_node.ml +++ b/etherlink/tezt/lib/evm_node.ml @@ -1065,6 +1065,12 @@ let sequencer_upgrade_payload ?client ~public_key ~pool_address let* payload = Process.check_and_read_stdout process in return (String.trim payload) +let debug_print_store_schemas ?(path = Uses.path Constant.octez_evm_node) ?hooks + () = + let args = ["debug"; "print"; "store"; "schemas"] in + let process = Process.spawn ?hooks path @@ args in + Process.check process + let chunk_data ~rollup_address ?sequencer_key ?timestamp ?parent_hash ?number ?client data = let args = "chunk" :: "data" :: data in diff --git a/etherlink/tezt/lib/evm_node.mli b/etherlink/tezt/lib/evm_node.mli index 0136a64806ec..4d139cde13f1 100644 --- a/etherlink/tezt/lib/evm_node.mli +++ b/etherlink/tezt/lib/evm_node.mli @@ -438,6 +438,9 @@ val make_kernel_installer_config : unit -> (Process.t, unit) Runnable.t +val debug_print_store_schemas : + ?path:string -> ?hooks:Process_hooks.t -> unit -> unit Lwt.t + module Agent : sig val create : ?path:string -> diff --git a/etherlink/tezt/tests/evm_sequencer.ml b/etherlink/tezt/tests/evm_sequencer.ml index 18ce299c5b42..d45a908594fe 100644 --- a/etherlink/tezt/tests/evm_sequencer.ml +++ b/etherlink/tezt/tests/evm_sequencer.ml @@ -5997,6 +5997,19 @@ let test_batch_limit_size_rpc = unit +let test_debug_print_store_schemas () = + Regression.register + ~__FILE__ + ~title:"EVM Node: debug print store schemas" + ~tags:["evm"; "store"; "schemas"] + ~uses:[Constant.octez_evm_node] + ~uses_node:false + ~uses_client:false + ~uses_admin_client:false + @@ fun () -> + let hooks = Tezos_regression.hooks in + Evm_node.debug_print_store_schemas ~hooks () + let test_relay_restricted_rpcs = register_all ~bootstrap_accounts:Eth_account.lots_of_address @@ -6099,4 +6112,5 @@ let () = test_trace_transaction_calltracer_all_types protocols ; test_trace_transaction_call_tracer_with_logs protocols ; test_trace_transaction_call_trace_certain_depth protocols ; - test_trace_transaction_call_trace_revert protocols + test_trace_transaction_call_trace_revert protocols ; + test_debug_print_store_schemas () diff --git a/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- debug print store schemas.out b/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- debug print store schemas.out new file mode 100644 index 000000000000..d74c16de3cd8 --- /dev/null +++ b/etherlink/tezt/tests/expected/evm_sequencer.ml/EVM Node- debug print store schemas.out @@ -0,0 +1,39 @@ + +./octez-evm-node debug print store schemas +CREATE TABLE context_hashes ( + id SERIAL PRIMARY KEY, + context_hash VARCHAR(52) NOT NULL +); + +CREATE TABLE kernel_upgrades ( + injected_before INT NOT NULL, + root_hash TEXT NOT NULL, + activation_timestamp INT NOT NULL, + applied_before INT +); + +CREATE TABLE blueprints ( + id SERIAL PRIMARY KEY, + payload BLOB NOT NULL, + timestamp DATETIME NOT NULL +); + +CREATE TABLE delayed_transactions ( + injected_before INT NOT NULL, + hash TEXT NOT NULL, + payload TEXT NOT NULL +); + +CREATE TABLE "l1_l2_levels_relationships" ( + latest_l2_level PRIMARY KEY ON CONFLICT REPLACE, + l1_level INT NOT NULL UNIQUE ON CONFLICT ABORT +, finalized_l2_level INT DEFAULT 0); + +CREATE TABLE metadata ( + smart_rollup_address TEXT NOT NULL +); + +CREATE UNIQUE INDEX unapplied_upgrade +ON kernel_upgrades ( + COALESCE(applied_before, -1) +) -- GitLab