Skip to content

Commit f581b27

Browse files
committed
temp
1 parent 82d8685 commit f581b27

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

apps/fortuna/src/api.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::collections::{BTreeMap, VecDeque};
2+
use chrono::DateTime;
3+
use ethers::types::TxHash;
14
use {
25
crate::{
36
chain::reader::{BlockNumber, BlockStatus, EntropyReader},
@@ -41,10 +44,107 @@ pub struct ApiMetrics {
4144
pub http_requests: Family<RequestLabel, Counter>,
4245
}
4346

47+
48+
49+
#[derive(Clone)]
50+
enum JournalLog {
51+
Observed,
52+
FailedToReveal {
53+
reason: String,
54+
},
55+
Revealed {
56+
tx_hash: TxHash
57+
},
58+
Landed {
59+
block_number: BlockNumber,
60+
}
61+
}
62+
63+
#[derive(Clone)]
64+
struct TimedJournalLog {
65+
pub timestamp: DateTime<chrono::Utc>,
66+
pub log:JournalLog,
67+
}
68+
69+
#[derive(Clone)]
70+
struct RequestJournal {
71+
pub chain_id: ChainId,
72+
pub sequence: u64,
73+
pub journal: Vec<TimedJournalLog>,
74+
}
75+
76+
type RequestKey = (ChainId, u64);
77+
78+
#[derive(Default)]
79+
struct History {
80+
pub by_time: VecDeque<RequestKey>,
81+
pub by_chain: BTreeMap<RequestKey, RequestJournal>,
82+
}
83+
84+
impl History {
85+
const MAX_HISTORY: usize = 1_000_000;
86+
pub fn new() -> Self {
87+
Self::default()
88+
}
89+
90+
pub fn add(&mut self, (chain_id, sequence): RequestKey, request_journal_log: TimedJournalLog){
91+
// Add to the by_chain map
92+
let mut new_entry = false;
93+
let entry = self.by_chain.entry((chain_id.clone(), sequence)).or_insert_with(|| {
94+
new_entry = true;
95+
RequestJournal {
96+
chain_id: chain_id.clone(),
97+
sequence,
98+
journal: vec![],
99+
}
100+
});
101+
entry.journal.push(request_journal_log);
102+
if new_entry {
103+
self.by_time.push_back((chain_id.clone(), sequence));
104+
if self.by_time.len() > Self::MAX_HISTORY {
105+
let oldest_key = self.by_time.pop_front().unwrap();
106+
self.by_chain.remove(&oldest_key);
107+
}
108+
}
109+
}
110+
111+
pub fn get_request_logs(&self, request_key: &RequestKey) -> Option<&Vec<TimedJournalLog>> {
112+
self.by_chain.get(request_key).map(|entry| &entry.journal)
113+
}
114+
115+
pub fn get_latest_requests(&self, chain_id: Option<&ChainId>, limit: u64) -> Vec<RequestJournal> {
116+
match chain_id {
117+
Some(chain_id) => {
118+
let range = self.by_chain.range((chain_id.clone(), 0)..(chain_id.clone(), u64::MAX));
119+
range.rev()
120+
.take(limit as usize)
121+
.map(|(_, entry)| entry.clone())
122+
.collect()
123+
124+
},
125+
None => {
126+
self.by_time
127+
.iter()
128+
.rev()
129+
.take(limit as usize)
130+
.map(|request_key| {
131+
self.by_chain.get(request_key).unwrap().clone()
132+
})
133+
.collect::<Vec<_>>()
134+
},
135+
}
136+
}
137+
138+
139+
}
140+
141+
44142
#[derive(Clone)]
45143
pub struct ApiState {
46144
pub chains: Arc<HashMap<ChainId, BlockchainState>>,
47145

146+
// pub history: Arc<History>
147+
48148
pub metrics_registry: Arc<RwLock<Registry>>,
49149

50150
/// Prometheus metrics

apps/fortuna/src/command/run.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
137137
let rpc_metrics = Arc::new(RpcMetrics::new(metrics_registry.clone()).await);
138138

139139
let mut tasks = Vec::new();
140+
tracing::info!("Starting Fortuna server...");
140141
for (chain_id, chain_config) in config.chains.clone() {
141142
let secret_copy = secret.clone();
142143
let rpc_metrics = rpc_metrics.clone();
@@ -155,6 +156,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
155156
}));
156157
}
157158
let states = join_all(tasks).await;
159+
tracing::info!("Finished setting up chains");
158160

159161
let mut chains: HashMap<ChainId, BlockchainState> = HashMap::new();
160162
for result in states {
@@ -249,6 +251,7 @@ async fn setup_chain_state(
249251
{
250252
return Err(anyhow!("The current hash chain for chain id {} has configured commitments for sequence numbers greater than the current on-chain sequence number. Are the commitments configured correctly?", &chain_id));
251253
}
254+
tracing::info!("latest metadata: {:?}", latest_metadata);
252255

253256
provider_commitments.push(Commitment {
254257
seed: latest_metadata.seed,

0 commit comments

Comments
 (0)