Skip to content

Commit

Permalink
fix: throw error instead of panicing on runtime checks
Browse files Browse the repository at this point in the history
  • Loading branch information
petarvujovic98 committed Jun 27, 2024
1 parent ca1c7b0 commit 7e48839
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions core/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use alloy_primitives::*;
use alloy_provider::{Provider, ReqwestProvider};
use alloy_rpc_types::{Block, BlockTransactions, Filter, Transaction as AlloyRpcTransaction};
use alloy_sol_types::{SolCall, SolEvent};
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, bail, ensure, Result};
use c_kzg::{Blob, KzgCommitment};
use raiko_lib::{
builder::{OptimisticDatabase, RethBlockBuilder},
Expand Down Expand Up @@ -276,7 +276,7 @@ async fn prepare_taiko_chain_input(
debug!("blob active");
// Get the blob hashes attached to the propose tx
let blob_hashes = proposal_tx.blob_versioned_hashes.unwrap_or_default();
assert!(!blob_hashes.is_empty());
ensure!(!blob_hashes.is_empty());
// Currently the protocol enforces the first blob hash to be used
let blob_hash = blob_hashes[0];
// Get the blob data for this block
Expand Down Expand Up @@ -305,7 +305,7 @@ async fn prepare_taiko_chain_input(
Some(anchor_tx.clone()),
);
// Do a sanity check using the transactions returned by the node
assert!(
ensure!(
transactions.len() >= block.transactions.len(),
"unexpected number of transactions"
);
Expand Down Expand Up @@ -409,7 +409,7 @@ async fn get_blob_data_beacon(
let response = reqwest::get(url.clone()).await?;
if response.status().is_success() {
let blobs: GetBlobsResponse = response.json().await?;
assert!(!blobs.data.is_empty(), "blob data not available anymore");
ensure!(!blobs.data.is_empty(), "blob data not available anymore");
// Get the blob data for the blob storing the tx list
let tx_blob = blobs
.data
Expand All @@ -419,7 +419,7 @@ async fn get_blob_data_beacon(
blob_hash == calc_blob_versioned_hash(&blob.blob)
})
.cloned();
assert!(tx_blob.is_some());
ensure!(tx_blob.is_some());
Ok(blob_to_bytes(&tx_blob.unwrap().blob))
} else {
warn!(
Expand Down Expand Up @@ -521,7 +521,7 @@ fn get_transactions_from_block(block: &Block) -> RaikoResult<Vec<TxEnvelope>> {
},
_ => unreachable!("Block is too old, please connect to an archive node or use a block that is at most 128 blocks old."),
};
assert!(
ensure!(
transactions.len() == block.transactions.len(),
"unexpected number of transactions"
);
Expand Down
29 changes: 14 additions & 15 deletions lib/src/protocol_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ impl ProtocolInstance {
)
.expect("Fail to calculate KZG commitment");
let versioned_hash = kzg_to_versioned_hash(&kzg_commit);
assert_eq!(
versioned_hash,
input.taiko.tx_blob_hash.unwrap(),
ensure!(
versioned_hash == input.taiko.tx_blob_hash.unwrap(),
"Blob version hash not matching"
);
drop(aligned_vec);
Expand All @@ -80,28 +79,28 @@ impl ProtocolInstance {
if let Some(verified_chain_spec) =
SupportedChainSpecs::default().get_chain_spec_with_chain_id(input.chain_spec.chain_id)
{
assert_eq!(
input.chain_spec.max_spec_id, verified_chain_spec.max_spec_id,
ensure!(
input.chain_spec.max_spec_id == verified_chain_spec.max_spec_id,
"unexpected max_spec_id"
);
assert_eq!(
input.chain_spec.hard_forks, verified_chain_spec.hard_forks,
ensure!(
input.chain_spec.hard_forks == verified_chain_spec.hard_forks,
"unexpected hard_forks"
);
assert_eq!(
input.chain_spec.eip_1559_constants, verified_chain_spec.eip_1559_constants,
ensure!(
input.chain_spec.eip_1559_constants == verified_chain_spec.eip_1559_constants,
"unexpected eip_1559_constants"
);
assert_eq!(
input.chain_spec.l1_contract, verified_chain_spec.l1_contract,
ensure!(
input.chain_spec.l1_contract == verified_chain_spec.l1_contract,
"unexpected l1_contract"
);
assert_eq!(
input.chain_spec.l2_contract, verified_chain_spec.l2_contract,
ensure!(
input.chain_spec.l2_contract == verified_chain_spec.l2_contract,
"unexpected l2_contract"
);
assert_eq!(
input.chain_spec.is_taiko, verified_chain_spec.is_taiko,
ensure!(
input.chain_spec.is_taiko == verified_chain_spec.is_taiko,
"unexpected eip_1559_constants"
);
}
Expand Down
1 change: 1 addition & 0 deletions task_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
hex = { workspace = true }
tracing = { workspace = true }
anyhow = { workspace = true }

[dev-dependencies]
rand = "0.9.0-alpha.1" # This is an alpha version, that has rng.gen_iter::<T>()
Expand Down
9 changes: 5 additions & 4 deletions task_manager/src/mem_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
TaskManagerResult, TaskProvingStatus, TaskProvingStatusRecords, TaskStatus,
};

use anyhow::ensure;
use chrono::Utc;
use raiko_core::interfaces::ProofType;
use raiko_lib::primitives::{keccak::keccak, ChainId, B256};
Expand Down Expand Up @@ -82,7 +83,7 @@ impl InMemoryTaskDb {
}
.into();
let key = keccak(td_data).into();
assert!(self.enqueue_task.contains_key(&key));
ensure!(self.enqueue_task.contains_key(&key));

let task_proving_records = self.enqueue_task.get(&key).unwrap();
let task_status = task_proving_records.last().unwrap().0;
Expand Down Expand Up @@ -128,7 +129,7 @@ impl InMemoryTaskDb {
&mut self,
task_id: u64,
) -> Result<TaskProvingStatusRecords, TaskManagerError> {
assert!(self.task_id_desc.contains_key(&task_id));
ensure!(self.task_id_desc.contains_key(&task_id));
let key = self.task_id_desc.get(&task_id).unwrap();
let task_status = self.enqueue_task.get(key).unwrap();
Ok(task_status.clone())
Expand All @@ -151,7 +152,7 @@ impl InMemoryTaskDb {
.to_vec(),
)
.into();
assert!(self.enqueue_task.contains_key(&key));
ensure!(self.enqueue_task.contains_key(&key));

let proving_status_records = self.enqueue_task.get(&key).unwrap();
let task_status = proving_status_records.last().unwrap();
Expand All @@ -164,7 +165,7 @@ impl InMemoryTaskDb {
}

fn get_task_proof_by_id(&mut self, task_id: u64) -> Result<Vec<u8>, TaskManagerError> {
assert!(self.task_id_desc.contains_key(&task_id));
ensure!(self.task_id_desc.contains_key(&task_id));
let key = self.task_id_desc.get(&task_id).unwrap();
let task_records = self.enqueue_task.get(key).unwrap();
let task_status = task_records.last().unwrap();
Expand Down

0 comments on commit 7e48839

Please sign in to comment.