Skip to content

Commit

Permalink
feat(verifier-alliance-database): initialize
Browse files Browse the repository at this point in the history
Extract all verifier-alliance database manipulations into separate crate. Will be used further by eth-bytecode-db service
  • Loading branch information
rimrakhimov committed Dec 2, 2024
1 parent 63ea25e commit 1164e09
Show file tree
Hide file tree
Showing 29 changed files with 2,626 additions and 58 deletions.
50 changes: 49 additions & 1 deletion eth-bytecode-db/Cargo.lock

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

13 changes: 10 additions & 3 deletions eth-bytecode-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"eth-bytecode-db/migration",
"eth-bytecode-db/verifier-alliance-entity",
"eth-bytecode-db/verifier-alliance-migration",
"verifier-alliance-database",
]

[workspace.dependencies]
Expand All @@ -17,9 +18,12 @@ eth-bytecode-db-proto = { path = "eth-bytecode-db-proto" }
eth-bytecode-db-server = { path = "eth-bytecode-db-server" }
migration = { path = "eth-bytecode-db/migration" }
verifier-alliance-entity = { git = "https://github.com/blockscout/blockscout-rs", rev = "ec1c755" }
verifier-alliance-database = { path = "verifier-alliance-database" }
verifier-alliance-migration = { git = "https://github.com/blockscout/blockscout-rs", rev = "ec1c755" }
#verifier-alliance-entity = { path = "eth-bytecode-db/verifier-alliance-entity" }
#verifier-alliance-migration = { path = "eth-bytecode-db/verifier-alliance-migration" }

verifier-alliance-entity-v1 = { path = "eth-bytecode-db/verifier-alliance-entity", package = "verifier-alliance-entity" }
verifier-alliance-migration-v1 = { path = "eth-bytecode-db/verifier-alliance-migration", package = "verifier-alliance-migration" }
verification-common-v1 = { path = "../libs/verification-common", package = "verification-common" }

actix-prost = { git = "https://github.com/blockscout/actix-prost", rev = "4cbba2a" }
actix-prost-macros = { git = "https://github.com/blockscout/actix-prost", rev = "4cbba2a" }
Expand Down Expand Up @@ -58,9 +62,12 @@ semver = { version = "1.0" }
serde = { version = "1" }
serde_json = { version = "1.0" }
serde_with = { version = "3.11.0" }
sha2 = { version = "0.10.8" }
sha3 = { version = "0.10.8" }
smart-contract-verifier-proto = { git = "https://github.com/blockscout/blockscout-rs", rev = "7a6e9400" }
solidity-metadata = { version = "1.0" }
sourcify = { git = "https://github.com/blockscout/blockscout-rs", rev = "457af68" }
strum = { version = "0.26.3", default-features = false, features = ["derive"] }
thiserror = { version = "1" }
tokio = { version = "1" }
tokio-stream = { version = "0.1" }
Expand All @@ -69,4 +76,4 @@ tonic-build = { version = "0.8" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
url = { version = "2" }
verification-common = { git = "https://github.com/blockscout/blockscout-rs", rev = "3892914" }
verification-common = { git = "https://github.com/blockscout/blockscout-rs", rev = "3892914" }
25 changes: 25 additions & 0 deletions eth-bytecode-db/verifier-alliance-database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "verifier-alliance-database"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
blockscout-display-bytes = { workspace = true }
keccak-hash = { workspace = true }
sea-orm = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
sha3 = { workspace = true }
strum = { workspace = true }
verification-common-v1 = { workspace = true }
verifier-alliance-entity-v1 = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
blockscout-service-launcher = { workspace = true, features = ["test-database"] }
pretty_assertions = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
verifier-alliance-migration-v1 = { workspace = true }
69 changes: 69 additions & 0 deletions eth-bytecode-db/verifier-alliance-database/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
macro_rules! insert_then_select {
( $txn:expr, $entity_module:ident, $active_model:expr, $update_on_conflict:expr, [ $( ($column:ident, $value:expr) ),+ $(,)? ] ) => {
{
use anyhow::Context;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

let result: Result<_, sea_orm::DbErr> = $entity_module::Entity::insert($active_model.clone())
.on_conflict(sea_orm::sea_query::OnConflict::new().do_nothing().to_owned())
.exec($txn)
.await;

// Returns the model and the bool flag showing whether the model was actually inserted.
match result {
Ok(res) => {
let last_insert_id = res.last_insert_id;
let model = $entity_module::Entity::find_by_id(last_insert_id.clone())
.one($txn)
.await
.context(format!("select from \"{}\" by \"id\"", stringify!($entity_module)))?
.ok_or(anyhow::anyhow!(
"select from \"{}\" by \"id\"={:?} returned no data",
stringify!($entity_module),
last_insert_id
))?;

Ok((model, true))
}
Err(sea_orm::DbErr::RecordNotInserted) => {
let mut model =
$entity_module::Entity::find()
$(
.filter($entity_module::Column::$column.eq($value))
)*
.one($txn)
.await
.context(format!("select from \"{}\" by unique columns", stringify!($entity_module)))?
.ok_or(anyhow::anyhow!("select from \"{}\" by unique columns returned no data", stringify!($entity_module)))?;
// The active model have not been inserted.
// Thus, there were a value already that we need to update.
if $update_on_conflict {
let mut active_model_to_update = $active_model;
for primary_key in <$entity_module::PrimaryKey as sea_orm::Iterable>::iter() {
let column = sea_orm::PrimaryKeyToColumn::into_column(primary_key);
let value = sea_orm::ModelTrait::get(&model, column);
sea_orm::ActiveModelTrait::set(&mut active_model_to_update, column, value);
}
let updated_model = sea_orm::ActiveModelTrait::update(
active_model_to_update, $txn
).await.context(format!("update on conflict in \"{}\"", stringify!($entity_module)))?;

if updated_model != model {
// tracing::warn!(
// model=?model,
// updated_model=?updated_model,
// "the \"{}\" model has been updated",
// stringify!($entity_module)
// );
model = updated_model;
}
}

Ok((model, false))
}
Err(err) => Err(err).context(format!("insert into \"{}\"", stringify!($entity_module))),
}
}
};
}
pub(crate) use insert_then_select;
Loading

0 comments on commit 1164e09

Please sign in to comment.