Skip to content

Commit

Permalink
Merge branch 'main' into collision
Browse files Browse the repository at this point in the history
  • Loading branch information
gianalarcon committed Oct 26, 2023
2 parents c8d14db + 1018840 commit 3e85b2a
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 230 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/dojo-core/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"
2 changes: 2 additions & 0 deletions crates/dojo-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ cairo-lang-syntax.workspace = true
cairo-lang-utils.workspace = true
camino.workspace = true
convert_case.workspace = true
directories = "5"
dojo-types = { path = "../dojo-types" }
dojo-world = { path = "../dojo-world", features = [ "manifest" ] }
indoc.workspace = true
itertools.workspace = true
lazy_static.workspace = true
once_cell.workspace = true
salsa.workspace = true
scarb.workspace = true
Expand Down
52 changes: 31 additions & 21 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fs;
use std::fs::File;
use std::io::Write;
use std::sync::Arc;

use anyhow::Result;
Expand All @@ -12,9 +15,11 @@ use cairo_lang_syntax::attribute::structured::{
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_syntax::node::{ast, Terminal};
use camino::Utf8Path;
use camino::{Utf8Path, Utf8PathBuf};
use directories::ProjectDirs;
use dojo_types::system::Dependency;
use dojo_world::manifest::Member;
use lazy_static::lazy_static;
use scarb::compiler::plugin::builtin::BuiltinStarkNetPlugin;
use scarb::compiler::plugin::{CairoPlugin, CairoPluginInstance};
use scarb::core::{DependencyVersionReq, ManifestDependency, PackageId, PackageName, SourceId};
Expand Down Expand Up @@ -69,6 +74,27 @@ pub const PACKAGE_NAME: &str = "dojo_plugin";
#[derive(Debug, Default)]
pub struct BuiltinDojoPlugin;

lazy_static! {
static ref MANIFEST_VERSION: Version = Version::parse(env!("CARGO_PKG_VERSION")).expect("Manifest version not defined");
static ref MANIFEST_PATH: Utf8PathBuf = {
let pd = ProjectDirs::from("com", "dojoengine", "sozo")
.expect("no valid home directory path could be retrieved from the operating system");

let content = include_str!("../Scarb.toml");
let path = pd.cache_dir().join(MANIFEST_VERSION.to_string()).join("Scarb.toml");

// Create the directory if it doesn't exist
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).expect("Failed to create directory");
}

let mut file = File::create(&path).expect("unable to create file");
write!(file, "{}", content).expect("unable to write to file");

Utf8Path::from_path(&path).expect("invalid UTF-8 path").to_path_buf()
};
}

impl BuiltinDojoPlugin {
fn handle_mod(&self, db: &dyn SyntaxGroup, module_ast: ast::ItemModule) -> PluginResult {
if module_ast.has_attr(db, DOJO_CONTRACT_ATTR) {
Expand All @@ -79,37 +105,21 @@ impl BuiltinDojoPlugin {
}

pub fn manifest_dependency() -> ManifestDependency {
let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
let version_req = DependencyVersionReq::exact(&version);
let version_req = DependencyVersionReq::exact(&MANIFEST_VERSION);
ManifestDependency::builder()
.name(PackageName::new(PACKAGE_NAME))
.source_id(
SourceId::for_path(
&Utf8Path::new(env!("CARGO_MANIFEST_DIR"))
.join("Scarb.toml")
.canonicalize_utf8()
.unwrap(),
)
.unwrap(),
)
.source_id(SourceId::for_path(MANIFEST_PATH.as_path()).unwrap())
.version_req(version_req)
.build()
}
}

impl CairoPlugin for BuiltinDojoPlugin {
fn id(&self) -> PackageId {
let version = env!("CARGO_PKG_VERSION");
PackageId::new(
PackageName::new(PACKAGE_NAME),
Version::parse(version).unwrap(),
SourceId::for_path(
&Utf8Path::new(env!("CARGO_MANIFEST_DIR"))
.join("Scarb.toml")
.canonicalize_utf8()
.unwrap(),
)
.unwrap(),
MANIFEST_VERSION.to_owned(),
SourceId::for_path(MANIFEST_PATH.as_path()).unwrap(),
)
}

Expand Down
20 changes: 20 additions & 0 deletions crates/dojo-primitives/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.1"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.1"

[[package]]
name = "dojo_primitives"
version = "0.3.1"
dependencies = [
"dojo",
]
69 changes: 46 additions & 23 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use anyhow::Result;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{
BlockId, BlockWithTxs, Event, InvokeTransaction, InvokeTransactionReceipt,
BlockId, BlockWithTxs, Event, InvokeTransaction, InvokeTransactionReceipt, InvokeTransactionV1,
MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, Transaction, TransactionReceipt,
};
use starknet::core::utils::get_selector_from_name;
Expand Down Expand Up @@ -147,36 +147,48 @@ where
_ => continue,
};

let receipt = match self
let receipt = self
.provider
.get_transaction_receipt(invoke_transaction.transaction_hash)
.await
{
Ok(receipt) => receipt,
.ok()
.and_then(|receipt| match receipt {
MaybePendingTransactionReceipt::Receipt(TransactionReceipt::Invoke(
receipt,
)) => Some(receipt),
_ => None,
});

let invoke_receipt = match receipt {
Some(receipt) => receipt,
_ => continue,
};

let receipt = match receipt {
MaybePendingTransactionReceipt::Receipt(receipt) => receipt,
_ => continue,
};

if let TransactionReceipt::Invoke(invoke_receipt) = receipt.clone() {
for (event_idx, event) in invoke_receipt.events.iter().enumerate() {
if event.from_address != self.world.address() {
continue;
}
let mut world_event = false;
for (event_idx, event) in invoke_receipt.events.iter().enumerate() {
if event.from_address != self.world.address() {
continue;
}

let event_id = format!(
"0x{:064x}:0x{:04x}:0x{:04x}",
block.block_number, tx_idx, event_idx
);
world_event = true;
let event_id =
format!("0x{:064x}:0x{:04x}:0x{:04x}", block.block_number, tx_idx, event_idx);

Self::process_event(self, &block, &invoke_receipt, &event_id, event).await?;
}
Self::process_event(self, &block, &invoke_receipt, &event_id, event).await?;
}

Self::process_transaction(self, &block, &receipt).await?;
if world_event {
let transaction_id = format!("0x{:064x}:0x{:04x}", block.block_number, tx_idx);

Self::process_transaction(
self,
&block,
&invoke_receipt,
&transaction_id,
invoke_transaction,
)
.await?;
}
}

info!("processed block: {}", block.block_number);
Expand All @@ -194,10 +206,21 @@ where
async fn process_transaction(
&mut self,
block: &BlockWithTxs,
receipt: &TransactionReceipt,
invoke_receipt: &InvokeTransactionReceipt,
transaction_id: &str,
transaction: &InvokeTransactionV1,
) -> Result<()> {
for processor in &self.processors.transaction {
processor.process(self.db, self.provider.as_ref(), block, receipt).await?
processor
.process(
self.db,
self.provider.as_ref(),
block,
invoke_receipt,
transaction,
transaction_id,
)
.await?
}

Ok(())
Expand Down
7 changes: 5 additions & 2 deletions crates/torii/core/src/processors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use anyhow::{Error, Result};
use async_trait::async_trait;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{BlockWithTxs, Event, InvokeTransactionReceipt, TransactionReceipt};
use starknet::core::types::{BlockWithTxs, Event, InvokeTransactionReceipt, InvokeTransactionV1};
use starknet::providers::Provider;

use crate::sql::Sql;

pub mod metadata_update;
pub mod register_model;
pub mod store_set_record;
pub mod store_transaction;

#[async_trait]
pub trait EventProcessor<P>
Expand Down Expand Up @@ -42,6 +43,8 @@ pub trait TransactionProcessor<P: Provider + Sync> {
db: &mut Sql,
provider: &P,
block: &BlockWithTxs,
transaction_receipt: &TransactionReceipt,
invoke_receipt: &InvokeTransactionReceipt,
transaction: &InvokeTransactionV1,
transaction_id: &str,
) -> Result<(), Error>;
}
56 changes: 0 additions & 56 deletions crates/torii/core/src/processors/store_system_call.rs

This file was deleted.

27 changes: 27 additions & 0 deletions crates/torii/core/src/processors/store_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use starknet::core::types::{BlockWithTxs, InvokeTransactionReceipt, InvokeTransactionV1};
use starknet::providers::Provider;

use super::TransactionProcessor;
use crate::sql::Sql;

#[derive(Default)]
pub struct StoreTransactionProcessor;

#[async_trait]
impl<P: Provider + Sync> TransactionProcessor<P> for StoreTransactionProcessor {
async fn process(
&self,
db: &mut Sql,
_provider: &P,
_block: &BlockWithTxs,
_receipt: &InvokeTransactionReceipt,
transaction: &InvokeTransactionV1,
transaction_id: &str,
) -> Result<(), Error> {
db.store_transaction(transaction, transaction_id);

Ok(())
}
}
Loading

0 comments on commit 3e85b2a

Please sign in to comment.