Skip to content

Commit

Permalink
Ah, fix some incorrect controldb paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Nov 4, 2024
1 parent 4614074 commit 3b80687
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 17 additions & 2 deletions crates/lib/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,26 @@ impl Identity {
}

/// Returns an `Identity` defined as the given byte `slice`.
/// The slice is assumed to be in big-endian format.
pub fn from_slice(slice: &[u8]) -> Self {
/// The slice is assumed to be in BIG-ENDIAN format.
///
/// This method is the correct choice if you have converted the bytes of a hexadecimal-formatted `Identity`
/// to a byte array in the following way:
/// ```ignore
/// "0xb0b1b2..."
/// ->
/// [0xb0, 0xb1, 0xb2, ...]
/// ```
pub fn from_be_slice(slice: &[u8]) -> Self {
Self::from_be_byte_array(slice.try_into().unwrap())
}

/// Returns an `Identity` defined as the given byte `slice`.
/// The slice is assumed to be in LITTLE-ENDIAN format.
/// If you are parsing an `Identity` from a string, you probably want `from_be_slice` instead.
pub fn from_slice(slice: &[u8]) -> Self {
Self::from_byte_array(slice.try_into().unwrap())
}

#[doc(hidden)]
pub fn __dummy() -> Self {
Self::ZERO
Expand Down
14 changes: 7 additions & 7 deletions crates/standalone/src/control_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod tests;
/// A control database when SpacetimeDB is running standalone.
///
/// Important note: The `Addresses` and `Identities` stored in this database
/// are stored as *little-endian* byte arrays. This means that printing such an array
/// are stored as *LITTLE-ENDIAN* byte arrays. This means that printing such an array
/// in hexadecimal will result in the REVERSE of the standard way to print `Addresses` and `Identities`.
#[derive(Clone)]
pub struct ControlDb {
Expand Down Expand Up @@ -145,7 +145,7 @@ impl ControlDb {

let identity_bytes = database_identity.to_byte_array();
let tree = self.db.open_tree("dns")?;
tree.insert(domain.to_lowercase().as_bytes(), &identity_bytes)?;
tree.insert(domain.to_lowercase(), &identity_bytes)?;

let tree = self.db.open_tree("reverse_dns")?;
match tree.get(identity_bytes)? {
Expand Down Expand Up @@ -225,8 +225,8 @@ impl ControlDb {

pub fn get_database_by_identity(&self, identity: &Identity) -> Result<Option<Database>> {
let tree = self.db.open_tree("database_by_identity")?;
let key = identity.to_hex();
let value = tree.get(key.as_bytes())?;
let key = identity.to_be_byte_array();
let value = tree.get(&key[..])?;
if let Some(value) = value {
let database = compat::Database::from_slice(&value[..]).unwrap().into();
return Ok(Some(database));
Expand All @@ -238,7 +238,7 @@ impl ControlDb {
let id = self.db.generate_id()?;
let tree = self.db.open_tree("database_by_identity")?;

let key = database.database_identity.to_hex();
let key = database.database_identity.to_be_byte_array();
if tree.contains_key(key)? {
return Err(Error::DatabaseAlreadyExists(database.database_identity));
}
Expand All @@ -261,9 +261,9 @@ impl ControlDb {

if let Some(old_value) = tree.get(id.to_be_bytes())? {
let database = compat::Database::from_slice(&old_value[..])?;
let key = database.database_identity().to_hex();
let key = database.database_identity().to_be_byte_array();

tree_by_identity.remove(key.as_bytes())?;
tree_by_identity.remove(&key[..])?;
tree.remove(id.to_be_bytes())?;
return Ok(Some(id));
}
Expand Down

0 comments on commit 3b80687

Please sign in to comment.