qualia_client_core/wellfair/api/
sync.rs1use super::super::journal::JournalEntry;
4use super::super::sync_outbox::{SyncOutbox, SyncOutboxEntry, SyncOutboxState};
5use super::super::sync_protocol::{AdmitOutcome, InboxRecord, SyncInbox, SyncOperation};
6use super::super::sync_transport::SyncTransport;
7use ed25519_dalek::Signer;
8
9use super::*;
10
11impl WebizenHostApi {
12 pub fn build_outbound_operation(
18 &self,
19 entry: &JournalEntry,
20 lamport: u64,
21 ) -> Option<SyncOperation> {
22 if entry.sensitivity == "Classified" {
23 return None;
24 }
25 let op = SyncOperation::new(
26 uuid::Uuid::new_v4().to_string(),
27 entry.id.clone(),
28 entry.kind.clone(),
29 self.author_did.clone(),
30 entry.sensitivity.clone(),
31 entry.summary.clone().unwrap_or_default(),
32 lamport,
33 entry.committed_unix,
34 );
35 let signature = self.signing_key.sign(&op.signing_payload());
36 Some(op.with_signature(hex::encode(signature.to_bytes())))
37 }
38
39 pub fn admit_sync_operation(&self, op: &SyncOperation) -> Result<AdmitOutcome, String> {
42 let inbox = SyncInbox::open(&self.storage_root).map_err(|e| e.to_string())?;
43 inbox
44 .admit(op, Self::now_unix() as u32)
45 .map_err(|e| e.to_string())
46 }
47
48 pub fn validated_sync_operations(&self) -> Result<Vec<SyncOperation>, String> {
50 SyncInbox::open(&self.storage_root)
51 .map_err(|e| e.to_string())?
52 .validated_operations()
53 .map_err(|e| e.to_string())
54 }
55
56 pub fn list_sync_inbox(&self, limit: usize) -> Result<Vec<InboxRecord>, String> {
58 SyncInbox::open(&self.storage_root)
59 .map_err(|e| e.to_string())?
60 .list_recent(limit)
61 .map_err(|e| e.to_string())
62 }
63
64 fn next_sync_lamport(&self) -> Result<u64, String> {
69 let max = self
70 .validated_sync_operations()?
71 .iter()
72 .map(|o| o.lamport)
73 .max()
74 .unwrap_or(0);
75 Ok(max + 1)
76 }
77
78 pub fn sync_push_via<T: SyncTransport>(
86 &self,
87 transport: &T,
88 limit: usize,
89 ) -> Result<usize, String> {
90 let outbox = SyncOutbox::open(&self.storage_root).map_err(|e| e.to_string())?;
91 let queued: Vec<SyncOutboxEntry> = outbox
92 .list_all()
93 .map_err(|e| e.to_string())?
94 .into_iter()
95 .filter(|e| e.state == SyncOutboxState::Queued)
96 .take(limit)
97 .collect();
98 if queued.is_empty() {
99 return Ok(0);
100 }
101 let journal = self.list_health_records(512)?;
102 let mut lamport = self.next_sync_lamport()?;
103 let mut ops = Vec::new();
104 let mut sent_ids = Vec::new();
105 for entry in &queued {
106 let Some(journal_entry) = journal.iter().find(|j| j.id == entry.record_id) else {
107 continue; };
109 match self.build_outbound_operation(journal_entry, lamport) {
110 Some(op) => {
111 lamport += 1;
112 ops.push(op);
113 sent_ids.push(entry.operation_id.clone());
114 }
115 None => {
116 let _ = outbox.update_state(&entry.operation_id, SyncOutboxState::Rejected);
118 }
119 }
120 }
121 if ops.is_empty() {
122 return Ok(0);
123 }
124 transport.publish(&ops)?;
125 for id in &sent_ids {
126 let _ = outbox.update_state(id, SyncOutboxState::Sent);
127 }
128 Ok(ops.len())
129 }
130
131 pub fn sync_pull_via<T: SyncTransport>(
136 &self,
137 transport: &T,
138 since: u64,
139 ) -> Result<SyncPullReport, String> {
140 let ops = transport.pull(since)?;
141 let mut report = SyncPullReport {
142 pulled: ops.len(),
143 validated: 0,
144 duplicate: 0,
145 rejected: 0,
146 };
147 for op in &ops {
148 match self.admit_sync_operation(op)? {
149 AdmitOutcome::Validated => report.validated += 1,
150 AdmitOutcome::Duplicate => report.duplicate += 1,
151 AdmitOutcome::Rejected(_) => report.rejected += 1,
152 }
153 }
154 Ok(report)
155 }
156
157 #[cfg(not(target_arch = "wasm32"))]
160 pub fn sync_with_http_relay(
161 &self,
162 base_url: &str,
163 since: u64,
164 ) -> Result<(usize, SyncPullReport), String> {
165 let transport = super::super::sync_transport::HttpRelayTransport::new(base_url);
166 let pushed = self.sync_push_via(&transport, 256)?;
167 let report = self.sync_pull_via(&transport, since)?;
168 Ok((pushed, report))
169 }
170
171 #[cfg(not(target_arch = "wasm32"))]
177 pub fn sync_with_libp2p_peer(
178 &self,
179 peer_id: &str,
180 peer_addr: &str,
181 since: u64,
182 ) -> Result<(usize, SyncPullReport), String> {
183 let transport =
184 super::super::sync_transport::Libp2pSyncTransport::connect(peer_id, peer_addr)?;
185 let pushed = self.sync_push_via(&transport, 256)?;
186 let report = self.sync_pull_via(&transport, since)?;
187 Ok((pushed, report))
188 }
189}