diff --git a/fortuna/src/api.rs b/fortuna/src/api.rs index 7964e22857..f482c41523 100644 --- a/fortuna/src/api.rs +++ b/fortuna/src/api.rs @@ -1,8 +1,9 @@ use { crate::{ chain::reader::{ - EntropyReader, + BlockNumber, BlockStatus, + EntropyReader, }, state::HashChainState, }, @@ -80,7 +81,7 @@ pub struct BlockchainState { pub provider_address: Address, /// The server will wait for this many block confirmations of a request before revealing /// the random number. - pub reveal_delay_blocks: u64, + pub reveal_delay_blocks: BlockNumber, /// The BlockStatus of the block that includes the random number request. /// For eg., Finalized, Safe pub confirmed_block_status: BlockStatus, @@ -217,8 +218,7 @@ mod test { TestResponse, TestServer, }, - ethers:: - prelude::Address, + ethers::prelude::Address, lazy_static::lazy_static, std::sync::Arc, }; diff --git a/fortuna/src/chain/ethereum.rs b/fortuna/src/chain/ethereum.rs index 55c29d8e71..f5857b7576 100644 --- a/fortuna/src/chain/ethereum.rs +++ b/fortuna/src/chain/ethereum.rs @@ -2,8 +2,9 @@ use { crate::{ chain::reader::{ self, - EntropyReader, + BlockNumber, BlockStatus, + EntropyReader, }, config::EthereumConfig, }, @@ -38,7 +39,7 @@ use { LocalWallet, Signer, }, - types::transaction::eip2718::TypedTransaction, + types::{transaction::eip2718::TypedTransaction, BlockId}, }, sha3::{ Digest, @@ -208,18 +209,17 @@ impl EntropyReader for PythContract { } } - async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result { + async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result { + let block_id: BlockId = confirmed_block_status.into(); let block = self .client() - .get_block(confirmed_block_status.into()) + .get_block(block_id) .await? .ok_or_else(|| Error::msg("pending block confirmation"))?; - println!("{:?}", block); - Ok(block .number - .ok_or_else(|| Error::msg("pending confirmation"))? + .ok_or_else(|| Error::msg("pending confirmation"))? .as_u64()) } } diff --git a/fortuna/src/chain/reader.rs b/fortuna/src/chain/reader.rs index 9cf15ccfd2..0744d3e30b 100644 --- a/fortuna/src/chain/reader.rs +++ b/fortuna/src/chain/reader.rs @@ -3,12 +3,15 @@ use { axum::async_trait, ethers::types::{ Address, - BlockNumber, + BlockNumber as EthersBlockNumber, + BlockId, }, }; +pub type BlockNumber = u64; + /// A block status. -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum BlockStatus { /// Latest block #[default] @@ -19,12 +22,12 @@ pub enum BlockStatus { Safe, } -impl Into for BlockStatus { - fn into(self) ->BlockNumber { +impl Into for BlockStatus { + fn into(self) -> BlockId { match self { - BlockStatus::Latest => BlockNumber::Latest, - BlockStatus::Finalized => BlockNumber::Finalized, - BlockStatus::Safe => BlockNumber::Safe, + BlockStatus::Latest => BlockId::Number(EthersBlockNumber::Latest), + BlockStatus::Finalized => BlockId::Number(EthersBlockNumber::Finalized), + BlockStatus::Safe => BlockId::Number(EthersBlockNumber::Safe), } } } @@ -38,7 +41,7 @@ pub trait EntropyReader: Send + Sync { async fn get_request(&self, provider: Address, sequence_number: u64) -> Result>; - async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result; + async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result; } /// An in-flight request stored in the contract. @@ -49,7 +52,7 @@ pub struct Request { pub provider: Address, pub sequence_number: u64, // The block number where this request was created - pub block_number: u64, + pub block_number: BlockNumber, pub use_blockhash: bool, } @@ -65,7 +68,7 @@ pub mod mock { axum::async_trait, ethers::types::{ Address, - BlockNumber, + EthersBlockNumber, }, std::sync::RwLock, }; @@ -74,15 +77,15 @@ pub mod mock { /// This class is internally locked to allow tests to modify the in-flight requests while /// the API is also holding a pointer to the same data structure. pub struct MockEntropyReader { - block_number: RwLock, + block_number: RwLock, /// The set of requests that are currently in-flight. requests: RwLock>, } impl MockEntropyReader { pub fn with_requests( - block_number: u64, - requests: &[(Address, u64, u64, bool)], + block_number: BlockNumber, + requests: &[(Address, u64, BlockNumber, bool)], ) -> MockEntropyReader { MockEntropyReader { block_number: RwLock::new(block_number), @@ -105,7 +108,7 @@ pub mod mock { &self, provider: Address, sequence: u64, - block_number: u64, + block_number: BlockNumber, use_blockhash: bool, ) -> &Self { self.requests.write().unwrap().push(Request { @@ -117,7 +120,7 @@ pub mod mock { self } - pub fn set_block_number(&self, block_number: u64) -> &Self { + pub fn set_block_number(&self, block_number: BlockNumber) -> &Self { *(self.block_number.write().unwrap()) = block_number; self } @@ -139,7 +142,10 @@ pub mod mock { .map(|r| (*r).clone())) } - async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result { + async fn get_block_number( + &self, + confirmed_block_status: BlockStatus, + ) -> Result { Ok(*self.block_number.read().unwrap()) } } diff --git a/fortuna/src/config.rs b/fortuna/src/config.rs index dd0586ed89..ad3d39690f 100644 --- a/fortuna/src/config.rs +++ b/fortuna/src/config.rs @@ -1,5 +1,11 @@ use { - crate::{api::ChainId, chain::reader::BlockStatus}, + crate::{ + api::ChainId, + chain::reader::{ + BlockNumber, + BlockStatus, + }, + }, anyhow::{ anyhow, Result, @@ -12,8 +18,7 @@ use { Args, Parser, }, - ethers::types:: - Address, + ethers::types::Address, std::{ collections::HashMap, fs, @@ -129,8 +134,11 @@ pub struct EthereumConfig { /// Address of a Pyth Randomness contract to interact with. pub contract_addr: Address, - /// How many blocks to wait before revealing the random number. - pub reveal_delay_blocks: u64, + /// reveal_delay_blocks - The difference between the block number with the + /// confirmed_block_status(see below) and the block number of a request to + /// Entropy should be greater than `reveal_delay_blocks` for Fortuna to reveal + /// its commitment. + pub reveal_delay_blocks: BlockNumber, /// Use the legacy transaction format (for networks without EIP 1559) #[serde(default)]