From 1625517f77ca766a984ba0e35b08a325d4e28252 Mon Sep 17 00:00:00 2001 From: Rim Rakhimov Date: Mon, 2 Dec 2024 12:26:46 +0400 Subject: [PATCH] Update verification values to support metadata matches --- .../src/verification/handlers/mod.rs | 51 +++++++++---------- .../handlers/verifier_alliance.rs | 12 +++-- .../eth-bytecode-db/src/verification/types.rs | 4 +- .../integration/transformations_types.rs | 5 +- .../src/verifier_alliance/mod.rs | 2 +- .../verifier_alliance/verification_match.rs | 26 ++-------- .../verification_match_values.rs | 8 +-- 7 files changed, 44 insertions(+), 64 deletions(-) diff --git a/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs b/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs index 4b3b1fec4..270f4e354 100644 --- a/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs +++ b/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs @@ -26,7 +26,7 @@ use anyhow::Context; use sea_orm::DatabaseConnection; use verification_common::verifier_alliance::MatchBuilder; use verifier_alliance_database::{ - CompiledContract, CompiledContractCompiler, CompiledContractLanguage, ContractDeployment, + CompiledContract, CompiledContractCompiler, CompiledContractLanguage, InsertContractDeployment, RetrieveContractDeployment, VerifiedContract, VerifiedContractMatches, }; use verifier_alliance_entity::contract_deployments; @@ -109,7 +109,7 @@ enum VerifierAllianceDbAction<'a> { }, SaveWithDeploymentData { db_client: &'a DatabaseConnection, - deployment_data: ContractDeployment, + deployment_data: InsertContractDeployment, }, } @@ -133,7 +133,7 @@ impl<'a> VerifierAllianceDbAction<'a> { runtime_code: Some(runtime_code), }), ) if is_authorized => { - let contract_deployment = ContractDeployment::Regular { + let contract_deployment = InsertContractDeployment::Regular { chain_id: u128::try_from(chain_id).unwrap(), address: contract_address.to_vec(), transaction_hash: transaction_hash.to_vec(), @@ -157,7 +157,7 @@ impl<'a> VerifierAllianceDbAction<'a> { .. }), ) if is_authorized => { - let contract_deployment = ContractDeployment::Genesis { + let contract_deployment = InsertContractDeployment::Genesis { chain_id: u128::try_from(chain_id).unwrap(), address: contract_address.to_vec(), runtime_code: runtime_code.to_vec(), @@ -364,8 +364,7 @@ async fn process_verifier_alliance_db_action( let database_source = DatabaseReadySource::try_from(source) .context("Converting source into database ready version")?; - let verified_contract = - check_code_matches_copy(db_client, &database_source, &contract_deployment).await?; + let verified_contract = check_code_matches_copy(&database_source, contract_deployment).await?; let _model = verifier_alliance_database::insert_verified_contract(db_client, verified_contract) .await @@ -426,19 +425,23 @@ async fn process_abi_data( async fn retrieve_deployment_from_action( action: VerifierAllianceDbAction<'_>, -) -> Result, anyhow::Error> { +) -> Result< + Option<( + &DatabaseConnection, + verifier_alliance_database::ContractDeployment, + )>, + anyhow::Error, +> { match action { VerifierAllianceDbAction::IgnoreDb => Ok(None), VerifierAllianceDbAction::SaveIfDeploymentExists { db_client, deployment_data, } => { - let contract_deployment = verifier_alliance_database::retrieve_contract_deployment( - db_client, - deployment_data, - ) - .await - .context("retrieve contract deployment")?; + let contract_deployment = + verifier_alliance_database::find_contract_deployment(db_client, deployment_data) + .await + .context("retrieve contract deployment")?; if let Some(contract_deployment) = contract_deployment { return Ok(Some((db_client, contract_deployment))); } @@ -452,7 +455,8 @@ async fn retrieve_deployment_from_action( } => { let contract_deployment = verifier_alliance_database::insert_contract_deployment(db_client, deployment_data) - .await?; + .await + .context("insert contract deployment")?; Ok(Some((db_client, contract_deployment))) } @@ -460,21 +464,16 @@ async fn retrieve_deployment_from_action( } async fn check_code_matches_copy( - db_client: &DatabaseConnection, database_source: &DatabaseReadySource, - contract_deployment: &contract_deployments::Model, + contract_deployment: verifier_alliance_database::ContractDeployment, ) -> Result { let compiled_contract = build_compiled_contract(database_source.clone())?; - let (deployed_creation_code, deployed_runtime_code) = - db::verifier_alliance_db::retrieve_contract_codes(db_client, contract_deployment) - .await - .context("retrieve deployment contract codes")?; - let deployed_runtime_code = deployed_runtime_code.code.unwrap(); + let deployed_creation_code = contract_deployment.creation_code; + let deployed_runtime_code = contract_deployment.runtime_code; let creation_match = if let Some(builder) = deployed_creation_code - .code .as_ref() .and_then(|deployed_creation_code| { MatchBuilder::new( @@ -664,7 +663,7 @@ async fn process_batch_import_response( eth_bytecode_db_client: &DatabaseConnection, alliance_db_client: &DatabaseConnection, response: smart_contract_verifier::BatchVerifyResponse, - deployment_data: Vec, + deployment_data: Vec, ) -> Result { let mut import_result = response.try_into()?; @@ -745,7 +744,7 @@ async fn process_batch_import_response( async fn process_batch_import_verifier_alliance( db_client: &DatabaseConnection, database_source: DatabaseReadySource, - deployment_data: ContractDeployment, + deployment_data: InsertContractDeployment, contract_import_success: &AllianceContractImportSuccess, ) -> Result<(), anyhow::Error> { let contract_deployment = @@ -758,7 +757,7 @@ async fn process_batch_import_verifier_alliance( check_match_statuses( db_client, - &contract_deployment, + &contract_deployment.model, &creation_code_match, &runtime_code_match, ) @@ -767,7 +766,7 @@ async fn process_batch_import_verifier_alliance( db::verifier_alliance_db::insert_data( db_client, database_source, - contract_deployment, + contract_deployment.model, creation_code_match, runtime_code_match, ) diff --git a/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/verifier_alliance.rs b/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/verifier_alliance.rs index 3a1dbb9d1..18cb9b1e6 100644 --- a/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/verifier_alliance.rs +++ b/eth-bytecode-db/eth-bytecode-db/src/verification/handlers/verifier_alliance.rs @@ -10,9 +10,11 @@ use eth_bytecode_db_proto::{blockscout::eth_bytecode_db::v2 as eth_bytecode_db_v use serde::{Deserialize, Serialize}; use smart_contract_verifier_proto::http_client::solidity_verifier_client; use std::{collections::BTreeMap, str::FromStr}; -use verifier_alliance_database::ContractDeployment; +use verifier_alliance_database::InsertContractDeployment; -fn convert_contracts(contracts: Vec) -> Vec { +fn convert_contracts( + contracts: Vec, +) -> Vec { contracts .into_iter() .map(|v| smart_contract_verifier::Contract { @@ -28,7 +30,7 @@ fn convert_contracts(contracts: Vec) -> Vec Result { +) -> Result { let str_to_bytes = |value: &str| { blockscout_display_bytes::decode_hex(value) .map_err(|err| tonic::Status::invalid_argument(err.to_string())) @@ -53,7 +55,7 @@ fn verifier_alliance_contract_try_into_contract_deployment( value.deployer, value.creation_code, ) { - (None, None, None, None, None) => ContractDeployment::Genesis { + (None, None, None, None, None) => InsertContractDeployment::Genesis { chain_id, address, runtime_code, @@ -64,7 +66,7 @@ fn verifier_alliance_contract_try_into_contract_deployment( Some(transaction_index), Some(deployer), Some(creation_code), - ) => ContractDeployment::Regular { + ) => InsertContractDeployment::Regular { chain_id, address, transaction_hash: str_to_bytes(&transaction_hash)?, diff --git a/eth-bytecode-db/eth-bytecode-db/src/verification/types.rs b/eth-bytecode-db/eth-bytecode-db/src/verification/types.rs index 56c03b3bd..5392e5e58 100644 --- a/eth-bytecode-db/eth-bytecode-db/src/verification/types.rs +++ b/eth-bytecode-db/eth-bytecode-db/src/verification/types.rs @@ -8,7 +8,7 @@ use std::collections::BTreeMap; use verification_common::verifier_alliance::{ CompilationArtifacts, CreationCodeArtifacts, RuntimeCodeArtifacts, }; -use verifier_alliance_database::ContractDeployment; +use verifier_alliance_database::InsertContractDeployment; /********** Bytecode Part **********/ #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -488,7 +488,7 @@ impl TryFrom for AllianceContract #[derive(Clone, Debug, PartialEq, Eq)] pub struct AllianceImportRequest { - pub contracts: Vec, + pub contracts: Vec, pub compiler_version: String, pub content: T, } diff --git a/eth-bytecode-db/verifier-alliance-database/tests/integration/transformations_types.rs b/eth-bytecode-db/verifier-alliance-database/tests/integration/transformations_types.rs index 20c12039e..150634a81 100644 --- a/eth-bytecode-db/verifier-alliance-database/tests/integration/transformations_types.rs +++ b/eth-bytecode-db/verifier-alliance-database/tests/integration/transformations_types.rs @@ -1,8 +1,7 @@ -use sea_orm::prelude::{BigDecimal, Uuid}; +use sea_orm::prelude::{Uuid}; use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter}; use serde::{Deserialize, Deserializer}; use serde_json::Value; -use sha2::Sha256; use sha3::{Digest, Keccak256}; use std::collections::BTreeMap; use std::path::PathBuf; @@ -139,7 +138,7 @@ impl TestCase { impl TestCase { pub async fn validate_final_database_state(&self, database_connection: &DatabaseConnection) { - let contract_deployment = self + let _contract_deployment = self .validate_contract_deployments_table(database_connection) .await; let contract = self.validate_contracts_table(database_connection).await; diff --git a/libs/verification-common/src/verifier_alliance/mod.rs b/libs/verification-common/src/verifier_alliance/mod.rs index 4c47f29c4..0070d14bb 100644 --- a/libs/verification-common/src/verifier_alliance/mod.rs +++ b/libs/verification-common/src/verifier_alliance/mod.rs @@ -9,4 +9,4 @@ mod verification_match_values; pub use compilation_artifacts::{CompilationArtifacts, SourceId, ToCompilationArtifacts}; pub use creation_code_artifacts::{CreationCodeArtifacts, ToCreationCodeArtifacts}; pub use runtime_code_artifacts::{RuntimeCodeArtifacts, ToRuntimeCodeArtifacts}; -pub use verification_match::{Match, MatchBuilder, MatchTransformation, MatchType, MatchValues}; +pub use verification_match::{Match, MatchBuilder, MatchTransformation, MatchValues}; diff --git a/libs/verification-common/src/verifier_alliance/verification_match.rs b/libs/verification-common/src/verifier_alliance/verification_match.rs index 73c125e32..ef5a041bf 100644 --- a/libs/verification-common/src/verifier_alliance/verification_match.rs +++ b/libs/verification-common/src/verifier_alliance/verification_match.rs @@ -10,26 +10,10 @@ use alloy_dyn_abi::JsonAbiExt; use anyhow::Context; use bytes::Bytes; use serde::Deserialize; -use std::fmt::{Display, Formatter}; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum MatchType { - Full, - Partial, -} - -impl Display for MatchType { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - MatchType::Full => f.write_str("full"), - MatchType::Partial => f.write_str("partial"), - } - } -} #[derive(Clone, Debug, PartialEq, Eq)] pub struct Match { - pub r#type: MatchType, + pub metadata_match: bool, pub transformations: Vec, pub values: MatchValues, } @@ -90,13 +74,9 @@ impl<'a> MatchBuilder<'a> { if !self.invalid_constructor_arguments && self.deployed_code == self.compiled_code.as_slice() { - let match_type = if self.has_cbor_auxdata_transformation || !self.has_cbor_auxdata { - MatchType::Partial - } else { - MatchType::Full - }; + let metadata_match = self.has_cbor_auxdata && !self.has_cbor_auxdata_transformation; return Some(Match { - r#type: match_type, + metadata_match, transformations: self.transformations, values: self.values, }); diff --git a/libs/verification-common/src/verifier_alliance/verification_match_values.rs b/libs/verification-common/src/verifier_alliance/verification_match_values.rs index 7969c8974..86f6cc255 100644 --- a/libs/verification-common/src/verifier_alliance/verification_match_values.rs +++ b/libs/verification-common/src/verifier_alliance/verification_match_values.rs @@ -8,16 +8,16 @@ use std::collections::BTreeMap; #[serde(rename_all = "camelCase")] #[readonly::make] pub struct Values { - #[serde(skip_serializing_if = "BTreeMap::is_empty")] + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] #[serde_as(as = "BTreeMap<_, blockscout_display_bytes::serde_as::Hex>")] pub cbor_auxdata: BTreeMap, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "Option::is_none")] #[serde_as(as = "Option")] pub constructor_arguments: Option, - #[serde(skip_serializing_if = "BTreeMap::is_empty")] + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] #[serde_as(as = "BTreeMap<_, blockscout_display_bytes::serde_as::Hex>")] pub libraries: BTreeMap, - #[serde(skip_serializing_if = "BTreeMap::is_empty")] + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] #[serde_as(as = "BTreeMap<_, blockscout_display_bytes::serde_as::Hex>")] pub immutables: BTreeMap, }