From e2726c579574aa9b7aea0d908c23e5ae48e64181 Mon Sep 17 00:00:00 2001 From: Yucong Sun Date: Thu, 16 Jul 2020 11:17:12 -0700 Subject: [PATCH] Adding /v1 for JSONRPC endpoint. Closes: #5147 Approved by: xli --- json-rpc/src/runtime.rs | 14 +++++++++++--- json-rpc/src/tests/unit_tests.rs | 18 +++++++++++++++--- scripts/cli/start_cli_testnet.sh | 2 +- testsuite/cli/src/client_proxy.rs | 2 +- testsuite/cluster-test/src/instance.rs | 2 +- testsuite/tests/libratest/smoke_test.rs | 2 +- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/json-rpc/src/runtime.rs b/json-rpc/src/runtime.rs index 14935387788b..3b7ba582b44f 100644 --- a/json-rpc/src/runtime.rs +++ b/json-rpc/src/runtime.rs @@ -48,8 +48,7 @@ pub fn bootstrap( let registry = Arc::new(build_registry()); let service = JsonRpcService::new(libra_db, mp_sender, role); - let handler = warp::any() - .and(warp::path::end()) + let base_route = warp::any() .and(warp::post()) .and(warp::header::exact("content-type", "application/json")) .and(warp::body::json()) @@ -57,6 +56,15 @@ pub fn bootstrap( .and(warp::any().map(move || Arc::clone(®istry))) .and_then(rpc_endpoint); + // For now we still allow user to use "/", but user should start to move to "/v1" soon + let route_root = warp::path::end().and(base_route.clone()); + + let route_v1 = warp::path::path("v1") + .and(warp::path::end()) + .and(base_route); + + let full_route = route_v1.or(route_root); + // Ensure that we actually bind to the socket first before spawning the // server tasks. This helps in tests to prevent races where a client attempts // to make a request before the server task is actually listening on the @@ -64,7 +72,7 @@ pub fn bootstrap( // // Note: we need to enter the runtime context first to actually bind, since // tokio TcpListener can only be bound inside a tokio context. - let server = runtime.enter(move || warp::serve(handler).bind(address)); + let server = runtime.enter(move || warp::serve(full_route).bind(address)); runtime.handle().spawn(server); runtime } diff --git a/json-rpc/src/tests/unit_tests.rs b/json-rpc/src/tests/unit_tests.rs index dabf2249deee..d0e30ff1ea7f 100644 --- a/json-rpc/src/tests/unit_tests.rs +++ b/json-rpc/src/tests/unit_tests.rs @@ -132,7 +132,7 @@ fn test_json_rpc_protocol() { assert_eq!(resp.status(), 404); // only post method is allowed - let url = format!("http://{}", address); + let url = format!("http://{}/v1", address); let resp = client.get(&url).send().unwrap(); assert_eq!(resp.status(), 405); @@ -140,6 +140,17 @@ fn test_json_rpc_protocol() { let resp = client.post(&url).send().unwrap(); assert_eq!(resp.status(), 400); + // For now /v1 and / are both supported + { + let url_v1 = format!("http://{}", address); + let resp = client.post(&url_v1).send().unwrap(); + assert_eq!(resp.status(), 400); + + let url_v2 = format!("http://{}/v2", address); + let resp = client.post(&url_v2).send().unwrap(); + assert_eq!(resp.status(), 404); + } + // non json payload let resp = client.post(&url).body("non json").send().unwrap(); assert_eq!(resp.status(), 400); @@ -188,7 +199,7 @@ fn test_transaction_submission() { let address = format!("0.0.0.0:{}", port); let mut runtime = test_bootstrap(address.parse().unwrap(), Arc::new(mock_db), mp_sender); let client = JsonRpcAsyncClient::new( - reqwest::Url::from_str(format!("http://{}:{}", "127.0.0.1", port).as_str()) + reqwest::Url::from_str(format!("http://{}:{}/v1", "127.0.0.1", port).as_str()) .expect("invalid url"), ); @@ -636,7 +647,8 @@ fn create_database_client_and_runtime( mp_sender, ); let client = JsonRpcAsyncClient::new( - reqwest::Url::from_str(format!("http://127.0.0.1:{}", port).as_str()).expect("invalid url"), + reqwest::Url::from_str(format!("http://127.0.0.1:{}/v1", port).as_str()) + .expect("invalid url"), ); (mock_db, client, runtime) diff --git a/scripts/cli/start_cli_testnet.sh b/scripts/cli/start_cli_testnet.sh index fae031d450fb..a45c978a8a88 100755 --- a/scripts/cli/start_cli_testnet.sh +++ b/scripts/cli/start_cli_testnet.sh @@ -13,7 +13,7 @@ source "$HOME/.cargo/env" SCRIPT_PATH="$(dirname $0)" -RUN_PARAMS="--url https://client.testnet.libra.org --waypoint_url https://developers.libra.org/testnet_waypoint.txt --chain-id TESTNET" +RUN_PARAMS="--url https://client.testnet.libra.org/v1 --waypoint_url https://developers.libra.org/testnet_waypoint.txt --chain-id TESTNET" RELEASE="" while [[ ! -z "$1" ]]; do diff --git a/testsuite/cli/src/client_proxy.rs b/testsuite/cli/src/client_proxy.rs index 464728493ccb..213d82cae0e6 100644 --- a/testsuite/cli/src/client_proxy.rs +++ b/testsuite/cli/src/client_proxy.rs @@ -1693,7 +1693,7 @@ mod tests { // generate random accounts let mut client_proxy = ClientProxy::new( ChainId::test(), - "http://localhost:8080", + "http://localhost:8080/v1", &"", &"", false, diff --git a/testsuite/cluster-test/src/instance.rs b/testsuite/cluster-test/src/instance.rs index 0b8871184744..c8344c0fa32c 100644 --- a/testsuite/cluster-test/src/instance.rs +++ b/testsuite/cluster-test/src/instance.rs @@ -252,7 +252,7 @@ impl Instance { } pub fn json_rpc_url(&self) -> Url { - Url::from_str(&format!("http://{}:{}", self.ip(), self.ac_port())).expect("Invalid URL.") + Url::from_str(&format!("http://{}:{}/v1", self.ip(), self.ac_port())).expect("Invalid URL.") } fn k8s_backend(&self) -> &K8sInstanceInfo { diff --git a/testsuite/tests/libratest/smoke_test.rs b/testsuite/tests/libratest/smoke_test.rs index dd9b2a0cc749..74fd49a25de2 100644 --- a/testsuite/tests/libratest/smoke_test.rs +++ b/testsuite/tests/libratest/smoke_test.rs @@ -130,7 +130,7 @@ impl TestEnvironment { ClientProxy::new( ChainId::test(), - &format!("http://localhost:{}", port), + &format!("http://localhost:{}/v1", port), &self.faucet_key.1, &self.faucet_key.1, false,