use crate::errors;
use ethexe_common::{
SimpleBlockData,
db::{GlobalsStorageRO, OnChainStorageRO},
};
use ethexe_db::Database;
use jsonrpsee::core::RpcResult;
use sp_core::H256;
pub fn block_at_or_latest_synced(
db: &Database,
at: impl Into<Option<H256>>,
) -> RpcResult<SimpleBlockData> {
let hash = if let Some(hash) = at.into() {
if !db.block_synced(hash) {
return Err(errors::db("Requested block is not synced"));
}
hash
} else {
db.globals().latest_synced_eb.hash
};
db.block_header(hash)
.map(|header| SimpleBlockData { hash, header })
.ok_or_else(|| errors::db("Block header for requested hash wasn't found"))
}
pub fn latest_computed_mb(db: &Database) -> RpcResult<H256> {
let hash = db.globals().latest_computed_mb_hash;
if hash.is_zero() {
return Err(errors::db(
"no computed MB available yet; RPC reads require an MB-side state",
));
}
Ok(hash)
}