Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 34 additions & 9 deletions magicblock-api/src/domain_registry_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{io, sync::Arc, thread, time::Duration};
use anyhow::Context;
use borsh::BorshDeserialize;
use magicblock_rpc_client::{
MagicBlockSendTransactionConfig, MagicblockRpcClient,
BaseLayerBlockhash, MagicBlockSendTransactionConfig, MagicblockRpcClient,
};
use mdp::{
consts::ER_RECORD_SEED,
Expand All @@ -29,16 +29,25 @@ const UNREGISTER_CONFIRMATION_INTERVAL: Duration = Duration::from_millis(400);
pub struct DomainRegistryManager {
client: Arc<RpcClient>,
rpc_client: MagicblockRpcClient,
blockhash_registry: Option<BaseLayerBlockhash>,
}

impl DomainRegistryManager {
pub fn new(url: impl ToString) -> Self {
Self::new_with_commitment(url, CommitmentConfig::confirmed())
pub fn new(
url: impl ToString,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Self {
Self::new_with_commitment(
url,
CommitmentConfig::confirmed(),
blockhash_registry,
)
}

pub fn new_with_commitment(
url: impl ToString,
commitment: CommitmentConfig,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Self {
let client = Arc::new(RpcClient::new_with_commitment(
url.to_string(),
Expand All @@ -47,6 +56,7 @@ impl DomainRegistryManager {
Self {
client: client.clone(),
rpc_client: MagicblockRpcClient::new(client),
blockhash_registry,
}
}

Expand Down Expand Up @@ -189,8 +199,9 @@ impl DomainRegistryManager {
url: impl ToString,
payer: &Keypair,
validator_info: ErRecord,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Result<(), Error> {
let manager = DomainRegistryManager::new(url);
let manager = DomainRegistryManager::new(url, blockhash_registry);
manager.handle_registration(payer, validator_info).await
}

Expand Down Expand Up @@ -239,11 +250,19 @@ impl DomainRegistryManager {

let instruction =
Instruction::new_with_borsh(ID, &instruction, accounts);
let recent_blockhash = self
.client
.get_latest_blockhash()
.await
.context("Failed to get latest blockhash")?;
let recent_blockhash = match self
.blockhash_registry
.as_ref()
.and_then(|r| r.latest_blockhash())
{
Some(cached) => cached,
None => self
.client
.get_latest_blockhash()
.await
.context("Failed to get latest blockhash")?,
};

Ok(Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
Expand All @@ -255,35 +274,41 @@ impl DomainRegistryManager {
pub async fn handle_unregistration_static(
url: impl ToString,
payer: &Keypair,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Result<(), Error> {
info!("Unregistering validator from domain registry");
let manager = DomainRegistryManager::new_with_commitment(
url,
CommitmentConfig::confirmed(),
blockhash_registry,
);
manager.unregister(payer).await
}

pub async fn send_unregistration_static(
url: impl ToString,
payer: &Keypair,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Result<Signature, Error> {
info!("Sending validator unregister transaction");
let manager = DomainRegistryManager::new_with_commitment(
url,
CommitmentConfig::confirmed(),
blockhash_registry,
);
manager.send_unregister(payer).await
}

pub async fn send_unregistration_and_confirm_in_background_static(
url: impl ToString,
payer: &Keypair,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> Result<(Signature, thread::JoinHandle<()>), Error> {
info!("Sending validator unregister transaction");
let manager = DomainRegistryManager::new_with_commitment(
url,
CommitmentConfig::confirmed(),
blockhash_registry,
);
let signature = manager.send_unregister(payer).await?;
let rpc_client = manager.rpc_client.clone();
Expand Down
34 changes: 30 additions & 4 deletions magicblock-api/src/magic_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use magicblock_program::{
TransactionScheduler as ActionTransactionScheduler,
};
use magicblock_replicator::{nats::Broker, ReplicationService};
use magicblock_rpc_client::BaseLayerBlockhash;
use magicblock_services::actions_callback_service::ActionsCallbackService;
use magicblock_task_scheduler::{SchedulerDatabase, TaskSchedulerService};
use magicblock_validator_admin::claim_fees::{claim_fees, ClaimFeesTask};
Expand Down Expand Up @@ -127,6 +128,7 @@ pub struct MagicValidator {
replication_tx: Sender<Message>,
unregister_handle: Option<thread::JoinHandle<()>>,
is_standalone: bool,
blockhash_registry: BaseLayerBlockhash,
}

impl MagicValidator {
Expand Down Expand Up @@ -220,10 +222,22 @@ impl MagicValidator {
.then(Arc::<AtomicU64>::default);

let step_start = Instant::now();
let base_blockhash = BaseLayerBlockhash::default();
base_blockhash
.spawn_blockhash_refresher(
Arc::new(RpcClient::new_with_commitment(
config.rpc_url().to_owned(),
CommitmentConfig::confirmed(),
)),
token.clone(),
)
.await;

let committor_service = Self::init_committor_service(
&config,
ledger.latest_block(),
shared_chain_slot.clone(),
base_blockhash.clone(),
)
.await?;
log_timing("startup", "committor_service_init", step_start);
Expand Down Expand Up @@ -449,6 +463,7 @@ impl MagicValidator {
replication_tx: validator_channels.replication_messages,
unregister_handle: None,
is_standalone,
blockhash_registry: base_blockhash,
})
}

Expand All @@ -457,6 +472,7 @@ impl MagicValidator {
config: &ValidatorParams,
latest_block: &LatestBlock,
chain_slot: Option<Arc<AtomicU64>>,
blockhash_registry: BaseLayerBlockhash,
) -> ApiResult<Option<Arc<CommittorService>>> {
let committor_persist_path =
config.storage.join("committor_service.sqlite");
Expand All @@ -481,6 +497,7 @@ impl MagicValidator {
config.commit.compute_unit_price,
),
actions_timeout: DEFAULT_ACTIONS_TIMEOUT,
blockhash_registry: Some(blockhash_registry),
},
chain_slot,
actions_callback_executor,
Expand Down Expand Up @@ -658,6 +675,7 @@ impl MagicValidator {
config: &ChainOperationConfig,
block_time_ms: u64,
base_fee: u64,
blockhash_registry: Option<BaseLayerBlockhash>,
) -> ApiResult<()> {
let country_code = CountryCode::from(config.country_code.alpha3());
let validator_keypair = validator_authority();
Expand All @@ -676,6 +694,7 @@ impl MagicValidator {
rpc_url,
&validator_keypair,
validator_info,
blockhash_registry,
)
.await
.map_err(|err| {
Expand Down Expand Up @@ -703,7 +722,7 @@ impl MagicValidator {
DomainRegistryManager::send_unregistration_and_confirm_in_background_static(
rpc_url,
&validator_keypair,
)
Some(self.blockhash_registry.clone()))
.await
.map_err(|err| {
ApiError::FailedToUnregisterValidatorOnChain(err.to_string())
Expand Down Expand Up @@ -851,6 +870,7 @@ impl MagicValidator {
let chain_operation_config = self.config.chain_operation.clone();
let block_time_ms = self.config.ledger.block_time_ms();
let base_fee = self.config.validator.basefee;
let bh_registry = self.blockhash_registry.clone();

// Ephemeral mode does a non-blocking startup balance check.
// Intentionally fire-and-forget: the task itself exits the process on failure.
Expand Down Expand Up @@ -893,7 +913,9 @@ impl MagicValidator {
if let Some(ref config) = chain_operation_config {
if !config.claim_fees_frequency.is_zero() {
let step_start = Instant::now();
if let Err(err) = claim_fees(rpc_url.clone()).await {
if let Err(err) =
claim_fees(rpc_url.clone(), bh_registry.clone()).await
{
error!(
error = ?err,
"Failed to claim validator fees on startup"
Expand All @@ -913,6 +935,7 @@ impl MagicValidator {
config,
block_time_ms,
base_fee,
Some(bh_registry.clone()),
)
.await
{
Expand Down Expand Up @@ -1003,8 +1026,11 @@ impl MagicValidator {
.map(|co| co.claim_fees_frequency)
{
let step_start = Instant::now();
self.claim_fees_task
.start(frequency, self.config.rpc_url().to_owned());
self.claim_fees_task.start(
frequency,
self.config.rpc_url().to_owned(),
self.blockhash_registry.clone(),
);
log_timing("startup", "claim_fees_task_start", step_start);
}

Expand Down
22 changes: 6 additions & 16 deletions magicblock-committor-service/src/committor_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,12 @@ impl CommittorProcessor {
);
let rpc_client = Arc::new(rpc_client);
let websocket_uri = chain_config.websocket_uri.clone();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This local is no longer used after the constructor simplification, and cargo check reports it as an unused variable.
Please remove it to keep the crate warning-free.

warning: unused variable: `websocket_uri`
  --> magicblock-committor-service/src/committor_processor.rs:73:13
   |
73 |         let websocket_uri = chain_config.websocket_uri.clone();
   |             ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_websocket_uri`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

let magic_block_rpc_client = match (chain_slot, websocket_uri) {
(Some(chain_slot), websocket_uri) => {
MagicblockRpcClient::new_with_chain_slot_and_websocket(
rpc_client,
chain_slot,
websocket_uri,
)
}
(None, Some(websocket_uri)) => {
MagicblockRpcClient::new_with_websocket(
rpc_client,
Some(websocket_uri),
)
}
(None, None) => MagicblockRpcClient::new(rpc_client),
};
let magic_block_rpc_client = MagicblockRpcClient::new_with_registry(
rpc_client,
chain_slot,
chain_config.websocket_uri.clone(),
chain_config.blockhash_registry.clone(),
);

// Create TableMania
let gc_config = GarbageCollectorConfig::default();
Expand Down
5 changes: 5 additions & 0 deletions magicblock-committor-service/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use magicblock_rpc_client::BaseLayerBlockhash;
use solana_commitment_config::CommitmentConfig;

use crate::compute_budget::ComputeBudgetConfig;
Expand All @@ -13,6 +14,7 @@ pub struct ChainConfig {
pub commitment: CommitmentConfig,
pub compute_budget_config: ComputeBudgetConfig,
pub actions_timeout: Duration,
pub blockhash_registry: Option<BaseLayerBlockhash>,
}

impl ChainConfig {
Expand All @@ -23,6 +25,7 @@ impl ChainConfig {
commitment: CommitmentConfig::confirmed(),
compute_budget_config,
actions_timeout: DEFAULT_ACTIONS_TIMEOUT,
blockhash_registry: None,
}
}

Expand All @@ -33,6 +36,7 @@ impl ChainConfig {
commitment: CommitmentConfig::confirmed(),
compute_budget_config,
actions_timeout: DEFAULT_ACTIONS_TIMEOUT,
blockhash_registry: None,
}
}

Expand All @@ -43,6 +47,7 @@ impl ChainConfig {
commitment: CommitmentConfig::processed(),
compute_budget_config,
actions_timeout: DEFAULT_ACTIONS_TIMEOUT,
blockhash_registry: None,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions magicblock-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ doctest = false
[dependencies]
tracing = { workspace = true }
magicblock-metrics = { workspace = true }
arc-swap = { workspace = true }
tokio-util = { workspace = true }
solana-pubsub-client = { workspace = true }
solana-rpc-client = { workspace = true }
solana-rpc-client-api = { workspace = true }
Expand Down
Loading