Skip to content

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

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions magicblock-accounts-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub type AdbResult<T> = Result<T, AccountsDbError>;
/// some critical operation is in action, e.g. snapshotting
pub type StWLock = Arc<RwLock<()>>;

const ACCOUNTSDB_SUB_DIR: &str = "accountsdb/main";
Copy link
Collaborator

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.

pub const ACCOUNTSDB_DIR: &str = "accountsdb";
fn accountsdb_sub_dir() -> String {
format!("{}/main", ACCOUNTSDB_DIR)
}

pub struct AccountsDb {
/// Main accounts storage, where actual account records are kept
Expand All @@ -41,7 +44,7 @@ impl AccountsDb {
directory: &Path,
lock: StWLock,
) -> AdbResult<Self> {
let directory = directory.join(ACCOUNTSDB_SUB_DIR);
let directory = directory.join(accountsdb_sub_dir());

std::fs::create_dir_all(&directory).inspect_err(log_err!(
"ensuring existence of accountsdb directory"
Expand Down
5 changes: 3 additions & 2 deletions magicblock-api/src/fund_account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;

use magicblock_bank::bank::Bank;
use magicblock_config::LedgerResumeStrategy;
use magicblock_core::magic_program;
use solana_sdk::{
account::Account, clock::Epoch, pubkey::Pubkey, signature::Keypair,
Expand Down Expand Up @@ -56,9 +57,9 @@ pub(crate) fn fund_validator_identity(bank: &Bank, validator_id: &Pubkey) {
pub(crate) fn funded_faucet(
bank: &Bank,
ledger_path: &Path,
create_new: bool,
resume_strategy: &LedgerResumeStrategy,
) -> ApiResult<Keypair> {
let faucet_keypair = if create_new {
let faucet_keypair = if resume_strategy.remove_ledger() {
let faucet_keypair = Keypair::new();
write_faucet_keypair_to_ledger(ledger_path, &faucet_keypair)?;
faucet_keypair
Expand Down
88 changes: 68 additions & 20 deletions magicblock-api/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 bool should be something with is|has|does, etc.

resume looks like an action (in this case I'd think it resumes something and returns true if successful)

is_resuming is much better.

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))
}

// -----------------
Expand Down Expand Up @@ -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(())
}
1 change: 0 additions & 1 deletion magicblock-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod ledger;
pub mod magic_validator;
mod slot;
mod tickers;
mod utils;

pub use init_geyser_service::InitGeyserServiceConfig;
pub use magicblock_config::EphemeralConfig;
36 changes: 17 additions & 19 deletions magicblock-api/src/magic_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_replaying

return Ok(());
}
let slot_to_continue_at = process_ledger(&self.ledger, &self.bank)?;
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 0 additions & 18 deletions magicblock-api/src/utils/fs.rs

This file was deleted.

1 change: 0 additions & 1 deletion magicblock-api/src/utils/mod.rs

This file was deleted.

Loading
Loading