1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
use compact_encoding::{CompactEncoding, State};
use dashmap::DashMap;
use futures::channel::mpsc::UnboundedSender;
use futures::StreamExt;
use hypercore_protocol::{Event, Protocol};
use random_access_storage::RandomAccess;
use std::io::ErrorKind;
use std::sync::Arc;
use std::{collections::HashMap, fmt::Debug};
use tracing::{debug, instrument};
use super::{
messaging::{
CHILD_DOCUMENT_CREATED_LOCAL_SIGNAL_NAME, FEEDS_CHANGED_LOCAL_SIGNAL_NAME,
FEED_VERIFICATION_LOCAL_SIGNAL_NAME,
},
HypercoreWrapper,
};
use crate::common::keys::discovery_key_from_public_key;
use crate::common::state::DocumentFeedsState;
use crate::common::utils::Mutex;
use crate::common::{message::FeedVerificationMessage, state::ChildDocumentInfo};
use crate::common::{message::FeedsChangedMessage, FeedEvent};
use crate::document::{get_document, get_document_by_discovery_key, get_document_ids, Document};
use crate::{DocumentId, FeedDiscoveryKey, FeedPersistence, PeerId, PeermergeError, IO};
#[instrument(level = "debug", skip_all, fields(is_initiator = protocol.is_initiator()))]
pub(crate) async fn on_protocol<T, U, V>(
local_peer_id: PeerId,
protocol: &mut Protocol<V>,
documents: Arc<DashMap<DocumentId, Document<T, U>>>,
feed_event_sender: &mut UnboundedSender<FeedEvent>,
) -> Result<(), PeermergeError>
where
T: RandomAccess + Debug + Send + 'static,
U: FeedPersistence,
V: IO,
{
let is_initiator = protocol.is_initiator();
debug!("Begin listening to protocol events");
// Stores discovery keys that have been received only via hypercore's
// Event::DiscoveryKey, but which have either not yet been broadcasted and thus
// hypercores not yet created either, or then the doc feed isn't verified. These
// are keys across different documents, and only relevant for responders to know
// to open a protocol when the time is right.
let mut unbound_discovery_keys: Vec<FeedDiscoveryKey> = vec![];
// Documents that have a Channel open, but possibly not yet have a doc feed verified.
let mut opened_documents: Vec<DocumentId> = vec![];
// Stores discovery keys per doc feed discovery key, that have been broadcasted
// but which are not yet opened because doc feed has not been verified yet.
let mut discovery_keys_to_open: HashMap<FeedDiscoveryKey, Vec<FeedDiscoveryKey>> =
HashMap::new();
while let Some(event) = protocol.next().await {
debug!("Got protocol event {:?}", event);
match event {
Err(err) => {
match err.kind() {
ErrorKind::BrokenPipe => {
// Ignore broken pipe, can happen when the other end closes channel
// abruptly
break;
}
ErrorKind::Other => {
if format!("{err}").contains("closed channel") {
// Error sending to a closed channel, does not break the protocol, just
// one channel.
break;
}
}
_ => {}
}
return Err(PeermergeError::IO {
context: Some("Unexpected protocol error".to_string()),
source: err,
});
}
Ok(event) => {
match event {
Event::Handshake(_) => {
if is_initiator {
// On handshake, we can only open the doc hypercores, because
// it is not known which of our documents the other side knowns about.
// Only one side needs to reveal what document ids they have, i.e.
// the initiator.
for document_id in get_document_ids(&documents).await {
let document =
get_document(&documents, &document_id).await.unwrap();
let doc_hypercore = document.doc_feed().await;
let doc_hypercore = doc_hypercore.lock().await;
debug!("Event:Handshake: opening doc channel");
protocol.open(*doc_hypercore.public_key()).await?;
}
}
}
Event::DiscoveryKey(discovery_key) => {
if let Some((hypercore, _is_doc)) =
get_openeable_hypercore_for_discovery_key(
&discovery_key,
&documents,
&opened_documents,
&mut discovery_keys_to_open,
)
.await
{
unbound_discovery_keys.retain(|key| key != &discovery_key);
let hypercore = hypercore.lock().await;
protocol.open(*hypercore.public_key()).await?;
} else {
unbound_discovery_keys.push(discovery_key);
}
}
Event::Channel(mut channel) => {
debug!("Event:Channel: id={}", channel.id());
let discovery_key = channel.discovery_key();
if let Some((document, hypercore, is_doc)) =
get_document_and_openeable_hypercore_for_discovery_key(
discovery_key,
&documents,
&opened_documents,
)
.await
{
if is_doc {
opened_documents.push(*discovery_key);
if is_initiator {
if document.doc_feed_verified().await {
// Now that the doc channel is open, we can open channels for the write feed and peer feeds
let active_feeds = document.active_feeds().await;
for active_feed in active_feeds {
let active_feed = active_feed.lock().await;
debug!("Event:Channel: opening active feed");
protocol.open(*active_feed.public_key()).await?;
}
} else {
// Doc feed is not verified, need to wait for the others,
// tolerate unverified.
let doc_discovery_key = document.doc_discovery_key();
let active_peer_feeds_discovery_keys =
document.active_feeds_discovery_keys().await;
if let Some(existing_discovery_keys_to_open) =
discovery_keys_to_open
.get_mut(&doc_discovery_key)
.as_mut()
{
existing_discovery_keys_to_open
.extend(active_peer_feeds_discovery_keys);
existing_discovery_keys_to_open.dedup();
} else {
discovery_keys_to_open.insert(
doc_discovery_key,
active_peer_feeds_discovery_keys,
);
}
}
}
}
let (feeds_state, child_documents, peer_id): (
Option<DocumentFeedsState>,
Vec<ChildDocumentInfo>,
Option<PeerId>,
) = if is_doc {
let (feeds_state, child_documents) =
document.feeds_state_and_child_documents().await;
(Some(feeds_state), child_documents, None)
} else {
let peer_id =
document.peer_id_from_discovery_key(discovery_key).await;
(None, vec![], Some(peer_id))
};
let mut hypercore = hypercore.lock().await;
let channel_receiver = channel.take_receiver().unwrap();
let channel_sender = channel.local_sender();
hypercore.on_channel(
local_peer_id,
is_doc,
feeds_state,
child_documents,
peer_id,
document.doc_discovery_key(),
document.doc_signature_verifying_key(),
document.settings().max_write_feed_length,
channel,
channel_receiver,
channel_sender,
feed_event_sender,
);
} else {
panic!(
"Could not find hypercore with discovery key {discovery_key:02X?}",
);
}
}
Event::Close(discovery_key) => {
if let Some(index) = opened_documents
.iter()
.position(|&doc_id| doc_id == discovery_key)
{
opened_documents.remove(index);
}
if opened_documents.is_empty() {
// When all of the documents' doc feeds have been closed, the
// protocol can also be closed.
break;
}
}
Event::LocalSignal((name, data)) => match name.as_str() {
FEEDS_CHANGED_LOCAL_SIGNAL_NAME => {
let mut dec_state = State::from_buffer(&data);
let message: FeedsChangedMessage = dec_state.decode(&data)?;
let document = get_document_by_discovery_key(
&documents,
&message.doc_discovery_key,
)
.await
.unwrap();
let new_discovery_keys_to_open: Vec<[u8; 32]> = message
.feeds_to_create
.iter()
.map(|peer| discovery_key_from_public_key(&peer.public_key))
.filter(|discovery_key| {
should_open_discovery_key(
discovery_key,
is_initiator,
&mut unbound_discovery_keys,
)
})
.collect();
if let Some(existing_discovery_keys_to_open) = discovery_keys_to_open
.get_mut(&message.doc_discovery_key)
.as_mut()
{
existing_discovery_keys_to_open.extend(new_discovery_keys_to_open);
existing_discovery_keys_to_open.dedup();
} else {
discovery_keys_to_open
.insert(message.doc_discovery_key, new_discovery_keys_to_open);
}
if document.doc_feed_verified().await {
if let Some(document_discovery_keys_to_open) =
discovery_keys_to_open.get_mut(&message.doc_discovery_key)
{
for discovery_key in
std::mem::take(document_discovery_keys_to_open)
{
if let Some(hypercore) =
document.active_feed(&discovery_key).await
{
let hypercore = hypercore.lock().await;
protocol.open(*hypercore.public_key()).await?;
} else {
panic!(
"Could not find new hypercore with discovery key {discovery_key:02X?}",
);
};
}
}
}
}
FEED_VERIFICATION_LOCAL_SIGNAL_NAME => {
let mut dec_state = State::from_buffer(&data);
let message: FeedVerificationMessage = dec_state.decode(&data)?;
if message.verified && message.peer_id.is_none() {
// The doc peer was verified, only now ready to start listening
// to all the other feeds
let document = get_document_by_discovery_key(
&documents,
&message.doc_discovery_key,
)
.await
.unwrap();
if let Some(document_discovery_keys_to_open) =
discovery_keys_to_open.get_mut(&message.doc_discovery_key)
{
for discovery_key in
std::mem::take(document_discovery_keys_to_open)
{
if let Some(hypercore) =
document.active_feed(&discovery_key).await
{
let hypercore = hypercore.lock().await;
protocol.open(*hypercore.public_key()).await?;
} else {
panic!(
"Could not find new hypercore with discovery key {discovery_key:02X?}",
);
};
}
}
} else {
unimplemented!("TODO: Invalid feed deletion");
}
}
CHILD_DOCUMENT_CREATED_LOCAL_SIGNAL_NAME => {
let mut dec_state = State::from_buffer(&data);
let child_document_info: ChildDocumentInfo = dec_state.decode(&data)?;
let child_public_key = child_document_info.doc_public_key;
let child_discovery_key =
discovery_key_from_public_key(&child_public_key);
if should_open_discovery_key(
&child_discovery_key,
is_initiator,
&mut unbound_discovery_keys,
) {
debug!("Event:ChildDocumentCreated: opening doc channel");
protocol.open(child_public_key).await?;
}
}
_ => panic!("Unknown local signal: {name}"),
},
_ => {}
}
}
}
}
debug!("Exiting");
Ok(())
}
async fn get_openeable_hypercore_for_discovery_key<T, U>(
discovery_key: &[u8; 32],
documents: &Arc<DashMap<DocumentId, Document<T, U>>>,
opened_documents: &Vec<DocumentId>,
discovery_keys_to_open: &mut HashMap<FeedDiscoveryKey, Vec<FeedDiscoveryKey>>,
) -> Option<(Arc<Mutex<HypercoreWrapper<U>>>, bool)>
where
T: RandomAccess + Debug + Send + 'static,
U: FeedPersistence,
{
if let Some(document) = get_document_by_discovery_key(documents, discovery_key).await {
Some((document.doc_feed().await, true))
} else {
for opened_document_id in opened_documents {
let document = get_document_by_discovery_key(documents, opened_document_id)
.await
.unwrap();
let active_feed = document.active_feed(discovery_key).await;
if active_feed.is_some() {
if document.doc_feed_verified().await {
return active_feed.map(|feed| (feed, false));
} else {
// The doc feed is not verified yet, store this announced discovery key
let doc_discovery_key = document.doc_discovery_key();
if let Some(existing_discovery_keys_to_open) =
discovery_keys_to_open.get_mut(&doc_discovery_key).as_mut()
{
existing_discovery_keys_to_open.push(*discovery_key);
existing_discovery_keys_to_open.dedup();
} else {
discovery_keys_to_open.insert(doc_discovery_key, vec![*discovery_key]);
}
}
}
}
None
}
}
async fn get_document_and_openeable_hypercore_for_discovery_key<T, U>(
discovery_key: &[u8; 32],
documents: &Arc<DashMap<DocumentId, Document<T, U>>>,
opened_documents: &Vec<DocumentId>,
) -> Option<(Document<T, U>, Arc<Mutex<HypercoreWrapper<U>>>, bool)>
where
T: RandomAccess + Debug + Send + 'static,
U: FeedPersistence,
{
if let Some(document) = get_document_by_discovery_key(documents, discovery_key).await {
let doc_feed = document.doc_feed().await;
Some((document, doc_feed, true))
} else {
for opened_document_id in opened_documents {
let document = get_document_by_discovery_key(documents, opened_document_id)
.await
.unwrap();
let active_feed = document.active_feed(discovery_key).await;
if active_feed.is_some() {
return active_feed.map(|feed| (document.clone(), feed, false));
}
}
None
}
}
fn should_open_discovery_key(
discovery_key: &FeedDiscoveryKey,
is_initiator: bool,
unbound_discovery_keys: &mut Vec<FeedDiscoveryKey>,
) -> bool {
if is_initiator {
true
} else {
// Only open protocol to those that have previously
// been announced.
if unbound_discovery_keys.contains(discovery_key) {
unbound_discovery_keys.retain(|key| key != discovery_key);
true
} else {
false
}
}
}