Skip to content

Commit

Permalink
fix: do not set peer id with max (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeeon233 authored Oct 3, 2024
1 parent a93efd5 commit b1e03d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/loro-common/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error;

use serde_columnar::ColumnarError;
use thiserror::Error;

Expand Down Expand Up @@ -90,6 +92,8 @@ pub enum LoroError {
"The container {container} is deleted. You cannot apply the op on a deleted container."
)]
ContainerDeleted { container: Box<ContainerID> },
#[error("You cannot set the `PeerID` with `PeerID::MAX`, which is an internal specific value")]
InvalidPeerID,
}

#[derive(Error, Debug, PartialEq)]
Expand Down
3 changes: 3 additions & 0 deletions crates/loro-internal/src/loro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ impl LoroDoc {

#[inline(always)]
pub fn set_peer_id(&self, peer: PeerID) -> LoroResult<()> {
if peer == PeerID::MAX {
return Err(LoroError::InvalidPeerID);
}
let next_id = self.oplog.try_lock().unwrap().next_id(peer);
if self.auto_commit.load(Acquire) {
let doc_state = self.state.try_lock().unwrap();
Expand Down
11 changes: 10 additions & 1 deletion crates/loro-internal/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::{atomic::AtomicBool, Arc, Mutex};

use fxhash::FxHashMap;
use loro_common::{ContainerID, ContainerType, LoroResult, LoroValue, ID};
use loro_common::{ContainerID, ContainerType, LoroError, LoroResult, LoroValue, PeerID, ID};
use loro_internal::{
delta::ResolvedMapValue,
event::{Diff, EventTriggerKind},
Expand Down Expand Up @@ -1262,3 +1262,12 @@ fn test_map_contains_key() {
map.delete("bro").unwrap();
assert!(!map.contains_key("bro"));
}

#[test]
fn set_max_peer_id() {
let doc = LoroDoc::new_auto_commit();
assert_eq!(
doc.set_peer_id(PeerID::MAX),
Result::Err(LoroError::InvalidPeerID)
);
}

0 comments on commit b1e03d9

Please sign in to comment.