Skip to content

Commit

Permalink
finality checks in Fortuna
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfirefist committed Feb 23, 2024
1 parent 0d49986 commit a461431
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 36 deletions.
2 changes: 1 addition & 1 deletion fortuna/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "3.2.4"
version = "3.3.4"
edition = "2021"

[dependencies]
Expand Down
15 changes: 9 additions & 6 deletions fortuna/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use {
crate::{
chain::reader::{
BlockNumber,
EntropyReader,
},
chain::reader::EntropyReader,
state::HashChainState,
},
anyhow::Result,
Expand All @@ -17,7 +14,10 @@ use {
routing::get,
Router,
},
ethers::core::types::Address,
ethers::{
core::types::Address,
types::BlockNumber,
},
prometheus_client::{
encoding::EncodeLabelSet,
metrics::{
Expand Down Expand Up @@ -80,7 +80,8 @@ 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: BlockNumber,
pub reveal_delay_blocks: u64,
pub recent_block_status: BlockNumber,
}

pub struct Metrics {
Expand Down Expand Up @@ -242,6 +243,7 @@ mod test {
contract: eth_read.clone(),
provider_address: PROVIDER,
reveal_delay_blocks: 1,
recent_block_status: BlockStatus::Latest,
};

let avax_read = Arc::new(MockEntropyReader::with_requests(10, &[]));
Expand All @@ -251,6 +253,7 @@ mod test {
contract: avax_read.clone(),
provider_address: PROVIDER,
reveal_delay_blocks: 2,
recent_block_status: BlockStatus::Latest,
};

let api_state = ApiState::new(&[
Expand Down
2 changes: 1 addition & 1 deletion fortuna/src/api/revelation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn revelation(

let maybe_request_fut = state.contract.get_request(state.provider_address, sequence);

let current_block_number_fut = state.contract.get_block_number();
let current_block_number_fut = state.contract.get_block_number(state.recent_block_status);

let (maybe_request, current_block_number) =
try_join!(maybe_request_fut, current_block_number_fut).map_err(|e| {
Expand Down
28 changes: 19 additions & 9 deletions fortuna/src/chain/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use {
crate::{
chain::{
reader,
reader::{
BlockNumber,
EntropyReader,
},
chain::reader::{
self,
EntropyReader,
},
config::EthereumConfig,
},
anyhow::{
anyhow,
Error,
Result,
},
axum::async_trait,
Expand Down Expand Up @@ -39,7 +37,10 @@ use {
LocalWallet,
Signer,
},
types::transaction::eip2718::TypedTransaction,
types::{
transaction::eip2718::TypedTransaction,
BlockNumber,
},
},
sha3::{
Digest,
Expand Down Expand Up @@ -209,7 +210,16 @@ impl EntropyReader for PythContract {
}
}

async fn get_block_number(&self) -> Result<BlockNumber> {
Ok(self.client().get_block_number().await?.as_u64())
async fn get_block_number(&self, recent_block_status: BlockNumber) -> Result<u64> {
let block = self
.client()
.get_block(recent_block_status)
.await?
.ok_or_else(|| Error::msg("pending block confirmation"))?;

Ok(block
.number
.ok_or_else(|| Error::msg("pending confirmation"))?
.as_u64())
}
}
24 changes: 12 additions & 12 deletions fortuna/src/chain/reader.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use {
anyhow::Result,
axum::async_trait,
ethers::types::Address,
ethers::types::{
Address,
BlockNumber,
},
};

pub type BlockNumber = u64;

/// EntropyReader is the read-only interface of the Entropy contract.
#[async_trait]
pub trait EntropyReader: Send + Sync {
Expand All @@ -15,7 +16,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) -> Result<BlockNumber>;
async fn get_block_number(&self, recent_block_status: BlockNumber) -> Result<u64>;
}

/// An in-flight request stored in the contract.
Expand All @@ -26,7 +27,7 @@ pub struct Request {
pub provider: Address,
pub sequence_number: u64,
// The block number where this request was created
pub block_number: BlockNumber,
pub block_number: u64,
pub use_blockhash: bool,
}

Expand All @@ -35,7 +36,6 @@ pub struct Request {
pub mod mock {
use {
crate::chain::reader::{
BlockNumber,
EntropyReader,
Request,
},
Expand All @@ -49,15 +49,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<BlockNumber>,
block_number: RwLock<u64>,
/// The set of requests that are currently in-flight.
requests: RwLock<Vec<Request>>,
}

impl MockEntropyReader {
pub fn with_requests(
block_number: BlockNumber,
requests: &[(Address, u64, BlockNumber, bool)],
block_number: u64,
requests: &[(Address, u64, u64, bool)],
) -> MockEntropyReader {
MockEntropyReader {
block_number: RwLock::new(block_number),
Expand All @@ -80,7 +80,7 @@ pub mod mock {
&self,
provider: Address,
sequence: u64,
block_number: BlockNumber,
block_number: u64,
use_blockhash: bool,
) -> &Self {
self.requests.write().unwrap().push(Request {
Expand All @@ -92,7 +92,7 @@ pub mod mock {
self
}

pub fn set_block_number(&self, block_number: BlockNumber) -> &Self {
pub fn set_block_number(&self, block_number: u64) -> &Self {
*(self.block_number.write().unwrap()) = block_number;
self
}
Expand All @@ -114,7 +114,7 @@ pub mod mock {
.map(|r| (*r).clone()))
}

async fn get_block_number(&self) -> Result<BlockNumber> {
async fn get_block_number(&self) -> Result<u64> {
Ok(*self.block_number.read().unwrap())
}
}
Expand Down
1 change: 1 addition & 0 deletions fortuna/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
contract,
provider_address: opts.provider,
reveal_delay_blocks: chain_config.reveal_delay_blocks,
recent_block_status: chain_config.block_status,
};

chains.insert(chain_id.clone(), state);
Expand Down
16 changes: 10 additions & 6 deletions fortuna/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
api::ChainId,
chain::reader::BlockNumber,
},
crate::api::ChainId,
anyhow::{
anyhow,
Result,
Expand All @@ -15,7 +12,10 @@ use {
Args,
Parser,
},
ethers::types::Address,
ethers::types::{
Address,
BlockNumber,
},
std::{
collections::HashMap,
fs,
Expand Down Expand Up @@ -132,9 +132,13 @@ pub struct EthereumConfig {
pub contract_addr: Address,

/// How many blocks to wait before revealing the random number.
pub reveal_delay_blocks: BlockNumber,
pub reveal_delay_blocks: u64,

/// Use the legacy transaction format (for networks without EIP 1559)
#[serde(default)]
pub legacy_tx: bool,

/// Use the legacy transaction format (for networks without EIP 1559)
#[serde(default)]
pub block_status: BlockNumber,
}

0 comments on commit a461431

Please sign in to comment.