Skip to content

Commit e7fdd01

Browse files
committed
update save file naming and remove unnecessary merkle_root_dir arg in favor of save_path
1 parent d58b148 commit e7fdd01

File tree

9 files changed

+35
-324
lines changed

9 files changed

+35
-324
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ jobs:
178178
name: jito_tip_router_program.so
179179
path: integration_tests/tests/fixtures/
180180
- uses: taiki-e/install-action@nextest
181-
- run: cargo nextest run --all-features -E 'not test(ledger_utils::tests) and not test(test_meta_merkle_creation_from_ledger)'
181+
- run: cargo nextest run --all-features -E 'not test(ledger_utils::tests)'
182182
env:
183183
SBF_OUT_DIR: ${{ github.workspace }}/integration_tests/tests/fixtures
184184

format.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cargo fmt --all
1414

1515
print_executing "cargo nextest run --all-features"
1616
cargo build-sbf --sbf-out-dir integration_tests/tests/fixtures
17-
SBF_OUT_DIR=integration_tests/tests/fixtures cargo nextest run --all-features -E 'not test(ledger_utils::tests::test_get_bank_from_ledger_success) and not test(test_meta_merkle_creation_from_ledger)'
17+
SBF_OUT_DIR=integration_tests/tests/fixtures cargo nextest run --all-features -E 'not test(ledger_utils::tests::test_get_bank_from_ledger_success)'
1818

1919
# Code coverage only runs with flag
2020
if [[ "$*" == *"--code-coverage"* ]]; then

tip-router-operator-cli/src/claim.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use solana_sdk::{
3232
use thiserror::Error;
3333

3434
use crate::{
35+
merkle_tree_collection_file_name,
3536
rpc_utils::{get_batched_accounts, send_until_blockhash_expires},
3637
Cli,
3738
};
@@ -77,10 +78,9 @@ pub async fn claim_mev_tips_with_emit(
7778
let keypair = read_keypair_file(cli.keypair_path.clone())
7879
.map_err(|e| anyhow::anyhow!("Failed to read keypair file: {:?}", e))?;
7980
let keypair = Arc::new(keypair);
80-
let meta_merkle_tree_dir = cli.meta_merkle_tree_dir.clone();
81+
let meta_merkle_tree_dir = cli.save_path.clone();
8182
let rpc_url = cli.rpc_url.clone();
82-
let merkle_tree_coll_path =
83-
meta_merkle_tree_dir.join(format!("generated_merkle_tree_{}.json", epoch));
83+
let merkle_tree_coll_path = meta_merkle_tree_dir.join(merkle_tree_collection_file_name(epoch));
8484
let merkle_tree_coll = GeneratedMerkleTreeCollection::new_from_file(&merkle_tree_coll_path)
8585
.map_err(|e| anyhow::anyhow!(e))?;
8686
let start = Instant::now();

tip-router-operator-cli/src/cli.rs

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub struct Cli {
3030
#[arg(short, long, env)]
3131
pub snapshot_output_dir: PathBuf,
3232

33-
#[arg(short, long, env)]
34-
pub meta_merkle_tree_dir: PathBuf,
35-
3633
#[arg(long, env, default_value = "false")]
3734
pub submit_as_memo: bool,
3835

tip-router-operator-cli/src/lib.rs

+18-230
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ pub mod process_epoch;
1212
pub mod rpc_utils;
1313
pub mod submit;
1414

15-
use std::fs::{self, File};
16-
use std::io::{BufWriter, Write};
15+
use std::fs;
1716
use std::path::{Path, PathBuf};
1817
use std::process::Command;
1918
use std::sync::Arc;
@@ -27,15 +26,14 @@ use jito_tip_payment_sdk::{
2726
TIP_ACCOUNT_SEED_7,
2827
};
2928
use ledger_utils::get_bank_from_ledger;
30-
use log::{error, info};
31-
use meta_merkle_tree::generated_merkle_tree::MerkleRootGeneratorError;
29+
use log::info;
3230
use meta_merkle_tree::generated_merkle_tree::StakeMetaCollection;
3331
use meta_merkle_tree::{
3432
generated_merkle_tree::GeneratedMerkleTreeCollection, meta_merkle_tree::MetaMerkleTree,
3533
};
3634
use solana_metrics::{datapoint_error, datapoint_info};
3735
use solana_runtime::bank::Bank;
38-
use solana_sdk::{account::AccountSharedData, pubkey::Pubkey, slot_history::Slot};
36+
use solana_sdk::{account::AccountSharedData, pubkey::Pubkey};
3937
use stake_meta_generator::generate_stake_meta_collection;
4038

4139
// TODO: Should this be loaded from somewhere?
@@ -52,6 +50,18 @@ pub enum OperatorState {
5250
WaitForNextEpoch,
5351
}
5452

53+
pub fn stake_meta_file_name(epoch: u64) -> String {
54+
format!("{}_stake_meta_collection.json", epoch)
55+
}
56+
57+
pub fn merkle_tree_collection_file_name(epoch: u64) -> String {
58+
format!("{}_merkle_tree_collection.json", epoch)
59+
}
60+
61+
pub fn meta_merkle_tree_file_name(epoch: u64) -> String {
62+
format!("{}_meta_merkle_tree.json", epoch)
63+
}
64+
5565
// STAGE 1 LoadBankFromSnapshot
5666
pub fn load_bank_from_snapshot(cli: Cli, slot: u64, store_snapshot: bool) -> Arc<Bank> {
5767
let ledger_path = cli.ledger_path.clone();
@@ -118,7 +128,7 @@ pub fn create_stake_meta(
118128
if save {
119129
// Note: We have the epoch come before the file name so ordering is neat on a machine
120130
// with multiple epochs saved.
121-
let file = save_path.join(format!("{}_stake_meta_collection.json", epoch));
131+
let file = save_path.join(stake_meta_file_name(epoch));
122132
stake_meta_coll.write_to_file(&file);
123133
}
124134

@@ -189,7 +199,7 @@ pub fn create_merkle_tree_collection(
189199
if save {
190200
// Note: We have the epoch come before the file name so ordering is neat on a machine
191201
// with multiple epochs saved.
192-
let file = save_path.join(format!("{}_merkle_tree_collection.json", epoch));
202+
let file = save_path.join(merkle_tree_collection_file_name(epoch));
193203
match merkle_tree_coll.write_to_file(&file) {
194204
Ok(_) => {}
195205
Err(e) => {
@@ -253,7 +263,7 @@ pub fn create_meta_merkle_tree(
253263
if save {
254264
// Note: We have the epoch come before the file name so ordering is neat on a machine
255265
// with multiple epochs saved.
256-
let file = save_path.join(format!("{}_meta_merkle_tree.json", epoch));
266+
let file = save_path.join(meta_merkle_tree_file_name(epoch));
257267
match meta_merkle_tree.write_to_file(&file) {
258268
Ok(_) => {}
259269
Err(e) => {
@@ -323,235 +333,13 @@ fn derive_tip_payment_pubkeys(program_id: &Pubkey) -> TipPaymentPubkeys {
323333
}
324334
}
325335

326-
fn write_to_json_file(
327-
merkle_tree_coll: &GeneratedMerkleTreeCollection,
328-
file_path: &PathBuf,
329-
) -> std::result::Result<(), MerkleRootGeneratorError> {
330-
let file = File::create(file_path)?;
331-
let mut writer = BufWriter::new(file);
332-
let json = serde_json::to_string_pretty(&merkle_tree_coll).unwrap();
333-
writer.write_all(json.as_bytes())?;
334-
writer.flush()?;
335-
336-
Ok(())
337-
}
338-
339336
/// Convenience wrapper around [TipDistributionAccount]
340337
pub struct TipDistributionAccountWrapper {
341338
pub tip_distribution_account: TipDistributionAccount,
342339
pub account_data: AccountSharedData,
343340
pub tip_distribution_pubkey: Pubkey,
344341
}
345342

346-
#[allow(clippy::too_many_arguments)]
347-
pub fn get_meta_merkle_root(
348-
ledger_path: &Path,
349-
account_paths: Vec<PathBuf>,
350-
full_snapshots_path: PathBuf,
351-
incremental_snapshots_path: PathBuf,
352-
desired_slot: &Slot,
353-
tip_distribution_program_id: &Pubkey,
354-
out_path: &str,
355-
tip_payment_program_id: &Pubkey,
356-
tip_router_program_id: &Pubkey,
357-
ncn_address: &Pubkey,
358-
operator_address: &Pubkey,
359-
epoch: u64,
360-
protocol_fee_bps: u64,
361-
snapshots_enabled: bool,
362-
meta_merkle_tree_dir: &Path,
363-
) -> std::result::Result<MetaMerkleTree, MerkleRootError> {
364-
let start = Instant::now();
365-
366-
datapoint_info!(
367-
"tip_router_cli.get_meta_merkle_root",
368-
("operator_address", operator_address.to_string(), String),
369-
("state", "stake_meta_generation", String),
370-
("step", 1, i64),
371-
("epoch", epoch, i64),
372-
("duration_ms", start.elapsed().as_millis() as i64, i64)
373-
);
374-
375-
// cleanup tmp files - update with path where stake meta is written
376-
match cleanup_tmp_files(&incremental_snapshots_path) {
377-
Ok(_) => {}
378-
Err(e) => {
379-
datapoint_info!(
380-
"tip_router_cli.get_meta_merkle_root",
381-
("operator_address", operator_address.to_string(), String),
382-
("state", "cleanup_tmp_files", String),
383-
("error", format!("{:?}", e), String),
384-
("epoch", epoch, i64),
385-
("duration_ms", start.elapsed().as_millis() as i64, i64)
386-
);
387-
}
388-
}
389-
390-
// Get stake meta collection
391-
let stake_meta_collection = stake_meta_generator::generate_stake_meta(
392-
operator_address,
393-
ledger_path,
394-
account_paths,
395-
full_snapshots_path,
396-
incremental_snapshots_path.clone(),
397-
desired_slot,
398-
tip_distribution_program_id,
399-
out_path,
400-
tip_payment_program_id,
401-
snapshots_enabled,
402-
)
403-
.map_err(|e| {
404-
MerkleRootError::StakeMetaGeneratorError(format!("Failed to generate stake meta: {:?}", e))
405-
})?;
406-
407-
info!(
408-
"Created StakeMetaCollection:\n - epoch: {:?}\n - slot: {:?}\n - num stake metas: {:?}\n - bank_hash: {:?}",
409-
stake_meta_collection.epoch,
410-
stake_meta_collection.slot,
411-
stake_meta_collection.stake_metas.len(),
412-
stake_meta_collection.bank_hash
413-
);
414-
415-
datapoint_info!(
416-
"tip_router_cli.get_meta_merkle_root",
417-
("operator_address", operator_address.to_string(), String),
418-
("state", "generated_merkle_tree_collection", String),
419-
("step", 2, i64),
420-
("epoch", epoch, i64),
421-
("duration_ms", start.elapsed().as_millis() as i64, i64)
422-
);
423-
424-
// Cleanup tmp files
425-
match cleanup_tmp_files(&incremental_snapshots_path) {
426-
Ok(_) => {}
427-
Err(e) => {
428-
datapoint_info!(
429-
"tip_router_cli.get_meta_merkle_root",
430-
("operator_address", operator_address.to_string(), String),
431-
("state", "cleanup_tmp_files", String),
432-
("error", format!("{:?}", e), String),
433-
("epoch", epoch, i64),
434-
("duration_ms", start.elapsed().as_millis() as i64, i64)
435-
);
436-
}
437-
}
438-
439-
// Generate merkle tree collection
440-
let merkle_tree_coll = GeneratedMerkleTreeCollection::new_from_stake_meta_collection(
441-
stake_meta_collection,
442-
ncn_address,
443-
epoch,
444-
protocol_fee_bps,
445-
tip_router_program_id,
446-
)
447-
.map_err(|_| {
448-
MerkleRootError::MerkleRootGeneratorError(
449-
"Failed to generate merkle tree collection".to_string(),
450-
)
451-
})?;
452-
453-
info!(
454-
"Created GeneratedMerkleTreeCollection:\n - epoch: {:?}\n - slot: {:?}\n - num generated merkle trees: {:?}\n - bank_hash: {:?}",
455-
merkle_tree_coll.epoch,
456-
merkle_tree_coll.slot,
457-
merkle_tree_coll.generated_merkle_trees.len(),
458-
merkle_tree_coll.bank_hash
459-
);
460-
461-
// Write GeneratedMerkleTreeCollection to file for debugging/verification
462-
let generated_merkle_tree_path = incremental_snapshots_path.join(format!(
463-
"generated_merkle_tree_{}.json",
464-
merkle_tree_coll.epoch
465-
));
466-
match write_to_json_file(&merkle_tree_coll, &generated_merkle_tree_path) {
467-
Ok(_) => {
468-
info!(
469-
"Wrote GeneratedMerkleTreeCollection to {}",
470-
generated_merkle_tree_path.display()
471-
);
472-
}
473-
Err(e) => {
474-
error!(
475-
"Failed to write GeneratedMerkleTreeCollection to file {}: {:?}",
476-
generated_merkle_tree_path.display(),
477-
e
478-
);
479-
}
480-
}
481-
482-
datapoint_info!(
483-
"tip_router_cli.get_meta_merkle_root",
484-
("operator_address", operator_address.to_string(), String),
485-
("state", "meta_merkle_tree_creation", String),
486-
("step", 3, i64),
487-
("epoch", epoch, i64),
488-
("duration_ms", start.elapsed().as_millis() as i64, i64)
489-
);
490-
491-
// TODO: Hide this behind a flag when the process gets split up into the various stages and
492-
// checkpoints.
493-
494-
// Write GeneratedMerkleTreeCollection to disk. Required for Claiming
495-
let merkle_tree_coll_path =
496-
meta_merkle_tree_dir.join(format!("generated_merkle_tree_{}.json", epoch));
497-
let generated_merkle_tree_col_json = match serde_json::to_string(&merkle_tree_coll) {
498-
Ok(json) => json,
499-
Err(e) => {
500-
datapoint_error!(
501-
"tip_router_cli.process_epoch",
502-
("operator_address", operator_address.to_string(), String),
503-
("epoch", epoch, i64),
504-
("status", "error", String),
505-
("error", format!("{:?}", e), String),
506-
("state", "merkle_root_serialization", String),
507-
("duration_ms", start.elapsed().as_millis() as i64, i64)
508-
);
509-
return Err(MerkleRootError::MerkleRootGeneratorError(
510-
"Failed to serialize merkle tree collection".to_string(),
511-
));
512-
}
513-
};
514-
515-
if let Err(e) = std::fs::write(merkle_tree_coll_path, generated_merkle_tree_col_json) {
516-
datapoint_error!(
517-
"tip_router_cli.process_epoch",
518-
("operator_address", operator_address.to_string(), String),
519-
("epoch", epoch, i64),
520-
("status", "error", String),
521-
("error", format!("{:?}", e), String),
522-
("state", "merkle_root_file_write", String),
523-
("duration_ms", start.elapsed().as_millis() as i64, i64)
524-
);
525-
return Err(MerkleRootError::MerkleRootGeneratorError(
526-
"Failed to write meta merkle tree to file".to_string(),
527-
));
528-
}
529-
530-
// Convert to MetaMerkleTree
531-
let meta_merkle_tree = MetaMerkleTree::new_from_generated_merkle_tree_collection(
532-
merkle_tree_coll,
533-
)
534-
.map_err(|e| {
535-
MerkleRootError::MerkleTreeError(format!("Failed to create meta merkle tree: {:?}", e))
536-
})?;
537-
538-
info!(
539-
"Created MetaMerkleTree:\n - num nodes: {:?}\n - merkle root: {:?}",
540-
meta_merkle_tree.num_nodes, meta_merkle_tree.merkle_root
541-
);
542-
543-
datapoint_info!(
544-
"tip_router_cli.get_meta_merkle_root",
545-
("operator_address", operator_address.to_string(), String),
546-
("state", "meta_merkle_tree_creation", String),
547-
("step", 4, i64),
548-
("epoch", epoch, i64),
549-
("duration_ms", start.elapsed().as_millis() as i64, i64)
550-
);
551-
552-
Ok(meta_merkle_tree)
553-
}
554-
555343
fn get_validator_cmdline() -> Result<String> {
556344
let output = Command::new("pgrep").arg("solana-validator").output()?;
557345

tip-router-operator-cli/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ async fn main() -> Result<()> {
182182
set_merkle_roots,
183183
} => {
184184
let meta_merkle_tree_path = PathBuf::from(format!(
185-
"{}/meta_merkle_tree_{}.json",
186-
cli.meta_merkle_tree_dir.display(),
185+
"{}/{}_meta_merkle_tree.json",
186+
cli.save_path.display(),
187187
epoch
188188
));
189189
info!(

0 commit comments

Comments
 (0)