Skip to content

Commit

Permalink
chore(core): move validation logic to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWad3r committed Feb 4, 2025
1 parent c776558 commit e14b42a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
11 changes: 11 additions & 0 deletions block-util/src/message/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
mod ext_msg_repr;
pub use ext_msg_repr::{create_big_message, create_deep_merkle, ExtMsgRepr, MsgStorageStat};

use crate::message::ext_msg_repr::InvalidExtMsg;

pub async fn validate_external_message(body: &bytes::Bytes) -> Result<(), InvalidExtMsg> {
let cloned = body.clone();
if body.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
tycho_util::sync::rayon_run_fifo(move || ExtMsgRepr::validate(&cloned)).await
} else {
ExtMsgRepr::validate(&cloned)
}
}
14 changes: 3 additions & 11 deletions core/src/blockchain_rpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bytes::{Buf, Bytes};
use everscale_types::models::BlockId;
use futures_util::Future;
use serde::{Deserialize, Serialize};
use tycho_block_util::message::ExtMsgRepr;
use tycho_network::{try_handle_prefix, InboundRequestMeta, Response, Service, ServiceRequest};
use tycho_storage::{ArchiveId, BlockConnection, KeyBlocksDirection, PersistentStateKind, Storage};
use tycho_util::futures::BoxFutureOrNoop;
Expand Down Expand Up @@ -327,19 +326,12 @@ impl<B: BroadcastListener> Service<ServiceRequest> for BlockchainRpcService<B> {
.increment(req.body.len() as u64);

BoxFutureOrNoop::future(async move {
let cloned = req.body.clone();
let validation_result = if req.body.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
tycho_util::sync::rayon_run_fifo(move || ExtMsgRepr::validate(&cloned))
.await
} else {
ExtMsgRepr::validate(&cloned)
};

if let Err(e) = validation_result {
if let Err(e) =
tycho_block_util::message::validate_external_message(&req.body).await
{
tracing::debug!("failed to validate external message: {e:?}");
return;
}

inner
.broadcast_listener
.handle_message(req.metadata, req.body)
Expand Down
24 changes: 7 additions & 17 deletions rpc/src/endpoint/jrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use everscale_types::models::*;
use everscale_types::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use tycho_block_util::message::ExtMsgRepr;
use tycho_storage::{CodeHashesIter, TransactionsIterBuilder};
use tycho_util::metrics::HistogramGuard;
use tycho_util::serde_helpers;
Expand Down Expand Up @@ -65,23 +64,14 @@ pub async fn route(State(state): State<RpcState>, req: Jrpc<Method>) -> Response
}
}
MethodParams::SendMessage(p) => {
let bytes = p.message.clone();
if p.message.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
let validation_result = if p.message.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
tycho_util::sync::rayon_run_fifo(move || ExtMsgRepr::validate(&bytes)).await
} else {
ExtMsgRepr::validate(&bytes)
};

if let Err(e) = validation_result {
return JrpcErrorResponse {
id: Some(req.id),
code: INVALID_BOC_CODE,
message: e.to_string().into(),
}
.into_response();
if let Err(e) = tycho_block_util::message::validate_external_message(&p.message).await {
return JrpcErrorResponse {
id: Some(req.id),
code: INVALID_BOC_CODE,
message: e.to_string().into(),
}
};
.into_response();
}
state.broadcast_external_message(&p.message).await;
ok_to_response(req.id, ())
}
Expand Down
22 changes: 6 additions & 16 deletions rpc/src/endpoint/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bytes::Bytes;
use everscale_types::cell::HashBytes;
use everscale_types::models::*;
use everscale_types::prelude::*;
use tycho_block_util::message::ExtMsgRepr;

pub use self::cache::ProtoEndpointCache;
use self::protos::rpc::{self, request, response, Request};
Expand Down Expand Up @@ -66,22 +65,13 @@ pub async fn route(State(state): State<RpcState>, Protobuf(req): Protobuf<Reques
}
}
Some(request::Call::SendMessage(p)) => {
let bytes = p.message.clone();
if p.message.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
let validation_result = if p.message.len() > ExtMsgRepr::BOUNDARY_BOC_SIZE {
tycho_util::sync::rayon_run_fifo(move || ExtMsgRepr::validate(&bytes)).await
} else {
ExtMsgRepr::validate(&bytes)
};

if let Err(e) = validation_result {
return ProtoErrorResponse {
code: INVALID_BOC_CODE,
message: e.to_string().into(),
}
.into_response();
if let Err(e) = tycho_block_util::message::validate_external_message(&p.message).await {
return ProtoErrorResponse {
code: INVALID_BOC_CODE,
message: e.to_string().into(),
}
};
.into_response();
}

state.broadcast_external_message(&p.message).await;
ok_to_response(response::Result::SendMessage(()))
Expand Down

0 comments on commit e14b42a

Please sign in to comment.