|
| 1 | +use std::io::prelude::*; |
| 2 | +use std::os::unix::net::UnixStream; |
| 3 | +use std::slice::Iter; |
| 4 | + |
| 5 | +use hyper::{self, Body, Client, Method, Request}; |
| 6 | +use log::info; |
| 7 | +use serde_json::{self, Value}; |
| 8 | + |
| 9 | +use trin_core::portalnet::U256; |
| 10 | + |
| 11 | +#[derive(Copy, Clone)] |
| 12 | +pub struct JsonRpcEndpoint { |
| 13 | + pub method: &'static str, |
| 14 | + pub id: &'static u8, |
| 15 | +} |
| 16 | + |
| 17 | +const ALL_ENDPOINTS: [JsonRpcEndpoint; 6] = [ |
| 18 | + JsonRpcEndpoint { |
| 19 | + method: "web3_clientVersion", |
| 20 | + id: &0, |
| 21 | + }, |
| 22 | + JsonRpcEndpoint { |
| 23 | + method: "discv5_nodeInfo", |
| 24 | + id: &1, |
| 25 | + }, |
| 26 | + JsonRpcEndpoint { |
| 27 | + method: "discv5_routingTableInfo", |
| 28 | + id: &2, |
| 29 | + }, |
| 30 | + JsonRpcEndpoint { |
| 31 | + method: "eth_blockNumber", |
| 32 | + id: &3, |
| 33 | + }, |
| 34 | + JsonRpcEndpoint { |
| 35 | + method: "portalHistory_dataRadius", |
| 36 | + id: &4, |
| 37 | + }, |
| 38 | + JsonRpcEndpoint { |
| 39 | + method: "portalState_dataRadius", |
| 40 | + id: &5, |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +fn validate_endpoint_response(method: &str, result: &Value) { |
| 45 | + match method { |
| 46 | + "web3_clientVersion" => { |
| 47 | + assert_eq!(result.as_str().unwrap(), "trin 0.0.1-alpha"); |
| 48 | + } |
| 49 | + "discv5_nodeInfo" => { |
| 50 | + let enr = result.get("enr").unwrap(); |
| 51 | + assert!(enr.is_string()); |
| 52 | + assert!(enr.as_str().unwrap().contains("enr:")); |
| 53 | + assert!(result.get("nodeId").unwrap().is_string()); |
| 54 | + } |
| 55 | + "discv5_routingTableInfo" => { |
| 56 | + let local_key = result.get("localKey").unwrap(); |
| 57 | + assert!(local_key.is_string()); |
| 58 | + assert!(local_key.as_str().unwrap().contains("0x")); |
| 59 | + assert!(result.get("buckets").unwrap().is_array()); |
| 60 | + } |
| 61 | + "eth_blockNumber" => { |
| 62 | + assert!(result.is_string()); |
| 63 | + assert!(result.as_str().unwrap().contains("0x")); |
| 64 | + } |
| 65 | + "portalHistory_dataRadius" => { |
| 66 | + assert_eq!(result.as_str().unwrap(), U256::from(u64::MAX).to_string()); |
| 67 | + } |
| 68 | + "portalState_dataRadius" => { |
| 69 | + assert_eq!(result.as_str().unwrap(), U256::from(u64::MAX).to_string()); |
| 70 | + } |
| 71 | + _ => panic!("Unsupported endpoint"), |
| 72 | + }; |
| 73 | + info!("{:?} returned a valid response.", method); |
| 74 | +} |
| 75 | + |
| 76 | +impl JsonRpcEndpoint { |
| 77 | + pub fn all_endpoints() -> Iter<'static, Self> { |
| 78 | + ALL_ENDPOINTS.iter() |
| 79 | + } |
| 80 | + |
| 81 | + pub fn to_jsonrpc(self) -> Vec<u8> { |
| 82 | + let data = format!( |
| 83 | + r#" |
| 84 | + {{ |
| 85 | + "jsonrpc":"2.0", |
| 86 | + "id": {}, |
| 87 | + "method": "{}" |
| 88 | + }}"#, |
| 89 | + self.id, self.method |
| 90 | + ); |
| 91 | + let v: Value = serde_json::from_str(&data).unwrap(); |
| 92 | + serde_json::to_vec(&v).unwrap() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub async fn test_jsonrpc_endpoints_over_ipc() { |
| 97 | + for endpoint in JsonRpcEndpoint::all_endpoints() { |
| 98 | + info!("Testing over IPC: {:?}", endpoint.method); |
| 99 | + let mut stream = UnixStream::connect("/tmp/trin-jsonrpc.ipc").unwrap(); |
| 100 | + stream.write_all(&endpoint.to_jsonrpc()).unwrap(); |
| 101 | + stream.flush().unwrap(); |
| 102 | + let deser = serde_json::Deserializer::from_reader(stream); |
| 103 | + for obj in deser.into_iter::<Value>() { |
| 104 | + let response_obj = obj.unwrap(); |
| 105 | + let result = response_obj.get("result").unwrap(); |
| 106 | + validate_endpoint_response(endpoint.method, result); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +pub async fn test_jsonrpc_endpoints_over_http() { |
| 112 | + let client = Client::new(); |
| 113 | + for endpoint in JsonRpcEndpoint::all_endpoints() { |
| 114 | + info!("Testing over HTTP: {:?}", endpoint.method); |
| 115 | + let json_string = format!( |
| 116 | + r#"{{"jsonrpc":"2.0","id":0,"method":"{}","params":[]}}"#, |
| 117 | + endpoint.method |
| 118 | + ); |
| 119 | + let req = Request::builder() |
| 120 | + .method(Method::POST) |
| 121 | + .uri("http://127.0.0.1:8545") |
| 122 | + .header("content-type", "application/json") |
| 123 | + .body(Body::from(json_string)) |
| 124 | + .unwrap(); |
| 125 | + let resp = client.request(req).await.unwrap(); |
| 126 | + let body = hyper::body::to_bytes(resp.into_body()).await.unwrap(); |
| 127 | + let body_json: Value = serde_json::from_slice(&body).unwrap(); |
| 128 | + let result = body_json.get("result").unwrap(); |
| 129 | + validate_endpoint_response(endpoint.method, result); |
| 130 | + } |
| 131 | +} |
0 commit comments