Skip to content

feat(ledger): remove requirement to use the same keypair during replay #458

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 4 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
13 changes: 8 additions & 5 deletions magicblock-api/src/magic_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,14 @@ impl MagicValidator {
config.validator_config.ledger.path.as_ref(),
config.validator_config.ledger.reset,
)?;
Self::sync_validator_keypair_with_ledger(
ledger.ledger_path(),
&identity_keypair,
config.validator_config.ledger.reset,
)?;

if config.validator_config.ledger.enforce_keypair_match {
Self::sync_validator_keypair_with_ledger(
ledger.ledger_path(),
&identity_keypair,
config.validator_config.ledger.reset,
)?;
}

// SAFETY:
// this code will never panic as the ledger_path always appends the
Expand Down
17 changes: 17 additions & 0 deletions magicblock-config/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pub struct LedgerConfig {
#[arg(help = "Whether to reset the ledger before starting the validator.")]
#[serde(default = "bool_true")]
pub reset: bool,
/// Checks that the validator keypair matches the one in the ledger.
#[derive_env_var]
#[arg(
help = "Whether to check that the validator keypair matches the one in the ledger."
)]
#[serde(default = "bool_true")]
pub enforce_keypair_match: bool,
/// The file system path onto which the ledger should be written at
/// If left empty it will be auto-generated to a temporary folder
#[derive_env_var]
Expand All @@ -39,6 +46,11 @@ impl LedgerConfig {
if self.reset == bool_true() && other.reset != bool_true() {
self.reset = other.reset;
}
if self.enforce_keypair_match == bool_true()
&& other.enforce_keypair_match != bool_true()
{
self.enforce_keypair_match = other.enforce_keypair_match;
}
if self.path == Default::default() && other.path != Default::default() {
self.path = other.path;
}
Expand All @@ -54,6 +66,7 @@ impl Default for LedgerConfig {
fn default() -> Self {
Self {
reset: bool_true(),
enforce_keypair_match: bool_true(),
path: Default::default(),
size: DEFAULT_LEDGER_SIZE_BYTES,
}
Expand All @@ -72,6 +85,7 @@ mod tests {
fn test_merge_with_default() {
let mut config = LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
};
Expand All @@ -88,6 +102,7 @@ mod tests {
let mut config = LedgerConfig::default();
let other = LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
};
Expand All @@ -101,12 +116,14 @@ mod tests {
fn test_merge_non_default() {
let mut config = LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
};
let original_config = config.clone();
let other = LedgerConfig {
reset: true,
enforce_keypair_match: true,
path: Some("ledger2.example.com".to_string()),
size: 10000,
};
Expand Down
4 changes: 4 additions & 0 deletions magicblock-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ mod tests {
},
ledger: LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
},
Expand Down Expand Up @@ -325,6 +326,7 @@ mod tests {
},
ledger: LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
},
Expand Down Expand Up @@ -400,6 +402,7 @@ mod tests {
},
ledger: LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger2.example.com".to_string()),
size: 100000,
},
Expand Down Expand Up @@ -468,6 +471,7 @@ mod tests {
},
ledger: LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("ledger.example.com".to_string()),
size: 1000000000,
},
Expand Down
2 changes: 2 additions & 0 deletions magicblock-config/tests/read_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fn test_load_local_dev_with_programs_toml_envs_override() {
env::set_var("VALIDATOR_COUNTRY_CODE", "CY");
env::set_var("VALIDATOR_FQDN", "magicblock.er.com");
env::set_var("LEDGER_RESET", "false");
env::set_var("LEDGER_ENFORCE_KEYPAIR_MATCH", "false");
env::set_var("LEDGER_PATH", "/hello/world");
env::set_var("METRICS_ENABLED", "false");
env::set_var("METRICS_PORT", "1234");
Expand Down Expand Up @@ -165,6 +166,7 @@ fn test_load_local_dev_with_programs_toml_envs_override() {
},
ledger: LedgerConfig {
reset: false,
enforce_keypair_match: false,
path: Some("/hello/world".to_string()),
size: 123123
},
Expand Down
4 changes: 4 additions & 0 deletions test-integration/configs/restore-ledger-conf.devnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ path = "../target/deploy/program_flexi_counter.so"
id = "DmnRGfyyftzacFb1XadYhWF6vWqXwtQk5tbr6XgR3BA1"
path = "../schedulecommit/elfs/mdp.so"

[[program]]
id = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
path = "../programs/memo/memo.so"

[rpc]
port = 7799

Expand Down
Binary file added test-integration/programs/memo/memo.so
Binary file not shown.
10 changes: 6 additions & 4 deletions test-integration/test-ledger-restore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub fn setup_offline_validator(
reset,
path: Some(ledger_path.display().to_string()),
size: DEFAULT_LEDGER_SIZE_BYTES,
..Default::default()
},
accounts: accounts_config.clone(),
programs,
Expand All @@ -146,6 +147,8 @@ pub fn setup_validator_with_local_remote(
ledger_path: &Path,
programs: Option<Vec<ProgramConfig>>,
reset: bool,
enforce_keypair_match: bool,
loaded_accounts: &LoadedAccounts,
) -> (TempDir, Child, IntegrationTestContext) {
let mut accounts_config = AccountsConfig {
lifecycle: LifecycleMode::Ephemeral,
Expand All @@ -163,19 +166,18 @@ pub fn setup_validator_with_local_remote(
let config = EphemeralConfig {
ledger: LedgerConfig {
reset,
enforce_keypair_match,
path: Some(ledger_path.display().to_string()),
size: DEFAULT_LEDGER_SIZE_BYTES,
..Default::default()
},
accounts: accounts_config.clone(),
programs,
..Default::default()
};

let (default_tmpdir_config, Some(mut validator)) =
start_validator_with_config(
config,
&LoadedAccounts::with_delegation_program_test_authority(),
)
start_validator_with_config(config, &loaded_accounts)
else {
panic!("validator should set up correctly");
};
Expand Down
22 changes: 17 additions & 5 deletions test-integration/test-ledger-restore/tests/00_empty_validator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{path::Path, process::Child};

use integration_test_tools::tmpdir::resolve_tmp_dir;
use integration_test_tools::{
loaded_accounts::LoadedAccounts, tmpdir::resolve_tmp_dir,
};
use test_ledger_restore::{
setup_validator_with_local_remote, wait_for_ledger_persist, TMP_DIR_LEDGER,
};
Expand All @@ -22,8 +24,13 @@ fn restore_ledger_empty_validator() {

fn write(ledger_path: &Path) -> (Child, u64) {
// Launch a validator and airdrop to an account
let (_, mut validator, _) =
setup_validator_with_local_remote(ledger_path, None, true);
let (_, mut validator, _) = setup_validator_with_local_remote(
ledger_path,
None,
true,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

let slot = wait_for_ledger_persist(&mut validator);

Expand All @@ -33,8 +40,13 @@ fn write(ledger_path: &Path) -> (Child, u64) {

fn read(ledger_path: &Path) -> Child {
// Launch another validator reusing ledger
let (_, validator, _) =
setup_validator_with_local_remote(ledger_path, None, false);
let (_, validator, _) = setup_validator_with_local_remote(
ledger_path,
None,
false,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

validator
}
19 changes: 15 additions & 4 deletions test-integration/test-ledger-restore/tests/06_delegated_account.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cleanass::assert_eq;
use integration_test_tools::loaded_accounts::LoadedAccounts;
use std::{path::Path, process::Child};

use integration_test_tools::{expect, tmpdir::resolve_tmp_dir};
Expand Down Expand Up @@ -49,8 +50,13 @@ fn write(ledger_path: &Path, payer: &Keypair) -> (Child, u64) {

// NOTE: in this test we preload the counter program in the ephemeral instead
// of relying on it being cloned from the remote
let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), true);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
true,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

// Airdrop to payer on chain
expect!(
Expand Down Expand Up @@ -104,8 +110,13 @@ fn write(ledger_path: &Path, payer: &Keypair) -> (Child, u64) {
fn read(ledger_path: &Path, payer: &Pubkey) -> Child {
let programs = get_programs();

let (_, mut validator, _) =
setup_validator_with_local_remote(ledger_path, Some(programs), false);
let (_, mut validator, _) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
false,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

wait_for_cloned_accounts_hydration();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cleanass::assert_eq;
use integration_test_tools::loaded_accounts::LoadedAccounts;
use std::{path::Path, process::Child};

use integration_test_tools::{expect, tmpdir::resolve_tmp_dir};
Expand Down Expand Up @@ -46,8 +47,13 @@ fn restore_ledger_containing_delegated_and_committed_account() {
fn write(ledger_path: &Path, payer: &Keypair) -> (Child, u64) {
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), true);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
true,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

// Airdrop to payer on chain
expect!(
Expand Down Expand Up @@ -167,8 +173,13 @@ fn write(ledger_path: &Path, payer: &Keypair) -> (Child, u64) {
fn read(ledger_path: &Path, payer: &Pubkey) -> Child {
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), false);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
false,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

wait_for_cloned_accounts_hydration();

Expand Down
19 changes: 15 additions & 4 deletions test-integration/test-ledger-restore/tests/08_commit_update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cleanass::assert_eq;
use integration_test_tools::loaded_accounts::LoadedAccounts;
use std::{path::Path, process::Child};

use integration_test_tools::{expect, tmpdir::resolve_tmp_dir};
Expand Down Expand Up @@ -49,8 +50,13 @@ fn restore_ledger_committed_and_updated_account() {
fn write(ledger_path: &Path, payer: &Keypair) -> (Child, u64) {
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), true);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
true,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

// Airdrop to payer on chain
expect!(
Expand Down Expand Up @@ -158,8 +164,13 @@ fn read(ledger_path: &Path, payer_kp: &Keypair) -> Child {
let payer = &payer_kp.pubkey();
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), false);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
false,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

wait_for_cloned_accounts_hydration();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cleanass::assert_eq;
use integration_test_tools::loaded_accounts::LoadedAccounts;
use std::{path::Path, process::Child};

use integration_test_tools::{expect, tmpdir::resolve_tmp_dir};
Expand Down Expand Up @@ -63,8 +64,13 @@ fn write(
) -> (Child, u64, u64) {
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), true);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
true,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

// Airdrop to payers on chain
expect!(
Expand Down Expand Up @@ -169,8 +175,13 @@ fn read(
let payer_readonly = &payer_readonly_kp.pubkey();
let programs = get_programs_with_flexi_counter();

let (_, mut validator, ctx) =
setup_validator_with_local_remote(ledger_path, Some(programs), false);
let (_, mut validator, ctx) = setup_validator_with_local_remote(
ledger_path,
Some(programs),
false,
true,
&LoadedAccounts::with_delegation_program_test_authority(),
);

wait_for_cloned_accounts_hydration();

Expand Down
Loading