Skip to content

Commit

Permalink
block number type
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfirefist committed Feb 27, 2024
1 parent 0c6ccc9 commit 0b80c69
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
8 changes: 4 additions & 4 deletions fortuna/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
crate::{
chain::reader::{
EntropyReader,
BlockNumber,
BlockStatus,
EntropyReader,
},
state::HashChainState,
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -217,8 +218,7 @@ mod test {
TestResponse,
TestServer,
},
ethers::
prelude::Address,
ethers::prelude::Address,
lazy_static::lazy_static,
std::sync::Arc,
};
Expand Down
14 changes: 7 additions & 7 deletions fortuna/src/chain/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use {
crate::{
chain::reader::{
self,
EntropyReader,
BlockNumber,
BlockStatus,
EntropyReader,
},
config::EthereumConfig,
},
Expand Down Expand Up @@ -38,7 +39,7 @@ use {
LocalWallet,
Signer,
},
types::transaction::eip2718::TypedTransaction,
types::{transaction::eip2718::TypedTransaction, BlockId},
},
sha3::{
Digest,
Expand Down Expand Up @@ -208,18 +209,17 @@ impl EntropyReader for PythContract {
}
}

async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<u64> {
async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<BlockNumber> {
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())
}
}
38 changes: 22 additions & 16 deletions fortuna/src/chain/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -19,12 +22,12 @@ pub enum BlockStatus {
Safe,
}

impl Into<BlockNumber> for BlockStatus {
fn into(self) ->BlockNumber {
impl Into<BlockId> 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),
}
}
}
Expand All @@ -38,7 +41,7 @@ pub trait EntropyReader: Send + Sync {
async fn get_request(&self, provider: Address, sequence_number: u64)
-> Result<Option<Request>>;

async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<u64>;
async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<BlockNumber>;
}

/// An in-flight request stored in the contract.
Expand All @@ -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,
}

Expand All @@ -65,7 +68,7 @@ pub mod mock {
axum::async_trait,
ethers::types::{
Address,
BlockNumber,
EthersBlockNumber,
},
std::sync::RwLock,
};
Expand All @@ -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<u64>,
block_number: RwLock<BlockNumber>,
/// The set of requests that are currently in-flight.
requests: RwLock<Vec<Request>>,
}

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),
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -139,7 +142,10 @@ pub mod mock {
.map(|r| (*r).clone()))
}

async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<u64> {
async fn get_block_number(
&self,
confirmed_block_status: BlockStatus,
) -> Result<BlockNumber> {
Ok(*self.block_number.read().unwrap())
}
}
Expand Down
18 changes: 13 additions & 5 deletions fortuna/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use {
crate::{api::ChainId, chain::reader::BlockStatus},
crate::{
api::ChainId,
chain::reader::{
BlockNumber,
BlockStatus,
},
},
anyhow::{
anyhow,
Result,
Expand All @@ -12,8 +18,7 @@ use {
Args,
Parser,
},
ethers::types::
Address,
ethers::types::Address,
std::{
collections::HashMap,
fs,
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 0b80c69

Please sign in to comment.