|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +import urllib.parse |
| 7 | +from update_config_and_restart_nodes_lib import ( |
| 8 | + ApolloArgsParserBuilder, |
| 9 | + RestartStrategy, |
| 10 | + Service, |
| 11 | + get_context_list_from_args, |
| 12 | + get_current_block_number, |
| 13 | + get_logs_explorer_url, |
| 14 | + get_namespace_list_from_args, |
| 15 | + print_colored, |
| 16 | + print_error, |
| 17 | + update_config_and_restart_nodes, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +# TODO(guy.f): Remove this once we have metrics we use to decide based on. |
| 22 | +def get_logs_explorer_url_for_revert( |
| 23 | + namespace: str, |
| 24 | + block_number: int, |
| 25 | + project_name: str, |
| 26 | +) -> str: |
| 27 | + query = ( |
| 28 | + f'resource.labels.namespace_name:"{urllib.parse.quote(namespace)}"\n' |
| 29 | + f'resource.labels.container_name="sequencer-core"\n' |
| 30 | + f'textPayload =~ "Done reverting.*storage up to height {block_number}"' |
| 31 | + ) |
| 32 | + return get_logs_explorer_url(query, project_name) |
| 33 | + |
| 34 | + |
| 35 | +def set_revert_mode( |
| 36 | + namespace_list: list[str], |
| 37 | + context_list: Optional[list[str]], |
| 38 | + project_name: str, |
| 39 | + should_revert: bool, |
| 40 | + revert_up_to_block: int, |
| 41 | + restart_strategy: RestartStrategy, |
| 42 | +): |
| 43 | + config_overrides = { |
| 44 | + "revert_config.should_revert": should_revert, |
| 45 | + "revert_config.revert_up_to_and_including": revert_up_to_block, |
| 46 | + } |
| 47 | + |
| 48 | + post_restart_instructions = [] |
| 49 | + for namespace, context in zip(namespace_list, context_list): |
| 50 | + url = get_logs_explorer_url_for_revert(namespace, revert_up_to_block, project_name) |
| 51 | + |
| 52 | + post_restart_instructions.append( |
| 53 | + f"Please check logs and verify that revert has completed (both in the batcher and for sync). Logs URL: {url}" |
| 54 | + ) |
| 55 | + |
| 56 | + update_config_and_restart_nodes( |
| 57 | + config_overrides, |
| 58 | + namespace_list, |
| 59 | + Service.Core, |
| 60 | + context_list, |
| 61 | + restart_strategy, |
| 62 | + post_restart_instructions, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def main(): |
| 67 | + usage_example = """ |
| 68 | +Examples: |
| 69 | + # Set revert mode up to a specific block |
| 70 | + %(prog)s --namespace apollo-sepolia-integration --num-nodes 3 -t all_at_once --revert-only --revert_up_to_block 12345 |
| 71 | + %(prog)s -n apollo-sepolia-integration -N 3 -t one_by_one --revert-only -b 12345 |
| 72 | + |
| 73 | + # Set revert mode using feeder URL to get current block |
| 74 | + %(prog)s --namespace apollo-sepolia-integration --num-nodes 3 -t all_at_once --revert-only --feeder-url feeder.integration-sepolia.starknet.io |
| 75 | + %(prog)s -n apollo-sepolia-integration -N 3 -t one_by_one --revert-only -f feeder.integration-sepolia.starknet.io |
| 76 | + |
| 77 | + # Disable revert mode |
| 78 | + %(prog)s --namespace apollo-sepolia-integration --num-nodes 3 -t all_at_once --disable-revert-only |
| 79 | + %(prog)s -n apollo-sepolia-integration -N 3 -t one_by_one --disable-revert-only |
| 80 | + |
| 81 | + # Set revert mode with cluster prefix |
| 82 | + %(prog)s -n apollo-sepolia-integration -N 3 -c my-cluster -t all_at_once --revert-only -b 12345 |
| 83 | + |
| 84 | + # Set revert mode with feeder URL and cluster prefix |
| 85 | + %(prog)s -n apollo-sepolia-integration -N 3 -c my-cluster -t one_by_one --revert-only -f feeder.integration-sepolia.starknet.io |
| 86 | + |
| 87 | + # Disable revert mode without restarting nodes |
| 88 | + %(prog)s -n apollo-sepolia-integration -N 3 -t no_restart --disable-revert-only |
| 89 | + |
| 90 | + # Set revert mode with explicit restart |
| 91 | + %(prog)s -n apollo-sepolia-integration -N 3 -t all_at_once --revert-only -b 12345 |
| 92 | + |
| 93 | + # Set revert mode with feeder URL and explicit restart |
| 94 | + %(prog)s -n apollo-sepolia-integration -N 3 -t one_by_one --revert-only -f feeder.integration-sepolia.starknet.io |
| 95 | + |
| 96 | + # Set revert mode starting from specific node index |
| 97 | + %(prog)s -n apollo-sepolia-integration -N 3 -i 5 -t all_at_once --revert-only -b 12345 |
| 98 | + |
| 99 | + # Set revert mode with feeder URL starting from specific node index |
| 100 | + %(prog)s -n apollo-sepolia-integration -N 3 -i 5 -t one_by_one --revert-only -f feeder.integration-sepolia.starknet.io |
| 101 | + """ |
| 102 | + |
| 103 | + args_builder = ApolloArgsParserBuilder( |
| 104 | + "Sets or unsets the revert mode for the sequencer nodes", usage_example |
| 105 | + ) |
| 106 | + |
| 107 | + revert_group = args_builder.parser.add_mutually_exclusive_group() |
| 108 | + revert_group.add_argument("--revert-only", action="store_true", help="Enable revert mode") |
| 109 | + revert_group.add_argument( |
| 110 | + "--disable-revert-only", action="store_true", help="Disable revert mode" |
| 111 | + ) |
| 112 | + |
| 113 | + args_builder.add_argument( |
| 114 | + "-b", |
| 115 | + "--revert-up-to-block", |
| 116 | + type=int, |
| 117 | + help="Block number up to which to revert (inclusive). Must be a positive integer.", |
| 118 | + ) |
| 119 | + |
| 120 | + args_builder.add_argument( |
| 121 | + "-f", |
| 122 | + "--feeder-url", |
| 123 | + type=str, |
| 124 | + help="The feeder URL to get the current block from. We will revert all blocks above it.", |
| 125 | + ) |
| 126 | + |
| 127 | + # TODO(guy.f): Remove this when we rely on metrics for restarting. |
| 128 | + args_builder.add_argument( |
| 129 | + "--project-name", |
| 130 | + help="The name of the project to get logs from. If One_By_One strategy is used, this is required.", |
| 131 | + ) |
| 132 | + |
| 133 | + args = args_builder.build() |
| 134 | + |
| 135 | + if args.restart_strategy == RestartStrategy.ONE_BY_ONE and args.project_name is None: |
| 136 | + print_error("Error: --project-name is required when using One_By_One strategy") |
| 137 | + sys.exit(1) |
| 138 | + |
| 139 | + should_revert = not args.disable_revert_only |
| 140 | + if should_revert: |
| 141 | + if args.feeder_url is None and args.revert_up_to_block is None: |
| 142 | + print_error( |
| 143 | + "Error: Either --feeder-url or --revert_up_to_block (-b) are required when reverting is requested." |
| 144 | + ) |
| 145 | + sys.exit(1) |
| 146 | + if args.feeder_url is not None and args.revert_up_to_block is not None: |
| 147 | + print_error("Error: Cannot specify both --feeder-url and --revert_up_to_block (-b).") |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + if args.disable_revert_only: |
| 151 | + if args.feeder_url is not None: |
| 152 | + print_error("Error: --feeder-url cannot be set when using --disable-revert-only") |
| 153 | + sys.exit(1) |
| 154 | + if args.revert_up_to_block is not None: |
| 155 | + print_error("Error: --revert-up-to-block (-b) cannot be set when disabling revert.") |
| 156 | + sys.exit(1) |
| 157 | + |
| 158 | + namespace_list = get_namespace_list_from_args(args) |
| 159 | + context_list = get_context_list_from_args(args) |
| 160 | + |
| 161 | + should_disable_revert = not args.revert_only |
| 162 | + if should_revert: |
| 163 | + revert_up_to_block = ( |
| 164 | + args.revert_up_to_block |
| 165 | + if args.revert_up_to_block is not None |
| 166 | + else get_current_block_number(args.feeder_url) |
| 167 | + ) |
| 168 | + f"\nEnabling revert mode up to (and including) block {revert_up_to_block}" |
| 169 | + set_revert_mode( |
| 170 | + namespace_list, |
| 171 | + context_list, |
| 172 | + args.project_name, |
| 173 | + True, |
| 174 | + revert_up_to_block, |
| 175 | + args.restart_strategy, |
| 176 | + ) |
| 177 | + if should_disable_revert: |
| 178 | + print_colored(f"\nDisabling revert mode") |
| 179 | + # Setting to max block to max u64. |
| 180 | + set_revert_mode( |
| 181 | + namespace_list, |
| 182 | + context_list, |
| 183 | + args.project_name, |
| 184 | + False, |
| 185 | + 18446744073709551615, |
| 186 | + args.restart_strategy, |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
0 commit comments