Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer use the log crate in the light client #1560

Merged
merged 14 commits into from
Jan 8, 2024
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions light-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ futures-util = { version = "0.3.27", default-features = false, features = ["allo
hashbrown = { version = "0.14.0", default-features = false }
hex = { version = "0.4.3", default-features = false }
itertools = { version = "0.12.0", default-features = false, features = ["use_alloc"] }
log = { version = "0.4.18", default-features = false }
lru = { version = "0.12.0", default-features = false, features = ["hashbrown"] } # The `hashbrown` feature brings no-std compatibility.
no-std-net = { version = "0.6.0", default-features = false }
pin-project = "1.1.3"
Expand All @@ -43,11 +42,12 @@ zeroize = { version = "1.6.0", default-features = false, features = ["alloc"] }
# `std` feature
# Add here the crates that cannot function without the help of the operating system or environment.
parking_lot = { version = "0.12.1", optional = true }
log = { version = "0.4.18", default-features = false, optional = true }
smol = { version = "2.0.0", optional = true }

[features]
default = ["std", "wasmtime"]
std = ["dep:parking_lot", "dep:smol", "rand/std", "rand/std_rng", "smoldot/std"]
std = ["dep:parking_lot", "dep:log", "dep:smol", "rand/std", "rand/std_rng", "smoldot/std"]
wasmtime = ["smoldot/wasmtime"]

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion light-base/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::{iter, num::NonZeroU32};
use futures_lite::FutureExt as _;

fn main() {
// The `smoldot_light` library uses the `log` crate to emit logs.
// The `DefaultPlatform` that we use below uses the `log` crate to emit logs.
// We need to register some kind of logs listener, in this example `env_logger`.
// See also <https://docs.rs/log>.
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
Expand Down
43 changes: 27 additions & 16 deletions light-base/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
mod background;

use crate::{
network_service, platform::PlatformRef, runtime_service, sync_service, transactions_service,
log, network_service, platform::PlatformRef, runtime_service, sync_service,
transactions_service,
};

use alloc::{
Expand All @@ -52,7 +53,10 @@ use core::num::NonZeroU32;
use smoldot::{chain_spec, json_rpc::service};

/// Configuration for [`service()`].
pub struct Config {
pub struct Config<TPlat: PlatformRef> {
/// Access to the platform's capabilities.
pub platform: TPlat,

/// Name of the chain, for logging purposes.
///
/// > **Note**: This name will be directly printed out. Any special character should already
Expand Down Expand Up @@ -86,7 +90,7 @@ pub struct Config {
/// be initialized using [`ServicePrototype::start`].
///
/// Destroying the [`Frontend`] automatically shuts down the service.
pub fn service(config: Config) -> (Frontend, ServicePrototype) {
pub fn service<TPlat: PlatformRef>(config: Config<TPlat>) -> (Frontend<TPlat>, ServicePrototype) {
let log_target = format!("json-rpc-{}", config.log_name);

let (requests_processing_task, requests_responses_io) =
Expand All @@ -96,6 +100,7 @@ pub fn service(config: Config) -> (Frontend, ServicePrototype) {
});

let frontend = Frontend {
platform: config.platform,
log_target: log_target.clone(),
requests_responses_io: Arc::new(requests_responses_io),
};
Expand All @@ -116,7 +121,10 @@ pub fn service(config: Config) -> (Frontend, ServicePrototype) {
///
/// Destroying all the [`Frontend`]s automatically shuts down the associated service.
#[derive(Clone)]
pub struct Frontend {
pub struct Frontend<TPlat> {
/// See [`Config::platform`].
platform: TPlat,

/// Sending requests and receiving responses.
///
/// Connected to the [`background`].
Expand All @@ -126,7 +134,7 @@ pub struct Frontend {
log_target: String,
}

impl Frontend {
impl<TPlat: PlatformRef> Frontend<TPlat> {
/// Queues the given JSON-RPC request to be processed in the background.
///
/// An error is returned if [`Config::max_pending_requests`] is exceeded, which can happen
Expand All @@ -142,10 +150,12 @@ impl Frontend {
.try_send_request(json_rpc_request)
{
Ok(()) => {
log::debug!(
target: &self.log_target,
"JSON-RPC => {}",
log_friendly_request
log!(
&self.platform,
Debug,
&self.log_target,
"json-rpc-request-queued",
request = log_friendly_request
);
Ok(())
}
Expand All @@ -172,13 +182,13 @@ impl Frontend {
Err(service::WaitNextResponseError::ClientMainTaskDestroyed) => unreachable!(),
};

log::debug!(
target: &self.log_target,
"JSON-RPC <= {}",
crate::util::truncated_str(
message.chars().filter(|c| !c.is_control()),
250,
)
log!(
&self.platform,
Debug,
&self.log_target,
"json-rpc-response-yielded",
response =
crate::util::truncated_str(message.chars().filter(|c| !c.is_control()), 250,)
);

message
Expand All @@ -202,6 +212,7 @@ pub struct ServicePrototype {
/// Configuration for a JSON-RPC service.
pub struct StartConfig<'a, TPlat: PlatformRef> {
/// Access to the platform's capabilities.
// TODO: redundant with Config above?
pub platform: TPlat,

/// Access to the network, and identifier of the chain from the point of view of the network
Expand Down
72 changes: 42 additions & 30 deletions light-base/src/json_rpc_service/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::{
network_service, platform::PlatformRef, runtime_service, sync_service, transactions_service,
util,
log, network_service, platform::PlatformRef, runtime_service, sync_service,
transactions_service, util,
};

use super::StartConfig;
Expand Down Expand Up @@ -325,16 +325,20 @@ impl<TPlat: PlatformRef> Background<TPlat> {
.printed_legacy_json_rpc_warning
.swap(true, atomic::Ordering::Relaxed)
{
log::warn!(
target: &self.log_target,
"The JSON-RPC client has just called a JSON-RPC function from the legacy \
JSON-RPC API ({}). Legacy JSON-RPC functions have loose semantics and \
cannot be properly implemented on a light client. You are encouraged to \
use the new JSON-RPC API \
<https://github.com/paritytech/json-rpc-interface-spec/> instead. The \
legacy JSON-RPC API functions will be deprecated and removed in the \
distant future.",
request.request().name()
log!(
&self.platform,
Warn,
&self.log_target,
format!(
"The JSON-RPC client has just called a JSON-RPC function from \
the legacy JSON-RPC API ({}). Legacy JSON-RPC functions have \
loose semantics and cannot be properly implemented on a light \
client. You are encouraged to use the new JSON-RPC API \
<https://github.com/paritytech/json-rpc-interface-spec/> instead. The \
legacy JSON-RPC API functions will be deprecated and removed in the \
distant future.",
request.request().name()
)
)
}
}
Expand Down Expand Up @@ -503,7 +507,12 @@ impl<TPlat: PlatformRef> Background<TPlat> {
| methods::MethodCall::system_networkState { .. }
| methods::MethodCall::system_removeReservedPeer { .. }) => {
// TODO: implement the ones that make sense to implement ^
log::error!(target: &self.log_target, "JSON-RPC call not supported yet: {:?}", _method);
log!(
&self.platform,
Warn,
&self.log_target,
format!("JSON-RPC call not supported yet: {:?}", _method)
);
request.fail(json_rpc::parse::ErrorResponse::ServerError(
-32000,
"Not implemented in smoldot yet",
Expand All @@ -520,12 +529,6 @@ impl<TPlat: PlatformRef> Background<TPlat> {
request: service::SubscriptionStartProcess,
) {
// TODO: restore some form of logging
/*log::debug!(target: &self.log_target, "PendingRequestsQueue => {}",
crate::util::truncated_str(
json_rpc_request.chars().filter(|c| !c.is_control()),
100,
)
);*/

// Print a warning for legacy JSON-RPC functions.
match request.request() {
Expand Down Expand Up @@ -593,16 +596,20 @@ impl<TPlat: PlatformRef> Background<TPlat> {
.printed_legacy_json_rpc_warning
.swap(true, atomic::Ordering::Relaxed)
{
log::warn!(
target: &self.log_target,
"The JSON-RPC client has just called a JSON-RPC function from the legacy \
JSON-RPC API ({}). Legacy JSON-RPC functions have loose semantics and \
cannot be properly implemented on a light client. You are encouraged to \
use the new JSON-RPC API \
<https://github.com/paritytech/json-rpc-interface-spec/> instead. The \
legacy JSON-RPC API functions will be deprecated and removed in the \
distant future.",
request.request().name()
log!(
&self.platform,
Warn,
&self.log_target,
format!(
"The JSON-RPC client has just called a JSON-RPC function from \
the legacy JSON-RPC API ({}). Legacy JSON-RPC functions have \
loose semantics and cannot be properly implemented on a light \
client. You are encouraged to use the new JSON-RPC API \
<https://github.com/paritytech/json-rpc-interface-spec/> instead. The \
legacy JSON-RPC API functions will be deprecated and removed in the \
distant future.",
request.request().name()
)
)
}
}
Expand Down Expand Up @@ -650,7 +657,12 @@ impl<TPlat: PlatformRef> Background<TPlat> {

_method @ methods::MethodCall::network_unstable_subscribeEvents { .. } => {
// TODO: implement the ones that make sense to implement ^
log::error!(target: &self.log_target, "JSON-RPC call not supported yet: {:?}", _method);
log!(
&self.platform,
Warn,
&self.log_target,
format!("JSON-RPC call not supported yet: {:?}", _method)
);
request.fail(json_rpc::parse::ErrorResponse::ServerError(
-32000,
"Not implemented in smoldot yet",
Expand Down
14 changes: 9 additions & 5 deletions light-base/src/json_rpc_service/background/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::Background;

use crate::{platform::PlatformRef, runtime_service, sync_service};
use crate::{log, platform::PlatformRef, runtime_service, sync_service};

use alloc::{
borrow::ToOwned as _,
Expand Down Expand Up @@ -93,8 +93,10 @@ impl<TPlat: PlatformRef> Background<TPlat> {
// JSON-RPC client implementations are made aware of this limit. This number of 2 might
// be relaxed and/or configurable in the future.
if lock.len() >= 2 {
log::warn!(
target: &self.log_target,
log!(
&self.platform,
Warn,
&self.log_target,
"Rejected `chainHead_unstable_follow` subscription due to limit reached."
);
request.fail(json_rpc::parse::ErrorResponse::ApplicationDefined(
Expand Down Expand Up @@ -1031,8 +1033,10 @@ impl<TPlat: PlatformRef> ChainHeadFollowTask<TPlat> {
-32000,
"Child key storage queries not supported yet",
));
log::warn!(
target: &self.log_target,
log!(
&self.platform,
Warn,
&self.log_target,
"chainHead_unstable_storage has been called with a non-null childTrie. \
This isn't supported by smoldot yet."
);
Expand Down
Loading
Loading