-
Notifications
You must be signed in to change notification settings - Fork 32
added a basic test to run the oracle #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
502d17b
added a basic test to run the oracle
loocapro 9c43ac5
moving on
loocapro 221ad86
remote reth
loocapro 0415ac7
oracle can peer
loocapro 6398259
mock data feed
loocapro 295c3aa
feat: e2e tests
loocapro 6f63a82
Update Cargo.toml
loocapro ac08cd3
Update Cargo.toml
loocapro 7f281ee
chore: tokio tungstenite as workspace dep and 2 crates under misc crates
loocapro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| use clap::Parser; | ||
| use cli_ext::OracleExt; | ||
| use exex::ExEx; | ||
| use futures::FutureExt; | ||
| use futures::{FutureExt, Stream}; | ||
| use network::{proto::OracleProtoHandler, OracleNetwork}; | ||
| use offchain_data::DataFeederStream; | ||
| use offchain_data::{DataFeederError, DataFeederStream, DataFeeds}; | ||
| use oracle::Oracle; | ||
| use reth::chainspec::EthereumChainSpecParser; | ||
| use reth_exex::ExExContext; | ||
| use reth_network::{protocol::IntoRlpxSubProtocol, NetworkProtocols}; | ||
| use reth_node_api::FullNodeComponents; | ||
| use reth_node_ethereum::EthereumNode; | ||
|
|
||
| mod cli_ext; | ||
|
|
@@ -17,6 +19,35 @@ mod oracle; | |
|
|
||
| const ORACLE_EXEX_ID: &str = "exex-oracle"; | ||
|
|
||
| /// Helper function to start the oracle stack. | ||
| async fn start<Node: FullNodeComponents, D>( | ||
| ctx: ExExContext<Node>, | ||
| tcp_port: u16, | ||
| udp_port: u16, | ||
| data_feeder: D, | ||
| ) -> eyre::Result<(Oracle<Node, D>, Node::Network)> | ||
| where | ||
| Node::Network: NetworkProtocols, | ||
| D: Stream<Item = Result<DataFeeds, DataFeederError>> + Send + 'static, | ||
| { | ||
| // Define the oracle subprotocol | ||
| let (subproto, proto_events, to_peers) = OracleProtoHandler::new(); | ||
| // Add it to the network as a subprotocol | ||
| let net = ctx.network().clone(); | ||
| net.add_rlpx_sub_protocol(subproto.into_rlpx_sub_protocol()); | ||
|
|
||
| // The instance of the execution extension that will handle chain events | ||
| let exex = ExEx::new(ctx); | ||
|
|
||
| // The instance of the oracle network that will handle discovery and gossiping of data | ||
| let network = OracleNetwork::new(proto_events, tcp_port, udp_port).await?; | ||
|
|
||
| // The oracle instance that will orchestrate the network, the execution extensions, | ||
| // the off-chain data stream, and the gossiping | ||
| let oracle = Oracle::new(exex, network, data_feeder, to_peers); | ||
| Ok((oracle, net.clone())) | ||
| } | ||
|
|
||
| fn main() -> eyre::Result<()> { | ||
| reth::cli::Cli::<EthereumChainSpecParser, OracleExt>::parse().run(|builder, args| async move { | ||
| let tcp_port = args.tcp_port; | ||
|
|
@@ -36,26 +67,8 @@ fn main() -> eyre::Result<()> { | |
| // Source: https://github.com/vados-cosmonic/wasmCloud/commit/440e8c377f6b02f45eacb02692e4d2fabd53a0ec | ||
| tokio::task::spawn_blocking(move || { | ||
| tokio::runtime::Handle::current().block_on(async move { | ||
| // define the oracle subprotocol | ||
| let (subproto, proto_events, to_peers) = OracleProtoHandler::new(); | ||
| // add it to the network as a subprotocol | ||
| ctx.network().add_rlpx_sub_protocol(subproto.into_rlpx_sub_protocol()); | ||
|
|
||
| // the instance of the execution extension that will handle chain events | ||
| let exex = ExEx::new(ctx); | ||
|
|
||
| // the instance of the oracle network that will handle discovery and | ||
| // gossiping of data | ||
| let network = OracleNetwork::new(proto_events, tcp_port, udp_port).await?; | ||
| // the off-chain data feed stream | ||
| let data_feed = DataFeederStream::new(args.binance_symbols).await?; | ||
|
|
||
| // the oracle instance that will orchestrate the network, the execution | ||
| // extensions, the offchain data stream and the | ||
| // gossiping the oracle will always sign and | ||
| // broadcast data via the channel until a peer is | ||
| // subcribed to it | ||
| let oracle = Oracle::new(exex, network, data_feed, to_peers); | ||
| let (oracle, _net) = start(ctx, tcp_port, udp_port, data_feed).await?; | ||
| Ok(oracle) | ||
| }) | ||
| }) | ||
|
|
@@ -67,3 +80,108 @@ fn main() -> eyre::Result<()> { | |
| handle.wait_for_node_exit().await | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::{offchain_data::binance::ticker::Ticker, start}; | ||
| use futures::{Stream, StreamExt}; | ||
| use reth_exex_test_utils::test_exex_context; | ||
| use reth_network::{NetworkEvent, NetworkEventListenerProvider, NetworkInfo, Peers}; | ||
| use reth_network_api::PeerId; | ||
| use reth_tokio_util::EventStream; | ||
| use reth_tracing::tracing::info; | ||
| use tokio_stream::wrappers::BroadcastStream; | ||
|
|
||
| async fn wait_for_session(mut events: EventStream<NetworkEvent>) -> PeerId { | ||
| while let Some(event) = events.next().await { | ||
| if let NetworkEvent::SessionEstablished { peer_id, .. } = event { | ||
| info!("Session established with {}", peer_id); | ||
| return peer_id; | ||
| } | ||
| info!("Unexpected event: {:?}", event); | ||
| } | ||
|
|
||
| unreachable!() | ||
| } | ||
|
|
||
| use crate::offchain_data::{DataFeederError, DataFeeds}; | ||
| use futures::stream::{self}; | ||
| use std::pin::Pin; | ||
|
|
||
| fn mock_stream() -> Pin<Box<dyn Stream<Item = Result<DataFeeds, DataFeederError>> + Send>> { | ||
| let ticker = Ticker { | ||
| event_type: "24hrTicker".to_string(), | ||
| event_time: 1698323450000, | ||
| symbol: "BTCUSDT".to_string(), | ||
| price_change: "100.00".to_string(), | ||
| price_change_percent: "2.5".to_string(), | ||
| weighted_avg_price: "40200.00".to_string(), | ||
| prev_close_price: "40000.00".to_string(), | ||
| last_price: "40100.00".to_string(), | ||
| last_quantity: "0.5".to_string(), | ||
| best_bid_price: "40095.00".to_string(), | ||
| best_bid_quantity: "1.0".to_string(), | ||
| best_ask_price: "40105.00".to_string(), | ||
| best_ask_quantity: "1.0".to_string(), | ||
| open_price: "39900.00".to_string(), | ||
| high_price: "40500.00".to_string(), | ||
| low_price: "39800.00".to_string(), | ||
| volume: "1500".to_string(), | ||
| quote_volume: "60000000".to_string(), | ||
| open_time: 1698237050000, | ||
| close_time: 1698323450000, | ||
| first_trade_id: 1, | ||
| last_trade_id: 2000, | ||
| num_trades: 2000, | ||
| }; | ||
|
|
||
| // Wrap the Ticker in DataFeeds::Binance | ||
| let data_feed = DataFeeds::Binance(ticker); | ||
|
|
||
| // Create a stream that sends a single item and then ends, boxed and pinned | ||
| Box::pin(stream::once(async { Ok(data_feed) })) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn e2e_oracles() { | ||
| reth_tracing::init_test_tracing(); | ||
|
|
||
| // spawn first instance | ||
| let (ctx_1, _handle) = test_exex_context().await.unwrap(); | ||
| let data_feed1 = mock_stream(); | ||
| let (oracle_1, network_1) = start(ctx_1, 30303, 30304, data_feed1).await.unwrap(); | ||
| let mut broadcast_stream_1 = BroadcastStream::new(oracle_1.signed_ticks().subscribe()); | ||
| let signer_1 = oracle_1.signer().address(); | ||
| tokio::spawn(oracle_1); | ||
| let net_1_events = network_1.event_listener(); | ||
|
|
||
| // spawn second instance | ||
| let (ctx_2, _handle) = test_exex_context().await.unwrap(); | ||
| let data_feed2 = mock_stream(); | ||
| let (oracle_2, network_2) = start(ctx_2, 30305, 30306, data_feed2).await.unwrap(); | ||
| let mut broadcast_stream_2 = BroadcastStream::new(oracle_2.signed_ticks().subscribe()); | ||
| let signer_2 = oracle_2.signer().address(); | ||
| tokio::spawn(oracle_2); | ||
| let net_2_events = network_2.event_listener(); | ||
|
|
||
| // expect peers connected | ||
| let (peer_2, addr_2) = (network_2.peer_id(), network_2.local_addr()); | ||
| network_1.add_peer(*peer_2, addr_2); | ||
| let expected_peer_2 = wait_for_session(net_1_events).await; | ||
| assert_eq!(expected_peer_2, *peer_2); | ||
|
|
||
| let (peer_1, addr_1) = (network_1.peer_id(), network_1.local_addr()); | ||
| network_2.add_peer(*peer_1, addr_1); | ||
| let expected_peer_1 = wait_for_session(net_2_events).await; | ||
| assert_eq!(expected_peer_1, *peer_1); | ||
|
|
||
| // expect signed data | ||
| let signed_ticker_1 = broadcast_stream_1.next().await.unwrap().unwrap(); | ||
| assert_eq!(signed_ticker_1.ticker.symbol, "BTCUSDT"); | ||
| assert_eq!(signed_ticker_1.signer, signer_1); | ||
|
|
||
| let signed_ticker_2 = broadcast_stream_2.next().await.unwrap().unwrap(); | ||
| assert_eq!(signed_ticker_2.ticker.symbol, "BTCUSDT"); | ||
| assert_eq!(signed_ticker_2.signer, signer_2); | ||
|
Comment on lines
+178
to
+185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so far this tests that both instances received an item from the stream, but it doesn't test if they actually exchange messages as peers after connecting. Can we do that as well? |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.