Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_shield/src/srv/context.rs

4.3 KiB, 89 runs

created by r1870400018:4090, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1use crate::srv::{
2 cfg::ServerConfig,
3 msg::{
4 core::IdTypes,
5 protocol::{
6 Protocol,
7 ProtocolTypes,
8 },
9 },
10};
11
12use oxedyne_fe2o3_core::{
13 prelude::*,
14 path::NormPathBuf,
15 rand::Rand,
16};
17use oxedyne_fe2o3_crypto::enc::EncryptionScheme;
18use oxedyne_fe2o3_hash::{
19 csum::ChecksumScheme,
20 hash::HashScheme,
21};
22use oxedyne_fe2o3_iop_crypto::enc::Encrypter;
23use oxedyne_fe2o3_iop_db::api::Database;
24use oxedyne_fe2o3_iop_hash::api::Hasher;
25use oxedyne_fe2o3_net::id;
26use oxedyne_fe2o3_o3db_sync::{
27 O3db,
28 base::cfg::OzoneConfig,
29 data::core::RestSchemesInput,
30};
31
32use std::{
33 collections::BTreeMap,
34 marker::PhantomData,
35 path::Path,
36 sync::{
37 Arc,
38 RwLock,
39 },
40};
41
42
43#[derive(Clone, Debug)]
44pub struct ServerContext<
45 const C: usize, // Length of user secret pow code.
46 const ML: usize,
47 const SL: usize,
48 const UL: usize,
49 P: ProtocolTypes<ML, SL, UL>,
50 // Database
51 ENC: Encrypter, // Symmetric encryption of database.
52 KH: Hasher, // Hashes database keys.
53 DB: Database<UL, <P::ID as IdTypes<ML, SL, UL>>::U, ENC, KH>,
54> {
55 pub cfg: ServerConfig,
56 pub root: NormPathBuf,
57 pub db: Option<(Arc<RwLock<DB>>, <P::ID as IdTypes<ML, SL, UL>>::U)>,
58 pub protocol: Protocol<C, ML, SL, UL, P>,
59 phantom3: PhantomData<ENC>,
60 phantom4: PhantomData<KH>,
61}
62
63impl<
64 const C: usize,
65 const ML: usize,
66 const SL: usize,
67 const UL: usize,
68 P: ProtocolTypes<ML, SL, UL> + 'static,
69 // Database
70 ENC: Encrypter + 'static,
71 KH: Hasher + 'static,
72 DB: Database<UL, <P::ID as IdTypes<ML, SL, UL>>::U, ENC, KH> + 'static,
73>
74 ServerContext<C, ML, SL, UL, P, ENC, KH, DB>
75{
76 pub fn new(
77 cfg: ServerConfig,
78 root: NormPathBuf,
79 db: Option<(DB, <P::ID as IdTypes<ML, SL, UL>>::U)>,
80 protocol: Protocol<C, ML, SL, UL, P>,
81 )
82 -> Self
83 {
84 Self {
85 cfg,
86 root,
87 db: db.map(|(db, uid)| (Arc::new(RwLock::new(db)), uid)),
88 protocol,
89 phantom3: PhantomData,
90 phantom4: PhantomData,
91 }
92 }
93
94 //pub fn clone_self(&self) -> Self {
95 // self.clone()
96 //}
97
98 pub fn err_id() -> String {
99 Rand::generate_random_string(6, "abcdefghikmnpqrstuvw0123456789")
100 }
101}
102
103pub fn new_db(
104 db_root: &Path,
105 enc_key: &[u8],
106)
107 -> Outcome<O3db<
108 { id::UID_LEN },
109 id::Uid,
110 EncryptionScheme,
111 HashScheme,
112 HashScheme,
113 ChecksumScheme,
114 >>
115{
116 // Only the deviations from the library's own configuration are stated, so that a field
117 // added upstream reaches this caller instead of failing to compile here, and so that
118 // each value below reads as a decision rather than as one entry in a list of twenty.
119 // The values are those this server has always used; nothing about the store changes.
120 let cfg = OzoneConfig {
121 // A Shield server's store is small, so it takes a tenth of the library's cache.
122 cache_size_limit_bytes: 100_000_000,
123 // Files
124 data_file_max_bytes: 1_000_000,
125 // Chunking. These are far below the library's own figures and match its *test*
126 // setup, which is where Steel found the same pair and replaced them; they are
127 // left alone here because changing them changes how values already in a Shield
128 // store were split. Worth revisiting deliberately.
129 rest_chunk_threshold: 1_500,
130 rest_chunk_bytes: 64,
131 // One writer per zone: the store is write-light.
132 num_wbots_per_zone: 1,
133 // Zone state is reported more often than the library default.
134 zone_state_update_secs: 1,
135 // No per-zone size caps.
136 zone_overrides: BTreeMap::new(),
137 ..Default::default()
138 };
139
140
141 let aes_gcm = res!(EncryptionScheme::new_aes_256_gcm_with_key(enc_key));
142 let crc32 = ChecksumScheme::new_crc32();
143 let schms_input = RestSchemesInput::new(
144 Some(aes_gcm.clone()),
145 None::<HashScheme>,
146 None::<HashScheme>,
147 Some(crc32.clone()),
148 );
149 O3db::new(
150 &db_root,
151 Some(cfg),
152 schms_input,
153 id::Uid::default(),
154 )
155}