-
Notifications
You must be signed in to change notification settings - Fork 189
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
fix: use sozo manifest when possible to avoid recomputing diff #2649
Merged
+610
−171
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use anyhow::{bail, Result}; | ||
use dojo_world::diff::WorldDiff; | ||
use dojo_world::ResourceType; | ||
use dojo_world::contracts::contract_info::ContractInfo; | ||
use slot::account_sdk::account::session::hash::{Policy, ProvedPolicy}; | ||
use slot::account_sdk::account::session::merkle::MerkleTree; | ||
use slot::account_sdk::account::session::SessionAccount; | ||
use slot::session::{FullSessionInfo, PolicyMethod}; | ||
use starknet::core::types::contract::{AbiEntry, StateMutability}; | ||
use starknet::core::types::Felt; | ||
use starknet::core::utils::get_selector_from_name; | ||
use starknet::macros::felt; | ||
|
@@ -35,16 +34,12 @@ | |
/// * Starknet mainnet | ||
/// * Starknet sepolia | ||
/// * Slot hosted networks | ||
#[tracing::instrument( | ||
name = "create_controller", | ||
skip(rpc_url, provider, world_address, world_diff) | ||
)] | ||
#[tracing::instrument(name = "create_controller", skip(rpc_url, provider, contracts))] | ||
pub async fn create_controller<P>( | ||
// Ideally we can get the url from the provider so we dont have to pass an extra url param here | ||
rpc_url: Url, | ||
provider: P, | ||
world_address: Felt, | ||
world_diff: &WorldDiff, | ||
contracts: &HashMap<String, ContractInfo>, | ||
Comment on lines
+37
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle empty Sensei, please consider adding a check for an empty Suggested addition: if contracts.is_empty() {
bail!("Contracts map is empty. Cannot collect policies without contracts.");
} |
||
) -> Result<ControllerSessionAccount<P>> | ||
where | ||
P: Provider, | ||
|
@@ -62,7 +57,7 @@ | |
bail!("No Controller is associated with this account."); | ||
}; | ||
|
||
let policies = collect_policies(world_address, contract_address, world_diff)?; | ||
let policies = collect_policies(contract_address, contracts)?; | ||
|
||
// Check if the session exists, if not create a new one | ||
let session_details = match slot::session::get(chain_id)? { | ||
|
@@ -132,37 +127,28 @@ | |
/// This function collect all the contracts' methods in the current project according to the | ||
/// project's base manifest ( `/manifests/<profile>/base` ) and convert them into policies. | ||
fn collect_policies( | ||
world_address: Felt, | ||
user_address: Felt, | ||
world_diff: &WorldDiff, | ||
contracts: &HashMap<String, ContractInfo>, | ||
) -> Result<Vec<PolicyMethod>> { | ||
let policies = collect_policies_from_local_world(world_address, user_address, world_diff)?; | ||
let policies = collect_policies_from_contracts(user_address, contracts)?; | ||
trace!(target: "account::controller", policies_count = policies.len(), "Extracted policies from project."); | ||
Ok(policies) | ||
} | ||
|
||
fn collect_policies_from_local_world( | ||
world_address: Felt, | ||
fn collect_policies_from_contracts( | ||
user_address: Felt, | ||
world_diff: &WorldDiff, | ||
contracts: &HashMap<String, ContractInfo>, | ||
) -> Result<Vec<PolicyMethod>> { | ||
let mut policies: Vec<PolicyMethod> = Vec::new(); | ||
|
||
// get methods from all project contracts | ||
for (selector, resource) in world_diff.resources.iter() { | ||
if resource.resource_type() == ResourceType::Contract { | ||
// Safe to unwrap the two methods since the selector comes from the resources registry | ||
// in the local world. | ||
let contract_address = world_diff.get_contract_address(*selector).unwrap(); | ||
let sierra_class = world_diff.get_class(*selector).unwrap(); | ||
|
||
policies_from_abis(&mut policies, &resource.tag(), contract_address, &sierra_class.abi); | ||
for (tag, info) in contracts { | ||
for e in &info.entrypoints { | ||
let policy = PolicyMethod { target: info.address, method: e.clone() }; | ||
trace!(target: "account::controller", tag, target = format!("{:#x}", policy.target), method = %policy.method, "Adding policy"); | ||
policies.push(policy); | ||
} | ||
} | ||
|
||
// get method from world contract | ||
policies_from_abis(&mut policies, "world", world_address, &world_diff.world_info.class.abi); | ||
|
||
// special policy for sending declare tx | ||
// corresponds to [account_sdk::account::DECLARATION_SELECTOR] | ||
let method = "__declare_transaction__".to_string(); | ||
|
@@ -179,39 +165,12 @@ | |
Ok(policies) | ||
} | ||
|
||
/// Recursively extract methods and convert them into policies from the all the | ||
/// ABIs in the project. | ||
fn policies_from_abis( | ||
policies: &mut Vec<PolicyMethod>, | ||
contract_tag: &str, | ||
contract_address: Felt, | ||
entries: &[AbiEntry], | ||
) { | ||
for entry in entries { | ||
match entry { | ||
AbiEntry::Function(f) => { | ||
// we only create policies for non-view functions | ||
if let StateMutability::External = f.state_mutability { | ||
let policy = | ||
PolicyMethod { target: contract_address, method: f.name.to_string() }; | ||
trace!(target: "account::controller", tag = contract_tag, target = format!("{:#x}", policy.target), method = %policy.method, "Adding policy"); | ||
policies.push(policy); | ||
} | ||
} | ||
|
||
AbiEntry::Interface(i) => { | ||
policies_from_abis(policies, contract_tag, contract_address, &i.items) | ||
} | ||
|
||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use dojo_test_utils::compiler::CompilerTestSetup; | ||
use dojo_world::diff::WorldDiff; | ||
use dojo_world::contracts::ContractInfo; | ||
use scarb::compiler::Profile; | ||
use sozo_scarbext::WorkspaceExt; | ||
use starknet::macros::felt; | ||
|
@@ -228,13 +187,12 @@ | |
let ws = scarb::ops::read_workspace(config.manifest_path(), &config) | ||
.unwrap_or_else(|op| panic!("Error building workspace: {op:?}")); | ||
|
||
let world_local = ws.load_world_local().unwrap(); | ||
let world_diff = WorldDiff::from_local(world_local).unwrap(); | ||
let manifest = ws.read_manifest_profile().expect("Failed to read manifest").unwrap(); | ||
let contracts: HashMap<String, ContractInfo> = (&manifest).into(); | ||
|
||
let user_addr = felt!("0x2af9427c5a277474c079a1283c880ee8a6f0f8fbf73ce969c08d88befec1bba"); | ||
|
||
let policies = | ||
collect_policies(world_diff.world_info.address, user_addr, &world_diff).unwrap(); | ||
let policies = collect_policies(user_addr, &contracts).unwrap(); | ||
|
||
if std::env::var("POLICIES_FIX").is_ok() { | ||
let policies_json = serde_json::to_string_pretty(&policies).unwrap(); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix inconsistent error message condition.
The error message suggests using
--diff
even when it's already enabled, which could be confusing to users.📝 Committable suggestion