Skip to content

Commit c5d6153

Browse files
committed
added a basic test to run the oracle
1 parent a0077f1 commit c5d6153

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

Cargo.lock

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

oracle/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ futures.workspace = true
3636
tokio-stream.workspace = true
3737
tokio-tungstenite = "0.23"
3838
tokio.workspace = true
39+
rand = "0.8.5"
40+
41+
thiserror = "1"
3942

4043
# misc
4144
clap = "4"
4245
eyre.workspace = true
4346
serde.workspace = true
4447
serde_json.workspace = true
45-
thiserror = "1"
4648
uuid = "1.10.0"
4749

4850
[dev-dependencies]

oracle/src/main.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use network::{proto::OracleProtoHandler, OracleNetwork};
66
use offchain_data::DataFeederStream;
77
use oracle::Oracle;
88
use reth::chainspec::EthereumChainSpecParser;
9+
use reth_exex::ExExContext;
910
use reth_network::{protocol::IntoRlpxSubProtocol, NetworkProtocols};
11+
use reth_node_api::FullNodeComponents;
1012
use reth_node_ethereum::EthereumNode;
1113

1214
mod cli_ext;
@@ -17,6 +19,35 @@ mod oracle;
1719

1820
const ORACLE_EXEX_ID: &str = "exex-oracle";
1921

22+
async fn start<Node: FullNodeComponents>(
23+
ctx: ExExContext<Node>,
24+
tcp_port: u16,
25+
udp_port: u16,
26+
binance_symbols: Vec<String>,
27+
) -> eyre::Result<(Oracle<Node>, Node::Network)>
28+
where
29+
Node::Network: NetworkProtocols,
30+
{
31+
// Define the oracle subprotocol
32+
let (subproto, proto_events, to_peers) = OracleProtoHandler::new();
33+
// Add it to the network as a subprotocol
34+
let net = ctx.network().clone();
35+
net.add_rlpx_sub_protocol(subproto.into_rlpx_sub_protocol());
36+
37+
// The instance of the execution extension that will handle chain events
38+
let exex = ExEx::new(ctx);
39+
40+
// The instance of the oracle network that will handle discovery and gossiping of data
41+
let network = OracleNetwork::new(proto_events, tcp_port, udp_port).await?;
42+
// The off-chain data feed stream
43+
let data_feed = DataFeederStream::new(binance_symbols).await?;
44+
45+
// The oracle instance that will orchestrate the network, the execution extensions,
46+
// the off-chain data stream, and the gossiping
47+
let oracle = Oracle::new(exex, network, data_feed, to_peers);
48+
Ok((oracle, net.clone()))
49+
}
50+
2051
fn main() -> eyre::Result<()> {
2152
reth::cli::Cli::<EthereumChainSpecParser, OracleExt>::parse().run(|builder, args| async move {
2253
let tcp_port = args.tcp_port;
@@ -67,3 +98,51 @@ fn main() -> eyre::Result<()> {
6798
handle.wait_for_node_exit().await
6899
})
69100
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use crate::start;
105+
use reth_exex_test_utils::test_exex_context;
106+
107+
#[tokio::test]
108+
async fn test_oracle() {
109+
reth_tracing::init_test_tracing();
110+
let (ctx, _handle) = test_exex_context().await.unwrap();
111+
let (oracle, network_1) =
112+
start(ctx, 30306, 30307, vec!["btcusdc".to_string(), "ethusdc".to_string()])
113+
.await
114+
.unwrap();
115+
tokio::spawn(oracle);
116+
117+
// // spawn second
118+
// let (ctx, _handle) = test_exex_context().await.unwrap();
119+
// let (oracle, network_2) =
120+
// start(ctx, 30308, 30309, vec!["btcusdc".to_string(), "ethusdc".to_string()])
121+
// .await
122+
// .unwrap();
123+
// tokio::spawn(oracle);
124+
125+
// // make them connect
126+
// let (peer_1, addr_1) = (network_1.peer_id(), network_1.local_addr());
127+
// let (peer_2, addr_2) = (network_2.peer_id(), network_2.local_addr());
128+
// network_1.add_peer(*peer_1, addr_1);
129+
// network_2.add_peer(*peer_2, addr_2);
130+
131+
// let mut events_1 = network_1.event_listener();
132+
133+
// let expected_peer_2 = loop {
134+
// if let Some(ev) = events_1.next().await {
135+
// match ev {
136+
// NetworkEvent::SessionEstablished { peer_id, .. } => {
137+
// info!("Session established with peer: {:?}", peer_id);
138+
// break peer_id;
139+
// }
140+
// _ => continue,
141+
// }
142+
// } else {
143+
// unreachable!()
144+
// }
145+
// };
146+
// assert_eq!(expected_peer_2, *peer_2);
147+
}
148+
}

oracle/src/network/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Future for OracleNetwork {
6262
"Established connection, will start gossiping"
6363
);
6464
}
65-
None => return Poll::Ready(Ok(())),
65+
None => return Poll::Pending,
6666
}
6767
}
6868
}

0 commit comments

Comments
 (0)