|
| 1 | +use std::collections::{BTreeMap, VecDeque}; |
| 2 | +use chrono::DateTime; |
| 3 | +use ethers::types::TxHash; |
1 | 4 | use { |
2 | 5 | crate::{ |
3 | 6 | chain::reader::{BlockNumber, BlockStatus, EntropyReader}, |
@@ -41,10 +44,107 @@ pub struct ApiMetrics { |
41 | 44 | pub http_requests: Family<RequestLabel, Counter>, |
42 | 45 | } |
43 | 46 |
|
| 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 | + |
44 | 142 | #[derive(Clone)] |
45 | 143 | pub struct ApiState { |
46 | 144 | pub chains: Arc<HashMap<ChainId, BlockchainState>>, |
47 | 145 |
|
| 146 | + // pub history: Arc<History> |
| 147 | + |
48 | 148 | pub metrics_registry: Arc<RwLock<Registry>>, |
49 | 149 |
|
50 | 150 | /// Prometheus metrics |
|
0 commit comments