Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verifier - extract types conversion into logic crate #697

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"blockscout-auth",
"blockscout-db",
"blockscout-service-launcher",
"conversion-error",
"display-bytes",
"mismatch",
"reqwest-rate-limiter",
Expand Down
20 changes: 20 additions & 0 deletions libs/conversion-primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "conversion-primitives"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
thiserror = "1"
cfg-if = "1.0.0"

tonic-0_8 = { package = "tonic", version = "0.8", optional = true }

[features]
tonic = []

tonic-0_8 = [
"dep:tonic-0_8",
"tonic",
]
73 changes: 73 additions & 0 deletions libs/conversion-primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use thiserror::Error;

#[derive(Error, Debug)]
#[error("invalid argument: {0}")]
pub struct InvalidArgument(String);
impl InvalidArgument {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}

#[derive(Error, Debug)]
#[error("internal error: {0}")]
pub struct InternalError(String);
impl InternalError {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}

#[derive(Error, Debug)]
pub enum Error {
#[error("invalid argument: {0}")]
InvalidArgument(String),
#[error("internal error: {0}")]
InternalError(String),
}

impl Error {
pub fn invalid_argument(message: impl Into<String>) -> Self {
Self::InvalidArgument(message.into())
}

pub fn internal_error(message: impl Into<String>) -> Self {
Self::InternalError(message.into())
}
}

#[cfg(feature = "tonic")]
cfg_if::cfg_if! {
if #[cfg(feature = "tonic-0_8")] {
use tonic_0_8 as tonic;
} else {
compile_error!(
"one of the features ['tonic-0_8'] \
must be enabled"
);
}
}

#[cfg(feature = "tonic")]
impl From<InvalidArgument> for tonic::Status {
fn from(value: InvalidArgument) -> Self {
tonic::Status::invalid_argument(value.0)
}
}

#[cfg(feature = "tonic")]
impl From<InternalError> for tonic::Status {
fn from(value: InternalError) -> Self {
tonic::Status::internal(value.0)
}
}

#[cfg(feature = "tonic")]
impl From<Error> for tonic::Status {
fn from(value: Error) -> Self {
match value {
Error::InvalidArgument(message) => tonic::Status::invalid_argument(message),
Error::InternalError(message) => tonic::Status::internal(message),
}
}
}
135 changes: 12 additions & 123 deletions smart-contract-verifier/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion smart-contract-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"smart-contract-verifier",
"smart-contract-verifier-http",
# "smart-contract-verifier-http",
"smart-contract-verifier-proto",
"smart-contract-verifier-server",
"sig-provider-extension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ blockscout-display-bytes = { version = "1.0" }
blockscout-service-launcher = { version = "0.9.0" }
bytes = "1.3"
config = "0.13"
conversion-primitives = { path = "../../libs/conversion-primitives", features = ["tonic-0_8"] }
cron = "0.11"
ethers-solc = "2.0.10"
ethers-core = "2.0.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use crate::{
VerifySolidityStandardJsonRequest,
},
settings::{Extensions, FetcherSettings, S3FetcherSettings, SoliditySettings},
types::{
LookupMethodsRequestWrapper, LookupMethodsResponseWrapper, StandardJsonParseError,
VerifyResponseWrapper, VerifySolidityMultiPartRequestWrapper,
VerifySolidityStandardJsonRequestWrapper,
},
types::{LookupMethodsRequestWrapper, LookupMethodsResponseWrapper, VerifyResponseWrapper},
};
use s3::{creds::Credentials, Bucket, Region};
use smart_contract_verifier::{
Expand Down Expand Up @@ -89,7 +85,7 @@ impl SolidityVerifier for SolidityVerifierService {
&self,
request: Request<VerifySolidityMultiPartRequest>,
) -> Result<Response<VerifyResponse>, Status> {
let request: VerifySolidityMultiPartRequestWrapper = request.into_inner().into();
let request = request.into_inner();
let chain_id = request
.metadata
.as_ref()
Expand Down Expand Up @@ -160,7 +156,8 @@ impl SolidityVerifier for SolidityVerifierService {
&self,
request: Request<VerifySolidityStandardJsonRequest>,
) -> Result<Response<VerifyResponse>, Status> {
let request: VerifySolidityStandardJsonRequestWrapper = request.into_inner().into();
// let request: VerifySolidityStandardJsonRequestWrapper = request.into_inner().into();
let request = request.into_inner();
let chain_id = request
.metadata
.as_ref()
Expand Down Expand Up @@ -191,17 +188,19 @@ impl SolidityVerifier for SolidityVerifierService {
);

let verification_request = {
let request: Result<_, StandardJsonParseError> = request.try_into();
let request = request.try_into();
if let Err(err) = request {
match err {
StandardJsonParseError::InvalidContent(_) => {
solidity::standard_json::proto::StandardJsonParseError::InvalidContent(_) => {
let response = VerifyResponseWrapper::err(err).into_inner();
tracing::info!(request_id=request_id.to_string(), response=?response, "Request processed");
return Ok(Response::new(response));
}
StandardJsonParseError::BadRequest(_) => {
tracing::info!(request_id=request_id.to_string(), err=%err, "Bad request");
return Err(Status::invalid_argument(err.to_string()));
solidity::standard_json::proto::StandardJsonParseError::InvalidArgument(
error,
) => {
tracing::info!(request_id=request_id.to_string(), err=%error, "Bad request");
return Err(error.into());
}
}
}
Expand Down
Loading
Loading