Skip to content

Commit

Permalink
move start_block and world_address to engine config
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Jul 29, 2023
1 parent e3bdc69 commit c921f76
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/torii/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ struct Args {
#[arg(short, long)]
manifest: Option<Utf8PathBuf>,
/// Specify a block to start indexing from, ignored if stored head exists
#[arg(short, long)]
start_block: Option<u64>,
#[arg(short, long, default_value = "0")]
start_block: u64,
}

#[tokio::main]
Expand Down
27 changes: 15 additions & 12 deletions crates/torii/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ impl<S: State, T: JsonRpcTransport + Sync + Send> Default for Processors<S, T> {
#[derive(Debug)]
pub struct EngineConfig {
pub block_time: Duration,
pub world_address: FieldElement,
pub start_block: u64,
}

impl Default for EngineConfig {
fn default() -> Self {
Self { block_time: Duration::from_secs(1) }
Self {
block_time: Duration::from_secs(1),
world_address: FieldElement::ZERO,
start_block: 0,
}
}
}

pub struct Engine<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> {
storage: &'a S,
provider: &'a JsonRpcClient<T>,
processors: Processors<S, T>,
world_address: FieldElement,
start_block: Option<u64>,
config: EngineConfig,
}

Expand All @@ -53,23 +57,22 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S,
storage: &'a S,
provider: &'a JsonRpcClient<T>,
processors: Processors<S, T>,
world_address: FieldElement,
start_block: Option<u64>,
config: EngineConfig,
) -> Self {
Self { storage, provider, processors, world_address, start_block, config }
Self { storage, provider, processors, config }
}

pub async fn start(&self) -> Result<(), Box<dyn Error>> {
let storage_head = self.storage.head().await?;

let mut current_block_number = match (storage_head, self.start_block) {
(0, Some(start_block)) => start_block,
(_, Some(_)) => {
warn!("start block ignored, stored head exists and will be used instead");
let mut current_block_number = match storage_head {
0 => self.config.start_block,
_ => {
if self.config.start_block != 0 {
warn!("start block ignored, stored head exists and will be used instead");
}
storage_head
}
(_, None) => storage_head,
};

loop {
Expand Down Expand Up @@ -158,7 +161,7 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Engine<'a, S,

if let TransactionReceipt::Invoke(invoke_receipt) = receipt.clone() {
for event in &invoke_receipt.events {
if event.from_address != self.world_address {
if event.from_address != self.config.world_address {
info!("event not from world address, skipping");
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions crates/torii/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ impl<'a, S: State + Executable, T: JsonRpcTransport + Sync + Send> Indexer<'a, S
processors: Processors<S, T>,
manifest: Manifest,
world_address: FieldElement,
start_block: Option<u64>,
start_block: u64,
) -> Self {
let engine = Engine::new(
storage,
provider,
processors,
world_address,
start_block,
EngineConfig::default(),
EngineConfig { world_address, start_block, ..Default::default() },
);
Self { storage, provider, engine, manifest }
}
Expand Down

0 comments on commit c921f76

Please sign in to comment.