Skip to content

Commit 49616cb

Browse files
authored
feat: set default ledger path (#521)
Closes #520 Replaying a ledger without providing a path created a new, empty ledger without a validator and faucet keypairs, which caused errors. This PR sets a default ledger path and creates the corresponding key pairs if they are missing.
1 parent 851e0b8 commit 49616cb

File tree

9 files changed

+65
-72
lines changed

9 files changed

+65
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ target/
1818

1919
# Ledger
2020
test-ledger/
21+
test-ledger-magicblock/
2122

2223
# Mac
2324
**/.DS_Store

magicblock-api/src/fund_account.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::path::Path;
22

33
use magicblock_bank::bank::Bank;
4-
use magicblock_config::LedgerResumeStrategy;
54
use magicblock_core::magic_program;
65
use solana_sdk::{
76
account::Account, clock::Epoch, pubkey::Pubkey, signature::Keypair,
@@ -57,14 +56,14 @@ pub(crate) fn fund_validator_identity(bank: &Bank, validator_id: &Pubkey) {
5756
pub(crate) fn funded_faucet(
5857
bank: &Bank,
5958
ledger_path: &Path,
60-
resume_strategy: &LedgerResumeStrategy,
6159
) -> ApiResult<Keypair> {
62-
let faucet_keypair = if resume_strategy.is_removing_ledger() {
63-
let faucet_keypair = Keypair::new();
64-
write_faucet_keypair_to_ledger(ledger_path, &faucet_keypair)?;
65-
faucet_keypair
66-
} else {
67-
read_faucet_keypair_from_ledger(ledger_path)?
60+
let faucet_keypair = match read_faucet_keypair_from_ledger(ledger_path) {
61+
Ok(faucet_keypair) => faucet_keypair,
62+
Err(_) => {
63+
let faucet_keypair = Keypair::new();
64+
write_faucet_keypair_to_ledger(ledger_path, &faucet_keypair)?;
65+
faucet_keypair
66+
}
6867
};
6968

7069
fund_account(bank, &faucet_keypair.pubkey(), u64::MAX / 2);

magicblock-api/src/ledger.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,33 @@ use crate::errors::{ApiError, ApiResult};
1717
// Init
1818
// -----------------
1919
pub(crate) fn init(
20-
ledger_path: PathBuf,
20+
ledger_path: &Path,
2121
ledger_config: &LedgerConfig,
2222
) -> ApiResult<(Ledger, Slot)> {
2323
let resume_strategy = &ledger_config.resume_strategy();
2424
let starting_slot = match resume_strategy {
2525
LedgerResumeStrategy::Resume { .. } => {
26-
let previous_ledger = Ledger::open(ledger_path.as_path())?;
26+
let previous_ledger = Ledger::open(ledger_path)?;
2727
previous_ledger.get_max_blockhash().map(|(slot, _)| slot)?
2828
}
2929
LedgerResumeStrategy::Reset { slot, .. } => *slot,
3030
};
3131

3232
if resume_strategy.is_removing_ledger() {
33-
remove_ledger_directory_if_exists(
34-
ledger_path.as_path(),
35-
resume_strategy,
36-
)
37-
.map_err(|err| {
38-
error!(
39-
"Error: Unable to remove {}: {}",
40-
ledger_path.display(),
41-
err
42-
);
43-
ApiError::UnableToCleanLedgerDirectory(
44-
ledger_path.display().to_string(),
45-
)
46-
})?;
33+
remove_ledger_directory_if_exists(ledger_path, resume_strategy)
34+
.map_err(|err| {
35+
error!(
36+
"Error: Unable to remove {}: {}",
37+
ledger_path.display(),
38+
err
39+
);
40+
ApiError::UnableToCleanLedgerDirectory(
41+
ledger_path.display().to_string(),
42+
)
43+
})?;
4744
}
4845

49-
fs::create_dir_all(&ledger_path)?;
50-
51-
Ok((Ledger::open(ledger_path.as_path())?, starting_slot))
46+
Ok((Ledger::open(ledger_path)?, starting_slot))
5247
}
5348

5449
// -----------------

magicblock-api/src/magic_validator.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
net::SocketAddr,
3-
path::{Path, PathBuf},
3+
path::Path,
44
process,
55
sync::{
66
atomic::{AtomicBool, Ordering},
@@ -89,7 +89,6 @@ use solana_sdk::{
8989
signature::Keypair,
9090
signer::Signer,
9191
};
92-
use tempfile::TempDir;
9392
use tokio_util::sync::CancellationToken;
9493

9594
use crate::{
@@ -210,13 +209,12 @@ impl MagicValidator {
210209
Self::init_ledger(&config.validator_config.ledger)?;
211210
info!("Starting slot: {}", starting_slot);
212211

213-
if !config.validator_config.ledger.skip_keypair_match_check {
214-
Self::sync_validator_keypair_with_ledger(
215-
ledger.ledger_path(),
216-
&identity_keypair,
217-
ledger_resume_strategy,
218-
)?;
219-
}
212+
Self::sync_validator_keypair_with_ledger(
213+
ledger.ledger_path(),
214+
&identity_keypair,
215+
ledger_resume_strategy,
216+
config.validator_config.ledger.skip_keypair_match_check,
217+
)?;
220218

221219
// SAFETY:
222220
// this code will never panic as the ledger_path always appends the
@@ -249,11 +247,8 @@ impl MagicValidator {
249247

250248
fund_validator_identity(&bank, &validator_pubkey);
251249
fund_magic_context(&bank);
252-
let faucet_keypair = funded_faucet(
253-
&bank,
254-
ledger.ledger_path().as_path(),
255-
ledger_resume_strategy,
256-
)?;
250+
let faucet_keypair =
251+
funded_faucet(&bank, ledger.ledger_path().as_path())?;
257252

258253
load_programs_into_bank(
259254
&bank,
@@ -554,13 +549,7 @@ impl MagicValidator {
554549
fn init_ledger(
555550
ledger_config: &LedgerConfig,
556551
) -> ApiResult<(Arc<Ledger>, Slot)> {
557-
let ledger_path = match &ledger_config.path {
558-
Some(ledger_path) => PathBuf::from(ledger_path),
559-
None => {
560-
let ledger_path = TempDir::new()?;
561-
ledger_path.path().to_path_buf()
562-
}
563-
};
552+
let ledger_path = Path::new(&ledger_config.path);
564553
let (ledger, last_slot) = ledger::init(ledger_path, ledger_config)?;
565554
let ledger_shared = Arc::new(ledger);
566555
init_persister(ledger_shared.clone());
@@ -571,21 +560,27 @@ impl MagicValidator {
571560
ledger_path: &Path,
572561
validator_keypair: &Keypair,
573562
resume_strategy: &LedgerResumeStrategy,
563+
skip_keypair_match_check: bool,
574564
) -> ApiResult<()> {
575565
if resume_strategy.is_removing_ledger() {
576566
write_validator_keypair_to_ledger(ledger_path, validator_keypair)?;
577-
} else {
578-
let ledger_validator_keypair =
579-
read_validator_keypair_from_ledger(ledger_path)?;
580-
if ledger_validator_keypair.ne(validator_keypair) {
567+
} else if let Ok(ledger_validator_keypair) =
568+
read_validator_keypair_from_ledger(ledger_path)
569+
{
570+
if ledger_validator_keypair.ne(validator_keypair)
571+
&& !skip_keypair_match_check
572+
{
581573
return Err(
582574
ApiError::LedgerValidatorKeypairNotMatchingProvidedKeypair(
583575
ledger_path.display().to_string(),
584576
ledger_validator_keypair.pubkey().to_string(),
585577
),
586578
);
587579
}
580+
} else {
581+
write_validator_keypair_to_ledger(ledger_path, validator_keypair)?;
588582
}
583+
589584
Ok(())
590585
}
591586

magicblock-config/src/ledger.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ pub struct LedgerConfig {
3434
/// The file system path onto which the ledger should be written at
3535
/// If left empty it will be auto-generated to a temporary folder
3636
#[derive_env_var]
37-
#[clap_from_serde_skip]
3837
#[arg(
3938
help = "The file system path onto which the ledger should be written at."
4039
)]
41-
#[serde(default)]
42-
pub path: Option<String>,
40+
#[serde(default = "default_ledger_path")]
41+
pub path: String,
4342
/// The size under which it's desired to keep ledger in bytes.
4443
#[derive_env_var]
4544
#[arg(help = "The size under which it's desired to keep ledger in bytes.")]
@@ -75,7 +74,7 @@ impl Default for LedgerConfig {
7574
Self {
7675
resume_strategy_config: LedgerResumeStrategyConfig::default(),
7776
skip_keypair_match_check: false,
78-
path: Default::default(),
77+
path: default_ledger_path(),
7978
size: DEFAULT_LEDGER_SIZE_BYTES,
8079
}
8180
}
@@ -226,6 +225,10 @@ const fn default_cloning_concurrency() -> usize {
226225
10
227226
}
228227

228+
fn default_ledger_path() -> String {
229+
"test-ledger-magicblock".to_string()
230+
}
231+
229232
#[cfg(test)]
230233
mod tests {
231234
use magicblock_config_helpers::Merge;
@@ -284,7 +287,7 @@ mod tests {
284287
account_hydration_concurrency: 20,
285288
},
286289
skip_keypair_match_check: true,
287-
path: Some("ledger.example.com".to_string()),
290+
path: "ledger.example.com".to_string(),
288291
size: 1000000000,
289292
};
290293
let original_config = config.clone();
@@ -306,7 +309,7 @@ mod tests {
306309
account_hydration_concurrency: 20,
307310
},
308311
skip_keypair_match_check: true,
309-
path: Some("ledger.example.com".to_string()),
312+
path: "ledger.example.com".to_string(),
310313
size: 1000000000,
311314
};
312315

@@ -325,7 +328,7 @@ mod tests {
325328
account_hydration_concurrency: 20,
326329
},
327330
skip_keypair_match_check: true,
328-
path: Some("ledger.example.com".to_string()),
331+
path: "ledger.example.com".to_string(),
329332
size: 1000000000,
330333
};
331334
let original_config = config.clone();
@@ -337,7 +340,7 @@ mod tests {
337340
account_hydration_concurrency: 150,
338341
},
339342
skip_keypair_match_check: true,
340-
path: Some("ledger2.example.com".to_string()),
343+
path: "ledger2.example.com".to_string(),
341344
size: 10000,
342345
};
343346

@@ -368,7 +371,7 @@ size = 1000000000
368371
),
369372
},
370373
skip_keypair_match_check: true,
371-
path: Some("ledger.example.com".to_string()),
374+
path: "ledger.example.com".to_string(),
372375
size: 1000000000,
373376
}
374377
);
@@ -391,7 +394,7 @@ size = 1000000000
391394
),
392395
},
393396
skip_keypair_match_check: false,
394-
path: None,
397+
path: default_ledger_path(),
395398
size: 1000000000,
396399
}
397400
);
@@ -414,7 +417,7 @@ size = 1000000000
414417
),
415418
},
416419
skip_keypair_match_check: false,
417-
path: None,
420+
path: "test-ledger-magicblock".to_string(),
418421
size: 1000000000,
419422
}
420423
);

magicblock-config/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ mod tests {
257257
account_hydration_concurrency: 20,
258258
},
259259
skip_keypair_match_check: true,
260-
path: Some("ledger.example.com".to_string()),
260+
path: "ledger.example.com".to_string(),
261261
size: 1000000000,
262262
},
263263
programs: vec![ProgramConfig {
@@ -346,7 +346,7 @@ mod tests {
346346
account_hydration_concurrency: 20,
347347
},
348348
skip_keypair_match_check: true,
349-
path: Some("ledger.example.com".to_string()),
349+
path: "ledger.example.com".to_string(),
350350
size: 1000000000,
351351
},
352352
programs: vec![ProgramConfig {
@@ -432,7 +432,7 @@ mod tests {
432432
account_hydration_concurrency: 20,
433433
},
434434
skip_keypair_match_check: true,
435-
path: Some("ledger2.example.com".to_string()),
435+
path: "ledger2.example.com".to_string(),
436436
size: 100000,
437437
},
438438
programs: vec![ProgramConfig {
@@ -511,7 +511,7 @@ mod tests {
511511
account_hydration_concurrency: 20,
512512
},
513513
skip_keypair_match_check: true,
514-
path: Some("ledger.example.com".to_string()),
514+
path: "ledger.example.com".to_string(),
515515
size: 1000000000,
516516
},
517517
programs: vec![ProgramConfig {
@@ -569,7 +569,7 @@ mod tests {
569569
account_hydration_concurrency: 20,
570570
},
571571
skip_keypair_match_check: true,
572-
path: Some("ledger.example.com".to_string()),
572+
path: "ledger.example.com".to_string(),
573573
size: 1000000000,
574574
},
575575
programs: vec![],

magicblock-config/tests/parse_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn test_everything_defined() {
274274
account_hydration_concurrency: 20,
275275
},
276276
skip_keypair_match_check: true,
277-
path: Some("ledger.example.com".to_string()),
277+
path: "ledger.example.com".to_string(),
278278
size: 1_000_000_000,
279279
},
280280
programs: vec![ProgramConfig {

magicblock-config/tests/read_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn test_load_local_dev_with_programs_toml_envs_override() {
199199
account_hydration_concurrency: 20,
200200
},
201201
skip_keypair_match_check: true,
202-
path: Some("/hello/world".to_string()),
202+
path: "/hello/world".to_string(),
203203
size: 123123,
204204
},
205205
metrics: MetricsConfig {

test-integration/test-ledger-restore/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn setup_offline_validator(
6161
ledger: LedgerConfig {
6262
resume_strategy_config: resume_strategy.into(),
6363
skip_keypair_match_check,
64-
path: Some(ledger_path.display().to_string()),
64+
path: ledger_path.display().to_string(),
6565
size: DEFAULT_LEDGER_SIZE_BYTES,
6666
},
6767
accounts: accounts_config.clone(),
@@ -121,7 +121,7 @@ pub fn setup_validator_with_local_remote(
121121
ledger: LedgerConfig {
122122
resume_strategy_config,
123123
skip_keypair_match_check,
124-
path: Some(ledger_path.display().to_string()),
124+
path: ledger_path.display().to_string(),
125125
size: DEFAULT_LEDGER_SIZE_BYTES,
126126
},
127127
accounts: accounts_config.clone(),

0 commit comments

Comments
 (0)