From 5046272cc5afb554e4bd694b142bd0dfb0678808 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 24 Oct 2022 12:52:45 +0200 Subject: [PATCH] Print a warning if a legacy JSON-RPC function is used (#2907) cc https://github.com/paritytech/smoldot/issues/2804 Most legacy JSON-RPC functions can't be implemented properly by smoldot. The situation kind of sucks, as the new JSON-RPC functions aren't stable and aren't implemented in Substrate yet. However, the fact that the replacement isn't ready yet doesn't remove the fact that the old ones don't work properly. Therefore I think it's appropriate to print this warning. The main reason why I'd like to print this warning is to raise awareness to anyone reading the logs of the fact that things might not work properly, and that it's not necessarily smoldot's fault but usually the API's fault. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- bin/light-base/src/json_rpc_service.rs | 108 +++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/bin/light-base/src/json_rpc_service.rs b/bin/light-base/src/json_rpc_service.rs index ece6b1ef6a..ea0cf62060 100644 --- a/bin/light-base/src/json_rpc_service.rs +++ b/bin/light-base/src/json_rpc_service.rs @@ -358,6 +358,7 @@ impl ServicePrototype { Default::default(), ), }), + printed_legacy_json_rpc_warning: atomic::AtomicBool::new(false), }); // Spawns the background task that actually runs the logic of that JSON-RPC service. @@ -476,6 +477,10 @@ struct Background { next_subscription_id: atomic::AtomicU64, subscriptions: Mutex, + + /// If `true`, we have already printed a warning about usage of the legacy JSON-RPC API. This + /// flag prevents printing this message multiple times. + printed_legacy_json_rpc_warning: atomic::AtomicBool, } struct Subscriptions { @@ -722,6 +727,109 @@ impl Background { } }; + // Print a warning for legacy JSON-RPC functions. + match call { + methods::MethodCall::account_nextIndex { .. } + | methods::MethodCall::author_hasKey { .. } + | methods::MethodCall::author_hasSessionKeys { .. } + | methods::MethodCall::author_insertKey { .. } + | methods::MethodCall::author_pendingExtrinsics { .. } + | methods::MethodCall::author_removeExtrinsic { .. } + | methods::MethodCall::author_rotateKeys { .. } + | methods::MethodCall::author_submitAndWatchExtrinsic { .. } + | methods::MethodCall::author_submitExtrinsic { .. } + | methods::MethodCall::author_unwatchExtrinsic { .. } + | methods::MethodCall::babe_epochAuthorship { .. } + | methods::MethodCall::chain_getBlock { .. } + | methods::MethodCall::chain_getBlockHash { .. } + | methods::MethodCall::chain_getFinalizedHead { .. } + | methods::MethodCall::chain_getHeader { .. } + | methods::MethodCall::chain_subscribeAllHeads { .. } + | methods::MethodCall::chain_subscribeFinalizedHeads { .. } + | methods::MethodCall::chain_subscribeNewHeads { .. } + | methods::MethodCall::chain_unsubscribeAllHeads { .. } + | methods::MethodCall::chain_unsubscribeFinalizedHeads { .. } + | methods::MethodCall::chain_unsubscribeNewHeads { .. } + | methods::MethodCall::childstate_getKeys { .. } + | methods::MethodCall::childstate_getStorage { .. } + | methods::MethodCall::childstate_getStorageHash { .. } + | methods::MethodCall::childstate_getStorageSize { .. } + | methods::MethodCall::grandpa_roundState { .. } + | methods::MethodCall::offchain_localStorageGet { .. } + | methods::MethodCall::offchain_localStorageSet { .. } + | methods::MethodCall::payment_queryInfo { .. } + | methods::MethodCall::state_call { .. } + | methods::MethodCall::state_getKeys { .. } + | methods::MethodCall::state_getKeysPaged { .. } + | methods::MethodCall::state_getMetadata { .. } + | methods::MethodCall::state_getPairs { .. } + | methods::MethodCall::state_getReadProof { .. } + | methods::MethodCall::state_getRuntimeVersion { .. } + | methods::MethodCall::state_getStorage { .. } + | methods::MethodCall::state_getStorageHash { .. } + | methods::MethodCall::state_getStorageSize { .. } + | methods::MethodCall::state_queryStorage { .. } + | methods::MethodCall::state_queryStorageAt { .. } + | methods::MethodCall::state_subscribeRuntimeVersion { .. } + | methods::MethodCall::state_subscribeStorage { .. } + | methods::MethodCall::state_unsubscribeRuntimeVersion { .. } + | methods::MethodCall::state_unsubscribeStorage { .. } + | methods::MethodCall::system_accountNextIndex { .. } + | methods::MethodCall::system_addReservedPeer { .. } + | methods::MethodCall::system_chain { .. } + | methods::MethodCall::system_chainType { .. } + | methods::MethodCall::system_dryRun { .. } + | methods::MethodCall::system_health { .. } + | methods::MethodCall::system_localListenAddresses { .. } + | methods::MethodCall::system_localPeerId { .. } + | methods::MethodCall::system_name { .. } + | methods::MethodCall::system_networkState { .. } + | methods::MethodCall::system_nodeRoles { .. } + | methods::MethodCall::system_peers { .. } + | methods::MethodCall::system_properties { .. } + | methods::MethodCall::system_removeReservedPeer { .. } + | methods::MethodCall::system_version { .. } => { + if !self + .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 \ + instead. The \ + legacy JSON-RPC API functions will be deprecated and removed in the \ + distant future.", + call.name() + ) + } + } + methods::MethodCall::chainHead_unstable_body { .. } + | methods::MethodCall::chainHead_unstable_call { .. } + | methods::MethodCall::chainHead_unstable_follow { .. } + | methods::MethodCall::chainHead_unstable_genesisHash { .. } + | methods::MethodCall::chainHead_unstable_header { .. } + | methods::MethodCall::chainHead_unstable_stopBody { .. } + | methods::MethodCall::chainHead_unstable_stopCall { .. } + | methods::MethodCall::chainHead_unstable_stopStorage { .. } + | methods::MethodCall::chainHead_unstable_storage { .. } + | methods::MethodCall::chainHead_unstable_unfollow { .. } + | methods::MethodCall::chainHead_unstable_unpin { .. } + | methods::MethodCall::chainSpec_unstable_chainName { .. } + | methods::MethodCall::chainSpec_unstable_genesisHash { .. } + | methods::MethodCall::chainSpec_unstable_properties { .. } + | methods::MethodCall::rpc_methods { .. } + | methods::MethodCall::sudo_unstable_p2pDiscover { .. } + | methods::MethodCall::sudo_unstable_version { .. } + | methods::MethodCall::transaction_unstable_submitAndWatch { .. } + | methods::MethodCall::transaction_unstable_unwatch { .. } + | methods::MethodCall::network_unstable_subscribeEvents { .. } + | methods::MethodCall::network_unstable_unsubscribeEvents { .. } + | methods::MethodCall::chainHead_unstable_finalizedDatabase { .. } => {} + } + // Each call is handled in a separate method. match call { methods::MethodCall::author_pendingExtrinsics {} => {