Skip to content

Commit

Permalink
refactor: re-format files according to new rustfmt config (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jns-ps authored Oct 31, 2024
1 parent 6344658 commit 2982abf
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 192 deletions.
5 changes: 2 additions & 3 deletions crates/common/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ impl TryFrom<String> for VerifyingKey {
/// * `Ok(VerifyingKey)` if the conversion was successful.
/// * `Err` if the input is invalid or the conversion failed.
fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
let bytes = engine
.decode(s)
.map_err(|e| anyhow!("Failed to decode base64 string: {}", e))?;
let bytes =
engine.decode(s).map_err(|e| anyhow!("Failed to decode base64 string: {}", e))?;

match bytes.len() {
32 => Ok(VerifyingKey::Ed25519(bytes)),
Expand Down
34 changes: 9 additions & 25 deletions crates/common/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl TestTreeState {
return Err(anyhow!("{:?} already contained in tree", account.key_hash));
}

let proof = self
.tree
.process_operation(&account.hashchain.last().unwrap().operation)?;
let proof = self.tree.process_operation(&account.hashchain.last().unwrap().operation)?;
if let Proof::Insert(insert_proof) = proof {
self.inserted_keys.insert(account.key_hash);
return Ok(insert_proof);
Expand All @@ -101,9 +99,7 @@ impl TestTreeState {
return Err(anyhow!("{:?} not found in tree", account.key_hash));
}

let proof = self
.tree
.process_operation(&account.hashchain.last().unwrap().operation)?;
let proof = self.tree.process_operation(&account.hashchain.last().unwrap().operation)?;
if let Proof::Update(update_proof) = proof {
return Ok(update_proof);
}
Expand Down Expand Up @@ -186,16 +182,12 @@ impl Default for TestTreeState {

pub fn create_random_insert(state: &mut TestTreeState, rng: &mut StdRng) -> InsertProof {
loop {
let random_string: String = (0..10)
.map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
.collect();
let random_string: String =
(0..10).map(|_| rng.sample(rand::distributions::Alphanumeric) as char).collect();
let sk = create_mock_signing_key();

let (_, service) = state
.services
.iter()
.nth(rng.gen_range(0..state.services.len()))
.unwrap();
let (_, service) =
state.services.iter().nth(rng.gen_range(0..state.services.len())).unwrap();

let hc = create_new_hashchain(random_string.as_str(), &sk, service.clone()); //Hashchain::new(random_string);
let key = hc.get_keyhash();
Expand All @@ -214,11 +206,7 @@ pub fn create_random_update(state: &mut TestTreeState, rng: &mut StdRng) -> Upda
panic!("No keys have been inserted yet. Cannot perform update.");
}

let key = *state
.inserted_keys
.iter()
.nth(rng.gen_range(0..state.inserted_keys.len()))
.unwrap();
let key = *state.inserted_keys.iter().nth(rng.gen_range(0..state.inserted_keys.len())).unwrap();

let Found(mut hc, _) = state.tree.get(key).unwrap() else {
panic!("No response found for key. Cannot perform update.");
Expand All @@ -241,13 +229,9 @@ pub fn create_random_update(state: &mut TestTreeState, rng: &mut StdRng) -> Upda
0,
)
.unwrap();
hc.perform_operation(operation)
.expect("Adding to hashchain should succeed");
hc.perform_operation(operation).expect("Adding to hashchain should succeed");

state
.tree
.update(key, hc.last().unwrap().clone())
.expect("Update should succeed")
state.tree.update(key, hc.last().unwrap().clone()).expect("Update should succeed")
}

#[cfg(not(feature = "secp256k1"))]
Expand Down
52 changes: 13 additions & 39 deletions crates/common/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ pub struct MembershipProof {
impl MembershipProof {
pub fn verify(&self) -> Result<()> {
let value = bincode::serialize(&self.value)?;
self.proof
.verify_existence(self.root.into(), self.key, value)
self.proof.verify_existence(self.root.into(), self.key, value)
}
}

Expand Down Expand Up @@ -78,9 +77,7 @@ pub struct InsertProof {

impl InsertProof {
pub fn verify(&self) -> Result<()> {
self.non_membership_proof
.verify()
.context("Invalid NonMembershipProof")?;
self.non_membership_proof.verify().context("Invalid NonMembershipProof")?;

let value = bincode::serialize(&self.value)?;

Expand Down Expand Up @@ -116,8 +113,7 @@ impl UpdateProof {
// Verify existence of old value.
// Otherwise, any arbitrary hashchain could be set
let old_value = bincode::serialize(&self.old_value)?;
self.inclusion_proof
.verify_existence(self.old_root, self.key, old_value)?;
self.inclusion_proof.verify_existence(self.old_root, self.key, old_value)?;

// Append the new entry and verify it's validity
let new_hashchain = self.old_value.insert_unsafe(self.new_entry.clone());
Expand Down Expand Up @@ -214,9 +210,7 @@ where
}

pub fn get_current_root(&self) -> Result<RootHash> {
self.jmt
.get_root_hash(self.epoch)
.map_err(|e| anyhow!("Failed to get root hash: {}", e))
self.jmt.get_root_hash(self.epoch).map_err(|e| anyhow!("Failed to get root hash: {}", e))
}

fn serialize_value(value: &Hashchain) -> Result<Vec<u8>> {
Expand Down Expand Up @@ -311,10 +305,7 @@ where

value.verify_signature(
&bincode::serialize(
&new_account_entry
.operation
.without_challenge()
.without_signature(),
&new_account_entry.operation.without_challenge().without_signature(),
)?,
signature,
)?;
Expand Down Expand Up @@ -448,9 +439,7 @@ mod tests {
let service = tree_state.register_service("service_1".to_string());
let account = tree_state.create_account("key_1".to_string(), service.clone());

let insert_proof = tree_state
.insert_account(service.registration.clone())
.unwrap();
let insert_proof = tree_state.insert_account(service.registration.clone()).unwrap();
assert!(insert_proof.verify().is_ok());

let insert_proof = tree_state.insert_account(account.clone()).unwrap();
Expand Down Expand Up @@ -485,9 +474,7 @@ mod tests {

let account = tree_state.create_account("key_1".to_string(), falsified_service.clone());

let insert_proof = tree_state
.insert_account(service.registration.clone())
.unwrap();
let insert_proof = tree_state.insert_account(service.registration.clone()).unwrap();
assert!(insert_proof.verify().is_ok());

let insert_proof = tree_state.insert_account(account.clone());
Expand All @@ -500,9 +487,7 @@ mod tests {
let service = tree_state.register_service("service_1".to_string());
let account = tree_state.create_account("key_1".to_string(), service.clone());

let insert_proof = tree_state
.insert_account(service.registration.clone())
.unwrap();
let insert_proof = tree_state.insert_account(service.registration.clone()).unwrap();
assert!(insert_proof.verify().is_ok());

tree_state.insert_account(account.clone()).unwrap();
Expand All @@ -517,9 +502,7 @@ mod tests {

let service = tree_state.register_service("service_1".to_string());
let mut account = tree_state.create_account("key_1".to_string(), service.clone());
tree_state
.insert_account(service.registration.clone())
.unwrap();
tree_state.insert_account(service.registration.clone()).unwrap();
tree_state.insert_account(account.clone()).unwrap();

// Add a new key
Expand All @@ -538,9 +521,7 @@ mod tests {
let mut tree_state = TestTreeState::default();
let service = tree_state.register_service("service_1".to_string());
let account = tree_state.create_account("key_1".to_string(), service.clone());
tree_state
.insert_account(service.registration.clone())
.unwrap();
tree_state.insert_account(service.registration.clone()).unwrap();

let result = tree_state.update_account(account);
assert!(result.is_err());
Expand Down Expand Up @@ -577,13 +558,9 @@ mod tests {
tree_state.add_key_to_account(&mut account1).unwrap();
tree_state.update_account(account1.clone()).unwrap();

tree_state
.add_unsigned_data_to_account(b"unsigned", &mut account2)
.unwrap();
tree_state.add_unsigned_data_to_account(b"unsigned", &mut account2).unwrap();
tree_state.update_account(account2.clone()).unwrap();
tree_state
.add_signed_data_to_account(b"signed", &mut account2)
.unwrap();
tree_state.add_signed_data_to_account(b"signed", &mut account2).unwrap();
tree_state.update_account(account2.clone()).unwrap();

let get_result1 = tree_state.tree.get(account1.key_hash);
Expand Down Expand Up @@ -636,10 +613,7 @@ mod tests {
tree_state.insert_account(service.registration).unwrap();

let root_before = tree_state.tree.get_current_root().unwrap();
tree_state
.tree
.insert(account.key_hash, account.hashchain)
.unwrap();
tree_state.tree.insert(account.key_hash, account.hashchain).unwrap();
let root_after = tree_state.tree.get_current_root().unwrap();

assert_ne!(root_before, root_after);
Expand Down
6 changes: 2 additions & 4 deletions crates/da/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ impl FinalizedEpoch {
let message = bincode::serialize(&epoch_without_signature)
.map_err(|e| anyhow::anyhow!("Failed to serialize epoch: {}", e))?;

let signature = self
.signature
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No signature present"))?;
let signature =
self.signature.as_ref().ok_or_else(|| anyhow::anyhow!("No signature present"))?;

let signature_bytes = hex::decode(signature)
.map_err(|e| anyhow::anyhow!("Failed to decode signature: {}", e))?;
Expand Down
14 changes: 5 additions & 9 deletions crates/node_types/prover/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ impl Prover {
}
};

self.sync_loop(start_height, historical_sync_height, height_rx)
.await
self.sync_loop(start_height, historical_sync_height, height_rx).await
}

async fn sync_loop(
Expand All @@ -180,8 +179,7 @@ impl Prover {
let mut current_height = start_height;

while current_height <= end_height {
self.process_da_height(current_height, &mut buffered_operations, false)
.await?;
self.process_da_height(current_height, &mut buffered_operations, false).await?;
// TODO: Race between set_epoch and set_last_synced_height
self.db.set_last_synced_height(&current_height)?;
current_height += 1;
Expand All @@ -201,8 +199,7 @@ impl Prover {
height
));
}
self.process_da_height(height, &mut buffered_operations, true)
.await?;
self.process_da_height(height, &mut buffered_operations, true).await?;
current_height += 1;
// TODO: Race between set_epoch and set_last_synced_height - updating these should be a single atomic operation
self.db.set_last_synced_height(&current_height)?;
Expand Down Expand Up @@ -334,9 +331,8 @@ impl Prover {

let new_commitment = self.get_commitment().await?;

let finalized_epoch = self
.prove_epoch(epoch_height, prev_commitment, new_commitment, proofs)
.await?;
let finalized_epoch =
self.prove_epoch(epoch_height, prev_commitment, new_commitment, proofs).await?;

self.da.submit_finalized_epoch(finalized_epoch).await?;

Expand Down
5 changes: 1 addition & 4 deletions crates/node_types/prover/src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ async fn update_entry(
State(session): State<Arc<Prover>>,
Json(operation_input): Json<OperationInput>,
) -> impl IntoResponse {
match session
.validate_and_queue_update(&operation_input.operation)
.await
{
match session.validate_and_queue_update(&operation_input.operation).await {
Ok(_) => (
StatusCode::OK,
"Entry update queued for insertion into next epoch",
Expand Down
26 changes: 10 additions & 16 deletions crates/storage/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl RedisConnection {
}

let client = Client::open(connection_string).map_err(convert_to_connection_error)?;
let connection = client
.get_connection()
.map_err(convert_to_connection_error)?;
let connection = client.get_connection().map_err(convert_to_connection_error)?;

Ok(RedisConnection {
connection: Mutex::new(connection),
Expand All @@ -73,9 +71,7 @@ impl RedisConnection {
// because rust can not make sure that that's the case, we need to use the 'static lifetime here
// (but i dont really know why the issue pops up now and not before, i think we were using the same/similar pattern in the other functions)
fn lock_connection(&self) -> Result<MutexGuard<Connection>> {
self.connection
.lock()
.map_err(|_| anyhow!(DatabaseError::LockError))
self.connection.lock().map_err(|_| anyhow!(DatabaseError::LockError))
}
}

Expand Down Expand Up @@ -156,9 +152,8 @@ impl TreeWriter for RedisConnection {
impl Database for RedisConnection {
fn get_commitment(&self, epoch: &u64) -> Result<Digest> {
let mut con = self.lock_connection()?;
let redis_value = con
.get::<&str, String>(&format!("commitments:epoch_{}", epoch))
.map_err(|_| {
let redis_value =
con.get::<&str, String>(&format!("commitments:epoch_{}", epoch)).map_err(|_| {
DatabaseError::NotFoundError(format!("commitment from epoch_{}", epoch))
})?;

Expand All @@ -178,13 +173,12 @@ impl Database for RedisConnection {

fn set_last_synced_height(&self, height: &u64) -> Result<()> {
let mut con = self.lock_connection()?;
con.set::<&str, &u64, ()>("app_state:sync_height", height)
.map_err(|_| {
anyhow!(DatabaseError::WriteError(format!(
"sync_height: {}",
height
)))
})
con.set::<&str, &u64, ()>("app_state:sync_height", height).map_err(|_| {
anyhow!(DatabaseError::WriteError(format!(
"sync_height: {}",
height
)))
})
}

fn get_epoch(&self) -> Result<u64> {
Expand Down
13 changes: 4 additions & 9 deletions crates/storage/src/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ impl Database for RocksDBConnection {
DatabaseError::NotFoundError(format!("commitment from epoch_{}", epoch))
})?;

let value: [u8; 32] = raw_bytes
.try_into()
.expect("commitment digest should always be 32 bytes");
let value: [u8; 32] =
raw_bytes.try_into().expect("commitment digest should always be 32 bytes");

Ok(Digest(value))
}
Expand All @@ -57,9 +56,7 @@ impl Database for RocksDBConnection {
}

fn set_last_synced_height(&self, height: &u64) -> anyhow::Result<()> {
Ok(self
.connection
.put(b"app_state:sync_height", height.to_be_bytes())?)
Ok(self.connection.put(b"app_state:sync_height", height.to_be_bytes())?)
}

fn get_epoch(&self) -> anyhow::Result<u64> {
Expand All @@ -72,9 +69,7 @@ impl Database for RocksDBConnection {
}

fn set_epoch(&self, epoch: &u64) -> anyhow::Result<()> {
Ok(self
.connection
.put(b"app_state:epoch", epoch.to_be_bytes())?)
Ok(self.connection.put(b"app_state:epoch", epoch.to_be_bytes())?)
}

fn flush_database(&self) -> Result<()> {
Expand Down
Loading

0 comments on commit 2982abf

Please sign in to comment.