Skip to content

Commit

Permalink
refactor: add server settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aoudiamoncef committed Nov 17, 2022
1 parent e88ff2a commit 750a38b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 46 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions massa-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ pub struct APIConfig {
pub max_arguments: u64,
/// openrpc specification path
pub openrpc_spec_path: PathBuf,
/// maximum size in bytes of a request.
pub max_request_body_size: u32,
/// maximum size in bytes of a response.
pub max_response_body_size: u32,
/// maximum number of incoming connections allowed.
pub max_connections: u32,
/// maximum number of subscriptions per connection.
pub max_subscriptions_per_connection: u32,
/// max length for logging for requests and responses. Logs bigger than this limit will be truncated.
pub max_log_length: u32,
/// host filtering.
pub allow_hosts: Vec<String>,
/// whether batch requests are supported by this server or not.
pub batch_requests_supported: bool,
/// the interval at which `Ping` frames are submitted.
pub ping_interval: MassaTime,
/// max datastore value length
pub max_datastore_value_length: u64,
/// max op datastore entry
Expand Down
32 changes: 28 additions & 4 deletions massa-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::error::ApiError::WrongAPI;
use jsonrpsee::core::{Error as JsonRpseeError, RpcResult};
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::server::{ServerBuilder, ServerHandle};
use jsonrpsee::server::{AllowHosts, ServerBuilder, ServerHandle};
use massa_consensus_exports::ConsensusController;
use massa_execution_exports::ExecutionController;
use massa_models::api::{
Expand Down Expand Up @@ -97,12 +97,36 @@ pub struct API<T>(T);
#[async_trait::async_trait]
pub trait RpcServer: MassaRpcServer {
/// Start the API
async fn serve(self, url: &SocketAddr) -> Result<StopHandle, JsonRpseeError>;
async fn serve(
self,
url: &SocketAddr,
api_config: &APIConfig,
) -> Result<StopHandle, JsonRpseeError>;
}

async fn serve(api: impl MassaRpcServer, url: &SocketAddr) -> Result<StopHandle, JsonRpseeError> {
async fn serve(
api: impl MassaRpcServer,
url: &SocketAddr,
api_config: &APIConfig,
) -> Result<StopHandle, JsonRpseeError> {
let allowed_hosts = if api_config.allow_hosts.is_empty() {
AllowHosts::Any
} else {
let hosts = api_config
.allow_hosts
.iter()
.map(|hostname| hostname.into())
.collect();
AllowHosts::Only(hosts)
};

let server = ServerBuilder::new()
.max_request_body_size(50 * 1024 * 1024)
.max_request_body_size(api_config.max_request_body_size)
.max_response_body_size(api_config.max_response_body_size)
.max_connections(api_config.max_connections)
.set_host_filtering(allowed_hosts)
.batch_requests_supported(api_config.batch_requests_supported)
.ping_interval(api_config.ping_interval.to_duration())
.build(url)
.await
.expect("server builder failed");
Expand Down
8 changes: 6 additions & 2 deletions massa-api/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ impl API<Private> {

#[async_trait]
impl RpcServer for API<Private> {
async fn serve(self, url: &SocketAddr) -> Result<StopHandle, JsonRpseeError> {
crate::serve(self, url).await
async fn serve(
self,
url: &SocketAddr,
settings: &APIConfig,
) -> Result<StopHandle, JsonRpseeError> {
crate::serve(self, url, settings).await
}
}

Expand Down
10 changes: 7 additions & 3 deletions massa-api/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ impl API<Public> {

#[async_trait]
impl RpcServer for API<Public> {
async fn serve(self, url: &SocketAddr) -> Result<StopHandle, JsonRpseeError> {
crate::serve(self, url).await
async fn serve(
self,
url: &SocketAddr,
api_config: &APIConfig,
) -> Result<StopHandle, JsonRpseeError> {
crate::serve(self, url, api_config).await
}
}

Expand Down Expand Up @@ -377,7 +381,7 @@ impl MassaRpcServer for API<Public> {
next_slot,
execution_stats,
consensus_stats,
network_stats,
network_stats,
pool_stats,
config,
current_cycle: last_slot
Expand Down
54 changes: 35 additions & 19 deletions massa-node/base_config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@
bind_public = "0.0.0.0:33035"
# max number of arguments per RPC call
max_arguments = 128
# Path to the openrpc specification file used in `rpc.discover` method.
# path to the openrpc specification file used in `rpc.discover` method.
openrpc_spec_path = "base_config/openrpc.json"
# maximum size in bytes of a request.
max_request_body_size = 52428800
# maximum size in bytes of a response.
max_response_body_size = 52428800
# maximum number of incoming connections allowed.
max_connections = 100
# maximum number of subscriptions per connection.
max_subscriptions_per_connection = 1024
# max length for logging for requests and responses. Logs bigger than this limit will be truncated.
max_log_length = 4096
# host filtering.
allow_hosts = []
# whether batch requests are supported by this server or not.
batch_requests_supported = true
# the interval at which `Ping` frames are submitted in milliseconds.
ping_interval = 60000

[execution]
# max number of generated events kept in RAM
Expand Down Expand Up @@ -80,22 +96,22 @@
max_known_endorsements_size = 2048
# max cache size for which endorsements a foreign node knows about
max_node_known_endorsements_size = 2048
# Maximum number of batches in the memory buffer.
# Dismiss the new batches if overflow
# maximum number of batches in the memory buffer.
# dismiss the new batches if overflow
operation_batch_buffer_capacity = 10024
# Immediately announce ops if overflow
# immediately announce ops if overflow
operation_announcement_buffer_capacity = 2000
# Start processing batches in the buffer each `operation_batch_proc_period` in millisecond
# start processing batches in the buffer each `operation_batch_proc_period` in millisecond
operation_batch_proc_period = 500
# All operations asked are prune each `operation_asked_pruning_period` millisecond
# all operations asked are prune each `operation_asked_pruning_period` millisecond
asked_operations_pruning_period = 100000
# Interval at which operations are announced in batches.
# interval at which operations are announced in batches.
operation_announcement_interval = 300
# Max number of operation per message, same as network param but can be smaller
# max number of operation per message, same as network param but can be smaller
max_operations_per_message = 1024
# Time threshold after which operation are not propagated
# time threshold after which operation are not propagated
max_operations_propagation_time = 32000
# Time threshold after which operation are not propagated
# time threshold after which operation are not propagated
max_endorsements_propagation_time = 48000

[network]
Expand Down Expand Up @@ -141,15 +157,15 @@
max_send_wait_network_event = 0
# we forget we banned a node after ban_timeout milliseconds
ban_timeout = 3600000
# Timeout duration when in handshake we respond with a PeerList
# timeout duration when in handshake we respond with a PeerList
# (on max in connection reached we send a list of peers)
peer_list_send_timeout = 100
# Max number of in connection overflowed managed by the handshake
# max number of in connection overflowed managed by the handshake
# that send a list of peers
max_in_connection_overflow = 100
# Read limitation for a connection in bytes per seconds
# read limitation for a connection in bytes per seconds
max_bytes_read = 20_000_000.0
# Write limitation for a connection in bytes per seconds
# write limitation for a connection in bytes per seconds
max_bytes_write = 20_000_000.0

[network.peer_types_config]
Expand All @@ -169,9 +185,9 @@
["54.36.174.177:31245", "P1gEdBVEbRFbBxBtrjcTDDK9JPbJFDay27uiJRE3vmbFAFDKNh7"],
["51.75.60.228:31245", "P13Ykon8Zo73PTKMruLViMMtE2rEG646JQ4sCcee2DnopmVM3P5"]
]
# Path to the bootstrap whitelist file. This whitelist define IPs that can bootstrap on your node.
# path to the bootstrap whitelist file. This whitelist define IPs that can bootstrap on your node.
bootstrap_whitelist_file = "base_config/bootstrap_whitelist.json"
# Path to the bootstrap blacklist file. This whitelist define IPs that will not be able to bootstrap on your node. This list is optional.
# path to the bootstrap blacklist file. This whitelist define IPs that will not be able to bootstrap on your node. This list is optional.
bootstrap_blacklist_file = "base_config/bootstrap_blacklist.json"
# [optionnal] port on which to listen for incoming bootstrap requests
bind = "[::]:31245"
Expand Down Expand Up @@ -199,7 +215,7 @@
ip_list_max_size = 10000
# refuse consecutive bootstrap attempts from a given IP when the interval between them is lower than per_ip_min_interval milliseconds
per_ip_min_interval = 180000
# Read-Write limitation for a connection in bytes per seconds (about the bootstrap specifically)
# read-write limitation for a connection in bytes per seconds (about the bootstrap specifically)
max_bytes_read_write = 20_000_000.0

[pool]
Expand All @@ -213,9 +229,9 @@
max_item_return_count = 100

[selector]
# Maximum number of computed cycle's draws we keep in cache
# maximum number of computed cycle's draws we keep in cache
max_draw_cache = 10
# Path to the initial roll distribution
# path to the initial roll distribution
initial_rolls_path = "base_config/initial_rolls.json"

[factory]
Expand Down
14 changes: 11 additions & 3 deletions massa-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ async fn launch(
draw_lookahead_period_count: SETTINGS.api.draw_lookahead_period_count,
max_arguments: SETTINGS.api.max_arguments,
openrpc_spec_path: SETTINGS.api.openrpc_spec_path.clone(),
max_request_body_size: SETTINGS.api.max_request_body_size,
max_response_body_size: SETTINGS.api.max_response_body_size,
max_connections: SETTINGS.api.max_connections,
max_subscriptions_per_connection: SETTINGS.api.max_subscriptions_per_connection,
max_log_length: SETTINGS.api.max_log_length,
allow_hosts: SETTINGS.api.allow_hosts.clone(),
batch_requests_supported: SETTINGS.api.batch_requests_supported,
ping_interval: SETTINGS.api.ping_interval,
max_datastore_value_length: MAX_DATASTORE_VALUE_LENGTH,
max_op_datastore_entry_count: MAX_OPERATION_DATASTORE_ENTRY_COUNT,
max_op_datastore_key_length: MAX_OPERATION_DATASTORE_KEY_LENGTH,
Expand All @@ -498,15 +506,15 @@ async fn launch(
node_wallet,
);
let api_private_handle = api_private
.serve(&SETTINGS.api.bind_private)
.serve(&SETTINGS.api.bind_private, &api_config)
.await
.expect("failed to start PRIVATE API");

// spawn public API
let api_public = API::<Public>::new(
consensus_controller.clone(),
execution_controller.clone(),
api_config,
api_config.clone(),
selector_controller.clone(),
pool_controller.clone(),
ProtocolCommandSender(protocol_command_sender.clone()),
Expand All @@ -518,7 +526,7 @@ async fn launch(
shared_storage.clone(),
);
let api_public_handle = api_public
.serve(&SETTINGS.api.bind_public)
.serve(&SETTINGS.api.bind_public, &api_config)
.await
.expect("failed to start PUBLIC API");

Expand Down
10 changes: 9 additions & 1 deletion massa-node/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,22 @@ pub struct PoolSettings {
pub max_item_return_count: usize,
}

/// API configuration, read from a file configuration
/// API and server configuration, read from a file configuration.
#[derive(Debug, Deserialize, Clone)]
pub struct APISettings {
pub draw_lookahead_period_count: u64,
pub bind_private: SocketAddr,
pub bind_public: SocketAddr,
pub max_arguments: u64,
pub openrpc_spec_path: PathBuf,
pub max_request_body_size: u32,
pub max_response_body_size: u32,
pub max_connections: u32,
pub max_subscriptions_per_connection: u32,
pub max_log_length: u32,
pub allow_hosts: Vec<String>,
pub batch_requests_supported: bool,
pub ping_interval: MassaTime,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down
4 changes: 1 addition & 3 deletions massa-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ impl RpcClient {
/// Default constructor
pub async fn from_url(url: &str) -> RpcClient {
match HttpClientBuilder::default().build(url) {
Ok(http_client) => RpcClient {
http_client,
},
Ok(http_client) => RpcClient { http_client },
Err(_) => panic!("unable to connect to Node."),
}
}
Expand Down

0 comments on commit 750a38b

Please sign in to comment.