Skip to content

Commit d88eb8b

Browse files
committed
remove one chain state view + nits (#4623)
## Motivation Shared state views should only be used for GraphQL / service queries. ## Proposal Introduce a proper enum and return value (we were halfway there) ## Test Plan CI ## Release Plan - These changes should be backported to the latest `testnet` branch, then - be released in a new SDK,
1 parent ee89e7e commit d88eb8b

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

linera-core/src/chain_worker/actor.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use tracing::{debug, instrument, trace, Instrument as _};
3333

3434
use super::{config::ChainWorkerConfig, state::ChainWorkerState, DeliveryNotifier};
3535
use crate::{
36+
chain_worker::BlockOutcome,
3637
data_types::{ChainInfoQuery, ChainInfoResponse},
3738
value_cache::ValueCache,
3839
worker::{NetworkActions, WorkerError},
@@ -100,7 +101,8 @@ where
100101
ProcessValidatedBlock {
101102
certificate: ValidatedBlockCertificate,
102103
#[debug(skip)]
103-
callback: oneshot::Sender<Result<(ChainInfoResponse, NetworkActions, bool), WorkerError>>,
104+
callback:
105+
oneshot::Sender<Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError>>,
104106
},
105107

106108
/// Process a confirmed block (a commit).
@@ -109,7 +111,8 @@ where
109111
#[debug(with = "elide_option")]
110112
notify_when_messages_are_delivered: Option<oneshot::Sender<()>>,
111113
#[debug(skip)]
112-
callback: oneshot::Sender<Result<(ChainInfoResponse, NetworkActions), WorkerError>>,
114+
callback:
115+
oneshot::Sender<Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError>>,
113116
},
114117

115118
/// Process a cross-chain update.

linera-core/src/chain_worker/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pub(crate) use self::state::CrossChainUpdateHelper;
1414
pub(crate) use self::{
1515
actor::{ChainWorkerActor, ChainWorkerRequest},
1616
config::ChainWorkerConfig,
17+
state::BlockOutcome,
1718
};

linera-core/src/chain_worker/state.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ where
6565
knows_chain_is_active: bool,
6666
}
6767

68+
/// Whether the block was processed or skipped. Used for metrics.
69+
pub enum BlockOutcome {
70+
Processed,
71+
Preprocessed,
72+
Skipped,
73+
}
74+
6875
impl<StorageClient> ChainWorkerState<StorageClient>
6976
where
7077
StorageClient: Storage + Clone + Send + Sync + 'static,
@@ -617,7 +624,7 @@ where
617624
pub(super) async fn process_validated_block(
618625
&mut self,
619626
certificate: ValidatedBlockCertificate,
620-
) -> Result<(ChainInfoResponse, NetworkActions, bool), WorkerError> {
627+
) -> Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError> {
621628
let block = certificate.block();
622629

623630
let header = &block.header;
@@ -637,7 +644,11 @@ where
637644
};
638645
if already_committed_block || should_skip_validated_block()? {
639646
// If we just processed the same pending block, return the chain info unchanged.
640-
return Ok((self.chain_info_response(), NetworkActions::default(), true));
647+
return Ok((
648+
self.chain_info_response(),
649+
NetworkActions::default(),
650+
BlockOutcome::Skipped,
651+
));
641652
}
642653

643654
self.block_values
@@ -667,7 +678,7 @@ where
667678
)?;
668679
self.save().await?;
669680
let actions = self.create_network_actions(Some(old_round)).await?;
670-
Ok((self.chain_info_response(), actions, false))
681+
Ok((self.chain_info_response(), actions, BlockOutcome::Processed))
671682
}
672683

673684
/// Processes a confirmed block (aka a commit).
@@ -680,7 +691,7 @@ where
680691
&mut self,
681692
certificate: ConfirmedBlockCertificate,
682693
notify_when_messages_are_delivered: Option<oneshot::Sender<()>>,
683-
) -> Result<(ChainInfoResponse, NetworkActions), WorkerError> {
694+
) -> Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError> {
684695
let block = certificate.block();
685696
let height = block.header.height;
686697
let chain_id = block.header.chain_id;
@@ -692,7 +703,7 @@ where
692703
let actions = self.create_network_actions(None).await?;
693704
self.register_delivery_notifier(height, &actions, notify_when_messages_are_delivered)
694705
.await;
695-
return Ok((self.chain_info_response(), actions));
706+
return Ok((self.chain_info_response(), actions, BlockOutcome::Skipped));
696707
}
697708

698709
// We haven't processed the block - verify the certificate first
@@ -771,7 +782,11 @@ where
771782
trace!("Preprocessed confirmed block {height} on chain {chain_id:.8}");
772783
self.register_delivery_notifier(height, &actions, notify_when_messages_are_delivered)
773784
.await;
774-
return Ok((self.chain_info_response(), actions));
785+
return Ok((
786+
self.chain_info_response(),
787+
actions,
788+
BlockOutcome::Preprocessed,
789+
));
775790
}
776791

777792
// This should always be true for valid certificates.
@@ -878,7 +893,7 @@ where
878893
self.register_delivery_notifier(height, &actions, notify_when_messages_are_delivered)
879894
.await;
880895

881-
Ok((self.chain_info_response(), actions))
896+
Ok((self.chain_info_response(), actions, BlockOutcome::Processed))
882897
}
883898

884899
/// Schedules a notification for when cross-chain messages are delivered up to the given

linera-core/src/worker.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ use tokio::sync::{mpsc, oneshot, OwnedRwLockReadGuard};
3636
use tracing::{error, instrument, trace, warn};
3737

3838
use crate::{
39-
chain_worker::{ChainWorkerActor, ChainWorkerConfig, ChainWorkerRequest, DeliveryNotifier},
39+
chain_worker::{
40+
BlockOutcome, ChainWorkerActor, ChainWorkerConfig, ChainWorkerRequest, DeliveryNotifier,
41+
},
4042
data_types::{ChainInfoQuery, ChainInfoResponse, CrossChainRequest},
4143
join_set_ext::{JoinSet, JoinSetExt},
4244
notifier::Notifier,
@@ -600,7 +602,7 @@ where
600602
&self,
601603
certificate: ConfirmedBlockCertificate,
602604
notify_when_messages_are_delivered: Option<oneshot::Sender<()>>,
603-
) -> Result<(ChainInfoResponse, NetworkActions), WorkerError> {
605+
) -> Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError> {
604606
let chain_id = certificate.block().header.chain_id;
605607
self.query_chain_worker(chain_id, move |callback| {
606608
ChainWorkerRequest::ProcessConfirmedBlock {
@@ -621,7 +623,7 @@ where
621623
async fn process_validated_block(
622624
&self,
623625
certificate: ValidatedBlockCertificate,
624-
) -> Result<(ChainInfoResponse, NetworkActions, bool), WorkerError> {
626+
) -> Result<(ChainInfoResponse, NetworkActions, BlockOutcome), WorkerError> {
625627
let chain_id = certificate.block().header.chain_id;
626628
self.query_chain_worker(chain_id, move |callback| {
627629
ChainWorkerRequest::ProcessValidatedBlock {
@@ -876,37 +878,25 @@ where
876878
) -> Result<(ChainInfoResponse, NetworkActions), WorkerError> {
877879
trace!("{} <-- {:?}", self.nickname, certificate);
878880
#[cfg(with_metrics)]
879-
let metrics_data = if self
880-
.chain_state_view(certificate.block().header.chain_id)
881-
.await?
882-
.tip_state
883-
.get()
884-
.next_block_height
885-
== certificate.block().header.height
886-
{
887-
Some((
888-
certificate.inner().to_log_str(),
889-
certificate.round.type_name(),
890-
certificate.round.number(),
891-
certificate.block().body.transactions.len() as u64,
892-
certificate
893-
.signatures()
894-
.iter()
895-
.map(|(validator_name, _)| validator_name.to_string())
896-
.collect::<Vec<_>>(),
897-
))
898-
} else {
899-
// Block already processed or will only be preprocessed, no metrics to report.
900-
None
901-
};
881+
let metrics_data = (
882+
certificate.inner().to_log_str(),
883+
certificate.round.type_name(),
884+
certificate.round.number(),
885+
certificate.block().body.transactions.len() as u64,
886+
certificate
887+
.signatures()
888+
.iter()
889+
.map(|(validator_name, _)| validator_name.to_string())
890+
.collect::<Vec<_>>(),
891+
);
902892

903-
let result = self
893+
let (info, actions, _outcome) = self
904894
.process_confirmed_block(certificate, notify_when_messages_are_delivered)
905895
.await?;
906896

907897
#[cfg(with_metrics)]
908898
{
909-
if let Some(metrics_data) = metrics_data {
899+
if matches!(_outcome, BlockOutcome::Processed) {
910900
let (
911901
certificate_log_str,
912902
round_type,
@@ -931,7 +921,7 @@ where
931921
}
932922
}
933923
}
934-
Ok(result)
924+
Ok((info, actions))
935925
}
936926

937927
/// Processes a validated block certificate.
@@ -951,10 +941,10 @@ where
951941
#[cfg(with_metrics)]
952942
let cert_str = certificate.inner().to_log_str();
953943

954-
let (info, actions, _duplicated) = self.process_validated_block(certificate).await?;
944+
let (info, actions, _outcome) = self.process_validated_block(certificate).await?;
955945
#[cfg(with_metrics)]
956946
{
957-
if !_duplicated {
947+
if matches!(_outcome, BlockOutcome::Processed) {
958948
metrics::NUM_ROUNDS_IN_CERTIFICATE
959949
.with_label_values(&[cert_str, round.type_name()])
960950
.observe(round.number() as f64);

0 commit comments

Comments
 (0)