linera_storage_service/
common.rs1use std::path::PathBuf;
5
6use linera_base::command::resolve_binary;
7use linera_views::{
8 lru_caching::LruCachingConfig,
9 store::{CommonStoreInternalConfig, KeyValueStoreError},
10};
11use serde::{Deserialize, Serialize};
12use thiserror::Error;
13use tonic::Status;
14
15pub const MAX_PAYLOAD_SIZE: usize = 4000000;
23
24#[repr(u8)]
26pub enum KeyPrefix {
27 Key,
29 Namespace,
31 RootKey,
33}
34
35#[derive(Debug, Error)]
36pub enum ServiceStoreError {
37 #[error("Store already exists during a create operation")]
39 StoreAlreadyExists,
40
41 #[error("Not matching entry")]
43 NotMatchingEntry,
44
45 #[error("Failed to find the linera-storage-server binary")]
47 FailedToFindStorageServerBinary,
48
49 #[error(transparent)]
51 GrpcError(#[from] Box<Status>),
52
53 #[error("The key size must be at most 1 MB")]
55 KeyTooLong,
56
57 #[error(transparent)]
59 TransportError(#[from] tonic::transport::Error),
60
61 #[error(transparent)]
63 VarError(#[from] std::env::VarError),
64
65 #[error(transparent)]
67 BcsError(#[from] bcs::Error),
68}
69
70impl From<Status> for ServiceStoreError {
71 fn from(error: Status) -> Self {
72 Box::new(error).into()
73 }
74}
75
76impl KeyValueStoreError for ServiceStoreError {
77 const BACKEND: &'static str = "service";
78}
79
80pub fn storage_service_test_endpoint() -> Result<String, ServiceStoreError> {
81 Ok(std::env::var("LINERA_STORAGE_SERVICE")?)
82}
83
84#[derive(Clone, Debug, Deserialize, Serialize)]
85pub struct ServiceStoreInternalConfig {
86 pub endpoint: String,
88 pub common_config: CommonStoreInternalConfig,
90}
91
92pub type ServiceStoreConfig = LruCachingConfig<ServiceStoreInternalConfig>;
94
95impl ServiceStoreInternalConfig {
96 pub fn http_address(&self) -> String {
97 format!("http://{}", self.endpoint)
98 }
99}
100
101pub async fn get_service_storage_binary() -> Result<PathBuf, ServiceStoreError> {
105 let binary = resolve_binary("linera-storage-server", "linera-storage-service").await;
106 if let Ok(binary) = binary {
107 return Ok(binary);
108 }
109 let binary = resolve_binary("../linera-storage-server", "linera-storage-service").await;
110 if let Ok(binary) = binary {
111 return Ok(binary);
112 }
113 Err(ServiceStoreError::FailedToFindStorageServerBinary)
114}