Skip to content

Commit

Permalink
Shift evm db flush in DVM db flush pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
sieniven committed Jan 24, 2024
1 parent c862f06 commit c86861f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
7 changes: 1 addition & 6 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use crate::{
gas::check_tx_intrinsic_gas,
precompiles::MetachainPrecompiles,
receipt::ReceiptService,
storage::{
traits::{BlockStorage, FlushableStorage},
Storage,
},
storage::{traits::BlockStorage, Storage},
transaction::{
cache::{TransactionCache, ValidateTxInfo},
SignedTx,
Expand Down Expand Up @@ -146,8 +143,6 @@ impl EVMCoreService {
);
storage.put_latest_block(Some(&block))?;
storage.put_block(&block)?;
handler.flush()?;
storage.flush()?;

Ok(handler)
}
Expand Down
14 changes: 11 additions & 3 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ impl EVMServices {
.sender
.send(Notification::Block(block.header.hash()))
.map_err(|e| format_err!(e.to_string()))?;

self.core.clear_account_nonce();
self.core.flush()?;
self.storage.flush()?;
Ok(())
}

Expand Down Expand Up @@ -467,6 +464,17 @@ impl EVMServices {
template.backend.increase_tx_count();
Ok(())
}

///
/// # Safety
///
/// Result cannot be used safety unless `cs_main` lock is taken on C++ side
/// across all usages. Note: To be replaced with a proper lock flow later.
///
pub unsafe fn flush_state_to_db(&self) -> Result<()> {
self.core.flush()?;
self.storage.flush()
}
}

// Block template methods
Expand Down
3 changes: 1 addition & 2 deletions lib/ain-evm/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ impl Storage {
impl Rollback for Storage {
fn disconnect_latest_block(&self) -> Result<()> {
self.cache.disconnect_latest_block()?;
self.blockstore.disconnect_latest_block()?;
self.flush()
self.blockstore.disconnect_latest_block()
}
}
5 changes: 5 additions & 0 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,3 +858,8 @@ fn evm_try_dispatch_pending_transactions_event(raw_tx: &str) -> Result<()> {
.send(Notification::Transaction(signed_tx.hash()))
.map_err(|e| format_err!(e.to_string()))?)
}

#[ffi_fallible]
fn evm_try_flush_db() -> Result<()> {
unsafe { SERVICES.evm.flush_state_to_db() }
}
2 changes: 2 additions & 0 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ pub mod ffi {
result: &mut CrossBoundaryResult,
raw_tx: &str,
);

fn evm_try_flush_db(result: &mut CrossBoundaryResult);
}

// ========= Debug ==========
Expand Down
7 changes: 7 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,13 @@ bool CChainState::FlushStateToDisk(const CChainParams &chainparams,
if (!CoinsTip().Flush() || !pcustomcsDB->Flush()) {
return AbortNode(state, "Failed to write to coin or masternode db to disk");
}
// Flush the EVM chainstate
if (IsEVMEnabled(pcustomcsview->GetAttributes())) {
auto res = XResultStatusLogged(evm_try_flush_db(result));
if (!res) {
return AbortNode(state, "Failed to write to EVM db to disk");
}
}
if (!compactBegin.empty() && !compactEnd.empty()) {
auto time = GetTimeMillis();
pcustomcsDB->Compact(compactBegin, compactEnd);
Expand Down

0 comments on commit c86861f

Please sign in to comment.