Skip to content

Commit

Permalink
graph: mock chain store
Browse files Browse the repository at this point in the history
  • Loading branch information
incrypto32 authored and zorancv committed Dec 18, 2024
1 parent f432dd0 commit 5975fea
Showing 1 changed file with 116 additions and 4 deletions.
120 changes: 116 additions & 4 deletions graph/src/blockchain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ use crate::{
subgraph::InstanceDSTemplateInfo,
},
data::subgraph::UnifiedMappingApiVersion,
prelude::{BlockHash, DataSourceTemplateInfo},
prelude::{
transaction_receipt::LightTransactionReceipt, BlockHash, ChainStore,
DataSourceTemplateInfo, StoreError,
},
};
use anyhow::{Error, Result};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::Value;
use slog::Logger;
use std::{collections::HashSet, convert::TryFrom, sync::Arc};
use std::{
collections::{BTreeMap, HashMap, HashSet},
convert::TryFrom,
sync::Arc,
};
use web3::types::H256;

use super::{
block_stream::{self, BlockStream, FirehoseCursor},
client::ChainClient,
BlockIngestor, BlockTime, EmptyNodeCapabilities, HostFn, IngestorError, MappingTriggerTrait,
NoopDecoderHook, Trigger, TriggerFilterWrapper, TriggerWithHandler,
BlockIngestor, BlockTime, ChainIdentifier, EmptyNodeCapabilities, ExtendedBlockPtr, HostFn,
IngestorError, MappingTriggerTrait, NoopDecoderHook, Trigger, TriggerFilterWrapper,
TriggerWithHandler,
};

use super::{
Expand Down Expand Up @@ -431,3 +441,105 @@ impl Blockchain for MockBlockchain {
todo!()
}
}

// Mock implementation
#[derive(Default)]
pub struct MockChainStore {
pub blocks: BTreeMap<BlockNumber, Vec<ExtendedBlockPtr>>,
}

#[async_trait]
impl ChainStore for MockChainStore {
async fn block_ptrs_by_numbers(
self: Arc<Self>,
numbers: Vec<BlockNumber>,
) -> Result<BTreeMap<BlockNumber, Vec<ExtendedBlockPtr>>, Error> {
let mut result = BTreeMap::new();
for num in numbers {
if let Some(blocks) = self.blocks.get(&num) {
result.insert(num, blocks.clone());
}
}
Ok(result)
}

// Implement other required methods with minimal implementations
fn genesis_block_ptr(&self) -> Result<BlockPtr, Error> {
unimplemented!()
}
async fn upsert_block(&self, _block: Arc<dyn Block>) -> Result<(), Error> {
unimplemented!()
}
fn upsert_light_blocks(&self, _blocks: &[&dyn Block]) -> Result<(), Error> {
unimplemented!()
}
async fn attempt_chain_head_update(
self: Arc<Self>,
_ancestor_count: BlockNumber,
) -> Result<Option<H256>, Error> {
unimplemented!()
}
async fn chain_head_ptr(self: Arc<Self>) -> Result<Option<BlockPtr>, Error> {
unimplemented!()
}
fn chain_head_cursor(&self) -> Result<Option<String>, Error> {
unimplemented!()
}
async fn set_chain_head(
self: Arc<Self>,
_block: Arc<dyn Block>,
_cursor: String,
) -> Result<(), Error> {
unimplemented!()
}
async fn blocks(self: Arc<Self>, _hashes: Vec<BlockHash>) -> Result<Vec<Value>, Error> {
unimplemented!()
}
async fn ancestor_block(
self: Arc<Self>,
_block_ptr: BlockPtr,
_offset: BlockNumber,
_root: Option<BlockHash>,
) -> Result<Option<(Value, BlockPtr)>, Error> {
unimplemented!()
}
fn cleanup_cached_blocks(
&self,
_ancestor_count: BlockNumber,
) -> Result<Option<(BlockNumber, usize)>, Error> {
unimplemented!()
}
fn block_hashes_by_block_number(&self, _number: BlockNumber) -> Result<Vec<BlockHash>, Error> {
unimplemented!()
}
fn confirm_block_hash(&self, _number: BlockNumber, _hash: &BlockHash) -> Result<usize, Error> {
unimplemented!()
}
async fn block_number(
&self,
_hash: &BlockHash,
) -> Result<Option<(String, BlockNumber, Option<u64>, Option<BlockHash>)>, StoreError> {
unimplemented!()
}
async fn block_numbers(
&self,
_hashes: Vec<BlockHash>,
) -> Result<HashMap<BlockHash, BlockNumber>, StoreError> {
unimplemented!()
}
async fn transaction_receipts_in_block(
&self,
_block_ptr: &H256,
) -> Result<Vec<LightTransactionReceipt>, StoreError> {
unimplemented!()
}
async fn clear_call_cache(&self, _from: BlockNumber, _to: BlockNumber) -> Result<(), Error> {
unimplemented!()
}
fn chain_identifier(&self) -> Result<ChainIdentifier, Error> {
unimplemented!()
}
fn set_chain_identifier(&self, _ident: &ChainIdentifier) -> Result<(), Error> {
unimplemented!()
}
}

0 comments on commit 5975fea

Please sign in to comment.