Skip to content

Commit

Permalink
Update verification values to support metadata matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rimrakhimov committed Dec 2, 2024
1 parent 0c5805b commit 1625517
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 64 deletions.
51 changes: 25 additions & 26 deletions eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,7 +109,7 @@ enum VerifierAllianceDbAction<'a> {
},
SaveWithDeploymentData {
db_client: &'a DatabaseConnection,
deployment_data: ContractDeployment,
deployment_data: InsertContractDeployment,
},
}

Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -426,19 +425,23 @@ async fn process_abi_data(

async fn retrieve_deployment_from_action(
action: VerifierAllianceDbAction<'_>,
) -> Result<Option<(&DatabaseConnection, contract_deployments::Model)>, 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)));
}
Expand All @@ -452,29 +455,25 @@ 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)))
}
}
}

async fn check_code_matches_copy(
db_client: &DatabaseConnection,
database_source: &DatabaseReadySource,
contract_deployment: &contract_deployments::Model,
contract_deployment: verifier_alliance_database::ContractDeployment,
) -> Result<VerifiedContract, anyhow::Error> {
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(
Expand Down Expand Up @@ -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<ContractDeployment>,
deployment_data: Vec<InsertContractDeployment>,
) -> Result<AllianceBatchImportResult, Error> {
let mut import_result = response.try_into()?;

Expand Down Expand Up @@ -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 =
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractDeployment>) -> Vec<smart_contract_verifier::Contract> {
fn convert_contracts(
contracts: Vec<InsertContractDeployment>,
) -> Vec<smart_contract_verifier::Contract> {
contracts
.into_iter()
.map(|v| smart_contract_verifier::Contract {
Expand All @@ -28,7 +30,7 @@ fn convert_contracts(contracts: Vec<ContractDeployment>) -> Vec<smart_contract_v

fn verifier_alliance_contract_try_into_contract_deployment(
value: eth_bytecode_db_v2::VerifierAllianceContract,
) -> Result<ContractDeployment, tonic::Status> {
) -> Result<InsertContractDeployment, tonic::Status> {
let str_to_bytes = |value: &str| {
blockscout_display_bytes::decode_hex(value)
.map_err(|err| tonic::Status::invalid_argument(err.to_string()))
Expand All @@ -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,
Expand All @@ -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)?,
Expand Down
4 changes: 2 additions & 2 deletions eth-bytecode-db/eth-bytecode-db/src/verification/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -488,7 +488,7 @@ impl TryFrom<eth_bytecode_db_v2::VerifierAllianceContract> for AllianceContract

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AllianceImportRequest<T> {
pub contracts: Vec<ContractDeployment>,
pub contracts: Vec<InsertContractDeployment>,
pub compiler_version: String,
pub content: T,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libs/verification-common/src/verifier_alliance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Original file line number Diff line number Diff line change
Expand Up @@ -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<MatchTransformation>,
pub values: MatchValues,
}
Expand Down Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Bytes>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default, skip_serializing_if = "Option::is_none")]
#[serde_as(as = "Option<blockscout_display_bytes::serde_as::Hex>")]
pub constructor_arguments: Option<Bytes>,
#[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<String, Bytes>,
#[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<String, Bytes>,
}
Expand Down

0 comments on commit 1625517

Please sign in to comment.