Skip to content

Commit e3f51d3

Browse files
committed
update
1 parent ee868a9 commit e3f51d3

File tree

7 files changed

+82
-55
lines changed

7 files changed

+82
-55
lines changed

apps/fortuna/src/api.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::history::History;
2-
use serde::Serialize;
3-
use utoipa::ToSchema;
42
use {
53
crate::{
64
chain::reader::{BlockNumber, BlockStatus, EntropyReader},

apps/fortuna/src/api/explorer.rs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,19 @@ use crate::api::{ChainId, RestError};
22
use crate::history::RequestStatus;
33
use axum::extract::{Query, State};
44
use axum::Json;
5-
use ethers::types::TxHash;
5+
use ethers::types::{Address, TxHash};
6+
use std::str::FromStr;
67
use utoipa::{IntoParams, ToSchema};
78

89
#[derive(Debug, serde::Serialize, serde::Deserialize, IntoParams)]
910
#[into_params(parameter_in=Query)]
1011
pub struct ExplorerQueryParams {
11-
pub mode: ExplorerQueryParamsMode,
12-
1312
pub min_timestamp: Option<u64>,
1413
pub max_timestamp: Option<u64>,
15-
pub sequence_id: Option<u64>,
16-
#[param(value_type = Option<String>)]
17-
pub tx_hash: Option<TxHash>,
14+
pub query: Option<String>,
1815
#[param(value_type = Option<String>)]
1916
pub chain_id: Option<ChainId>,
2017
}
21-
#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
22-
#[serde(rename_all = "kebab-case")]
23-
pub enum ExplorerQueryParamsMode {
24-
TxHash,
25-
ChainAndSequence,
26-
ChainAndTimestamp,
27-
Timestamp,
28-
}
2918

3019
#[utoipa::path(
3120
get,
@@ -39,35 +28,27 @@ pub async fn explorer(
3928
State(state): State<crate::api::ApiState>,
4029
Query(query_params): Query<ExplorerQueryParams>,
4130
) -> anyhow::Result<Json<Vec<RequestStatus>>, RestError> {
42-
let result = match query_params.mode {
43-
ExplorerQueryParamsMode::TxHash => {
44-
let tx_hash = query_params.tx_hash.ok_or(RestError::BadFilterParameters(
45-
"tx_hash is required when mode=tx-hash".to_string(),
46-
))?;
47-
state.history.get_request_logs_by_tx_hash(tx_hash).await
48-
}
49-
ExplorerQueryParamsMode::ChainAndSequence => {
50-
let chain_id = query_params.chain_id.ok_or(RestError::BadFilterParameters(
51-
"chain_id is required when mode=chain-and-sequence".to_string(),
52-
))?;
53-
let sequence_id = query_params
54-
.sequence_id
55-
.ok_or(RestError::BadFilterParameters(
56-
"sequence_id is required when mode=chain-and-sequence".to_string(),
57-
))?;
58-
state
59-
.history
60-
.get_request_logs(&(chain_id, sequence_id))
61-
.await
62-
.into_iter()
63-
.collect()
31+
if let Some(query) = query_params.query {
32+
if let Ok(tx_hash) = TxHash::from_str(&query) {
33+
return Ok(Json(state.history.get_requests_by_tx_hash(tx_hash).await));
6434
}
65-
ExplorerQueryParamsMode::ChainAndTimestamp => {
66-
vec![]
35+
if let Ok(sender) = Address::from_str(&query) {
36+
return Ok(Json(
37+
state
38+
.history
39+
.get_requests_by_sender(sender, query_params.chain_id)
40+
.await,
41+
));
6742
}
68-
ExplorerQueryParamsMode::Timestamp => {
69-
vec![]
43+
if let Ok(sequence_number) = u64::from_str(&query) {
44+
return Ok(Json(
45+
state
46+
.history
47+
.get_requests_by_sequence(sequence_number, query_params.chain_id)
48+
.await,
49+
));
7050
}
71-
};
72-
Ok(Json(result))
51+
}
52+
//TODO: handle more types of queries
53+
Ok(Json(vec![]))
7354
}

apps/fortuna/src/chain/reader.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use ethers::prelude::LogMeta;
2-
use ethers::types::TxHash;
32
use {
43
anyhow::Result,
54
axum::async_trait,

apps/fortuna/src/command/run.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub async fn run_api(
4141
schemas(
4242
crate::api::GetRandomValueResponse,
4343
crate::history::RequestStatus,
44-
crate::api::ExplorerQueryParamsMode,
4544
crate::api::Blob,
4645
crate::api::BinaryEncoding,
4746
)

apps/fortuna/src/eth_utils/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ pub async fn submit_tx_with_backoff<T: Middleware + NonceManaged + 'static>(
157157
call: ContractCall<T, ()>,
158158
gas_limit: U256,
159159
escalation_policy: EscalationPolicy,
160-
history: Arc<History>,
161160
) -> Result<SubmitTxResult> {
162161
let start_time = std::time::Instant::now();
163162

apps/fortuna/src/history.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ impl From<RequestRow> for RequestStatus {
9393
pub struct History {
9494
pool: Pool<Sqlite>,
9595
write_queue: mpsc::Sender<RequestStatus>,
96-
writer_thread: Arc<tokio::task::JoinHandle<()>>,
96+
_writer_thread: Arc<tokio::task::JoinHandle<()>>,
9797
}
9898

9999
impl History {
100-
const MAX_HISTORY: usize = 1_000_000;
101100
const MAX_WRITE_QUEUE: usize = 1_000;
102101
pub async fn new() -> Self {
103102
Self::new_with_url("sqlite:fortuna.db").await
@@ -124,14 +123,14 @@ impl History {
124123
Self {
125124
pool,
126125
write_queue: sender,
127-
writer_thread: Arc::new(writer_thread),
126+
_writer_thread: Arc::new(writer_thread),
128127
}
129128
}
130129

131130
async fn update_request_status(pool: &Pool<Sqlite>, new_status: RequestStatus) {
132131
let sequence = new_status.sequence as i64;
133132
let chain_id = new_status.chain_id;
134-
let state = match new_status.state {
133+
match new_status.state {
135134
RequestEntryState::Pending => {
136135
let block_number = new_status.request_block_number as i64;
137136
let request_tx_hash: String = new_status.request_tx_hash.encode_hex();
@@ -204,7 +203,7 @@ impl History {
204203
self.get_from_db(request_key.clone()).await
205204
}
206205

207-
pub async fn get_request_logs_by_tx_hash(&self, tx_hash: TxHash) -> Vec<RequestStatus> {
206+
pub async fn get_requests_by_tx_hash(&self, tx_hash: TxHash) -> Vec<RequestStatus> {
208207
let tx_hash: String = tx_hash.encode_hex();
209208
let rows = sqlx::query_as!(
210209
RequestRow,
@@ -218,6 +217,58 @@ impl History {
218217
rows.into_iter().map(|row| row.into()).collect()
219218
}
220219

220+
pub async fn get_requests_by_sender(
221+
&self,
222+
sender: Address,
223+
chain_id: Option<ChainId>,
224+
) -> Vec<RequestStatus> {
225+
let sender: String = sender.encode_hex();
226+
let rows = match chain_id {
227+
Some(chain_id) => sqlx::query_as!(
228+
RequestRow,
229+
"SELECT * FROM request WHERE sender = ? AND chain_id = ?",
230+
sender,
231+
chain_id,
232+
)
233+
.fetch_all(&self.pool)
234+
.await
235+
.unwrap(),
236+
None => sqlx::query_as!(RequestRow, "SELECT * FROM request WHERE sender = ?", sender,)
237+
.fetch_all(&self.pool)
238+
.await
239+
.unwrap(),
240+
};
241+
rows.into_iter().map(|row| row.into()).collect()
242+
}
243+
244+
pub async fn get_requests_by_sequence(
245+
&self,
246+
sequence: u64,
247+
chain_id: Option<ChainId>,
248+
) -> Vec<RequestStatus> {
249+
let sequence = sequence as i64;
250+
let rows = match chain_id {
251+
Some(chain_id) => sqlx::query_as!(
252+
RequestRow,
253+
"SELECT * FROM request WHERE sequence = ? AND chain_id = ?",
254+
sequence,
255+
chain_id,
256+
)
257+
.fetch_all(&self.pool)
258+
.await
259+
.unwrap(),
260+
None => sqlx::query_as!(
261+
RequestRow,
262+
"SELECT * FROM request WHERE sequence = ?",
263+
sequence,
264+
)
265+
.fetch_all(&self.pool)
266+
.await
267+
.unwrap(),
268+
};
269+
rows.into_iter().map(|row| row.into()).collect()
270+
}
271+
221272
pub async fn get_latest_requests(
222273
&self,
223274
chain_id: Option<&ChainId>,
@@ -251,7 +302,8 @@ impl History {
251302
}
252303

253304
mod tests {
254-
use super::*;
305+
use crate::history::{History, RequestEntryState, RequestStatus};
306+
use ethers::types::{Address, TxHash};
255307
use tokio::time::sleep;
256308

257309
#[tokio::test]
@@ -274,7 +326,7 @@ mod tests {
274326

275327
#[tokio::test(flavor = "multi_thread")]
276328
async fn test_writer_thread() {
277-
let mut history = History::new_in_memory().await;
329+
let history = History::new_in_memory().await;
278330
let status = RequestStatus {
279331
chain_id: "ethereum".to_string(),
280332
sequence: 1,

apps/fortuna/src/keeper/process_event.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ pub async fn process_event_with_backoff(
7474
contract_call,
7575
gas_limit,
7676
escalation_policy,
77-
history.clone(),
7877
)
7978
.await;
8079

0 commit comments

Comments
 (0)