Skip to content

Commit

Permalink
implement a reader and writer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfirefist committed May 16, 2024
1 parent 23f8d02 commit a9a098a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apps/fortuna/src/chain/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use {
anyhow::{
Error,
Result,
},
axum::async_trait,
};

pub type ChainBlockNumber = u64;

#[derive(Clone, Debug)]
pub struct RequestWithCallbackData {
pub sequence_number: u64,
/// The random number submitted by the user while requesting a callback.
pub user_random_number: [u8; 32],
}

#[async_trait]
pub trait ChainReader: Send + Sync {
/// Returns data of all the requests with callback made on chain between
/// the given block numbers.
async fn get_requests_with_callback_data(
&self,
from_block: ChainBlockNumber,
to_block: ChainBlockNumber,
) -> Result<Vec<RequestWithCallbackData>>;

/// Returns the latest block which we consider to be included into the chain and
/// is safe from reorgs.
async fn get_latest_safe_block(&self) -> Result<ChainBlockNumber>;
}

pub enum RevealError {
HashChainRevealFailed(Error),
ContractError(Error),
RpcError(Error),
}

#[async_trait]
pub trait ChainWriter: Send + Sync + ChainReader {
/// Fulfill the given request on chain with the given provider revelation.
async fn reveal_with_callback(
&self,
request_with_callback_data: RequestWithCallbackData,
provider_revelation: [u8; 32],
) -> Result<(), RevealError>;
}

0 comments on commit a9a098a

Please sign in to comment.