Skip to content

Commit

Permalink
Adding /v1 for JSONRPC endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefallentree committed Jul 16, 2020
1 parent 28bee18 commit f9995f2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
14 changes: 11 additions & 3 deletions json-rpc/src/runtime.rs
Expand Up @@ -48,23 +48,31 @@ 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())
.and(warp::any().map(move || service.clone()))
.and(warp::any().map(move || Arc::clone(&registry)))
.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
// socket.
//
// 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
}
Expand Down
18 changes: 15 additions & 3 deletions json-rpc/src/tests/unit_tests.rs
Expand Up @@ -132,14 +132,25 @@ 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);

// empty payload is not allowed
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);
Expand Down Expand Up @@ -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"),
);

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion scripts/cli/start_cli_testnet.sh
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testsuite/cli/src/client_proxy.rs
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion testsuite/cluster-test/src/instance.rs
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion testsuite/tests/libratest/smoke_test.rs
Expand Up @@ -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,
Expand Down

0 comments on commit f9995f2

Please sign in to comment.