diff --git a/gravity-info-server/src/batch_relaying.rs b/gravity-info-server/src/batch_relaying.rs index 3220a94..a7eaaaf 100644 --- a/gravity-info-server/src/batch_relaying.rs +++ b/gravity-info-server/src/batch_relaying.rs @@ -4,7 +4,6 @@ use clarity::{Address as EthAddress, Uint256}; use cosmos_gravity::query::{ get_gravity_params, get_latest_transaction_batches, get_transaction_batch_signatures, }; -use deep_space::Contact; use ethereum_gravity::message_signatures::encode_tx_batch_confirm_hashed; use ethereum_gravity::submit_batch::encode_batch_payload; use gravity_proto::gravity::query_client::QueryClient as GravityQueryClient; @@ -14,11 +13,25 @@ use relayer::find_latest_valset::find_latest_valset; use std::time::Duration; use web30::client::Web3; -use crate::gravity_info::{ETH_NODE_RPC, GRAVITY_NODE_GRPC, GRAVITY_PREFIX, REQUEST_TIMEOUT}; +use crate::gravity_info::{ETH_NODE_RPC, GRAVITY_NODE_GRPC, REQUEST_TIMEOUT}; -pub async fn generate_raw_batch_tx(batch_nonce: u64) -> impl Responder { +#[derive(Debug)] +pub enum BatchRelayError { + ServerError(String), + BadRequest(String), +} + +pub async fn generate_batch_tx_responder(batch_nonce: u64) -> impl Responder { + let res = generate_raw_batch_tx(batch_nonce).await; + match res { + Ok(payload) => HttpResponse::Ok().json(bytes_to_hex_str(&payload)), + Err(BatchRelayError::ServerError(e)) => HttpResponse::InternalServerError().json(e), + Err(BatchRelayError::BadRequest(e)) => HttpResponse::BadRequest().json(e), + } +} + +pub async fn generate_raw_batch_tx(batch_nonce: u64) -> Result, BatchRelayError> { let web3 = Web3::new(ETH_NODE_RPC, REQUEST_TIMEOUT); - let _contact = Contact::new(GRAVITY_NODE_GRPC, REQUEST_TIMEOUT, GRAVITY_PREFIX).unwrap(); let mut grpc = loop { match GravityQueryClient::connect(GRAVITY_NODE_GRPC).await { Ok(client) => break client, @@ -30,29 +43,45 @@ pub async fn generate_raw_batch_tx(batch_nonce: u64) -> impl Responder { }; let params = match get_gravity_params(&mut grpc).await { Ok(p) => p, - Err(_) => return HttpResponse::InternalServerError().json("Failed to get gravity params!"), + Err(_) => { + return Err(BatchRelayError::ServerError( + "Failed to get gravity params!".to_string(), + )) + } }; let gravity_bridge_address: EthAddress = match params.bridge_ethereum_address.parse() { Ok(a) => a, - Err(_) => return HttpResponse::InternalServerError().json("Interanl error"), + Err(_) => { + return Err(BatchRelayError::ServerError( + "Failed to parse Gravity Address?".to_string(), + )) + } }; // find the target batch and check that it's not timed out let latest_eth_height = match web3.eth_block_number().await { Ok(bn) => bn, Err(_) => { - return HttpResponse::InternalServerError().json("Failed to get latest eth height") + return Err(BatchRelayError::ServerError( + "Failed to get latest eth height".to_string(), + )) } }; let latest_batches = match get_latest_transaction_batches(&mut grpc).await { Ok(v) => v, - Err(_) => return HttpResponse::InternalServerError().json("Failed to get batches!"), + Err(_) => { + return Err(BatchRelayError::ServerError( + "Failed to get batches!".to_string(), + )) + } }; let mut target_batch: Option = None; for current_batch in latest_batches { if current_batch.nonce == batch_nonce { if Uint256::from(current_batch.batch_timeout) < latest_eth_height { - return HttpResponse::BadRequest().json("Batch has timed out!"); + return Err(BatchRelayError::BadRequest( + "Batch has timed out!".to_string(), + )); } target_batch = Some(current_batch); break; @@ -61,7 +90,11 @@ pub async fn generate_raw_batch_tx(batch_nonce: u64) -> impl Responder { let target_batch = match target_batch { Some(b) => b, - None => return HttpResponse::BadRequest().json("Batch nonce not found!"), + None => { + return Err(BatchRelayError::BadRequest( + "Batch nonce not found!".to_string(), + )) + } }; let sigs = get_transaction_batch_signatures( @@ -72,13 +105,17 @@ pub async fn generate_raw_batch_tx(batch_nonce: u64) -> impl Responder { .await .expect("Failed to get sigs for batch!"); if sigs.is_empty() { - return HttpResponse::InternalServerError().json("Failed to get sigs for batch"); + return Err(BatchRelayError::ServerError( + "Failed to get sigs for batch".to_string(), + )); } let current_valset = find_latest_valset(&mut grpc, gravity_bridge_address, &web3).await; if current_valset.is_err() { error!("Could not get current valset! {:?}", current_valset); - return HttpResponse::InternalServerError().json("Failed tog et sigs for batch"); + return Err(BatchRelayError::ServerError( + "Failed tog et sigs for batch".to_string(), + )); } let current_valset = current_valset.unwrap(); @@ -88,11 +125,26 @@ pub async fn generate_raw_batch_tx(batch_nonce: u64) -> impl Responder { if let Err(e) = current_valset.order_sigs(&hash, &sigs, true) { error!("Current validator set is not valid to relay this batch, a validator set update must be submitted!"); error!("{:?}", e); - return HttpResponse::InternalServerError().json("sig order not valid"); + return Err(BatchRelayError::ServerError( + "sig order not valid".to_string(), + )); } match encode_batch_payload(current_valset, &target_batch, &sigs, params.gravity_id) { - Ok(payload) => HttpResponse::Ok().json(bytes_to_hex_str(&payload)), - Err(_) => HttpResponse::InternalServerError().json("Failed to encode payload!"), + Ok(payload) => Ok(payload), + Err(_) => Err(BatchRelayError::ServerError( + "Failed to encode payload!".to_string(), + )), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[actix_web::test] + async fn test_batch_relay_query() { + let res = generate_raw_batch_tx(34787).await; + println!("Got batch response {:?}", res.unwrap()); } } diff --git a/gravity-info-server/src/gravity_info.rs b/gravity-info-server/src/gravity_info.rs index 3c332af..0c782d6 100644 --- a/gravity-info-server/src/gravity_info.rs +++ b/gravity-info-server/src/gravity_info.rs @@ -28,7 +28,7 @@ use web30::client::Web3; const LOOP_TIME: Duration = Duration::from_secs(30); pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); -pub const GRAVITY_NODE_GRPC: &str = "http://gravitychain.io:9090"; +pub const GRAVITY_NODE_GRPC: &str = "https://gravitychain.io:9090"; //pub const GRAVITY_NODE_GRPC: &str = "http://chainripper-2.althea.net:9090"; pub const GRAVITY_PREFIX: &str = "gravity"; pub const ETH_NODE_RPC: &str = "https://eth.chandrastation.com"; diff --git a/gravity-info-server/src/main.rs b/gravity-info-server/src/main.rs index f7803ed..b3fc909 100644 --- a/gravity-info-server/src/main.rs +++ b/gravity-info-server/src/main.rs @@ -20,15 +20,13 @@ const INFO_SERVER_PORT: u16 = 9000; /// Provides the eip-712 metamask rpc for Gravity Bridge const METAMASK_RPC_PORT: u16 = 8545; -use crate::batch_relaying::generate_raw_batch_tx; +use crate::batch_relaying::generate_batch_tx_responder; use crate::gravity_info::get_erc20_metadata; use crate::total_suppy::get_supply_info; - use crate::volume::get_volume_info; use crate::{gravity_info::get_gravity_info, tls::*}; use actix_cors::Cors; use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; - use env_logger::Env; use futures::future::join; use gravity_info::{blockchain_info_thread, get_eth_info}; @@ -37,7 +35,6 @@ use log::{error, info}; use rocksdb::Options; use rocksdb::DB; use rustls::ServerConfig; - use std::sync::Arc; use total_suppy::chain_total_supply_thread; use transactions::database::transaction_info_thread; @@ -48,7 +45,7 @@ use volume::bridge_volume_thread; #[get("/batch_tx/{batch_nonce}")] async fn generate_batch_tx(data: web::Path<(u64,)>) -> impl Responder { let nonce = data.into_inner().0; - generate_raw_batch_tx(nonce).await + generate_batch_tx_responder(nonce).await } #[get("/total_supply")]