Skip to content

Commit

Permalink
feat: run block importer with postgres instead of inmemory (#314)
Browse files Browse the repository at this point in the history
* feat: run block importer with postgres instead of inmemory

* feat: testcontainer

* comment
  • Loading branch information
dinhani-cw authored Mar 5, 2024
1 parent 44c2ec1 commit 9338de5
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 33 deletions.
109 changes: 102 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ fake = { version = "2.9.2", features = ["derive"] }
binary_macros = "1.0.0"
serial_test = "2.0.0"
stringreader = "0.1.1"
testcontainers = "0.15.0"
testcontainers-modules = { version = "0.3.5", features = ["postgres"] }

[build-dependencies]
const-hex = "1.10.0"
Expand Down
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '.justfile_helpers' # _lint, _outdated

# Environment variables (automatically set in all actions).
export RUST_BACKTRACE := "1"
export RUST_LOG := env("RUST_LOG", "stratus=info,importer-download=info,importer-importer=info")

# Default URLs that can be passed as argument.
Expand Down
44 changes: 25 additions & 19 deletions src/eth/storage/inmemory/inmemory_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ use crate::eth::storage::StorageError;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct InMemoryPermanentStorageState {
accounts: HashMap<Address, InMemoryPermanentAccount>,
transactions: HashMap<Hash, TransactionMined>,
blocks_by_number: IndexMap<BlockNumber, Arc<Block>>,
blocks_by_hash: IndexMap<Hash, Arc<Block>>,
logs: Vec<LogMined>,
pub accounts: HashMap<Address, InMemoryPermanentAccount>,
pub transactions: HashMap<Hash, TransactionMined>,
pub blocks_by_number: IndexMap<BlockNumber, Arc<Block>>,
pub blocks_by_hash: IndexMap<Hash, Arc<Block>>,
pub logs: Vec<LogMined>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -194,13 +194,8 @@ impl PermanentStorage for InMemoryPermanentStorage {
let state = self.lock_read().await;

match state.accounts.get(address) {
Some(account) => {
let account = Account {
address: address.clone(),
balance: account.balance.get_at_point(point_in_time).unwrap_or_default(),
nonce: account.nonce.get_at_point(point_in_time).unwrap_or_default(),
bytecode: account.bytecode.get_at_point(point_in_time).unwrap_or_default(),
};
Some(inmemory_account) => {
let account = inmemory_account.to_account(point_in_time);
tracing::trace!(%address, ?account, "account found");
Ok(Some(account))
}
Expand Down Expand Up @@ -434,13 +429,13 @@ impl PermanentStorage for InMemoryPermanentStorage {
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct InMemoryPermanentAccount {
pub struct InMemoryPermanentAccount {
#[allow(dead_code)]
address: Address,
balance: InMemoryHistory<Wei>,
nonce: InMemoryHistory<Nonce>,
bytecode: InMemoryHistory<Option<Bytes>>,
slots: HashMap<SlotIndex, InMemoryHistory<Slot>>,
pub address: Address,
pub balance: InMemoryHistory<Wei>,
pub nonce: InMemoryHistory<Nonce>,
pub bytecode: InMemoryHistory<Option<Bytes>>,
pub slots: HashMap<SlotIndex, InMemoryHistory<Slot>>,
}

impl InMemoryPermanentAccount {
Expand Down Expand Up @@ -477,7 +472,18 @@ impl InMemoryPermanentAccount {
self.slots = new_slots;
}

fn is_contract(&self) -> bool {
/// Checks current account is a contract.
pub fn is_contract(&self) -> bool {
self.bytecode.get_current().is_some()
}

/// Converts itself to an account at a point-in-time.
pub fn to_account(&self, point_in_time: &StoragePointInTime) -> Account {
Account {
address: self.address.clone(),
balance: self.balance.get_at_point(point_in_time).unwrap_or_default(),
nonce: self.nonce.get_at_point(point_in_time).unwrap_or_default(),
bytecode: self.bytecode.get_at_point(point_in_time).unwrap_or_default(),
}
}
}
8 changes: 6 additions & 2 deletions src/infra/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ pub fn init_metrics() {
metrics.extend(metrics_for_storage_read());
metrics.extend(metrics_for_storage_write());

// init provider and buckets
let mut builder = PrometheusBuilder::new().set_buckets(&BUCKET_FOR_DURATION).unwrap();
// init exporter
let mut builder = PrometheusBuilder::new();

// init buckets (comment to use summary)
builder = builder.set_buckets(&BUCKET_FOR_DURATION).unwrap();
for metric in &metrics {
if metric.has_custom_buckets() {
builder = builder.set_buckets_for_metric(Matcher::Full(metric.name.to_string()), &metric.buckets).unwrap();
}
}

builder.install().expect("failed to start metrics");

// init metric description (always after provider started)
Expand Down
Loading

0 comments on commit 9338de5

Please sign in to comment.