Skip to content

Commit 45df937

Browse files
authored
feat(argus): Delete more unneeded logic (#2486)
* clean up entropy stuff * delete more
1 parent a7aa4e8 commit 45df937

File tree

8 files changed

+26
-180
lines changed

8 files changed

+26
-180
lines changed

apps/argus/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/argus/src/api.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {
22
crate::{
3-
chain::reader::{BlockNumber, BlockStatus, EntropyReader},
3+
chain::reader::{BlockNumber, BlockStatus},
44
},
55
anyhow::Result,
66
axum::{
@@ -16,7 +16,7 @@ use {
1616
metrics::{counter::Counter, family::Family},
1717
registry::Registry,
1818
},
19-
std::{collections::HashMap, sync::Arc},
19+
std::sync::Arc,
2020
tokio::sync::RwLock,
2121
url::Url,
2222
};
@@ -40,8 +40,6 @@ pub struct ApiMetrics {
4040

4141
#[derive(Clone)]
4242
pub struct ApiState {
43-
pub chains: Arc<HashMap<ChainId, BlockchainState>>,
44-
4543
pub metrics_registry: Arc<RwLock<Registry>>,
4644

4745
/// Prometheus metrics
@@ -50,7 +48,6 @@ pub struct ApiState {
5048

5149
impl ApiState {
5250
pub async fn new(
53-
chains: HashMap<ChainId, BlockchainState>,
5451
metrics_registry: Arc<RwLock<Registry>>,
5552
) -> ApiState {
5653
let metrics = ApiMetrics {
@@ -65,7 +62,6 @@ impl ApiState {
6562
);
6663

6764
ApiState {
68-
chains: Arc::new(chains),
6965
metrics: Arc::new(metrics),
7066
metrics_registry,
7167
}
@@ -77,8 +73,6 @@ impl ApiState {
7773
pub struct BlockchainState {
7874
/// The chain id for this blockchain, useful for logging
7975
pub id: ChainId,
80-
/// The contract that the server is fulfilling requests for.
81-
pub contract: Arc<dyn EntropyReader>,
8276
/// The address of the provider that this server is operating for.
8377
pub provider_address: Address,
8478
/// The server will wait for this many block confirmations of a request before revealing

apps/argus/src/chain/ethereum.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {
22
crate::{
33
api::ChainId,
44
chain::reader::{
5-
self, BlockNumber, BlockStatus, EntropyReader, RequestedWithCallbackEvent,
5+
self, BlockNumber, BlockStatus, RequestedWithCallbackEvent,
66
},
77
config::EthereumConfig,
88
},
@@ -13,7 +13,6 @@ use {
1313
traced_client::{RpcMetrics, TracedClient},
1414
},
1515
anyhow::{anyhow, Error, Result},
16-
axum::async_trait,
1716
ethers::{
1817
abi::RawLog,
1918
contract::{abigen, EthLogDecode},
@@ -199,9 +198,9 @@ impl InstrumentedPythContract {
199198
}
200199
}
201200

202-
#[async_trait]
203-
impl<T: JsonRpcClient + 'static> EntropyReader for PythRandom<Provider<T>> {
204-
async fn get_request(
201+
impl<M: Middleware + 'static> PythRandom<M> {
202+
203+
pub async fn get_request_wrapper(
205204
&self,
206205
provider_address: Address,
207206
sequence_number: u64,
@@ -226,7 +225,7 @@ impl<T: JsonRpcClient + 'static> EntropyReader for PythRandom<Provider<T>> {
226225
}
227226
}
228227

229-
async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<BlockNumber> {
228+
pub async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<BlockNumber> {
230229
let block_number: EthersBlockNumber = confirmed_block_status.into();
231230
let block = self
232231
.client()
@@ -240,7 +239,7 @@ impl<T: JsonRpcClient + 'static> EntropyReader for PythRandom<Provider<T>> {
240239
.as_u64())
241240
}
242241

243-
async fn get_request_with_callback_events(
242+
pub async fn get_request_with_callback_events(
244243
&self,
245244
from_block: BlockNumber,
246245
to_block: BlockNumber,
@@ -260,7 +259,7 @@ impl<T: JsonRpcClient + 'static> EntropyReader for PythRandom<Provider<T>> {
260259
.collect())
261260
}
262261

263-
async fn estimate_reveal_with_callback_gas(
262+
pub async fn estimate_reveal_with_callback_gas(
264263
&self,
265264
sender: Address,
266265
provider: Address,

apps/argus/src/chain/reader.rs

Lines changed: 1 addition & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use {
2-
anyhow::Result,
3-
axum::async_trait,
4-
ethers::types::{Address, BlockNumber as EthersBlockNumber, U256},
2+
ethers::types::{Address, BlockNumber as EthersBlockNumber},
53
};
64

75
pub type BlockNumber = u64;
@@ -36,34 +34,6 @@ pub struct RequestedWithCallbackEvent {
3634
pub provider_address: Address,
3735
}
3836

39-
/// EntropyReader is the read-only interface of the Entropy contract.
40-
#[async_trait]
41-
pub trait EntropyReader: Send + Sync {
42-
/// Get an in-flight request (if it exists)
43-
/// Note that if we support additional blockchains in the future, the type of `provider` may
44-
/// need to become more generic.
45-
async fn get_request(&self, provider: Address, sequence_number: u64)
46-
-> Result<Option<Request>>;
47-
48-
async fn get_block_number(&self, confirmed_block_status: BlockStatus) -> Result<BlockNumber>;
49-
50-
async fn get_request_with_callback_events(
51-
&self,
52-
from_block: BlockNumber,
53-
to_block: BlockNumber,
54-
) -> Result<Vec<RequestedWithCallbackEvent>>;
55-
56-
/// Estimate the gas required to reveal a random number with a callback.
57-
async fn estimate_reveal_with_callback_gas(
58-
&self,
59-
sender: Address,
60-
provider: Address,
61-
sequence_number: u64,
62-
user_random_number: [u8; 32],
63-
provider_revelation: [u8; 32],
64-
) -> Result<U256>;
65-
}
66-
6737
/// An in-flight request stored in the contract.
6838
/// (This struct is missing many fields that are defined in the contract, as they
6939
/// aren't used in fortuna anywhere. Feel free to add any missing fields as necessary.)
@@ -75,110 +45,3 @@ pub struct Request {
7545
pub block_number: BlockNumber,
7646
pub use_blockhash: bool,
7747
}
78-
79-
#[cfg(test)]
80-
pub mod mock {
81-
use {
82-
crate::chain::reader::{BlockNumber, BlockStatus, EntropyReader, Request},
83-
anyhow::Result,
84-
axum::async_trait,
85-
ethers::types::{Address, U256},
86-
std::sync::RwLock,
87-
};
88-
89-
/// Mock version of the entropy contract intended for testing.
90-
/// This class is internally locked to allow tests to modify the in-flight requests while
91-
/// the API is also holding a pointer to the same data structure.
92-
pub struct MockEntropyReader {
93-
block_number: RwLock<BlockNumber>,
94-
/// The set of requests that are currently in-flight.
95-
requests: RwLock<Vec<Request>>,
96-
}
97-
98-
impl MockEntropyReader {
99-
pub fn with_requests(
100-
block_number: BlockNumber,
101-
requests: &[(Address, u64, BlockNumber, bool)],
102-
) -> MockEntropyReader {
103-
MockEntropyReader {
104-
block_number: RwLock::new(block_number),
105-
requests: RwLock::new(
106-
requests
107-
.iter()
108-
.map(|&(a, s, b, u)| Request {
109-
provider: a,
110-
sequence_number: s,
111-
block_number: b,
112-
use_blockhash: u,
113-
})
114-
.collect(),
115-
),
116-
}
117-
}
118-
119-
/// Insert a new request into the set of in-flight requests.
120-
pub fn insert(
121-
&self,
122-
provider: Address,
123-
sequence: u64,
124-
block_number: BlockNumber,
125-
use_blockhash: bool,
126-
) -> &Self {
127-
self.requests.write().unwrap().push(Request {
128-
provider,
129-
sequence_number: sequence,
130-
block_number,
131-
use_blockhash,
132-
});
133-
self
134-
}
135-
136-
pub fn set_block_number(&self, block_number: BlockNumber) -> &Self {
137-
*(self.block_number.write().unwrap()) = block_number;
138-
self
139-
}
140-
}
141-
142-
#[async_trait]
143-
impl EntropyReader for MockEntropyReader {
144-
async fn get_request(
145-
&self,
146-
provider: Address,
147-
sequence_number: u64,
148-
) -> Result<Option<Request>> {
149-
Ok(self
150-
.requests
151-
.read()
152-
.unwrap()
153-
.iter()
154-
.find(|&r| r.sequence_number == sequence_number && r.provider == provider)
155-
.map(|r| (*r).clone()))
156-
}
157-
158-
async fn get_block_number(
159-
&self,
160-
_confirmed_block_status: BlockStatus,
161-
) -> Result<BlockNumber> {
162-
Ok(*self.block_number.read().unwrap())
163-
}
164-
165-
async fn get_request_with_callback_events(
166-
&self,
167-
_from_block: BlockNumber,
168-
_to_block: BlockNumber,
169-
) -> Result<Vec<super::RequestedWithCallbackEvent>> {
170-
Ok(vec![])
171-
}
172-
173-
async fn estimate_reveal_with_callback_gas(
174-
&self,
175-
_sender: Address,
176-
_provider: Address,
177-
_sequence_number: u64,
178-
_user_random_number: [u8; 32],
179-
_provider_revelation: [u8; 32],
180-
) -> Result<U256> {
181-
Ok(U256::from(5))
182-
}
183-
}
184-
}

apps/argus/src/command/run.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
22
crate::{
33
api::{self, BlockchainState, ChainId},
4-
chain::ethereum::InstrumentedPythContract,
54
config::{Config, EthereumConfig, RunOptions},
65
keeper::{self, keeper_metrics::KeeperMetrics},
76
},
@@ -37,11 +36,10 @@ const TRACK_INTERVAL: Duration = Duration::from_secs(10);
3736

3837
pub async fn run_api(
3938
socket_addr: SocketAddr,
40-
chains: HashMap<String, api::BlockchainState>,
4139
metrics_registry: Arc<RwLock<Registry>>,
4240
mut rx_exit: watch::Receiver<bool>,
4341
) -> Result<()> {
44-
let api_state = api::ApiState::new(chains, metrics_registry).await;
42+
let api_state = api::ApiState::new(metrics_registry).await;
4543

4644
// Initialize Axum Router. Note the type here is a `Router<State>` due to the use of the
4745
// `with_state` method which replaces `Body` with `State` in the type signature.
@@ -111,13 +109,11 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
111109

112110
let mut tasks = Vec::new();
113111
for (chain_id, chain_config) in config.chains.clone() {
114-
let rpc_metrics = rpc_metrics.clone();
115112
tasks.push(spawn(async move {
116113
let state = setup_chain_state(
117114
&config.provider.address,
118115
&chain_id,
119116
&chain_config,
120-
rpc_metrics,
121117
)
122118
.await;
123119

@@ -174,7 +170,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
174170
rpc_metrics.clone(),
175171
));
176172

177-
run_api(opts.addr, chains, metrics_registry, rx_exit).await?;
173+
run_api(opts.addr, metrics_registry, rx_exit).await?;
178174

179175
Ok(())
180176
}
@@ -183,17 +179,9 @@ async fn setup_chain_state(
183179
provider: &Address,
184180
chain_id: &ChainId,
185181
chain_config: &EthereumConfig,
186-
rpc_metrics: Arc<RpcMetrics>,
187182
) -> Result<BlockchainState> {
188-
let contract = Arc::new(InstrumentedPythContract::from_config(
189-
chain_config,
190-
chain_id.clone(),
191-
rpc_metrics,
192-
)?);
193-
194183
let state = BlockchainState {
195184
id: chain_id.clone(),
196-
contract,
197185
provider_address: *provider,
198186
reveal_delay_blocks: chain_config.reveal_delay_blocks,
199187
confirmed_block_status: chain_config.confirmed_block_status,

apps/argus/src/keeper.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ pub async fn run_keeper_threads(
6161
rpc_metrics: Arc<RpcMetrics>,
6262
) {
6363
tracing::info!("starting keeper");
64-
let latest_safe_block = get_latest_safe_block(&chain_state).in_current_span().await;
65-
tracing::info!("latest safe block: {}", &latest_safe_block);
6664

6765
let contract = Arc::new(
6866
InstrumentedSignablePythContract::from_config(
@@ -78,6 +76,9 @@ pub async fn run_keeper_threads(
7876

7977
let fulfilled_requests_cache = Arc::new(RwLock::new(HashSet::<u64>::new()));
8078

79+
let latest_safe_block = get_latest_safe_block(contract.clone(), &chain_state).in_current_span().await;
80+
tracing::info!("latest safe block: {}", &latest_safe_block);
81+
8182
// Spawn a thread to handle the events from last BACKLOG_RANGE blocks.
8283
let gas_limit: U256 = chain_eth_config.gas_limit.into();
8384
spawn(
@@ -101,6 +102,7 @@ pub async fn run_keeper_threads(
101102
// Spawn a thread to watch for new blocks and send the range of blocks for which events has not been handled to the `tx` channel.
102103
spawn(
103104
watch_blocks_wrapper(
105+
contract.clone(),
104106
chain_state.clone(),
105107
latest_safe_block,
106108
tx,

0 commit comments

Comments
 (0)