Skip to content

Commit

Permalink
Add optimistic option in neardata
Browse files Browse the repository at this point in the history
  • Loading branch information
Sliman4 committed Jul 12, 2024
1 parent c0c69e2 commit d523f1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/indexer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ async fn neardata_server_provider() {
assert_eq!(indexer.blocks_processed, 4)
}

#[tokio::test]
async fn neardata_optimistic_provider() {
const RANGE: Range<BlockHeight> =
MAINNET_GENESIS_BLOCK_HEIGHT..(MAINNET_GENESIS_BLOCK_HEIGHT + 10);

#[derive(Default)]
struct TestIndexer {
blocks_processed: BlockHeightDelta,
}

#[async_trait]
impl Indexer for TestIndexer {
type Error = String;

async fn process_block(&mut self, block: &StreamerMessage) -> Result<(), Self::Error> {
assert!(RANGE.contains(&block.block.header.height));
self.blocks_processed += 1;
Ok(())
}
}

let mut indexer = TestIndexer::default();

run_indexer(
&mut indexer,
NeardataServerProvider::mainnet().optimistic(),
IndexerOptions {
range: BlockIterator::iterator(RANGE),
preprocess_transactions: None,
..Default::default()
},
)
.await
.unwrap();

assert_eq!(indexer.blocks_processed, 4) // I guess old optimistic blocks are purged, so idk how to test
}

#[cfg(feature = "lake")]
#[tokio::test]
async fn lake_provider() {
Expand Down
19 changes: 17 additions & 2 deletions src/neardata_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::message_provider::MessageProvider;
pub struct NeardataServerProvider {
base_url: String,
client: reqwest::Client,
optimistic: bool,
}

impl NeardataServerProvider {
Expand All @@ -35,7 +36,11 @@ impl NeardataServerProvider {
}

pub fn with_base_url_and_client(base_url: String, client: reqwest::Client) -> Self {
Self { base_url, client }
Self {
base_url,
client,
optimistic: false,
}
}

pub fn mainnet() -> Self {
Expand All @@ -45,6 +50,11 @@ impl NeardataServerProvider {
pub fn testnet() -> Self {
Self::with_base_url("https://testnet.neardata.xyz".to_string())
}

pub fn optimistic(mut self) -> Self {
self.optimistic = true;
self
}
}

#[async_trait]
Expand All @@ -55,9 +65,14 @@ impl MessageProvider for NeardataServerProvider {
&self,
block_height: BlockHeight,
) -> Result<Option<StreamerMessage>, Self::Error> {
let finality = if self.optimistic {
"block_opt"
} else {
"block"
};
let response = self
.client
.get(&format!("{}/v0/block/{block_height}", self.base_url))
.get(&format!("{}/v0/{finality}/{block_height}", self.base_url))
.send()
.await?;
let text = response.text().await?;
Expand Down

0 comments on commit d523f1f

Please sign in to comment.