Skip to content
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

refactor(torii): clean up ercs branch and few fixes #2491

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(torii): load token_id into cache on startup
  • Loading branch information
lambda-0x committed Oct 2, 2024
commit a7e55cea35040d0625a3265ed6fabf4b6b52b995
11 changes: 1 addition & 10 deletions bin/torii/torii.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@
# { type = "WORLD", address = "<WORLD_CONTRACT_ADDRESS>" },
# { type = "ERC20", address = "<ERC20_CONTRACT_ADDRESS>" },
# { type = "ERC721", address = "<ERC721_CONTRACT_ADDRESS>" },
# ]

contracts = [
# num-sepolia
{ type = "WORLD", address = "0x50c46c6f69ea9f12b24fcdd1569a584ae8893db0c70ea7884b605384da78ebc" },
# LORDS
{ type = "ERC20", address = "0x044e6bcc627e6201ce09f781d1aae44ea4c21c2fdef299e34fce55bef2d02210" },
# LSVR
{ type = "ERC721", address = "0x0046506cccd3ffb110bc04445d25d94f1d26d645a30e6ac8a17c24e084bed08b" },
]
# ]
19 changes: 16 additions & 3 deletions crates/torii/core/src/sql/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::collections::{HashMap, HashSet};

use dojo_types::schema::Ty;
use dojo_world::contracts::abi::model::Layout;
use sqlx::SqlitePool;
use sqlx::{Pool, Sqlite, SqlitePool};
use starknet_crypto::Felt;
use tokio::sync::RwLock;

use crate::error::{Error, ParseError, QueryError};
use crate::model::{parse_sql_model_members, SqlModelMember};
use crate::sql::utils::I256;
use crate::types::ContractType;

#[derive(Debug, Clone)]
pub struct Model {
Expand Down Expand Up @@ -126,12 +127,24 @@ impl ModelCache {

#[derive(Debug)]
pub struct LocalCache {
pub erc_cache: HashMap<String, I256>,
pub erc_cache: HashMap<(ContractType, String), I256>,
pub token_id_registry: HashSet<String>,
}

impl LocalCache {
pub fn new() -> Self {
pub async fn new(pool: Pool<Sqlite>) -> Self {
// read existing token_id's from balances table and cache them
let token_id_registry: Vec<(String,)> = sqlx::query_as("SELECT token_id FROM balances")
.fetch_all(&pool)
.await
.expect("Should be able to read token_id's from blances table");

let token_id_registry = token_id_registry.into_iter().map(|token_id| token_id.0).collect();

Self { erc_cache: HashMap::new(), token_id_registry }
}

pub fn empty() -> Self {
Self { erc_cache: HashMap::new(), token_id_registry: HashSet::new() }
}

Expand Down
45 changes: 30 additions & 15 deletions crates/torii/core/src/sql/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::query_queue::{Argument, QueryType};
use super::utils::{sql_string_to_u256, u256_to_sql_string, I256};
use super::{Sql, FELT_DELIMITER};
use crate::sql::utils::{felt_and_u256_to_sql_string, felt_to_sql_string, felts_to_sql_string};
use crate::types::ContractType;
use crate::utils::utc_dt_string_from_timestamp;

impl Sql {
Expand Down Expand Up @@ -40,18 +41,21 @@ impl Sql {
block_timestamp,
);

self.query_queue.execute_all().await?;

if from_address != Felt::ZERO {
// from_address/contract_address/
let from_balance_id = felts_to_sql_string(&[from_address, contract_address]);
let from_balance = self.local_cache.erc_cache.entry(from_balance_id).or_default();
let from_balance = self
.local_cache
.erc_cache
.entry((ContractType::ERC20, from_balance_id))
.or_default();
*from_balance -= I256::from(amount);
}

if to_address != Felt::ZERO {
let to_balance_id = felts_to_sql_string(&[to_address, contract_address]);
let to_balance = self.local_cache.erc_cache.entry(to_balance_id).or_default();
let to_balance =
self.local_cache.erc_cache.entry((ContractType::ERC20, to_balance_id)).or_default();
*to_balance += I256::from(amount);
}

Expand Down Expand Up @@ -89,20 +93,26 @@ impl Sql {
block_timestamp,
);

self.query_queue.execute_all().await?;

// from_address/contract_address:id
if from_address != Felt::ZERO {
let from_balance_id =
format!("{}{FELT_DELIMITER}{}", felt_to_sql_string(&from_address), &token_id);
let from_balance = self.local_cache.erc_cache.entry(from_balance_id).or_default();
let from_balance = self
.local_cache
.erc_cache
.entry((ContractType::ERC721, from_balance_id))
.or_default();
*from_balance -= I256::from(1u8);
}

if to_address != Felt::ZERO {
let to_balance_id =
format!("{}{FELT_DELIMITER}{}", felt_to_sql_string(&to_address), &token_id);
let to_balance = self.local_cache.erc_cache.entry(to_balance_id).or_default();
let to_balance = self
.local_cache
.erc_cache
.entry((ContractType::ERC721, to_balance_id))
.or_default();
*to_balance += I256::from(1u8);
}

Expand Down Expand Up @@ -319,11 +329,16 @@ impl Sql {
}

pub async fn apply_cache_diff(&mut self) -> Result<()> {
for (id_str, balance) in self.local_cache.erc_cache.iter() {
// execute the query queue to apply any queries registering token metadata
self.query_queue.execute_all().await?;

for ((contract_type, id_str), balance) in self.local_cache.erc_cache.iter() {
let id = id_str.split(FELT_DELIMITER).collect::<Vec<&str>>();
match id.len() {
// account_address/contract_address:id => ERC721
2 => {
match contract_type {
ContractType::WORLD => unreachable!(),
ContractType::ERC721 => {
// account_address/contract_address:id => ERC721
assert!(id.len() == 2);
let account_address = id[0];
let token_id = id[1];
let mid = token_id.split(":").collect::<Vec<&str>>();
Expand All @@ -338,8 +353,9 @@ impl Sql {
)
.await?;
}
// account_address/contract_address/ => ERC20
3 => {
ContractType::ERC20 => {
// account_address/contract_address/ => ERC20
assert!(id.len() == 3);
let account_address = id[0];
let contract_address = id[1];
let token_id = id[1];
Expand All @@ -353,7 +369,6 @@ impl Sql {
)
.await?;
}
_ => unreachable!(),
}
}

Expand Down
12 changes: 5 additions & 7 deletions crates/torii/core/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Clone for Sql {
pool: self.pool.clone(),
query_queue: QueryQueue::new(self.pool.clone()),
model_cache: self.model_cache.clone(),
local_cache: LocalCache::new(),
local_cache: LocalCache::empty(),
}
}
}
Expand Down Expand Up @@ -88,12 +88,14 @@ impl Sql {

query_queue.execute_all().await?;

let local_cache = LocalCache::new(pool.clone()).await;

Ok(Self {
pool: pool.clone(),
world_address,
query_queue,
model_cache: Arc::new(ModelCache::new(pool)),
local_cache: LocalCache::new(),
local_cache,
})
}

Expand Down Expand Up @@ -804,11 +806,7 @@ impl Sql {
Ty::Enum(e) => {
if e.options.iter().all(
|o| {
if let Ty::Tuple(t) = &o.ty {
t.is_empty()
} else {
false
}
if let Ty::Tuple(t) = &o.ty { t.is_empty() } else { false }
},
) {
return;
Expand Down
3 changes: 2 additions & 1 deletion scripts/verify_db_balances/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

if db_balance != starknet_balance {
error!(
"Mismatch for account {} and contract {}: DB balance = {}, Starknet balance = {}",
"Mismatch for account {} and contract {}: DB balance = {}, Starknet balance = \
{}",
account_address, contract_address, db_balance, starknet_balance
);
} else {
Expand Down