2024-09-30 05:49:46 +03:00
|
|
|
use actix_web::Error as HttpError;
|
2024-11-28 22:05:14 +02:00
|
|
|
use sequencer_core::sequecer_store::accounts_store::AccountPublicData;
|
2024-09-30 05:49:46 +03:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
|
|
use rpc_primitives::{
|
|
|
|
|
errors::RpcError,
|
|
|
|
|
message::{Message, Request},
|
|
|
|
|
parser::RpcRequest,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
rpc_error_responce_inverter,
|
2024-11-28 22:05:14 +02:00
|
|
|
types::rpc_structs::{
|
|
|
|
|
HelloRequest, HelloResponse, RegisterAccountRequest, RegisterAccountResponse,
|
|
|
|
|
SendTxRequest, SendTxResponse,
|
|
|
|
|
},
|
2024-09-30 05:49:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{respond, types::err_rpc::RpcErr, JsonHandler};
|
|
|
|
|
|
|
|
|
|
impl JsonHandler {
|
|
|
|
|
pub async fn process(&self, message: Message) -> Result<Message, HttpError> {
|
|
|
|
|
let id = message.id();
|
|
|
|
|
if let Message::Request(request) = message {
|
|
|
|
|
let message_inner = self
|
|
|
|
|
.process_request_internal(request)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| e.0)
|
|
|
|
|
.map_err(rpc_error_responce_inverter);
|
|
|
|
|
Ok(Message::response(id, message_inner))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Message::error(RpcError::parse_error(
|
|
|
|
|
"JSON RPC Request format was expected".to_owned(),
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::unused_async)]
|
|
|
|
|
///Example of request processing
|
|
|
|
|
async fn process_temp_hello(&self, request: Request) -> Result<Value, RpcErr> {
|
|
|
|
|
let _hello_request = HelloRequest::parse(Some(request.params))?;
|
|
|
|
|
|
|
|
|
|
let helperstruct = HelloResponse {
|
|
|
|
|
greeting: "HELLO_FROM_SEQUENCER".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
respond(helperstruct)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:05:14 +02:00
|
|
|
async fn process_register_account_request(&self, request: Request) -> Result<Value, RpcErr> {
|
|
|
|
|
let acc_req = RegisterAccountRequest::parse(Some(request.params))?;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut acc_store = self.sequencer_state.lock().await;
|
|
|
|
|
|
|
|
|
|
acc_store.register_account(AccountPublicData::from_raw(
|
|
|
|
|
acc_req.address,
|
|
|
|
|
acc_req.nullifier_public_key,
|
|
|
|
|
acc_req.viewing_public_key,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let helperstruct = RegisterAccountResponse {
|
|
|
|
|
status: "Success".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
respond(helperstruct)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn process_send_tx(&self, request: Request) -> Result<Value, RpcErr> {
|
|
|
|
|
let send_tx_req = SendTxRequest::parse(Some(request.params))?;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut state = self.sequencer_state.lock().await;
|
|
|
|
|
|
|
|
|
|
state.mempool.push_item(send_tx_req.transaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let helperstruct = SendTxResponse {
|
|
|
|
|
status: "Success".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
respond(helperstruct)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 05:49:46 +03:00
|
|
|
pub async fn process_request_internal(&self, request: Request) -> Result<Value, RpcErr> {
|
|
|
|
|
match request.method.as_ref() {
|
|
|
|
|
"hello" => self.process_temp_hello(request).await,
|
2024-11-28 22:05:14 +02:00
|
|
|
"register_account" => self.process_register_account_request(request).await,
|
|
|
|
|
"send_tx" => self.process_send_tx(request).await,
|
2024-09-30 05:49:46 +03:00
|
|
|
_ => Err(RpcErr(RpcError::method_not_found(request.method))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|