Ver Fonte

implement a reader and writer interface

0xfirefist há 1 ano atrás
pai
commit
a9a098a208
1 ficheiros alterados com 47 adições e 0 exclusões
  1. 47 0
      apps/fortuna/src/chain/chain.rs

+ 47 - 0
apps/fortuna/src/chain/chain.rs

@@ -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>;
+}