1pub mod format;
2pub mod interpret;
3pub mod raw;
4mod network;
6pub mod server;
7
8use eyre::eyre;
9use format::*;
10use futures::prelude::*;
11use interpret::*;
12use raw::{RawCommit, RawRepository};
13use serde::{Deserialize, Serialize};
14use simperby_core::reserved::ReservedState;
15use simperby_core::utils::get_timestamp;
16use simperby_core::verify::CommitSequenceVerifier;
17use simperby_core::*;
18use simperby_network::*;
19use std::sync::Arc;
20use std::{collections::HashSet, fmt};
21use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
22use tokio::sync::RwLock;
23
24pub use network::RepositoryMessage;
25
26pub type Branch = String;
27pub type Tag = String;
28
29pub const FINALIZED_BRANCH_NAME: &str = "finalized";
30pub const FP_BRANCH_NAME: &str = "fp";
31pub const COMMIT_TITLE_HASH_DIGITS: usize = 8;
32pub const TAG_NAME_HASH_DIGITS: usize = 8;
33pub const BRANCH_NAME_HASH_DIGITS: usize = 8;
34pub const UNKNOWN_COMMIT_AUTHOR: &str = "unknown";
35
36pub type Error = eyre::Error;
37
38#[derive(thiserror::Error, Debug)]
39#[error("repository integrity broken: {msg}")]
40pub struct IntegrityError {
41 pub msg: String,
42}
43
44impl IntegrityError {
45 pub fn new(msg: String) -> Self {
46 Self { msg }
47 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct Config {
52 pub long_range_attack_distance: usize,
58}
59
60pub struct DistributedRepository {
67 dms: Option<Arc<RwLock<Dms<RepositoryMessage>>>>,
68 raw: Arc<RwLock<RawRepository>>,
70 _config: Config,
71 private_key: Option<PrivateKey>,
72}
73
74impl DistributedRepository {
75 pub fn get_raw(&self) -> Arc<RwLock<RawRepository>> {
76 Arc::clone(&self.raw)
77 }
78
79 pub fn get_dms(&self) -> Option<Arc<RwLock<Dms<RepositoryMessage>>>> {
80 self.dms.as_ref().map(Arc::clone)
81 }
82
83 pub async fn new(
84 dms: Option<Arc<RwLock<Dms<RepositoryMessage>>>>,
85 raw: Arc<RwLock<RawRepository>>,
86 config: Config,
87 private_key: Option<PrivateKey>,
88 ) -> Result<Self, Error> {
89 Ok(Self {
90 dms,
91 raw,
92 _config: config,
93 private_key,
94 })
95 }
96
97 pub async fn genesis(mut raw: RawRepository) -> Result<(), Error> {
105 genesis(&mut raw).await
106 }
107
108 pub async fn read_last_finalization_info(&self) -> Result<FinalizationInfo, Error> {
114 read_last_finalization_info(&*self.raw.read().await).await
115 }
116
117 pub async fn read_finalization_info(
119 &self,
120 height: BlockHeight,
121 ) -> Result<FinalizationInfo, Error> {
122 read_finalization_info(&*self.raw.read().await, height).await
123 }
124
125 pub async fn read_commit(&self, commit_hash: CommitHash) -> Result<Commit, Error> {
127 read_commit(&*self.raw.read().await, commit_hash).await
128 }
129
130 pub async fn read_agendas(&self) -> Result<Vec<(CommitHash, Hash256)>, Error> {
132 read_agendas(&*self.raw.read().await).await
133 }
134
135 pub async fn read_governance_approved_agendas(
138 &self,
139 ) -> Result<Vec<(CommitHash, Hash256)>, Error> {
140 read_governance_approved_agendas(&*self.raw.read().await).await
141 }
142
143 pub async fn read_blocks(&self) -> Result<Vec<(CommitHash, Hash256)>, Error> {
145 read_blocks(&*self.raw.read().await).await
146 }
147
148 pub async fn check(&self, _starting_height: BlockHeight) -> Result<bool, Error> {
157 Ok(true)
159 }
160
161 pub async fn check_gitignore(&self) -> Result<bool, Error> {
164 check_gitignore(&*self.raw.read().await).await
165 }
166
167 pub async fn sync(&mut self, commit_hash: CommitHash) -> Result<Result<(), String>, Error> {
185 sync(&mut *self.raw.write().await, commit_hash).await
186 }
187
188 pub async fn sync_all(&mut self) -> Result<Vec<(String, Result<(), String>)>, Error> {
192 sync_all(&mut *self.raw.write().await).await
193 }
194
195 pub async fn test_push_eligibility(
197 &self,
198 commit_hash: CommitHash,
199 branch_name: String,
200 timestamp: Timestamp,
201 signature: TypedSignature<(CommitHash, String, Timestamp)>,
202 _timestamp_to_test: Timestamp,
203 ) -> Result<bool, Error> {
204 test_push_eligibility(
205 &*self.raw.read().await,
206 commit_hash,
207 branch_name,
208 timestamp,
209 signature,
210 _timestamp_to_test,
211 )
212 .await
213 }
214
215 pub async fn clean(&mut self, hard: bool) -> Result<(), Error> {
230 clean(&mut *self.raw.write().await, hard).await
231 }
232
233 pub async fn broadcast(&mut self) -> Result<(), Error> {
235 broadcast(&mut *self.raw.write().await, self.private_key.clone()).await
236 }
237
238 pub async fn flush(&mut self) -> Result<(), Error> {
243 self.flush_().await
244 }
245
246 pub async fn update(&mut self, _no_network: bool) -> Result<(), Error> {
251 self.update_().await
252 }
253
254 pub async fn approve(
265 &mut self,
266 agenda_hash: &Hash256,
267 proof: Vec<TypedSignature<Agenda>>,
268 timestamp: Timestamp,
269 ) -> Result<CommitHash, Error> {
270 approve(&mut *self.raw.write().await, agenda_hash, proof, timestamp).await
271 }
272
273 pub async fn create_transaction(
275 &mut self,
276 transaction: Transaction,
277 ) -> Result<CommitHash, Error> {
278 create_transaction(&mut *self.raw.write().await, transaction).await
279 }
280
281 pub async fn create_agenda(
283 &mut self,
284 author: MemberName,
285 ) -> Result<(Agenda, CommitHash), Error> {
286 create_agenda(&mut *self.raw.write().await, author).await
287 }
288
289 pub async fn create_block(
291 &mut self,
292 author: PublicKey,
293 ) -> Result<(BlockHeader, CommitHash), Error> {
294 create_block(&mut *self.raw.write().await, author).await
295 }
296
297 pub async fn create_extra_agenda_transaction(
299 &mut self,
300 transaction: &ExtraAgendaTransaction,
301 ) -> Result<CommitHash, Error> {
302 create_extra_agenda_transaction(&mut *self.raw.write().await, transaction).await
303 }
304
305 pub async fn finalize(
307 &mut self,
308 block_commit_hash: CommitHash,
309 proof: FinalizationProof,
310 ) -> Result<CommitHash, Error> {
311 finalize(&mut *self.raw.write().await, block_commit_hash, proof).await
312 }
313
314 pub async fn commit_gitignore(&mut self) -> Result<(), Error> {
317 commit_gitignore(&mut *self.raw.write().await).await
318 }
319
320 pub async fn vote(&mut self, commit_hash: CommitHash) -> Result<(), Error> {
326 vote(&mut *self.raw.write().await, commit_hash).await
327 }
328
329 pub async fn veto(&mut self, commit_hash: CommitHash) -> Result<(), Error> {
331 veto(&mut *self.raw.write().await, commit_hash).await
332 }
333}