Skip to content

Commit

Permalink
Optimistic clients don't prepare chains before operation execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
christos-h committed Oct 1, 2024
1 parent bd27e42 commit 02a6ccf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion linera-client/src/client_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use linera_base::{
};
use linera_chain::data_types::Certificate;
use linera_core::{
client::{ChainClient, Client, MessagePolicy},
client::{ChainClient, Client, ClientMode, MessagePolicy},
data_types::ClientOutcome,
join_set_ext::{JoinSet, JoinSetExt as _},
node::CrossChainMessageDelivery,
Expand Down Expand Up @@ -215,6 +215,7 @@ where
chain.next_block_height,
chain.pending_block.clone(),
chain.pending_blobs.clone(),
ClientMode::Optimistic,
);
chain_client.options_mut().message_policy = MessagePolicy::new(
self.options.blanket_message_policy,
Expand Down
3 changes: 2 additions & 1 deletion linera-client/src/unit_tests/chain_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use linera_base::{
ownership::{ChainOwnership, TimeoutConfig},
};
use linera_core::{
client::{ChainClient, Client},
client::{ChainClient, Client, ClientMode},
node::CrossChainMessageDelivery,
test_utils::{MemoryStorageBuilder, NodeProvider, StorageBuilder as _, TestBuilder},
};
Expand Down Expand Up @@ -75,6 +75,7 @@ impl chain_listener::ClientContext for ClientContext {
chain.next_block_height,
chain.pending_block.clone(),
chain.pending_blobs.clone(),
ClientMode::Optimistic,
)
}

Expand Down
24 changes: 23 additions & 1 deletion linera-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<P, S: Storage + Clone> Client<P, S> {
next_block_height: BlockHeight,
pending_block: Option<Block>,
pending_blobs: BTreeMap<BlobId, Blob>,
mode: ClientMode,
) -> ChainClient<P, S> {
let dashmap::mapref::entry::Entry::Vacant(e) = self.chains.entry(chain_id) else {
panic!("Inserting already-existing chain {chain_id}");
Expand All @@ -276,6 +277,7 @@ impl<P, S: Storage + Clone> Client<P, S> {
message_policy: self.message_policy.clone(),
cross_chain_message_delivery: self.cross_chain_message_delivery,
},
mode,
}
}
}
Expand Down Expand Up @@ -417,6 +419,14 @@ where
chain_id: ChainId,
/// The client options.
options: ChainClientOptions,
/// The client's mode. Optmistic or pessimistic.
mode: ClientMode,
}

#[derive(Clone)]
pub enum ClientMode {
Optimistic,
Pessimistic,
}

impl<P, S> Clone for ChainClient<P, S>
Expand All @@ -428,6 +438,7 @@ where
client: self.client.clone(),
chain_id: self.chain_id,
options: self.options.clone(),
mode: self.mode.clone(),
}
}
}
Expand Down Expand Up @@ -607,6 +618,15 @@ impl<P: 'static, S: Storage> ChainClient<P, S> {
pub fn pending_blobs(&self) -> ChainGuardMapped<BTreeMap<BlobId, Blob>> {
Unsend::new(self.state().inner.map(|state| state.pending_blobs()))
}

#[tracing::instrument(level = "trace", skip(self))]
/// Returns if the client is optimistic.
pub fn is_optimistic(&self) -> bool {
match self.mode {
ClientMode::Optimistic => true,
ClientMode::Pessimistic => false,
}
}
}

enum ReceiveCertificateMode {
Expand Down Expand Up @@ -1800,7 +1820,9 @@ where
&self,
operations: Vec<Operation>,
) -> Result<ClientOutcome<Certificate>, ChainClientError> {
self.prepare_chain().await?;
if !self.is_optimistic() {
self.prepare_chain().await?;
}
self.execute_without_prepare(operations).await
}

Expand Down
3 changes: 2 additions & 1 deletion linera-core/src/unit_tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use {
};

use crate::{
client::{ChainClient, Client},
client::{ChainClient, Client, ClientMode},
data_types::*,
node::{
CrossChainMessageDelivery, NodeError, NotificationStream, ValidatorNode,
Expand Down Expand Up @@ -799,6 +799,7 @@ where
block_height,
None,
BTreeMap::new(),
ClientMode::Optimistic,
))
}

Expand Down

0 comments on commit 02a6ccf

Please sign in to comment.