-
Notifications
You must be signed in to change notification settings - Fork 15
feat: option to skip ledger replay #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
395a8b8
2e4c08c
556d08f
f684bc4
55e964a
1865b70
cdaf658
f723e75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,36 +6,48 @@ use std::{ | |
|
||
use fd_lock::{RwLock, RwLockWriteGuard}; | ||
use log::*; | ||
use magicblock_accounts_db::ACCOUNTSDB_DIR; | ||
use magicblock_config::LedgerResumeStrategy; | ||
use magicblock_ledger::Ledger; | ||
use solana_sdk::{signature::Keypair, signer::EncodableKey}; | ||
use solana_sdk::{clock::Slot, signature::Keypair, signer::EncodableKey}; | ||
|
||
use crate::{ | ||
errors::{ApiError, ApiResult}, | ||
utils::fs::remove_directory_contents_if_exists, | ||
}; | ||
use crate::errors::{ApiError, ApiResult}; | ||
|
||
// ----------------- | ||
// Init | ||
// ----------------- | ||
pub(crate) fn init(ledger_path: PathBuf, reset: bool) -> ApiResult<Ledger> { | ||
if reset { | ||
remove_directory_contents_if_exists(ledger_path.as_path()).map_err( | ||
|err| { | ||
error!( | ||
"Error: Unable to remove {}: {}", | ||
ledger_path.display(), | ||
err | ||
); | ||
ApiError::UnableToCleanLedgerDirectory( | ||
ledger_path.display().to_string(), | ||
) | ||
}, | ||
)?; | ||
pub(crate) fn init( | ||
ledger_path: PathBuf, | ||
resume_strategy: &LedgerResumeStrategy, | ||
) -> ApiResult<(Ledger, Slot)> { | ||
// Save the last slot from the previous ledger to restart from it | ||
let last_slot = if resume_strategy.resume() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know I wasn't very good providing the naming for these things, just wanted to provide quick examples, but anything returning a
|
||
let previous_ledger = Ledger::open(ledger_path.as_path())?; | ||
previous_ledger.get_max_blockhash().map(|(slot, _)| slot)? | ||
} else { | ||
Slot::default() | ||
}; | ||
|
||
if resume_strategy.remove_ledger() { | ||
remove_ledger_directory_if_exists( | ||
ledger_path.as_path(), | ||
resume_strategy, | ||
) | ||
.map_err(|err| { | ||
error!( | ||
"Error: Unable to remove {}: {}", | ||
ledger_path.display(), | ||
err | ||
); | ||
ApiError::UnableToCleanLedgerDirectory( | ||
ledger_path.display().to_string(), | ||
) | ||
})?; | ||
} | ||
|
||
fs::create_dir_all(&ledger_path)?; | ||
|
||
Ok(Ledger::open(ledger_path.as_path())?) | ||
Ok((Ledger::open(ledger_path.as_path())?, last_slot)) | ||
} | ||
|
||
// ----------------- | ||
|
@@ -165,3 +177,39 @@ pub(crate) fn ledger_parent_dir(ledger_path: &Path) -> ApiResult<PathBuf> { | |
})?; | ||
Ok(parent.to_path_buf()) | ||
} | ||
|
||
fn remove_ledger_directory_if_exists( | ||
dir: &Path, | ||
resume_strategy: &LedgerResumeStrategy, | ||
) -> Result<(), std::io::Error> { | ||
if !dir.exists() { | ||
return Ok(()); | ||
} | ||
for entry in fs::read_dir(dir)? { | ||
let entry = entry?; | ||
|
||
// When resuming, keep the accounts db | ||
if entry.file_name() == ACCOUNTSDB_DIR && resume_strategy.resume() { | ||
continue; | ||
} | ||
|
||
// When resuming, keep the validator keypair | ||
if let Ok(validator_keypair_path) = validator_keypair_path(dir) { | ||
if resume_strategy.resume() | ||
&& validator_keypair_path | ||
.file_name() | ||
.map(|key_path| key_path == entry.file_name()) | ||
.unwrap_or(false) | ||
{ | ||
continue; | ||
} | ||
} | ||
|
||
if entry.metadata()?.is_dir() { | ||
fs::remove_dir_all(entry.path())? | ||
} else { | ||
fs::remove_file(entry.path())? | ||
} | ||
} | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,8 @@ use magicblock_committor_service::{ | |
config::ChainConfig, CommittorService, ComputeBudgetConfig, | ||
}; | ||
use magicblock_config::{ | ||
AccountsDbConfig, EphemeralConfig, LifecycleMode, PrepareLookupTables, | ||
ProgramConfig, | ||
AccountsDbConfig, EphemeralConfig, LedgerConfig, LedgerResumeStrategy, | ||
LifecycleMode, PrepareLookupTables, ProgramConfig, | ||
}; | ||
use magicblock_geyser_plugin::rpc::GeyserRpcService; | ||
use magicblock_ledger::{ | ||
|
@@ -196,14 +196,12 @@ impl MagicValidator { | |
config.validator_config.validator.base_fees, | ||
); | ||
|
||
let ledger = Self::init_ledger( | ||
config.validator_config.ledger.path.as_ref(), | ||
config.validator_config.ledger.reset, | ||
)?; | ||
let (ledger, last_slot) = | ||
Self::init_ledger(&config.validator_config.ledger)?; | ||
Self::sync_validator_keypair_with_ledger( | ||
ledger.ledger_path(), | ||
&identity_keypair, | ||
config.validator_config.ledger.reset, | ||
&config.validator_config.ledger.resume_strategy, | ||
)?; | ||
|
||
// SAFETY: | ||
|
@@ -223,7 +221,7 @@ impl MagicValidator { | |
config.validator_config.validator.millis_per_slot, | ||
validator_pubkey, | ||
ledger_parent_path, | ||
ledger.get_max_blockhash().map(|(slot, _)| slot)?, | ||
last_slot, | ||
)?; | ||
|
||
let ledger_truncator = LedgerTruncator::new( | ||
|
@@ -238,7 +236,7 @@ impl MagicValidator { | |
let faucet_keypair = funded_faucet( | ||
&bank, | ||
ledger.ledger_path().as_path(), | ||
config.validator_config.ledger.reset, | ||
&config.validator_config.ledger.resume_strategy, | ||
)?; | ||
|
||
load_programs_into_bank( | ||
|
@@ -520,28 +518,28 @@ impl MagicValidator { | |
} | ||
|
||
fn init_ledger( | ||
ledger_path: Option<&String>, | ||
reset: bool, | ||
) -> ApiResult<Arc<Ledger>> { | ||
let ledger_path = match ledger_path { | ||
ledger_config: &LedgerConfig, | ||
) -> ApiResult<(Arc<Ledger>, Slot)> { | ||
let ledger_path = match &ledger_config.path { | ||
Some(ledger_path) => PathBuf::from(ledger_path), | ||
None => { | ||
let ledger_path = TempDir::new()?; | ||
ledger_path.path().to_path_buf() | ||
} | ||
}; | ||
let ledger = ledger::init(ledger_path, reset)?; | ||
let (ledger, last_slot) = | ||
ledger::init(ledger_path, &ledger_config.resume_strategy)?; | ||
let ledger_shared = Arc::new(ledger); | ||
init_persister(ledger_shared.clone()); | ||
Ok(ledger_shared) | ||
Ok((ledger_shared, last_slot)) | ||
} | ||
|
||
fn sync_validator_keypair_with_ledger( | ||
ledger_path: &Path, | ||
validator_keypair: &Keypair, | ||
reset_ledger: bool, | ||
resume_strategy: &LedgerResumeStrategy, | ||
) -> ApiResult<()> { | ||
if reset_ledger { | ||
if !resume_strategy.resume() { | ||
write_validator_keypair_to_ledger(ledger_path, validator_keypair)?; | ||
} else { | ||
let ledger_validator_keypair = | ||
|
@@ -581,7 +579,7 @@ impl MagicValidator { | |
// Start/Stop | ||
// ----------------- | ||
fn maybe_process_ledger(&self) -> ApiResult<()> { | ||
if self.config.ledger.reset { | ||
if !self.config.ledger.resume_strategy.replay() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return Ok(()); | ||
} | ||
let slot_to_continue_at = process_ledger(&self.ledger, &self.bank)?; | ||
|
@@ -814,7 +812,7 @@ impl MagicValidator { | |
} | ||
} | ||
|
||
if !self.config.ledger.reset { | ||
if self.config.ledger.resume_strategy.resume() { | ||
let remote_account_cloner_worker = | ||
remote_account_cloner_worker.clone(); | ||
tokio::spawn(async move { | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using https://crates.io/crates/const_format since otherwise the method call is doing work every time.
With const_fmt you'd be able to assign to a const and use as you were before.