|
| 1 | +use anyhow::Error; |
| 2 | +use entity::{content, execution_metadata}; |
| 3 | +use ethportal_api::{ |
| 4 | + utils::bytes::hex_encode, BlockBodyKey, BlockHeaderKey, BlockReceiptsKey, HistoryContentKey, |
| 5 | + OverlayContentKey, |
| 6 | +}; |
| 7 | +use sea_orm::DatabaseConnection; |
| 8 | +use tracing::{debug, error}; |
| 9 | + |
| 10 | +/// Stores the content keys and block metadata for the given block. |
| 11 | +/// |
| 12 | +/// The metadata included is the block number and hash under the execution |
| 13 | +/// header, body and receipts tables. |
| 14 | +/// |
| 15 | +/// Errors are logged. |
| 16 | +pub async fn store_block_keys( |
| 17 | + block_number: i32, |
| 18 | + block_hash: &[u8; 32], |
| 19 | + conn: &DatabaseConnection, |
| 20 | +) -> Vec<content::Model> { |
| 21 | + let header = HistoryContentKey::BlockHeaderWithProof(BlockHeaderKey { |
| 22 | + block_hash: *block_hash, |
| 23 | + }); |
| 24 | + let body = HistoryContentKey::BlockBody(BlockBodyKey { |
| 25 | + block_hash: *block_hash, |
| 26 | + }); |
| 27 | + let receipts = HistoryContentKey::BlockReceipts(BlockReceiptsKey { |
| 28 | + block_hash: *block_hash, |
| 29 | + }); |
| 30 | + |
| 31 | + let header = store_content_key(&header, "block_header", block_number, conn).await; |
| 32 | + let body = store_content_key(&body, "block_body", block_number, conn).await; |
| 33 | + let receipts = store_content_key(&receipts, "block_receipts", block_number, conn).await; |
| 34 | + |
| 35 | + let mut returned_values = vec![]; |
| 36 | + if let Some(header) = header { |
| 37 | + returned_values.push(header); |
| 38 | + } |
| 39 | + if let Some(body) = body { |
| 40 | + returned_values.push(body); |
| 41 | + } |
| 42 | + if let Some(receipts) = receipts { |
| 43 | + returned_values.push(receipts); |
| 44 | + } |
| 45 | + returned_values |
| 46 | +} |
| 47 | + |
| 48 | +/// Accepts a ContentKey from the History and attempts to store it. |
| 49 | +/// |
| 50 | +/// Errors are logged. |
| 51 | +pub async fn store_content_key<T: OverlayContentKey>( |
| 52 | + key: &T, |
| 53 | + name: &str, |
| 54 | + block_number: i32, |
| 55 | + conn: &DatabaseConnection, |
| 56 | +) -> Option<content::Model> { |
| 57 | + // Store key |
| 58 | + match content::get_or_create(key, conn).await { |
| 59 | + Ok(content_model) => { |
| 60 | + log_record_outcome(key, name, DbOutcome::Success); |
| 61 | + // Store metadata |
| 62 | + let metadata_str = format!("{name}_metadata"); |
| 63 | + match execution_metadata::get_or_create(content_model.id, block_number, conn).await { |
| 64 | + Ok(_) => log_record_outcome(key, metadata_str.as_str(), DbOutcome::Success), |
| 65 | + Err(e) => log_record_outcome(key, metadata_str.as_str(), DbOutcome::Fail(e)), |
| 66 | + }; |
| 67 | + Some(content_model) |
| 68 | + } |
| 69 | + Err(e) => { |
| 70 | + log_record_outcome(key, name, DbOutcome::Fail(e)); |
| 71 | + None |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Logs a database record error for the given key. |
| 77 | +/// |
| 78 | +/// Helper function for common error pattern to be logged. |
| 79 | +pub fn log_record_outcome<T: OverlayContentKey>(key: &T, name: &str, outcome: DbOutcome) { |
| 80 | + match outcome { |
| 81 | + DbOutcome::Success => debug!( |
| 82 | + content.key = hex_encode(key.to_bytes()), |
| 83 | + content.kind = name, |
| 84 | + "Imported new record", |
| 85 | + ), |
| 86 | + DbOutcome::Fail(e) => error!( |
| 87 | + content.key=hex_encode(key.to_bytes()), |
| 88 | + content.kind=name, |
| 89 | + err=?e, |
| 90 | + "Failed to create database record", |
| 91 | + ), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +pub enum DbOutcome { |
| 96 | + Success, |
| 97 | + Fail(Error), |
| 98 | +} |
0 commit comments