From 8bf6ef8b38707c198ee1232fa3f97da853ebe5b5 Mon Sep 17 00:00:00 2001 From: LEE Dongjun Date: Wed, 19 Sep 2018 20:32:16 +0900 Subject: [PATCH] Add WebSocket support to JSON-RPC --- Cargo.lock | 127 +++++++++++++++++----- codechain/codechain.yml | 25 +++++ codechain/config/mod.rs | 43 +++++++- codechain/config/presets/config.dev.toml | 6 + codechain/config/presets/config.prod.toml | 6 + codechain/rpc.rs | 30 ++++- codechain/run_node.rs | 10 +- rpc/Cargo.toml | 1 + rpc/src/lib.rs | 4 + rpc/src/rpc_server.rs | 13 +++ 10 files changed, 235 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd1c5c3c61..306810f7fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -448,6 +448,7 @@ dependencies = [ "jsonrpc-http-server 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "jsonrpc-ipc-server 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "jsonrpc-macros 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", + "jsonrpc-ws-server 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "kvdb 0.1.0", "kvdb-rocksdb 0.1.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -990,7 +991,7 @@ dependencies = [ [[package]] name = "jsonrpc-core" version = "8.0.1" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#06bec6ab682ec4ce16d7e7daf2612870c4272028" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1002,7 +1003,7 @@ dependencies = [ [[package]] name = "jsonrpc-http-server" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#6b972b5fd34ada4a7de6aed1a8c92475a907732d" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "hyper 0.11.26 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", @@ -1015,19 +1016,19 @@ dependencies = [ [[package]] name = "jsonrpc-ipc-server" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#6b972b5fd34ada4a7de6aed1a8c92475a907732d" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "jsonrpc-server-utils 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-tokio-ipc 0.1.5 (git+https://github.com/nikvolf/parity-tokio-ipc)", + "parity-tokio-ipc 0.1.5 (git+https://github.com/nikvolf/parity-tokio-ipc?rev=7c9bbe3bc45d8e72a92b0951acc877da228abd50)", "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "jsonrpc-macros" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#06bec6ab682ec4ce16d7e7daf2612870c4272028" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "jsonrpc-pubsub 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", @@ -1037,17 +1038,17 @@ dependencies = [ [[package]] name = "jsonrpc-pubsub" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#06bec6ab682ec4ce16d7e7daf2612870c4272028" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "jsonrpc-server-utils" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#06bec6ab682ec4ce16d7e7daf2612870c4272028" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1060,15 +1061,29 @@ dependencies = [ [[package]] name = "jsonrpc-tcp-server" version = "8.0.0" -source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#06bec6ab682ec4ce16d7e7daf2612870c4272028" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" dependencies = [ "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "jsonrpc-server-utils 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "jsonrpc-ws-server" +version = "8.0.0" +source = "git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11#811b0204cadd9c9ffb5c0e205f1694d57f550a7d" +dependencies = [ + "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", + "jsonrpc-server-utils 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)", + "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ws 0.7.5 (git+https://github.com/tomusdrw/ws-rs)", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -1161,6 +1176,15 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lock_api" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "log" version = "0.3.9" @@ -1252,12 +1276,12 @@ dependencies = [ [[package]] name = "mio-named-pipes" -version = "0.1.5" -source = "git+https://github.com/alexcrichton/mio-named-pipes#6ad80e67fe7993423b281bc13d307785ade05d37" +version = "0.1.6" +source = "git+https://github.com/alexcrichton/mio-named-pipes#2072ae0de5b3632dbb065fcd9c8be6c6a2fc39ae" dependencies = [ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", - "miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1284,7 +1308,7 @@ dependencies = [ [[package]] name = "miow" -version = "0.3.1" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "socket2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1358,18 +1382,19 @@ dependencies = [ [[package]] name = "parity-tokio-ipc" version = "0.1.5" -source = "git+https://github.com/nikvolf/parity-tokio-ipc#2af3e5b6b746552d8181069a2c6be068377df1de" +source = "git+https://github.com/nikvolf/parity-tokio-ipc?rev=7c9bbe3bc45d8e72a92b0951acc877da228abd50#7c9bbe3bc45d8e72a92b0951acc877da228abd50" dependencies = [ "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "mio-named-pipes 0.1.5 (git+https://github.com/alexcrichton/mio-named-pipes)", - "miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-named-pipes 0.1.6 (git+https://github.com/alexcrichton/mio-named-pipes)", + "miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-named-pipes 0.1.0 (git+https://github.com/nikvolf/tokio-named-pipes)", + "tokio-named-pipes 0.2.0 (git+https://github.com/nikvolf/tokio-named-pipes)", "tokio-uds 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1381,6 +1406,15 @@ dependencies = [ "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot_core" version = "0.2.14" @@ -1392,6 +1426,18 @@ dependencies = [ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "percent-encoding" version = "1.0.1" @@ -1780,6 +1826,11 @@ dependencies = [ "serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sha1" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "shell32-sys" version = "0.1.2" @@ -2039,14 +2090,14 @@ dependencies = [ [[package]] name = "tokio-named-pipes" -version = "0.1.0" -source = "git+https://github.com/nikvolf/tokio-named-pipes#0b9b728eaeb0a6673c287ac7692be398fd651752" +version = "0.2.0" +source = "git+https://github.com/nikvolf/tokio-named-pipes#0afa6247222a7aa6e8b370e949a0f4007f0018b6" dependencies = [ "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", - "mio-named-pipes 0.1.5 (git+https://github.com/alexcrichton/mio-named-pipes)", - "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-named-pipes 0.1.6 (git+https://github.com/alexcrichton/mio-named-pipes)", + "tokio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2339,6 +2390,22 @@ dependencies = [ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ws" +version = "0.7.5" +source = "git+https://github.com/tomusdrw/ws-rs#f12d19c4c19422fc79af28a3181f598bc07ecd1e" +dependencies = [ + "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" @@ -2434,6 +2501,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum jsonrpc-pubsub 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)" = "" "checksum jsonrpc-server-utils 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)" = "" "checksum jsonrpc-tcp-server 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)" = "" +"checksum jsonrpc-ws-server 8.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.11)" = "" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" @@ -2443,6 +2511,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939" "checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" "checksum local-encoding 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1ceb20f39ff7ae42f3ff9795f3986b1daad821caaa1e1732a0944103a5a1a66" +"checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" "checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21" @@ -2452,10 +2521,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum mime 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0b28683d0b09bbc20be1c9b3f6f24854efb1356ffcffee08ea3f6e65596e85fa" "checksum mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = "6d771e3ef92d58a8da8df7d6976bfca9371ed1de6619d9d5a5ce5b1f29b85bfe" -"checksum mio-named-pipes 0.1.5 (git+https://github.com/alexcrichton/mio-named-pipes)" = "" +"checksum mio-named-pipes 0.1.6 (git+https://github.com/alexcrichton/mio-named-pipes)" = "" "checksum mio-uds 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "84c7b5caa3a118a6e34dbac36504503b1e8dc5835e833306b9d6af0e05929f79" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" -"checksum miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9224c91f82b3c47cf53dcf78dfaa20d6888fbcc5d272d5f2fcdf8a697f3c987d" +"checksum miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "396aa0f2003d7df8395cb93e09871561ccc3e785f0acb369170e8cc74ddf9226" "checksum net2 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)" = "9044faf1413a1057267be51b5afba8eb1090bd2231c693664aa1db716fe1eae0" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" @@ -2463,9 +2532,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c" "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" -"checksum parity-tokio-ipc 0.1.5 (git+https://github.com/nikvolf/parity-tokio-ipc)" = "" +"checksum parity-tokio-ipc 0.1.5 (git+https://github.com/nikvolf/parity-tokio-ipc?rev=7c9bbe3bc45d8e72a92b0951acc877da228abd50)" = "" "checksum parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4d05f1349491390b1730afba60bb20d55761bef489a954546b58b4b34e1e2ac" +"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa" +"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7" "checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07" @@ -2507,6 +2578,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)" = "de4dee3b122edad92d80c66cac8d967ec7f8bf16a3b452247d6eb1dbf83c8f22" "checksum serde_derive 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)" = "7149ef7af607b09e0e7df38b1fd74264f08a29a67f604d5cb09d3fbdb1e256bc" "checksum serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f3ad6d546e765177cf3dded3c2e424a8040f870083a0e64064746b958ece9cb1" +"checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c" "checksum shell32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9ee04b46101f57121c9da2b151988283b6beb79b34f5bb29a58ee48cb695122c" "checksum skeptic 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24ebf8a06f5f8bae61ae5bbc7af7aac4ef6907ae975130faba1199e5fe82256a" "checksum slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6dbdd334bd28d328dad1c41b0ea662517883d8880d8533895ef96c8003dec9c4" @@ -2537,7 +2609,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum tokio-executor 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8cac2a7883ff3567e9d66bb09100d09b33d90311feca0206c7ca034bc0c55113" "checksum tokio-fs 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "76766830bbf9a2d5bfb50c95350d56a2e79e2c80f675967fff448bc615899708" "checksum tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6af9eb326f64b2d6b68438e1953341e00ab3cf54de7e35d92bfc73af8555313a" -"checksum tokio-named-pipes 0.1.0 (git+https://github.com/nikvolf/tokio-named-pipes)" = "" +"checksum tokio-named-pipes 0.2.0 (git+https://github.com/nikvolf/tokio-named-pipes)" = "" "checksum tokio-proto 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fbb47ae81353c63c487030659494b295f6cb6576242f907f203473b191b0389" "checksum tokio-reactor 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3cedc8e5af5131dc3423ffa4f877cce78ad25259a9a62de0613735a13ebc64b" "checksum tokio-service 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24da22d077e0f15f55162bdbdc661228c1581892f52074fb242678d015b45162" @@ -2571,6 +2643,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767" +"checksum ws 0.7.5 (git+https://github.com/tomusdrw/ws-rs)" = "" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61" "checksum yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992" diff --git a/codechain/codechain.yml b/codechain/codechain.yml index b83f11ceaf..39cd66d289 100644 --- a/codechain/codechain.yml +++ b/codechain/codechain.yml @@ -93,6 +93,31 @@ args: takes_value: true conflicts_with: - no-ipc + - ws-interface: + long: ws-interface + value_name: INTERFACE + help: Specify the interface address for the WebSockets JSON-RPC server. + takes_value: true + conflicts_with: + - no-ws + - ws-port: + long: ws-port + value_name: PORT + help: Specify the port portion of the WebSockets JSON-RPC server. + takes_value: true + conflicts_with: + - no-ws + - ws-max-connections: + long: ws-max-connections + value_name: CONN + help: Maximum number of allowed concurrent WebSockets JSON-RPC connections. + takes_value: true + conflicts_with: + - no-ws + - no-ws: + long: no-ws + help: Do not run the WebSockets JSON-RPC server. + takes_value: false - no-jsonrpc: long: no-jsonrpc help: Do not run jsonrpc. diff --git a/codechain/config/mod.rs b/codechain/config/mod.rs index e5a38a49d5..fd81fe4292 100644 --- a/codechain/config/mod.rs +++ b/codechain/config/mod.rs @@ -25,7 +25,7 @@ use ccore::{MinerOptions, ShardValidatorConfig, StratumConfig}; use ckey::PlatformAddress; use clap; use cnetwork::{NetworkConfig, SocketAddr}; -use rpc::{RpcHttpConfig, RpcIpcConfig}; +use rpc::{RpcHttpConfig, RpcIpcConfig, RpcWsConfig}; use toml; pub use self::chain_type::ChainType; @@ -39,6 +39,7 @@ pub struct Config { pub mining: Mining, pub network: Network, pub rpc: Rpc, + pub ws: Ws, pub snapshot: Snapshot, pub stratum: Stratum, pub shard_validator: ShardValidator, @@ -109,6 +110,17 @@ impl Config { } } + pub fn rpc_ws_config(&self) -> RpcWsConfig { + debug_assert!(!self.ws.disable.unwrap()); + + // FIXME: Add hosts and origins options. + RpcWsConfig { + interface: self.ws.interface.clone().unwrap(), + port: self.ws.port.unwrap(), + max_connections: self.ws.max_connections.unwrap(), + } + } + pub fn network_config(&self) -> Result { debug_assert!(!self.network.disable.unwrap()); @@ -235,6 +247,15 @@ pub struct Rpc { pub enable_devel_api: bool, } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Ws { + pub disable: Option, + pub interface: Option, + pub port: Option, + pub max_connections: Option, +} + fn default_enable_devel_api() -> bool { cfg!(debug_assertions) } @@ -536,6 +557,25 @@ impl Rpc { } } +impl Ws { + pub fn overwrite_with(&mut self, matches: &clap::ArgMatches) -> Result<(), String> { + if matches.is_present("no-ws") { + self.disable = Some(true); + } + + if let Some(interface) = matches.value_of("ws-interface") { + self.interface = Some(interface.to_string()); + } + if let Some(port) = matches.value_of("ws-port") { + self.port = Some(port.parse().map_err(|_| "Invalid port")?); + } + if let Some(max_connections) = matches.value_of("ws-max-connections") { + self.max_connections = Some(max_connections.parse().map_err(|_| "Invalid max connections")?); + } + Ok(()) + } +} + impl Snapshot { pub fn merge(&mut self, other: &Snapshot) { if other.disable.is_some() { @@ -633,6 +673,7 @@ pub fn load_config(matches: &clap::ArgMatches) -> Result { config.mining.overwrite_with(&matches)?; config.network.overwrite_with(&matches)?; config.rpc.overwrite_with(&matches)?; + config.ws.overwrite_with(&matches)?; config.snapshot.overwrite_with(&matches)?; config.stratum.overwrite_with(&matches)?; config.shard_validator.overwrite_with(&matches)?; diff --git a/codechain/config/presets/config.dev.toml b/codechain/config/presets/config.dev.toml index 358f48676f..057492f2f6 100644 --- a/codechain/config/presets/config.dev.toml +++ b/codechain/config/presets/config.dev.toml @@ -40,6 +40,12 @@ port = 8080 disable = false path = "/tmp/jsonrpc.ipc" +[ws] +disable = false +interface = "127.0.0.1" +port = 8081 +max_connections = 100 + [snapshot] disable = false path = "snapshot" diff --git a/codechain/config/presets/config.prod.toml b/codechain/config/presets/config.prod.toml index dced948591..96466328ac 100644 --- a/codechain/config/presets/config.prod.toml +++ b/codechain/config/presets/config.prod.toml @@ -39,6 +39,12 @@ port = 8080 disable = true path = "/tmp/jsonrpc.ipc" +[ws] +disable = true +interface = "127.0.0.1" +port = 8081 +max_connections = 100 + [snapshot] disable = true path = "snapshot" diff --git a/codechain/rpc.rs b/codechain/rpc.rs index 0d850a850b..6f7781522b 100644 --- a/codechain/rpc.rs +++ b/codechain/rpc.rs @@ -18,7 +18,7 @@ use std::io; use std::net::SocketAddr; use std::sync::Arc; -use crpc::{start_http, start_ipc, HttpServer, IpcServer}; +use crpc::{start_http, start_ipc, start_ws, HttpServer, IpcServer, WsError, WsErrorKind, WsServer}; use crpc::{Compatibility, MetaIoHandler}; use rpc_apis; @@ -84,6 +84,34 @@ pub fn rpc_ipc_start( } } +#[derive(Debug, PartialEq)] +pub struct RpcWsConfig { + pub interface: String, + pub port: u16, + pub max_connections: usize, +} + +pub fn rpc_ws_start( + cfg: RpcWsConfig, + enable_devel_api: bool, + deps: Arc, +) -> Result { + let server = setup_rpc_server(enable_devel_api, deps); + let url = format!("{}:{}", cfg.interface, cfg.port); + let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?; + let start_result = start_ws(&addr, server, cfg.max_connections); + match start_result { + Err(WsError(WsErrorKind::Io(ref err), _)) if err.kind() == io::ErrorKind::AddrInUse => { + Err(format!("WebSockets address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ws-port options.", addr)) + }, + Err(e) => Err(format!("WebSockets error: {:?}", e)), + Ok(server) => { + cinfo!(RPC, "WebSockets Listening on {}", addr); + Ok(server) + }, + } +} + fn setup_rpc_server(enable_devel_api: bool, deps: Arc) -> MetaIoHandler<()> { let mut handler = MetaIoHandler::with_compatibility(Compatibility::Both); deps.extend_api(enable_devel_api, &mut handler); diff --git a/codechain/run_node.rs b/codechain/run_node.rs index adc47077ab..8b87d25395 100644 --- a/codechain/run_node.rs +++ b/codechain/run_node.rs @@ -42,7 +42,7 @@ use super::config::{self, load_config}; use super::constants::DEFAULT_KEYS_PATH; use super::dummy_network_service::DummyNetworkService; use super::json::PasswordFile; -use super::rpc::{rpc_http_start, rpc_ipc_start}; +use super::rpc::{rpc_http_start, rpc_ipc_start, rpc_ws_start}; use super::rpc_apis::ApiDependencies; fn network_start(cfg: &NetworkConfig) -> Result, String> { @@ -296,6 +296,14 @@ pub fn run_node(matches: ArgMatches) -> Result<(), String> { } }; + let _ws_server = { + if !config.ws.disable.unwrap() { + Some(rpc_ws_start(config.rpc_ws_config(), config.rpc.enable_devel_api, Arc::clone(&rpc_apis_deps))?) + } else { + None + } + }; + if (!config.stratum.disable.unwrap()) && (miner.engine_type() == EngineType::PoW) { stratum_start(config.stratum_config(), Arc::clone(&miner), client.client())? } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index e4b4510156..d0b2eafd0a 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -28,3 +28,4 @@ jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "pa jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } jsonrpc-ipc-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } +jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 5086edddee..9e9c446ab7 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -25,6 +25,7 @@ extern crate codechain_types as ctypes; extern crate jsonrpc_core; extern crate jsonrpc_http_server; extern crate jsonrpc_ipc_server; +extern crate jsonrpc_ws_server; extern crate kvdb; extern crate kvdb_rocksdb as rocksdb; #[macro_use] @@ -55,3 +56,6 @@ pub use rpc_server::start_http; pub use jsonrpc_ipc_server::Server as IpcServer; pub use rpc_server::start_ipc; + +pub use jsonrpc_ws_server::{Error as WsError, ErrorKind as WsErrorKind, Server as WsServer}; +pub use rpc_server::start_ws; diff --git a/rpc/src/rpc_server.rs b/rpc/src/rpc_server.rs index b670c065bd..e3c7be75f4 100644 --- a/rpc/src/rpc_server.rs +++ b/rpc/src/rpc_server.rs @@ -18,6 +18,7 @@ use jsonrpc_core; use jsonrpc_http_server::{self, Host, Server as HttpServer, ServerBuilder as HttpServerBuilder}; use jsonrpc_ipc_server::{Server as IpcServer, ServerBuilder as IpcServerBuilder}; +use jsonrpc_ws_server::{Error as WsError, Server as WsServer, ServerBuilder as WsServerBuilder}; use std::default::Default; use std::io; use std::net::SocketAddr; @@ -57,3 +58,15 @@ where M: Default, { IpcServerBuilder::new(handler).start(addr) } + +/// Start WS server and return `Server` handle. +pub fn start_ws( + addr: &SocketAddr, + handler: jsonrpc_core::MetaIoHandler, + max_connections: usize, +) -> Result +where + M: Default, { + // FIXME: Add Hosts, Origins and Session States + WsServerBuilder::new(handler).max_connections(max_connections).start(addr) +}