Skip to content

Commit b25800b

Browse files
committed
temp
1 parent 962350b commit b25800b

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

apps/fortuna/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ url = "2.5.0"
4141
chrono = { version = "0.4.38", features = [
4242
"clock",
4343
"std",
44+
"serde"
4445
], default-features = false }
4546
backoff = { version = "0.4.0", features = ["futures", "tokio"] }
4647
thiserror = "1.0.61"

apps/fortuna/src/api.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::collections::{BTreeMap, VecDeque};
22
use chrono::DateTime;
33
use ethers::types::TxHash;
4+
use serde::{Deserialize, Serialize};
5+
use utoipa::ToSchema;
46
use {
57
crate::{
68
chain::reader::{BlockNumber, BlockStatus, EntropyReader},
@@ -47,7 +49,7 @@ pub struct ApiMetrics {
4749

4850

4951

50-
#[derive(Clone)]
52+
#[derive(Clone, Debug, Serialize)]
5153
enum JournalLog {
5254
Observed {
5355
tx_hash: TxHash
@@ -74,13 +76,13 @@ impl JournalLog {
7476
}
7577
}
7678

77-
#[derive(Clone)]
79+
#[derive(Clone, Debug, Serialize)]
7880
struct TimedJournalLog {
7981
pub timestamp: DateTime<chrono::Utc>,
8082
pub log:JournalLog,
8183
}
8284

83-
#[derive(Clone)]
85+
#[derive(Clone, Debug, Serialize, ToSchema)]
8486
struct RequestJournal {
8587
pub chain_id: ChainId,
8688
pub sequence: u64,
@@ -90,7 +92,7 @@ struct RequestJournal {
9092
type RequestKey = (ChainId, u64);
9193

9294
#[derive(Default)]
93-
struct History {
95+
pub struct History {
9496
pub by_hash: HashMap<TxHash, Vec<RequestKey>>,
9597
pub by_chain_and_time: BTreeMap<(ChainId, DateTime<chrono::Utc>), RequestKey>,
9698
pub by_time: BTreeMap<DateTime<chrono::Utc>, RequestKey>,
@@ -133,16 +135,16 @@ impl History {
133135
}
134136
}
135137

136-
pub fn get_request_logs(&self, request_key: &RequestKey) -> Option<&RequestJournal> {
137-
self.by_request_key.get(request_key)
138+
pub fn get_request_logs(&self, request_key: &RequestKey) -> Option<RequestJournal> {
139+
self.by_request_key.get(request_key).cloned()
138140
}
139141

140-
pub fn get_request_logs_by_tx_hash(&self, tx_hash: TxHash) -> Option<Vec<&RequestJournal>> {
142+
pub fn get_request_logs_by_tx_hash(&self, tx_hash: TxHash) -> Vec<RequestJournal> {
141143
self.by_hash.get(&tx_hash).map(|request_keys| {
142144
request_keys.iter()
143-
.map(|request_key| self.by_request_key.get(request_key).unwrap())
145+
.map(|request_key| self.by_request_key.get(request_key).unwrap().clone())
144146
.collect()
145-
})
147+
}).unwrap_or_default()
146148
}
147149

148150
pub fn get_latest_requests(&self, chain_id: Option<&ChainId>, limit: u64,
@@ -190,6 +192,7 @@ impl ApiState {
190192
pub async fn new(
191193
chains: HashMap<ChainId, BlockchainState>,
192194
metrics_registry: Arc<RwLock<Registry>>,
195+
history: Arc<RwLock<History>>
193196
) -> ApiState {
194197
let metrics = ApiMetrics {
195198
http_requests: Family::default(),
@@ -205,7 +208,7 @@ impl ApiState {
205208
ApiState {
206209
chains: Arc::new(chains),
207210
metrics: Arc::new(metrics),
208-
history: Arc::new(RwLock::new(History::new())),
211+
history,
209212
metrics_registry,
210213
}
211214
}

apps/fortuna/src/api/explorer.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use axum::extract::{Path, Query, State};
22
use axum::Json;
33
use ethers::types::TxHash;
44
use utoipa::IntoParams;
5-
use crate::api::{BinaryEncoding, ChainId, GetRandomValueResponse, RestError, RevelationPathParams, RevelationQueryParams};
5+
use crate::api::{BinaryEncoding, ChainId, GetRandomValueResponse, RequestJournal, RestError, RevelationPathParams, RevelationQueryParams};
66
use crate::chain::reader::BlockNumber;
77

88

@@ -40,17 +40,25 @@ pub enum ExplorerQueryParamsMode {
4040
pub async fn get_requests(
4141
State(state): State<crate::api::ApiState>,
4242
Query(query_params): Query<ExplorerQueryParams>,
43-
) -> anyhow::Result<Json<()>, RestError> {
44-
match query_params.mode {
43+
) -> anyhow::Result<Json<Vec<RequestJournal>>, RestError> {
44+
let result = match query_params.mode {
4545
ExplorerQueryParamsMode::TxHash => {
4646
let tx_hash = query_params.tx_hash.ok_or(RestError::BadFilterParameters("tx_hash is required when mode=tx-hash".to_string()))?;
47-
state.history.read().await.get_request_logs_by_tx_hash(tx_hash);
47+
state.history.read().await.get_request_logs_by_tx_hash(tx_hash)
48+
}
49+
ExplorerQueryParamsMode::ChainAndSequence => {
50+
let chain_id = query_params.chain_id.ok_or(RestError::BadFilterParameters("chain_id is required when mode=chain-and-sequence".to_string()))?;
51+
let sequence_id = query_params.sequence_id.ok_or(RestError::BadFilterParameters("sequence_id is required when mode=chain-and-sequence".to_string()))?;
52+
state.history.read().await.get_request_logs(&(chain_id, sequence_id)).into_iter().collect()
53+
}
54+
ExplorerQueryParamsMode::ChainAndTimestamp => {
55+
vec![]
56+
}
57+
ExplorerQueryParamsMode::Timestamp => {
58+
vec![]
4859
}
49-
ExplorerQueryParamsMode::ChainAndSequence => {}
50-
ExplorerQueryParamsMode::ChainAndTimestamp => {}
51-
ExplorerQueryParamsMode::Timestamp => {}
5260
};
5361
Ok(
54-
Json(()),
62+
Json(result),
5563
)
5664
}

apps/fortuna/src/command/run.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use {
3535
utoipa::OpenApi,
3636
utoipa_swagger_ui::SwaggerUi,
3737
};
38+
use crate::api::History;
3839

3940
/// Track metrics in this interval
4041
const TRACK_INTERVAL: Duration = Duration::from_secs(10);
@@ -43,6 +44,7 @@ pub async fn run_api(
4344
socket_addr: SocketAddr,
4445
chains: HashMap<String, api::BlockchainState>,
4546
metrics_registry: Arc<RwLock<Registry>>,
47+
history: Arc<RwLock<History>>,
4648
mut rx_exit: watch::Receiver<bool>,
4749
) -> Result<()> {
4850
#[derive(OpenApi)]
@@ -64,7 +66,7 @@ pub async fn run_api(
6466
)]
6567
struct ApiDoc;
6668

67-
let api_state = api::ApiState::new(chains, metrics_registry).await;
69+
let api_state = api::ApiState::new(chains, metrics_registry, history).await;
6870

6971
// Initialize Axum Router. Note the type here is a `Router<State>` due to the use of the
7072
// `with_state` method which replaces `Body` with `State` in the type signature.
@@ -98,6 +100,7 @@ pub async fn run_keeper(
98100
config: Config,
99101
private_key: String,
100102
metrics_registry: Arc<RwLock<Registry>>,
103+
history: Arc<RwLock<History>>,
101104
rpc_metrics: Arc<RpcMetrics>,
102105
) -> Result<()> {
103106
let mut handles = Vec::new();
@@ -120,13 +123,15 @@ pub async fn run_keeper(
120123
chain_eth_config,
121124
chain_config.clone(),
122125
keeper_metrics.clone(),
126+
history.clone(),
123127
rpc_metrics.clone(),
124128
)));
125129
}
126130

127131
Ok(())
128132
}
129133

134+
130135
pub async fn run(opts: &RunOptions) -> Result<()> {
131136
let config = Config::load(&opts.config.config)?;
132137
let secret = config.provider.secret.load()?.ok_or(anyhow!(
@@ -187,12 +192,14 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
187192
Ok::<(), Error>(())
188193
});
189194

195+
let history = Arc::new(RwLock::new(History::default()));
190196
if let Some(keeper_private_key) = config.keeper.private_key.load()? {
191197
spawn(run_keeper(
192198
chains.clone(),
193199
config.clone(),
194200
keeper_private_key,
195201
metrics_registry.clone(),
202+
history.clone(),
196203
rpc_metrics.clone(),
197204
));
198205
} else {
@@ -206,7 +213,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
206213
rpc_metrics.clone(),
207214
));
208215

209-
run_api(opts.addr, chains, metrics_registry, rx_exit).await?;
216+
run_api(opts.addr, chains, metrics_registry, history, rx_exit).await?;
210217

211218
Ok(())
212219
}

apps/fortuna/src/keeper.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use {
2525
},
2626
tracing::{self, Instrument},
2727
};
28+
use crate::api::History;
2829

2930
pub(crate) mod block;
3031
pub(crate) mod commitment;
@@ -58,6 +59,7 @@ pub async fn run_keeper_threads(
5859
chain_eth_config: EthereumConfig,
5960
chain_state: BlockchainState,
6061
metrics: Arc<KeeperMetrics>,
62+
history: Arc<RwLock<History>>,
6163
rpc_metrics: Arc<RpcMetrics>,
6264
) {
6365
tracing::info!("starting keeper");

0 commit comments

Comments
 (0)