Skip to content

Commit

Permalink
[ETH_BYTECODE_DB] Update verifier alliance db schema (#745)
Browse files Browse the repository at this point in the history
* Update verifier alliance database schema

* Update insertion along with updated verifier_alliance database schema
  • Loading branch information
rimrakhimov authored Jan 17, 2024
1 parent ff0326b commit 7b50065
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ async fn insert_contract_deployment(txn: &DatabaseTransaction, test_case: &TestC
chain_id: Set(test_case.chain_id.into()),
address: Set(test_case.address.to_vec()),
transaction_hash: Set(test_case.transaction_hash.to_vec()),
block_number: Set(Some(test_case.block_number.into())),
txindex: Set(Some(test_case.transaction_index.into())),
deployer: Set(Some(test_case.deployer.to_vec())),
block_number: Set(test_case.block_number.into()),
txindex: Set(test_case.transaction_index.into()),
deployer: Set(test_case.deployer.to_vec()),
contract_id: Set(contract_id),
}
.insert(txn)
Expand Down Expand Up @@ -386,17 +386,15 @@ async fn check_contract_deployment(db: &DatabaseConnection, test_case: &TestCase
"Invalid contract_deployments.transaction_hash"
);
assert_eq!(
Some(test_case_block_number),
contract_deployment.block_number,
test_case_block_number, contract_deployment.block_number,
"Invalid contract_deployments.block_number"
);
assert_eq!(
Some(test_case_transaction_index),
contract_deployment.txindex,
test_case_transaction_index, contract_deployment.txindex,
"Invalid contract_deployments.txindex"
);
assert_eq!(
Some(test_case.deployer.to_vec()),
test_case.deployer.to_vec(),
contract_deployment.deployer,
"Invalid contract_deployments.deployer"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::Context;
use blockscout_display_bytes::Bytes as DisplayBytes;
use sea_orm::{
entity::prelude::ColumnTrait, ActiveValue::Set, DatabaseConnection, DatabaseTransaction,
EntityTrait, JoinType, QueryFilter, QuerySelect, RelationTrait, TransactionTrait,
EntityTrait, QueryFilter, TransactionTrait,
};
use verifier_alliance_entity::{
code, compiled_contracts, contract_deployments, contracts, verified_contracts,
Expand All @@ -33,26 +33,35 @@ pub(crate) async fn insert_data(
.await
.context("begin database transaction")?;

let contract = retrieve_contract(&txn, &deployment_data)
let contract_deployment = retrieve_contract_deployment(&txn, &deployment_data)
.await
.context("retrieve contract")?
.context("retrieve contract contract_deployment")?
.ok_or_else(|| {
anyhow::anyhow!(
"contract was not found: chain_id={}, address={}, transaction_hash={}",
"contract deployment was not found: chain_id={}, address={}, transaction_hash={}",
deployment_data.chain_id,
DisplayBytes::from(deployment_data.contract_address.clone()),
DisplayBytes::from(deployment_data.transaction_hash.clone())
)
})?;

let contract = retrieve_contract(&txn, &contract_deployment)
.await
.context("retrieve contract")?;

let compiled_contract = insert_compiled_contract(&txn, source_response)
.await
.context("insert compiled_contract")?;

let _verified_contract =
insert_verified_contract(&deployment_data, &txn, &contract, &compiled_contract)
.await
.context("insert verified_contract")?;
let _verified_contract = insert_verified_contract(
&deployment_data,
&txn,
&contract,
&contract_deployment,
&compiled_contract,
)
.await
.context("insert verified_contract")?;

txn.commit().await.context("commit transaction")?;

Expand Down Expand Up @@ -85,15 +94,11 @@ pub(crate) async fn insert_deployment_data(
Ok(())
}

async fn retrieve_contract(
async fn retrieve_contract_deployment(
txn: &DatabaseTransaction,
deployment_data: &ContractDeploymentData,
) -> Result<Option<contracts::Model>, anyhow::Error> {
contracts::Entity::find()
.join(
JoinType::Join,
contracts::Relation::ContractDeployments.def(),
)
) -> Result<Option<contract_deployments::Model>, anyhow::Error> {
contract_deployments::Entity::find()
.filter(contract_deployments::Column::ChainId.eq(deployment_data.chain_id))
.filter(contract_deployments::Column::Address.eq(deployment_data.contract_address.clone()))
.filter(
Expand All @@ -102,7 +107,23 @@ async fn retrieve_contract(
)
.one(txn)
.await
.context("select from \"contracts\" joined with \"contract_deployments\"")
.context("select from \"contract_deployments\"")
}

async fn retrieve_contract(
txn: &DatabaseTransaction,
contract_deployment: &contract_deployments::Model,
) -> Result<contracts::Model, anyhow::Error> {
contracts::Entity::find_by_id(contract_deployment.contract_id)
.one(txn)
.await
.context("select from \"contracts\" by id")?
.ok_or_else(|| {
anyhow::anyhow!(
"contract was not found, though referring contract deployment exists; contract_id={}",
contract_deployment.contract_id
)
})
}

async fn retrieve_code(
Expand Down Expand Up @@ -170,6 +191,7 @@ async fn insert_verified_contract(
deployment_data: &ContractDeploymentData,
txn: &DatabaseTransaction,
contract: &contracts::Model,
contract_deployment: &contract_deployments::Model,
compiled_contract: &compiled_contracts::Model,
) -> Result<verified_contracts::Model, anyhow::Error> {
let (creation_match, creation_values, creation_transformations) = check_code_match(
Expand Down Expand Up @@ -201,8 +223,10 @@ async fn insert_verified_contract(

let active_model = verified_contracts::ActiveModel {
id: Default::default(),
created_at: Default::default(),
updated_at: Default::default(),
deployment_id: Set(contract_deployment.id),
compilation_id: Set(compiled_contract.id),
contract_id: Set(contract.id),
creation_match: Set(creation_match),
creation_values: Set(creation_values),
creation_transformations: Set(creation_transformations),
Expand All @@ -218,7 +242,7 @@ async fn insert_verified_contract(
false,
[
(CompilationId, compiled_contract.id),
(ContractId, contract.id),
(DeploymentId, contract_deployment.id),
]
)?;

Expand Down Expand Up @@ -258,6 +282,8 @@ async fn insert_compiled_contract(

let active_model = compiled_contracts::ActiveModel {
id: Default::default(),
created_at: Default::default(),
updated_at: Default::default(),
compiler: Set(compiler.to_string()),
version: Set(source.compiler_version),
language: Set(language.to_string()),
Expand Down Expand Up @@ -297,9 +323,11 @@ async fn insert_contract_deployment(
chain_id: Set(deployment_data.chain_id.into()),
address: Set(deployment_data.contract_address.clone()),
transaction_hash: Set(deployment_data.transaction_hash.clone()),
block_number: Set(deployment_data.block_number.map(|v| v.into())),
txindex: Set(deployment_data.transaction_index.map(|v| v.into())),
deployer: Set(deployment_data.deployer),
block_number: Set(deployment_data.block_number.unwrap_or(-1).into()),
txindex: Set(deployment_data.transaction_index.unwrap_or(-1).into()),
deployer: Set(deployment_data
.deployer
.unwrap_or(ethers_core::types::Address::zero().0.to_vec())),
contract_id: Set(contract.id),
};
let (contract_deployment, _inserted) = insert_then_select!(
Expand Down
11 changes: 10 additions & 1 deletion eth-bytecode-db/eth-bytecode-db/src/verification/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,18 @@ async fn process_verifier_alliance_db_action(
let database_source = DatabaseReadySource::try_from(source)
.context("Converting source into database ready version")?;

// At least one of creation and runtime code should exist to add the contract into the database.
if creation_code.is_none() && runtime_code.is_none() {
anyhow::bail!("Both creation and runtime codes are nulls")
}

// TODO: make transaction_hash input argument optional, and calculate it
// as `keccak256(creation_code || runtime_code)` in that case
let transaction_hash = transaction_hash.to_vec();
let deployment_data = db::verifier_alliance_db::ContractDeploymentData {
chain_id,
contract_address: contract_address.to_vec(),
transaction_hash: transaction_hash.to_vec(),
transaction_hash,
block_number,
transaction_index,
deployer: deployer.map(|deployer| deployer.to_vec()),
Expand All @@ -357,6 +365,7 @@ async fn process_verifier_alliance_db_action(
.await
.context("Insert deployment data into verifier alliance database")?;
}

db::verifier_alliance_db::insert_data(db_client, database_source, deployment_data)
.await
.context("Insert data into verifier alliance database")?;
Expand Down
14 changes: 6 additions & 8 deletions eth-bytecode-db/eth-bytecode-db/tests/verifier_alliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ async fn insert_contract_deployment(txn: &DatabaseTransaction, test_case: &TestC
chain_id: Set(test_case.chain_id.into()),
address: Set(test_case.address.to_vec()),
transaction_hash: Set(test_case.transaction_hash.to_vec()),
block_number: Set(Some(test_case.block_number.into())),
txindex: Set(Some(test_case.transaction_index.into())),
deployer: Set(Some(test_case.deployer.to_vec())),
block_number: Set(test_case.block_number.into()),
txindex: Set(test_case.transaction_index.into()),
deployer: Set(test_case.deployer.to_vec()),
contract_id: Set(contract_id),
}
.insert(txn)
Expand Down Expand Up @@ -335,17 +335,15 @@ async fn check_contract_deployment(db: &DatabaseConnection, test_case: &TestCase
"Invalid contract_deployments.transaction_hash"
);
assert_eq!(
Some(test_case_block_number),
contract_deployment.block_number,
test_case_block_number, contract_deployment.block_number,
"Invalid contract_deployments.block_number"
);
assert_eq!(
Some(test_case_transaction_index),
contract_deployment.txindex,
test_case_transaction_index, contract_deployment.txindex,
"Invalid contract_deployments.txindex"
);
assert_eq!(
Some(test_case.deployer.to_vec()),
test_case.deployer.to_vec(),
contract_deployment.deployer,
"Invalid contract_deployments.deployer"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

use sea_orm::entity::prelude::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

use sea_orm::entity::prelude::*;

Expand All @@ -7,6 +7,8 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
pub compiler: String,
pub version: String,
pub language: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

use sea_orm::entity::prelude::*;

Expand All @@ -12,10 +12,10 @@ pub struct Model {
pub address: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub transaction_hash: Vec<u8>,
pub block_number: Option<Decimal>,
pub txindex: Option<Decimal>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))", nullable)]
pub deployer: Option<Vec<u8>>,
pub block_number: Decimal,
pub txindex: Decimal,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub deployer: Vec<u8>,
pub contract_id: Uuid,
}

Expand All @@ -29,6 +29,8 @@ pub enum Relation {
on_delete = "NoAction"
)]
Contracts,
#[sea_orm(has_many = "super::verified_contracts::Entity")]
VerifiedContracts,
}

impl Related<super::contracts::Entity> for Entity {
Expand All @@ -37,4 +39,10 @@ impl Related<super::contracts::Entity> for Entity {
}
}

impl Related<super::verified_contracts::Entity> for Entity {
fn to() -> RelationDef {
Relation::VerifiedContracts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

use sea_orm::entity::prelude::*;

Expand Down Expand Up @@ -33,8 +33,6 @@ pub enum Relation {
Code1,
#[sea_orm(has_many = "super::contract_deployments::Entity")]
ContractDeployments,
#[sea_orm(has_many = "super::verified_contracts::Entity")]
VerifiedContracts,
}

impl Related<super::contract_deployments::Entity> for Entity {
Expand All @@ -43,10 +41,4 @@ impl Related<super::contract_deployments::Entity> for Entity {
}
}

impl Related<super::verified_contracts::Entity> for Entity {
fn to() -> RelationDef {
Relation::VerifiedContracts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

pub mod prelude;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

pub use super::{
code::Entity as Code, compiled_contracts::Entity as CompiledContracts,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.11

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "verified_contracts")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(primary_key)]
pub id: i64,
pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
pub deployment_id: Uuid,
pub compilation_id: Uuid,
pub contract_id: Uuid,
pub creation_match: bool,
#[sea_orm(column_type = "JsonBinary", nullable)]
pub creation_values: Option<Json>,
Expand All @@ -32,13 +34,13 @@ pub enum Relation {
)]
CompiledContracts,
#[sea_orm(
belongs_to = "super::contracts::Entity",
from = "Column::ContractId",
to = "super::contracts::Column::Id",
belongs_to = "super::contract_deployments::Entity",
from = "Column::DeploymentId",
to = "super::contract_deployments::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Contracts,
ContractDeployments,
}

impl Related<super::compiled_contracts::Entity> for Entity {
Expand All @@ -47,9 +49,9 @@ impl Related<super::compiled_contracts::Entity> for Entity {
}
}

impl Related<super::contracts::Entity> for Entity {
impl Related<super::contract_deployments::Entity> for Entity {
fn to() -> RelationDef {
Relation::Contracts.def()
Relation::ContractDeployments.def()
}
}

Expand Down
Loading

0 comments on commit 7b50065

Please sign in to comment.