mod fallback;
mod jsonrpc;
pub mod proof;
pub use fallback::Fallback;
pub use jsonrpc::{BalProbe, JsonRpcSource, ProbeReport, BAL_HASH_FIELD, BAL_METHOD};
pub use proof::{check_requested, verify_account_proof, ProofError};
use alloy_primitives::{Address, Bytes, B256, U256};
use async_trait::async_trait;
use bal_codec::BlockAccessList;
#[derive(Debug, thiserror::Error)]
pub enum SourceError {
#[error("transport: {0}")]
Transport(String),
#[error("rpc error {code}: {message}")]
Rpc {
code: i64,
message: String,
},
#[error("block {0} not found")]
BlockNotFound(u64),
#[error("eth_getBlockAccessList returned null for block {0}")]
NoBal(u64),
#[error("header of block {0} has no block_access_list_hash")]
NoBalHash(u64),
#[error("malformed response: {0}")]
Malformed(String),
#[error("codec: {0}")]
Codec(#[from] bal_codec::CodecError),
}
pub type Result<T> = std::result::Result<T, SourceError>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Header {
pub number: u64,
pub hash: B256,
pub parent_hash: B256,
pub state_root: B256,
pub timestamp: u64,
pub block_access_list_hash: Option<B256>,
}
#[derive(Clone, Debug)]
pub struct SourcedBlock {
pub header: Header,
pub bal: BlockAccessList,
}
#[async_trait]
pub trait BalSource: Send + Sync {
async fn head(&self) -> Result<u64>;
async fn finalized(&self) -> Result<u64>;
async fn block(&self, number: u64) -> Result<SourcedBlock>;
async fn header(&self, number: u64) -> Result<Header> {
Ok(self.block(number).await?.header)
}
async fn bal(&self, number: u64) -> Result<BlockAccessList> {
Ok(self.block(number).await?.bal)
}
}
#[derive(Clone, Debug)]
pub struct StorageProof {
pub key: B256,
pub value: U256,
pub proof: Vec<Bytes>,
}
#[derive(Clone, Debug)]
pub struct AccountProof {
pub address: Address,
pub balance: U256,
pub nonce: u64,
pub code_hash: B256,
pub storage_hash: B256,
pub account_proof: Vec<Bytes>,
pub storage_proofs: Vec<StorageProof>,
}
#[async_trait]
pub trait StateSource: Send + Sync {
async fn proof(&self, addr: Address, slots: &[B256], block: u64) -> Result<AccountProof>;
}