movementlabsxyz / movement

The Movement Network is a Move-based L2 on Ethereum.
Apache License 2.0
76 stars 62 forks source link

Use `clap` to initialise vars for all services #110

Closed 0xmovses closed 3 months ago

0xmovses commented 5 months ago

Currently we are using env vars to initialise all the our Config structs. For eg:


    pub mod env_vars {
        pub const CHAIN_ID: &'static str = "MAPTOS_CHAIN_ID";
        pub const OPT_LISTEN_ADDR: &'static str = "MAPTOS_OPT_LISTEN_ADDR";
        pub const FIN_LISTEN_ADDR: &'static str = "MAPTOS_FIN_LISTEN_ADDR";
        pub const FAUCET_LISTEN_ADDR: &'static str = "MAPTOS_FAUCET_LISTEN_ADDR";
        pub const PRIVATE_KEY: &'static str = "MAPTOS_PRIVATE_KEY";
        pub const PUBLIC_KEY: &'static str = "MAPTOS_PUBLIC_KEY";
        pub const DB_PATH: &'static str = "MAPTOS_DB_PATH";
    }

    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct Config {
        pub chain_id: ChainId,
        pub opt_listen_url: String,
        pub fin_listen_url: String,
        pub faucet_listen_url: String,
        pub private_key: Ed25519PrivateKey,
        pub public_key: Ed25519PublicKey,
        pub db_path: PathBuf,
    }

Then we use a try_from_env method to pass the values into the new methods. I'd prefer to use a CLI to initialise these ars with flags so that we can do cargo run suzuka-full-node -- --chain-id=42 opt_listen_url="url" ... This way we will get the benefit of claps helpful error handling nice ways to set defaults, type checking on arg values etc. And we get a cargo run suzuka-full-node --help output out of the box.

Here's an example from the fuel indexer repo:

pub struct IndexerArgs {
    /// Log level passed to the Fuel Indexer service.
    #[clap(long, default_value = defaults::LOG_LEVEL, value_parser(["info", "debug", "error", "warn"]), help = "Log level passed to the Fuel Indexer service.")]
    pub log_level: String,

    /// Indexer service config file.
    #[clap(
        short,
        long,
        value_name = "FILE",
        help = "Indexer service config file."
    )]
    pub config: Option<PathBuf>,

    /// Indexer config file.
    #[clap(short, long, value_name = "FILE", help = "Indexer config file.")]
    pub manifest: Option<PathBuf>,

    /// Host of the running Fuel node.
    #[clap(
        long,
        help = "Host of the running Fuel node.",
        default_value = defaults::FUEL_NODE_HOST
    )]
    pub fuel_node_host: String,

    /// Listening port of the running Fuel node.
    #[clap(
        long,
        help = "Listening port of the running Fuel node.",
        default_value = defaults::FUEL_NODE_PORT
    )]
    pub fuel_node_port: String,

    /// Web API host.
    #[clap(long, help = "Web API host.", default_value = defaults::WEB_API_HOST)]
    pub web_api_host: String,

    /// Web API port.
    #[clap(long, help = "Web API port.", default_value = defaults::WEB_API_PORT)]
    pub web_api_port: String,

    /// Database type.
    #[clap(long, help = "Database type.", default_value = defaults::DATABASE, value_parser(["postgres"]))]
    pub database: String,

    /// The maximum number of database connections.
    #[clap(long, help = "The maximum number of database connections.", default_value_t = defaults::MAX_DB_CONNECTIONS)]
    pub max_db_connections: u32,

    /// Max body size for web server requests.
    #[clap(long, help = "Max body size for web server requests.", default_value_t = defaults::MAX_BODY_SIZE )]
    pub max_body_size: usize,

    /// Postgres username.
    #[clap(long, help = "Postgres username.")]
    pub postgres_user: Option<String>,

    /// Postgres database.
    #[clap(long, help = "Postgres database.")]
    pub postgres_database: Option<String>,

    /// Postgres password.
    #[clap(long, help = "Postgres password.")]
    pub postgres_password: Option<String>,

    /// Postgres host.
    #[clap(long, help = "Postgres host.")]
    pub postgres_host: Option<String>,

    /// Postgres port.
    #[clap(long, help = "Postgres port.")]
    pub postgres_port: Option<String>,

    /// Run database migrations before starting service.
    #[clap(long, help = "Run database migrations before starting service.")]
    pub run_migrations: bool,

    /// Use Prometheus metrics reporting.
    #[clap(long, help = "Use Prometheus metrics reporting.")]
    pub metrics: bool,

    /// Prevent indexers from running without handling any blocks.
    #[clap(
        long,
        help = "Prevent indexers from running without handling any blocks."
    )]
    pub stop_idle_indexers: bool,

    /// Automatically create and start database using provided options or defaults.
    #[clap(
        long,
        help = "Automatically create and start database using provided options or defaults."
    )]
    pub embedded_database: bool,

    /// Require users to authenticate for some operations.
    #[clap(long, help = "Require users to authenticate for some operations.")]
    pub auth_enabled: bool,

    /// Authentication scheme used.
    #[clap(long, help = "Authentication scheme used.")]
    pub auth_strategy: Option<String>,

    /// Secret used for JWT scheme (if JWT scheme is specified).
    #[clap(
        long,
        help = "Secret used for JWT scheme (if JWT scheme is specified)."
    )]
    pub jwt_secret: Option<String>,

    /// Issuer of JWT claims (if JWT scheme is specified).
    #[clap(long, help = "Issuer of JWT claims (if JWT scheme is specified).")]
    pub jwt_issuer: Option<String>,

    /// Amount of time (seconds) before expiring token (if JWT scheme is specified).
    #[clap(
        long,
        help = "Amount of time (seconds) before expiring token (if JWT scheme is specified)."
    )]
    pub jwt_expiry: Option<usize>,

    /// Enable verbose logging.
    #[clap(short, long, help = "Enable verbose logging.")]
    pub verbose: bool,

    /// Start a local Fuel node.
    #[clap(long, help = "Start a local Fuel node.")]
    pub local_fuel_node: bool,

    /// Allow network configuration via indexer manifests.
    #[clap(long, help = "Allow network configuration via indexer manifests.")]
    pub indexer_net_config: bool,

    /// Enable rate limiting.
    #[clap(long, help = "Enable rate limiting.")]
    pub rate_limit: bool,

    /// Maximum number of requests to allow over --rate-limit-window..
    #[clap(
        long,
        help = "Maximum number of requests to allow over --rate-limit-window.."
    )]
    pub rate_limit_request_count: Option<u64>,

    /// Number of seconds over which to allow --rate-limit-rps.
    #[clap(long, help = "Number of seconds over which to allow --rate-limit-rps.")]
    pub rate_limit_window_size: Option<u64>,

    /// The number of WASM opcodes after which the indexer's event handler will stop execution.
    #[clap(
        long,
        help = "The number of WASM opcodes after which the indexer's event handler will stop execution.",
        default_value_t = defaults::METERING_POINTS
    )]
    pub metering_points: u64,

    /// Whether to allow replacing an existing indexer. If not specified, an attempt to deploy over an existing indexer results in an error.
    #[clap(
        long,
        help = "Whether to allow replacing an existing indexer. If not specified, an attempt to deploy over an existing indexer results in an error."
    )]
    pub replace_indexer: bool,

    /// When replacing an indexer, also remove the indexed data.
    #[clap(
        long,
        help = "When replacing an indexer, also remove the indexed data."
    )]
    pub remove_data: bool,

    /// Allow the web server to accept raw SQL queries.
    #[clap(long, help = "Allow the web server to accept raw SQL queries.")]
    pub accept_sql_queries: bool,

    /// Amount of blocks to return in a request to a Fuel node.
    #[clap(long, help = "Amount of blocks to return in a request to a Fuel node.", default_value_t = defaults::NODE_BLOCK_PAGE_SIZE)]
    pub block_page_size: usize,

    /// Allow missing blocks or non-sequential block processing.
    #[clap(
        long,
        help = "Allow missing blocks or non-sequential block processing."
    )]
    pub allow_non_sequential_blocks: bool,

    /// By default, Fuel Indexer will only accept WASM indexer modules compiled with the same toolchain version as the version of Fuel Indexer.
    #[clap(
        long,
        help = "By default, Fuel Indexer will only accept WASM indexer modules compiled with the same toolchain version as the version of Fuel Indexer."
    )]
    pub disable_toolchain_version_check: bool,

    /// Make the service wait for the given duration between block requests to a Fuel client.
    #[clap(
        long,
        help = "Make the service wait for the given duration between block requests to a Fuel client."
    )]
    pub client_request_delay: Option<u64>,

    /// Use a network alias when connecting to a Fuel client.
    #[clap(long, value_parser(["beta-3", "beta-4", "beta-5"]), help = "Use a network alias when connecting to a Fuel client.")]
    pub network: Option<String>,
}
l-monninger commented 4 months ago

We will also be able to write health checks into our service binary--which tends to be nice for process orchestration.

0xmovses commented 3 months ago

Stale