Skip to main content

cargo_tangle/command/eigenlayer/
sync.rs

1/// Synchronize local registrations with on-chain state
2use blueprint_eigenlayer_extra::registration::RegistrationStateManager;
3use blueprint_runner::config::{BlueprintEnvironment, ContextConfig, SupportedChains};
4use blueprint_runner::eigenlayer::config::EigenlayerProtocolSettings;
5use color_eyre::Result;
6use std::path::{Path, PathBuf};
7use url::Url;
8
9/// Synchronize local AVS registrations with on-chain state
10///
11/// # Arguments
12///
13/// * `http_rpc_url` - HTTP RPC endpoint for EigenLayer contracts
14/// * `keystore_uri` - URI for the keystore containing operator keys
15/// * `settings_file` - Optional path to protocol settings file
16///
17/// # Errors
18///
19/// Returns error if:
20/// - Registration state cannot be loaded
21/// - Keystore cannot be accessed
22/// - RPC connection fails
23/// - On-chain queries fail
24pub async fn sync_avs_registrations(
25    http_rpc_url: &Url,
26    keystore_uri: &str,
27    settings_file: Option<&Path>,
28) -> Result<()> {
29    println!("🔄 Synchronizing AVS registrations with on-chain state");
30    println!("   RPC Endpoint: {}", http_rpc_url);
31
32    // Load registration state
33    let mut state_manager = RegistrationStateManager::load()?;
34
35    let registration_count = state_manager.registrations().registrations.len();
36    println!("   Local registrations: {}", registration_count);
37
38    if registration_count == 0 {
39        println!("⚠️  No local registrations to sync");
40        return Ok(());
41    }
42
43    println!("🔑 Using keystore: {}", keystore_uri);
44
45    // Load protocol settings if provided
46    let eigenlayer_settings = if let Some(settings_path) = settings_file {
47        println!(
48            "📄 Loading protocol settings from: {}",
49            settings_path.display()
50        );
51        // TODO: Implement settings file loading
52        // For now, use defaults
53        EigenlayerProtocolSettings::default()
54    } else {
55        println!("📄 Using default protocol settings");
56        EigenlayerProtocolSettings::default()
57    };
58
59    // Create a minimal BlueprintEnvironment for on-chain queries
60    let context_config = ContextConfig::create_eigenlayer_config(
61        http_rpc_url.clone(),
62        http_rpc_url.clone(), // Using HTTP for both
63        keystore_uri.to_string(),
64        None,                                  // No keystore password
65        PathBuf::from("/tmp/eigenlayer_sync"), // Temporary data directory
66        None,                                  // No bridge socket path
67        SupportedChains::LocalTestnet,         // Default to local testnet
68        eigenlayer_settings,
69    );
70
71    let env = BlueprintEnvironment::load_with_config(context_config)
72        .map_err(|e| color_eyre::eyre::eyre!("Failed to create environment: {}", e))?;
73
74    println!();
75    println!("🔍 Verifying registrations on-chain...");
76
77    // Reconcile with on-chain state
78    let changes = state_manager
79        .reconcile_with_chain(&env)
80        .await
81        .map_err(|e| color_eyre::eyre::eyre!("Failed to reconcile with chain: {}", e))?;
82
83    println!();
84    if changes > 0 {
85        println!(
86            "✅ Synchronization complete: {} registration(s) updated",
87            changes
88        );
89        println!();
90        println!("💡 Run 'cargo tangle blueprint eigenlayer list' to see updated registrations");
91    } else {
92        println!("✅ All registrations are in sync with on-chain state");
93    }
94
95    Ok(())
96}