Skip to content

Commit

Permalink
SVM: API method for setting cache fork graph
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 15, 2024
1 parent 9695566 commit 5d4d0f3
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/tests/unified_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn test_scheduler_waited_by_drop_bank_service() {
bank_forks.write().unwrap().install_scheduler_pool(pool);
let genesis = 0;
let genesis_bank = &bank_forks.read().unwrap().get(genesis).unwrap();
genesis_bank.set_fork_graph_in_program_cache(Arc::downgrade(&bank_forks));
genesis_bank.set_fork_graph_in_program_cache(Arc::clone(&bank_forks));

// Create bank, which is pruned later
let pruned = 2;
Expand Down
9 changes: 3 additions & 6 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ use {
AtomicBool, AtomicI64, AtomicU64, AtomicUsize,
Ordering::{AcqRel, Acquire, Relaxed},
},
Arc, LockResult, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard, Weak,
Arc, LockResult, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
},
thread::Builder,
time::{Duration, Instant},
Expand Down Expand Up @@ -1420,12 +1420,9 @@ impl Bank {
new
}

pub fn set_fork_graph_in_program_cache(&self, fork_graph: Weak<RwLock<BankForks>>) {
pub fn set_fork_graph_in_program_cache(&self, fork_graph: Arc<RwLock<BankForks>>) {
self.transaction_processor
.program_cache
.write()
.unwrap()
.set_fork_graph(fork_graph);
.set_fork_graph_in_program_cache(fork_graph);
}

pub fn prune_program_cache(&self, new_root_slot: Slot, new_root_epoch: Epoch) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl BankForks {
scheduler_pool: None,
}));

root_bank.set_fork_graph_in_program_cache(Arc::downgrade(&bank_forks));
root_bank.set_fork_graph_in_program_cache(Arc::clone(&bank_forks));
bank_forks
}

Expand Down
3 changes: 2 additions & 1 deletion svm/examples/json-rpc/server/src/svm_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ pub fn create_executable_environment(
mock_bank: &mut MockBankCallback,
transaction_processor: &TransactionBatchProcessor<MockForkGraph>,
) {
transaction_processor.set_fork_graph_in_program_cache(fork_graph);

let mut program_cache = transaction_processor.program_cache.write().unwrap();

program_cache.environments = ProgramRuntimeEnvironments {
Expand All @@ -214,7 +216,6 @@ pub fn create_executable_environment(
)),
};

program_cache.fork_graph = Some(Arc::downgrade(&fork_graph));
// add programs to cache
for key in account_keys.iter() {
if let Some(account) = mock_bank.get_account_shared_data(key) {
Expand Down
2 changes: 1 addition & 1 deletion svm/examples/paytube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl PayTubeChannel {
&account_loader,
&feature_set,
&compute_budget,
Arc::clone(&fork_graph),
fork_graph,
);

// The PayTube transaction processing runtime environment.
Expand Down
6 changes: 2 additions & 4 deletions svm/examples/paytube/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ pub(crate) fn create_transaction_batch_processor<CB: TransactionProcessingCallba
fork_graph: Arc<RwLock<PayTubeForkGraph>>,
) -> TransactionBatchProcessor<PayTubeForkGraph> {
let processor = TransactionBatchProcessor::<PayTubeForkGraph>::default();
// Initialize the mocked fork graph.
processor.set_fork_graph_in_program_cache(fork_graph);

{
let mut cache = processor.program_cache.write().unwrap();

// Initialize the mocked fork graph.
// let fork_graph = Arc::new(RwLock::new(PayTubeForkGraph {}));
cache.fork_graph = Some(Arc::downgrade(&fork_graph));

// Initialize a proper cache environment.
// (Use Loader v4 program to initialize runtime v2 if desired)
cache.environments.program_runtime_v1 = Arc::new(
Expand Down
18 changes: 12 additions & 6 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
}
}

pub fn set_fork_graph_in_program_cache(&self, fork_graph: Arc<RwLock<FG>>) {
self.program_cache
.write()
.unwrap()
.set_fork_graph(Arc::downgrade(&fork_graph));
}

/// Returns the current environments depending on the given epoch
/// Returns None if the call could result in a deadlock
#[cfg(feature = "dev-context-only-utils")]
Expand Down Expand Up @@ -1300,8 +1307,8 @@ mod tests {
let mock_bank = MockBankCallback::default();
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let fork_graph = Arc::new(RwLock::new(TestForkGraph {}));
batch_processor.program_cache.write().unwrap().fork_graph =
Some(Arc::downgrade(&fork_graph));
batch_processor.set_fork_graph_in_program_cache(fork_graph);

let key = Pubkey::new_unique();

let mut account_maps: HashMap<Pubkey, u64> = HashMap::new();
Expand All @@ -1315,8 +1322,8 @@ mod tests {
let mock_bank = MockBankCallback::default();
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let fork_graph = Arc::new(RwLock::new(TestForkGraph {}));
batch_processor.program_cache.write().unwrap().fork_graph =
Some(Arc::downgrade(&fork_graph));
batch_processor.set_fork_graph_in_program_cache(fork_graph);

let key = Pubkey::new_unique();

let mut account_data = AccountSharedData::default();
Expand Down Expand Up @@ -1803,8 +1810,7 @@ mod tests {
let mock_bank = MockBankCallback::default();
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::default();
let fork_graph = Arc::new(RwLock::new(TestForkGraph {}));
batch_processor.program_cache.write().unwrap().fork_graph =
Some(Arc::downgrade(&fork_graph));
batch_processor.set_fork_graph_in_program_cache(fork_graph);

let key = Pubkey::new_unique();
let name = "a_builtin_name";
Expand Down
4 changes: 2 additions & 2 deletions svm/tests/concurrent_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn program_cache_execution(threads: usize) {
let mut mock_bank = MockBankCallback::default();
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(5, 5, HashSet::new());
let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
batch_processor.program_cache.write().unwrap().fork_graph = Some(Arc::downgrade(&fork_graph));
batch_processor.set_fork_graph_in_program_cache(fork_graph);

let programs = vec![
deploy_program("hello-solana".to_string(), 0, &mut mock_bank),
Expand Down Expand Up @@ -131,7 +131,7 @@ fn svm_concurrent() {
2,
HashSet::new(),
));
let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
batch_processor.set_fork_graph_in_program_cache(Arc::new(RwLock::new(MockForkGraph {})));

create_executable_environment(
fork_graph.clone(),
Expand Down
3 changes: 1 addition & 2 deletions svm/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ fn run_fixture(fixture: InstrFixture, filename: OsString, execute_as_instr: bool

mock_bank.override_feature_set(feature_set);
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(42, 2, HashSet::new());
batch_processor.set_fork_graph_in_program_cache(Arc::new(RwLock::new(MockForkGraph {})));

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
{
let mut program_cache = batch_processor.program_cache.write().unwrap();
program_cache.environments = ProgramRuntimeEnvironments {
Expand All @@ -256,7 +256,6 @@ fn run_fixture(fixture: InstrFixture, filename: OsString, execute_as_instr: bool
FunctionRegistry::default(),
)),
};
program_cache.fork_graph = Some(Arc::downgrade(&fork_graph.clone()));
}

batch_processor.fill_missing_sysvar_cache_entries(&mock_bank);
Expand Down
8 changes: 2 additions & 6 deletions svm/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,9 @@ fn execute_test_entry(test_entry: SvmTestEntry) {
EXECUTION_EPOCH,
HashSet::new(),
);

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
batch_processor.set_fork_graph_in_program_cache(Arc::new(RwLock::new(MockForkGraph {})));

create_executable_environment(
fork_graph.clone(),
&mock_bank,
&mut batch_processor.program_cache.write().unwrap(),
);
Expand Down Expand Up @@ -1064,11 +1062,9 @@ fn svm_inspect_account() {
EXECUTION_EPOCH,
HashSet::new(),
);

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
batch_processor.set_fork_graph_in_program_cache(Arc::new(RwLock::new(MockForkGraph {})));

create_executable_environment(
fork_graph.clone(),
&mock_bank,
&mut batch_processor.program_cache.write().unwrap(),
);
Expand Down
3 changes: 0 additions & 3 deletions svm/tests/mock_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ pub fn deploy_program_with_upgrade_authority(

#[allow(unused)]
pub fn create_executable_environment(
fork_graph: Arc<RwLock<MockForkGraph>>,
mock_bank: &MockBankCallback,
program_cache: &mut ProgramCache<MockForkGraph>,
) {
Expand All @@ -214,8 +213,6 @@ pub fn create_executable_environment(
)),
};

program_cache.fork_graph = Some(Arc::downgrade(&fork_graph));

// We must fill in the sysvar cache entries

// clock contents are important because we use them for a sysvar loading test
Expand Down
2 changes: 1 addition & 1 deletion unified-scheduler-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ mod tests {
let slot = bank.slot();
let bank_fork = BankForks::new_rw_arc(bank);
let bank = bank_fork.read().unwrap().get(slot).unwrap();
bank.set_fork_graph_in_program_cache(Arc::downgrade(&bank_fork));
bank.set_fork_graph_in_program_cache(Arc::clone(&bank_fork));
(bank, bank_fork)
}

Expand Down

0 comments on commit 5d4d0f3

Please sign in to comment.