From 260b154027ed8ac4a7bd1420a5fbd31b5970d167 Mon Sep 17 00:00:00 2001 From: Marina Polubelova Date: Wed, 31 Jan 2024 17:43:55 +0100 Subject: [PATCH 1/3] Tezt/Tezos: expose missing parameter for baking_rights --- tezt/lib_tezos/RPC.ml | 3 ++- tezt/lib_tezos/RPC.mli | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tezt/lib_tezos/RPC.ml b/tezt/lib_tezos/RPC.ml index b979be969dbd..f609ae61a42d 100644 --- a/tezt/lib_tezos/RPC.ml +++ b/tezt/lib_tezos/RPC.ml @@ -799,10 +799,11 @@ let get_chain_block_context_contract_unstaked_frozen_balance ?(chain = "main") JSON.as_int let get_chain_block_helper_baking_rights ?(chain = "main") ?(block = "head") - ?delegate ?level () = + ?delegate ?level ?cycle () = let query_string = Query_arg.opt "delegate" Fun.id delegate @ Query_arg.opt "level" Int.to_string level + @ Query_arg.opt "cycle" string_of_int cycle in make ~query_string diff --git a/tezt/lib_tezos/RPC.mli b/tezt/lib_tezos/RPC.mli index bc4a268c34b5..eac762e15dd1 100644 --- a/tezt/lib_tezos/RPC.mli +++ b/tezt/lib_tezos/RPC.mli @@ -756,6 +756,7 @@ val get_chain_block_helper_baking_rights : ?block:string -> ?delegate:string -> ?level:int -> + ?cycle:int -> unit -> JSON.t t -- GitLab From 93b6a2da69429b4f51f152a42c05daaec8ca8c04 Mon Sep 17 00:00:00 2001 From: Marina Polubelova Date: Thu, 1 Feb 2024 16:23:54 +0100 Subject: [PATCH 2/3] Tezt/Tezos: use block_check in perform_protocol_migration --- tezt/tests/protocol_migration.ml | 100 ++++++++++++++----------------- 1 file changed, 45 insertions(+), 55 deletions(-) diff --git a/tezt/tests/protocol_migration.ml b/tezt/tests/protocol_migration.ml index fa1636a41814..7886ccd983e6 100644 --- a/tezt/tests/protocol_migration.ml +++ b/tezt/tests/protocol_migration.ml @@ -65,6 +65,35 @@ let user_migratable_node_init ?node_name ?client_name ?(more_node_args = []) let* client = Client.(init ?name:client_name ~endpoint:(Node node) ()) in Lwt.return (client, node) +(** [block_check ?level ~expected_block_type ~migrate_to ~migrate_from client] + is generic check that a block of type [expected_block_type] contains + (protocol) metatadata conforming to its type at [level]. **) +let block_check ?level ~expected_block_type ~migrate_to ~migrate_from client = + let block = + match level with Some level -> Some (string_of_int level) | None -> None + in + let* metadata = + Client.RPC.call client @@ RPC.get_chain_block_metadata ?block () + in + let protocol = metadata.protocol in + let next_protocol = metadata.next_protocol in + (match expected_block_type with + | `Migration -> + Check.( + (next_protocol = Protocol.hash migrate_to) + string + ~error_msg:"expected next protocol to be %R, got %L") ; + Check.( + (protocol = Protocol.hash migrate_from) + string + ~error_msg:"expected (from) protocol to be %R, got %L") + | `Non_migration -> + Check.( + (next_protocol = protocol) + string + ~error_msg:"expected a non migration block ")) ; + Lwt.return_unit + (* Migration to Tenderbake is only supported after the first cycle, therefore at [migration_level >= blocks_per_cycle]. *) let perform_protocol_migration ?node_name ?client_name ~blocks_per_cycle @@ -88,36 +117,26 @@ let perform_protocol_migration ?node_name ?client_name ~blocks_per_cycle repeat (migration_level - 1) (fun () -> Client.bake_for_and_wait client) in (* Ensure that the block before migration *) - let* pre_migration_block = - Client.RPC.call client - @@ RPC.get_chain_block_metadata ~block:(Int.to_string migration_level) () - in Log.info "Checking migration block consistency" ; - Check.( - (pre_migration_block.protocol = Protocol.hash migrate_from) - string - ~error_msg:"expected protocol = %R, got %L") ; - Check.( - (pre_migration_block.next_protocol = Protocol.hash migrate_to) - string - ~error_msg:"expected next_protocol = %R, got %L") ; + let* () = + block_check + ~expected_block_type:`Migration + client + ~migrate_from + ~migrate_to + ~level:migration_level + in let* () = Client.bake_for_and_wait client in (* Ensure that we migrated *) - let* migration_block = - Client.RPC.call client - @@ RPC.get_chain_block_metadata - ~block:(Int.to_string (migration_level + 1)) - () - in Log.info "Checking migration block consistency" ; - Check.( - (migration_block.protocol = Protocol.hash migrate_to) - string - ~error_msg:"expected protocol = %R, got %L") ; - Check.( - (migration_block.next_protocol = Protocol.hash migrate_to) - string - ~error_msg:"expected next_protocol = %R, got %L") ; + let* () = + block_check + ~expected_block_type:`Non_migration + client + ~migrate_from + ~migrate_to + ~level:(migration_level + 1) + in (* Test that we can still bake after migration *) let* () = repeat baked_blocks_after_migration (fun () -> @@ -298,35 +317,6 @@ let test_migration_with_snapshots ~migrate_from ~migrate_to = let* () = synchronize node1 [node0; node2; node3] in unit -(** [block_check ~level ~expected_block_type ~migrate_to ~migrate_from client] - is generic check that a block of type [expected_block_type] contains - (protocol) metatadata conforming to its type at [level]. **) -let block_check ?level ~expected_block_type ~migrate_to ~migrate_from client = - let block = - match level with Some level -> Some (string_of_int level) | None -> None - in - let* metadata = - Client.RPC.call client @@ RPC.get_chain_block_metadata ?block () - in - let protocol = metadata.protocol in - let next_protocol = metadata.next_protocol in - (match expected_block_type with - | `Migration -> - Check.( - (next_protocol = Protocol.hash migrate_to) - string - ~error_msg:"expected next protocol to be %R, got %L") ; - Check.( - (protocol = Protocol.hash migrate_from) - string - ~error_msg:"expected (from) protocol to be %R, got %L") - | `Non_migration -> - Check.( - (next_protocol = protocol) - string - ~error_msg:"expected a non migration block ")) ; - Lwt.return_unit - (** Number of elements in [l] that satisfy the predicate [p]. *) let list_count_p p l = let rec aux acc = function -- GitLab From 2383bf2f3a9315f9c7eda71f51ca6fec92a1f2ab Mon Sep 17 00:00:00 2001 From: Marina Polubelova Date: Thu, 1 Feb 2024 16:24:52 +0100 Subject: [PATCH 3/3] Tezt/Tezos: add test for a new consensus_rights_delay constant Check that baking_rights are only computed for the next consensus_rights_delay cycles. --- tezt/tests/protocol_migration.ml | 96 ++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/tezt/tests/protocol_migration.ml b/tezt/tests/protocol_migration.ml index 7886ccd983e6..aea901727d74 100644 --- a/tezt/tests/protocol_migration.ml +++ b/tezt/tests/protocol_migration.ml @@ -96,9 +96,9 @@ let block_check ?level ~expected_block_type ~migrate_to ~migrate_from client = (* Migration to Tenderbake is only supported after the first cycle, therefore at [migration_level >= blocks_per_cycle]. *) -let perform_protocol_migration ?node_name ?client_name ~blocks_per_cycle - ~migration_level ~migrate_from ~migrate_to ~baked_blocks_after_migration () - = +let perform_protocol_migration ?node_name ?client_name ?parameter_file + ~blocks_per_cycle ~migration_level ~migrate_from ~migrate_to + ~baked_blocks_after_migration () = assert (migration_level >= blocks_per_cycle) ; Log.info "Node starting" ; let* client, node = @@ -110,7 +110,9 @@ let perform_protocol_migration ?node_name ?client_name ~blocks_per_cycle () in Log.info "Node %s initialized" (Node.name node) ; - let* () = Client.activate_protocol ~protocol:migrate_from client in + let* () = + Client.activate_protocol ~protocol:migrate_from client ?parameter_file + in Log.info "Protocol %s activated" (Protocol.hash migrate_from) ; (* Bake until migration *) let* () = @@ -930,7 +932,93 @@ let test_forked_migration_bakers ~migrate_from ~migrate_to = in check_blocks ~level_from ~level_to +let check_baking_rights client nb_cycles_to_check ?nb_cycles_is_valid () = + let check_baking_rights_rpc ~expect_failure ~cycle () = + let*? process = + Client.RPC.spawn client + @@ RPC.get_chain_block_helper_baking_rights ~cycle () + in + let* () = Process.check ~expect_failure process in + Log.info + "Checked the baking rights for cycle = %s (expect_failure = %s)" + (Int.to_string cycle) + (Bool.to_string expect_failure) ; + unit + in + + let* level = + Client.RPC.call client @@ RPC.get_chain_block_helper_current_level () + in + let nb_cycles_is_valid = + match nb_cycles_is_valid with Some x -> x | None -> nb_cycles_to_check + in + Lwt_list.iter_s + (fun cycle -> + let expect_failure = cycle > level.cycle + nb_cycles_is_valid in + check_baking_rights_rpc ~expect_failure ~cycle ()) + (range level.cycle (level.cycle + nb_cycles_to_check)) + +let test_migration_from_oxford_for_whole_cycle ~migrate_from ~migrate_to = + if Protocol.number migrate_from = Protocol.number Protocol.Oxford then ( + let parameters = JSON.parse_file (Protocol.parameter_file migrate_to) in + let blocks_per_cycle = JSON.(get "blocks_per_cycle" parameters |> as_int) in + let consensus_rights_delay = + JSON.(get "consensus_rights_delay" parameters |> as_int) + in + let preserved_cycles = 5 in + Log.info + "consensus_rights_delay = %d and preserved_cycles = %d" + consensus_rights_delay + preserved_cycles ; + for migration_level = blocks_per_cycle to 2 * blocks_per_cycle do + Test.register + ~__FILE__ + ~title: + (Printf.sprintf + "protocol migration from Oxford at level %d" + migration_level) + ~tags:["protocol"; "migration"; "sandbox"; "baking_rights"] + @@ fun () -> + let* parameter_file = + let parameters = [(["preserved_cycles"], `Int preserved_cycles)] in + let base = Either.Right (migrate_from, None) in + Protocol.write_parameter_file ~base parameters + in + let* client, _node = + perform_protocol_migration + ~parameter_file + ~blocks_per_cycle + ~migration_level + ~migrate_from + ~migrate_to + ~baked_blocks_after_migration:0 + () + in + let* () = + check_baking_rights + client + preserved_cycles + ~nb_cycles_is_valid:consensus_rights_delay + () + in + (* Test that we can still bake after migration *) + let* () = + repeat + (2 * consensus_rights_delay * blocks_per_cycle) + (fun () -> Client.bake_for_and_wait client) + in + let* () = + check_baking_rights + client + preserved_cycles + ~nb_cycles_is_valid:consensus_rights_delay + () + in + unit + done) + let register ~migrate_from ~migrate_to = + test_migration_from_oxford_for_whole_cycle ~migrate_from ~migrate_to ; test_migration_for_whole_cycle ~migrate_from ~migrate_to ; test_migration_with_bakers ~migrate_from ~migrate_to () ; test_forked_migration_bakers ~migrate_from ~migrate_to ; -- GitLab