diff --git a/Cargo.lock b/Cargo.lock index 3a46906..d6d4e20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "anyhow" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" + [[package]] name = "async-attributes" version = "1.1.2" @@ -42,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5" dependencies = [ "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -92,6 +98,7 @@ dependencies = [ "blocking", "futures-lite", "once_cell", + "tokio", ] [[package]] @@ -188,7 +195,7 @@ checksum = "25f9db3b38af870bf7e5cc649167533b493928e50744e2c30ae350230b414670" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -205,7 +212,7 @@ checksum = "31e6e93155431f3931513b243d371981bb2770112b370c82745a1d19d2f99364" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -243,12 +250,30 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.3" @@ -435,7 +460,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" dependencies = [ "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -472,7 +497,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn", + "syn 1.0.104", ] [[package]] @@ -489,7 +514,16 @@ checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", ] [[package]] @@ -498,7 +532,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ - "block-buffer", + "block-buffer 0.10.3", "crypto-common", ] @@ -693,7 +727,7 @@ checksum = "bdfb8ce053d86b91919aad980c220b1fb8401a9394410e1c289ed7e66b61835d" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -809,6 +843,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "h2" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17f8a914c2987b688368b5138aa05321db91f4090cf26118185672ad588bce21" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "1.8.2" @@ -904,13 +957,13 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", + "h2", "http", "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2", "tokio", "tower-service", "tracing", @@ -1014,52 +1067,72 @@ dependencies = [ ] [[package]] -name = "jsonrpc-core" -version = "18.0.0" +name = "jsonrpsee" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14f7f76aef2d054868398427f6c54943cf3d1caa9a7ec7d0c38d69df97a965eb" +checksum = "1822d18e4384a5e79d94dc9e4d1239cfa9fad24e55b44d2efeff5b394c9fece4" dependencies = [ - "futures", - "futures-executor", - "futures-util", - "log", - "serde", - "serde_derive", - "serde_json", + "jsonrpsee-core", + "jsonrpsee-server", + "jsonrpsee-types", ] [[package]] -name = "jsonrpc-http-server" -version = "18.0.0" +name = "jsonrpsee-core" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1dea6e07251d9ce6a552abfb5d7ad6bc290a4596c8dcc3d795fae2bbdc1f3ff" +checksum = "64c6832a55f662b5a6ecc844db24b8b9c387453f923de863062c60ce33d62b81" dependencies = [ - "futures", + "anyhow", + "async-trait", + "beef", + "futures-util", + "globset", "hyper", - "jsonrpc-core", - "jsonrpc-server-utils", - "log", - "net2", - "parking_lot", - "unicase", + "jsonrpsee-types", + "parking_lot 0.12.1", + "rand 0.8.5", + "rustc-hash", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio", + "tracing", ] [[package]] -name = "jsonrpc-server-utils" -version = "18.0.0" +name = "jsonrpsee-server" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4fdea130485b572c39a460d50888beb00afb3e35de23ccd7fad8ff19f0e0d4" +checksum = "4f06661d1a6b6e5b85469dc9c29acfbb9b3bb613797a6fd10a3ebb8a70754057" dependencies = [ - "bytes", - "futures", - "globset", - "jsonrpc-core", - "lazy_static", - "log", + "futures-util", + "hyper", + "jsonrpsee-core", + "jsonrpsee-types", + "serde", + "serde_json", + "soketto", "tokio", "tokio-stream", "tokio-util", - "unicase", + "tower", + "tracing", +] + +[[package]] +name = "jsonrpsee-types" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e5bf6c75ce2a4217421154adfc65a24d2b46e77286e59bba5d9fa6544ccc8f4" +dependencies = [ + "anyhow", + "beef", + "serde", + "serde_json", + "thiserror", + "tracing", ] [[package]] @@ -1227,17 +1300,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "net2" -version = "0.2.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d0df99cfcd2530b2e694f6e17e7f37b8e26bb23983ac530c0c97408837c631" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "winapi 0.3.9", -] - [[package]] name = "nix" version = "0.25.0" @@ -1285,6 +1347,12 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "parking" version = "2.0.0" @@ -1299,7 +1367,17 @@ checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" dependencies = [ "instant", "lock_api", - "parking_lot_core", + "parking_lot_core 0.8.5", +] + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.6", ] [[package]] @@ -1316,6 +1394,19 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "parking_lot_core" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "percent-encoding" version = "2.2.0" @@ -1369,7 +1460,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.104", "version_check", ] @@ -1386,9 +1477,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -1410,9 +1501,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.21" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" dependencies = [ "proc-macro2", ] @@ -1530,6 +1621,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustix" version = "0.35.13" @@ -1612,7 +1709,7 @@ checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -1627,6 +1724,19 @@ dependencies = [ "serde", ] +[[package]] +name = "sha-1" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + [[package]] name = "sha2" version = "0.10.6" @@ -1635,7 +1745,7 @@ checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ "cfg-if 1.0.0", "cpufeatures", - "digest", + "digest 0.10.6", ] [[package]] @@ -1679,7 +1789,7 @@ dependencies = [ "fxhash", "libc", "log", - "parking_lot", + "parking_lot 0.11.2", ] [[package]] @@ -1715,9 +1825,25 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "soketto" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d1c5305e39e09653383c2c7244f2f78b3bcae37cf50c64cb4789c9f5096ec2" +dependencies = [ + "base64 0.13.1", + "bytes", + "futures", + "http", + "httparse", + "log", + "rand 0.8.5", + "sha-1", +] + [[package]] name = "solar" -version = "0.3.2" +version = "0.3.3" dependencies = [ "async-ctrlc", "async-std", @@ -1726,7 +1852,7 @@ dependencies = [ "env_logger", "futures", "hex", - "jsonrpc-http-server", + "jsonrpsee", "kuska-sodiumoxide", "kuska-ssb", "log", @@ -1775,7 +1901,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -1789,6 +1915,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tempdir" version = "0.3.7" @@ -1834,7 +1971,7 @@ checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", ] [[package]] @@ -1877,9 +2014,21 @@ dependencies = [ "num_cpus", "pin-project-lite", "socket2", + "tokio-macros", "winapi 0.3.9", ] +[[package]] +name = "tokio-macros" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.104", +] + [[package]] name = "tokio-stream" version = "0.1.11" @@ -1893,16 +2042,17 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.6.10" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36943ee01a6d67977dd3f84a5a1d2efeb4ada3a1ae771cadfaa535d9d9fc6507" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ "bytes", "futures-core", + "futures-io", "futures-sink", - "log", "pin-project-lite", "tokio", + "tracing", ] [[package]] @@ -1914,6 +2064,23 @@ dependencies = [ "serde", ] +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + [[package]] name = "tower-service" version = "0.3.2" @@ -1927,10 +2094,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if 1.0.0", + "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "tracing-core" version = "0.1.30" @@ -1952,15 +2132,6 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" -[[package]] -name = "unicase" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-bidi" version = "0.3.8" @@ -2087,7 +2258,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 1.0.104", "wasm-bindgen-shared", ] @@ -2121,7 +2292,7 @@ checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.104", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 0296c3e..0a3cbdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "solar" -version = "0.3.2" +version = "0.3.3" authors = ["adria0 ", "glyph "] edition = "2018" default-run = "solar" @@ -12,13 +12,13 @@ path = "src/main.rs" [dependencies] async-ctrlc = "1" -async-std = { version = "1", features=["unstable", "attributes"] } +async-std = { version = "1", features=["attributes", "tokio1"] } async-trait = "0.1" base64 = "0.13" env_logger = "0.10" futures = "0.3" hex = "0.4.0" -jsonrpc-http-server = "18" +jsonrpsee = { version = "0.18.2", features = ["server"] } kuska-sodiumoxide = "0.2.5-0" kuska-ssb = { git = "https://github.com/Kuska-ssb/ssb", branch = "master" } log = "0.4" diff --git a/src/actors/jsonrpc_server.rs b/src/actors/jsonrpc_server.rs index e1efb2e..1f4be0c 100644 --- a/src/actors/jsonrpc_server.rs +++ b/src/actors/jsonrpc_server.rs @@ -1,14 +1,15 @@ // src/actors/json_rpc_server.rs +use std::net::SocketAddr; + use async_std::task; use futures::FutureExt; -use jsonrpc_http_server::{ - jsonrpc_core::*, AccessControlAllowOrigin, DomainsValidation, ServerBuilder, -}; +use jsonrpsee::server::{logger::Params, RpcModule, ServerBuilder}; +use jsonrpsee::types::error::ErrorObject as JsonRpcError; use kuska_ssb::{api::dto::content::TypedMessage, feed::Message, keystore::OwnedIdentity}; use log::{info, warn}; use serde::Deserialize; -use serde_json::json; +use serde_json::{json, Value}; use crate::{broker::*, error::Error, Result, KV_STORAGE}; @@ -40,11 +41,16 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> let ch_terminate = broker.ch_terminate.fuse(); - let mut io = IoHandler::default(); + let server = ServerBuilder::default() + .http_only() + .build(&server_addr.parse::()?) + .await?; + + let mut rpc_module = RpcModule::new(()); // Retrieve a feed by public key. // Returns an array of messages as a KVTs. - io.add_sync_method("feed", move |params: Params| { + rpc_module.register_method("feed", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the public key. let pub_key: PubKey = params.parse()?; @@ -57,13 +63,13 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> let response = json!(feed); - Ok(response) + Ok::(response) }) - }); + })?; // Retrieve a message by key. // Returns the message as a KVT. - io.add_sync_method("message", move |params: Params| { + rpc_module.register_method("message", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the message reference (key). let msg_ref: MsgRef = params.parse()?; @@ -84,25 +90,25 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> let response = json!(msg_kvt); - Ok(response) + Ok::(response) }) - }); + })?; // Return the public key and latest sequence number for all feeds in the // local database. - io.add_sync_method("peers", |_| { + rpc_module.register_method("peers", |_, _| { task::block_on(async { let db = KV_STORAGE.read().await; let peers = db.get_peers().await?; let response = json!(peers); - Ok(response) + Ok::(response) }) - }); + })?; // Simple `ping` endpoint. - io.add_sync_method("ping", |_| Ok(Value::String("pong!".to_owned()))); + rpc_module.register_method("ping", |_, _| "pong!")?; // Clone the local public key (ID) so it can later be captured by the // `whoami` closure. @@ -110,7 +116,7 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> // Publish a typed message (raw). // Returns the key (hash) and sequence number of the published message. - io.add_sync_method("publish", move |params: Params| { + rpc_module.register_method("publish", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the post content. let post_content: TypedMessage = params.parse()?; @@ -137,28 +143,16 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> let response = json![{ "msg_ref": msg.id().to_string(), "seq_num": seq }]; - Ok(response) + Ok::(response) }) - }); + })?; // Return the public key of the local SSB server. - io.add_sync_method("whoami", move |_| Ok(Value::String(local_pk.to_owned()))); - - let server = ServerBuilder::new(io) - .cors(DomainsValidation::AllowOnly(vec![ - AccessControlAllowOrigin::Null, - ])) - .start_http(&server_addr.parse()?)?; - - // Create a close handle to be used when the termination signal is - // received. - let close_handle = server.close_handle(); + rpc_module.register_method("whoami", move |_, _| local_pk.clone())?; - // Start the JSON-RPC server in a task. - // This allows us to listen for the termination signal (without blocking). - task::spawn(async { - server.wait(); - }); + let addr = server.local_addr()?; + let handle = server.start(rpc_module)?; + info!("JSON-RPC server started on: {}", addr); // Listen for termination signal from broker. if let Err(err) = ch_terminate.await { @@ -166,7 +160,7 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: String) -> Result<()> } // When received, close (stop) the server. - close_handle.close(); + handle.stop()?; // Then send terminated signal back to broker. let _ = broker.ch_terminated.send(Void {}); diff --git a/src/actors/tcp_server.rs b/src/actors/tcp_server.rs index f36294f..4b710be 100644 --- a/src/actors/tcp_server.rs +++ b/src/actors/tcp_server.rs @@ -4,6 +4,7 @@ use async_std::{ }; use futures::{select_biased, FutureExt}; use kuska_ssb::keystore::OwnedIdentity; +use log::debug; use crate::{broker::*, Result}; @@ -18,6 +19,7 @@ pub async fn actor( let listener = TcpListener::bind(addr).await?; let mut incoming = listener.incoming(); + debug!("Listening for inbound TCP connection..."); loop { select_biased! { @@ -25,7 +27,14 @@ pub async fn actor( stream = incoming.next().fuse() => { if let Some(stream) = stream { if let Ok(stream) = stream { - Broker::spawn(super::peer::actor(server_id.clone(), super::peer::Connect::ClientStream{stream}, selective_replication)); + debug!("Received inbound TCP connection"); + Broker::spawn( + super::peer::actor( + server_id.clone(), + super::peer::Connect::ClientStream{stream}, + selective_replication + ) + ); } } else { break; diff --git a/src/error.rs b/src/error.rs index a61d963..4ea2966 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ use std::{fmt, io, net}; -use jsonrpc_http_server::jsonrpc_core; +use jsonrpsee::types::error::ErrorObjectOwned as JsonRpcErrorOwned; +use jsonrpsee::types::error::SERVER_ERROR_MSG; use kuska_ssb::{api, crypto, discovery, feed, handshake, rpc}; use toml::{de, ser}; @@ -21,6 +22,8 @@ pub enum Error { InvalidSequence, /// io::Error. Io(io::Error), + /// JSON RPC error. + JsonRpc(jsonrpsee::core::Error), /// LAN UDP discovery error. LanDiscovery(discovery::Error), /// SSB RPC error. @@ -59,6 +62,7 @@ impl fmt::Display for Error { "validation error. message contains incorrect sequence number" ), Error::Io(err) => write!(f, "i/o error: {err}"), + Error::JsonRpc(err) => write!(f, "json-rpc error: {err}"), Error::LanDiscovery(err) => write!(f, "lan udp discovery error: {err}"), Error::MuxRpc(err) => write!(f, "muxrpc error: {err}"), Error::SecretHandshake(err) => write!(f, "secret handshake error: {err}"), @@ -109,6 +113,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: jsonrpsee::core::Error) -> Error { + Error::JsonRpc(err) + } +} + impl From for Error { fn from(err: discovery::Error) -> Error { Error::LanDiscovery(err) @@ -166,24 +176,18 @@ impl From for Error { // Conversions for errors which occur in the context of a JSON-RPC method call. // Crate-local error variants are converted to JSON-RPC errors which are // then return to the caller. -impl From for jsonrpc_core::Error { +impl From for JsonRpcErrorOwned { fn from(err: Error) -> Self { match &err { - Error::SerdeJson(err_msg) => jsonrpc_core::Error { - code: jsonrpc_core::ErrorCode::ServerError(-32000), - message: err_msg.to_string(), - data: None, - }, - Error::UrlParse(err_msg) => jsonrpc_core::Error { - code: jsonrpc_core::ErrorCode::ServerError(-32001), - message: err_msg.to_string(), - data: None, - }, - Error::Validation(err_msg) => jsonrpc_core::Error { - code: jsonrpc_core::ErrorCode::ServerError(-32002), - message: err_msg.to_string(), - data: None, - }, + Error::SerdeJson(err_msg) => { + JsonRpcErrorOwned::owned(-32000, SERVER_ERROR_MSG, Some(err_msg.to_string())) + } + Error::UrlParse(err_msg) => { + JsonRpcErrorOwned::owned(-32001, SERVER_ERROR_MSG, Some(err_msg.to_string())) + } + Error::Validation(err_msg) => { + JsonRpcErrorOwned::owned(-32002, SERVER_ERROR_MSG, Some(err_msg.to_string())) + } _ => todo!(), } }