From 8b26910ebb39d24c22e694ebed8847ba5c7923f3 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Fri, 22 Nov 2024 16:20:40 +0300 Subject: [PATCH 01/11] fix(CI): advance after transactions are fully finalized (#982) --- contracts/context-config/tests/sandbox.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/contracts/context-config/tests/sandbox.rs b/contracts/context-config/tests/sandbox.rs index 612f10890..8282be74e 100644 --- a/contracts/context-config/tests/sandbox.rs +++ b/contracts/context-config/tests/sandbox.rs @@ -1124,7 +1124,6 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { let node1_balance = worker.view_account(&node1.id()).await?.balance; - // Deploy proxy contract let res = node1 .call(contract.id(), "mutate") .args_json(Signed::new( @@ -1155,11 +1154,15 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { let expected_log = format!("Context `{}` added", context_id); assert!(res.logs().iter().any(|log| log == &expected_log)); + worker.fast_forward(1).await?; + let node1_balance_after = worker.view_account(&node1.id()).await?.balance; let diff = node1_balance.saturating_sub(node1_balance_after); let node1_balance = node1_balance_after; + println!("Node1 balance diff: {}", diff); + assert!( diff < NearToken::from_millinear(10), "Node1 balance should not be reduced by more than 10 milliNEAR, but was reduced by {}", @@ -1185,6 +1188,7 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { println!("Initial storage usage: {}", initial_storage); println!("Initial WASM size: {}", initial_code_size); println!("Initial Balance: {}", initial_balance); + println!("Initial Node1 Balance: {}", node1_balance); let res = contract .call("set_proxy_code") @@ -1216,11 +1220,15 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { assert!(res.failures().is_empty(), "{:#?}", res.failures()); + worker.fast_forward(1).await?; + let node1_balance_after = worker.view_account(&node1.id()).await?.balance; let diff = node1_balance.saturating_sub(node1_balance_after); let node1_balance = node1_balance_after; + println!("Node1 balance diff: {}", diff); + assert!( diff < NearToken::from_millinear(10), "Node1 balance should not be reduced by more than 10 milliNEAR, but was reduced by {}", @@ -1236,6 +1244,7 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { println!("Intermediate storage usage: {}", intermediate_storage); println!("Intermediate WASM size: {}", intermediate_code_size); println!("Intermediate Balance: {}", intermediate_balance); + println!("Intermediate Node1 Balance: {}", node1_balance); // Calculate raw differences (can be negative) let storage_change = intermediate_storage as i64 - initial_storage as i64; @@ -1291,10 +1300,14 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { assert!(res.failures().is_empty(), "{:#?}", res.failures()); + worker.fast_forward(1).await?; + let node1_balance_after = worker.view_account(&node1.id()).await?.balance; let diff = node1_balance.saturating_sub(node1_balance_after); + println!("Node1 balance diff: {}", diff); + assert!( diff < NearToken::from_millinear(10), "Node1 balance should not be reduced by more than 10 milliNEAR, but was reduced by {}", @@ -1310,6 +1323,7 @@ async fn test_storage_usage_matches_code_size() -> eyre::Result<()> { println!("Final storage usage: {}", final_storage); println!("Final WASM size: {}", final_code_size); println!("Final Balance: {}", final_balance); + println!("Final Node1 Balance: {}", node1_balance); // Calculate raw differences (can be negative) let storage_change = final_storage as i64 - intermediate_storage as i64; From eade9238494729fb44db7234379446f895be535f Mon Sep 17 00:00:00 2001 From: Fico <70634661+fbozic@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:12:31 +0100 Subject: [PATCH 02/11] ci: setup e2e-tests in github workflow (#976) --- .github/workflows/e2e_tests.yml | 79 ++++++++++++++++++++++ e2e-tests/README.md | 18 ++++- e2e-tests/src/driver.rs | 66 +++++++++++++----- e2e-tests/src/main.rs | 9 +++ e2e-tests/src/meroctl.rs | 6 +- e2e-tests/src/merod.rs | 6 +- e2e-tests/src/output.rs | 65 ++++++++++++++++++ e2e-tests/src/steps/context_create.rs | 5 +- e2e-tests/src/steps/context_invite_join.rs | 3 +- e2e-tests/src/steps/jsonrpc_call.rs | 3 +- 10 files changed, 235 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/e2e_tests.yml create mode 100644 e2e-tests/src/output.rs diff --git a/.github/workflows/e2e_tests.yml b/.github/workflows/e2e_tests.yml new file mode 100644 index 000000000..6d8eb37d1 --- /dev/null +++ b/.github/workflows/e2e_tests.yml @@ -0,0 +1,79 @@ +name: End-to-end tests + +on: + push: + branches: + - '**' + paths: + - Cargo.toml + - Cargo.lock + - 'contracts/**' + - 'crates/**' + - 'e2e-tests/**' + - '.github/workflows/e2e_tests.yml' + +jobs: + test: + name: Test + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup rust toolchain + run: rustup toolchain install stable --profile minimal + + - name: Setup rust cache + uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + + - name: Build apps + run: | + ./apps/kv-store/build.sh + + - name: Build contracts + run: | + ./contracts/context-config/build.sh + ./contracts/proxy-lib/build.sh + + - name: Build binaries + run: | + cargo build -p meroctl -p merod -p e2e-tests + + - name: Run e2e tests + run: | + export NO_COLOR=1 + echo "Running e2e tests, check job summary for details" + echo "# E2E tests 🏗️" >> $GITHUB_STEP_SUMMARY + ./target/debug/e2e-tests \ + --input-dir ./e2e-tests/config \ + --output-dir ./e2e-tests/corpus \ + --merod-binary ./target/debug/merod \ + --meroctl-binary ./target/debug/meroctl \ + --output-format markdown >> $GITHUB_STEP_SUMMARY + + - name: Run e2e tests + if: success() || failure() + run: | + LOGS_DIR=./e2e-tests/corpus/logs + if [ ! -d "$LOGS_DIR" ]; then + echo "Directory $LOGS_DIR does not exist." + exit 1 + fi + + echo "# Node logs 📋" >> $GITHUB_STEP_SUMMARY + + for FOLDER in "$LOGS_DIR"/*; do + if [ -d "$FOLDER" ]; then + + RUN_LOG="$FOLDER/run.log" + if [ -f "$RUN_LOG" ]; then + echo "## Node logs: $(basename $FOLDER) 📋" >> $GITHUB_STEP_SUMMARY + cat "$RUN_LOG" >> $GITHUB_STEP_SUMMARY + else + echo "## No run.log found in $FOLDER ⚠️" >> $GITHUB_STEP_SUMMARY + fi + fi + done diff --git a/e2e-tests/README.md b/e2e-tests/README.md index 299b0387d..dcc926fed 100644 --- a/e2e-tests/README.md +++ b/e2e-tests/README.md @@ -4,12 +4,26 @@ Binary crate which runs e2e tests for the merod node. ## Usage -Build the merod and meroctl binaries and run the e2e tests with the following -commands: +First build apps, contracts, and mero binaries. After that run the e2e tests. + +Example of running the e2e tests: ```bash +./apps/kv-store/build.sh + +./contracts/context-config/build.sh +./contracts/proxy-lib/build.sh + cargo build -p merod cargo build -p meroctl +export NO_COLOR=1 # Disable color output for merod logs cargo run -p e2e-tests -- --input-dir ./e2e-tests/config --output-dir ./e2e-tests/corpus --merod-binary ./target/debug/merod --meroctl-binary ./target/debug/meroctl ``` + +Useful env vars for debugging: + +- `RUST_LOG=debug` - enable debug logs + - `RUST_LOG=near_jsonrpc_client=debug` - or more specific logs +- `NEAR_ENABLE_SANDBOX_LOG=1` - enable near sandbox logs +- `NO_COLOR=1` - disable color output diff --git a/e2e-tests/src/driver.rs b/e2e-tests/src/driver.rs index 728acc814..d91c4f7a4 100644 --- a/e2e-tests/src/driver.rs +++ b/e2e-tests/src/driver.rs @@ -14,6 +14,7 @@ use tokio::time::sleep; use crate::config::Config; use crate::meroctl::Meroctl; use crate::merod::Merod; +use crate::output::OutputWriter; use crate::steps::{TestScenario, TestStep}; use crate::TestEnvironment; @@ -24,6 +25,7 @@ pub struct TestContext<'a> { pub context_id: Option, pub inviter_public_key: Option, pub invitees_public_keys: HashMap, + pub output_writer: OutputWriter, } pub trait Test { @@ -31,7 +33,12 @@ pub trait Test { } impl<'a> TestContext<'a> { - pub fn new(inviter: String, invitees: Vec, meroctl: &'a Meroctl) -> Self { + pub fn new( + inviter: String, + invitees: Vec, + meroctl: &'a Meroctl, + output_writer: OutputWriter, + ) -> Self { Self { inviter, invitees, @@ -39,6 +46,7 @@ impl<'a> TestContext<'a> { context_id: None, inviter_public_key: None, invitees_public_keys: HashMap::new(), + output_writer, } } } @@ -81,6 +89,13 @@ impl Driver { self.stop_merods().await; + if let Err(e) = &result { + self.environment + .output_writer + .write_str("Error occurred during test run:"); + self.environment.output_writer.write_string(e.to_string()); + } + result } @@ -113,7 +128,9 @@ impl Driver { } async fn boot_merods(&mut self) -> EyreResult<()> { - println!("========================= Starting nodes ==========================="); + self.environment + .output_writer + .write_header("Starting merod nodes", 2); for i in 0..self.config.network.node_count { let node_name = format!("node{}", i + 1); @@ -178,9 +195,7 @@ impl Driver { } // TODO: Implement health check? - sleep(Duration::from_secs(20)).await; - - println!("===================================================================="); + sleep(Duration::from_secs(10)).await; Ok(()) } @@ -213,35 +228,50 @@ impl Driver { } async fn run_scenario(&self, file_path: PathBuf) -> EyreResult<()> { - println!("================= Setting up scenario and context =================="); + self.environment + .output_writer + .write_header("Running scenario", 2); + let scenario: TestScenario = from_slice(&read(&file_path).await?)?; - println!( - "Loaded test scenario from file: {:?}\n{:?}", - file_path, scenario - ); + self.environment + .output_writer + .write_string(format!("Source file: {:?}", file_path)); + self.environment + .output_writer + .write_string(format!("Steps count: {}", scenario.steps.len())); let (inviter, invitees) = match self.pick_inviter_node() { Some((inviter, invitees)) => (inviter, invitees), None => bail!("Not enough nodes to run the test"), }; - println!("Picked inviter: {}", inviter); - println!("Picked invitees: {:?}", invitees); + self.environment + .output_writer + .write_string(format!("Picked inviter: {}", inviter)); + self.environment + .output_writer + .write_string(format!("Picked invitees: {:?}", invitees)); - let mut ctx = TestContext::new(inviter, invitees, &self.meroctl); - - println!("===================================================================="); + let mut ctx = TestContext::new( + inviter, + invitees, + &self.meroctl, + self.environment.output_writer, + ); for step in scenario.steps.iter() { - println!("======================== Starting step ============================="); - println!("Step: {:?}", step); + self.environment + .output_writer + .write_header("Running test step", 3); + self.environment.output_writer.write_str("Step spec:"); + self.environment.output_writer.write_json(&step)?; + match step { TestStep::ContextCreate(step) => step.run_assert(&mut ctx).await?, TestStep::ContextInviteJoin(step) => step.run_assert(&mut ctx).await?, TestStep::JsonRpcCall(step) => step.run_assert(&mut ctx).await?, }; - println!("===================================================================="); } Ok(()) diff --git a/e2e-tests/src/main.rs b/e2e-tests/src/main.rs index a566f8705..8f7de8b27 100644 --- a/e2e-tests/src/main.rs +++ b/e2e-tests/src/main.rs @@ -4,6 +4,7 @@ use config::Config; use const_format::concatcp; use driver::Driver; use eyre::Result as EyreResult; +use output::{OutputFormat, OutputWriter}; use rand::Rng; use tokio::fs::{create_dir_all, read_to_string, remove_dir_all}; @@ -11,6 +12,7 @@ mod config; mod driver; mod meroctl; mod merod; +mod output; mod steps; pub const EXAMPLES: &str = r" @@ -51,6 +53,11 @@ pub struct Args { #[arg(long, value_name = "PATH")] #[arg(env = "MEROCTL_BINARY", hide_env_values = true)] pub meroctl_binary: Utf8PathBuf, + + /// Format of the E2E test output. + #[arg(long, value_name = "OUTPUT_FORMAT", default_value_t, value_enum)] + #[arg(env = "E2E_OUTPUT_FORMAT", hide_env_values = true)] + pub output_format: OutputFormat, } #[derive(Debug)] @@ -62,6 +69,7 @@ pub struct TestEnvironment { pub output_dir: Utf8PathBuf, pub nodes_dir: Utf8PathBuf, pub logs_dir: Utf8PathBuf, + pub output_writer: OutputWriter, } impl Into for Args { @@ -76,6 +84,7 @@ impl Into for Args { output_dir: self.output_dir.clone(), nodes_dir: self.output_dir.join("nodes"), logs_dir: self.output_dir.join("logs"), + output_writer: OutputWriter::new(self.output_format), } } } diff --git a/e2e-tests/src/meroctl.rs b/e2e-tests/src/meroctl.rs index 3eca3da26..75b798eca 100644 --- a/e2e-tests/src/meroctl.rs +++ b/e2e-tests/src/meroctl.rs @@ -4,11 +4,13 @@ use camino::Utf8PathBuf; use eyre::{bail, eyre, OptionExt, Result as EyreResult}; use tokio::process::Command; +use crate::output::OutputWriter; use crate::TestEnvironment; pub struct Meroctl { nodes_dir: Utf8PathBuf, binary: Utf8PathBuf, + output_writer: OutputWriter, } impl Meroctl { @@ -16,6 +18,7 @@ impl Meroctl { Self { nodes_dir: environment.nodes_dir.clone(), binary: environment.meroctl_binary.clone(), + output_writer: environment.output_writer, } } @@ -148,7 +151,8 @@ impl Meroctl { root_args.extend(args); - println!("Command: '{:}' {:?}", &self.binary, root_args); + self.output_writer + .write_string(format!("Command: '{:}' {:?}", &self.binary, root_args)); let output = Command::new(&self.binary) .args(root_args) diff --git a/e2e-tests/src/merod.rs b/e2e-tests/src/merod.rs index f3edcf2aa..61c413c4d 100644 --- a/e2e-tests/src/merod.rs +++ b/e2e-tests/src/merod.rs @@ -7,6 +7,7 @@ use tokio::fs::{create_dir_all, File}; use tokio::io::copy; use tokio::process::{Child, Command}; +use crate::output::OutputWriter; use crate::TestEnvironment; pub struct Merod { @@ -15,6 +16,7 @@ pub struct Merod { nodes_dir: Utf8PathBuf, log_dir: Utf8PathBuf, binary: Utf8PathBuf, + output_writer: OutputWriter, } impl Merod { @@ -25,6 +27,7 @@ impl Merod { log_dir: environment.logs_dir.join(&name), binary: environment.merod_binary.clone(), name, + output_writer: environment.output_writer, } } @@ -92,7 +95,8 @@ impl Merod { let log_file = self.log_dir.join(format!("{}.log", log_suffix)); let mut log_file = File::create(&log_file).await?; - println!("Command: '{:}' {:?}", &self.binary, root_args); + self.output_writer + .write_string(format!("Command: '{:}' {:?}", &self.binary, root_args)); let mut child = Command::new(&self.binary) .args(root_args) diff --git a/e2e-tests/src/output.rs b/e2e-tests/src/output.rs new file mode 100644 index 000000000..08a8df1e7 --- /dev/null +++ b/e2e-tests/src/output.rs @@ -0,0 +1,65 @@ +use clap::ValueEnum; +use eyre::{Ok, Result as EyreResult}; +use serde::Serialize; + +#[derive(Clone, Copy, Debug)] +pub struct OutputWriter { + format: OutputFormat, +} + +#[derive(Clone, Copy, Debug, Default, ValueEnum)] +pub enum OutputFormat { + Markdown, + #[default] + PlainText, +} + +impl OutputWriter { + pub fn new(format: OutputFormat) -> Self { + Self { format } + } + + pub fn write_str(&self, line: &str) { + match self.format { + OutputFormat::Markdown => println!("{} ", line), + OutputFormat::PlainText => println!("{}", line), + } + } + + pub fn write_string(&self, line: String) { + match self.format { + OutputFormat::Markdown => println!("{} ", line), + OutputFormat::PlainText => println!("{}", line), + } + } + + pub fn write_header(&self, header: &str, level: usize) { + match self.format { + OutputFormat::Markdown => println!("{} {} ", "#".repeat(level), header), + OutputFormat::PlainText => { + println!( + "{}{}{}", + "-".repeat(level * 5), + header, + "-".repeat(level * 5), + ) + } + } + } + + pub fn write_json(&self, json: &T) -> EyreResult<()> + where + T: ?Sized + Serialize, + { + match self.format { + OutputFormat::Markdown => { + println!("```json\n{}\n```", serde_json::to_string_pretty(json)?); + } + OutputFormat::PlainText => { + println!("{}", serde_json::to_string(json)?); + } + } + + Ok(()) + } +} diff --git a/e2e-tests/src/steps/context_create.rs b/e2e-tests/src/steps/context_create.rs index 2f88de830..82ba9331c 100644 --- a/e2e-tests/src/steps/context_create.rs +++ b/e2e-tests/src/steps/context_create.rs @@ -30,7 +30,10 @@ impl Test for CreateContextStep { ctx.context_id = Some(context_id); ctx.inviter_public_key = Some(member_public_key); - println!("Report: Created context on '{}' node", &ctx.inviter); + ctx.output_writer.write_string(format!( + "Report: Created context on '{}' node", + &ctx.inviter + )); Ok(()) } diff --git a/e2e-tests/src/steps/context_invite_join.rs b/e2e-tests/src/steps/context_invite_join.rs index d76621154..912ca7d86 100644 --- a/e2e-tests/src/steps/context_invite_join.rs +++ b/e2e-tests/src/steps/context_invite_join.rs @@ -57,7 +57,8 @@ impl Test for InviteJoinContextStep { .insert(invitee.clone(), invitee_public_key), ); - println!("Report: Node '{}' joined the context", invitee) + ctx.output_writer + .write_string(format!("Report: Node '{}' joined the context", invitee)); } Ok(()) diff --git a/e2e-tests/src/steps/jsonrpc_call.rs b/e2e-tests/src/steps/jsonrpc_call.rs index 31a1c447d..89cb98bbb 100644 --- a/e2e-tests/src/steps/jsonrpc_call.rs +++ b/e2e-tests/src/steps/jsonrpc_call.rs @@ -75,7 +75,8 @@ impl Test for JsonRpcCallStep { } } - println!("Report: Call on '{}' node passed assertion", node) + ctx.output_writer + .write_string(format!("Report: Call on '{}' node passed assertion", node)); } Ok(()) From db551eb9061b09213aff1d29f5129392bee748e4 Mon Sep 17 00:00:00 2001 From: petarjuki7 <36903459+petarjuki7@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:06:17 +0100 Subject: [PATCH 03/11] feat(encryption): Added support for nonces (#978) --- crates/crypto/src/lib.rs | 23 +++++++-------- crates/node/src/lib.rs | 11 +++++-- crates/node/src/sync.rs | 39 +++++++++++++++---------- crates/node/src/sync/blobs.rs | 37 ++++++++++++++++++------ crates/node/src/sync/key.rs | 51 +++++++++++++++++++++++++++------ crates/node/src/sync/state.rs | 54 +++++++++++++++++++++++++++-------- crates/node/src/types.rs | 5 +++- 7 files changed, 160 insertions(+), 60 deletions(-) diff --git a/crates/crypto/src/lib.rs b/crates/crypto/src/lib.rs index 765fa2eed..9d3929bcb 100644 --- a/crates/crypto/src/lib.rs +++ b/crates/crypto/src/lib.rs @@ -2,17 +2,15 @@ use calimero_primitives::identity::{PrivateKey, PublicKey}; use ed25519_dalek::{SecretKey, SigningKey}; use ring::aead; +pub const NONCE_LEN: usize = 12; + +pub type Nonce = [u8; NONCE_LEN]; + #[derive(Copy, Clone, Debug)] pub struct SharedKey { key: SecretKey, } -#[derive(Debug)] -pub struct Record { - pub token: Vec, - pub nonce: [u8; 12], -} - impl SharedKey { pub fn new(sk: &PrivateKey, pk: &PublicKey) -> Self { SharedKey { @@ -29,7 +27,7 @@ impl SharedKey { SharedKey { key: **sk } } - pub fn encrypt(&self, payload: Vec, nonce: [u8; 12]) -> Option> { + pub fn encrypt(&self, payload: Vec, nonce: Nonce) -> Option> { let encryption_key = aead::LessSafeKey::new(aead::UnboundKey::new(&aead::AES_256_GCM, &self.key).ok()?); @@ -45,7 +43,7 @@ impl SharedKey { Some(cipher_text) } - pub fn decrypt(&self, cipher_text: Vec, nonce: [u8; aead::NONCE_LEN]) -> Option> { + pub fn decrypt(&self, cipher_text: Vec, nonce: Nonce) -> Option> { let decryption_key = aead::LessSafeKey::new(aead::UnboundKey::new(&aead::AES_256_GCM, &self.key).ok()?); @@ -68,12 +66,13 @@ impl SharedKey { #[cfg(test)] mod tests { use eyre::OptionExt; + use rand::thread_rng; use super::*; #[test] fn test_encrypt_decrypt() -> eyre::Result<()> { - let mut csprng = rand::thread_rng(); + let mut csprng = thread_rng(); let signer = PrivateKey::random(&mut csprng); let verifier = PrivateKey::random(&mut csprng); @@ -82,7 +81,7 @@ mod tests { let verifier_shared_key = SharedKey::new(&verifier, &signer.public_key()); let payload = b"privacy is important"; - let nonce = [0u8; aead::NONCE_LEN]; + let nonce = [0u8; NONCE_LEN]; let encrypted_payload = signer_shared_key .encrypt(payload.to_vec(), nonce) @@ -100,7 +99,7 @@ mod tests { #[test] fn test_decrypt_with_invalid_key() -> eyre::Result<()> { - let mut csprng = rand::thread_rng(); + let mut csprng = thread_rng(); let signer = PrivateKey::random(&mut csprng); let verifier = PrivateKey::random(&mut csprng); @@ -110,7 +109,7 @@ mod tests { let invalid_shared_key = SharedKey::new(&invalid, &invalid.public_key()); let token = b"privacy is important"; - let nonce = [0u8; aead::NONCE_LEN]; + let nonce = [0u8; NONCE_LEN]; let encrypted_token = signer_shared_key .encrypt(token.to_vec(), nonce) diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 010ad204d..fecb5e625 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -16,7 +16,7 @@ use calimero_context::config::ContextConfig; use calimero_context::ContextManager; use calimero_context_config::repr::ReprTransmute; use calimero_context_config::ProposalAction; -use calimero_crypto::SharedKey; +use calimero_crypto::{Nonce, SharedKey, NONCE_LEN}; use calimero_network::client::NetworkClient; use calimero_network::config::NetworkConfig; use calimero_network::types::{NetworkEvent, PeerId}; @@ -305,6 +305,7 @@ impl Node { author_id, root_hash, artifact, + nonce, } => { self.handle_state_delta( source, @@ -312,6 +313,7 @@ impl Node { author_id, root_hash, artifact.into_owned(), + nonce, ) .await?; } @@ -327,6 +329,7 @@ impl Node { author_id: PublicKey, root_hash: Hash, artifact: Vec, + nonce: [u8; NONCE_LEN], ) -> EyreResult<()> { let Some(mut context) = self.ctx_manager.get_context(&context_id)? else { bail!("context '{}' not found", context_id); @@ -344,7 +347,7 @@ impl Node { let shared_key = SharedKey::from_sk(&sender_key); let artifact = &shared_key - .decrypt(artifact, [0; 12]) + .decrypt(artifact, nonce) .ok_or_eyre("failed to decrypt message")?; let Some(outcome) = self @@ -386,9 +389,10 @@ impl Node { .ok_or_eyre("expected own identity to have sender key")?; let shared_key = SharedKey::from_sk(&sender_key); + let nonce = thread_rng().gen::(); let artifact_encrypted = shared_key - .encrypt(outcome.artifact.clone(), [0; 12]) + .encrypt(outcome.artifact.clone(), nonce) .ok_or_eyre("encryption failed")?; let message = to_vec(&BroadcastMessage::StateDelta { @@ -396,6 +400,7 @@ impl Node { author_id: executor_public_key, root_hash: context.root_hash, artifact: artifact_encrypted.as_slice().into(), + nonce, })?; let _ignored = self diff --git a/crates/node/src/sync.rs b/crates/node/src/sync.rs index 0f7b066bd..52c23b4ea 100644 --- a/crates/node/src/sync.rs +++ b/crates/node/src/sync.rs @@ -1,9 +1,9 @@ use std::time::Duration; -use calimero_crypto::SharedKey; +use calimero_crypto::{Nonce, SharedKey}; use calimero_network::stream::{Message, Stream}; use calimero_primitives::context::ContextId; -use eyre::{bail, Result as EyreResult}; +use eyre::{bail, eyre, OptionExt, Result as EyreResult}; use futures_util::{SinkExt, StreamExt}; use libp2p::gossipsub::TopicHash; use libp2p::PeerId; @@ -28,15 +28,14 @@ pub struct SyncConfig { async fn send( stream: &mut Stream, message: &StreamMessage<'_>, - shared_key: Option, + shared_key: Option<(SharedKey, Nonce)>, ) -> EyreResult<()> { let base_data = borsh::to_vec(message)?; let data = match shared_key { - Some(key) => match key.encrypt(base_data, [0; 12]) { - Some(data) => data, - None => bail!("encryption failed"), - }, + Some((key, nonce)) => key + .encrypt(base_data, nonce) + .ok_or_eyre("encryption failed")?, None => base_data, }; @@ -47,7 +46,7 @@ async fn send( async fn recv( stream: &mut Stream, duration: Duration, - shared_key: Option, + shared_key: Option<(SharedKey, Nonce)>, ) -> EyreResult>> { let Some(message) = timeout(duration, stream.next()).await? else { return Ok(None); @@ -56,10 +55,17 @@ async fn recv( let message_data = message?.data.into_owned(); let data = match shared_key { - Some(key) => match key.decrypt(message_data, [0; 12]) { - Some(data) => data, - None => bail!("decryption failed"), - }, + Some((key, nonce)) => { + match key.decrypt( + message_data, + nonce + .try_into() + .map_err(|_| eyre!("nonce must be 12 bytes"))?, + ) { + Some(data) => data, + None => bail!("decryption failed"), + } + } None => message_data, }; @@ -158,12 +164,14 @@ impl Node { return Ok(None); }; - let (context_id, their_identity, payload) = match message { + let (context_id, their_identity, payload, nonce) = match message { StreamMessage::Init { context_id, party_id, payload, - } => (context_id, party_id, payload), + next_nonce, + .. + } => (context_id, party_id, payload, next_nonce), unexpected @ (StreamMessage::Message { .. } | StreamMessage::OpaqueError) => { bail!("expected initialization handshake, got {:?}", unexpected) } @@ -201,7 +209,7 @@ impl Node { match payload { InitPayload::KeyShare => { - self.handle_key_share_request(&context, our_identity, their_identity, stream) + self.handle_key_share_request(&context, our_identity, their_identity, stream, nonce) .await? } InitPayload::BlobShare { blob_id } => { @@ -233,6 +241,7 @@ impl Node { their_root_hash, their_application_id, stream, + nonce, ) .await? } diff --git a/crates/node/src/sync/blobs.rs b/crates/node/src/sync/blobs.rs index 9731a098f..454b20f0c 100644 --- a/crates/node/src/sync/blobs.rs +++ b/crates/node/src/sync/blobs.rs @@ -1,4 +1,4 @@ -use calimero_crypto::SharedKey; +use calimero_crypto::{Nonce, SharedKey, NONCE_LEN}; use calimero_network::stream::Stream; use calimero_primitives::blobs::BlobId; use calimero_primitives::context::Context; @@ -6,6 +6,7 @@ use calimero_primitives::identity::PublicKey; use eyre::{bail, OptionExt}; use futures_util::stream::poll_fn; use futures_util::TryStreamExt; +use rand::{thread_rng, Rng}; use tokio::sync::mpsc; use tracing::{debug, warn}; @@ -29,12 +30,15 @@ impl Node { "Initiating blob share", ); + let our_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Init { context_id: context.id, party_id: our_identity, payload: InitPayload::BlobShare { blob_id }, + next_nonce: our_nonce, }, None, ) @@ -44,13 +48,14 @@ impl Node { bail!("connection closed while awaiting blob share handshake"); }; - let their_identity = match ack { + let (their_identity, mut their_nonce) = match ack { StreamMessage::Init { party_id, payload: InitPayload::BlobShare { blob_id: ack_blob_id, }, + next_nonce, .. } => { if ack_blob_id != blob_id { @@ -61,7 +66,7 @@ impl Node { ); } - party_id + (party_id, next_nonce) } unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. } @@ -88,13 +93,20 @@ impl Node { let read_task = async { let mut sequencer = Sequencer::default(); - while let Some(msg) = recv(stream, self.sync_config.timeout, Some(shared_key)).await? { - let (sequence_id, chunk) = match msg { + while let Some(msg) = recv( + stream, + self.sync_config.timeout, + Some((shared_key, their_nonce)), + ) + .await? + { + let (sequence_id, chunk, their_new_nonce) = match msg { StreamMessage::OpaqueError => bail!("other peer ran into an error"), StreamMessage::Message { sequence_id, payload: MessagePayload::BlobShare { chunk }, - } => (sequence_id, chunk), + next_nonce, + } => (sequence_id, chunk, next_nonce), unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. }) => { bail!("unexpected message: {:?}", unexpected) } @@ -107,6 +119,8 @@ impl Node { } tx.send(Ok(chunk)).await?; + + their_nonce = their_new_nonce; } drop(tx); @@ -163,6 +177,7 @@ impl Node { .ok_or_eyre("expected own identity to have private key")?; let shared_key = SharedKey::new(&private_key, &their_identity); + let mut our_nonce = thread_rng().gen::(); send( stream, @@ -170,6 +185,7 @@ impl Node { context_id: context.id, party_id: our_identity, payload: InitPayload::BlobShare { blob_id }, + next_nonce: our_nonce, }, None, ) @@ -178,6 +194,7 @@ impl Node { let mut sequencer = Sequencer::default(); while let Some(chunk) = blob.try_next().await? { + let our_new_nonce = thread_rng().gen::(); send( stream, &StreamMessage::Message { @@ -185,10 +202,13 @@ impl Node { payload: MessagePayload::BlobShare { chunk: chunk.into_vec().into(), }, + next_nonce: our_new_nonce, }, - Some(shared_key), + Some((shared_key, our_nonce)), ) .await?; + + our_nonce = our_new_nonce; } send( @@ -196,8 +216,9 @@ impl Node { &StreamMessage::Message { sequence_id: sequencer.next(), payload: MessagePayload::BlobShare { chunk: b"".into() }, + next_nonce: [0; NONCE_LEN], }, - Some(shared_key), + Some((shared_key, our_nonce)), ) .await?; diff --git a/crates/node/src/sync/key.rs b/crates/node/src/sync/key.rs index 9068710c6..0848bbe75 100644 --- a/crates/node/src/sync/key.rs +++ b/crates/node/src/sync/key.rs @@ -1,8 +1,9 @@ -use calimero_crypto::SharedKey; +use calimero_crypto::{Nonce, SharedKey}; use calimero_network::stream::Stream; use calimero_primitives::context::Context; use calimero_primitives::identity::PublicKey; use eyre::{bail, OptionExt}; +use rand::{thread_rng, Rng}; use tracing::debug; use crate::sync::{recv, send, Sequencer}; @@ -22,12 +23,15 @@ impl Node { "Initiating key share", ); + let our_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Init { context_id: context.id, party_id: our_identity, payload: InitPayload::KeyShare, + next_nonce: our_nonce, }, None, ) @@ -37,12 +41,13 @@ impl Node { bail!("connection closed while awaiting state sync handshake"); }; - let their_identity = match ack { + let (their_identity, their_nonce) = match ack { StreamMessage::Init { party_id, payload: InitPayload::KeyShare, + next_nonce, .. - } => party_id, + } => (party_id, next_nonce), unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. } | StreamMessage::OpaqueError) => { @@ -50,8 +55,15 @@ impl Node { } }; - self.bidirectional_key_share(context, our_identity, their_identity, stream) - .await + self.bidirectional_key_share( + context, + our_identity, + their_identity, + stream, + our_nonce, + their_nonce, + ) + .await } pub(super) async fn handle_key_share_request( @@ -60,6 +72,7 @@ impl Node { our_identity: PublicKey, their_identity: PublicKey, stream: &mut Stream, + their_nonce: Nonce, ) -> eyre::Result<()> { debug!( context_id=%context.id, @@ -67,19 +80,29 @@ impl Node { "Received key share request", ); + let our_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Init { context_id: context.id, party_id: our_identity, payload: InitPayload::KeyShare, + next_nonce: our_nonce, }, None, ) .await?; - self.bidirectional_key_share(context, our_identity, their_identity, stream) - .await + self.bidirectional_key_share( + context, + our_identity, + their_identity, + stream, + our_nonce, + their_nonce, + ) + .await } async fn bidirectional_key_share( @@ -88,6 +111,8 @@ impl Node { our_identity: PublicKey, their_identity: PublicKey, stream: &mut Stream, + our_nonce: Nonce, + their_nonce: Nonce, ) -> eyre::Result<()> { debug!( context_id=%context.id, @@ -115,12 +140,19 @@ impl Node { &StreamMessage::Message { sequence_id: sqx_out.next(), payload: MessagePayload::KeyShare { sender_key }, + next_nonce: our_nonce, }, - Some(shared_key), + Some((shared_key, our_nonce)), ) .await?; - let Some(msg) = recv(stream, self.sync_config.timeout, Some(shared_key)).await? else { + let Some(msg) = recv( + stream, + self.sync_config.timeout, + Some((shared_key, their_nonce)), + ) + .await? + else { bail!("connection closed while awaiting key share"); }; @@ -128,6 +160,7 @@ impl Node { StreamMessage::Message { sequence_id, payload: MessagePayload::KeyShare { sender_key }, + .. } => (sequence_id, sender_key), unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. } diff --git a/crates/node/src/sync/state.rs b/crates/node/src/sync/state.rs index 1761ca813..d723dc28b 100644 --- a/crates/node/src/sync/state.rs +++ b/crates/node/src/sync/state.rs @@ -1,12 +1,13 @@ use std::borrow::Cow; -use calimero_crypto::SharedKey; +use calimero_crypto::{Nonce, SharedKey}; use calimero_network::stream::Stream; use calimero_primitives::application::ApplicationId; use calimero_primitives::context::Context; use calimero_primitives::hash::Hash; use calimero_primitives::identity::PublicKey; use eyre::{bail, OptionExt}; +use rand::{thread_rng, Rng}; use tracing::debug; use crate::sync::{recv, send, Sequencer}; @@ -28,6 +29,8 @@ impl Node { "Initiating state sync", ); + let our_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Init { @@ -37,19 +40,20 @@ impl Node { root_hash: context.root_hash, application_id: context.application_id, }, + next_nonce: our_nonce, }, None, ) .await?; - let mut pair = None; + let mut triple = None; for _ in 1..=2 { let Some(ack) = recv(stream, self.sync_config.timeout, None).await? else { bail!("connection closed while awaiting state sync handshake"); }; - let (root_hash, their_identity) = match ack { + let (root_hash, their_identity, their_nonce) = match ack { StreamMessage::Init { party_id, payload: @@ -57,6 +61,7 @@ impl Node { root_hash, application_id, }, + next_nonce, .. } => { if application_id != context.application_id { @@ -67,7 +72,7 @@ impl Node { ); } - (root_hash, party_id) + (root_hash, party_id, next_nonce) } StreamMessage::Init { party_id: their_identity, @@ -92,12 +97,12 @@ impl Node { } }; - pair = Some((root_hash, their_identity)); + triple = Some((root_hash, their_identity, their_nonce)); break; } - let Some((root_hash, their_identity)) = pair else { + let Some((root_hash, their_identity, their_nonce)) = triple else { bail!("expected two state sync handshakes, got none"); }; @@ -113,6 +118,7 @@ impl Node { .ok_or_eyre("expected own identity to have private key")?; let shared_key = SharedKey::new(&private_key, &their_identity); + let our_new_nonce = thread_rng().gen::(); send( stream, @@ -121,11 +127,11 @@ impl Node { payload: MessagePayload::StateSync { artifact: b"".into(), }, + next_nonce: our_new_nonce, }, - Some(shared_key), + Some((shared_key, our_nonce)), ) .await?; - self.bidirectional_sync( context, our_identity, @@ -133,6 +139,8 @@ impl Node { &mut sqx_out, stream, shared_key, + our_new_nonce, + their_nonce, ) .await?; @@ -147,6 +155,7 @@ impl Node { their_root_hash: Hash, their_application_id: ApplicationId, stream: &mut Stream, + their_nonce: Nonce, ) -> eyre::Result<()> { debug!( context_id=%context.id, @@ -191,6 +200,8 @@ impl Node { debug!(context_id=%context.id, "Resuming state sync"); } + let our_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Init { @@ -200,6 +211,7 @@ impl Node { root_hash: context.root_hash, application_id: context.application_id, }, + next_nonce: our_nonce, }, None, ) @@ -225,6 +237,8 @@ impl Node { &mut sqx_out, stream, shared_key, + our_nonce, + their_nonce, ) .await @@ -239,6 +253,8 @@ impl Node { sqx_out: &mut Sequencer, stream: &mut Stream, shared_key: SharedKey, + mut our_nonce: Nonce, + mut their_nonce: Nonce, ) -> eyre::Result<()> { debug!( context_id=%context.id, @@ -249,18 +265,27 @@ impl Node { let mut sqx_in = Sequencer::default(); - while let Some(msg) = recv(stream, self.sync_config.timeout, Some(shared_key)).await? { - let (sequence_id, artifact) = match msg { + while let Some(msg) = recv( + stream, + self.sync_config.timeout, + Some((shared_key, their_nonce)), + ) + .await? + { + let (sequence_id, artifact, their_new_nonce) = match msg { StreamMessage::OpaqueError => bail!("other peer ran into an error"), StreamMessage::Message { sequence_id, payload: MessagePayload::StateSync { artifact }, - } => (sequence_id, artifact), + next_nonce, + } => (sequence_id, artifact, next_nonce), unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. }) => { bail!("unexpected message: {:?}", unexpected) } }; + their_nonce = their_new_nonce; + sqx_in.test(sequence_id)?; if artifact.is_empty() && sqx_out.current() != 0 { @@ -283,6 +308,8 @@ impl Node { "State sync outcome", ); + let our_new_nonce = thread_rng().gen::(); + send( stream, &StreamMessage::Message { @@ -290,10 +317,13 @@ impl Node { payload: MessagePayload::StateSync { artifact: Cow::from(&outcome.artifact), }, + next_nonce: our_new_nonce, }, - Some(shared_key), + Some((shared_key, our_nonce)), ) .await?; + + our_nonce = our_new_nonce; } debug!( diff --git a/crates/node/src/types.rs b/crates/node/src/types.rs index 1d02526c4..e07848555 100644 --- a/crates/node/src/types.rs +++ b/crates/node/src/types.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use borsh::{BorshDeserialize, BorshSerialize}; +use calimero_crypto::{Nonce, NONCE_LEN}; use calimero_primitives::application::ApplicationId; use calimero_primitives::blobs::BlobId; use calimero_primitives::context::ContextId; @@ -18,6 +19,7 @@ pub enum BroadcastMessage<'a> { author_id: PublicKey, root_hash: Hash, artifact: Cow<'a, [u8]>, + nonce: [u8; NONCE_LEN], }, } @@ -26,12 +28,13 @@ pub enum StreamMessage<'a> { Init { context_id: ContextId, party_id: PublicKey, - // nonce: usize, payload: InitPayload, + next_nonce: Nonce, }, Message { sequence_id: usize, payload: MessagePayload<'a>, + next_nonce: Nonce, }, /// Other peers must not learn anything about the node's state if anything goes wrong. OpaqueError, From 55c6121c755f3a12efc69c1ae0ceaf67af898f30 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Tue, 26 Nov 2024 16:22:45 +0300 Subject: [PATCH 04/11] fix: state sync (#969) --- Cargo.lock | 2 + apps/gen-ext/Cargo.toml | 3 +- apps/kv-store/src/__private.rs | 85 ---- apps/kv-store/src/lib.rs | 20 +- apps/only-peers/Cargo.toml | 1 + apps/only-peers/src/lib.rs | 44 +- apps/visited/src/lib.rs | 18 +- crates/node/src/lib.rs | 23 +- crates/node/src/sync/state.rs | 22 +- crates/sdk/macros/src/logic/method.rs | 68 ++- crates/storage-macros/src/lib.rs | 227 +-------- crates/storage-macros/tests/atomic_unit.rs | 335 ------------- crates/storage-macros/tests/collection.rs | 184 ------- .../tests/compile_fail/collection.rs | 37 -- .../tests/compile_fail/collection.stderr | 10 - crates/storage/src/address.rs | 10 +- crates/storage/src/collections.rs | 406 +++++++++++++++- crates/storage/src/collections/error.rs | 3 +- crates/storage/src/collections/root.rs | 194 ++++++++ .../storage/src/collections/unordered_map.rs | 338 +++++++------ .../storage/src/collections/unordered_set.rs | 224 +++++---- crates/storage/src/collections/vector.rs | 297 +++++++----- crates/storage/src/entities.rs | 193 ++++---- crates/storage/src/env.rs | 2 +- crates/storage/src/index.rs | 79 +-- crates/storage/src/integration.rs | 18 +- crates/storage/src/interface.rs | 455 ++++++++++-------- crates/storage/src/lib.rs | 9 +- crates/storage/src/store.rs | 2 +- crates/storage/src/tests/common.rs | 110 +---- crates/storage/src/tests/entities.rs | 130 +---- crates/storage/src/tests/index.rs | 258 ++++++---- crates/storage/src/tests/interface.rs | 212 ++++---- scripts/build-all-apps.sh | 5 +- 34 files changed, 1905 insertions(+), 2119 deletions(-) delete mode 100644 apps/kv-store/src/__private.rs delete mode 100644 crates/storage-macros/tests/atomic_unit.rs delete mode 100644 crates/storage-macros/tests/collection.rs delete mode 100644 crates/storage-macros/tests/compile_fail/collection.rs delete mode 100644 crates/storage-macros/tests/compile_fail/collection.stderr create mode 100644 crates/storage/src/collections/root.rs diff --git a/Cargo.lock b/Cargo.lock index ab7c0be65..d4aa212de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2589,6 +2589,7 @@ version = "0.1.0" dependencies = [ "calimero-sdk", "calimero-sdk-near", + "calimero-storage", ] [[package]] @@ -5494,6 +5495,7 @@ name = "only-peers" version = "0.1.0" dependencies = [ "calimero-sdk", + "calimero-storage", ] [[package]] diff --git a/apps/gen-ext/Cargo.toml b/apps/gen-ext/Cargo.toml index a29fe22be..8d34c86c6 100644 --- a/apps/gen-ext/Cargo.toml +++ b/apps/gen-ext/Cargo.toml @@ -10,5 +10,6 @@ version = "0.1.0" crate-type = ["cdylib"] [dependencies] -calimero-sdk = {path = "../../crates/sdk"} +calimero-sdk = { path = "../../crates/sdk" } calimero-sdk-near = { path = "../../crates/sdk/libs/near" } +calimero-storage = { path = "../../crates/storage" } diff --git a/apps/kv-store/src/__private.rs b/apps/kv-store/src/__private.rs deleted file mode 100644 index 471b84c67..000000000 --- a/apps/kv-store/src/__private.rs +++ /dev/null @@ -1,85 +0,0 @@ -use calimero_sdk::borsh::{from_slice, to_vec}; -use calimero_sdk::{app, env}; -use calimero_storage::collections::unordered_map::{Entry, UnorderedMap}; -use calimero_storage::entities::Data; -use calimero_storage::integration::Comparison; -use calimero_storage::interface::{Action, Interface, StorageError}; -use calimero_storage::sync::{self, SyncArtifact}; - -use crate::KvStore; - -#[app::logic] -impl KvStore { - pub fn __calimero_sync_next() -> Result<(), StorageError> { - let args = env::input().expect("fatal: missing input"); - - let artifact = - from_slice::(&args).map_err(StorageError::DeserializationError)?; - - let this = Interface::root::()?; - - match artifact { - SyncArtifact::Actions(actions) => { - for action in actions { - let _ignored = match action { - Action::Add { type_id, .. } | Action::Update { type_id, .. } => { - match type_id { - 1 => Interface::apply_action::(action)?, - 254 => Interface::apply_action::>(action)?, - 255 => { - Interface::apply_action::>(action)? - } - _ => return Err(StorageError::UnknownType(type_id)), - } - } - Action::Delete { .. } => { - todo!("how are we supposed to identify the entity to delete???????") - } - Action::Compare { .. } => { - todo!("how are we supposed to compare when `Comparison` needs `type_id`???????") - } - }; - } - - if let Some(this) = this { - return Interface::commit_root(this); - } - } - SyncArtifact::Comparisons(comparisons) => { - if comparisons.is_empty() { - sync::push_comparison(Comparison { - type_id: ::type_id(), - data: this - .as_ref() - .map(to_vec) - .transpose() - .map_err(StorageError::SerializationError)?, - comparison_data: Interface::generate_comparison_data(this.as_ref())?, - }); - } - - for Comparison { - type_id, - data, - comparison_data, - } in comparisons - { - match type_id { - 1 => Interface::compare_affective::(data, comparison_data)?, - 254 => Interface::compare_affective::>( - data, - comparison_data, - )?, - 255 => Interface::compare_affective::>( - data, - comparison_data, - )?, - _ => return Err(StorageError::UnknownType(type_id)), - }; - } - } - } - - Ok(()) - } -} diff --git a/apps/kv-store/src/lib.rs b/apps/kv-store/src/lib.rs index ebe163683..807b0b521 100644 --- a/apps/kv-store/src/lib.rs +++ b/apps/kv-store/src/lib.rs @@ -2,22 +2,16 @@ use std::collections::BTreeMap; +use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize}; use calimero_sdk::types::Error; use calimero_sdk::{app, env}; use calimero_storage::collections::UnorderedMap; -use calimero_storage::entities::Element; -use calimero_storage::AtomicUnit; - -mod __private; #[app::state(emits = for<'a> Event<'a>)] -#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)] -#[root] -#[type_id(1)] +#[derive(Debug, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] +#[borsh(crate = "calimero_sdk::borsh")] pub struct KvStore { items: UnorderedMap, - #[storage] - storage: Element, } #[app::event] @@ -33,8 +27,7 @@ impl KvStore { #[app::init] pub fn init() -> KvStore { KvStore { - items: UnorderedMap::new().unwrap(), - storage: Element::root(), + items: UnorderedMap::new(), } } @@ -93,7 +86,10 @@ impl KvStore { app::emit!(Event::Removed { key }); - self.items.remove(key).map_err(Into::into) + self.items + .remove(key) + .map(|v| v.is_some()) + .map_err(Into::into) } pub fn clear(&mut self) -> Result<(), Error> { diff --git a/apps/only-peers/Cargo.toml b/apps/only-peers/Cargo.toml index cc3360bac..11b352580 100644 --- a/apps/only-peers/Cargo.toml +++ b/apps/only-peers/Cargo.toml @@ -11,3 +11,4 @@ crate-type = ["cdylib"] [dependencies] calimero-sdk = { path = "../../crates/sdk" } +calimero-storage = { path = "../../crates/storage" } diff --git a/apps/only-peers/src/lib.rs b/apps/only-peers/src/lib.rs index ded6f74b4..e8fa3d978 100644 --- a/apps/only-peers/src/lib.rs +++ b/apps/only-peers/src/lib.rs @@ -1,12 +1,14 @@ use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize}; use calimero_sdk::serde::Serialize; +use calimero_sdk::types::Error; use calimero_sdk::{app, env}; +use calimero_storage::collections::Vector; #[app::state(emits = for<'a> Event<'a>)] #[derive(BorshDeserialize, BorshSerialize, Default)] #[borsh(crate = "calimero_sdk::borsh")] pub struct OnlyPeers { - posts: Vec, + posts: Vector, } #[derive(BorshDeserialize, BorshSerialize, Default, Serialize)] @@ -16,10 +18,10 @@ pub struct Post { id: usize, title: String, content: String, - comments: Vec, + comments: Vector, } -#[derive(BorshDeserialize, BorshSerialize, Default, Serialize)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Default, Serialize)] #[borsh(crate = "calimero_sdk::borsh")] #[serde(crate = "calimero_sdk::serde")] pub struct Comment { @@ -48,40 +50,40 @@ impl OnlyPeers { OnlyPeers::default() } - pub fn post(&self, id: usize) -> Option<&Post> { + pub fn post(&self, id: usize) -> Result, Error> { env::log(&format!("Getting post with id: {:?}", id)); - self.posts.get(id) + Ok(self.posts.get(id)?) } - pub fn posts(&self) -> &[Post] { + pub fn posts(&self) -> Result, Error> { env::log("Getting all posts"); - &self.posts + Ok(self.posts.entries()?.collect()) } - pub fn create_post(&mut self, title: String, content: String) -> &Post { + pub fn create_post(&mut self, title: String, content: String) -> Result { env::log(&format!( "Creating post with title: {:?} and content: {:?}", title, content )); app::emit!(Event::PostCreated { - id: self.posts.len(), + id: self.posts.len()?, // todo! should we maybe only emit an ID, and let notified clients fetch the post? title: &title, content: &content, }); self.posts.push(Post { - id: self.posts.len(), + id: self.posts.len()?, title, content, - comments: Vec::new(), - }); + comments: Vector::new(), + })?; - match self.posts.last() { - Some(post) => post, + match self.posts.last()? { + Some(post) => Ok(post), None => env::unreachable(), } } @@ -91,13 +93,15 @@ impl OnlyPeers { post_id: usize, user: String, // todo! expose executor identity to app context text: String, - ) -> Option<&Comment> { + ) -> Result, Error> { env::log(&format!( "Creating comment under post with id: {:?} as user: {:?} with text: {:?}", post_id, user, text )); - let post = self.posts.get_mut(post_id)?; + let Some(mut post) = self.posts.get(post_id)? else { + return Ok(None); + }; app::emit!(Event::CommentCreated { post_id, @@ -106,8 +110,12 @@ impl OnlyPeers { text: &text, }); - post.comments.push(Comment { user, text }); + let comment = Comment { user, text }; + + post.comments.push(comment.clone())?; + + self.posts.update(post_id, post)?; - post.comments.last() + Ok(Some(comment)) } } diff --git a/apps/visited/src/lib.rs b/apps/visited/src/lib.rs index 563425900..22638befd 100644 --- a/apps/visited/src/lib.rs +++ b/apps/visited/src/lib.rs @@ -3,19 +3,15 @@ use std::result::Result; use calimero_sdk::app; +use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize}; use calimero_sdk::types::Error; use calimero_storage::collections::{UnorderedMap, UnorderedSet}; -use calimero_storage::entities::Element; -use calimero_storage::AtomicUnit; #[app::state] -#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)] -#[root] -#[type_id(1)] +#[derive(Debug, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] +#[borsh(crate = "calimero_sdk::borsh")] pub struct VisitedCities { visited: UnorderedMap>, - #[storage] - storage: Element, } #[app::logic] @@ -23,16 +19,12 @@ impl VisitedCities { #[app::init] pub fn init() -> VisitedCities { VisitedCities { - visited: UnorderedMap::new().unwrap(), - storage: Element::root(), + visited: UnorderedMap::new(), } } pub fn add_person(&mut self, person: String) -> Result { - Ok(self - .visited - .insert(person, UnorderedSet::new().unwrap())? - .is_some()) + Ok(self.visited.insert(person, UnorderedSet::new())?.is_some()) } pub fn add_visited_city(&mut self, person: String, city: String) -> Result { diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index fecb5e625..2aac51b4c 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -346,17 +346,12 @@ impl Node { let shared_key = SharedKey::from_sk(&sender_key); - let artifact = &shared_key + let artifact = shared_key .decrypt(artifact, nonce) .ok_or_eyre("failed to decrypt message")?; let Some(outcome) = self - .execute( - &mut context, - "apply_state_delta", - to_vec(&artifact)?, - author_id, - ) + .execute(&mut context, "__calimero_sync_next", artifact, author_id) .await? else { bail!("application not installed"); @@ -505,11 +500,13 @@ impl Node { })?; } - if let Err(err) = self - .send_state_delta(&context, &outcome, executor_public_key) - .await - { - error!(%err, "Failed to send state delta."); + if !outcome.artifact.is_empty() { + if let Err(err) = self + .send_state_delta(&context, &outcome, executor_public_key) + .await + { + error!(%err, "Failed to send state delta."); + } } Ok(outcome) @@ -544,7 +541,7 @@ impl Node { if outcome.returns.is_ok() { if let Some(root_hash) = outcome.root_hash { - if outcome.artifact.is_empty() { + if outcome.artifact.is_empty() && method != "__calimero_sync_next" { eyre::bail!("context state changed, but no actions were generated, discarding execution outcome to mitigate potential state inconsistency"); } diff --git a/crates/node/src/sync/state.rs b/crates/node/src/sync/state.rs index d723dc28b..ec8396171 100644 --- a/crates/node/src/sync/state.rs +++ b/crates/node/src/sync/state.rs @@ -107,6 +107,13 @@ impl Node { }; if root_hash == context.root_hash { + debug!( + context_id=%context.id, + our_identity=%our_identity, + their_identity=%their_identity, + "Root hashes match, up to date", + ); + return Ok(()); } @@ -218,6 +225,13 @@ impl Node { .await?; if their_root_hash == context.root_hash { + debug!( + context_id=%context.id, + our_identity=%our_identity, + their_identity=%their_identity, + "Root hashes match, up to date", + ); + return Ok(()); } @@ -308,7 +322,9 @@ impl Node { "State sync outcome", ); - let our_new_nonce = thread_rng().gen::(); + let our_new_nonce = (!outcome.artifact.is_empty()) + .then(|| thread_rng().gen()) + .unwrap_or_default(); send( stream, @@ -323,6 +339,10 @@ impl Node { ) .await?; + if our_new_nonce == [0; 12] { + break; + } + our_nonce = our_new_nonce; } diff --git a/crates/sdk/macros/src/logic/method.rs b/crates/sdk/macros/src/logic/method.rs index 4f3b83e2d..57b508b16 100644 --- a/crates/sdk/macros/src/logic/method.rs +++ b/crates/sdk/macros/src/logic/method.rs @@ -1,5 +1,6 @@ use proc_macro2::TokenStream; -use quote::{quote, ToTokens}; +use quote::{quote, quote_spanned, ToTokens}; +use syn::spanned::Spanned; use syn::{Error as SynError, GenericParam, Ident, ImplItemFn, Path, ReturnType, Visibility}; use crate::errors::{Errors, ParseError}; @@ -61,12 +62,14 @@ impl ToTokens for PublicLogicMethod<'_> { let input_lifetime = if self.has_refs { let lifetime = lifetimes::input(); - quote! { <#lifetime> } + quote_spanned! { name.span()=> + <#lifetime> + } } else { quote! {} }; - quote! { + quote_spanned! {name.span()=> #[derive(::calimero_sdk::serde::Deserialize)] #[serde(crate = "::calimero_sdk::serde")] struct #input_ident #input_lifetime { @@ -94,41 +97,44 @@ impl ToTokens for PublicLogicMethod<'_> { let (def, mut call) = match &self.self_type { Some(type_) => ( { - let mutability = match type_ { - SelfType::Mutable(_) => Some(quote! {mut}), - SelfType::Owned(_) | SelfType::Immutable(_) => None, + let (mutability, ty) = match type_ { + SelfType::Mutable(ty) => (Some(quote! {mut}), ty), + SelfType::Owned(ty) | SelfType::Immutable(ty) => (None, ty), }; - quote! { - let Some(#mutability app) = ::calimero_storage::interface::Interface::root::<#self_>().ok().flatten() + quote_spanned! {ty.span()=> + let Some(#mutability app) = ::calimero_storage::collections::Root::<#self_>::fetch() else { ::calimero_sdk::env::panic_str("Failed to find or read app state") }; } }, - quote! { app.#name(#(#arg_idents),*); }, + quote_spanned! {name.span()=> + app.#name(#(#arg_idents),*) + }, ), None => ( if init_method { - quote! { - if let Some(mut app) = ::calimero_storage::interface::Interface::root::<#self_>().ok().flatten() { + quote_spanned! {name.span()=> + if ::calimero_storage::collections::Root::<#self_>::fetch().is_some() { ::calimero_sdk::env::panic_str("Cannot initialize over already existing state.") }; - let mut app: #self_ = + let app = } } else { quote! {} }, - quote! { <#self_>::#name(#(#arg_idents),*); }, + quote_spanned! {name.span()=> + <#self_>::#name(#(#arg_idents),*) + }, ), }; - if let (Some(_), false) = (&self.ret, init_method) { - //only when it's not init - call = quote! { + if let (Some(ret), false) = (&self.ret, init_method) { + call = quote_spanned! {ret.ty.span()=> let output = #call; let output = { - #[expect(unused_imports)] + #[allow(unused_imports)] use ::calimero_sdk::__private::IntoResult; match ::calimero_sdk::__private::WrappedReturn::new(output) .into_result() @@ -140,22 +146,34 @@ impl ToTokens for PublicLogicMethod<'_> { ), } }; - ::calimero_sdk::env::value_return(&output); - }; + ::calimero_sdk::env::value_return(&output) + } } let state_finalizer = match (&self.self_type, init_method) { (Some(SelfType::Mutable(_)), _) | (_, true) => quote! { - if let Err(_) = ::calimero_storage::interface::Interface::commit_root(app) { - ::calimero_sdk::env::panic_str("Failed to commit app state") - } + app.commit(); }, _ => quote! {}, }; // todo! when generics are present, strip them let init_impl = if init_method { - quote! { + call = quote_spanned! {name.span()=> + ::calimero_storage::collections::Root::new(|| #call) + }; + + quote_spanned! {name.span()=> + #[cfg(target_arch = "wasm32")] + #[no_mangle] + pub extern "C" fn __calimero_sync_next() { + let Some(args) = ::calimero_sdk::env::input() else { + ::calimero_sdk::env::panic_str("Expected payload to sync method.") + }; + + ::calimero_storage::collections::Root::<#self_>::sync(&args).expect("fatal: sync failed"); + } + impl ::calimero_sdk::state::AppStateInit for #self_ { type Return = #ret; } @@ -164,7 +182,7 @@ impl ToTokens for PublicLogicMethod<'_> { quote! {} }; - quote! { + quote_spanned! {name.span()=> #[cfg(target_arch = "wasm32")] #[no_mangle] pub extern "C" fn #name() { @@ -176,7 +194,7 @@ impl ToTokens for PublicLogicMethod<'_> { #def - #call + #call; #state_finalizer } diff --git a/crates/storage-macros/src/lib.rs b/crates/storage-macros/src/lib.rs index 4cd1edcfa..3b6750bc1 100644 --- a/crates/storage-macros/src/lib.rs +++ b/crates/storage-macros/src/lib.rs @@ -1,7 +1,7 @@ use borsh as _; use proc_macro::TokenStream; -use quote::{format_ident, quote}; -use syn::{parse_macro_input, Data, DeriveInput, Fields, LitInt, Type}; +use quote::quote; +use syn::{parse_macro_input, Data, DeriveInput, Fields, Type}; #[cfg(test)] mod integration_tests_package_usage { @@ -95,8 +95,9 @@ mod integration_tests_package_usage { /// ``` /// use calimero_storage::entities::Element; /// use calimero_storage_macros::AtomicUnit; +/// use borsh::{BorshSerialize, BorshDeserialize}; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// #[type_id(43)] /// struct Page { /// title: String, @@ -110,8 +111,9 @@ mod integration_tests_package_usage { /// ``` /// use calimero_storage::entities::Element; /// use calimero_storage_macros::{AtomicUnit, Collection}; +/// use borsh::{BorshSerialize, BorshDeserialize}; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// #[type_id(44)] /// struct Person { /// name: String, @@ -160,15 +162,6 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { let name = &input.ident; let where_clause = input.generics.make_where_clause().clone(); let (impl_generics, ty_generics, _) = input.generics.split_for_impl(); - let is_root = input.attrs.iter().any(|attr| attr.path().is_ident("root")); - let type_id = input - .attrs - .iter() - .find(|attr| attr.path().is_ident("type_id")) - .and_then(|attr| attr.parse_args::().ok()) - .expect("AtomicUnit derive requires a #[type_id(n)] attribute, where n is a u8") - .base10_parse::() - .expect("type_id must be a valid u8"); let fields = match &input.data { Data::Struct(data) => &data.fields, @@ -189,73 +182,6 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { .expect("You must designate one field with #[storage] for the Element"); let storage_ident = storage_field.ident.as_ref().unwrap(); - let storage_ty = &storage_field.ty; - - let field_implementations = named_fields - .iter() - .filter_map(|f| { - let ident = f.ident.as_ref().unwrap(); - let ty = &f.ty; - - let private = f.attrs.iter().any(|attr| attr.path().is_ident("private")); - let skip = f.attrs.iter().any(|attr| attr.path().is_ident("skip")); - - if skip || ident == storage_ident { - None - } else { - let setter = format_ident!("set_{}", ident); - - let setter_action = if private { - quote! { - self.#ident = value; - } - } else { - quote! { - self.#ident = value; - self.#storage_ident.update(); - } - }; - - Some(quote! { - #[doc = "Setter for the "] - #[doc = stringify!(#ident)] - #[doc = " field."] - pub fn #setter(&mut self, value: #ty) -> bool { - if self.#ident == value { - false - } else { - #setter_action - true - } - } - }) - } - }) - .collect::>(); - - let serializable_fields: Vec<_> = named_fields - .iter() - .filter(|f| { - !f.attrs - .iter() - .any(|attr| attr.path().is_ident("skip") || attr.path().is_ident("private")) - && f.ident.as_ref().unwrap() != storage_ident - }) - .map(|f| f.ident.as_ref().unwrap()) - .collect(); - - let regular_fields: Vec<_> = named_fields - .iter() - .filter(|f| { - !f.attrs.iter().any(|attr| { - attr.path().is_ident("skip") - || attr.path().is_ident("private") - || attr.path().is_ident("collection") - || attr.path().is_ident("storage") - }) - }) - .map(|f| f.ident.as_ref().unwrap()) - .collect(); let collection_fields: Vec<_> = named_fields .iter() @@ -267,130 +193,25 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { .map(|f| f.ident.as_ref().unwrap()) .collect(); - let collection_field_types: Vec<_> = named_fields - .iter() - .filter(|f| { - f.attrs - .iter() - .any(|attr| attr.path().is_ident("collection")) - }) - .map(|f| f.ty.clone()) - .collect(); - - let skipped_fields: Vec<_> = named_fields - .iter() - .filter(|f| { - f.attrs - .iter() - .any(|attr| attr.path().is_ident("skip") || attr.path().is_ident("private")) - }) - .map(|f| f.ident.as_ref().unwrap()) - .collect(); - - let mut data_where_clause = where_clause.clone(); + let mut serde_where_clause = where_clause.clone(); for ty in input.generics.type_params() { let ident = &ty.ident; - data_where_clause.predicates.push(syn::parse_quote!( + serde_where_clause.predicates.push(syn::parse_quote!( #ident: calimero_sdk::borsh::BorshSerialize + calimero_sdk::borsh::BorshDeserialize )); } - let deserialize_impl = quote! { - impl #impl_generics calimero_sdk::borsh::BorshDeserialize for #name #ty_generics #data_where_clause { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - let #storage_ident = #storage_ty::deserialize_reader(reader)?; - Ok(Self { - #storage_ident, - #(#serializable_fields: calimero_sdk::borsh::BorshDeserialize::deserialize_reader(reader)?,)* - #(#skipped_fields: Default::default(),)* - }) - } - } - }; - - let serialize_impl = quote! { - impl #impl_generics calimero_sdk::borsh::BorshSerialize for #name #ty_generics #data_where_clause { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - calimero_sdk::borsh::BorshSerialize::serialize(&self.#storage_ident, writer)?; - #(calimero_sdk::borsh::BorshSerialize::serialize(&self.#serializable_fields, writer)?;)* - Ok(()) - } - } - }; - - let root_impl = if is_root { - quote! { - fn is_root() -> bool { - true - } - } - } else { - quote! { - fn is_root() -> bool { - false - } - } - }; - - let mut local_where_clause = where_clause; - - for ty in input.generics.type_params() { - let ident = &ty.ident; - local_where_clause.predicates.push(syn::parse_quote!( - #ident: PartialEq - )); - } - let expanded = quote! { - impl #impl_generics #name #ty_generics #local_where_clause { - #(#field_implementations)* - } - - impl #impl_generics calimero_storage::entities::Data for #name #ty_generics #data_where_clause { - fn calculate_merkle_hash(&self) -> Result<[u8; 32], calimero_storage::interface::StorageError> { - use calimero_storage::exports::Digest; - let mut hasher = calimero_storage::exports::Sha256::new(); - hasher.update(self.element().id().as_bytes()); - #( - hasher.update( - &calimero_sdk::borsh::to_vec(&self.#regular_fields) - .map_err(calimero_storage::interface::StorageError::SerializationError)? - ); - )* - hasher.update( - &calimero_sdk::borsh::to_vec(&self.element().metadata()) - .map_err(calimero_storage::interface::StorageError::SerializationError)? - ); - Ok(hasher.finalize().into()) - } - - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - slice: &[u8], - ) -> Result<[u8; 32], calimero_storage::interface::StorageError> { - use calimero_sdk::borsh::BorshDeserialize; - match collection { - #( - stringify!(#collection_fields) => { - let child = <#collection_field_types #ty_generics as calimero_storage::entities::Collection>::Child::try_from_slice(slice) - .map_err(|e| calimero_storage::interface::StorageError::DeserializationError(e))?; - child.calculate_merkle_hash() - }, - )* - _ => Err(calimero_storage::interface::StorageError::UnknownCollectionType(collection.to_owned())), - } - } - + impl #impl_generics calimero_storage::entities::Data for #name #ty_generics #serde_where_clause { fn collections(&self) -> std::collections::BTreeMap> { use calimero_storage::entities::Collection; let mut collections = std::collections::BTreeMap::new(); #( collections.insert( stringify!(#collection_fields).to_owned(), - calimero_storage::interface::Interface::child_info_for(self.id(), &self.#collection_fields).unwrap_or_default() + calimero_storage::interface::MainInterface::child_info_for(self.id(), &self.#collection_fields).unwrap_or_default() ); )* collections @@ -403,22 +224,17 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { fn element_mut(&mut self) -> &mut calimero_storage::entities::Element { &mut self.#storage_ident } - - #root_impl - - fn type_id() -> u8 { - #type_id - } } - impl #impl_generics calimero_storage::entities::AtomicUnit for #name #ty_generics #data_where_clause {} - - #deserialize_impl - - #serialize_impl + impl #impl_generics calimero_storage::entities::AtomicUnit for #name #ty_generics #serde_where_clause {} }; - TokenStream::from(expanded) + TokenStream::from(quote! { + #[allow(unused_mut)] + const _: () = { + #expanded + }; + }) } /// Derives the [`Collection`](calimero_storage::entities::Collection) trait for @@ -459,9 +275,9 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { /// ``` /// use calimero_storage_macros::{AtomicUnit, Collection}; /// use calimero_storage::entities::{Data, Element}; +/// use borsh::{BorshSerialize, BorshDeserialize}; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -/// #[type_id(42)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// struct Book { /// title: String, /// pages: Pages, @@ -473,8 +289,7 @@ pub fn atomic_unit_derive(input: TokenStream) -> TokenStream { /// #[children(Page)] /// struct Pages; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -/// #[type_id(43)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// struct Page { /// content: String, /// #[storage] @@ -540,7 +355,7 @@ pub fn collection_derive(input: TokenStream) -> TokenStream { let data = (!data.is_empty()).then(|| quote! { { #(#data)* } }); let default_impl = quote! { - impl #impl_generics ::core::default::Default for #name #ty_generics #where_clause { + impl #impl_generics ::core::default::Default for #name #ty_generics { fn default() -> Self { Self #data } diff --git a/crates/storage-macros/tests/atomic_unit.rs b/crates/storage-macros/tests/atomic_unit.rs deleted file mode 100644 index 08b0508e8..000000000 --- a/crates/storage-macros/tests/atomic_unit.rs +++ /dev/null @@ -1,335 +0,0 @@ -#![allow(unused_crate_dependencies, reason = "Creates a lot of noise")] -// Lints specifically disabled for integration tests -#![allow( - non_snake_case, - unreachable_pub, - clippy::cast_lossless, - clippy::cast_precision_loss, - clippy::cognitive_complexity, - clippy::default_numeric_fallback, - clippy::exhaustive_enums, - clippy::exhaustive_structs, - clippy::expect_used, - clippy::indexing_slicing, - clippy::let_underscore_must_use, - clippy::let_underscore_untyped, - clippy::missing_assert_message, - clippy::missing_panics_doc, - clippy::mod_module_files, - clippy::must_use_candidate, - clippy::panic, - clippy::print_stdout, - clippy::tests_outside_test_module, - clippy::unwrap_in_result, - clippy::unwrap_used, - reason = "Not useful in tests" -)] - -use borsh::{to_vec, BorshDeserialize}; -use calimero_storage::address::Path; -use calimero_storage::entities::{Data, Element}; -use calimero_storage::exports::{Digest, Sha256}; -use calimero_storage::interface::Interface; -use calimero_storage_macros::AtomicUnit; - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[root] -#[type_id(1)] -struct Private { - public: String, - #[private] - private: String, - #[storage] - storage: Element, -} - -impl Private { - fn new(path: &Path) -> Self { - Self { - public: String::new(), - private: String::new(), - storage: Element::new(path, None), - } - } -} - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[root] -#[type_id(2)] -struct Simple { - name: String, - value: i32, - #[storage] - storage: Element, -} - -impl Simple { - fn new(path: &Path) -> Self { - Self { - name: String::new(), - value: 0, - storage: Element::new(path, None), - } - } -} - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[root] -#[type_id(3)] -struct Skipped { - included: String, - #[skip] - skipped: String, - #[storage] - storage: Element, -} - -impl Skipped { - fn new(path: &Path) -> Self { - Self { - included: String::new(), - skipped: String::new(), - storage: Element::new(path, None), - } - } -} - -#[cfg(test)] -mod basics { - use super::*; - - #[test] - fn creation() { - let path = Path::new("::root::node").unwrap(); - let unit = Simple::new(&path); - - assert_eq!(unit.path(), path); - assert_eq!(unit.element().path(), path); - assert!(unit.element().is_dirty()); - } - - #[test] - fn getters() { - let path = Path::new("::root::node").unwrap(); - let unit = Simple::new(&path); - - assert_eq!(unit.name, ""); - assert_eq!(unit.value, 0); - } - - #[test] - fn setters__set() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - - _ = unit.set_name("Test Name".to_owned()); - _ = unit.set_value(42); - - assert_eq!(unit.name, "Test Name"); - assert_eq!(unit.value, 42); - } - - #[test] - fn setters__confirm_set() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - assert_ne!(unit.name, "Test Name"); - assert_ne!(unit.value, 42); - - assert!(unit.set_name("Test Name".to_owned())); - assert!(unit.set_value(42)); - assert_eq!(unit.name, "Test Name"); - assert_eq!(unit.value, 42); - } - - #[test] - fn setters__confirm_not_set() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - assert_ne!(unit.name, "Test Name"); - assert_ne!(unit.value, 42); - - assert!(unit.set_name("Test Name".to_owned())); - assert!(unit.set_value(42)); - assert_eq!(unit.name, "Test Name"); - assert_eq!(unit.value, 42); - assert!(!unit.set_name("Test Name".to_owned())); - assert!(!unit.set_value(42)); - } - - #[test] - fn setters__confirm_set_dirty() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - assert!(Interface::save(&mut unit).unwrap()); - assert!(!unit.element().is_dirty()); - - assert!(unit.set_name("Test Name".to_owned())); - assert!(unit.element().is_dirty()); - } - - #[test] - fn setters__confirm_not_set_not_dirty() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - assert!(Interface::save(&mut unit).unwrap()); - assert!(!unit.element().is_dirty()); - - assert!(unit.set_name("Test Name".to_owned())); - assert!(unit.element().is_dirty()); - assert!(Interface::save(&mut unit).unwrap()); - assert!(!unit.set_name("Test Name".to_owned())); - assert!(!unit.element().is_dirty()); - } -} - -#[cfg(test)] -mod visibility { - use super::*; - - #[test] - fn private_field() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Private::new(&path); - - _ = unit.set_public("Public".to_owned()); - _ = unit.set_private("Private".to_owned()); - - let serialized = to_vec(&unit).unwrap(); - let deserialized = Private::try_from_slice(&serialized).unwrap(); - - assert_eq!(unit.public, deserialized.public); - assert_ne!(unit.private, deserialized.private); - assert_eq!(deserialized.private, ""); - } - - #[test] - fn public_field() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - - _ = unit.set_name("Public".to_owned()); - - let serialized = to_vec(&unit).unwrap(); - let deserialized = Simple::try_from_slice(&serialized).unwrap(); - - assert_eq!(unit.name, deserialized.name); - } - - #[test] - fn skipped_field() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Skipped::new(&path); - - _ = unit.set_included("Public".to_owned()); - // Skipping fields also skips the setters - // _ = unit.set_skipped("Skipped".to_owned()); - unit.skipped = "Skipped".to_owned(); - - let serialized = to_vec(&unit).unwrap(); - let deserialized = Skipped::try_from_slice(&serialized).unwrap(); - - assert_eq!(unit.included, deserialized.included); - // Skipping fields also skips the getters - // assert_ne!(unit.skipped(), deserialized.skipped()); - assert_ne!(unit.skipped, deserialized.skipped); - assert_eq!(deserialized.skipped, ""); - } -} - -#[cfg(test)] -mod hashing { - use super::*; - - #[test] - fn private_field() { - let path = Path::new("::root::node::leaf").unwrap(); - let mut unit = Private::new(&path); - - _ = unit.set_public("Public".to_owned()); - _ = unit.set_private("Private".to_owned()); - - let mut hasher = Sha256::new(); - hasher.update(unit.id().as_bytes()); - hasher.update(to_vec(&unit.public).unwrap()); - hasher.update(to_vec(&unit.element().metadata()).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash); - - _ = unit.set_private("Test 1".to_owned()); - assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash); - - _ = unit.set_public("Test 2".to_owned()); - assert_ne!(unit.calculate_merkle_hash().unwrap(), expected_hash); - } - - #[test] - fn public_field() { - let path = Path::new("::root::node::leaf").unwrap(); - let mut unit = Simple::new(&path); - - _ = unit.set_name("Public".to_owned()); - _ = unit.set_value(42); - - let mut hasher = Sha256::new(); - hasher.update(unit.id().as_bytes()); - hasher.update(to_vec(&unit.name).unwrap()); - hasher.update(to_vec(&unit.value).unwrap()); - hasher.update(to_vec(&unit.element().metadata()).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash); - } - - #[test] - fn skipped_field() { - let path = Path::new("::root::node::leaf").unwrap(); - let mut unit = Skipped::new(&path); - - _ = unit.set_included("Public".to_owned()); - // Skipping fields also skips the setters - // _ = unit.set_skipped("Skipped".to_owned()); - unit.skipped = "Skipped".to_owned(); - - let mut hasher = Sha256::new(); - hasher.update(unit.id().as_bytes()); - hasher.update(to_vec(&unit.included).unwrap()); - hasher.update(to_vec(&unit.element().metadata()).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash); - - unit.skipped = "Test 1".to_owned(); - assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash); - - _ = unit.set_included("Test 2".to_owned()); - assert_ne!(unit.calculate_merkle_hash().unwrap(), expected_hash); - } -} - -#[cfg(test)] -mod traits { - use super::*; - - #[test] - fn serialization_and_deserialization() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - - _ = unit.set_name("Test Name".to_owned()); - _ = unit.set_value(42); - - let serialized = to_vec(&unit).unwrap(); - let deserialized = Simple::try_from_slice(&serialized).unwrap(); - - assert_eq!(unit, deserialized); - assert_eq!(unit.id(), deserialized.id()); - assert_eq!(unit.name, deserialized.name); - assert_eq!(unit.path(), deserialized.path()); - assert_eq!(unit.value, deserialized.value); - assert_eq!(unit.element().id(), deserialized.element().id()); - assert_eq!(unit.element().path(), deserialized.element().path()); - assert_eq!(unit.element().metadata(), deserialized.element().metadata()); - } -} diff --git a/crates/storage-macros/tests/collection.rs b/crates/storage-macros/tests/collection.rs deleted file mode 100644 index 7145ef22a..000000000 --- a/crates/storage-macros/tests/collection.rs +++ /dev/null @@ -1,184 +0,0 @@ -#![allow(unused_crate_dependencies, reason = "Creates a lot of noise")] -// Lints specifically disabled for integration tests -#![allow( - non_snake_case, - unreachable_pub, - clippy::cast_lossless, - clippy::cast_precision_loss, - clippy::cognitive_complexity, - clippy::default_numeric_fallback, - clippy::exhaustive_enums, - clippy::exhaustive_structs, - clippy::expect_used, - clippy::indexing_slicing, - clippy::let_underscore_must_use, - clippy::let_underscore_untyped, - clippy::missing_assert_message, - clippy::missing_panics_doc, - clippy::mod_module_files, - clippy::must_use_candidate, - clippy::panic, - clippy::print_stdout, - clippy::tests_outside_test_module, - clippy::unwrap_in_result, - clippy::unwrap_used, - reason = "Not useful in tests" -)] - -use borsh::{to_vec, BorshDeserialize}; -use calimero_storage::address::Path; -use calimero_storage::entities::{Data, Element}; -use calimero_storage::exports::{Digest, Sha256}; -use calimero_storage_macros::{AtomicUnit, Collection}; - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(22)] -struct Child { - content: String, - #[storage] - storage: Element, -} - -impl Child { - fn new(path: &Path) -> Self { - Self { - content: String::new(), - storage: Element::new(path, None), - } - } -} - -#[derive(Collection, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[children(Child)] -struct Group; - -impl Group { - const fn new() -> Self { - Self {} - } -} - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(21)] -#[root] -struct Parent { - title: String, - #[collection] - children: Group, - #[storage] - storage: Element, -} - -impl Parent { - fn new(path: &Path) -> Self { - Self { - title: String::new(), - children: Group::new(), - storage: Element::new(path, None), - } - } -} - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(23)] -#[root] -struct Simple { - name: String, - value: i32, - #[storage] - storage: Element, -} - -impl Simple { - fn new(path: &Path) -> Self { - Self { - name: String::new(), - value: 0, - storage: Element::new(path, None), - } - } -} - -#[cfg(test)] -mod hierarchy { - use super::*; - - #[test] - fn parent_child() { - let parent_path = Path::new("::root::node").unwrap(); - let mut parent = Parent::new(&parent_path); - _ = parent.set_title("Parent Title".to_owned()); - - let child_path = Path::new("::root::node::leaf").unwrap(); - let mut child = Child::new(&child_path); - _ = child.set_content("Child Content".to_owned()); - - assert_eq!(parent.title, "Parent Title"); - - // TODO: Add in tests for loading and checking children - } - - #[test] - fn compile_fail() { - trybuild::TestCases::new().compile_fail("tests/compile_fail/collection.rs"); - } -} - -#[cfg(test)] -mod hashing { - use super::*; - - #[test] - fn calculate_merkle_hash__child() { - let mut child = Child::new(&Path::new("::root::node::leaf").unwrap()); - _ = child.set_content("Child Content".to_owned()); - - let mut hasher = Sha256::new(); - hasher.update(child.id().as_bytes()); - hasher.update(to_vec(&child.content).unwrap()); - hasher.update(to_vec(&child.element().metadata()).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(child.calculate_merkle_hash().unwrap(), expected_hash); - } - - #[test] - fn calculate_merkle_hash__parent() { - let mut parent = Parent::new(&Path::new("::root::node").unwrap()); - _ = parent.set_title("Parent Title".to_owned()); - - let mut hasher = Sha256::new(); - hasher.update(parent.id().as_bytes()); - hasher.update(to_vec(&parent.title).unwrap()); - hasher.update(to_vec(&parent.element().metadata()).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(parent.calculate_merkle_hash().unwrap(), expected_hash); - } -} - -#[cfg(test)] -mod traits { - use super::*; - - #[test] - fn serialization_and_deserialization() { - let path = Path::new("::root::node").unwrap(); - let mut unit = Simple::new(&path); - - _ = unit.set_name("Test Name".to_owned()); - _ = unit.set_value(42); - - let serialized = to_vec(&unit).unwrap(); - let deserialized = Simple::try_from_slice(&serialized).unwrap(); - - assert_eq!(unit, deserialized); - assert_eq!(unit.id(), deserialized.id()); - assert_eq!(unit.name, deserialized.name); - assert_eq!(unit.path(), deserialized.path()); - assert_eq!(unit.value, deserialized.value); - assert_eq!(unit.element().id(), deserialized.element().id()); - assert_eq!(unit.element().path(), deserialized.element().path()); - assert_eq!(unit.element().metadata(), deserialized.element().metadata()); - } -} diff --git a/crates/storage-macros/tests/compile_fail/collection.rs b/crates/storage-macros/tests/compile_fail/collection.rs deleted file mode 100644 index 2c1ecc34a..000000000 --- a/crates/storage-macros/tests/compile_fail/collection.rs +++ /dev/null @@ -1,37 +0,0 @@ -use calimero_storage::address::Path; -use calimero_storage::entities::{Data, Element}; -use calimero_storage::interface::Interface; -use calimero_storage_macros::{AtomicUnit, Collection}; - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(2)] -struct Child { - #[storage] - storage: Element, -} - -#[derive(Collection, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[children(Child)] -struct Group; - -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[root] -#[type_id(1)] -struct Parent { - group: Group, - #[storage] - storage: Element, -} - -fn main() { - fn child_type_specification() { - let parent: Parent = Parent { - group: Group {}, - storage: Element::new(&Path::new("::root::node").unwrap(), None), - }; - let _: Vec = Interface::children_of(parent.id(), &parent.group).unwrap(); - - // This should fail to compile if the child type is incorrect - let _: Vec = Interface::children_of(parent.id(), &parent.group).unwrap(); - } -} diff --git a/crates/storage-macros/tests/compile_fail/collection.stderr b/crates/storage-macros/tests/compile_fail/collection.stderr deleted file mode 100644 index eeb500f05..000000000 --- a/crates/storage-macros/tests/compile_fail/collection.stderr +++ /dev/null @@ -1,10 +0,0 @@ -error[E0308]: mismatched types - --> tests/compile_fail/collection.rs:35:30 - | -35 | let _: Vec = Interface::children_of(parent.id(), &parent.group).unwrap(); - | ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Vec`, found `Vec` - | | - | expected due to this - | - = note: expected struct `Vec` - found struct `Vec` diff --git a/crates/storage/src/address.rs b/crates/storage/src/address.rs index b8eb8e7e9..4ad9cc3f2 100644 --- a/crates/storage/src/address.rs +++ b/crates/storage/src/address.rs @@ -13,7 +13,6 @@ use core::fmt::{self, Debug, Display, Formatter}; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write}; use borsh::{BorshDeserialize, BorshSerialize}; -use calimero_sdk::serde::{Deserialize, Serialize}; use fixedstr::Flexstr; use thiserror::Error as ThisError; @@ -40,15 +39,13 @@ use crate::env::{context_id, random_bytes}; Copy, Clone, Debug, - Deserialize, Eq, PartialEq, Hash, Ord, PartialOrd, - Serialize, + Default, )] -#[serde(crate = "calimero_sdk::serde")] pub struct Id { /// The byte array representation of the ID. bytes: [u8; 32], @@ -106,6 +103,11 @@ impl Id { pub const fn as_bytes(&self) -> &[u8; 32] { &self.bytes } + + /// Checks if the ID is the root. + pub fn is_root(&self) -> bool { + self.bytes == context_id() + } } impl Display for Id { diff --git a/crates/storage/src/collections.rs b/crates/storage/src/collections.rs index 1122bb681..4b79e22a3 100644 --- a/crates/storage/src/collections.rs +++ b/crates/storage/src/collections.rs @@ -1,14 +1,412 @@ //! High-level data structures for storage. -/// This module provides functionality for hashmap data structures. +use core::fmt; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; +use std::ptr; +use std::sync::LazyLock; + +use borsh::{BorshDeserialize, BorshSerialize}; +use indexmap::IndexSet; + pub mod unordered_map; pub use unordered_map::UnorderedMap; -/// This module provides functionality for hashset data structures. pub mod unordered_set; pub use unordered_set::UnorderedSet; -/// This module provides functionality for vector data structures. pub mod vector; pub use vector::Vector; -/// This module provides functionality for handling errors. +mod root; +#[doc(hidden)] +pub use root::Root; pub mod error; pub use error::StoreError; + +// fixme! macro expects `calimero_storage` to be in deps +use crate as calimero_storage; +use crate::address::{Id, Path}; +use crate::entities::{ChildInfo, Data, Element}; +use crate::interface::{Interface, StorageError}; +use crate::store::{MainStorage, StorageAdaptor}; +use crate::{AtomicUnit, Collection}; + +mod compat { + use std::collections::BTreeMap; + + use borsh::{BorshDeserialize, BorshSerialize}; + + use crate::entities::{ChildInfo, Collection, Data, Element}; + + /// Thing. + #[derive(Copy, Clone, Debug)] + pub(super) struct RootHandle; + + #[derive(BorshSerialize, BorshDeserialize)] + pub(super) struct RootChildDud; + + impl Data for RootChildDud { + fn collections(&self) -> BTreeMap> { + unimplemented!() + } + + fn element(&self) -> &Element { + unimplemented!() + } + + fn element_mut(&mut self) -> &mut Element { + unimplemented!() + } + } + + impl Collection for RootHandle { + type Child = RootChildDud; + + fn name(&self) -> &str { + "no collection, remove this nonsense" + } + } +} + +use compat::RootHandle; + +#[derive(BorshSerialize, BorshDeserialize)] +struct Collection { + storage: Element, + + #[borsh(skip)] + children_ids: RefCell>>, + + #[borsh(skip)] + _priv: PhantomData<(T, S)>, +} + +impl Data for Collection { + fn collections(&self) -> BTreeMap> { + BTreeMap::new() + } + + fn element(&self) -> &Element { + &self.storage + } + + fn element_mut(&mut self) -> &mut Element { + &mut self.storage + } +} + +/// A collection of entries in a map. +#[derive(Collection, Copy, Clone, Debug, Eq, PartialEq)] +#[children(Entry)] +struct Entries { + /// Helper to associate the generic types with the collection. + _priv: PhantomData, +} + +/// An entry in a map. +#[derive(AtomicUnit, BorshSerialize, BorshDeserialize, Clone, Debug)] +struct Entry { + /// The item in the entry. + item: T, + /// The storage element for the entry. + #[storage] + storage: Element, +} + +#[expect(unused_qualifications, reason = "AtomicUnit macro is unsanitized")] +type StoreResult = std::result::Result; + +static ROOT_ID: LazyLock = LazyLock::new(|| Id::root()); + +impl Collection { + /// Creates a new collection. + #[expect(clippy::expect_used, reason = "fatal error if it happens")] + fn new(id: Option) -> Self { + let id = id.unwrap_or_else(|| Id::random()); + + let mut this = Self { + children_ids: RefCell::new(None), + storage: Element::new(&Path::new("::unused").expect("valid path"), Some(id)), + _priv: PhantomData, + }; + + if id.is_root() { + let _ignored = >::save(&mut this).expect("save"); + } else { + let _ = + >::add_child_to(*ROOT_ID, &RootHandle, &mut this).expect("add child"); + } + + this + } + + /// Inserts an item into the collection. + fn insert(&mut self, id: Option, item: T) -> StoreResult { + let path = self.path(); + + let mut collection = CollectionMut::new(self); + + let mut entry = Entry { + item, + storage: Element::new(&path, id), + }; + + collection.insert(&mut entry)?; + + Ok(entry.item) + } + + #[inline(never)] + fn get(&self, id: Id) -> StoreResult> { + let entry = >::find_by_id::>(id)?; + + Ok(entry.map(|entry| entry.item)) + } + + fn get_mut(&mut self, id: Id) -> StoreResult>> { + let entry = >::find_by_id::>(id)?; + + Ok(entry.map(|entry| EntryMut { + collection: CollectionMut::new(self), + entry, + })) + } + + fn len(&self) -> StoreResult { + Ok(self.children_cache()?.len()) + } + + fn entries( + &self, + ) -> StoreResult> + DoubleEndedIterator + '_> { + let iter = self.children_cache()?.iter().copied().map(|child| { + let entry = >::find_by_id::>(child)? + .ok_or(StoreError::StorageError(StorageError::NotFound(child)))?; + + Ok(entry.item) + }); + + Ok(iter) + } + + fn nth(&self, index: usize) -> StoreResult> { + Ok(self.children_cache()?.get_index(index).copied()) + } + + fn last(&self) -> StoreResult> { + Ok(self.children_cache()?.last().copied()) + } + + fn clear(&mut self) -> StoreResult<()> { + let mut collection = CollectionMut::new(self); + + collection.clear()?; + + Ok(()) + } + + #[expect( + clippy::unwrap_in_result, + clippy::expect_used, + reason = "fatal error if it happens" + )] + fn children_cache(&self) -> StoreResult<&mut IndexSet> { + let mut cache = self.children_ids.borrow_mut(); + + if cache.is_none() { + let children = >::child_info_for(self.id(), &RootHandle)?; + + let children = children.into_iter().map(|c| c.id()).collect(); + + *cache = Some(children); + } + + let children = cache.as_mut().expect("children"); + + #[expect(unsafe_code, reason = "necessary for caching")] + let children = unsafe { &mut *ptr::from_mut(children) }; + + Ok(children) + } +} + +#[derive(Debug)] +struct EntryMut<'a, T: BorshSerialize + BorshDeserialize, S: StorageAdaptor> { + collection: CollectionMut<'a, T, S>, + entry: Entry, +} + +impl EntryMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn remove(self) -> StoreResult { + let old = self + .collection + .get(self.entry.id())? + .ok_or(StoreError::StorageError(StorageError::NotFound( + self.entry.id(), + )))?; + + let _ = + >::remove_child_from(self.collection.id(), &RootHandle, self.entry.id())?; + + let _ = self + .collection + .children_cache()? + .shift_remove(&self.entry.id()); + + Ok(old) + } +} + +impl Deref for EntryMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.entry.item + } +} + +impl DerefMut for EntryMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.entry.item + } +} + +impl Drop for EntryMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn drop(&mut self) { + self.entry.element_mut().update(); + let _ignored = >::save(&mut self.entry); + } +} + +#[derive(Debug)] +struct CollectionMut<'a, T: BorshSerialize + BorshDeserialize, S: StorageAdaptor> { + collection: &'a mut Collection, +} + +impl<'a, T, S> CollectionMut<'a, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn new(collection: &'a mut Collection) -> Self { + Self { collection } + } + + fn insert(&mut self, item: &mut Entry) -> StoreResult<()> { + let _ = >::add_child_to(self.collection.id(), &RootHandle, item)?; + + let _ignored = self.collection.children_cache()?.insert(item.id()); + + Ok(()) + } + + fn clear(&mut self) -> StoreResult<()> { + let children = self.collection.children_cache()?; + + for child in children.drain(..) { + let _ = >::remove_child_from(self.collection.id(), &RootHandle, child)?; + } + + Ok(()) + } +} + +impl Deref for CollectionMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + type Target = Collection; + + fn deref(&self) -> &Self::Target { + self.collection + } +} + +impl DerefMut for CollectionMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + self.collection + } +} + +impl Drop for CollectionMut<'_, T, S> +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn drop(&mut self) { + self.collection.element_mut().update(); + } +} + +impl fmt::Debug for Collection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Collection") + .field("element", &self.storage) + .finish() + } +} + +impl Default for Collection +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn default() -> Self { + Self::new(None) + } +} + +impl Eq for Collection {} + +impl PartialEq + for Collection +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn eq(&self, other: &Self) -> bool { + let l = self.entries().unwrap().flatten(); + let r = other.entries().unwrap().flatten(); + + l.eq(r) + } +} + +impl Ord for Collection { + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let l = self.entries().unwrap().flatten(); + let r = other.entries().unwrap().flatten(); + + l.cmp(r) + } +} + +impl PartialOrd + for Collection +{ + fn partial_cmp(&self, other: &Self) -> Option { + let l = self.entries().ok()?.flatten(); + let r = other.entries().ok()?.flatten(); + + l.partial_cmp(r) + } +} diff --git a/crates/storage/src/collections/error.rs b/crates/storage/src/collections/error.rs index bd91c67ff..1181db385 100644 --- a/crates/storage/src/collections/error.rs +++ b/crates/storage/src/collections/error.rs @@ -1,6 +1,7 @@ +//! Error types for storage operations. + use thiserror::Error; -// fixme! macro expects `calimero_storage` to be in deps use crate::address::PathError; use crate::interface::StorageError; diff --git a/crates/storage/src/collections/root.rs b/crates/storage/src/collections/root.rs new file mode 100644 index 000000000..4de3ee951 --- /dev/null +++ b/crates/storage/src/collections/root.rs @@ -0,0 +1,194 @@ +//! A root collection that stores a single value. + +use core::fmt; +use std::cell::RefCell; +use std::ops::{Deref, DerefMut}; +use std::ptr; + +use borsh::{from_slice, BorshDeserialize, BorshSerialize}; + +use super::{Collection, ROOT_ID}; +use crate::address::Id; +use crate::integration::Comparison; +use crate::interface::{Action, Interface, StorageError}; +use crate::store::{MainStorage, StorageAdaptor}; +use crate::sync::{self, SyncArtifact}; + +/// A set collection that stores unqiue values once. +pub struct Root { + inner: Collection, + value: RefCell>, + dirty: bool, +} + +impl fmt::Debug for Root +where + T: BorshSerialize + BorshDeserialize + fmt::Debug, + S: StorageAdaptor, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Root") + .field("inner", &self.inner) + .field("value", &self.value) + .field("dirty", &self.dirty) + .finish() + } +} + +impl Root +where + T: BorshSerialize + BorshDeserialize, +{ + /// Creates a new root collection with the given value. + pub fn new T>(f: F) -> Self { + Self::new_internal(f) + } +} + +impl Root +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + /// Creates a new root collection with the given value. + #[expect(clippy::unwrap_used, reason = "fatal error if it happens")] + pub fn new_internal T>(f: F) -> Self { + let mut inner = Collection::new(Some(*ROOT_ID)); + + let id = Self::entry_id(); + + let value = inner.insert(Some(id), f()).unwrap(); + + Self { + inner, + dirty: false, + value: RefCell::new(Some(value)), + } + } + + fn entry_id() -> Id { + Id::new([118; 32]) + } + + #[expect(clippy::mut_from_ref, reason = "'tis fine")] + #[expect(clippy::unwrap_used, reason = "fatal error if it happens")] + fn get(&self) -> &mut T { + let mut value = self.value.borrow_mut(); + + let id = Self::entry_id(); + + let value = value.get_or_insert_with(|| self.inner.get(id).unwrap().unwrap()); + + #[expect(unsafe_code, reason = "necessary for caching")] + let value = unsafe { &mut *ptr::from_mut(value) }; + + value + } + + /// Fetches the root collection. + #[expect( + clippy::unwrap_used, + clippy::unwrap_in_result, + reason = "fatal error if it happens" + )] + pub fn fetch() -> Option { + let inner = >::root().unwrap()?; + + Some(Self { + inner, + dirty: false, + value: RefCell::new(None), + }) + } + + /// Commits the root collection. + #[expect(clippy::unwrap_used, reason = "fatal error if it happens")] + pub fn commit(mut self) { + if self.dirty { + if let Some(value) = self.value.into_inner() { + if let Some(mut entry) = self.inner.get_mut(Self::entry_id()).unwrap() { + *entry = value; + } + } + } + + >::commit_root(Some(self.inner)).unwrap(); + } + + /// Commits the root collection without an instance of the root state. + #[expect(clippy::unwrap_used, reason = "fatal error if it happens")] + pub fn commit_headless() { + >::commit_root::>(None).unwrap(); + } + + /// Syncs the root collection. + #[expect(clippy::missing_errors_doc, reason = "NO")] + pub fn sync(args: &[u8]) -> Result<(), StorageError> { + let artifact = + from_slice::(args).map_err(StorageError::DeserializationError)?; + + match artifact { + SyncArtifact::Actions(actions) => { + for action in actions { + let _ignored = match action { + Action::Compare { id } => { + sync::push_comparison(Comparison { + data: >::find_by_id_raw(id), + comparison_data: >::generate_comparison_data(Some( + id, + ))?, + }); + } + Action::Add { .. } | Action::Update { .. } | Action::Delete { .. } => { + >::apply_action(action)?; + } + }; + } + } + SyncArtifact::Comparisons(comparisons) => { + if comparisons.is_empty() { + sync::push_comparison(Comparison { + data: >::find_by_id_raw(Id::root()), + comparison_data: >::generate_comparison_data(None)?, + }); + } + + for Comparison { + data, + comparison_data, + } in comparisons + { + >::compare_affective(data, comparison_data)?; + } + } + } + + Self::commit_headless(); + + Ok(()) + } +} + +impl Deref for Root +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + type Target = T; + + fn deref(&self) -> &Self::Target { + self.get() + } +} + +impl DerefMut for Root +where + T: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + self.dirty = true; + + self.get() + } +} diff --git a/crates/storage/src/collections/unordered_map.rs b/crates/storage/src/collections/unordered_map.rs index 7eaea3726..d103f0e1b 100644 --- a/crates/storage/src/collections/unordered_map.rs +++ b/crates/storage/src/collections/unordered_map.rs @@ -1,102 +1,59 @@ +//! This module provides functionality for the unordered map data structure. + use core::borrow::Borrow; -use core::marker::PhantomData; +use core::fmt; +use std::mem; use borsh::{BorshDeserialize, BorshSerialize}; +use serde::ser::SerializeMap; +use serde::Serialize; use sha2::{Digest, Sha256}; -// fixme! macro expects `calimero_storage` to be in deps -use crate as calimero_storage; -use crate::address::{Id, Path}; +use super::{Collection, StorageAdaptor}; +use crate::address::Id; use crate::collections::error::StoreError; -use crate::entities::{Data, Element}; -use crate::interface::Interface; -use crate::{AtomicUnit, Collection}; +use crate::entities::Data; +use crate::store::MainStorage; /// A map collection that stores key-value pairs. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(255)] -#[root] -pub struct UnorderedMap { - /// The id used for the map's entries. - id: Id, - /// The entries in the map. - entries: Entries, - /// The storage element for the map. - #[storage] - storage: Element, -} - -/// A collection of entries in a map. -#[derive(Collection, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[children(Entry)] -struct Entries { - /// Helper to associate the generic types with the collection. - _priv: PhantomData<(K, V)>, +#[derive(BorshSerialize, BorshDeserialize)] +pub struct UnorderedMap { + #[borsh(bound(serialize = "", deserialize = ""))] + inner: Collection<(K, V), S>, } -/// An entry in a map. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(254)] -pub struct Entry { - /// The key for the entry. - key: K, - /// The value for the entry. - value: V, - /// The storage element for the entry. - #[storage] - storage: Element, +impl UnorderedMap +where + K: BorshSerialize + BorshDeserialize, + V: BorshSerialize + BorshDeserialize, +{ + /// Create a new map collection. + pub fn new() -> Self { + Self::new_internal() + } } -impl - UnorderedMap +impl UnorderedMap +where + K: BorshSerialize + BorshDeserialize, + V: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, { /// Create a new map collection. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, or a child - /// [`Element`](crate::entities::Element) cannot be found, an error will be - /// returned. - /// - pub fn new() -> Result { - let id = Id::random(); - let mut this = Self { - id: id, - entries: Entries::default(), - storage: Element::new(&Path::new(format!("::unused::map::{id}::path"))?, Some(id)), - }; - - let _ = Interface::save(&mut this)?; - - Ok(this) + fn new_internal() -> Self { + Self { + inner: Collection::new(None), + } } /// Compute the ID for a key. fn compute_id(&self, key: &[u8]) -> Id { let mut hasher = Sha256::new(); - hasher.update(self.id.as_bytes()); + hasher.update(self.inner.id().as_bytes()); hasher.update(key); Id::new(hasher.finalize().into()) } - /// Get the raw entry for a key in the map. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, or a child - /// [`Element`](crate::entities::Element) cannot be found, an error will be - /// returned. - /// - fn get_raw(&self, key: &Q) -> Result>, StoreError> - where - K: Borrow, - Q: PartialEq + AsRef<[u8]> + ?Sized, - { - Ok(Interface::find_by_id::>( - self.compute_id(key.as_ref()), - )?) - } - /// Insert a key-value pair into the map. /// /// # Errors @@ -109,26 +66,15 @@ impl where K: AsRef<[u8]> + PartialEq, { - if let Some(mut entry) = self.get_raw(&key)? { - let ret_value = entry.value; - entry.value = value; - // has to be called to update the entry - entry.element_mut().update(); - let _ = Interface::save(&mut entry)?; - return Ok(Some(ret_value)); + let id = self.compute_id(key.as_ref()); + + if let Some(mut entry) = self.inner.get_mut(id)? { + let (_, v) = &mut *entry; + + return Ok(Some(mem::replace(v, value))); } - let path = self.path(); - let storage = Element::new(&path, Some(self.compute_id(key.as_ref()))); - let _ = Interface::add_child_to( - self.storage.id(), - &mut self.entries, - &mut Entry { - key, - value, - storage, - }, - )?; + let _ignored = self.inner.insert(Some(id), (key, value))?; Ok(None) } @@ -141,10 +87,8 @@ impl /// [`Element`](crate::entities::Element) cannot be found, an error will be /// returned. /// - pub fn entries(&self) -> Result, StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; - - Ok(entries.into_iter().map(|entry| (entry.key, entry.value))) + pub fn entries(&self) -> Result + '_, StoreError> { + Ok(self.inner.entries()?.flatten().fuse()) } /// Get the number of entries in the map. @@ -156,7 +100,7 @@ impl /// returned. /// pub fn len(&self) -> Result { - Ok(Interface::child_info_for(self.id(), &self.entries)?.len()) + self.inner.len() } /// Get the value for a key in the map. @@ -172,10 +116,9 @@ impl K: Borrow, Q: PartialEq + AsRef<[u8]> + ?Sized, { - let entry = Interface::find_by_id::>(self.compute_id(key.as_ref()))?; - let value = entry.map(|e| e.value); + let id = self.compute_id(key.as_ref()); - Ok(value) + Ok(self.inner.get(id)?.map(|(_, v)| v)) } /// Check if the map contains a key. @@ -191,7 +134,7 @@ impl K: Borrow + PartialEq, Q: PartialEq + AsRef<[u8]> + ?Sized, { - Ok(self.get_raw(key)?.is_some()) + self.get(key).map(|v| v.is_some()) } /// Remove a key from the map, returning the value at the key if it previously existed. @@ -202,18 +145,18 @@ impl /// [`Element`](crate::entities::Element) cannot be found, an error will be /// returned. /// - pub fn remove(&mut self, key: &Q) -> Result + pub fn remove(&mut self, key: &Q) -> Result, StoreError> where K: Borrow, Q: PartialEq + AsRef<[u8]> + ?Sized, { - let entry = Element::new(&self.path(), Some(self.compute_id(key.as_ref()))); + let id = self.compute_id(key.as_ref()); - Ok(Interface::remove_child_from( - self.id(), - &mut self.entries, - entry.id(), - )?) + let Some(entry) = self.inner.get_mut(id)? else { + return Ok(None); + }; + + entry.remove().map(|(_, v)| Some(v)) } /// Clear the map, removing all entries. @@ -225,23 +168,120 @@ impl /// returned. /// pub fn clear(&mut self) -> Result<(), StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; + self.inner.clear() + } +} - for entry in entries { - let _ = Interface::remove_child_from(self.id(), &mut self.entries, entry.id())?; +impl Eq for UnorderedMap +where + K: Eq + BorshSerialize + BorshDeserialize, + V: Eq + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ +} + +impl PartialEq for UnorderedMap +where + K: PartialEq + BorshSerialize + BorshDeserialize, + V: PartialEq + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn eq(&self, other: &Self) -> bool { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); + + l.eq(r) + } +} + +impl Ord for UnorderedMap +where + K: Ord + BorshSerialize + BorshDeserialize, + V: Ord + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); + + l.cmp(r) + } +} + +impl PartialOrd for UnorderedMap +where + K: PartialOrd + BorshSerialize + BorshDeserialize, + V: PartialOrd + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn partial_cmp(&self, other: &Self) -> Option { + let l = self.entries().ok()?; + let r = other.entries().ok()?; + + l.partial_cmp(r) + } +} + +impl fmt::Debug for UnorderedMap +where + K: fmt::Debug + BorshSerialize + BorshDeserialize, + V: fmt::Debug + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + #[expect(clippy::unwrap_used, clippy::unwrap_in_result, reason = "'tis fine")] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + f.debug_struct("UnorderedMap") + .field("entries", &self.inner) + .finish() + } else { + f.debug_map().entries(self.entries().unwrap()).finish() } + } +} + +impl Default for UnorderedMap +where + K: Default + BorshSerialize + BorshDeserialize, + V: Default + BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn default() -> Self { + Self::new_internal() + } +} + +impl Serialize for UnorderedMap +where + K: BorshSerialize + BorshDeserialize + Serialize, + V: BorshSerialize + BorshDeserialize + Serialize, + S: StorageAdaptor, +{ + fn serialize(&self, serializer: Ser) -> Result + where + Ser: serde::Serializer, + { + let len = self.len().map_err(serde::ser::Error::custom)?; - Ok(()) + let mut seq = serializer.serialize_map(Some(len))?; + + for (k, v) in self.entries().map_err(serde::ser::Error::custom)? { + seq.serialize_entry(&k, &v)?; + } + + seq.end() } } #[cfg(test)] mod tests { - use crate::collections::unordered_map::UnorderedMap; + use crate::collections::{Root, UnorderedMap}; #[test] fn test_unordered_map_basic_operations() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key".to_string(), "value".to_string()) @@ -249,18 +289,19 @@ mod tests { .is_none()); assert_eq!( - map.get("key").expect("get failed"), - Some("value".to_string()) + map.get("key").expect("get failed").as_deref(), + Some("value") ); assert_ne!( - map.get("key").expect("get failed"), - Some("value2".to_string()) + map.get("key").expect("get failed").as_deref(), + Some("value2") ); assert_eq!( map.insert("key".to_string(), "value2".to_string()) - .expect("insert failed"), - Some("value".to_string()) + .expect("insert failed") + .as_deref(), + Some("value") ); assert!(map .insert("key2".to_string(), "value".to_string()) @@ -268,23 +309,28 @@ mod tests { .is_none()); assert_eq!( - map.get("key").expect("get failed"), - Some("value2".to_string()) + map.get("key").expect("get failed").as_deref(), + Some("value2") ); assert_eq!( - map.get("key2").expect("get failed"), - Some("value".to_string()) + map.get("key2").expect("get failed").as_deref(), + Some("value") ); - assert_eq!(map.remove("key").expect("error while removing key"), true); - assert_eq!(map.remove("key").expect("error while removing key"), false); + assert_eq!( + map.remove("key") + .expect("error while removing key") + .as_deref(), + Some("value2") + ); + assert_eq!(map.remove("key").expect("error while removing key"), None); assert_eq!(map.get("key").expect("get failed"), None); } #[test] fn test_unordered_map_insert_and_get() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key1".to_string(), "value1".to_string()) @@ -296,18 +342,18 @@ mod tests { .is_none()); assert_eq!( - map.get("key1").expect("get failed"), - Some("value1".to_string()) + map.get("key1").expect("get failed").as_deref(), + Some("value1") ); assert_eq!( - map.get("key2").expect("get failed"), - Some("value2".to_string()) + map.get("key2").expect("get failed").as_deref(), + Some("value2") ); } #[test] fn test_unordered_map_update_value() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key".to_string(), "value".to_string()) @@ -319,27 +365,30 @@ mod tests { .is_none()); assert_eq!( - map.get("key").expect("get failed"), - Some("new_value".to_string()) + map.get("key").expect("get failed").as_deref(), + Some("new_value") ); } #[test] fn test_remove() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key".to_string(), "value".to_string()) .expect("insert failed") .is_none()); - assert_eq!(map.remove("key").expect("remove failed"), true); + assert_eq!( + map.remove("key").expect("remove failed").as_deref(), + Some("value") + ); assert_eq!(map.get("key").expect("get failed"), None); } #[test] fn test_clear() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key1".to_string(), "value1".to_string()) @@ -358,7 +407,7 @@ mod tests { #[test] fn test_unordered_map_len() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert_eq!(map.len().expect("len failed"), 0); @@ -377,14 +426,17 @@ mod tests { assert_eq!(map.len().expect("len failed"), 2); - assert_eq!(map.remove("key1").expect("remove failed"), true); + assert_eq!( + map.remove("key1").expect("remove failed").as_deref(), + Some("value1") + ); assert_eq!(map.len().expect("len failed"), 1); } #[test] fn test_unordered_map_contains() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key".to_string(), "value".to_string()) @@ -397,7 +449,7 @@ mod tests { #[test] fn test_unordered_map_entries() { - let mut map = UnorderedMap::::new().expect("failed to create map"); + let mut map = Root::new(|| UnorderedMap::new()); assert!(map .insert("key1".to_string(), "value1".to_string()) diff --git a/crates/storage/src/collections/unordered_set.rs b/crates/storage/src/collections/unordered_set.rs index 378ef5bb7..adb92f517 100644 --- a/crates/storage/src/collections/unordered_set.rs +++ b/crates/storage/src/collections/unordered_set.rs @@ -1,76 +1,52 @@ +//! This module provides functionality for the unordered set data structure. + use core::borrow::Borrow; -use core::marker::PhantomData; +use core::fmt; use borsh::{BorshDeserialize, BorshSerialize}; +use serde::ser::SerializeSeq; +use serde::Serialize; use sha2::{Digest, Sha256}; -// fixme! macro expects `calimero_storage` to be in deps -use crate as calimero_storage; -use crate::address::{Id, Path}; +use super::Collection; +use crate::address::Id; use crate::collections::error::StoreError; -use crate::entities::{Data, Element}; -use crate::interface::Interface; -use crate::{AtomicUnit, Collection}; +use crate::entities::Data; +use crate::store::{MainStorage, StorageAdaptor}; /// A set collection that stores unqiue values once. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(253)] -#[root] -pub struct UnorderedSet { - /// The prefix used for the set's entries. - id: Id, - /// The entries in the set. - entries: Entries, - /// The storage element for the set. - #[storage] - storage: Element, -} - -/// A collection of entries in a set. -#[derive(Collection, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[children(Entry)] -struct Entries { - /// Helper to associate the generic types with the collection. - _priv: PhantomData, +#[derive(BorshSerialize, BorshDeserialize)] +pub struct UnorderedSet { + #[borsh(bound(serialize = "", deserialize = ""))] + inner: Collection, } -/// An entry in a set. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(252)] -pub struct Entry { - /// The value for the entry. - value: V, - /// The storage element for the entry. - #[storage] - storage: Element, +impl UnorderedSet +where + V: BorshSerialize + BorshDeserialize, +{ + /// Create a new set collection. + pub fn new() -> Self { + Self::new_internal() + } } -impl UnorderedSet { +impl UnorderedSet +where + V: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ /// Create a new set collection. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, or a child - /// [`Element`](crate::entities::Element) cannot be found, an error will be - /// returned. - /// - pub fn new() -> Result { - let id = Id::random(); - let mut this = Self { - id: id, - entries: Entries::default(), - storage: Element::new(&Path::new(format!("::unused::set::{id}::path"))?, Some(id)), - }; - - let _ = Interface::save(&mut this)?; - - Ok(this) + fn new_internal() -> Self { + Self { + inner: Collection::new(None), + } } /// Compute the ID for a value in the set. fn compute_id(&self, value: &[u8]) -> Id { let mut hasher = Sha256::new(); - hasher.update(self.id.as_bytes()); + hasher.update(self.inner.id().as_bytes()); hasher.update(value); Id::new(hasher.finalize().into()) } @@ -87,18 +63,13 @@ impl UnorderedSet { where V: AsRef<[u8]> + PartialEq, { - let path = self.path(); + let id = self.compute_id(value.as_ref()); - if self.contains(&value)? { + if self.inner.get_mut(id)?.is_some() { return Ok(false); - } + }; - let storage = Element::new(&path, Some(self.compute_id(value.as_ref()))); - let _ = Interface::add_child_to( - self.storage.id(), - &mut self.entries, - &mut Entry { value, storage }, - )?; + let _ignored = self.inner.insert(Some(id), value)?; Ok(true) } @@ -111,10 +82,8 @@ impl UnorderedSet { /// [`Element`](crate::entities::Element) cannot be found, an error will be /// returned. /// - pub fn entries(&self) -> Result, StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; - - Ok(entries.into_iter().map(|entry| entry.value)) + pub fn entries(&self) -> Result + '_, StoreError> { + Ok(self.inner.entries()?.flatten().fuse()) } /// Get the number of entries in the set. @@ -126,7 +95,7 @@ impl UnorderedSet { /// returned. /// pub fn len(&self) -> Result { - Ok(Interface::child_info_for(self.id(), &self.entries)?.len()) + self.inner.len() } /// Get the value for a key in the set. @@ -142,8 +111,9 @@ impl UnorderedSet { V: Borrow, Q: PartialEq + ?Sized + AsRef<[u8]>, { - let entry = Interface::find_by_id::>(self.compute_id(value.as_ref()))?; - Ok(entry.is_some()) + let id = self.compute_id(value.as_ref()); + + Ok(self.inner.get(id)?.is_some()) } /// Remove a key from the set, returning the value at the key if it previously existed. @@ -159,13 +129,15 @@ impl UnorderedSet { V: Borrow, Q: PartialEq + AsRef<[u8]> + ?Sized, { - let entry = Element::new(&self.path(), Some(self.compute_id(value.as_ref()))); + let id = self.compute_id(value.as_ref()); - Ok(Interface::remove_child_from( - self.id(), - &mut self.entries, - entry.id(), - )?) + let Some(entry) = self.inner.get_mut(id)? else { + return Ok(false); + }; + + let _ignored = entry.remove()?; + + Ok(true) } /// Clear the set, removing all entries. @@ -177,23 +149,103 @@ impl UnorderedSet { /// returned. /// pub fn clear(&mut self) -> Result<(), StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; + self.inner.clear() + } +} + +impl Eq for UnorderedSet where V: Eq + BorshSerialize + BorshDeserialize {} + +impl PartialEq for UnorderedSet +where + V: PartialEq + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn eq(&self, other: &Self) -> bool { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); + + l.eq(r) + } +} + +impl Ord for UnorderedSet +where + V: Ord + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); + + l.cmp(r) + } +} + +impl PartialOrd for UnorderedSet +where + V: PartialOrd + BorshSerialize + BorshDeserialize, +{ + fn partial_cmp(&self, other: &Self) -> Option { + let l = self.entries().ok()?; + let r = other.entries().ok()?; + + l.partial_cmp(r) + } +} + +impl fmt::Debug for UnorderedSet +where + V: fmt::Debug + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, clippy::unwrap_in_result, reason = "'tis fine")] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + f.debug_struct("UnorderedSet") + .field("entries", &self.inner) + .finish() + } else { + f.debug_set().entries(self.entries().unwrap()).finish() + } + } +} + +impl Default for UnorderedSet +where + V: BorshSerialize + BorshDeserialize, +{ + fn default() -> Self { + Self::new_internal() + } +} + +impl Serialize for UnorderedSet +where + V: BorshSerialize + BorshDeserialize + Serialize, + S: StorageAdaptor, +{ + fn serialize(&self, serializer: Ser) -> Result + where + Ser: serde::Serializer, + { + let len = self.len().map_err(serde::ser::Error::custom)?; + + let mut seq = serializer.serialize_seq(Some(len))?; - for entry in entries { - let _ = Interface::remove_child_from(self.id(), &mut self.entries, entry.id())?; + for v in self.entries().map_err(serde::ser::Error::custom)? { + seq.serialize_element(&v)?; } - Ok(()) + seq.end() } } #[cfg(test)] mod tests { - use crate::collections::UnorderedSet; + use crate::collections::{Root, UnorderedSet}; #[test] fn test_unordered_set_operations() { - let mut set = UnorderedSet::::new().expect("failed to create set"); + let mut set = Root::new(|| UnorderedSet::new()); assert!(set.insert("value1".to_string()).expect("insert failed")); @@ -221,7 +273,7 @@ mod tests { #[test] fn test_unordered_set_len() { - let mut set = UnorderedSet::::new().expect("failed to create set"); + let mut set = Root::new(|| UnorderedSet::new()); assert!(set.insert("value1".to_string()).expect("insert failed")); assert!(set.insert("value2".to_string()).expect("insert failed")); @@ -236,7 +288,7 @@ mod tests { #[test] fn test_unordered_set_clear() { - let mut set = UnorderedSet::::new().expect("failed to create set"); + let mut set = Root::new(|| UnorderedSet::new()); assert!(set.insert("value1".to_string()).expect("insert failed")); assert!(set.insert("value2".to_string()).expect("insert failed")); @@ -252,7 +304,7 @@ mod tests { #[test] fn test_unordered_set_entries() { - let mut set = UnorderedSet::::new().expect("failed to create set"); + let mut set = Root::new(|| UnorderedSet::new()); assert!(set.insert("value1".to_string()).expect("insert failed")); assert!(set.insert("value2".to_string()).expect("insert failed")); diff --git a/crates/storage/src/collections/vector.rs b/crates/storage/src/collections/vector.rs index 4454d337b..a1d57ae74 100644 --- a/crates/storage/src/collections/vector.rs +++ b/crates/storage/src/collections/vector.rs @@ -1,68 +1,45 @@ +//! This module provides functionality for the vector data structure. + use core::borrow::Borrow; -use core::marker::PhantomData; +use core::fmt; +use std::mem; use borsh::{BorshDeserialize, BorshSerialize}; +use serde::ser::SerializeSeq; +use serde::Serialize; -// fixme! macro expects `calimero_storage` to be in deps -use crate::address::{Id, Path}; +use super::Collection; use crate::collections::error::StoreError; -use crate::entities::{Data, Element}; -use crate::interface::{Interface, StorageError}; -use crate::{self as calimero_storage, AtomicUnit, Collection}; +use crate::store::{MainStorage, StorageAdaptor}; /// A vector collection that stores key-value pairs. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(251)] -#[root] -pub struct Vector { - /// The entries in the vector. - entries: Entries, - /// The storage element for the vector. - #[storage] - storage: Element, -} - -/// A collection of entries in a vector. -#[derive(Collection, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[children(Entry)] -struct Entries { - /// Helper to associate the generic types with the collection. - _priv: PhantomData, +#[derive(BorshSerialize, BorshDeserialize)] +pub struct Vector { + // Borrow/ToOwned + #[borsh(bound(serialize = "", deserialize = ""))] + inner: Collection, } -/// An entry in a vector. -#[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] -#[type_id(250)] -pub struct Entry { - /// The value for the entry. - value: V, - /// The storage element for the entry. - #[storage] - storage: Element, +impl Vector +where + V: BorshSerialize + BorshDeserialize, +{ + /// Create a new vector collection. + pub fn new() -> Self { + Self::new_internal() + } } -impl Vector { +impl Vector +where + V: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ /// Create a new vector collection. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, or a child - /// [`Element`](crate::entities::Element) cannot be found, an error will be - /// returned. - /// - pub fn new() -> Result { - let id = Id::random(); - let mut this = Self { - entries: Entries::default(), - storage: Element::new( - &Path::new(format!("::unused::vector::{id}::path"))?, - Some(id), - ), - }; - - let _ = Interface::save(&mut this)?; - - Ok(this) + fn new_internal() -> Self { + Self { + inner: Collection::new(None), + } } /// Add a value to the end of the vector. @@ -74,18 +51,9 @@ impl Vector { /// returned. /// pub fn push(&mut self, value: V) -> Result<(), StoreError> { - let mut entry = Entry { - value, - storage: Element::new(&self.path(), None), - }; + let _ignored = self.inner.insert(None, value)?; - if Interface::add_child_to(self.id(), &mut self.entries, &mut entry)? { - return Ok(()); - } - - Err(StoreError::StorageError(StorageError::ActionNotAllowed( - "Failed to add child".to_owned(), - ))) + Ok(()) } /// Remove and return the last value from the vector. @@ -97,35 +65,17 @@ impl Vector { /// returned. /// pub fn pop(&mut self) -> Result, StoreError> { - let id = match Interface::child_info_for(self.id(), &self.entries)?.pop() { - Some(info) => info.id(), - None => return Ok(None), + let Some(last) = self.inner.last()? else { + return Ok(None); }; - let entry = Interface::find_by_id::>(id)?; - let _ = Interface::remove_child_from(self.id(), &mut self.entries, id)?; - Ok(entry.map(|e| e.value)) - } - /// Get the raw storage entry at a specific index in the vector. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, or a child - /// [`Element`](crate::entities::Element) cannot be found, an error will be - /// returned. - /// - fn get_raw(&self, index: usize) -> Result>, StoreError> { - let id = match Interface::child_info_for(self.id(), &self.entries)?.get(index) { - Some(info) => info.id(), - None => return Ok(None), + let Some(entry) = self.inner.get_mut(last)? else { + return Ok(None); }; - let entry = match Interface::find_by_id::>(id) { - Ok(entry) => entry, - Err(_) => return Ok(None), - }; + let last = entry.remove()?; - Ok(entry) + Ok(Some(last)) } /// Get the value at a specific index in the vector. @@ -137,7 +87,7 @@ impl Vector { /// returned. /// pub fn get(&self, index: usize) -> Result, StoreError> { - Ok(self.get_raw(index)?.map(|entry| entry.value)) + self.inner.entries()?.nth(index).transpose() } /// Update the value at a specific index in the vector. @@ -148,18 +98,18 @@ impl Vector { /// [`Element`](crate::entities::Element) cannot be found, an error will be /// returned. /// - pub fn update(&mut self, index: usize, value: V) -> Result<(), StoreError> { - let mut entry = self.get_raw(index)?.ok_or(StoreError::StorageError( - StorageError::ActionNotAllowed("error".to_owned()), - ))?; + pub fn update(&mut self, index: usize, value: V) -> Result, StoreError> { + let Some(id) = self.inner.nth(index)? else { + return Ok(None); + }; - // has to be called to update the entry - entry.value = value; - entry.element_mut().update(); + let Some(mut entry) = self.inner.get_mut(id)? else { + return Ok(None); + }; - let _ = Interface::save::>(&mut entry)?; + let old = mem::replace(&mut *entry, value); - Ok(()) + Ok(Some(old)) } /// Get an iterator over the entries in the vector. @@ -170,10 +120,24 @@ impl Vector { /// [`Element`](crate::entities::Element) cannot be found, an error will be /// returned. /// - pub fn entries(&self) -> Result, StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; + pub fn entries(&self) -> Result + '_, StoreError> { + Ok(self.inner.entries()?.flatten().fuse()) + } + + /// Get the last value in the vector. + /// + /// # Errors + /// + /// If an error occurs when interacting with the storage system, or a child + /// [`Element`](crate::entities::Element) cannot be found, an error will be + /// returned. + /// + pub fn last(&self) -> Result, StoreError> { + let Some(last) = self.inner.last()? else { + return Ok(None); + }; - Ok(entries.into_iter().map(|entry| (entry.value))) + self.inner.get(last) } /// Get the number of entries in the vector. @@ -186,7 +150,7 @@ impl Vector { /// #[expect(clippy::len_without_is_empty, reason = "TODO: will be implemented")] pub fn len(&self) -> Result { - Ok(Interface::child_info_for(self.id(), &self.entries)?.len()) + self.inner.len() } /// Get the value for a key in the vector. @@ -200,10 +164,10 @@ impl Vector { pub fn contains(&self, value: &Q) -> Result where V: Borrow, - Q: PartialEq + ?Sized, + Q: PartialEq, { for entry in self.entries()? { - if value.borrow() == &entry { + if value == entry.borrow() { return Ok(true); } } @@ -220,30 +184,105 @@ impl Vector { /// returned. /// pub fn clear(&mut self) -> Result<(), StoreError> { - let entries = Interface::children_of(self.id(), &self.entries)?; + self.inner.clear() + } +} + +impl Eq for Vector where V: Eq + BorshSerialize + BorshDeserialize {} + +impl PartialEq for Vector +where + V: PartialEq + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn eq(&self, other: &Self) -> bool { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); + + l.eq(r) + } +} + +impl Ord for Vector +where + V: Ord + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, reason = "'tis fine")] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let l = self.entries().unwrap(); + let r = other.entries().unwrap(); - for entry in entries { - let _ = Interface::remove_child_from(self.id(), &mut self.entries, entry.id())?; + l.cmp(r) + } +} + +impl PartialOrd for Vector +where + V: PartialOrd + BorshSerialize + BorshDeserialize, +{ + fn partial_cmp(&self, other: &Self) -> Option { + let l = self.entries().ok()?; + let r = other.entries().ok()?; + + l.partial_cmp(r) + } +} + +impl fmt::Debug for Vector +where + V: fmt::Debug + BorshSerialize + BorshDeserialize, +{ + #[expect(clippy::unwrap_used, clippy::unwrap_in_result, reason = "'tis fine")] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + f.debug_struct("Vector") + .field("entries", &self.inner) + .finish() + } else { + f.debug_list().entries(self.entries().unwrap()).finish() } + } +} - Ok(()) +impl Default for Vector +where + V: BorshSerialize + BorshDeserialize, + S: StorageAdaptor, +{ + fn default() -> Self { + Self::new_internal() } } -#[cfg(test)] -mod tests { - use crate::collections::error::StoreError; - use crate::collections::vector::Vector; +impl Serialize for Vector +where + V: BorshSerialize + BorshDeserialize + Serialize, + S: StorageAdaptor, +{ + fn serialize(&self, serializer: Ser) -> Result + where + Ser: serde::Serializer, + { + let len = self.len().map_err(serde::ser::Error::custom)?; - #[test] - fn test_vector_new() { - let vector: Result, StoreError> = Vector::new(); - assert!(vector.is_ok()); + let mut seq = serializer.serialize_seq(Some(len))?; + + for entry in self.entries().map_err(serde::ser::Error::custom)? { + seq.serialize_element(&entry)?; + } + + seq.end() } +} + +#[cfg(test)] +mod tests { + use crate::collections::{Root, Vector}; #[test] fn test_vector_push() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value = "test_data".to_string(); let result = vector.push(value.clone()); assert!(result.is_ok()); @@ -252,7 +291,8 @@ mod tests { #[test] fn test_vector_get() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value = "test_data".to_string(); let _ = vector.push(value.clone()).unwrap(); let retrieved_value = vector.get(0).unwrap(); @@ -261,18 +301,21 @@ mod tests { #[test] fn test_vector_update() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value1 = "test_data1".to_string(); let value2 = "test_data2".to_string(); let _ = vector.push(value1.clone()).unwrap(); - let _ = vector.update(0, value2.clone()).unwrap(); + let old = vector.update(0, value2.clone()).unwrap(); let retrieved_value = vector.get(0).unwrap(); assert_eq!(retrieved_value, Some(value2)); + assert_eq!(old, Some(value1)); } #[test] fn test_vector_get_non_existent() { - let vector: Vector = Vector::new().unwrap(); + let vector = Root::new(|| Vector::::new()); + match vector.get(0) { Ok(retrieved_value) => assert_eq!(retrieved_value, None), Err(e) => panic!("Error occurred: {:?}", e), @@ -281,7 +324,8 @@ mod tests { #[test] fn test_vector_pop() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value = "test_data".to_string(); let _ = vector.push(value.clone()).unwrap(); let popped_value = vector.pop().unwrap(); @@ -291,7 +335,8 @@ mod tests { #[test] fn test_vector_entries() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value1 = "test_data1".to_string(); let value2 = "test_data2".to_string(); let _ = vector.push(value1.clone()).unwrap(); @@ -302,7 +347,8 @@ mod tests { #[test] fn test_vector_contains() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value = "test_data".to_string(); let _ = vector.push(value.clone()).unwrap(); assert!(vector.contains(&value).unwrap()); @@ -312,7 +358,8 @@ mod tests { #[test] fn test_vector_clear() { - let mut vector: Vector = Vector::new().unwrap(); + let mut vector = Root::new(|| Vector::new()); + let value = "test_data".to_string(); let _ = vector.push(value.clone()).unwrap(); vector.clear().unwrap(); diff --git a/crates/storage/src/entities.rs b/crates/storage/src/entities.rs index 5de099efa..78e047ad0 100644 --- a/crates/storage/src/entities.rs +++ b/crates/storage/src/entities.rs @@ -214,13 +214,12 @@ mod tests; use core::fmt::{self, Debug, Display, Formatter}; use std::collections::BTreeMap; +use std::ops::{Deref, DerefMut}; use borsh::{BorshDeserialize, BorshSerialize}; -use serde::{Deserialize, Serialize}; use crate::address::{Id, Path}; use crate::env::time_now; -use crate::interface::StorageError; /// Represents an atomic unit in the storage system. /// @@ -233,10 +232,11 @@ use crate::interface::StorageError; /// # Examples /// /// ``` +/// use borsh::{BorshSerialize, BorshDeserialize}; /// use calimero_storage::entities::Element; /// use calimero_storage_macros::AtomicUnit; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// #[type_id(43)] /// struct Page { /// title: String, @@ -258,10 +258,11 @@ pub trait AtomicUnit: Data {} /// # Examples /// /// ``` +/// use borsh::{BorshSerialize, BorshDeserialize}; /// use calimero_storage_macros::{AtomicUnit, Collection}; /// use calimero_storage::entities::{ChildInfo, Data, Element}; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// #[type_id(42)] /// struct Book { /// title: String, @@ -274,7 +275,7 @@ pub trait AtomicUnit: Data {} /// #[children(Page)] /// struct Pages; /// -/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd)] +/// #[derive(AtomicUnit, Clone, Debug, Eq, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)] /// #[type_id(43)] /// struct Page { /// content: String, @@ -314,56 +315,6 @@ pub trait Collection { /// contentious methods are included, to keep the interface simple and focused. /// pub trait Data: BorshDeserialize + BorshSerialize { - /// Calculates the Merkle hash of the [`Element`]. - /// - /// This method calculates the Merkle hash of the [`Data`] for the - /// [`Element`], which should be based on any regular fields, but ignore - /// skipped fields, private fields, collections, and the storage field (but - /// include the metadata). - /// - /// **IMPORTANT NOTE**: Collection fields do need to be included in the hash - /// calculation, but that is a job for the caller to combine, and this - /// method therefore only calculates the hash of available data (as hashing - /// the children would involve recursive lookups). - /// - /// # Errors - /// - /// This method will return an error if there is a problem calculating the - /// hash. - /// - /// # See also - /// - /// * [`calculate_merkle_hash_for_child()`](Data::calculate_merkle_hash_for_child()) - /// - fn calculate_merkle_hash(&self) -> Result<[u8; 32], StorageError>; - - /// Calculates the Merkle hash of a child of the [`Element`]. - /// - /// This method calculates the Merkle hash of the specified child of the - /// [`Element`]. - /// - /// # Parameters - /// - /// * `collection` - The name of the collection to calculate the hash for. - /// * `slice` - The slice of data to calculate the hash for. This will - /// get deserialised into the appropriate type, and the - /// hash will be calculated based on the data. - /// - /// # Errors - /// - /// This method will return an error if there is a problem calculating the - /// hash, or looking up children. - /// - /// # See also - /// - /// * [`calculate_merkle_hash()`](Data::calculate_merkle_hash()) - /// - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - slice: &[u8], - ) -> Result<[u8; 32], StorageError>; - /// Information about the [`Collection`]s present in the [`Data`]. /// /// This method allows details about the subtree structure and children to @@ -409,14 +360,6 @@ pub trait Data: BorshDeserialize + BorshSerialize { self.element().id() } - /// Whether the [`Element`] is a root. - /// - /// This should return `true` for any types that should sit at the top of - /// the hierarchy; and `false` for all other types, i.e. ones that can have - /// parents. - /// - fn is_root() -> bool; - /// The path to the [`Element`] in the hierarchy. /// /// This is a convenience function that passes through to @@ -430,19 +373,6 @@ pub trait Data: BorshDeserialize + BorshSerialize { fn path(&self) -> Path { self.element().path() } - - /// The type identifier of the entity. - /// - /// This is noted so that the entity can be deserialised correctly in the - /// absence of other semantic information. It is intended that the [`Path`] - /// will be used to help with this at some point, but at present paths are - /// not fully utilised. - /// - /// The value returned is arbitrary, and is up to the implementer to decide - /// what it should be. It is recommended that it be unique for each type of - /// entity. - /// - fn type_id() -> u8; } /// Summary information for the child of an [`Element`] in the storage. @@ -453,20 +383,7 @@ pub trait Data: BorshDeserialize + BorshSerialize { /// purpose is to make information such as the Merkle hash trivially available /// and prevent the need for repeated lookups. /// -#[derive( - BorshDeserialize, - BorshSerialize, - Clone, - Copy, - Debug, - Deserialize, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - Serialize, -)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Copy, Debug, Eq, PartialEq)] #[non_exhaustive] pub struct ChildInfo { /// The unique identifier for the child [`Element`]. @@ -476,13 +393,34 @@ pub struct ChildInfo { /// of the significant data in the "scope" of the child [`Element`], and is /// used to determine whether the data has changed and is valid. pub(crate) merkle_hash: [u8; 32], + + /// The metadata for the child [`Element`]. + pub(crate) metadata: Metadata, +} + +impl Ord for ChildInfo { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.created_at() + .cmp(&other.created_at()) + .then_with(|| self.id.cmp(&other.id)) + } +} + +impl PartialOrd for ChildInfo { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } } impl ChildInfo { /// Creates a new [`ChildInfo`]. #[must_use] - pub const fn new(id: Id, merkle_hash: [u8; 32]) -> Self { - Self { id, merkle_hash } + pub const fn new(id: Id, merkle_hash: [u8; 32], metadata: Metadata) -> Self { + Self { + id, + merkle_hash, + metadata, + } } /// The unique identifier for the child [`Element`]. @@ -502,6 +440,18 @@ impl ChildInfo { pub const fn merkle_hash(&self) -> [u8; 32] { self.merkle_hash } + + /// The timestamp when the child was created. + #[must_use] + pub const fn created_at(&self) -> u64 { + self.metadata.created_at + } + + /// The timestamp when the child was last updated. + #[must_use] + pub fn updated_at(&self) -> u64 { + *self.metadata.updated_at + } } impl Display for ChildInfo { @@ -594,14 +544,15 @@ impl Display for ChildInfo { /// key-value stores. Therefore, this approach and other similar patterns are /// not suitable for our use case. /// -#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] #[non_exhaustive] pub struct Element { /// The unique identifier for the [`Element`]. - id: Id, + pub(crate) id: Id, /// Whether the [`Element`] is dirty, i.e. has been modified since it was /// last saved. + #[borsh(skip)] pub(crate) is_dirty: bool, /// The Merkle hash of the [`Element`]. This is a cryptographic hash of the @@ -610,11 +561,13 @@ pub struct Element { /// hashing the substantive data in the [`Element`], along with the hashes /// of the children of the [`Element`], thereby representing the state of /// the entire hierarchy below the [`Element`]. + #[borsh(skip)] pub(crate) merkle_hash: [u8; 32], /// The metadata for the [`Element`]. This represents a range of /// system-managed properties that are used to process the [`Element`], but /// are not part of the primary data. + #[borsh(skip)] pub(crate) metadata: Metadata, /// The path to the [`Element`] in the hierarchy of the storage. @@ -660,7 +613,7 @@ impl Element { is_dirty: true, metadata: Metadata { created_at: timestamp, - updated_at: timestamp, + updated_at: timestamp.into(), }, merkle_hash: [0; 32], path: path.clone(), @@ -677,7 +630,7 @@ impl Element { is_dirty: true, metadata: Metadata { created_at: timestamp, - updated_at: timestamp, + updated_at: timestamp.into(), }, merkle_hash: [0; 32], #[expect(clippy::unwrap_used, reason = "This is expected to be valid")] @@ -780,13 +733,13 @@ impl Element { /// pub fn update(&mut self) { self.is_dirty = true; - self.metadata.updated_at = time_now(); + *self.metadata.updated_at = time_now(); } /// The timestamp when the [`Element`] was last updated. #[must_use] - pub const fn updated_at(&self) -> u64 { - self.metadata.updated_at + pub fn updated_at(&self) -> u64 { + *self.metadata.updated_at } } @@ -821,26 +774,50 @@ impl Display for Element { /// Using a [`u64`] timestamp allows for 585 years from the Unix epoch, at /// nanosecond precision. This is more than sufficient for our current needs. /// -#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive( + BorshDeserialize, BorshSerialize, Copy, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, +)] #[non_exhaustive] pub struct Metadata { /// When the [`Element`] was first created. Note that this is a global /// creation time, and does not reflect the time that the [`Element`] was /// added to the local storage. - created_at: u64, + pub(crate) created_at: u64, /// When the [`Element`] was last updated. This is the time that the /// [`Element`] was last modified in any way, and is used to determine the /// freshness of the data. It is critical for the "last write wins" strategy /// that is used to resolve conflicts. - pub(crate) updated_at: u64, + pub(crate) updated_at: UpdatedAt, } -#[cfg(test)] -impl Metadata { - /// Sets the created timestamp of the [`Element`]. This is **ONLY** for use - /// in tests. - pub fn set_created_at(&mut self, created_at: u64) { - self.created_at = created_at; +/// The timestamp when the [`Element`] was last updated. +#[derive(BorshDeserialize, BorshSerialize, Copy, Clone, Debug, Default, Eq, Ord, PartialOrd)] +pub struct UpdatedAt(u64); + +impl PartialEq for UpdatedAt { + fn eq(&self, _other: &Self) -> bool { + // we don't care + true + } +} + +impl Deref for UpdatedAt { + type Target = u64; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for UpdatedAt { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl From for UpdatedAt { + fn from(value: u64) -> Self { + Self(value) } } diff --git a/crates/storage/src/env.rs b/crates/storage/src/env.rs index 44e74874f..1423e57a8 100644 --- a/crates/storage/src/env.rs +++ b/crates/storage/src/env.rs @@ -163,7 +163,7 @@ mod mocked { /// Return the context id. pub(super) const fn context_id() -> [u8; 32] { - [0; 32] + [236; 32] } /// Gets the current time. diff --git a/crates/storage/src/index.rs b/crates/storage/src/index.rs index ed8e431db..dae8a2d01 100644 --- a/crates/storage/src/index.rs +++ b/crates/storage/src/index.rs @@ -5,18 +5,18 @@ mod tests; use core::marker::PhantomData; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use borsh::{to_vec, BorshDeserialize, BorshSerialize}; use sha2::{Digest, Sha256}; use crate::address::Id; -use crate::entities::ChildInfo; +use crate::entities::{ChildInfo, Metadata, UpdatedAt}; use crate::interface::StorageError; use crate::store::{Key, StorageAdaptor}; /// Stored index information for an entity in the storage system. -#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] struct EntityIndex { /// Unique identifier of the entity. id: Id, @@ -35,11 +35,8 @@ struct EntityIndex { /// the hashes of its children to form the full hash. own_hash: [u8; 32], - /// Type identifier of the entity. This is noted so that the entity can be - /// deserialised correctly in the absence of other semantic information. It - /// is intended that the [`Path`](crate::address::Path) will be used to help - /// with this at some point, but at present paths are not fully utilised. - type_id: u8, + /// Metadata about the entity. + metadata: Metadata, } /// Manages the indexing system for efficient tree navigation. @@ -73,7 +70,6 @@ impl Index { parent_id: Id, collection: &str, child: ChildInfo, - type_id: u8, ) -> Result<(), StorageError> { let mut parent_index = Self::get_index(parent_id)?.ok_or(StorageError::IndexNotFound(parent_id))?; @@ -84,7 +80,7 @@ impl Index { children: BTreeMap::new(), full_hash: [0; 32], own_hash: [0; 32], - type_id, + metadata: child.metadata, }); child_index.parent_id = Some(parent_id); child_index.own_hash = child.merkle_hash(); @@ -92,11 +88,21 @@ impl Index { child_index.full_hash = Self::calculate_full_merkle_hash_for(child.id(), false)?; Self::save_index(&child_index)?; - parent_index + let children = parent_index .children .entry(collection.to_owned()) - .or_insert_with(Vec::new) - .push(ChildInfo::new(child.id(), child_index.full_hash)); + .or_insert_with(Vec::new); + + let mut ordered = children.drain(..).collect::>(); + + let _ignored = ordered.replace(ChildInfo::new( + child.id(), + child_index.full_hash, + child.metadata, + )); + + children.extend(ordered.into_iter()); + Self::save_index(&parent_index)?; parent_index.full_hash = Self::calculate_full_merkle_hash_for(parent_id, false)?; Self::save_index(&parent_index)?; @@ -123,14 +129,14 @@ impl Index { /// /// * [`add_child_to()`](Index::add_child_to()) /// - pub(crate) fn add_root(root: ChildInfo, type_id: u8) -> Result<(), StorageError> { + pub(crate) fn add_root(root: ChildInfo) -> Result<(), StorageError> { let mut index = Self::get_index(root.id())?.unwrap_or_else(|| EntityIndex { id: root.id(), parent_id: None, children: BTreeMap::new(), full_hash: [0; 32], own_hash: [0; 32], - type_id, + metadata: root.metadata, }); index.own_hash = root.merkle_hash(); Self::save_index(&index)?; @@ -231,13 +237,20 @@ impl Index { while let Some(parent_id) = Self::get_parent_id(current_id)? { let (parent_full_hash, _) = Self::get_hashes_for(parent_id)?.ok_or(StorageError::IndexNotFound(parent_id))?; - ancestors.push(ChildInfo::new(parent_id, parent_full_hash)); + let metadata = + Self::get_metadata(parent_id)?.ok_or(StorageError::IndexNotFound(parent_id))?; + ancestors.push(ChildInfo::new(parent_id, parent_full_hash, metadata)); current_id = parent_id; } Ok(ancestors) } + /// Retrieves the metadata of a given entity. + pub(crate) fn get_metadata(id: Id) -> Result, StorageError> { + Ok(Self::get_index(id)?.map(|index| index.metadata)) + } + /// Retrieves the children of a given entity. /// /// # Parameters @@ -277,9 +290,8 @@ impl Index { /// pub(crate) fn get_collection_names_for(parent_id: Id) -> Result, StorageError> { Ok(Self::get_index(parent_id)? - .ok_or(StorageError::IndexNotFound(parent_id))? - .children - .keys() + .iter() + .flat_map(|e| e.children.keys()) .cloned() .collect()) } @@ -341,23 +353,6 @@ impl Index { Ok(Self::get_index(child_id)?.and_then(|index| index.parent_id)) } - /// Retrieves the type of the given entity. - /// - /// # Parameters - /// - /// * `id` - The [`Id`] of the entity whose type is to be retrieved. - /// - /// # Errors - /// - /// If there's an issue retrieving or deserialising the index information, - /// an error will be returned. - /// - pub(crate) fn get_type_id(id: Id) -> Result { - Ok(Self::get_index(id)? - .ok_or(StorageError::IndexNotFound(id))? - .type_id) - } - /// Whether the collection has children. /// /// # Parameters @@ -404,7 +399,7 @@ impl Index { if let Some(child) = children.iter_mut().find(|c| c.id() == current_id) { let new_child_hash = Self::calculate_full_merkle_hash_for(current_id, false)?; if child.merkle_hash() != new_child_hash { - *child = ChildInfo::new(current_id, new_child_hash); + *child = ChildInfo::new(current_id, new_child_hash, child.metadata); } break; } @@ -508,12 +503,20 @@ impl Index { /// If there's an issue updating or saving the index, an error will be /// returned. /// - pub(crate) fn update_hash_for(id: Id, merkle_hash: [u8; 32]) -> Result<[u8; 32], StorageError> { + pub(crate) fn update_hash_for( + id: Id, + merkle_hash: [u8; 32], + updated_at: Option, + ) -> Result<[u8; 32], StorageError> { let mut index = Self::get_index(id)?.ok_or(StorageError::IndexNotFound(id))?; index.own_hash = merkle_hash; Self::save_index(&index)?; index.full_hash = Self::calculate_full_merkle_hash_for(id, false)?; + if let Some(updated_at) = updated_at { + index.metadata.updated_at = updated_at; + } Self::save_index(&index)?; + >::recalculate_ancestor_hashes_for(id)?; Ok(index.full_hash) } } diff --git a/crates/storage/src/integration.rs b/crates/storage/src/integration.rs index 0baea1231..cd4818224 100644 --- a/crates/storage/src/integration.rs +++ b/crates/storage/src/integration.rs @@ -1,29 +1,13 @@ //! Types used for integration with the runtime. use borsh::{BorshDeserialize, BorshSerialize}; -use calimero_sdk::serde::{Deserialize, Serialize}; use crate::interface::ComparisonData; /// Comparison data for synchronisation. -#[derive( - BorshDeserialize, - BorshSerialize, - Clone, - Debug, - Deserialize, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - Serialize, -)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] #[expect(clippy::exhaustive_structs, reason = "Exhaustive")] pub struct Comparison { - /// The type of the entity. - pub type_id: u8, - /// The serialised data of the entity. pub data: Option>, diff --git a/crates/storage/src/interface.rs b/crates/storage/src/interface.rs index cf3f34d13..295668171 100644 --- a/crates/storage/src/interface.rs +++ b/crates/storage/src/interface.rs @@ -218,20 +218,21 @@ use core::marker::PhantomData; use std::collections::BTreeMap; use std::io::Error as IoError; -use borsh::{to_vec, BorshDeserialize, BorshSerialize}; +use borsh::{from_slice, to_vec, BorshDeserialize, BorshSerialize}; use eyre::Report; use indexmap::IndexMap; -use serde::{Deserialize, Serialize}; +use serde::Serialize; +use sha2::{Digest, Sha256}; use thiserror::Error as ThisError; use crate::address::{Id, Path}; -use crate::entities::{ChildInfo, Collection, Data}; +use crate::entities::{ChildInfo, Collection, Data, Metadata}; use crate::index::Index; use crate::store::{Key, MainStorage, StorageAdaptor}; use crate::sync; /// Convenient type alias for the main storage system. -pub type Interface = MainInterface; +pub type MainInterface = Interface; /// Actions to be taken during synchronisation. /// @@ -262,19 +263,7 @@ pub type Interface = MainInterface; /// Note: This enum contains the entity type, for passing to the guest for /// processing along with the ID and data. /// -#[derive( - BorshDeserialize, - BorshSerialize, - Clone, - Debug, - Deserialize, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - Serialize, -)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] #[expect(clippy::exhaustive_enums, reason = "Exhaustive")] pub enum Action { /// Add an entity with the given ID, type, and data. @@ -282,14 +271,14 @@ pub enum Action { /// Unique identifier of the entity. id: Id, - /// Type identifier of the entity. - type_id: u8, - /// Serialised data of the entity. data: Vec, /// Details of the ancestors of the entity. ancestors: Vec, + + /// The metadata of the entity. + metadata: Metadata, }, /// Compare the entity with the given ID and type. Note that this results in @@ -316,31 +305,19 @@ pub enum Action { /// Unique identifier of the entity. id: Id, - /// Type identifier of the entity. - type_id: u8, - /// Serialised data of the entity. data: Vec, /// Details of the ancestors of the entity. ancestors: Vec, + + /// The metadata of the entity. + metadata: Metadata, }, } /// Data that is used for comparison between two nodes. -#[derive( - BorshDeserialize, - BorshSerialize, - Clone, - Debug, - Deserialize, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - Serialize, -)] +#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct ComparisonData { /// The unique identifier of the entity being compared. id: Id, @@ -359,16 +336,17 @@ pub struct ComparisonData { /// The list of children of the entity, with their IDs and hashes, /// organised by collection name. children: BTreeMap>, + + /// The metadata of the entity. + metadata: Metadata, } /// The primary interface for the storage system. #[derive(Debug, Default, Clone)] -#[expect(private_bounds, reason = "The StorageAdaptor is for internal use only")] #[non_exhaustive] -pub struct MainInterface(PhantomData); +pub struct Interface(PhantomData); -#[expect(private_bounds, reason = "The StorageAdaptor is for internal use only")] -impl MainInterface { +impl Interface { /// Adds a child to a collection. /// /// # Parameters @@ -385,18 +363,31 @@ impl MainInterface { /// pub fn add_child_to( parent_id: Id, - collection: &mut C, + collection: &C, child: &mut D, ) -> Result { - let own_hash = child.calculate_merkle_hash()?; + if !child.element().is_dirty() { + return Ok(false); + } + + let data = to_vec(child).map_err(|e| StorageError::SerializationError(e.into()))?; + + let own_hash = Sha256::digest(&data).into(); + >::add_child_to( parent_id, collection.name(), - ChildInfo::new(child.id(), own_hash), - D::type_id(), + ChildInfo::new(child.id(), own_hash, child.element().metadata), )?; - child.element_mut().merkle_hash = >::update_hash_for(child.id(), own_hash)?; - Self::save(child) + + let Some(hash) = Self::save_raw(child.id(), data, child.element().metadata)? else { + return Ok(false); + }; + + child.element_mut().is_dirty = false; + child.element_mut().merkle_hash = hash; + + Ok(true) } /// Applies an [`Action`] to the storage system. @@ -427,36 +418,50 @@ impl MainInterface { /// If there is an error when deserialising into the specified type, or when /// applying the [`Action`], an error will be returned. /// - pub fn apply_action(action: Action) -> Result, StorageError> { - let ancestors = match action { + pub fn apply_action(action: Action) -> Result<(), StorageError> { + match action { Action::Add { - data, ancestors, .. + id, + data, + // todo! we only need parent_id + ancestors, + metadata, } | Action::Update { - data, ancestors, .. + id, + data, + ancestors, + metadata, } => { - let mut entity = - D::try_from_slice(&data).map_err(StorageError::DeserializationError)?; - _ = Self::save(&mut entity)?; - ancestors + if let Some(parent) = ancestors.first() { + let own_hash = Sha256::digest(&data).into(); + + >::add_child_to( + parent.id(), + "no collection, remove this nonsense", + ChildInfo::new(id, own_hash, metadata), + )?; + } + + if Self::save_internal(id, &data, metadata)?.is_none() { + // we didn't save anything, so we skip updating the ancestors + return Ok(()); + } + + sync::push_action(Action::Compare { id }); } Action::Compare { .. } => { return Err(StorageError::ActionNotAllowed("Compare".to_owned())) } - Action::Delete { id, ancestors, .. } => { - _ = S::storage_remove(Key::Entry(id)); - ancestors + Action::Delete { + id, ancestors: _, .. + } => { + // todo! remove_child_from here + let _ignored = S::storage_remove(Key::Entry(id)); } }; - for ancestor in &ancestors { - let (current_hash, _) = >::get_hashes_for(ancestor.id())? - .ok_or(StorageError::IndexNotFound(ancestor.id()))?; - if current_hash != ancestor.merkle_hash() { - return Ok(Some(ancestor.id())); - } - } - Ok(None) + Ok(()) } /// The children of the [`Collection`]. @@ -574,34 +579,34 @@ impl MainInterface { /// This function will return an error if there are issues accessing local /// data or if there are problems during the comparison process. /// - pub fn compare_trees( + pub fn compare_trees( foreign_entity_data: Option>, - foreign_index_data: &ComparisonData, + foreign_index_data: ComparisonData, ) -> Result<(Vec, Vec), StorageError> { let mut actions = (vec![], vec![]); - let foreign_entity = foreign_entity_data - .as_ref() - .map(|d| D::try_from_slice(d)) - .transpose() - .map_err(StorageError::DeserializationError)?; + let id = foreign_index_data.id; + + let local_metadata = >::get_metadata(id)?; - let Some(local_entity) = Self::find_by_id::(foreign_index_data.id)? else { + let Some(local_entity) = Self::find_by_id_raw(id) else { if let Some(foreign_entity) = foreign_entity_data { // Local entity doesn't exist, so we need to add it actions.0.push(Action::Add { - id: foreign_index_data.id, - type_id: D::type_id(), + id, data: foreign_entity, - ancestors: foreign_index_data.ancestors.clone(), + ancestors: foreign_index_data.ancestors, + metadata: foreign_index_data.metadata, }); } return Ok(actions); }; - let (local_full_hash, local_own_hash) = >::get_hashes_for(local_entity.id())? - .ok_or(StorageError::IndexNotFound(local_entity.id()))?; + let local_metadata = local_metadata.ok_or(StorageError::IndexNotFound(id))?; + + let (local_full_hash, local_own_hash) = + >::get_hashes_for(id)?.ok_or(StorageError::IndexNotFound(id))?; // Compare full Merkle hashes if local_full_hash == foreign_index_data.full_hash { @@ -610,24 +615,23 @@ impl MainInterface { // Compare own hashes and timestamps if local_own_hash != foreign_index_data.own_hash { - match (foreign_entity, foreign_entity_data) { - (Some(foreign_entity), Some(foreign_entity_data)) - if local_entity.element().updated_at() - <= foreign_entity.element().updated_at() => + match foreign_entity_data { + Some(foreign_entity_data) + if local_metadata.updated_at <= foreign_index_data.metadata.updated_at => { actions.0.push(Action::Update { - id: local_entity.id(), - type_id: D::type_id(), + id, data: foreign_entity_data, - ancestors: foreign_index_data.ancestors.clone(), + ancestors: foreign_index_data.ancestors, + metadata: foreign_index_data.metadata, }); } _ => { actions.1.push(Action::Update { - id: foreign_index_data.id, - type_id: D::type_id(), - data: to_vec(&local_entity).map_err(StorageError::SerializationError)?, - ancestors: >::get_ancestors_of(local_entity.id())?, + id, + data: local_entity, + ancestors: >::get_ancestors_of(id)?, + metadata: local_metadata, }); } } @@ -635,7 +639,16 @@ impl MainInterface { // The list of collections from the type will be the same on both sides, as // the type is the same. - let local_collections = local_entity.collections(); + + let local_collection_names = >::get_collection_names_for(id)?; + + let local_collections = local_collection_names + .into_iter() + .map(|name| { + let children = >::get_children_of(id, &name)?; + Ok((name, children)) + }) + .collect::, StorageError>>()?; // Compare children for (local_coll_name, local_children) in &local_collections { @@ -649,19 +662,22 @@ impl MainInterface { .map(|child| (child.id(), child.merkle_hash())) .collect(); - for (id, local_hash) in &local_child_map { - match foreign_child_map.get(id) { + for (child_id, local_hash) in &local_child_map { + match foreign_child_map.get(child_id) { Some(foreign_hash) if local_hash != foreign_hash => { - actions.0.push(Action::Compare { id: *id }); - actions.1.push(Action::Compare { id: *id }); + actions.0.push(Action::Compare { id: *child_id }); + actions.1.push(Action::Compare { id: *child_id }); } None => { - if let Some(local_child) = Self::find_by_id_raw(*id)? { + if let Some(local_child) = Self::find_by_id_raw(*child_id) { + let metadata = >::get_metadata(*child_id)? + .ok_or(StorageError::IndexNotFound(*child_id))?; + actions.1.push(Action::Add { - id: *id, - type_id: >::get_type_id(*id)?, + id: *child_id, data: local_child, - ancestors: >::get_ancestors_of(local_entity.id())?, + ancestors: >::get_ancestors_of(id)?, + metadata, }); } } @@ -681,12 +697,15 @@ impl MainInterface { } else { // The entire collection is missing from the foreign entity for child in local_children { - if let Some(local_child) = Self::find_by_id_raw(child.id())? { + if let Some(local_child) = Self::find_by_id_raw(child.id()) { + let metadata = >::get_metadata(child.id())? + .ok_or(StorageError::IndexNotFound(child.id()))?; + actions.1.push(Action::Add { id: child.id(), - type_id: >::get_type_id(child.id())?, data: local_child, - ancestors: >::get_ancestors_of(local_entity.id())?, + ancestors: >::get_ancestors_of(child.id())?, + metadata, }); } } @@ -714,14 +733,18 @@ impl MainInterface { /// This function will return an error if there are issues accessing local /// data or if there are problems during the comparison process. /// - pub fn compare_affective( + pub fn compare_affective( data: Option>, comparison_data: ComparisonData, ) -> Result<(), StorageError> { - let (local, remote) = Interface::compare_trees::(data, &comparison_data)?; + let (local, remote) = >::compare_trees(data, comparison_data)?; for action in local { - let _ignored = Interface::apply_action::(action)?; + if let Action::Compare { .. } = &action { + continue; + } + + >::apply_action(action)?; } for action in remote { @@ -750,18 +773,21 @@ impl MainInterface { pub fn find_by_id(id: Id) -> Result, StorageError> { let value = S::storage_read(Key::Entry(id)); - match value { - Some(slice) => { - let mut entity = - D::try_from_slice(&slice).map_err(StorageError::DeserializationError)?; - // TODO: This is needed for now, as the field gets stored. Later we will - // TODO: implement a custom serialiser that will skip this field along with - // TODO: any others that should not be stored. - entity.element_mut().is_dirty = false; - Ok(Some(entity)) - } - None => Ok(None), - } + let Some(slice) = value else { + return Ok(None); + }; + + let mut item = from_slice::(&slice).map_err(StorageError::DeserializationError)?; + + let (full_hash, _) = + >::get_hashes_for(id)?.ok_or(StorageError::IndexNotFound(id))?; + + item.element_mut().merkle_hash = full_hash; + + item.element_mut().metadata = + >::get_metadata(id)?.ok_or(StorageError::IndexNotFound(id))?; + + Ok(Some(item)) } /// Finds an [`Element`](crate::entities::Element) by its unique identifier @@ -779,13 +805,8 @@ impl MainInterface { /// * `id` - The unique identifier of the [`Element`](crate::entities::Element) /// to find. /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, an error - /// will be returned. - /// - pub fn find_by_id_raw(id: Id) -> Result>, StorageError> { - Ok(S::storage_read(Key::Entry(id))) + pub fn find_by_id_raw(id: Id) -> Option> { + S::storage_read(Key::Entry(id)) } /// Finds one or more [`Element`](crate::entities::Element)s by path in the @@ -859,38 +880,32 @@ impl MainInterface { /// If an error occurs when interacting with the storage system, an error /// will be returned. /// - pub fn generate_comparison_data( - entity: Option<&D>, - ) -> Result { - let Some(entity) = entity else { - return Ok(ComparisonData { - id: Id::root(), - own_hash: [0; 32], - full_hash: [0; 32], - ancestors: Vec::new(), - children: BTreeMap::new(), - }); - }; + pub fn generate_comparison_data(id: Option) -> Result { + let id = id.unwrap_or_else(Id::root); - let (full_hash, own_hash) = >::get_hashes_for(entity.id())? - .ok_or(StorageError::IndexNotFound(entity.id()))?; + let (full_hash, own_hash) = >::get_hashes_for(id)?.unwrap_or_default(); - let ancestors = >::get_ancestors_of(entity.id())?; - let children = entity - .collections() - .into_keys() + let metadata = >::get_metadata(id)?.unwrap_or_default(); + + let ancestors = >::get_ancestors_of(id)?; + + let collection_names = >::get_collection_names_for(id)?; + + let children = collection_names + .into_iter() .map(|collection_name| { - >::get_children_of(entity.id(), &collection_name) + >::get_children_of(id, &collection_name) .map(|children| (collection_name.clone(), children)) }) .collect::, _>>()?; Ok(ComparisonData { - id: entity.id(), + id, own_hash, full_hash, ancestors, children, + metadata, }) } @@ -950,7 +965,7 @@ impl MainInterface { /// pub fn remove_child_from( parent_id: Id, - collection: &mut C, + collection: &C, child_id: Id, ) -> Result { let child_exists = >::get_children_of(parent_id, collection.name())? @@ -965,7 +980,9 @@ impl MainInterface { let (parent_full_hash, _) = >::get_hashes_for(parent_id)?.ok_or(StorageError::IndexNotFound(parent_id))?; let mut ancestors = >::get_ancestors_of(parent_id)?; - ancestors.insert(0, ChildInfo::new(parent_id, parent_full_hash)); + let metadata = + >::get_metadata(parent_id)?.ok_or(StorageError::IndexNotFound(parent_id))?; + ancestors.insert(0, ChildInfo::new(parent_id, parent_full_hash, metadata)); _ = S::storage_remove(Key::Entry(child_id)); @@ -1002,19 +1019,28 @@ impl MainInterface { /// This function will return an error if there are issues accessing local /// data or if there are problems during the comparison process. /// - pub fn commit_root(mut root: D) -> Result<(), StorageError> { - if root.id() != Id::root() { - return Err(StorageError::UnexpectedId(root.id())); - } + pub fn commit_root(root: Option) -> Result<(), StorageError> { + let id: Id = Id::root(); - // fixme! mutations (action application) doesn't propagate to the root - // fixme! so, a best-attempt approach is to force a save on the root - // fixme! deeply nested entries have undefined behaviour - root.element_mut().is_dirty = true; + let hash = if let Some(root) = root { + if root.id() != id { + return Err(StorageError::UnexpectedId(root.id())); + } - let _ = Self::save(&mut root)?; + if !root.element().is_dirty() { + return Ok(()); + } - sync::commit_root(&root.element().merkle_hash())?; + let data = to_vec(&root).map_err(|e| StorageError::SerializationError(e.into()))?; + + Self::save_raw(id, data, root.element().metadata)? + } else { + >::get_hashes_for(id)?.map(|(full_hash, _)| full_hash) + }; + + if let Some(hash) = hash { + sync::commit_root(&hash)?; + } Ok(()) } @@ -1083,78 +1109,95 @@ impl MainInterface { /// pub fn save(entity: &mut D) -> Result { if !entity.element().is_dirty() { - return Ok(true); + return Ok(false); } - let id = entity.id(); - if !D::is_root() && >::get_parent_id(id)?.is_none() { - return Err(StorageError::CannotCreateOrphan(id)); - } + let data = to_vec(entity).map_err(|e| StorageError::SerializationError(e.into()))?; - let is_new = Self::find_by_id::(id)?.is_none(); - if !is_new { - if let Some(existing) = Self::find_by_id::(id)? { - if existing.element().metadata.updated_at >= entity.element().metadata.updated_at { - return Ok(false); - } + let Some(hash) = Self::save_raw(entity.id(), data, entity.element().metadata)? else { + return Ok(false); + }; + + entity.element_mut().is_dirty = false; + entity.element_mut().merkle_hash = hash; + + Ok(true) + } + + /// Saves raw data to the storage system. + /// + /// # Errors + /// + /// If an error occurs when serialising data or interacting with the storage + /// system, an error will be returned. + /// + fn save_internal( + id: Id, + data: &[u8], + metadata: Metadata, + ) -> Result, StorageError> { + let last_metadata = >::get_metadata(id)?; + + if let Some(last_metadata) = &last_metadata { + if last_metadata.updated_at > metadata.updated_at { + return Ok(None); } - } else if D::is_root() { - >::add_root(ChildInfo::new(id, [0_u8; 32]), D::type_id())?; + } else if id.is_root() { + >::add_root(ChildInfo::new(id, [0_u8; 32], metadata))?; } - let own_hash = entity.calculate_merkle_hash()?; - entity.element_mut().merkle_hash = >::update_hash_for(id, own_hash)?; + let own_hash = Sha256::digest(data).into(); - _ = S::storage_write( - Key::Entry(id), - &to_vec(entity).map_err(StorageError::SerializationError)?, - ); + let full_hash = >::update_hash_for(id, own_hash, Some(metadata.updated_at))?; - entity.element_mut().is_dirty = false; + _ = S::storage_write(Key::Entry(id), data); + + let is_new = metadata.created_at == *metadata.updated_at; + + Ok(Some((is_new, full_hash))) + } + + /// Saves raw data to the storage system. + /// + /// # Errors + /// + /// If an error occurs when serialising data or interacting with the storage + /// system, an error will be returned. + /// + pub fn save_raw( + id: Id, + data: Vec, + metadata: Metadata, + ) -> Result, StorageError> { + if !id.is_root() && >::get_parent_id(id)?.is_none() { + return Err(StorageError::CannotCreateOrphan(id)); + } + + let Some((is_new, full_hash)) = Self::save_internal(id, &data, metadata)? else { + return Ok(None); + }; + + let ancestors = >::get_ancestors_of(id)?; let action = if is_new { Action::Add { id, - type_id: D::type_id(), - data: to_vec(entity).map_err(StorageError::SerializationError)?, - ancestors: >::get_ancestors_of(id)?, + data, + ancestors, + metadata, } } else { Action::Update { id, - type_id: D::type_id(), - data: to_vec(entity).map_err(StorageError::SerializationError)?, - ancestors: >::get_ancestors_of(id)?, + data, + ancestors, + metadata, } }; sync::push_action(action); - Ok(true) - } - - /// Type identifier of the entity. - /// - /// This is noted so that the entity can be deserialised correctly in the - /// absence of other semantic information. It is intended that the [`Path`] - /// will be used to help with this at some point, but at present paths are - /// not fully utilised. - /// - /// The value returned is arbitrary, and is up to the implementer to decide - /// what it should be. It is recommended that it be unique for each type of - /// entity. - /// - /// # Parameters - /// - /// * `id` - The [`Id`] of the entity whose type is to be retrieved. - /// - /// # Errors - /// - /// If an error occurs when interacting with the storage system, an error - /// will be returned. - /// - pub fn type_of(id: Id) -> Result { - >::get_type_id(id) + Ok(Some(full_hash)) } /// Validates the stored state. diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index bcbed3a05..14a8e4bcb 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -4,16 +4,11 @@ //! system, as a layer on top of the underlying database store. //! -#![forbid( - unreachable_pub, - unsafe_code, - unsafe_op_in_unsafe_fn, - clippy::missing_docs_in_private_items -)] +#![forbid(unreachable_pub, unsafe_op_in_unsafe_fn)] #![deny( + unsafe_code, clippy::expect_used, clippy::missing_errors_doc, - clippy::missing_panics_doc, clippy::panic, clippy::unwrap_in_result, clippy::unwrap_used diff --git a/crates/storage/src/store.rs b/crates/storage/src/store.rs index bcc839472..e54ed5149 100644 --- a/crates/storage/src/store.rs +++ b/crates/storage/src/store.rs @@ -41,7 +41,7 @@ impl Key { /// used for key operations during testing, such as modelling a foreign node's /// data store. /// -pub(crate) trait StorageAdaptor { +pub trait StorageAdaptor { /// Reads data from persistent storage. /// /// # Parameters diff --git a/crates/storage/src/tests/common.rs b/crates/storage/src/tests/common.rs index 41bd46cb2..3d5adc46d 100644 --- a/crates/storage/src/tests/common.rs +++ b/crates/storage/src/tests/common.rs @@ -1,11 +1,10 @@ use std::collections::BTreeMap; -use borsh::{to_vec, BorshDeserialize, BorshSerialize}; -use sha2::{Digest, Sha256}; +use borsh::{BorshDeserialize, BorshSerialize}; use velcro::btree_map; use crate::entities::{AtomicUnit, ChildInfo, Collection, Data, Element}; -use crate::interface::{Interface, StorageError}; +use crate::interface::MainInterface; /// For tests against empty data structs. #[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, PartialEq, PartialOrd)] @@ -14,21 +13,6 @@ pub struct EmptyData { } impl Data for EmptyData { - fn calculate_merkle_hash(&self) -> Result<[u8; 32], StorageError> { - let mut hasher = Sha256::new(); - hasher.update(self.element().id().as_bytes()); - hasher.update(&to_vec(&self.element().metadata).map_err(StorageError::SerializationError)?); - Ok(hasher.finalize().into()) - } - - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - _slice: &[u8], - ) -> Result<[u8; 32], StorageError> { - Err(StorageError::UnknownCollectionType(collection.to_owned())) - } - fn collections(&self) -> BTreeMap> { BTreeMap::new() } @@ -40,14 +24,6 @@ impl Data for EmptyData { fn element_mut(&mut self) -> &mut Element { &mut self.storage } - - fn is_root() -> bool { - true - } - - fn type_id() -> u8 { - 101 - } } /// A simple page with a title, and paragraphs as children. @@ -72,32 +48,9 @@ impl Page { impl AtomicUnit for Page {} impl Data for Page { - fn calculate_merkle_hash(&self) -> Result<[u8; 32], StorageError> { - let mut hasher = Sha256::new(); - hasher.update(self.element().id().as_bytes()); - hasher.update(&to_vec(&self.title).map_err(StorageError::SerializationError)?); - hasher.update(&to_vec(&self.element().metadata).map_err(StorageError::SerializationError)?); - Ok(hasher.finalize().into()) - } - - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - slice: &[u8], - ) -> Result<[u8; 32], StorageError> { - match collection { - "Paragraphs" => { - let child = ::Child::try_from_slice(slice) - .map_err(|e| StorageError::DeserializationError(e))?; - child.calculate_merkle_hash() - } - _ => Err(StorageError::UnknownCollectionType(collection.to_owned())), - } - } - fn collections(&self) -> BTreeMap> { btree_map! { - "Paragraphs".to_owned(): Interface::child_info_for(self.id(), &self.paragraphs).unwrap_or_default(), + "Paragraphs".to_owned(): MainInterface::child_info_for(self.id(), &self.paragraphs).unwrap_or_default(), } } @@ -108,14 +61,6 @@ impl Data for Page { fn element_mut(&mut self) -> &mut Element { &mut self.storage } - - fn is_root() -> bool { - true - } - - fn type_id() -> u8 { - 102 - } } /// A simple paragraph with text. No children. Belongs to a page. @@ -138,22 +83,6 @@ impl Paragraph { impl AtomicUnit for Paragraph {} impl Data for Paragraph { - fn calculate_merkle_hash(&self) -> Result<[u8; 32], StorageError> { - let mut hasher = Sha256::new(); - hasher.update(self.element().id().as_bytes()); - hasher.update(&to_vec(&self.text).map_err(StorageError::SerializationError)?); - hasher.update(&to_vec(&self.element().metadata).map_err(StorageError::SerializationError)?); - Ok(hasher.finalize().into()) - } - - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - _slice: &[u8], - ) -> Result<[u8; 32], StorageError> { - Err(StorageError::UnknownCollectionType(collection.to_owned())) - } - fn collections(&self) -> BTreeMap> { BTreeMap::new() } @@ -165,14 +94,6 @@ impl Data for Paragraph { fn element_mut(&mut self) -> &mut Element { &mut self.storage } - - fn is_root() -> bool { - false - } - - fn type_id() -> u8 { - 103 - } } /// A collection of paragraphs for a page. @@ -203,23 +124,6 @@ pub struct Person { } impl Data for Person { - fn calculate_merkle_hash(&self) -> Result<[u8; 32], StorageError> { - let mut hasher = Sha256::new(); - hasher.update(self.element().id().as_bytes()); - hasher.update(&to_vec(&self.name).map_err(StorageError::SerializationError)?); - hasher.update(&to_vec(&self.age).map_err(StorageError::SerializationError)?); - hasher.update(&to_vec(&self.element().metadata).map_err(StorageError::SerializationError)?); - Ok(hasher.finalize().into()) - } - - fn calculate_merkle_hash_for_child( - &self, - collection: &str, - _slice: &[u8], - ) -> Result<[u8; 32], StorageError> { - Err(StorageError::UnknownCollectionType(collection.to_owned())) - } - fn collections(&self) -> BTreeMap> { BTreeMap::new() } @@ -231,12 +135,4 @@ impl Data for Person { fn element_mut(&mut self) -> &mut Element { &mut self.storage } - - fn is_root() -> bool { - true - } - - fn type_id() -> u8 { - 104 - } } diff --git a/crates/storage/src/tests/entities.rs b/crates/storage/src/tests/entities.rs index 6db091247..63d094364 100644 --- a/crates/storage/src/tests/entities.rs +++ b/crates/storage/src/tests/entities.rs @@ -1,14 +1,11 @@ use std::time::{SystemTime, UNIX_EPOCH}; -use borsh::to_vec; use claims::{assert_ge, assert_le}; use sha2::{Digest, Sha256}; use velcro::btree_map; use super::*; -use crate::index::Index; -use crate::interface::Interface; -use crate::store::MainStorage; +use crate::interface::MainInterface; use crate::tests::common::{Page, Paragraph, Paragraphs, Person}; #[cfg(test)] @@ -26,81 +23,6 @@ mod collection__public_methods { mod data__public_methods { use super::*; - #[test] - fn calculate_merkle_hash() { - let element = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let person = Person { - name: "Alice".to_owned(), - age: 30, - storage: element.clone(), - }; - - let mut hasher = Sha256::new(); - hasher.update(person.id().as_bytes()); - hasher.update(&to_vec(&person.name).unwrap()); - hasher.update(&to_vec(&person.age).unwrap()); - hasher.update(&to_vec(&person.element().metadata).unwrap()); - let expected_hash: [u8; 32] = hasher.finalize().into(); - - assert_eq!(person.calculate_merkle_hash().unwrap(), expected_hash); - } - - #[test] - fn calculate_merkle_hash_for_child__valid() { - let parent = Element::new(&Path::new("::root::node").unwrap(), None); - let mut page = Page::new_from_element("Node", parent); - let child1 = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let mut para1 = Paragraph::new_from_element("Leaf1", child1); - assert!(Interface::save(&mut page).unwrap()); - - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para1).unwrap()); - let para1_slice = to_vec(¶1).unwrap(); - let para1_hash = page - .calculate_merkle_hash_for_child("Paragraphs", ¶1_slice) - .unwrap(); - let expected_hash1 = para1.calculate_merkle_hash().unwrap(); - assert_eq!(para1_hash, expected_hash1); - - let child2 = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let para2 = Paragraph::new_from_element("Leaf2", child2); - let para2_slice = to_vec(¶2).unwrap(); - let para2_hash = page - .calculate_merkle_hash_for_child("Paragraphs", ¶2_slice) - .unwrap(); - assert_ne!(para2_hash, para1_hash); - } - - #[test] - fn calculate_merkle_hash_for_child__invalid() { - let parent = Element::new(&Path::new("::root::node").unwrap(), None); - let mut page = Page::new_from_element("Node", parent); - let child1 = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let mut para1 = Paragraph::new_from_element("Leaf1", child1); - assert!(Interface::save(&mut page).unwrap()); - - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para1).unwrap()); - let invalid_slice = &[0, 1, 2, 3]; - let result = page.calculate_merkle_hash_for_child("Paragraphs", invalid_slice); - assert!(matches!(result, Err(StorageError::DeserializationError(_)))); - } - - #[test] - fn calculate_merkle_hash_for_child__unknown_collection() { - let parent = Element::new(&Path::new("::root::node").unwrap(), None); - let mut page = Page::new_from_element("Node", parent); - let child = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let mut para = Paragraph::new_from_element("Leaf", child); - assert!(Interface::save(&mut page).unwrap()); - - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para).unwrap()); - let para_slice = to_vec(¶).unwrap(); - let result = page.calculate_merkle_hash_for_child("unknown_collection", ¶_slice); - assert!(matches!( - result, - Err(StorageError::UnknownCollectionType(_)) - )); - } - #[test] fn collections() { let parent = Element::new(&Path::new("::root::node").unwrap(), None); @@ -108,7 +30,7 @@ mod data__public_methods { assert_eq!( page.collections(), btree_map! { - "Paragraphs".to_owned(): Interface::child_info_for(page.id(), &page.paragraphs).unwrap_or_default(), + "Paragraphs".to_owned(): MainInterface::child_info_for(page.id(), &page.paragraphs).unwrap_or_default(), } ); @@ -179,7 +101,7 @@ mod child_info__constructor { fn new() { let id = Id::random(); let hash = Sha256::digest(b"1").into(); - let info = ChildInfo::new(id, hash); + let info = ChildInfo::new(id, hash, Metadata::default()); assert_eq!(info.id, id); assert_eq!(info.merkle_hash, hash); } @@ -191,13 +113,21 @@ mod child_info__public_methods { #[test] fn id() { - let info = ChildInfo::new(Id::random(), Sha256::digest(b"1").into()); + let info = ChildInfo::new( + Id::random(), + Sha256::digest(b"1").into(), + Metadata::default(), + ); assert_eq!(info.id(), info.id); } #[test] fn merkle_hash() { - let info = ChildInfo::new(Id::random(), Sha256::digest(b"1").into()); + let info = ChildInfo::new( + Id::random(), + Sha256::digest(b"1").into(), + Metadata::default(), + ); assert_eq!(info.merkle_hash(), info.merkle_hash); } } @@ -208,7 +138,11 @@ mod child_info__traits { #[test] fn display() { - let info = ChildInfo::new(Id::random(), Sha256::digest(b"1").into()); + let info = ChildInfo::new( + Id::random(), + Sha256::digest(b"1").into(), + Metadata::default(), + ); assert_eq!( format!("{info}"), format!( @@ -247,8 +181,8 @@ mod element__constructor { assert_eq!(element.path, path); assert_ge!(element.metadata.created_at, timestamp1); assert_le!(element.metadata.created_at, timestamp2); - assert_ge!(element.metadata.updated_at, timestamp1); - assert_le!(element.metadata.updated_at, timestamp2); + assert_ge!(*element.metadata.updated_at, timestamp1); + assert_le!(*element.metadata.updated_at, timestamp2); assert!(element.is_dirty); } } @@ -281,7 +215,7 @@ mod element__public_methods { #[test] fn is_dirty() { - let element = Element::new(&Path::new("::root::node::leaf").unwrap(), None); + let element = Element::root(); assert!(element.is_dirty()); let mut person = Person { @@ -289,29 +223,13 @@ mod element__public_methods { age: 30, storage: element, }; - assert!(Interface::save(&mut person).unwrap()); + assert!(MainInterface::save(&mut person).unwrap()); assert!(!person.element().is_dirty()); person.element_mut().update(); assert!(person.element().is_dirty()); } - #[test] - fn merkle_hash() { - let element = Element::new(&Path::new("::root::node::leaf").unwrap(), None); - let mut person = Person { - name: "Steve".to_owned(), - age: 50, - storage: element.clone(), - }; - assert_eq!(person.element().merkle_hash(), [0_u8; 32]); - - assert!(Interface::save(&mut person).unwrap()); - let expected_hash = - >::calculate_full_merkle_hash_for(person.id(), false).unwrap(); - assert_eq!(person.element().merkle_hash(), expected_hash); - } - #[test] #[ignore] fn metadata() { @@ -327,14 +245,14 @@ mod element__public_methods { #[test] fn update() { - let element = Element::new(&Path::new("::root::node::leaf").unwrap(), None); + let element = Element::root(); let updated_at = element.metadata.updated_at; let mut person = Person { name: "Bob".to_owned(), age: 40, storage: element, }; - assert!(Interface::save(&mut person).unwrap()); + assert!(MainInterface::save(&mut person).unwrap()); assert!(!person.element().is_dirty); person.element_mut().update(); diff --git a/crates/storage/src/tests/index.rs b/crates/storage/src/tests/index.rs index 4c4d25995..92b9292b8 100644 --- a/crates/storage/src/tests/index.rs +++ b/crates/storage/src/tests/index.rs @@ -9,7 +9,12 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); @@ -29,8 +34,7 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child_id, child_own_hash), - 2, + ChildInfo::new(child_id, child_own_hash, Metadata::default()), ) .is_ok()); @@ -41,7 +45,7 @@ mod index__public_methods { assert_eq!(updated_root_index.children.len(), 1); assert_eq!( updated_root_index.children[collection_name][0], - ChildInfo::new(child_id, child_full_hash) + ChildInfo::new(child_id, child_full_hash, Metadata::default()) ); let child_index = >::get_index(child_id).unwrap().unwrap(); @@ -56,7 +60,12 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); @@ -73,35 +82,41 @@ mod index__public_methods { let grandchild_collection_name = "Pages"; let greatgrandchild_collection_name = "Paragraphs"; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let child_id = Id::random(); let child_hash = [2_u8; 32]; - let child_info = ChildInfo::new(child_id, child_hash); + let child_info = ChildInfo::new(child_id, child_hash, Metadata::default()); assert!( - >::add_child_to(root_id, child_collection_name, child_info, 2) - .is_ok() + >::add_child_to(root_id, child_collection_name, child_info).is_ok() ); let grandchild_id = Id::random(); let grandchild_hash = [3_u8; 32]; - let grandchild_info = ChildInfo::new(grandchild_id, grandchild_hash); + let grandchild_info = ChildInfo::new(grandchild_id, grandchild_hash, Metadata::default()); assert!(>::add_child_to( child_id, grandchild_collection_name, grandchild_info, - 3, ) .is_ok()); let greatgrandchild_id = Id::random(); let greatgrandchild_hash = [4_u8; 32]; - let greatgrandchild_info = ChildInfo::new(greatgrandchild_id, greatgrandchild_hash); + let greatgrandchild_info = ChildInfo::new( + greatgrandchild_id, + greatgrandchild_hash, + Metadata::default(), + ); assert!(>::add_child_to( grandchild_id, greatgrandchild_collection_name, greatgrandchild_info, - 4, ) .is_ok()); @@ -114,7 +129,8 @@ mod index__public_methods { >::get_hashes_for(grandchild_id) .unwrap() .unwrap() - .0 + .0, + Metadata::default() ) ); assert_eq!( @@ -124,7 +140,8 @@ mod index__public_methods { >::get_hashes_for(child_id) .unwrap() .unwrap() - .0 + .0, + Metadata::default() ) ); assert_eq!( @@ -134,7 +151,8 @@ mod index__public_methods { >::get_hashes_for(root_id) .unwrap() .unwrap() - .0 + .0, + Metadata::default() ) ); } @@ -144,10 +162,15 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let collection_name = "Books"; - let child1_id = Id::random(); + let child1_id = Id::from([2; 32]); let child1_own_hash = [2_u8; 32]; let child1_full_hash: [u8; 32] = hex::decode("75877bb41d393b5fb8455ce60ecd8dda001d06316496b14dfa7f895656eeca4a") @@ -155,7 +178,7 @@ mod index__public_methods { .try_into() .unwrap(); - let child2_id = Id::random(); + let child2_id = Id::from([3; 32]); let child2_own_hash = [3_u8; 32]; let child2_full_hash: [u8; 32] = hex::decode("648aa5c579fb30f38af744d97d6ec840c7a91277a499a0d780f3e7314eca090b") @@ -166,40 +189,49 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child1_id, child1_own_hash), - 2, + ChildInfo::new(child1_id, child1_own_hash, Metadata::default()), ) .is_ok()); assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child2_id, child2_own_hash), - 2, + ChildInfo::new(child2_id, child2_own_hash, Metadata::default()), ) .is_ok()); let children = >::get_children_of(root_id, collection_name).unwrap(); assert_eq!(children.len(), 2); - assert_eq!(children[0], ChildInfo::new(child1_id, child1_full_hash)); - assert_eq!(children[1], ChildInfo::new(child2_id, child2_full_hash)); + assert_eq!( + children[0], + ChildInfo::new(child1_id, child1_full_hash, Metadata::default()) + ); + assert_eq!( + children[1], + ChildInfo::new(child2_id, child2_full_hash, Metadata::default()) + ); } #[test] fn get_children_of__two_collections() { - let root_id = Id::random(); + let root_id = Id::from([1; 32]); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let collection1_name = "Pages"; - let child1_id = Id::random(); + let child1_id = Id::from([2; 32]); let child1_own_hash = [2_u8; 32]; let child1_full_hash: [u8; 32] = hex::decode("75877bb41d393b5fb8455ce60ecd8dda001d06316496b14dfa7f895656eeca4a") .unwrap() .try_into() .unwrap(); - let child2_id = Id::random(); + let child2_id = Id::from([3; 32]); let child2_own_hash = [3_u8; 32]; let child2_full_hash: [u8; 32] = hex::decode("648aa5c579fb30f38af744d97d6ec840c7a91277a499a0d780f3e7314eca090b") @@ -208,7 +240,7 @@ mod index__public_methods { .unwrap(); let collection2_name = "Reviews"; - let child3_id = Id::random(); + let child3_id = Id::from([4; 32]); let child3_own_hash = [4_u8; 32]; let child3_full_hash: [u8; 32] = hex::decode("9f4fb68f3e1dac82202f9aa581ce0bbf1f765df0e9ac3c8c57e20f685abab8ed") @@ -219,32 +251,38 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection1_name, - ChildInfo::new(child1_id, child1_own_hash), - 2, + ChildInfo::new(child1_id, child1_own_hash, Metadata::default()), ) .is_ok()); assert!(>::add_child_to( root_id, collection1_name, - ChildInfo::new(child2_id, child2_own_hash), - 2, + ChildInfo::new(child2_id, child2_own_hash, Metadata::default()), ) .is_ok()); assert!(>::add_child_to( root_id, collection2_name, - ChildInfo::new(child3_id, child3_own_hash), - 2, + ChildInfo::new(child3_id, child3_own_hash, Metadata::default()), ) .is_ok()); let children1 = >::get_children_of(root_id, collection1_name).unwrap(); assert_eq!(children1.len(), 2); - assert_eq!(children1[0], ChildInfo::new(child1_id, child1_full_hash)); - assert_eq!(children1[1], ChildInfo::new(child2_id, child2_full_hash)); + assert_eq!( + children1[0], + ChildInfo::new(child1_id, child1_full_hash, Metadata::default()) + ); + assert_eq!( + children1[1], + ChildInfo::new(child2_id, child2_full_hash, Metadata::default()) + ); let children2 = >::get_children_of(root_id, collection2_name).unwrap(); assert_eq!(children2.len(), 1); - assert_eq!(children2[0], ChildInfo::new(child3_id, child3_full_hash)); + assert_eq!( + children2[0], + ChildInfo::new(child3_id, child3_full_hash, Metadata::default()) + ); } #[test] @@ -252,7 +290,12 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let collection1_name = "Pages"; let collection2_name = "Chapters"; @@ -266,15 +309,13 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection1_name, - ChildInfo::new(child1_id, child1_own_hash), - 2, + ChildInfo::new(child1_id, child1_own_hash, Metadata::default()), ) .is_ok()); assert!(>::add_child_to( root_id, collection2_name, - ChildInfo::new(child2_id, child2_own_hash), - 2, + ChildInfo::new(child2_id, child2_own_hash, Metadata::default()), ) .is_ok()); @@ -290,7 +331,12 @@ mod index__public_methods { let root_own_hash = [1_u8; 32]; let root_full_hash = [0_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_own_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_own_hash, + Metadata::default() + ),) + .is_ok()); assert_eq!( >::get_hashes_for(root_id) @@ -305,7 +351,12 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); @@ -320,8 +371,7 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child_id, child_own_hash), - 2, + ChildInfo::new(child_id, child_own_hash, Metadata::default()), ) .is_ok()); @@ -332,28 +382,18 @@ mod index__public_methods { assert_eq!(>::get_parent_id(root_id).unwrap(), None); } - #[test] - fn get_type_id() { - let root_id = Id::random(); - let root_hash = [1_u8; 32]; - - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 99).is_ok()); - - let root_index = >::get_index(root_id).unwrap().unwrap(); - assert_eq!(root_index.id, root_id); - assert_eq!(root_index.own_hash, root_hash); - assert_eq!(root_index.type_id, 99); - - assert_eq!(>::get_type_id(root_id).unwrap(), 99,); - } - #[test] fn has_children() { let root_id = Id::random(); let root_hash = [1_u8; 32]; let collection_name = "Books"; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); assert!(!>::has_children(root_id, collection_name).unwrap()); let child_id = Id::random(); @@ -362,8 +402,7 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child_id, child_own_hash), - 2, + ChildInfo::new(child_id, child_own_hash, Metadata::default()), ) .is_ok()); assert!(>::has_children(root_id, collection_name).unwrap()); @@ -374,7 +413,12 @@ mod index__public_methods { let root_id = Id::random(); let root_hash = [1_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); @@ -389,8 +433,7 @@ mod index__public_methods { assert!(>::add_child_to( root_id, collection_name, - ChildInfo::new(child_id, child_own_hash), - 2, + ChildInfo::new(child_id, child_own_hash, Metadata::default()), ) .is_ok()); assert!( @@ -419,7 +462,7 @@ mod index__private_methods { children: BTreeMap::new(), full_hash: hash1, own_hash: hash2, - type_id: 1, + metadata: Metadata::default(), }; >::save_index(&index).unwrap(); @@ -439,7 +482,7 @@ mod index__private_methods { children: BTreeMap::new(), full_hash: hash1, own_hash: hash2, - type_id: 1, + metadata: Metadata::default(), }; >::save_index(&index).unwrap(); assert_eq!(>::get_index(id).unwrap().unwrap(), index); @@ -455,28 +498,27 @@ mod hashing { #[test] fn calculate_full_merkle_hash_for__with_children() { - let root_id = Id::random(); - assert!(>::add_root(ChildInfo::new(root_id, [0_u8; 32]), 1).is_ok()); + let root_id = Id::from([0; 32]); + assert!(>::add_root(ChildInfo::new( + root_id, + [0_u8; 32], + Metadata::default() + ),) + .is_ok()); let collection_name = "Children"; - let child1_id = Id::random(); + let child1_id = Id::from([1; 32]); let child1_hash = [1_u8; 32]; - let child1_info = ChildInfo::new(child1_id, child1_hash); - assert!( - >::add_child_to(root_id, collection_name, child1_info, 2).is_ok() - ); - let child2_id = Id::random(); + let child1_info = ChildInfo::new(child1_id, child1_hash, Metadata::default()); + assert!(>::add_child_to(root_id, collection_name, child1_info).is_ok()); + let child2_id = Id::from([2; 32]); let child2_hash = [2_u8; 32]; - let child2_info = ChildInfo::new(child2_id, child2_hash); - assert!( - >::add_child_to(root_id, collection_name, child2_info, 2).is_ok() - ); - let child3_id = Id::random(); + let child2_info = ChildInfo::new(child2_id, child2_hash, Metadata::default()); + assert!(>::add_child_to(root_id, collection_name, child2_info).is_ok()); + let child3_id = Id::from([3; 32]); let child3_hash = [3_u8; 32]; - let child3_info = ChildInfo::new(child3_id, child3_hash); - assert!( - >::add_child_to(root_id, collection_name, child3_info, 2).is_ok() - ); + let child3_info = ChildInfo::new(child3_id, child3_hash, Metadata::default()); + assert!(>::add_child_to(root_id, collection_name, child3_info).is_ok()); assert_eq!( hex::encode( @@ -512,17 +554,21 @@ mod hashing { let grandchild_collection_name = "Pages"; let greatgrandchild_collection_name = "Paragraphs"; - assert!(>::add_root(ChildInfo::new(root_id, root_hash), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.full_hash, [0_u8; 32]); let child_id = Id::random(); let child_hash = [2_u8; 32]; - let child_info = ChildInfo::new(child_id, child_hash); + let child_info = ChildInfo::new(child_id, child_hash, Metadata::default()); assert!( - >::add_child_to(root_id, child_collection_name, child_info, 2) - .is_ok() + >::add_child_to(root_id, child_collection_name, child_info).is_ok() ); let root_index_with_child = >::get_index(root_id).unwrap().unwrap(); @@ -538,12 +584,11 @@ mod hashing { let grandchild_id = Id::random(); let grandchild_hash = [3_u8; 32]; - let grandchild_info = ChildInfo::new(grandchild_id, grandchild_hash); + let grandchild_info = ChildInfo::new(grandchild_id, grandchild_hash, Metadata::default()); assert!(>::add_child_to( child_id, grandchild_collection_name, grandchild_info, - 3, ) .is_ok()); @@ -568,12 +613,15 @@ mod hashing { let greatgrandchild_id = Id::random(); let greatgrandchild_hash = [4_u8; 32]; - let greatgrandchild_info = ChildInfo::new(greatgrandchild_id, greatgrandchild_hash); + let greatgrandchild_info = ChildInfo::new( + greatgrandchild_id, + greatgrandchild_hash, + Metadata::default(), + ); assert!(>::add_child_to( grandchild_id, greatgrandchild_collection_name, greatgrandchild_info, - 4, ) .is_ok()); @@ -691,13 +739,18 @@ mod hashing { .try_into() .unwrap(); - assert!(>::add_root(ChildInfo::new(root_id, root_hash1), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash1, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); assert_eq!(root_index.full_hash, root_hash0); - assert!(>::update_hash_for(root_id, root_hash2).is_ok()); + assert!(>::update_hash_for(root_id, root_hash2, None).is_ok()); let updated_root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(updated_root_index.id, root_id); assert_eq!(updated_root_index.full_hash, root_full_hash); @@ -709,13 +762,18 @@ mod hashing { let root_hash1 = [1_u8; 32]; let root_hash2 = [2_u8; 32]; - assert!(>::add_root(ChildInfo::new(root_id, root_hash1), 1).is_ok()); + assert!(>::add_root(ChildInfo::new( + root_id, + root_hash1, + Metadata::default() + ),) + .is_ok()); let root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(root_index.id, root_id); assert_eq!(root_index.own_hash, root_hash1); - assert!(>::update_hash_for(root_id, root_hash2).is_ok()); + assert!(>::update_hash_for(root_id, root_hash2, None).is_ok()); let updated_root_index = >::get_index(root_id).unwrap().unwrap(); assert_eq!(updated_root_index.id, root_id); assert_eq!(updated_root_index.own_hash, root_hash2); diff --git a/crates/storage/src/tests/interface.rs b/crates/storage/src/tests/interface.rs index 3a44d1484..219cb8ade 100644 --- a/crates/storage/src/tests/interface.rs +++ b/crates/storage/src/tests/interface.rs @@ -14,11 +14,11 @@ mod interface__public_methods { #[test] fn children_of() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut page = Page::new_from_element("Node", element); - assert!(Interface::save(&mut page).unwrap()); + assert!(MainInterface::save(&mut page).unwrap()); assert_eq!( - Interface::children_of(page.id(), &page.paragraphs).unwrap(), + MainInterface::children_of(page.id(), &page.paragraphs).unwrap(), vec![] ); @@ -28,29 +28,31 @@ mod interface__public_methods { let mut para1 = Paragraph::new_from_element("Leaf1", child1); let mut para2 = Paragraph::new_from_element("Leaf2", child2); let mut para3 = Paragraph::new_from_element("Leaf3", child3); - assert!(Interface::save(&mut page).unwrap()); - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para1).unwrap()); - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para2).unwrap()); - assert!(Interface::add_child_to(page.id(), &mut page.paragraphs, &mut para3).unwrap()); + + assert!(!MainInterface::save(&mut page).unwrap()); + + assert!(MainInterface::add_child_to(page.id(), &mut page.paragraphs, &mut para1).unwrap()); + assert!(MainInterface::add_child_to(page.id(), &mut page.paragraphs, &mut para2).unwrap()); + assert!(MainInterface::add_child_to(page.id(), &mut page.paragraphs, &mut para3).unwrap()); assert_eq!( - Interface::children_of(page.id(), &page.paragraphs).unwrap(), + MainInterface::children_of(page.id(), &page.paragraphs).unwrap(), vec![para1, para2, para3] ); } #[test] fn find_by_id__existent() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut page = Page::new_from_element("Leaf", element); let id = page.id(); - assert!(Interface::save(&mut page).unwrap()); + assert!(MainInterface::save(&mut page).unwrap()); - assert_eq!(Interface::find_by_id(id).unwrap(), Some(page)); + assert_eq!(MainInterface::find_by_id(id).unwrap(), Some(page)); } #[test] fn find_by_id__non_existent() { - assert_none!(Interface::find_by_id::(Id::random()).unwrap()); + assert_none!(MainInterface::find_by_id::(Id::random()).unwrap()); } #[test] @@ -73,60 +75,49 @@ mod interface__public_methods { #[test] fn save__basic() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut page = Page::new_from_element("Node", element); - assert_ok!(Interface::save(&mut page)); - } - - #[test] - fn save__multiple() { - let element1 = Element::new(&Path::new("::root::node1").unwrap(), None); - let element2 = Element::new(&Path::new("::root::node2").unwrap(), None); - let mut page1 = Page::new_from_element("Node1", element1); - let mut page2 = Page::new_from_element("Node2", element2); - - assert!(Interface::save(&mut page1).unwrap()); - assert!(Interface::save(&mut page2).unwrap()); - assert_eq!(Interface::find_by_id(page1.id()).unwrap(), Some(page1)); - assert_eq!(Interface::find_by_id(page2.id()).unwrap(), Some(page2)); + assert_ok!(MainInterface::save(&mut page)); } #[test] fn save__not_dirty() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut page = Page::new_from_element("Node", element); - assert!(Interface::save(&mut page).unwrap()); + assert!(MainInterface::save(&mut page).unwrap()); page.element_mut().update(); - assert!(Interface::save(&mut page).unwrap()); + assert!(MainInterface::save(&mut page).unwrap()); } #[test] fn save__too_old() { - let element1 = Element::new(&Path::new("::root::node").unwrap(), None); + let element1 = Element::root(); let mut page1 = Page::new_from_element("Node", element1); let mut page2 = page1.clone(); - assert!(Interface::save(&mut page1).unwrap()); + assert!(MainInterface::save(&mut page1).unwrap()); page2.element_mut().update(); - sleep(Duration::from_millis(1)); + sleep(Duration::from_millis(2)); page1.element_mut().update(); - assert!(Interface::save(&mut page1).unwrap()); - assert!(!Interface::save(&mut page2).unwrap()); + assert!(MainInterface::save(&mut page1).unwrap()); + assert!(!MainInterface::save(&mut page2).unwrap()); } #[test] fn save__update_existing() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut page = Page::new_from_element("Node", element); let id = page.id(); - assert!(Interface::save(&mut page).unwrap()); + assert!(MainInterface::save(&mut page).unwrap()); + + page.storage.update(); // TODO: Modify the element's data and check it changed - assert!(Interface::save(&mut page).unwrap()); - assert_eq!(Interface::find_by_id(id).unwrap(), Some(page)); + assert!(MainInterface::save(&mut page).unwrap()); + assert_eq!(MainInterface::find_by_id(id).unwrap(), Some(page)); } #[test] @@ -163,120 +154,89 @@ mod interface__apply_actions { #[test] fn apply_action__add() { - let page = Page::new_from_element( - "Test Page", - Element::new(&Path::new("::test").unwrap(), None), - ); + let page = Page::new_from_element("Test Page", Element::root()); let serialized = to_vec(&page).unwrap(); let action = Action::Add { id: page.id(), - type_id: 102, data: serialized, ancestors: vec![], + metadata: page.element().metadata, }; - assert!(Interface::apply_action::(action).is_ok()); + assert!(MainInterface::apply_action(action).is_ok()); // Verify the page was added - let retrieved_page = Interface::find_by_id::(page.id()).unwrap(); + let retrieved_page = MainInterface::find_by_id::(page.id()).unwrap(); assert!(retrieved_page.is_some()); assert_eq!(retrieved_page.unwrap().title, "Test Page"); } #[test] fn apply_action__update() { - let mut page = Page::new_from_element( - "Old Title", - Element::new(&Path::new("::test").unwrap(), None), - ); - assert!(Interface::save(&mut page).unwrap()); + let mut page = Page::new_from_element("Old Title", Element::root()); + assert!(MainInterface::save(&mut page).unwrap()); page.title = "New Title".to_owned(); page.element_mut().update(); let serialized = to_vec(&page).unwrap(); let action = Action::Update { id: page.id(), - type_id: 102, data: serialized, ancestors: vec![], + metadata: page.element().metadata, }; - assert!(Interface::apply_action::(action).is_ok()); + assert!(MainInterface::apply_action(action).is_ok()); // Verify the page was updated - let retrieved_page = Interface::find_by_id::(page.id()).unwrap().unwrap(); + let retrieved_page = MainInterface::find_by_id::(page.id()) + .unwrap() + .unwrap(); assert_eq!(retrieved_page.title, "New Title"); } #[test] fn apply_action__delete() { - let mut page = Page::new_from_element( - "Test Page", - Element::new(&Path::new("::test").unwrap(), None), - ); - assert!(Interface::save(&mut page).unwrap()); + let mut page = Page::new_from_element("Test Page", Element::root()); + assert!(MainInterface::save(&mut page).unwrap()); let action = Action::Delete { id: page.id(), ancestors: vec![], }; - assert!(Interface::apply_action::(action).is_ok()); + assert!(MainInterface::apply_action(action).is_ok()); // Verify the page was deleted - let retrieved_page = Interface::find_by_id::(page.id()).unwrap(); + let retrieved_page = MainInterface::find_by_id::(page.id()).unwrap(); assert!(retrieved_page.is_none()); } #[test] fn apply_action__compare() { - let page = Page::new_from_element( - "Test Page", - Element::new(&Path::new("::test").unwrap(), None), - ); + let page = Page::new_from_element("Test Page", Element::root()); let action = Action::Compare { id: page.id() }; // Compare should fail - assert!(Interface::apply_action::(action).is_err()); - } - - #[test] - fn apply_action__wrong_type() { - let page = Page::new_from_element( - "Test Page", - Element::new(&Path::new("::test").unwrap(), None), - ); - let serialized = to_vec(&page).unwrap(); - let action = Action::Add { - id: page.id(), - type_id: 102, - data: serialized, - ancestors: vec![], - }; - - // Trying to apply a Page action as if it were a Paragraph should fail - assert!(Interface::apply_action::(action).is_err()); + assert!(MainInterface::apply_action(action).is_err()); } #[test] fn apply_action__non_existent_update() { - let page = Page::new_from_element( - "Test Page", - Element::new(&Path::new("::test").unwrap(), None), - ); + let page = Page::new_from_element("Test Page", Element::root()); let serialized = to_vec(&page).unwrap(); let action = Action::Update { id: page.id(), - type_id: 102, data: serialized, ancestors: vec![], + metadata: page.element().metadata, }; // Updating a non-existent page should still succeed (it will be added) - assert!(Interface::apply_action::(action).is_ok()); + assert!(MainInterface::apply_action(action).is_ok()); // Verify the page was added - let retrieved_page = Interface::find_by_id::(page.id()).unwrap(); + let retrieved_page = MainInterface::find_by_id::(page.id()).unwrap(); assert!(retrieved_page.is_some()); assert_eq!(retrieved_page.unwrap().title, "Test Page"); } @@ -286,13 +246,13 @@ mod interface__apply_actions { mod interface__comparison { use super::*; - type ForeignInterface = MainInterface>; + type ForeignInterface = Interface>; fn compare_trees( foreign: Option<&D>, - comparison_data: &ComparisonData, + comparison_data: ComparisonData, ) -> Result<(Vec, Vec), StorageError> { - Interface::compare_trees::( + MainInterface::compare_trees( foreign .map(to_vec) .transpose() @@ -303,11 +263,11 @@ mod interface__comparison { #[test] fn compare_trees__identical() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut local = Page::new_from_element("Test Page", element); let mut foreign = local.clone(); - assert!(Interface::save(&mut local).unwrap()); + assert!(MainInterface::save(&mut local).unwrap()); assert!(ForeignInterface::save(&mut foreign).unwrap()); assert_eq!( local.element().merkle_hash(), @@ -316,7 +276,7 @@ mod interface__comparison { let result = compare_trees( Some(&foreign), - &ForeignInterface::generate_comparison_data(Some(&foreign)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign.id())).unwrap(), ) .unwrap(); assert_eq!(result, (vec![], vec![])); @@ -324,7 +284,7 @@ mod interface__comparison { #[test] fn compare_trees__local_newer() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut local = Page::new_from_element("Test Page", element.clone()); let mut foreign = Page::new_from_element("Old Test Page", element); @@ -333,11 +293,11 @@ mod interface__comparison { // Make local newer sleep(Duration::from_millis(10)); local.element_mut().update(); - assert!(Interface::save(&mut local).unwrap()); + assert!(MainInterface::save(&mut local).unwrap()); let result = compare_trees( Some(&foreign), - &ForeignInterface::generate_comparison_data(Some(&foreign)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign.id())).unwrap(), ) .unwrap(); assert_eq!( @@ -346,9 +306,9 @@ mod interface__comparison { vec![], vec![Action::Update { id: local.id(), - type_id: 102, data: to_vec(&local).unwrap(), - ancestors: vec![] + ancestors: vec![], + metadata: local.element().metadata, }] ) ); @@ -356,11 +316,11 @@ mod interface__comparison { #[test] fn compare_trees__foreign_newer() { - let element = Element::new(&Path::new("::root::node").unwrap(), None); + let element = Element::root(); let mut local = Page::new_from_element("Old Test Page", element.clone()); let mut foreign = Page::new_from_element("Test Page", element); - assert!(Interface::save(&mut local).unwrap()); + assert!(MainInterface::save(&mut local).unwrap()); // Make foreign newer sleep(Duration::from_millis(10)); @@ -369,7 +329,7 @@ mod interface__comparison { let result = compare_trees( Some(&foreign), - &ForeignInterface::generate_comparison_data(Some(&foreign)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign.id())).unwrap(), ) .unwrap(); assert_eq!( @@ -377,9 +337,9 @@ mod interface__comparison { ( vec![Action::Update { id: foreign.id(), - type_id: 102, data: to_vec(&foreign).unwrap(), - ancestors: vec![] + ancestors: vec![], + metadata: foreign.element().metadata, }], vec![] ) @@ -388,7 +348,7 @@ mod interface__comparison { #[test] fn compare_trees__with_collections() { - let page_element = Element::new(&Path::new("::root::node").unwrap(), None); + let page_element = Element::root(); let para1_element = Element::new(&Path::new("::root::node::leaf1").unwrap(), None); let para2_element = Element::new(&Path::new("::root::node::leaf2").unwrap(), None); let para3_element = Element::new(&Path::new("::root::node::leaf3").unwrap(), None); @@ -402,14 +362,14 @@ mod interface__comparison { let mut foreign_para1 = Paragraph::new_from_element("Updated Paragraph 1", para1_element); let mut foreign_para3 = Paragraph::new_from_element("Foreign Paragraph 3", para3_element); - assert!(Interface::save(&mut local_page).unwrap()); - assert!(Interface::add_child_to( + assert!(MainInterface::save(&mut local_page).unwrap()); + assert!(MainInterface::add_child_to( local_page.id(), &mut local_page.paragraphs, &mut local_para1 ) .unwrap()); - assert!(Interface::add_child_to( + assert!(MainInterface::add_child_to( local_page.id(), &mut local_page.paragraphs, &mut local_para2 @@ -432,7 +392,7 @@ mod interface__comparison { let (local_actions, foreign_actions) = compare_trees( Some(&foreign_page), - &ForeignInterface::generate_comparison_data(Some(&foreign_page)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign_page.id())).unwrap(), ) .unwrap(); @@ -442,9 +402,9 @@ mod interface__comparison { // Page needs update due to different child structure Action::Update { id: foreign_page.id(), - type_id: 102, data: to_vec(&foreign_page).unwrap(), - ancestors: vec![] + ancestors: vec![], + metadata: foreign_page.element().metadata, }, // Para1 needs comparison due to different hash Action::Compare { @@ -463,9 +423,9 @@ mod interface__comparison { // Para2 needs to be added to foreign Action::Add { id: local_para2.id(), - type_id: 103, data: to_vec(&local_para2).unwrap(), - ancestors: vec![] + ancestors: vec![], + metadata: local_para2.element().metadata, }, // Para3 needs to be added locally, but we don't have the data, so we compare Action::Compare { @@ -477,7 +437,7 @@ mod interface__comparison { // Compare the updated para1 let (local_para1_actions, foreign_para1_actions) = compare_trees( Some(&foreign_para1), - &ForeignInterface::generate_comparison_data(Some(&foreign_para1)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign_para1.id())).unwrap(), ) .unwrap(); @@ -498,9 +458,13 @@ mod interface__comparison { local_para1_actions, vec![Action::Update { id: foreign_para1.id(), - type_id: 103, data: to_vec(&foreign_para1).unwrap(), - ancestors: vec![ChildInfo::new(foreign_page.id(), local_para1_ancestor_hash,)], + ancestors: vec![ChildInfo::new( + foreign_page.id(), + local_para1_ancestor_hash, + local_page.element().metadata + )], + metadata: foreign_para1.element().metadata, }] ); assert_eq!(foreign_para1_actions, vec![]); @@ -508,7 +472,7 @@ mod interface__comparison { // Compare para3 which doesn't exist locally let (local_para3_actions, foreign_para3_actions) = compare_trees( Some(&foreign_para3), - &ForeignInterface::generate_comparison_data(Some(&foreign_para3)).unwrap(), + ForeignInterface::generate_comparison_data(Some(foreign_para3.id())).unwrap(), ) .unwrap(); @@ -529,9 +493,13 @@ mod interface__comparison { local_para3_actions, vec![Action::Add { id: foreign_para3.id(), - type_id: 103, data: to_vec(&foreign_para3).unwrap(), - ancestors: vec![ChildInfo::new(foreign_page.id(), local_para3_ancestor_hash,)], + ancestors: vec![ChildInfo::new( + foreign_page.id(), + local_para3_ancestor_hash, + foreign_page.element().metadata + )], + metadata: foreign_para3.element().metadata, }] ); assert_eq!(foreign_para3_actions, vec![]); diff --git a/scripts/build-all-apps.sh b/scripts/build-all-apps.sh index 827c97e05..685d38acc 100755 --- a/scripts/build-all-apps.sh +++ b/scripts/build-all-apps.sh @@ -7,9 +7,8 @@ cd "$(dirname $0)" BUILD_SCRIPTS=( "../apps/kv-store/build.sh" - # todo! update to use CRDTs - # "../apps/gen-ext/build.sh" - # "../apps/only-peers/build.sh" + "../apps/gen-ext/build.sh" + "../apps/only-peers/build.sh" ) run_script() { From 805cc1872d9db8d5cb68241d0babb8a60411c712 Mon Sep 17 00:00:00 2001 From: alenmestrov Date: Tue, 26 Nov 2024 17:17:18 +0100 Subject: [PATCH 05/11] feat: Context and Proxy Starknet contract integration into Calimero (#981) --- Cargo.lock | 2081 ++++++++--------- Cargo.toml | 3 + contracts/context-config/src/mutate.rs | 12 + crates/context/config/Cargo.toml | 1 + .../context/config/src/client/env/config.rs | 2 +- .../config/src/client/env/config/mutate.rs | 80 +- .../client/env/config/query/application.rs | 49 +- .../env/config/query/application_revision.rs | 26 +- .../src/client/env/config/query/has_member.rs | 37 +- .../src/client/env/config/query/members.rs | 58 +- .../env/config/query/members_revision.rs | 31 +- .../src/client/env/config/query/privileges.rs | 62 +- .../client/env/config/query/proxy_contract.rs | 25 +- .../config/src/client/env/config/types.rs | 1 + .../src/client/env/config/types/starknet.rs | 598 +++++ crates/context/config/src/client/env/proxy.rs | 3 +- .../config/src/client/env/proxy/mutate.rs | 73 +- .../env/proxy/query/active_proposals.rs | 24 +- .../src/client/env/proxy/query/proposal.rs | 58 +- .../env/proxy/query/proposal_approvals.rs | 48 +- .../env/proxy/query/proposal_approvers.rs | 45 +- .../src/client/env/proxy/query/proposals.rs | 50 +- .../config/src/client/env/proxy/types.rs | 1 + .../src/client/env/proxy/types/starknet.rs | 390 +++ .../config/src/client/protocol/starknet.rs | 156 +- crates/context/config/src/types.rs | 1 + crates/merod/src/cli/init.rs | 2 +- crates/server/src/admin/handlers/proposals.rs | 5 +- 28 files changed, 2775 insertions(+), 1147 deletions(-) create mode 100644 crates/context/config/src/client/env/config/types.rs create mode 100644 crates/context/config/src/client/env/config/types/starknet.rs create mode 100644 crates/context/config/src/client/env/proxy/types.rs create mode 100644 crates/context/config/src/client/env/proxy/types/starknet.rs diff --git a/Cargo.lock b/Cargo.lock index d4aa212de..5cf06ebd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "aead" version = "0.5.2" @@ -102,9 +108,9 @@ checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" [[package]] name = "allocator-api2" -version = "0.2.18" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +checksum = "45862d1c77f2228b9e10bc609d5bc203d86ebc9b87ad8d5d5167a6c9abf739d9" [[package]] name = "android-tzdata" @@ -123,9 +129,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.15" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" dependencies = [ "anstyle", "anstyle-parse", @@ -138,49 +144,49 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.8" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anstyle-parse" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.4" +version = "3.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] name = "anyhow" -version = "1.0.86" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" [[package]] name = "arbitrary" -version = "1.3.2" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" dependencies = [ "derive_arbitrary", ] @@ -193,9 +199,9 @@ checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" [[package]] name = "arrayref" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" [[package]] name = "arrayvec" @@ -205,9 +211,9 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "arrayvec" -version = "0.7.4" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] name = "asn1-rs" @@ -221,7 +227,7 @@ dependencies = [ "nom", "num-traits", "rusticata-macros", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -237,7 +243,7 @@ dependencies = [ "nom", "num-traits", "rusticata-macros", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -261,7 +267,7 @@ checksum = "965c2d33e53cb6b267e148a4cb0760bc01f4904c1cd4bb4002a085bb016d1490" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", "synstructure 0.13.1", ] @@ -284,7 +290,7 @@ checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -299,9 +305,9 @@ dependencies = [ [[package]] name = "async-io" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964" +checksum = "43a2b323ccce0a1d90b449fd71f2a06ca7faa7c54c2751f06c9bd851fc061059" dependencies = [ "async-lock", "cfg-if 1.0.0", @@ -313,7 +319,7 @@ dependencies = [ "rustix", "slab", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -329,9 +335,9 @@ dependencies = [ [[package]] name = "async-stream" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" dependencies = [ "async-stream-impl", "futures-core", @@ -340,24 +346,24 @@ dependencies = [ [[package]] name = "async-stream-impl" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "async-trait" -version = "0.1.81" +version = "0.1.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -411,30 +417,30 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "axum" -version = "0.7.5" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" dependencies = [ "async-trait", "axum-core", - "base64 0.21.7", + "base64 0.22.1", "bytes", "futures-util", "http 1.1.0", "http-body 1.0.1", "http-body-util", - "hyper 1.4.1", + "hyper 1.5.1", "hyper-util", "itoa", "matchit", @@ -450,8 +456,8 @@ dependencies = [ "sha1", "sync_wrapper 1.0.1", "tokio", - "tokio-tungstenite 0.21.0", - "tower", + "tokio-tungstenite", + "tower 0.5.1", "tower-layer", "tower-service", "tracing", @@ -459,9 +465,9 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" dependencies = [ "async-trait", "bytes", @@ -472,7 +478,7 @@ dependencies = [ "mime", "pin-project-lite", "rustversion", - "sync_wrapper 0.1.2", + "sync_wrapper 1.0.1", "tower-layer", "tower-service", "tracing", @@ -490,14 +496,14 @@ dependencies = [ "http 1.1.0", "http-body 1.0.1", "http-body-util", - "hyper 1.4.1", + "hyper 1.5.1", "hyper-util", "pin-project-lite", "rustls 0.21.12", - "rustls-pemfile 2.1.3", + "rustls-pemfile 2.2.0", "tokio", "tokio-rustls 0.24.1", - "tower", + "tower 0.4.13", "tower-service", ] @@ -529,7 +535,7 @@ dependencies = [ "cc", "cfg-if 1.0.0", "libc", - "miniz_oxide", + "miniz_oxide 0.7.4", "object", "rustc-demangle", ] @@ -590,9 +596,9 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.69.4" +version = "0.69.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" dependencies = [ "bitflags 2.6.0", "cexpr", @@ -605,7 +611,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -696,9 +702,9 @@ dependencies = [ [[package]] name = "borsh" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" +checksum = "2506947f73ad44e344215ccd6403ac2ae18cd8e046e581a441bf8d199f257f03" dependencies = [ "borsh-derive", "cfg_aliases", @@ -706,16 +712,15 @@ dependencies = [ [[package]] name = "borsh-derive" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" +checksum = "c2593a3b8b938bd68373196c9832f516be11fa487ef4ae745eb282e6a56a7244" dependencies = [ "once_cell", "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.72", - "syn_derive", + "syn 2.0.87", ] [[package]] @@ -775,12 +780,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" -dependencies = [ - "serde", -] +checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" [[package]] name = "bytesize" @@ -824,7 +826,7 @@ dependencies = [ "futures-util", "serde", "sha2", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-util", ] @@ -859,7 +861,7 @@ dependencies = [ "eyre", "futures-util", "rand 0.8.5", - "reqwest 0.12.5", + "reqwest 0.12.9", "serde", "tokio", "tokio-util", @@ -875,17 +877,18 @@ dependencies = [ "ed25519-dalek", "either", "eyre", - "near-crypto 0.27.0", + "hex", + "near-crypto", "near-jsonrpc-client", "near-jsonrpc-primitives", - "near-primitives 0.27.0", - "reqwest 0.12.5", + "near-primitives", + "reqwest 0.12.9", "serde", "serde_json", "starknet", - "starknet-crypto", + "starknet-crypto 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "starknet-types-core", - "thiserror", + "thiserror 1.0.69", "url", ] @@ -896,7 +899,7 @@ dependencies = [ "calimero-context-config", "ed25519-dalek", "eyre", - "near-crypto 0.27.0", + "near-crypto", "near-sdk", "near-workspaces", "rand 0.8.5", @@ -929,7 +932,7 @@ dependencies = [ "multiaddr", "owo-colors", "serde", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-test", "tokio-util", @@ -971,7 +974,7 @@ dependencies = [ "calimero-primitives", "calimero-runtime", "serde", - "thiserror", + "thiserror 1.0.69", "tokio", ] @@ -987,7 +990,7 @@ dependencies = [ "serde", "serde_json", "sha2", - "thiserror", + "thiserror 1.0.69", "url", ] @@ -1016,7 +1019,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "ureq", "wasmer", "wasmer-types", @@ -1039,11 +1042,11 @@ dependencies = [ name = "calimero-sdk-macros" version = "0.1.0" dependencies = [ - "prettyplease 0.2.20", + "prettyplease 0.2.25", "proc-macro2", "quote", - "syn 2.0.72", - "thiserror", + "syn 2.0.87", + "thiserror 1.0.69", ] [[package]] @@ -1056,7 +1059,7 @@ dependencies = [ "serde", "serde_json", "serde_with", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -1090,16 +1093,16 @@ dependencies = [ "multiaddr", "rand 0.8.5", "rcgen 0.13.1", - "reqwest 0.12.5", + "reqwest 0.12.9", "rust-embed", "serde", "serde_json", "sha2", "starknet", - "starknet-crypto", - "thiserror", + "starknet-crypto 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "thiserror 1.0.69", "tokio", - "tower", + "tower 0.4.13", "tower-http", "tower-sessions", "tracing", @@ -1118,7 +1121,7 @@ dependencies = [ "eyre", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "url", ] @@ -1137,7 +1140,7 @@ dependencies = [ "rand 0.8.5", "serde", "sha2", - "thiserror", + "thiserror 1.0.69", "velcro", ] @@ -1149,7 +1152,7 @@ dependencies = [ "calimero-sdk", "calimero-storage", "quote", - "syn 2.0.72", + "syn 2.0.87", "trybuild", ] @@ -1167,15 +1170,15 @@ dependencies = [ "serde_json", "strum 0.26.3", "tempdir", - "thiserror", + "thiserror 1.0.69", "thunderdome", ] [[package]] name = "camino" -version = "1.1.7" +version = "1.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0ec6b951b160caa93cc0c7b209e5a3bff7aae9062213451ac99493cd844c239" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" dependencies = [ "serde", ] @@ -1200,7 +1203,7 @@ dependencies = [ "serde", "serde_bytes", "stacker", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -1212,17 +1215,18 @@ dependencies = [ "lazy_static", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "cc" -version = "1.1.7" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" dependencies = [ "jobserver", "libc", + "shlex", ] [[package]] @@ -1324,9 +1328,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.13" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fbb260a053428790f3de475e304ff84cdbc4face759ea7a3e64c1edd938a7fc" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" dependencies = [ "clap_builder", "clap_derive", @@ -1334,33 +1338,33 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.13" +version = "4.5.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64b17d7ea74e9f833c7dbf2cbe4fb12ff26783eda4782a8975b72f895c9b4d99" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim 0.11.1", + "strsim", ] [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" [[package]] name = "color-eyre" @@ -1391,9 +1395,9 @@ dependencies = [ [[package]] name = "colorchoice" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "concurrent-queue" @@ -1412,18 +1416,18 @@ checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" [[package]] name = "const_format" -version = "0.2.32" +version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" +checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" dependencies = [ "const_format_proc_macros", ] [[package]] name = "const_format_proc_macros" -version = "0.2.32" +version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" +checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" dependencies = [ "proc-macro2", "quote", @@ -1471,9 +1475,9 @@ dependencies = [ [[package]] name = "core-foundation-sys" -version = "0.8.6" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "core2" @@ -1499,9 +1503,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" dependencies = [ "libc", ] @@ -1521,7 +1525,7 @@ version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98b022ed2a5913a38839dfbafe6cf135342661293b08049843362df4301261dc" dependencies = [ - "arrayvec 0.7.4", + "arrayvec 0.7.6", "bumpalo", "cranelift-bforest", "cranelift-codegen-meta", @@ -1702,17 +1706,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", -] - -[[package]] -name = "darling" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" -dependencies = [ - "darling_core 0.14.4", - "darling_macro 0.14.4", + "syn 2.0.87", ] [[package]] @@ -1721,22 +1715,8 @@ version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" dependencies = [ - "darling_core 0.20.10", - "darling_macro 0.20.10", -] - -[[package]] -name = "darling_core" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" -dependencies = [ - "fnv", - "ident_case", - "proc-macro2", - "quote", - "strsim 0.10.0", - "syn 1.0.109", + "darling_core", + "darling_macro", ] [[package]] @@ -1749,19 +1729,8 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim 0.11.1", - "syn 2.0.72", -] - -[[package]] -name = "darling_macro" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" -dependencies = [ - "darling_core 0.14.4", - "quote", - "syn 1.0.109", + "strsim", + "syn 2.0.87", ] [[package]] @@ -1770,18 +1739,19 @@ version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ - "darling_core 0.20.10", + "darling_core", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "dashmap" -version = "5.5.3" +version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" dependencies = [ "cfg-if 1.0.0", + "crossbeam-utils", "hashbrown 0.14.5", "lock_api", "once_cell", @@ -1875,44 +1845,13 @@ dependencies = [ [[package]] name = "derive_arbitrary" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.72", -] - -[[package]] -name = "derive_builder" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8" -dependencies = [ - "derive_builder_macro", -] - -[[package]] -name = "derive_builder_core" -version = "0.12.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f" +checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" dependencies = [ - "darling 0.14.4", "proc-macro2", "quote", - "syn 1.0.109", -] - -[[package]] -name = "derive_builder_macro" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e" -dependencies = [ - "derive_builder_core", - "syn 1.0.109", + "syn 2.0.87", ] [[package]] @@ -1925,7 +1864,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -1998,16 +1937,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", -] - -[[package]] -name = "document-features" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" -dependencies = [ - "litrs", + "syn 2.0.87", ] [[package]] @@ -2077,23 +2007,23 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "encoding_rs" -version = "0.8.34" +version = "0.8.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" dependencies = [ "cfg-if 1.0.0", ] [[package]] name = "enum-as-inner" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ffccbb6966c05b32ef8fbac435df276c4ae4d3dc55a8cd0eb9745e6c12f546a" +checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" dependencies = [ - "heck 0.4.1", + "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -2133,7 +2063,7 @@ checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -2151,10 +2081,10 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59c3b24c345d8c314966bdc1832f6c2635bfcce8e7cf363bd115987bba2ee242" dependencies = [ - "darling 0.20.10", + "darling", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -2191,7 +2121,7 @@ dependencies = [ "serde_json", "sha2", "sha3", - "thiserror", + "thiserror 1.0.69", "uuid 0.8.2", ] @@ -2208,7 +2138,7 @@ dependencies = [ "serde", "serde_json", "sha3", - "thiserror", + "thiserror 1.0.69", "uint", ] @@ -2278,9 +2208,9 @@ checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" [[package]] name = "fastrand" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" [[package]] name = "ff" @@ -2300,14 +2230,14 @@ checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" [[package]] name = "filetime" -version = "0.2.23" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" +checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586" dependencies = [ "cfg-if 1.0.0", "libc", - "redox_syscall 0.4.1", - "windows-sys 0.52.0", + "libredox", + "windows-sys 0.59.0", ] [[package]] @@ -2333,21 +2263,21 @@ dependencies = [ [[package]] name = "fixedstr" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54e049f021908beff8f8c430a99f5c136d3be69f1667346e581f446b173bc012" +checksum = "60aba7afd9b1b9e1950c2b7e8bcac3cc44a273c62a02717dedca2d0a1aee694d" dependencies = [ "serde", ] [[package]] name = "flate2" -version = "1.0.31" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f211bbe8e69bbd0cfdea405084f128ae8b4aaa6b0b522fc8f2b009084797920" +checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" dependencies = [ "crc32fast", - "miniz_oxide", + "miniz_oxide 0.8.0", ] [[package]] @@ -2365,6 +2295,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + [[package]] name = "foreign-types" version = "0.3.2" @@ -2428,9 +2364,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" dependencies = [ "futures-channel", "futures-core", @@ -2453,9 +2389,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", "futures-sink", @@ -2463,15 +2399,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" [[package]] name = "futures-executor" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" dependencies = [ "futures-core", "futures-task", @@ -2481,15 +2417,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" [[package]] name = "futures-lite" -version = "2.3.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" +checksum = "cef40d21ae2c515b51041df9ed313ed21e572df340ea58a922a0aefe7e8891a1" dependencies = [ "futures-core", "pin-project-lite", @@ -2497,13 +2433,13 @@ dependencies = [ [[package]] name = "futures-macro" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -2523,21 +2459,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f2f12607f92c69b12ed746fabf9ca4f5c482cba46679c1a75b874ed7c26adb" dependencies = [ "futures-io", - "rustls 0.23.12", + "rustls 0.23.17", "rustls-pki-types", ] [[package]] name = "futures-sink" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" [[package]] name = "futures-task" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" [[package]] name = "futures-ticker" @@ -2558,9 +2494,9 @@ checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" [[package]] name = "futures-util" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-channel", "futures-core", @@ -2689,9 +2625,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.5" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" +checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" dependencies = [ "atomic-waker", "bytes", @@ -2733,9 +2669,14 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] [[package]] name = "headers" @@ -2818,7 +2759,7 @@ dependencies = [ "once_cell", "rand 0.8.5", "socket2", - "thiserror", + "thiserror 1.0.69", "tinyvec", "tokio", "tracing", @@ -2841,7 +2782,7 @@ dependencies = [ "rand 0.8.5", "resolv-conf", "smallvec", - "thiserror", + "thiserror 1.0.69", "tokio", "tracing", ] @@ -2948,9 +2889,9 @@ checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a" [[package]] name = "httparse" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" [[package]] name = "httpdate" @@ -2960,9 +2901,9 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] name = "hyper" -version = "0.14.30" +version = "0.14.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" +checksum = "8c08302e8fa335b151b788c775ff56e7a03ae64ff85c548ee820fecb70356e85" dependencies = [ "bytes", "futures-channel", @@ -2984,14 +2925,14 @@ dependencies = [ [[package]] name = "hyper" -version = "1.4.1" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +checksum = "97818827ef4f364230e16705d4706e2897df2bb60617d6ca15d598025a3c481f" dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.5", + "h2 0.4.7", "http 1.1.0", "http-body 1.0.1", "httparse", @@ -3011,7 +2952,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.12", - "hyper 0.14.30", + "hyper 0.14.31", "rustls 0.21.12", "tokio", "tokio-rustls 0.24.1", @@ -3019,15 +2960,15 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.27.2" +version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http 1.1.0", - "hyper 1.4.1", + "hyper 1.5.1", "hyper-util", - "rustls 0.23.12", + "rustls 0.23.17", "rustls-pki-types", "tokio", "tokio-rustls 0.26.0", @@ -3041,7 +2982,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" dependencies = [ "bytes", - "hyper 0.14.30", + "hyper 0.14.31", "native-tls", "tokio", "tokio-native-tls", @@ -3055,7 +2996,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", "http-body-util", - "hyper 1.4.1", + "hyper 1.5.1", "hyper-util", "native-tls", "tokio", @@ -3065,36 +3006,35 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.7" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9" +checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4" dependencies = [ "bytes", "futures-channel", "futures-util", "http 1.1.0", "http-body 1.0.1", - "hyper 1.4.1", + "hyper 1.5.1", "pin-project-lite", "socket2", "tokio", - "tower", "tower-service", "tracing", ] [[package]] name = "iana-time-zone" -version = "0.1.60" +version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" dependencies = [ "android_system_properties", "core-foundation-sys", "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows-core", + "windows-core 0.52.0", ] [[package]] @@ -3122,7 +3062,7 @@ dependencies = [ "serde_bytes", "serde_cbor", "sha2", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3149,7 +3089,7 @@ dependencies = [ "quote", "serde", "serde_tokenstream", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -3233,7 +3173,125 @@ dependencies = [ "data-encoding", "serde", "sha2", - "thiserror", + "thiserror 1.0.69", +] + +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" + +[[package]] +name = "icu_properties" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", ] [[package]] @@ -3254,12 +3312,23 @@ dependencies = [ [[package]] name = "idna" -version = "0.5.0" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" dependencies = [ - "unicode-bidi", - "unicode-normalization", + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" +dependencies = [ + "icu_normalizer", + "icu_properties", ] [[package]] @@ -3274,9 +3343,9 @@ dependencies = [ [[package]] name = "if-watch" -version = "3.2.0" +version = "3.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6b0422c86d7ce0e97169cc42e04ae643caf278874a7a3c87b8150a220dc7e1e" +checksum = "cdf9d64cfcf380606e64f9a0bcf493616b65331199f984151a6fa11a7b3cde38" dependencies = [ "async-io", "core-foundation", @@ -3285,8 +3354,12 @@ dependencies = [ "if-addrs", "ipnet", "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-proto", + "netlink-sys", "rtnetlink", - "system-configuration", + "system-configuration 0.6.1", "tokio", "windows", ] @@ -3302,7 +3375,7 @@ dependencies = [ "bytes", "futures", "http 0.2.12", - "hyper 0.14.30", + "hyper 0.14.31", "log", "rand 0.8.5", "tokio", @@ -3372,7 +3445,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.15.0", + "hashbrown 0.15.1", "serde", ] @@ -3423,14 +3496,14 @@ dependencies = [ "socket2", "widestring", "windows-sys 0.48.0", - "winreg 0.50.0", + "winreg", ] [[package]] name = "ipnet" -version = "2.9.0" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" [[package]] name = "is_executable" @@ -3467,9 +3540,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "7a73e9fe3c49d7afb2ace819fa181a287ce54a0983eda4e0eb05c22f82ffe534" [[package]] name = "jobserver" @@ -3482,9 +3555,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" dependencies = [ "wasm-bindgen", ] @@ -3498,7 +3571,7 @@ dependencies = [ "jsonptr", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3630,9 +3703,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.164" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" [[package]] name = "libloading" @@ -3680,7 +3753,7 @@ dependencies = [ "multiaddr", "pin-project", "rw-stream-sink", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3728,7 +3801,7 @@ dependencies = [ "rand 0.8.5", "rw-stream-sink", "smallvec", - "thiserror", + "thiserror 1.0.69", "tracing", "unsigned-varint 0.8.0", "void", @@ -3753,7 +3826,7 @@ dependencies = [ "lru 0.11.1", "quick-protobuf", "quick-protobuf-codec 0.2.0", - "thiserror", + "thiserror 1.0.69", "tracing", "void", ] @@ -3819,11 +3892,11 @@ dependencies = [ "libp2p-core", "libp2p-identity", "libp2p-swarm", - "lru 0.12.4", + "lru 0.12.5", "quick-protobuf", "quick-protobuf-codec 0.3.1", "smallvec", - "thiserror", + "thiserror 1.0.69", "tracing", "void", ] @@ -3842,7 +3915,7 @@ dependencies = [ "rand 0.8.5", "serde", "sha2", - "thiserror", + "thiserror 1.0.69", "tracing", "zeroize", ] @@ -3853,7 +3926,7 @@ version = "0.45.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cc5767727d062c4eac74dd812c998f0e488008e82cce9c33b463d38423f9ad2" dependencies = [ - "arrayvec 0.7.4", + "arrayvec 0.7.6", "asynchronous-codec 0.7.0", "bytes", "either", @@ -3870,7 +3943,7 @@ dependencies = [ "rand 0.8.5", "sha2", "smallvec", - "thiserror", + "thiserror 1.0.69", "tracing", "uint", "void", @@ -3938,7 +4011,7 @@ dependencies = [ "sha2", "snow", "static_assertions", - "thiserror", + "thiserror 1.0.69", "tracing", "x25519-dalek", "zeroize", @@ -3979,9 +4052,9 @@ dependencies = [ "quinn", "rand 0.8.5", "ring 0.17.8", - "rustls 0.23.12", + "rustls 0.23.17", "socket2", - "thiserror", + "thiserror 1.0.69", "tokio", "tracing", ] @@ -4005,7 +4078,7 @@ dependencies = [ "quick-protobuf-codec 0.3.1", "rand 0.8.5", "static_assertions", - "thiserror", + "thiserror 1.0.69", "tracing", "void", "web-time", @@ -4030,7 +4103,7 @@ dependencies = [ "quick-protobuf", "quick-protobuf-codec 0.2.0", "rand 0.8.5", - "thiserror", + "thiserror 1.0.69", "tracing", "void", ] @@ -4084,7 +4157,7 @@ dependencies = [ "libp2p-core", "libp2p-identity", "libp2p-swarm-derive", - "lru 0.12.4", + "lru 0.12.5", "multistream-select", "once_cell", "rand 0.8.5", @@ -4103,7 +4176,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -4137,7 +4210,7 @@ dependencies = [ "ring 0.16.20", "rustls 0.21.12", "rustls-webpki 0.101.7", - "thiserror", + "thiserror 1.0.69", "x509-parser 0.15.1", "yasna", ] @@ -4154,9 +4227,9 @@ dependencies = [ "libp2p-identity", "rcgen 0.11.3", "ring 0.17.8", - "rustls 0.23.12", + "rustls 0.23.17", "rustls-webpki 0.101.7", - "thiserror", + "thiserror 1.0.69", "x509-parser 0.16.0", "yasna", ] @@ -4186,10 +4259,10 @@ dependencies = [ "either", "futures", "libp2p-core", - "thiserror", + "thiserror 1.0.69", "tracing", "yamux 0.12.1", - "yamux 0.13.3", + "yamux 0.13.4", ] [[package]] @@ -4200,6 +4273,7 @@ checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ "bitflags 2.6.0", "libc", + "redox_syscall", ] [[package]] @@ -4220,9 +4294,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.18" +version = "1.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c15da26e5af7e25c90b37a2d75cdbf940cf4a55316de9d84c679c9b8bfabf82e" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" dependencies = [ "cc", "pkg-config", @@ -4242,21 +4316,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] -name = "litrs" -version = "0.4.1" +name = "litemap" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" +checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" [[package]] name = "local-ip-address" -version = "0.6.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136ef34e18462b17bf39a7826f8f3bbc223341f8e83822beb8b77db9a3d49696" +checksum = "3669cf5561f8d27e8fc84cc15e58350e70f557d4d65f70e3154e54cd2f8e1782" dependencies = [ "libc", "neli", - "thiserror", - "windows-sys 0.48.0", + "thiserror 1.0.69", + "windows-sys 0.59.0", ] [[package]] @@ -4287,11 +4361,11 @@ dependencies = [ [[package]] name = "lru" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37ee39891760e7d94734f6f63fedc29a2e4a152f836120753a72503f09fcf904" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" dependencies = [ - "hashbrown 0.14.5", + "hashbrown 0.15.1", ] [[package]] @@ -4305,9 +4379,9 @@ dependencies = [ [[package]] name = "lz4-sys" -version = "1.10.0" +version = "1.11.1+lz4-1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109de74d5d2353660401699a4174a4ff23fcc649caf553df71933c7fb45ad868" +checksum = "6bd8c0d6c6ed0cd30b3652886bb8711dc4bb01d637a68105a3d5158039b418e6" dependencies = [ "cc", "libc", @@ -4349,15 +4423,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "memmap2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" -dependencies = [ - "libc", -] - [[package]] name = "memmap2" version = "0.6.2" @@ -4401,12 +4466,12 @@ dependencies = [ "futures-util", "libp2p", "notify", - "reqwest 0.12.5", + "reqwest 0.12.9", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "tokio", - "tokio-tungstenite 0.24.0", + "tokio-tungstenite", "url", ] @@ -4433,10 +4498,10 @@ dependencies = [ "hex", "libp2p", "multiaddr", - "near-crypto 0.27.0", + "near-crypto", "starknet", "tokio", - "toml_edit 0.22.20", + "toml_edit", "tracing", "tracing-subscriber", "url", @@ -4473,6 +4538,15 @@ dependencies = [ "adler", ] +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + [[package]] name = "mio" version = "0.8.11" @@ -4487,9 +4561,9 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ "hermit-abi 0.3.9", "libc", @@ -4505,9 +4579,9 @@ checksum = "7843ec2de400bcbc6a6328c958dc38e5359da6e93e72e37bc5246bf1ae776389" [[package]] name = "multiaddr" -version = "0.18.1" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b852bc02a2da5feed68cd14fa50d0774b92790a5bdbfa932a813926c8472070" +checksum = "fe6351f60b488e04c1d21bc69e56b89cb3f5e8f5d22557d6e8031bdfd79b6961" dependencies = [ "arrayref", "byteorder", @@ -4518,7 +4592,7 @@ dependencies = [ "percent-encoding", "serde", "static_assertions", - "unsigned-varint 0.7.2", + "unsigned-varint 0.8.0", "url", ] @@ -4535,12 +4609,12 @@ dependencies = [ [[package]] name = "multihash" -version = "0.19.1" +version = "0.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076d548d76a0e2a0d4ab471d0b1c36c577786dfc4471242035d97a12a735c492" +checksum = "cc41f430805af9d1cf4adae4ed2149c759b877b01d909a1f40256188d09345d2" dependencies = [ "core2", - "unsigned-varint 0.7.2", + "unsigned-varint 0.8.0", ] [[package]] @@ -4646,11 +4720,11 @@ dependencies = [ "bytesize", "chrono", "derive_more", - "near-config-utils 0.27.0", - "near-crypto 0.27.0", - "near-parameters 0.27.0", - "near-primitives 0.27.0", - "near-time 0.27.0", + "near-config-utils", + "near-crypto", + "near-parameters", + "near-primitives", + "near-time", "num-rational", "serde", "serde_json", @@ -4660,18 +4734,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "near-config-utils" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96c1682d13e9a8a62ea696395bf17afc4ed4b60535223251168217098c27a50" -dependencies = [ - "anyhow", - "json_comments", - "thiserror", - "tracing", -] - [[package]] name = "near-config-utils" version = "0.27.0" @@ -4680,39 +4742,13 @@ checksum = "4e7b41110a20f1d82bb06f06e4800068c5ade6d8ff844787f8753bc2ce7b16f7" dependencies = [ "anyhow", "json_comments", - "thiserror", + "thiserror 1.0.69", "tracing", ] [[package]] name = "near-crypto" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "907fdcefa3a42976cd6a8bf626fe2a87eb0d3b3ff144adc67cf32d53c9494b32" -dependencies = [ - "blake2", - "borsh", - "bs58 0.4.0", - "curve25519-dalek", - "derive_more", - "ed25519-dalek", - "hex", - "near-account-id", - "near-config-utils 0.26.0", - "near-stdx 0.26.0", - "once_cell", - "primitive-types 0.10.1", - "rand 0.8.5", - "secp256k1", - "serde", - "serde_json", - "subtle", - "thiserror", -] - -[[package]] -name = "near-crypto" -version = "0.27.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b17944c8d0f274c684227d79fcd46d583b1e36064b597c53a9ebec187a86f3" dependencies = [ @@ -4724,25 +4760,16 @@ dependencies = [ "ed25519-dalek", "hex", "near-account-id", - "near-config-utils 0.27.0", + "near-config-utils", "near-schema-checker-lib", - "near-stdx 0.27.0", + "near-stdx", "primitive-types 0.10.1", "rand 0.8.5", "secp256k1", "serde", "serde_json", "subtle", - "thiserror", -] - -[[package]] -name = "near-fmt" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a36518bfcf2177096d4298d9158ba698ffd6944cb035ecc0938b098337b933c" -dependencies = [ - "near-primitives-core 0.26.0", + "thiserror 1.0.69", ] [[package]] @@ -4751,7 +4778,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1eff0731995774d1498f017c968a3ebbfdadad84f556afea4b83679f6706ac9" dependencies = [ - "near-primitives-core 0.27.0", + "near-primitives-core", ] [[package]] @@ -4775,13 +4802,13 @@ dependencies = [ "lazy_static", "log", "near-chain-configs", - "near-crypto 0.27.0", + "near-crypto", "near-jsonrpc-primitives", - "near-primitives 0.27.0", - "reqwest 0.12.5", + "near-primitives", + "reqwest 0.12.9", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -4792,33 +4819,15 @@ checksum = "0c89197294a74af70fd5d06b4876dc2a400ffbdff6131e640e75fcb4fd194649" dependencies = [ "arbitrary", "near-chain-configs", - "near-crypto 0.27.0", - "near-primitives 0.27.0", + "near-crypto", + "near-primitives", "near-schema-checker-lib", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "time", ] -[[package]] -name = "near-parameters" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e41afea5c5e84763586bafc5f5e1b63d90ef4e5454e18406cab8df120178db8d" -dependencies = [ - "borsh", - "enum-map", - "near-account-id", - "near-primitives-core 0.26.0", - "num-rational", - "serde", - "serde_repr", - "serde_yaml", - "strum 0.24.1", - "thiserror", -] - [[package]] name = "near-parameters" version = "0.27.0" @@ -4828,57 +4837,14 @@ dependencies = [ "borsh", "enum-map", "near-account-id", - "near-primitives-core 0.27.0", + "near-primitives-core", "near-schema-checker-lib", "num-rational", "serde", "serde_repr", "serde_yaml", "strum 0.24.1", - "thiserror", -] - -[[package]] -name = "near-primitives" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "165c2dc0fc20d839cfd7948d930ef5e8a4ed2b095abe83e0076ef5d4a5df58ed" -dependencies = [ - "arbitrary", - "base64 0.21.7", - "borsh", - "bytes", - "bytesize", - "cfg-if 1.0.0", - "chrono", - "derive_more", - "easy-ext", - "enum-map", - "hex", - "itertools 0.10.5", - "near-crypto 0.26.0", - "near-fmt 0.26.0", - "near-parameters 0.26.0", - "near-primitives-core 0.26.0", - "near-rpc-error-macro", - "near-stdx 0.26.0", - "near-structs-checker-lib", - "near-time 0.26.0", - "num-rational", - "once_cell", - "ordered-float", - "primitive-types 0.10.1", - "rand 0.8.5", - "rand_chacha", - "serde", - "serde_json", - "serde_with", - "sha3", - "smart-default", - "strum 0.24.1", - "thiserror", - "tracing", - "zstd 0.13.2", + "thiserror 1.0.69", ] [[package]] @@ -4900,13 +4866,13 @@ dependencies = [ "enum-map", "hex", "itertools 0.10.5", - "near-crypto 0.27.0", - "near-fmt 0.27.0", - "near-parameters 0.27.0", - "near-primitives-core 0.27.0", + "near-crypto", + "near-fmt", + "near-parameters", + "near-primitives-core", "near-schema-checker-lib", - "near-stdx 0.27.0", - "near-time 0.27.0", + "near-stdx", + "near-time", "num-rational", "ordered-float", "primitive-types 0.10.1", @@ -4918,32 +4884,11 @@ dependencies = [ "sha3", "smart-default", "strum 0.24.1", - "thiserror", + "thiserror 1.0.69", "tracing", "zstd 0.13.2", ] -[[package]] -name = "near-primitives-core" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51fd53f992168589c52022dd220c84a7f2ede92251631a06a3817e4b22af5836" -dependencies = [ - "arbitrary", - "base64 0.21.7", - "borsh", - "bs58 0.4.0", - "derive_more", - "enum-map", - "near-account-id", - "near-structs-checker-lib", - "num-rational", - "serde", - "serde_repr", - "sha2", - "thiserror", -] - [[package]] name = "near-primitives-core" version = "0.27.0" @@ -4962,29 +4907,7 @@ dependencies = [ "serde", "serde_repr", "sha2", - "thiserror", -] - -[[package]] -name = "near-rpc-error-core" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df598b0785a3e36d7e4fb73afcdf20536988b13d07cead71dfa777db4783e552" -dependencies = [ - "quote", - "serde", - "syn 2.0.72", -] - -[[package]] -name = "near-rpc-error-macro" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "647ef261df99ad877c08c97af2f10368c8b8cde0968250d3482a5a249e9f3926" -dependencies = [ - "near-rpc-error-core", - "serde", - "syn 2.0.72", + "thiserror 1.0.69", ] [[package]] @@ -5024,19 +4947,19 @@ checksum = "a1bca8c93ff0ad17138c147323a07f036d11c9e1602e3bc2ac9d29c3cf78b89d" [[package]] name = "near-sdk" -version = "5.5.0" +version = "5.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e296b02c85539c16659e171242d6c6bbea87eec7c9ef860d8dfd3fb3168a18a" +checksum = "581c7e6b1962fca1637bf668c18b498328100f8974aa798ed44702ba77c3bdac" dependencies = [ "base64 0.22.1", "borsh", "bs58 0.5.1", "near-account-id", - "near-crypto 0.26.0", + "near-crypto", "near-gas", - "near-parameters 0.26.0", - "near-primitives 0.26.0", - "near-primitives-core 0.26.0", + "near-parameters", + "near-primitives", + "near-primitives-core", "near-sdk-macros", "near-sys", "near-token", @@ -5049,73 +4972,33 @@ dependencies = [ [[package]] name = "near-sdk-macros" -version = "5.5.0" +version = "5.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0adc79466aa556f56a995c0db34a933b32597ab92bbb0e526597118899c8bcaf" +checksum = "0818798144d69a248d76e22b04d1b89b84b80575c96c4af0f7f0332b22487a08" dependencies = [ "Inflector", - "darling 0.20.10", + "darling", "proc-macro2", "quote", "serde", "serde_json", "strum 0.26.3", "strum_macros 0.26.4", - "syn 2.0.72", + "syn 2.0.87", ] -[[package]] -name = "near-stdx" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d5c43f6181873287ddaa25edcc2943d0f2d5da9588231516f2ed0549db1fbac" - [[package]] name = "near-stdx" version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "427b4e4af5e32f682064772da8b1a7558b3f090e6151c8804cff24ee6c5c4966" -[[package]] -name = "near-structs-checker-core" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e53379bee876286de4ad31dc7f9fde8db12527c39d401d94f4d42cd021b8fce" - -[[package]] -name = "near-structs-checker-lib" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f984805f225c9b19681a124af7783078459e87ea63dde751b62e7cb404b1d6a" -dependencies = [ - "near-structs-checker-core", - "near-structs-checker-macro", -] - -[[package]] -name = "near-structs-checker-macro" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66319954983ae164fd0b714ae9d8b09edc26c74d9b3a1f51e564bb14720adb8e" - [[package]] name = "near-sys" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbf4ca5c805cb78700e10e43484902d8da05f25788db277999d209568aaf4c8e" -[[package]] -name = "near-time" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1d37b864f04d9aebbc3b88ac63ba989d94f221de694bcc8af639cc284a89f64" -dependencies = [ - "once_cell", - "serde", - "time", - "tokio", -] - [[package]] name = "near-time" version = "0.27.0" @@ -5138,22 +5021,23 @@ dependencies = [ [[package]] name = "near-vm-runner" -version = "0.26.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a2fecdbec69a1748bd80aa0d73e4de0064d2d8097f429677d3e37a6bde3b2d" +checksum = "7f31fac542ba77915f4f63ec1ba79edc559dd03ba471bf1feb08fcb434e5187f" dependencies = [ "blst", "borsh", "bytesize", "ed25519-dalek", "enum-map", - "lru 0.12.4", - "near-crypto 0.26.0", - "near-parameters 0.26.0", - "near-primitives-core 0.26.0", - "near-stdx 0.26.0", + "lru 0.12.5", + "near-crypto", + "near-parameters", + "near-primitives-core", + "near-schema-checker-lib", + "near-stdx", "num-rational", - "once_cell", + "rayon", "ripemd", "rustix", "serde", @@ -5162,7 +5046,7 @@ dependencies = [ "sha3", "strum 0.24.1", "tempfile", - "thiserror", + "thiserror 1.0.69", "tracing", "zeropool-bn", ] @@ -5182,20 +5066,20 @@ dependencies = [ "libc", "near-abi-client", "near-account-id", - "near-crypto 0.27.0", + "near-crypto", "near-gas", "near-jsonrpc-client", "near-jsonrpc-primitives", - "near-primitives 0.27.0", + "near-primitives", "near-sandbox-utils", "near-token", "rand 0.8.5", - "reqwest 0.12.5", + "reqwest 0.12.9", "serde", "serde_json", "sha2", "tempfile", - "thiserror", + "thiserror 1.0.69", "tokio", "tokio-retry", "tracing", @@ -5256,21 +5140,20 @@ dependencies = [ [[package]] name = "netlink-packet-core" -version = "0.4.2" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "345b8ab5bd4e71a2986663e88c56856699d060e78e152e6e9d7966fcd5491297" +checksum = "72724faf704479d67b388da142b186f916188505e7e0b26719019c525882eda4" dependencies = [ "anyhow", "byteorder", - "libc", "netlink-packet-utils", ] [[package]] name = "netlink-packet-route" -version = "0.12.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9ea4302b9759a7a88242299225ea3688e63c85ea136371bb6cf94fd674efaab" +checksum = "053998cea5a306971f88580d0829e90f270f940befd7cf928da179d4187a5a66" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -5289,21 +5172,21 @@ dependencies = [ "anyhow", "byteorder", "paste", - "thiserror", + "thiserror 1.0.69", ] [[package]] name = "netlink-proto" -version = "0.10.0" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65b4b14489ab424703c092062176d52ba55485a89c076b4f9db05092b7223aa6" +checksum = "86b33524dc0968bfad349684447bfce6db937a9ac3332a1fe60c0c5a5ce63f21" dependencies = [ "bytes", "futures", "log", "netlink-packet-core", "netlink-sys", - "thiserror", + "thiserror 1.0.69", "tokio", ] @@ -5322,9 +5205,9 @@ dependencies = [ [[package]] name = "nix" -version = "0.24.3" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" dependencies = [ "bitflags 1.3.2", "cfg-if 1.0.0", @@ -5477,18 +5360,18 @@ dependencies = [ [[package]] name = "oid-registry" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c958dd45046245b9c3c2547369bb634eb461670b2e7e0de552905801a648d1d" +checksum = "a8d8034d9489cdaf79228eb9f6a3b8d7bb32ba00d6645ebd48eef4077ceb5bd9" dependencies = [ "asn1-rs 0.6.2", ] [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "only-peers" @@ -5506,9 +5389,9 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "openssl" -version = "0.10.66" +version = "0.10.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" +checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" dependencies = [ "bitflags 2.6.0", "cfg-if 1.0.0", @@ -5527,7 +5410,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -5538,9 +5421,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.103" +version = "0.9.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" dependencies = [ "cc", "libc", @@ -5556,9 +5439,9 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" [[package]] name = "ordered-float" -version = "4.2.2" +version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a91171844676f8c7990ce64959210cd2eaef32c2612c50f9fae9f8aaa6065a6" +checksum = "c65ee1f9701bf938026630b455d5315f490640234259037edb259798b3bcf85e" dependencies = [ "borsh", "num-traits", @@ -5588,7 +5471,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -5614,35 +5497,36 @@ dependencies = [ [[package]] name = "parity-scale-codec" -version = "3.6.12" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "306800abfa29c7f16596b5970a588435e3d5b3149683d00c12b699cc19f895ee" +checksum = "8be4817d39f3272f69c59fe05d0535ae6456c2dc2fa1ba02910296c7e0a5c590" dependencies = [ - "arrayvec 0.7.4", + "arrayvec 0.7.6", "bitvec", "byte-slice-cast", "impl-trait-for-tuples", "parity-scale-codec-derive", + "rustversion", "serde", ] [[package]] name = "parity-scale-codec-derive" -version = "3.6.12" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d830939c76d294956402033aee57a6da7b438f2294eb94864c37b0569053a42c" +checksum = "8781a75c6205af67215f382092b6e0a4ff3734798523e69073d4bcd294ec767b" dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.87", ] [[package]] name = "parking" -version = "2.2.0" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" [[package]] name = "parking_lot" @@ -5662,7 +5546,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if 1.0.0", "libc", - "redox_syscall 0.5.3", + "redox_syscall", "smallvec", "windows-targets 0.52.6", ] @@ -5714,29 +5598,29 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project" -version = "1.1.5" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +checksum = "be57f64e946e500c8ee36ef6331845d40a93055567ec57e8fae13efd33759b95" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.5" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "pin-project-lite" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" [[package]] name = "pin-utils" @@ -5756,15 +5640,15 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "polling" -version = "3.7.2" +version = "3.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b" +checksum = "a604568c3202727d1507653cb121dbd627a58684eb09a820fd746bee38b4442f" dependencies = [ "cfg-if 1.0.0", "concurrent-queue", @@ -5772,7 +5656,7 @@ dependencies = [ "pin-project-lite", "rustix", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -5836,12 +5720,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.20" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" +checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -5869,11 +5753,11 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" dependencies = [ - "toml_edit 0.21.1", + "toml_edit", ] [[package]] @@ -5917,7 +5801,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", "version_check", "yansi", ] @@ -5942,7 +5826,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -5953,7 +5837,7 @@ dependencies = [ "calimero-context-config-near", "ed25519-dalek", "eyre", - "near-crypto 0.27.0", + "near-crypto", "near-sdk", "near-workspaces", "rand 0.8.5", @@ -5964,9 +5848,9 @@ dependencies = [ [[package]] name = "psm" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa37f80ca58604976033fae9515a8a2989fc13797d953f7c04fb8fa36a11f205" +checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810" dependencies = [ "cc", ] @@ -6015,7 +5899,7 @@ dependencies = [ "asynchronous-codec 0.6.2", "bytes", "quick-protobuf", - "thiserror", + "thiserror 1.0.69", "unsigned-varint 0.7.2", ] @@ -6028,15 +5912,15 @@ dependencies = [ "asynchronous-codec 0.7.0", "bytes", "quick-protobuf", - "thiserror", + "thiserror 1.0.69", "unsigned-varint 0.8.0", ] [[package]] name = "quinn" -version = "0.11.3" +version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b22d8e7369034b9a7132bc2008cac12f2013c8132b45e0554e6e20e2617f2156" +checksum = "62e96808277ec6f97351a2380e6c25114bc9e67037775464979f3037c92d05ef" dependencies = [ "bytes", "futures-io", @@ -6044,41 +5928,45 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash 2.0.0", - "rustls 0.23.12", + "rustls 0.23.17", "socket2", - "thiserror", + "thiserror 2.0.3", "tokio", "tracing", ] [[package]] name = "quinn-proto" -version = "0.11.6" +version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba92fb39ec7ad06ca2582c0ca834dfeadcaf06ddfc8e635c80aa7e1c05315fdd" +checksum = "a2fe5ef3495d7d2e377ff17b1a8ce2ee2ec2a18cde8b6ad6619d65d0701c135d" dependencies = [ "bytes", + "getrandom", "rand 0.8.5", "ring 0.17.8", "rustc-hash 2.0.0", - "rustls 0.23.12", + "rustls 0.23.17", + "rustls-pki-types", "slab", - "thiserror", + "thiserror 2.0.3", "tinyvec", "tracing", + "web-time", ] [[package]] name = "quinn-udp" -version = "0.5.4" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bffec3605b73c6f1754535084a85229fa8a30f86014e6c81aeec4abb68b0285" +checksum = "7d5a626c6807713b15cac82a6acaccd6043c9a5408c24baae07611fec3f243da" dependencies = [ + "cfg_aliases", "libc", "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -6212,31 +6100,22 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - -[[package]] -name = "redox_syscall" -version = "0.5.3" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ "bitflags 2.6.0", ] [[package]] name = "redox_users" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" dependencies = [ "getrandom", "libredox", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -6253,14 +6132,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.6" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.7", - "regex-syntax 0.8.4", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", ] [[package]] @@ -6274,13 +6153,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.7" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.4", + "regex-syntax 0.8.5", ] [[package]] @@ -6291,9 +6170,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "region" @@ -6339,7 +6218,7 @@ dependencies = [ "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", - "hyper 0.14.30", + "hyper 0.14.31", "hyper-rustls 0.24.2", "hyper-tls 0.5.0", "ipnet", @@ -6356,7 +6235,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper 0.1.2", - "system-configuration", + "system-configuration 0.5.1", "tokio", "tokio-native-tls", "tokio-rustls 0.24.1", @@ -6366,26 +6245,26 @@ dependencies = [ "wasm-bindgen-futures", "web-sys", "webpki-roots 0.25.4", - "winreg 0.50.0", + "winreg", ] [[package]] name = "reqwest" -version = "0.12.5" +version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" +checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ "base64 0.22.1", "bytes", "encoding_rs", "futures-core", "futures-util", - "h2 0.4.5", + "h2 0.4.7", "http 1.1.0", "http-body 1.0.1", "http-body-util", - "hyper 1.4.1", - "hyper-rustls 0.27.2", + "hyper 1.5.1", + "hyper-rustls 0.27.3", "hyper-tls 0.6.0", "hyper-util", "ipnet", @@ -6396,12 +6275,12 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls-pemfile 2.1.3", + "rustls-pemfile 2.2.0", "serde", "serde_json", "serde_urlencoded", "sync_wrapper 1.0.1", - "system-configuration", + "system-configuration 0.6.1", "tokio", "tokio-native-tls", "tokio-util", @@ -6411,7 +6290,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", - "winreg 0.52.0", + "windows-registry", ] [[package]] @@ -6475,9 +6354,9 @@ dependencies = [ [[package]] name = "rkyv" -version = "0.7.44" +version = "0.7.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cba464629b3394fc4dbc6f940ff8f5b4ff5c7aef40f29166fd4ad12acbc99c0" +checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" dependencies = [ "bitvec", "bytecheck", @@ -6489,14 +6368,14 @@ dependencies = [ "rkyv_derive", "seahash", "tinyvec", - "uuid 1.10.0", + "uuid 1.11.0", ] [[package]] name = "rkyv_derive" -version = "0.7.44" +version = "0.7.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7dddfff8de25e6f62b9d64e6e432bf1c6736c57d20323e15ee10435fbda7c65" +checksum = "503d1d27590a2b0a3a4ca4c94755aa2875657196ecbf401a42eff41d7de532c0" dependencies = [ "proc-macro2", "quote", @@ -6525,16 +6404,19 @@ dependencies = [ [[package]] name = "rtnetlink" -version = "0.10.1" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322c53fd76a18698f1c27381d58091de3a043d356aa5bd0d510608b565f469a0" +checksum = "7a552eb82d19f38c3beed3f786bd23aa434ceb9ac43ab44419ca6d67a7e186c0" dependencies = [ "futures", "log", + "netlink-packet-core", "netlink-packet-route", + "netlink-packet-utils", "netlink-proto", - "nix 0.24.3", - "thiserror", + "netlink-sys", + "nix 0.26.4", + "thiserror 1.0.69", "tokio", ] @@ -6558,7 +6440,7 @@ dependencies = [ "proc-macro2", "quote", "rust-embed-utils", - "syn 2.0.72", + "syn 2.0.87", "walkdir", ] @@ -6599,9 +6481,9 @@ checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" [[package]] name = "rustc_version" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ "semver", ] @@ -6617,9 +6499,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.34" +version = "0.38.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" dependencies = [ "bitflags 2.6.0", "errno", @@ -6642,15 +6524,15 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.12" +version = "0.23.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" +checksum = "7f1a745511c54ba6d4465e8d5dfbd81b45791756de28d4981af70d6dca128f1e" dependencies = [ "log", "once_cell", "ring 0.17.8", "rustls-pki-types", - "rustls-webpki 0.102.6", + "rustls-webpki 0.102.8", "subtle", "zeroize", ] @@ -6666,19 +6548,21 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64 0.22.1", "rustls-pki-types", ] [[package]] name = "rustls-pki-types" -version = "1.8.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" +checksum = "16f1201b3c9a7ee8039bcadc17b7e605e2945b27eee7631788c1bd2b0643674b" +dependencies = [ + "web-time", +] [[package]] name = "rustls-webpki" @@ -6692,9 +6576,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.102.6" +version = "0.102.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e6b52d4fda176fd835fdc55a835d4a89b8499cad995885a21149d5ad62f852e" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" dependencies = [ "ring 0.17.8", "rustls-pki-types", @@ -6703,9 +6587,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" [[package]] name = "rw-stream-sink" @@ -6744,11 +6628,11 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.23" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -6761,7 +6645,6 @@ dependencies = [ "schemars_derive", "serde", "serde_json", - "url", ] [[package]] @@ -6773,7 +6656,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -6844,9 +6727,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "fa39c7303dc58b5543c94d22c1766b0d31f2ee58306363ea622b10bbc075eaa2" dependencies = [ "core-foundation-sys", "libc", @@ -6863,15 +6746,12 @@ name = "semver" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" -dependencies = [ - "serde", -] [[package]] name = "serde" -version = "1.0.204" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] @@ -6908,13 +6788,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.204" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -6925,14 +6805,14 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "serde_json" -version = "1.0.122" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", "memchr", @@ -6969,14 +6849,14 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "serde_spanned" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" dependencies = [ "serde", ] @@ -6990,7 +6870,7 @@ dependencies = [ "proc-macro2", "quote", "serde", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7007,9 +6887,9 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.9.0" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cecfa94848272156ea67b2b1a53f20fc7bc638c4a46d2f8abde08f05f4b857" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" dependencies = [ "base64 0.22.1", "chrono", @@ -7025,14 +6905,14 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.9.0" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8fee4991ef4f274617a51ad4af30519438dacb2f56ac773b08a1922ff743350" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" dependencies = [ - "darling 0.20.10", + "darling", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7109,7 +6989,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6c99835bad52957e7aa241d3975ed17c1e5f8c92026377d117a606f36b84b16" dependencies = [ "bytes", - "memmap2 0.6.2", + "memmap2", ] [[package]] @@ -7138,9 +7018,9 @@ dependencies = [ [[package]] name = "simdutf8" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "simple_asn1" @@ -7150,7 +7030,7 @@ checksum = "adc4e5204eb1910f40f9cfa375f6f05b68c3abac4b6fd879c8ff5e7ae8a0a085" dependencies = [ "num-bigint 0.4.6", "num-traits", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -7264,27 +7144,27 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stacker" -version = "0.1.15" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce" +checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b" dependencies = [ "cc", "cfg-if 1.0.0", "libc", "psm", - "winapi", + "windows-sys 0.59.0", ] [[package]] name = "starknet" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0c9ac3809cc7630784e8c8565fa3013af819d83c97aa2720d566016d439011" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "starknet-accounts", "starknet-contract", "starknet-core", - "starknet-crypto", + "starknet-core-derive", + "starknet-crypto 0.7.3 (git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6)", "starknet-macros", "starknet-providers", "starknet-signers", @@ -7293,23 +7173,21 @@ dependencies = [ [[package]] name = "starknet-accounts" version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee27ded58ade61da410fccafd57ed5429b0e79a9d62a4ae8b65818cb9d6f400" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "async-trait", "auto_impl", "starknet-core", - "starknet-crypto", + "starknet-crypto 0.7.3 (git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6)", "starknet-providers", "starknet-signers", - "thiserror", + "thiserror 1.0.69", ] [[package]] name = "starknet-contract" version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd6ee5762d24c4f06ab7e9406550925df406712e73719bd2de905c879c674a87" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "serde", "serde_json", @@ -7317,33 +7195,62 @@ dependencies = [ "starknet-accounts", "starknet-core", "starknet-providers", - "thiserror", + "thiserror 1.0.69", ] [[package]] name = "starknet-core" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2538240cbe6663c673fe77465f294da707080f39678dd7066761554899e46100" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "base64 0.21.7", "crypto-bigint", "flate2", "hex", + "num-traits", "serde", "serde_json", "serde_json_pythonic", "serde_with", "sha3", - "starknet-crypto", + "starknet-core-derive", + "starknet-crypto 0.7.3 (git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6)", "starknet-types-core", ] +[[package]] +name = "starknet-core-derive" +version = "0.1.0" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "starknet-crypto" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60a5064173a8e8d2675e67744fd07f310de44573924b6b7af225a6bdd8102913" +checksum = "ded22ccf4cb9e572ce3f77de6066af53560cd2520d508876c83bb1e6b29d5cbc" +dependencies = [ + "crypto-bigint", + "hex", + "hmac", + "num-bigint 0.4.6", + "num-integer", + "num-traits", + "rfc6979", + "sha2", + "starknet-curve 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "starknet-types-core", + "zeroize", +] + +[[package]] +name = "starknet-crypto" +version = "0.7.3" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "crypto-bigint", "hex", @@ -7353,7 +7260,7 @@ dependencies = [ "num-traits", "rfc6979", "sha2", - "starknet-curve", + "starknet-curve 0.5.1 (git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6)", "starknet-types-core", "zeroize", ] @@ -7367,21 +7274,27 @@ dependencies = [ "starknet-types-core", ] +[[package]] +name = "starknet-curve" +version = "0.5.1" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" +dependencies = [ + "starknet-types-core", +] + [[package]] name = "starknet-macros" version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8986a940af916fc0a034f4e42c6ba76d94f1e97216d75447693dfd7aefaf3ef2" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "starknet-core", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] name = "starknet-providers" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60e8e69ba7a36dea2d28333be82b4011f8784333d3ae5618482b6587c1ffb66c" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "async-trait", "auto_impl", @@ -7394,15 +7307,14 @@ dependencies = [ "serde_json", "serde_with", "starknet-core", - "thiserror", + "thiserror 1.0.69", "url", ] [[package]] name = "starknet-signers" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70b9e01b61ae51d722e2b100d6ef913c5a2e70d1ea672733d385f7296d6055ef" +source = "git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6#5c676a64031901b5a203168fd8ef8d6b40a5862f" dependencies = [ "async-trait", "auto_impl", @@ -7411,8 +7323,8 @@ dependencies = [ "getrandom", "rand 0.8.5", "starknet-core", - "starknet-crypto", - "thiserror", + "starknet-crypto 0.7.3 (git+https://github.com/xJonathanLEI/starknet-rs?rev=5c676a6)", + "thiserror 1.0.69", ] [[package]] @@ -7435,12 +7347,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - [[package]] name = "strsim" version = "0.11.1" @@ -7488,7 +7394,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7510,27 +7416,15 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.72" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] -[[package]] -name = "syn_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn 2.0.72", -] - [[package]] name = "sync_wrapper" version = "0.1.2" @@ -7542,6 +7436,9 @@ name = "sync_wrapper" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +dependencies = [ + "futures-core", +] [[package]] name = "synstructure" @@ -7563,7 +7460,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7574,7 +7471,18 @@ checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ "bitflags 1.3.2", "core-foundation", - "system-configuration-sys", + "system-configuration-sys 0.5.0", +] + +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.6.0", + "core-foundation", + "system-configuration-sys 0.6.0", ] [[package]] @@ -7587,6 +7495,16 @@ dependencies = [ "libc", ] +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tap" version = "1.0.1" @@ -7595,9 +7513,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tar" -version = "0.4.41" +version = "0.4.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb797dad5fb5b76fcf519e702f4a589483b5ef06567f160c392832c1f5e44909" +checksum = "c65998313f8e17d0d553d28f91a0df93e4dbbbf770279c7bc21ca0f09ea1a1f6" dependencies = [ "filetime", "libc", @@ -7610,6 +7528,12 @@ version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" +[[package]] +name = "target-triple" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42a4d50cdb458045afc8131fd91b64904da29548bcb63c7236e0844936c13078" + [[package]] name = "tempdir" version = "0.3.7" @@ -7622,9 +7546,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.12.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if 1.0.0", "fastrand", @@ -7654,22 +7578,42 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +dependencies = [ + "thiserror-impl 2.0.3", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ - "thiserror-impl", + "proc-macro2", + "quote", + "syn 2.0.87", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7737,6 +7681,16 @@ dependencies = [ "crunchy", ] +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinyvec" version = "1.8.0" @@ -7754,14 +7708,14 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.39.2" +version = "1.41.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" +checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" dependencies = [ "backtrace", "bytes", "libc", - "mio 1.0.1", + "mio 1.0.2", "parking_lot", "pin-project-lite", "signal-hook-registry", @@ -7778,7 +7732,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -7818,16 +7772,16 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.12", + "rustls 0.23.17", "rustls-pki-types", "tokio", ] [[package]] name = "tokio-stream" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" dependencies = [ "futures-core", "pin-project-lite", @@ -7847,18 +7801,6 @@ dependencies = [ "tokio-stream", ] -[[package]] -name = "tokio-tungstenite" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" -dependencies = [ - "futures-util", - "log", - "tokio", - "tungstenite 0.21.0", -] - [[package]] name = "tokio-tungstenite" version = "0.24.0" @@ -7868,14 +7810,14 @@ dependencies = [ "futures-util", "log", "tokio", - "tungstenite 0.24.0", + "tungstenite", ] [[package]] name = "tokio-util" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" dependencies = [ "bytes", "futures-core", @@ -7894,7 +7836,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.20", + "toml_edit", ] [[package]] @@ -7908,26 +7850,15 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.21.1" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" -dependencies = [ - "indexmap 2.6.0", - "toml_datetime", - "winnow 0.5.40", -] - -[[package]] -name = "toml_edit" -version = "0.22.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap 2.6.0", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.18", + "winnow", ] [[package]] @@ -7940,6 +7871,21 @@ dependencies = [ "futures-util", "pin-project", "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 0.1.2", "tokio", "tower-layer", "tower-service", @@ -7990,21 +7936,21 @@ dependencies = [ [[package]] name = "tower-layer" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" [[package]] name = "tower-service" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tower-sessions" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2d9b6f0c4938eed0eefd9cce19319b4bdad10e11ca9d8c3be373ce734bbfd63" +checksum = "50571505955aaa8b73f2f40489953d92b4d7ff9eb9b2a8b4e11fee0dcdb2760e" dependencies = [ "async-trait", "http 1.1.0", @@ -8020,9 +7966,9 @@ dependencies = [ [[package]] name = "tower-sessions-core" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38767064990c327ec1d92bba2576dce0944750e9c9ae021f12ebc72de77ac406" +checksum = "6293bf33f1977d5ef422c2e02f909eb2c3d7bf921d93557c40d4f1b130b84aa4" dependencies = [ "async-trait", "axum-core", @@ -8033,7 +7979,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", "time", "tokio", "tracing", @@ -8041,9 +7987,9 @@ dependencies = [ [[package]] name = "tower-sessions-memory-store" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8b09bbe2c138a9b0ebf307dc6e6a4f7723c59545e0f4fe5e329a89868164ae3" +checksum = "cec5f88eeef0f036e6900217034efbce733cbdf0528a85204eaaed90bc34c354" dependencies = [ "async-trait", "time", @@ -8071,7 +8017,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -8131,37 +8077,19 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "trybuild" -version = "1.0.99" +version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "207aa50d36c4be8d8c6ea829478be44a372c6a77669937bb39c698e52f1491e8" +checksum = "8dcd332a5496c026f1e14b7f3d2b7bd98e509660c04239c58b0ba38a12daded4" dependencies = [ "glob", "serde", "serde_derive", "serde_json", + "target-triple", "termcolor", "toml", ] -[[package]] -name = "tungstenite" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" -dependencies = [ - "byteorder", - "bytes", - "data-encoding", - "http 1.1.0", - "httparse", - "log", - "rand 0.8.5", - "sha1", - "thiserror", - "url", - "utf-8", -] - [[package]] name = "tungstenite" version = "0.24.0" @@ -8176,7 +8104,7 @@ dependencies = [ "log", "rand 0.8.5", "sha1", - "thiserror", + "thiserror 1.0.69", "utf-8", ] @@ -8206,45 +8134,42 @@ dependencies = [ [[package]] name = "unicase" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" -dependencies = [ - "version_check", -] +checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" [[package]] name = "unicode-bidi" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" +checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "unicode-normalization" -version = "0.1.22" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] [[package]] name = "unicode-width" -version = "0.1.13" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "unicode-xid" -version = "0.2.4" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] name = "universal-hash" @@ -8292,18 +8217,18 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "ureq" -version = "2.10.0" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72139d247e5f97a3eff96229a7ae85ead5328a39efe76f8bf5a06313d505b6ea" +checksum = "b74fc6b57825be3373f7054754755f03ac3a8f5d70015ccad699ba2029956f4a" dependencies = [ "base64 0.22.1", "flate2", "log", "once_cell", - "rustls 0.23.12", + "rustls 0.23.17", "rustls-pki-types", "url", - "webpki-roots 0.26.3", + "webpki-roots 0.26.6", ] [[package]] @@ -8318,12 +8243,12 @@ dependencies = [ [[package]] name = "url" -version = "2.5.2" +version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada" dependencies = [ "form_urlencoded", - "idna 0.5.0", + "idna 1.0.3", "percent-encoding", "serde", ] @@ -8334,6 +8259,18 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" @@ -8352,9 +8289,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" [[package]] name = "valuable" @@ -8445,34 +8382,35 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" dependencies = [ "cfg-if 1.0.0", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -8482,9 +8420,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -8492,22 +8430,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" [[package]] name = "wasm-encoder" @@ -8520,9 +8458,9 @@ dependencies = [ [[package]] name = "wasm-streams" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b65dc4c90b63b118468cf747d8bf3566c1913ef60be765b5730ead9e0a3ba129" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" dependencies = [ "futures-util", "js-sys", @@ -8533,9 +8471,9 @@ dependencies = [ [[package]] name = "wasmer" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3a6e0f73e5ae361fe64db607eaf4ab2381d88ad2c1b0bb8cf254cf35d894687" +checksum = "2d920d06243e9f456c336c428a34560357dedf59d9febaae14f1995ac120cff6" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -8548,7 +8486,7 @@ dependencies = [ "serde-wasm-bindgen", "shared-buffer", "target-lexicon", - "thiserror", + "thiserror 1.0.69", "tracing", "wasm-bindgen", "wasmer-compiler", @@ -8557,14 +8495,14 @@ dependencies = [ "wasmer-types", "wasmer-vm", "wat", - "winapi", + "windows-sys 0.59.0", ] [[package]] name = "wasmer-compiler" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb1e7c79507f5d55f1afd99984717e8380440cd98e13d542e4d00661f986f2d4" +checksum = "0e01832173aa52345e480965f18c638a8a5a9e5e4d85a48675bdf1964147dc7f" dependencies = [ "backtrace", "bytes", @@ -8574,26 +8512,26 @@ dependencies = [ "lazy_static", "leb128", "libc", - "memmap2 0.5.10", + "memmap2", "more-asserts", "region", "rkyv", "self_cell", "shared-buffer", "smallvec", - "thiserror", + "thiserror 1.0.69", "wasmer-types", "wasmer-vm", "wasmparser", - "winapi", + "windows-sys 0.59.0", "xxhash-rust", ] [[package]] name = "wasmer-compiler-cranelift" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3352014573750327646a690d32774312b0e8b7920e7e8ba00c0449eac18390" +checksum = "1c1618f53b492cf6649beeb372930e376e0f52d9842c0c5eb5aa2b548251dab6" dependencies = [ "cranelift-codegen", "cranelift-entity", @@ -8608,33 +8546,11 @@ dependencies = [ "wasmer-types", ] -[[package]] -name = "wasmer-config" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b4a632496950fde9ad821e195ef1a301440076f7c7d80de55239a140359bcbd" -dependencies = [ - "anyhow", - "bytesize", - "derive_builder", - "hex", - "indexmap 2.6.0", - "schemars", - "semver", - "serde", - "serde_cbor", - "serde_json", - "serde_yaml", - "thiserror", - "toml", - "url", -] - [[package]] name = "wasmer-derive" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac6b0b0580cfa1fc7ad58cca3626a742f2b2e5ccd51cfc5de43e8edb0d1daa4c" +checksum = "9c5875633aea92153b6a561cb07363785ca9e07792ca6cd7c1cc371761001d8f" dependencies = [ "proc-macro-error", "proc-macro2", @@ -8644,9 +8560,9 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576442cc3d302ca215fd40aa7826a078571dca7eaa773d8cdedca14a2ec7c9a1" +checksum = "8fb32f0d231b591e4c8a65e81d4647fa3180496d71a123d4948dba8551bba9c2" dependencies = [ "bytecheck", "enum-iterator", @@ -8658,16 +8574,15 @@ dependencies = [ "rkyv", "sha2", "target-lexicon", - "thiserror", - "webc", + "thiserror 1.0.69", "xxhash-rust", ] [[package]] name = "wasmer-vm" -version = "4.3.5" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6483035d1df84a978cd6c6a35878e913dc8ec6311f8712548a922a75e87957ba" +checksum = "e38e9301f5bb9f18da9cda4002d74d2cb6ac1f36dcf919fd77f91fca321fb1e5" dependencies = [ "backtrace", "cc", @@ -8686,9 +8601,9 @@ dependencies = [ "more-asserts", "region", "scopeguard", - "thiserror", + "thiserror 1.0.69", "wasmer-types", - "winapi", + "windows-sys 0.59.0", ] [[package]] @@ -8725,9 +8640,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.69" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" dependencies = [ "js-sys", "wasm-bindgen", @@ -8749,7 +8664,7 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5388522c899d1e1c96a4c307e3797e0f697ba7c77dd8e0e625ecba9dd0342937" dependencies = [ - "arrayvec 0.7.4", + "arrayvec 0.7.6", "base64 0.21.7", "bytes", "derive_more", @@ -8786,40 +8701,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f6d8d1636b2627fe63518d5a9b38a569405d9c9bc665c43c9c341de57227ebb" dependencies = [ "native-tls", - "thiserror", + "thiserror 1.0.69", "tokio", "url", ] -[[package]] -name = "webc" -version = "6.0.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb3e2ccb43d303c5bd48f31db7a129481a9aaa5343d623f92951751df190df81" -dependencies = [ - "anyhow", - "base64 0.22.1", - "bytes", - "cfg-if 1.0.0", - "document-features", - "flate2", - "indexmap 1.9.3", - "libc", - "once_cell", - "semver", - "serde", - "serde_cbor", - "serde_json", - "sha2", - "shared-buffer", - "tar", - "tempfile", - "thiserror", - "toml", - "url", - "wasmer-config", -] - [[package]] name = "webpki-roots" version = "0.25.4" @@ -8828,9 +8714,9 @@ checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] name = "webpki-roots" -version = "0.26.3" +version = "0.26.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd7c23921eeb1713a4e851530e9b9756e4fb0e89978582942612524cf09f01cd" +checksum = "841c67bff177718f1d4dfefde8d8f0e78f9b6589319ba88312f567fc5841a958" dependencies = [ "rustls-pki-types", ] @@ -8886,21 +8772,70 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows" -version = "0.51.1" +version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" +checksum = "efc5cf48f83140dcaab716eeaea345f9e93d0018fb81162753a3f76c3397b538" dependencies = [ - "windows-core", - "windows-targets 0.48.5", + "windows-core 0.53.0", + "windows-targets 0.52.6", ] [[package]] name = "windows-core" -version = "0.51.1" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcc5b895a6377f1ab9fa55acedab1fd5ac0db66ad1e6c7f47e28a22e446a5dd" +dependencies = [ + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-registry" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +dependencies = [ + "windows-result 0.2.0", + "windows-strings", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result 0.2.0", + "windows-targets 0.52.6", ] [[package]] @@ -9096,18 +9031,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.5.40" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" -dependencies = [ - "memchr", -] - -[[package]] -name = "winnow" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] @@ -9123,14 +9049,16 @@ dependencies = [ ] [[package]] -name = "winreg" -version = "0.52.0" +name = "write16" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" -dependencies = [ - "cfg-if 1.0.0", - "windows-sys 0.48.0", -] +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" [[package]] name = "wyz" @@ -9166,7 +9094,7 @@ dependencies = [ "nom", "oid-registry 0.6.1", "rusticata-macros", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -9181,9 +9109,9 @@ dependencies = [ "der-parser 9.0.0", "lazy_static", "nom", - "oid-registry 0.7.0", + "oid-registry 0.7.1", "rusticata-macros", - "thiserror", + "thiserror 1.0.69", "time", ] @@ -9200,9 +9128,9 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.8.21" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "539a77ee7c0de333dcc6da69b177380a0b81e0dacfa4f7344c465a36871ee601" +checksum = "af310deaae937e48a26602b730250b4949e125f468f11e6990be3e5304ddd96f" [[package]] name = "xmltree" @@ -9236,9 +9164,9 @@ dependencies = [ [[package]] name = "yamux" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31b5e376a8b012bee9c423acdbb835fc34d45001cfa3106236a624e4b738028" +checksum = "17610762a1207ee816c6fadc29220904753648aba0a9ed61c7b8336e80a559c4" dependencies = [ "futures", "log", @@ -9265,6 +9193,30 @@ dependencies = [ "time", ] +[[package]] +name = "yoke" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", + "synstructure 0.13.1", +] + [[package]] name = "zerocopy" version = "0.7.35" @@ -9283,7 +9235,28 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", +] + +[[package]] +name = "zerofrom" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", + "synstructure 0.13.1", ] [[package]] @@ -9303,7 +9276,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.72", + "syn 2.0.87", ] [[package]] @@ -9319,6 +9292,28 @@ dependencies = [ "rustc-hex", ] +[[package]] +name = "zerovec" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "zip" version = "0.6.6" diff --git a/Cargo.toml b/Cargo.toml index b99c36678..cfc9a7d98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -304,3 +304,6 @@ multiple_crate_versions = "allow" # Cannot resolve all these missing_errors_doc = "allow" # TODO: Remove later once documentation has been added missing_panics_doc = "allow" # TODO: Remove later once documentation has been added future_not_send = "allow" # TODO: Remove later once Send is implemented + +[patch.crates-io] +starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "5c676a6" } diff --git a/contracts/context-config/src/mutate.rs b/contracts/context-config/src/mutate.rs index cdc82fe75..b1e9bdc60 100644 --- a/contracts/context-config/src/mutate.rs +++ b/contracts/context-config/src/mutate.rs @@ -251,6 +251,12 @@ impl ContextConfigs { .expect("unable to update member list") .priviledges() .grant(identity), + Capability::Proxy => context + .proxy + .get(signer_id) + .expect("unable to update proxy") + .priviledges() + .grant(identity), }; env::log_str(&format!( @@ -289,6 +295,12 @@ impl ContextConfigs { .expect("unable to update member list") .priviledges() .revoke(&identity), + Capability::Proxy => context + .proxy + .get(signer_id) + .expect("unable to update proxy") + .priviledges() + .revoke(&identity), }; env::log_str(&format!( diff --git a/crates/context/config/Cargo.toml b/crates/context/config/Cargo.toml index c12185579..0ebcea8bb 100644 --- a/crates/context/config/Cargo.toml +++ b/crates/context/config/Cargo.toml @@ -12,6 +12,7 @@ borsh = { workspace = true, features = ["derive"] } ed25519-dalek.workspace = true either = { workspace = true, optional = true } eyre = { workspace = true, optional = true } +hex.workspace = true near-crypto = { workspace = true, optional = true } near-jsonrpc-client = { workspace = true, optional = true } near-jsonrpc-primitives = { workspace = true, optional = true } diff --git a/crates/context/config/src/client/env/config.rs b/crates/context/config/src/client/env/config.rs index 04c95f9e6..ae704f599 100644 --- a/crates/context/config/src/client/env/config.rs +++ b/crates/context/config/src/client/env/config.rs @@ -2,7 +2,7 @@ use crate::client::{CallClient, Environment}; mod mutate; mod query; - +mod types; use mutate::ContextConfigMutate; use query::ContextConfigQuery; diff --git a/crates/context/config/src/client/env/config/mutate.rs b/crates/context/config/src/client/env/config/mutate.rs index 8084261b0..d5c851bf3 100644 --- a/crates/context/config/src/client/env/config/mutate.rs +++ b/crates/context/config/src/client/env/config/mutate.rs @@ -1,14 +1,19 @@ +use std::fmt::Debug; + use ed25519_dalek::{Signer, SigningKey}; +use starknet::core::codec::Encode; +use starknet::signers::SigningKey as StarknetSigningKey; +use starknet_crypto::{poseidon_hash_many, Felt}; +use super::types::starknet::{Request as StarknetRequest, Signed as StarknetSigned}; use crate::client::env::{utils, Method}; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; use crate::client::transport::Transport; use crate::client::{CallClient, ClientError, Operation}; -use crate::repr::ReprTransmute; +use crate::repr::{Repr, ReprTransmute}; use crate::types::Signed; -use crate::{Request, RequestKind}; - +use crate::{ContextIdentity, Request, RequestKind}; pub mod methods; #[derive(Debug)] @@ -25,6 +30,7 @@ pub struct ContextConfigMutateRequest<'a, T> { #[derive(Debug)] struct Mutate<'a> { pub(crate) signing_key: [u8; 32], + pub(crate) nonce: u64, pub(crate) kind: RequestKind<'a>, } @@ -56,31 +62,77 @@ impl<'a> Method for Mutate<'a> { impl<'a> Method for Mutate<'a> { type Returns = (); - const METHOD: &'static str = "mutate"; fn encode(self) -> eyre::Result> { - // sign the params, encode it and return - // since you will have a `Vec` here, you can - // `Vec::with_capacity(32 * calldata.len())` and then - // extend the `Vec` with each `Felt::to_bytes_le()` - // when this `Vec` makes it to `StarknetTransport`, - // reconstruct the `Vec` from it - todo!() + // Derive ecdsa key from private key + let secret_scalar = Felt::from_bytes_be(&self.signing_key); + let signing_key = StarknetSigningKey::from_secret_scalar(secret_scalar); + let verifying_key = signing_key.verifying_key().scalar(); + let verifying_key_bytes = verifying_key.to_bytes_be(); + + // Derive ed25519 key from private key + let user_key = SigningKey::from_bytes(&self.signing_key).verifying_key(); + let user_key_bytes = user_key.to_bytes(); + + // Create Repr wrapped ContextIdentity instances + let signer_id = verifying_key_bytes + .rt::() + .map_err(|e| eyre::eyre!("Failed to convert verifying key: {}", e))?; + let signer_id = Repr::new(signer_id); + + let user_id = user_key_bytes + .rt::() + .map_err(|e| eyre::eyre!("Failed to convert user key: {}", e))?; + let user_id = Repr::new(user_id); + + // Create the Request structure using into() conversions + let request = StarknetRequest { + signer_id: signer_id.into(), + user_id: user_id.into(), + nonce: self.nonce, + kind: self.kind.into(), + }; + + // Serialize the request + let mut serialized_request = vec![]; + request.encode(&mut serialized_request)?; + + // Hash the serialized request + let hash = poseidon_hash_many(&serialized_request); + + // Sign the hash with the signing key + let signature = signing_key.sign(&hash)?; + + let signed_request = StarknetSigned { + payload: serialized_request, + signature_r: signature.r, + signature_s: signature.s, + }; + + let mut signed_request_serialized = vec![]; + signed_request.encode(&mut signed_request_serialized)?; + + let bytes: Vec = signed_request_serialized + .iter() + .flat_map(|felt| felt.to_bytes_be()) + .collect(); + + Ok(bytes) } fn decode(_response: Vec) -> eyre::Result { - todo!() + Ok(()) } } -impl<'a, T: Transport> ContextConfigMutateRequest<'a, T> { +impl<'a, T: Transport + Debug> ContextConfigMutateRequest<'a, T> { pub async fn send(self, signing_key: [u8; 32]) -> Result<(), ClientError> { let request = Mutate { signing_key, // todo! when nonces are implemented in context // todo! config contract, we fetch it here first - // nonce: _, + nonce: 0, kind: self.kind, }; diff --git a/crates/context/config/src/client/env/config/query/application.rs b/crates/context/config/src/client/env/config/query/application.rs index 51873f281..c91c51ee6 100644 --- a/crates/context/config/src/client/env/config/query/application.rs +++ b/crates/context/config/src/client/env/config/query/application.rs @@ -1,5 +1,10 @@ use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet_crypto::Felt; +use crate::client::env::config::types::starknet::{ + Application as StarknetApplication, CallData, FeltPair, +}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -41,10 +46,48 @@ impl Method for ApplicationRequest { const METHOD: &'static str = "application"; fn encode(self) -> eyre::Result> { - todo!() + let felt_pair: FeltPair = self.context_id.into(); + let mut call_data = CallData::default(); + felt_pair.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Err(eyre::eyre!("No application found")); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + if felts.is_empty() { + return Err(eyre::eyre!("No felts decoded from response")); + } + + // Skip version felt and decode the application + let application = StarknetApplication::decode(&felts[1..]) + .map_err(|e| eyre::eyre!("Failed to decode application: {:?}", e))?; + + Ok(application.into()) } } diff --git a/crates/context/config/src/client/env/config/query/application_revision.rs b/crates/context/config/src/client/env/config/query/application_revision.rs index 2312e0824..70f39162b 100644 --- a/crates/context/config/src/client/env/config/query/application_revision.rs +++ b/crates/context/config/src/client/env/config/query/application_revision.rs @@ -1,5 +1,7 @@ use serde::Serialize; +use starknet::core::codec::Encode; +use crate::client::env::config::types::starknet::{CallData, FeltPair}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -7,8 +9,8 @@ use crate::repr::Repr; use crate::types::{ContextId, Revision}; #[derive(Copy, Clone, Debug, Serialize)] -pub(super) struct ApplicationRevisionRequest { - pub(super) context_id: Repr, +pub struct ApplicationRevisionRequest { + pub context_id: Repr, } impl Method for ApplicationRevisionRequest { @@ -31,10 +33,24 @@ impl Method for ApplicationRevisionRequest { const METHOD: &'static str = "application_revision"; fn encode(self) -> eyre::Result> { - todo!() + let felt_pair: FeltPair = self.context_id.into(); + let mut call_data = CallData::default(); + felt_pair.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.len() != 32 { + return Err(eyre::eyre!( + "Invalid response length: expected 32 bytes, got {}", + response.len() + )); + } + + // Response should be a single u64 in the last 8 bytes of a felt + let revision_bytes = &response[24..32]; // Take last 8 bytes + let revision = u64::from_be_bytes(revision_bytes.try_into()?); + + Ok(revision) } } diff --git a/crates/context/config/src/client/env/config/query/has_member.rs b/crates/context/config/src/client/env/config/query/has_member.rs index 2e9c91bad..beea351e6 100644 --- a/crates/context/config/src/client/env/config/query/has_member.rs +++ b/crates/context/config/src/client/env/config/query/has_member.rs @@ -1,5 +1,7 @@ use serde::Serialize; +use starknet::core::codec::Encode; +use crate::client::env::config::types::starknet::{CallData, FeltPair}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -32,10 +34,39 @@ impl Method for HasMemberRequest { const METHOD: &'static str = "has_member"; fn encode(self) -> eyre::Result> { - todo!() + let mut call_data = CallData::default(); + + // Encode context_id + let context_pair: FeltPair = self.context_id.into(); + context_pair.encode(&mut call_data)?; + + // Encode identity + let identity_pair: FeltPair = self.identity.into(); + identity_pair.encode(&mut call_data)?; + + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.len() != 32 { + return Err(eyre::eyre!( + "Invalid response length: expected 32 bytes, got {}", + response.len() + )); + } + + // Check if all bytes except the last one are zero + if !response[..31].iter().all(|&b| b == 0) { + return Err(eyre::eyre!( + "Invalid response format: non-zero bytes in prefix" + )); + } + + // Check the last byte is either 0 or 1 + match response[31] { + 0 => Ok(false), + 1 => Ok(true), + v => Err(eyre::eyre!("Invalid boolean value: {}", v)), + } } } diff --git a/crates/context/config/src/client/env/config/query/members.rs b/crates/context/config/src/client/env/config/query/members.rs index 6d18930f2..e688bece3 100644 --- a/crates/context/config/src/client/env/config/query/members.rs +++ b/crates/context/config/src/client/env/config/query/members.rs @@ -1,7 +1,12 @@ use core::mem; use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet_crypto::Felt; +use crate::client::env::config::types::starknet::{ + CallData, StarknetMembers, StarknetMembersRequest, +}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -9,10 +14,10 @@ use crate::repr::Repr; use crate::types::{ContextId, ContextIdentity}; #[derive(Copy, Clone, Debug, Serialize)] -pub(super) struct MembersRequest { - pub(super) context_id: Repr, - pub(super) offset: usize, - pub(super) length: usize, +pub(crate) struct MembersRequest { + pub(crate) context_id: Repr, + pub(crate) offset: usize, + pub(crate) length: usize, } impl Method for MembersRequest { @@ -45,10 +50,49 @@ impl Method for MembersRequest { const METHOD: &'static str = "members"; fn encode(self) -> eyre::Result> { - todo!() + let req: StarknetMembersRequest = self.into(); + let mut call_data = CallData::default(); + req.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(Vec::new()); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + // Check if it's a None response (single zero Felt) + if felts.len() == 1 && felts[0] == Felt::ZERO { + return Ok(Vec::new()); + } + + // Decode directly from the felts slice - the Decode trait will handle the array structure + let members = StarknetMembers::decode(&felts) + .map_err(|e| eyre::eyre!("Failed to decode members: {:?}", e))?; + + Ok(members.into()) } } diff --git a/crates/context/config/src/client/env/config/query/members_revision.rs b/crates/context/config/src/client/env/config/query/members_revision.rs index df4191ad8..dd2738fef 100644 --- a/crates/context/config/src/client/env/config/query/members_revision.rs +++ b/crates/context/config/src/client/env/config/query/members_revision.rs @@ -1,5 +1,7 @@ use serde::Serialize; +use starknet::core::codec::Encode; +use crate::client::env::config::types::starknet::{CallData, ContextId as StarknetContextId}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -31,10 +33,33 @@ impl Method for MembersRevisionRequest { const METHOD: &'static str = "members_revision"; fn encode(self) -> eyre::Result> { - todo!() + // Dereference Repr and encode context_id + let context_id: StarknetContextId = (*self.context_id).into(); + + let mut call_data = CallData::default(); + context_id.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.len() != 32 { + return Err(eyre::eyre!( + "Invalid response length: expected 32 bytes, got {}", + response.len() + )); + } + + // Response should be a single u64 in the last 8 bytes of a felt + // First 24 bytes should be zero + if !response[..24].iter().all(|&b| b == 0) { + return Err(eyre::eyre!( + "Invalid response format: non-zero bytes in prefix" + )); + } + + let revision_bytes = &response[24..32]; + let revision = u64::from_be_bytes(revision_bytes.try_into()?); + + Ok(revision) } } diff --git a/crates/context/config/src/client/env/config/query/privileges.rs b/crates/context/config/src/client/env/config/query/privileges.rs index 61e7b923b..496a97319 100644 --- a/crates/context/config/src/client/env/config/query/privileges.rs +++ b/crates/context/config/src/client/env/config/query/privileges.rs @@ -2,7 +2,13 @@ use core::{mem, ptr}; use std::collections::BTreeMap; use serde::Serialize; +use starknet::core::codec::{Decode, Encode, FeltWriter}; +use starknet_crypto::Felt; +use crate::client::env::config::types::starknet::{ + CallData, ContextId as StarknetContextId, ContextIdentity as StarknetContextIdentity, + StarknetPrivileges, +}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -63,10 +69,60 @@ impl<'a> Method for PrivilegesRequest<'a> { const METHOD: &'static str = "privileges"; fn encode(self) -> eyre::Result> { - todo!() + let mut call_data = CallData::default(); + + // Dereference Repr and encode context_id + let context_id: StarknetContextId = (*self.context_id).into(); + context_id.encode(&mut call_data)?; + + // Add array length + call_data.write(Felt::from(self.identities.len() as u64)); + + // Add each identity using StarknetIdentity + for identity in self.identities { + let starknet_id: StarknetContextIdentity = (*identity).into(); + starknet_id.encode(&mut call_data)?; + } + + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(BTreeMap::new()); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + // Check if it's a None response (single zero Felt) + if felts.len() == 1 && felts[0] == Felt::ZERO { + return Ok(BTreeMap::new()); + } + + let privileges = StarknetPrivileges::decode(&felts) + .map_err(|e| eyre::eyre!("Failed to decode privileges: {:?}", e))?; + + Ok(privileges.into()) } } diff --git a/crates/context/config/src/client/env/config/query/proxy_contract.rs b/crates/context/config/src/client/env/config/query/proxy_contract.rs index 2868767df..06fc9ed12 100644 --- a/crates/context/config/src/client/env/config/query/proxy_contract.rs +++ b/crates/context/config/src/client/env/config/query/proxy_contract.rs @@ -1,5 +1,8 @@ use serde::Serialize; +use starknet::core::codec::Encode; +use starknet_crypto::Felt; +use crate::client::env::config::types::starknet::{CallData, FeltPair}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -31,10 +34,26 @@ impl Method for ProxyContractRequest { type Returns = String; fn encode(self) -> eyre::Result> { - todo!() + let mut call_data = CallData::default(); + let felt_pair: FeltPair = self.context_id.into(); + felt_pair.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Err(eyre::eyre!("No proxy contract found")); + } + + // Check if it's a None response (single zero Felt) + if response.iter().all(|&x| x == 0) { + return Err(eyre::eyre!("No proxy contract found")); + } + + // Parse bytes as Felt + let felt = Felt::from_bytes_be_slice(&response); + + // Format felt as hex string with 0x prefix + Ok(format!("0x{:x}", felt)) } } diff --git a/crates/context/config/src/client/env/config/types.rs b/crates/context/config/src/client/env/config/types.rs new file mode 100644 index 000000000..a8dc8cd36 --- /dev/null +++ b/crates/context/config/src/client/env/config/types.rs @@ -0,0 +1 @@ +pub mod starknet; diff --git a/crates/context/config/src/client/env/config/types/starknet.rs b/crates/context/config/src/client/env/config/types/starknet.rs new file mode 100644 index 000000000..23224f989 --- /dev/null +++ b/crates/context/config/src/client/env/config/types/starknet.rs @@ -0,0 +1,598 @@ +use std::collections::BTreeMap; + +use hex; +use starknet::core::codec::{Decode, Encode, Error, FeltWriter}; +use starknet::core::types::Felt; + +use crate::repr::{Repr, ReprBytes, ReprTransmute}; +use crate::types::SignerId; + +// Base type for all Starknet Felt pairs +#[derive(Debug, Clone, Copy, Encode, Decode)] +pub struct FeltPair { + pub high: Felt, + pub low: Felt, +} + +// Newtype pattern following types.rs style +#[derive(Debug, Clone, Copy, Encode, Decode)] +pub struct ContextId(FeltPair); + +#[derive(Debug, Clone, Copy, Encode, Decode)] +pub struct ContextIdentity(FeltPair); + +#[derive(Debug, Clone, Copy, Encode, Decode)] +pub struct ApplicationId(FeltPair); + +#[derive(Debug, Clone, Copy, Encode, Decode)] +pub struct ApplicationBlob(FeltPair); + +// Single conversion trait +pub trait IntoFeltPair { + fn into_felt_pair(self) -> (Felt, Felt); +} + +impl From<(Felt, Felt)> for FeltPair { + fn from(value: (Felt, Felt)) -> Self { + FeltPair { + high: value.0, + low: value.1, + } + } +} + +#[derive(Default, Debug)] +pub struct CallData(pub Vec); + +impl FeltWriter for CallData { + fn write(&mut self, felt: Felt) { + self.0.extend(felt.to_bytes_be()) + } +} + +// Implement for our base types +impl IntoFeltPair for crate::types::ContextId { + fn into_felt_pair(self) -> (Felt, Felt) { + let bytes = self.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + ( + Felt::from_bytes_be_slice(high_bytes), + Felt::from_bytes_be_slice(low_bytes), + ) + } +} + +impl IntoFeltPair for crate::types::ContextIdentity { + fn into_felt_pair(self) -> (Felt, Felt) { + let bytes = self.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + ( + Felt::from_bytes_be_slice(high_bytes), + Felt::from_bytes_be_slice(low_bytes), + ) + } +} + +impl IntoFeltPair for crate::types::ApplicationId { + fn into_felt_pair(self) -> (Felt, Felt) { + let bytes = self.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + ( + Felt::from_bytes_be_slice(high_bytes), + Felt::from_bytes_be_slice(low_bytes), + ) + } +} + +impl IntoFeltPair for crate::types::BlobId { + fn into_felt_pair(self) -> (Felt, Felt) { + let bytes = self.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + ( + Felt::from_bytes_be_slice(high_bytes), + Felt::from_bytes_be_slice(low_bytes), + ) + } +} + +// Add IntoFeltPair implementation for SignerId +impl IntoFeltPair for SignerId { + fn into_felt_pair(self) -> (Felt, Felt) { + let bytes = self.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + ( + Felt::from_bytes_be_slice(high_bytes), + Felt::from_bytes_be_slice(low_bytes), + ) + } +} + +// Add From implementations for FeltPair +impl From for FeltPair { + fn from(value: crate::types::ContextId) -> Self { + value.into_felt_pair().into() + } +} + +impl From for FeltPair { + fn from(value: crate::types::ContextIdentity) -> Self { + value.into_felt_pair().into() + } +} + +impl From for FeltPair { + fn from(value: crate::types::ApplicationId) -> Self { + value.into_felt_pair().into() + } +} + +impl From for FeltPair { + fn from(value: crate::types::BlobId) -> Self { + value.into_felt_pair().into() + } +} + +// Simplify the existing From implementations +impl From for ContextId { + fn from(value: crate::types::ContextId) -> Self { + Self(value.into()) + } +} + +impl From for ContextIdentity { + fn from(value: crate::types::ContextIdentity) -> Self { + Self(value.into()) + } +} + +impl From for ApplicationId { + fn from(value: crate::types::ApplicationId) -> Self { + Self(value.into()) + } +} + +impl From for ApplicationBlob { + fn from(value: crate::types::BlobId) -> Self { + Self(value.into()) + } +} + +// Add From for ContextIdentity +impl From for ContextIdentity { + fn from(value: SignerId) -> Self { + let (high, low) = value.into_felt_pair(); + Self(FeltPair { high, low }) + } +} + +// Add From> for ContextIdentity +impl From> for ContextIdentity { + fn from(value: Repr) -> Self { + let (high, low) = value.into_inner().into_felt_pair(); + Self(FeltPair { high, low }) + } +} + +#[derive(Debug, Encode)] +pub enum RequestKind { + Context(ContextRequest), +} + +impl From> for RequestKind { + fn from(value: crate::RequestKind<'_>) -> Self { + match value { + crate::RequestKind::Context(ctx_req) => RequestKind::Context(ctx_req.into()), + } + } +} + +#[derive(Debug, Encode)] +pub struct ContextRequest { + pub context_id: ContextId, + pub kind: ContextRequestKind, +} + +impl From> for ContextRequest { + fn from(value: crate::ContextRequest<'_>) -> Self { + ContextRequest { + context_id: (*value.context_id).into(), + kind: value.kind.into(), + } + } +} + +#[derive(Debug, Encode)] +pub struct Request { + pub kind: RequestKind, + pub signer_id: ContextIdentity, + pub user_id: ContextIdentity, + pub nonce: u64, +} + +#[derive(Debug, Encode, Decode, Copy, Clone)] +pub enum Capability { + ManageApplication, + ManageMembers, + ProxyCode, +} + +impl From<&crate::Capability> for Capability { + fn from(value: &crate::Capability) -> Self { + match value { + crate::Capability::ManageApplication => Capability::ManageApplication, + crate::Capability::ManageMembers => Capability::ManageMembers, + crate::Capability::Proxy => Capability::ProxyCode, + } + } +} + +#[derive(Debug, Encode)] +pub struct CapabilityAssignment { + pub member: ContextIdentity, + pub capability: Capability, +} + +#[derive(Debug, Encode)] +pub enum ContextRequestKind { + Add(ContextIdentity, Application), + UpdateApplication(Application), + AddMembers(Vec), + RemoveMembers(Vec), + Grant(Vec), + Revoke(Vec), + UpdateProxyContract, +} + +impl From> for ContextRequestKind { + fn from(value: crate::ContextRequestKind<'_>) -> Self { + match value { + crate::ContextRequestKind::Add { + author_id, + application, + } => ContextRequestKind::Add(author_id.into_inner().into(), application.into()), + crate::ContextRequestKind::UpdateApplication { application } => { + ContextRequestKind::UpdateApplication(application.into()) + } + crate::ContextRequestKind::AddMembers { members } => ContextRequestKind::AddMembers( + members.into_iter().map(|m| m.into_inner().into()).collect(), + ), + crate::ContextRequestKind::RemoveMembers { members } => { + ContextRequestKind::RemoveMembers( + members.into_iter().map(|m| m.into_inner().into()).collect(), + ) + } + crate::ContextRequestKind::Grant { capabilities } => ContextRequestKind::Grant( + capabilities + .into_iter() + .map(|(id, cap)| CapabilityAssignment { + member: id.into_inner().into(), + capability: cap.into(), + }) + .collect(), + ), + crate::ContextRequestKind::Revoke { capabilities } => ContextRequestKind::Revoke( + capabilities + .into_iter() + .map(|(id, cap)| CapabilityAssignment { + member: id.into_inner().into(), + capability: cap.into(), + }) + .collect(), + ), + crate::ContextRequestKind::UpdateProxyContract => { + ContextRequestKind::UpdateProxyContract + } + } + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct Application { + pub id: ApplicationId, + pub blob: ApplicationBlob, + pub size: u64, + pub source: EncodableString, + pub metadata: EncodableString, +} + +impl From> for Application { + fn from(value: crate::Application<'_>) -> Self { + Application { + id: (*value.id).into(), + blob: (*value.blob).into(), + size: value.size, + source: value.source.into(), + metadata: value.metadata.into(), + } + } +} + +impl<'a> From for crate::Application<'a> { + fn from(value: Application) -> Self { + crate::Application { + id: Repr::new(value.id.into()), + blob: Repr::new(value.blob.into()), + size: value.size, + source: value.source.into(), + metadata: value.metadata.into(), + } + } +} + +#[derive(Debug, Clone)] +pub struct EncodableString(pub String); + +impl From> for EncodableString { + fn from(value: crate::types::ApplicationSource<'_>) -> Self { + EncodableString(value.0.into_owned()) + } +} + +impl From> for EncodableString { + fn from(value: crate::types::ApplicationMetadata<'_>) -> Self { + EncodableString(String::from_utf8_lossy(&value.0.into_inner()).into_owned()) + } +} + +impl From<&str> for EncodableString { + fn from(value: &str) -> Self { + EncodableString(value.to_owned()) + } +} + +impl Encode for EncodableString { + fn encode(&self, writer: &mut W) -> Result<(), Error> { + const WORD_SIZE: usize = 31; + let bytes = self.0.as_bytes(); + + // Calculate full words and pending word + #[allow(clippy::integer_division, reason = "Not harmful here")] + let full_words_count = bytes.len() / WORD_SIZE; + let pending_len = bytes.len() % WORD_SIZE; + + // Write number of full words + writer.write(Felt::from(full_words_count)); + + // Write full words (31 chars each) + for i in 0..full_words_count { + let start = i * WORD_SIZE; + let word_bytes = &bytes[start..start + WORD_SIZE]; + let word_hex = hex::encode(word_bytes); + let felt = Felt::from_hex(&format!("0x{}", word_hex)) + .map_err(|e| Error::custom(&format!("Invalid word hex: {}", e)))?; + writer.write(felt); + } + + // Write pending word if exists + if pending_len > 0 { + let pending_bytes = &bytes[full_words_count * WORD_SIZE..]; + let pending_hex = hex::encode(pending_bytes); + let felt = Felt::from_hex(&format!("0x{}", pending_hex)) + .map_err(|e| Error::custom(&format!("Invalid pending hex: {}", e)))?; + writer.write(felt); + } else { + writer.write(Felt::ZERO); + } + + // Write pending word length + writer.write(Felt::from(pending_len)); + + Ok(()) + } +} + +impl<'a> Decode<'a> for EncodableString { + fn decode_iter(iter: &mut T) -> Result + where + T: Iterator, + { + const WORD_SIZE: usize = 31; + + // Get number of full words + let first_felt = iter.next().ok_or_else(Error::input_exhausted)?; + + let full_words_count = first_felt.to_bytes_be()[31] as usize; + + let mut bytes = Vec::new(); + + // Read full words + for _ in 0..full_words_count { + let word = iter.next().ok_or_else(Error::input_exhausted)?; + let word_bytes = word.to_bytes_be(); + bytes.extend_from_slice(&word_bytes[1..WORD_SIZE + 1]); + } + + // Read pending word + let pending_word = iter.next().ok_or_else(Error::input_exhausted)?; + let pending_bytes = pending_word.to_bytes_be(); + + // Read pending length + let pending_len = iter + .next() + .ok_or_else(Error::input_exhausted)? + .to_bytes_be()[31] as usize; + + // Handle pending bytes - find first non-zero byte and take all remaining bytes + if pending_len > 0 { + let start = pending_bytes.iter().position(|&x| x != 0).unwrap_or(1); + bytes.extend_from_slice(&pending_bytes[start..]); + } + + let string = String::from_utf8(bytes).map_err(|_| Error::custom("Invalid UTF-8"))?; + + Ok(EncodableString(string)) + } +} + +#[derive(Debug, Encode)] +pub struct StarknetMembersRequest { + pub context_id: ContextId, + pub offset: u32, + pub length: u32, +} + +impl From for StarknetMembersRequest { + fn from(value: crate::client::env::config::query::members::MembersRequest) -> Self { + StarknetMembersRequest { + context_id: (*value.context_id).into(), + offset: value.offset as u32, + length: value.length as u32, + } + } +} + +#[derive(Debug, Encode)] +pub struct StarknetApplicationRevisionRequest { + pub context_id: ContextId, +} + +impl From + for StarknetApplicationRevisionRequest +{ + fn from( + value: crate::client::env::config::query::application_revision::ApplicationRevisionRequest, + ) -> Self { + StarknetApplicationRevisionRequest { + context_id: (*value.context_id).into(), + } + } +} + +#[derive(Debug, Encode)] +pub struct Signed { + pub payload: Vec, + pub signature_r: Felt, + pub signature_s: Felt, +} + +// Add reverse conversions for IDs +impl From for crate::types::ApplicationId { + fn from(value: ApplicationId) -> Self { + let FeltPair { high, low } = value.0; + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +impl From for crate::types::ContextId { + fn from(value: ContextId) -> Self { + let FeltPair { high, low } = value.0; + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +impl From for crate::types::BlobId { + fn from(value: ApplicationBlob) -> Self { + let FeltPair { high, low } = value.0; + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +impl<'a> From for crate::types::ApplicationSource<'a> { + fn from(value: EncodableString) -> Self { + crate::types::ApplicationSource(std::borrow::Cow::Owned(value.0)) + } +} + +impl<'a> From for crate::types::ApplicationMetadata<'a> { + fn from(value: EncodableString) -> Self { + crate::types::ApplicationMetadata(Repr::new(std::borrow::Cow::Owned(value.0.into_bytes()))) + } +} + +#[derive(Debug, Decode)] +pub struct StarknetPrivilegeEntry { + pub identity: ContextIdentity, + pub capabilities: Vec, +} + +#[derive(Debug, Decode)] +pub struct StarknetPrivileges { + pub privileges: Vec, +} + +impl From for BTreeMap> { + fn from(value: StarknetPrivileges) -> Self { + value + .privileges + .into_iter() + .map(|entry| { + ( + entry.identity.into(), + entry.capabilities.into_iter().map(Into::into).collect(), + ) + }) + .collect() + } +} + +// Add conversion from Starknet Capability to domain Capability +impl From for crate::Capability { + fn from(value: Capability) -> Self { + match value { + Capability::ManageApplication => crate::Capability::ManageApplication, + Capability::ManageMembers => crate::Capability::ManageMembers, + Capability::ProxyCode => crate::Capability::Proxy, + } + } +} + +// Add conversion from Starknet ContextIdentity to SignerId +impl From for SignerId { + fn from(value: ContextIdentity) -> Self { + let FeltPair { high, low } = value.0; + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +#[derive(Debug, Decode)] +pub struct StarknetMembers { + pub members: Vec, +} + +impl From for Vec { + fn from(value: StarknetMembers) -> Self { + value.members.into_iter().map(|id| id.into()).collect() + } +} + +// Add conversion from Starknet ContextIdentity to domain ContextIdentity +impl From for crate::types::ContextIdentity { + fn from(value: ContextIdentity) -> Self { + let FeltPair { high, low } = value.0; + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +// Add From implementation for Repr +impl From> for FeltPair { + fn from(value: Repr) -> Self { + value.into_inner().into() + } +} + +// Add From implementation for Repr +impl From> for FeltPair { + fn from(value: Repr) -> Self { + value.into_inner().into() + } +} diff --git a/crates/context/config/src/client/env/proxy.rs b/crates/context/config/src/client/env/proxy.rs index 6845903a0..74226627e 100644 --- a/crates/context/config/src/client/env/proxy.rs +++ b/crates/context/config/src/client/env/proxy.rs @@ -2,9 +2,10 @@ use crate::client::{CallClient, Environment}; mod mutate; mod query; - +mod types; use mutate::ContextProxyMutate; use query::ContextProxyQuery; +pub use types::*; #[derive(Copy, Clone, Debug)] pub enum ContextProxy {} diff --git a/crates/context/config/src/client/env/proxy/mutate.rs b/crates/context/config/src/client/env/proxy/mutate.rs index b07059020..14afae533 100644 --- a/crates/context/config/src/client/env/proxy/mutate.rs +++ b/crates/context/config/src/client/env/proxy/mutate.rs @@ -1,12 +1,17 @@ use ed25519_dalek::{Signer, SigningKey}; +use starknet::core::codec::Encode; +use starknet::signers::SigningKey as StarknetSigningKey; +use starknet_crypto::{poseidon_hash_many, Felt}; +use super::types::starknet::{StarknetProxyMutateRequest, StarknetSignedRequest}; use crate::client::env::{utils, Method}; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; use crate::client::transport::Transport; use crate::client::{CallClient, ClientError, Operation}; +use crate::repr::ReprTransmute; use crate::types::Signed; -use crate::{ProposalWithApprovals, ProxyMutateRequest}; +use crate::{ProposalWithApprovals, ProxyMutateRequest, Repr}; pub mod methods; @@ -49,21 +54,67 @@ impl Method for Mutate { impl Method for Mutate { type Returns = Option; - const METHOD: &'static str = "mutate"; fn encode(self) -> eyre::Result> { - // sign the params, encode it and return - // since you will have a `Vec` here, you can - // `Vec::with_capacity(32 * calldata.len())` and then - // extend the `Vec` with each `Felt::to_bytes_le()` - // when this `Vec` makes it to `StarknetTransport`, - // reconstruct the `Vec` from it - todo!() + // Derive ECDSA key for signing + let secret_scalar = Felt::from_bytes_be(&self.signing_key); + let signing_key = StarknetSigningKey::from_secret_scalar(secret_scalar); + let verifying_key = signing_key.verifying_key().scalar(); + let verifying_key_bytes = verifying_key.to_bytes_be(); + + // Create signer_id from ECDSA verifying key for signature verification + let signer_id = verifying_key_bytes + .rt() + .map_err(|_| eyre::eyre!("Infallible conversion"))?; + + // Create request with signer_id (no Repr) + let request = StarknetProxyMutateRequest::from((signer_id, self.raw_request)); + + // Serialize -> Hash -> Sign with ECDSA + let mut serialized_request = vec![]; + request.encode(&mut serialized_request)?; + let hash = poseidon_hash_many(&serialized_request); + let signature = signing_key.sign(&hash)?; + + let signed_request = StarknetSignedRequest { + payload: serialized_request, + signature_r: signature.r, + signature_s: signature.s, + }; + + let mut signed_request_serialized = vec![]; + signed_request.encode(&mut signed_request_serialized)?; + + let bytes: Vec = signed_request_serialized + .iter() + .flat_map(|felt| felt.to_bytes_be()) + .collect(); + + Ok(bytes) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(None); + } + + // Skip first 32 bytes (array length) + let response = &response[32..]; + + // Get proposal_id from the next 64 bytes (32 for high, 32 for low) + let mut proposal_bytes = [0u8; 32]; + proposal_bytes[..16].copy_from_slice(&response[16..32]); // Last 16 bytes of high + proposal_bytes[16..].copy_from_slice(&response[48..64]); // Last 16 bytes of low + let proposal_id = Repr::new(proposal_bytes.rt()?); + + // Get num_approvals from the last 32 bytes + let num_approvals = u32::from_be_bytes(response[64..][28..32].try_into()?) as usize; + + Ok(Some(ProposalWithApprovals { + proposal_id, + num_approvals, + })) } } diff --git a/crates/context/config/src/client/env/proxy/query/active_proposals.rs b/crates/context/config/src/client/env/proxy/query/active_proposals.rs index 1fa90a900..5e087f715 100644 --- a/crates/context/config/src/client/env/proxy/query/active_proposals.rs +++ b/crates/context/config/src/client/env/proxy/query/active_proposals.rs @@ -27,10 +27,28 @@ impl Method for ActiveProposalRequest { type Returns = u16; fn encode(self) -> eyre::Result> { - todo!() + // No parameters needed for this call + Ok(Vec::new()) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.len() != 32 { + return Err(eyre::eyre!( + "Invalid response length: expected 32 bytes, got {}", + response.len() + )); + } + + // Verify that all bytes except the last two are zero + if !response[..30].iter().all(|&b| b == 0) { + return Err(eyre::eyre!( + "Invalid response format: non-zero bytes in prefix" + )); + } + + // Take the last two bytes for u16 + let value = u16::from_be_bytes([response[30], response[31]]); + + Ok(value) } } diff --git a/crates/context/config/src/client/env/proxy/query/proposal.rs b/crates/context/config/src/client/env/proxy/query/proposal.rs index 64e90d635..136bef02c 100644 --- a/crates/context/config/src/client/env/proxy/query/proposal.rs +++ b/crates/context/config/src/client/env/proxy/query/proposal.rs @@ -1,6 +1,10 @@ use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet_crypto::Felt; use super::ProposalId; +use crate::client::env::proxy::starknet::CallData; +use crate::client::env::proxy::types::starknet::{StarknetProposal, StarknetProposalId}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -27,15 +31,61 @@ impl Method for ProposalRequest { } impl Method for ProposalRequest { - const METHOD: &'static str = "proposals"; + const METHOD: &'static str = "proposal"; type Returns = Option; fn encode(self) -> eyre::Result> { - todo!() + let starknet_id: StarknetProposalId = self.proposal_id.into(); + + let mut call_data = CallData::default(); + starknet_id.encode(&mut call_data)?; + + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(None); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + if felts.is_empty() { + return Ok(None); + } + + // First felt should be 1 for Some, 0 for None + match felts[0].to_bytes_be()[31] { + 0 => Ok(None), + 1 => { + // Decode the proposal starting from index 1 + let proposal = StarknetProposal::decode(&felts[1..]) + .map_err(|e| eyre::eyre!("Failed to decode proposal: {:?}", e))?; + Ok(Some(proposal.into())) + } + v => Err(eyre::eyre!("Invalid option discriminant: {}", v)), + } } } diff --git a/crates/context/config/src/client/env/proxy/query/proposal_approvals.rs b/crates/context/config/src/client/env/proxy/query/proposal_approvals.rs index 898b19085..96c73c5ed 100644 --- a/crates/context/config/src/client/env/proxy/query/proposal_approvals.rs +++ b/crates/context/config/src/client/env/proxy/query/proposal_approvals.rs @@ -1,6 +1,12 @@ use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet::core::types::Felt; use super::ProposalId; +use crate::client::env::proxy::starknet::CallData; +use crate::client::env::proxy::types::starknet::{ + StarknetProposalId, StarknetProposalWithApprovals, +}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -32,10 +38,46 @@ impl Method for ProposalApprovalsRequest { type Returns = ProposalWithApprovals; fn encode(self) -> eyre::Result> { - todo!() + // Convert ProposalId to StarknetProposalId + let starknet_id: StarknetProposalId = self.proposal_id.into(); + + // Encode both high and low parts + let mut call_data = CallData::default(); + starknet_id.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Err(eyre::eyre!("Empty response")); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + let approvals = StarknetProposalWithApprovals::decode(&felts) + .map_err(|e| eyre::eyre!("Failed to decode approvals: {:?}", e))?; + + Ok(approvals.into()) } } diff --git a/crates/context/config/src/client/env/proxy/query/proposal_approvers.rs b/crates/context/config/src/client/env/proxy/query/proposal_approvers.rs index 5e3c01df3..03e07124b 100644 --- a/crates/context/config/src/client/env/proxy/query/proposal_approvers.rs +++ b/crates/context/config/src/client/env/proxy/query/proposal_approvers.rs @@ -1,8 +1,12 @@ use std::mem; use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet::core::types::Felt; use super::ProposalId; +use crate::client::env::proxy::starknet::CallData; +use crate::client::env::proxy::types::starknet::{StarknetApprovers, StarknetProposalId}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -44,10 +48,45 @@ impl Method for ProposalApproversRequest { type Returns = Vec; fn encode(self) -> eyre::Result> { - todo!() + let starknet_id: StarknetProposalId = self.proposal_id.into(); + + let mut call_data = CallData::default(); + starknet_id.encode(&mut call_data)?; + + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(Vec::new()); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + let approvers = StarknetApprovers::decode(&felts) + .map_err(|e| eyre::eyre!("Failed to decode approvers: {:?}", e))?; + + Ok(approvers.into()) } } diff --git a/crates/context/config/src/client/env/proxy/query/proposals.rs b/crates/context/config/src/client/env/proxy/query/proposals.rs index 9bbb4ea22..1b8ddba91 100644 --- a/crates/context/config/src/client/env/proxy/query/proposals.rs +++ b/crates/context/config/src/client/env/proxy/query/proposals.rs @@ -1,5 +1,8 @@ use serde::Serialize; +use starknet::core::codec::{Decode, Encode}; +use starknet_crypto::Felt; +use crate::client::env::proxy::starknet::{CallData, StarknetProposals, StarknetProposalsRequest}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; @@ -31,10 +34,51 @@ impl Method for ProposalsRequest { type Returns = Vec; fn encode(self) -> eyre::Result> { - todo!() + let req = StarknetProposalsRequest { + offset: Felt::from(self.offset as u64), + length: Felt::from(self.length as u64), + }; + let mut call_data = CallData::default(); + req.encode(&mut call_data)?; + Ok(call_data.0) } - fn decode(_response: Vec) -> eyre::Result { - todo!() + fn decode(response: Vec) -> eyre::Result { + if response.is_empty() { + return Ok(Vec::new()); + } + + if response.len() % 32 != 0 { + return Err(eyre::eyre!( + "Invalid response length: {} bytes is not a multiple of 32", + response.len() + )); + } + + // Convert bytes to Felts + let mut felts = Vec::new(); + let chunks = response.chunks_exact(32); + + // Verify no remainder + if !chunks.remainder().is_empty() { + return Err(eyre::eyre!("Response length is not a multiple of 32 bytes")); + } + + for chunk in chunks { + let chunk_array: [u8; 32] = chunk + .try_into() + .map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?; + felts.push(Felt::from_bytes_be(&chunk_array)); + } + + if felts.is_empty() { + return Ok(Vec::new()); + } + + // Decode the array + let proposals = StarknetProposals::decode(&felts) + .map_err(|e| eyre::eyre!("Failed to decode proposals: {:?}", e))?; + + Ok(proposals.into()) } } diff --git a/crates/context/config/src/client/env/proxy/types.rs b/crates/context/config/src/client/env/proxy/types.rs new file mode 100644 index 000000000..a8dc8cd36 --- /dev/null +++ b/crates/context/config/src/client/env/proxy/types.rs @@ -0,0 +1 @@ +pub mod starknet; diff --git a/crates/context/config/src/client/env/proxy/types/starknet.rs b/crates/context/config/src/client/env/proxy/types/starknet.rs new file mode 100644 index 000000000..28672c1c7 --- /dev/null +++ b/crates/context/config/src/client/env/proxy/types/starknet.rs @@ -0,0 +1,390 @@ +use starknet::core::codec::{Decode, Encode, FeltWriter}; +use starknet::core::types::{Felt, U256}; + +use crate::repr::{Repr, ReprBytes, ReprTransmute}; +use crate::types::{ContextIdentity, ProposalId, SignerId}; +use crate::{ + Proposal, ProposalAction, ProposalApprovalWithSigner, ProposalWithApprovals, ProxyMutateRequest, +}; + +#[derive(Debug, Encode, Decode)] +pub struct FeltPair { + pub high: Felt, + pub low: Felt, +} + +#[derive(Debug, Encode, Decode)] +pub struct StarknetIdentity(pub FeltPair); + +#[derive(Debug, Encode, Decode)] +pub struct StarknetProposalId(pub FeltPair); + +#[derive(Debug, Encode, Decode)] +pub struct StarknetU256(pub FeltPair); + +#[derive(Debug, Encode, Decode)] +pub struct StarknetProposal { + pub proposal_id: StarknetProposalId, + pub author_id: StarknetIdentity, + pub actions: StarknetProposalActionWithArgs, +} + +#[derive(Debug, Encode, Decode)] +pub struct StarknetConfirmationRequest { + pub proposal_id: StarknetProposalId, + pub signer_id: StarknetIdentity, + pub added_timestamp: Felt, +} + +#[derive(Debug, Encode, Decode)] +pub struct StarknetProxyMutateRequest { + pub signer_id: StarknetIdentity, + pub kind: StarknetProxyMutateRequestKind, +} + +#[derive(Debug, Encode, Decode)] +pub enum StarknetProxyMutateRequestKind { + Propose(StarknetProposal), + Approve(StarknetConfirmationRequest), +} + +#[derive(Debug, Encode, Decode)] +pub enum StarknetProposalActionWithArgs { + ExternalFunctionCall(Felt, Felt, Vec), + Transfer(Felt, StarknetU256), + SetNumApprovals(Felt), + SetActiveProposalsLimit(Felt), + SetContextValue(Vec, Vec), +} + +#[derive(Debug, Encode, Decode)] +pub struct StarknetSignedRequest { + pub payload: Vec, + pub signature_r: Felt, + pub signature_s: Felt, +} + +#[derive(Debug, Decode)] +pub struct StarknetProposalWithApprovals { + pub proposal_id: StarknetProposalId, + pub num_approvals: Felt, +} + +#[derive(Debug, Decode)] +pub struct StarknetApprovers { + pub approvers: Vec, +} + +#[derive(Debug, Decode)] +pub struct StarknetProposals { + pub proposals: Vec, +} + +impl From for Vec { + fn from(value: StarknetProposals) -> Self { + value.proposals.into_iter().map(Into::into).collect() + } +} + +// Conversions for StarknetIdentity +impl From for FeltPair { + fn from(value: SignerId) -> Self { + let bytes = value.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + FeltPair { + high: Felt::from_bytes_be_slice(high_bytes), + low: Felt::from_bytes_be_slice(low_bytes), + } + } +} + +impl From for SignerId { + fn from(value: FeltPair) -> Self { + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&value.high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&value.low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +impl From for StarknetIdentity { + fn from(value: SignerId) -> Self { + StarknetIdentity(value.into()) + } +} + +impl From for SignerId { + fn from(value: StarknetIdentity) -> Self { + value.0.into() + } +} + +// Conversions for ProposalId +impl From for FeltPair { + fn from(value: ProposalId) -> Self { + let bytes = value.as_bytes(); + let mid_point = bytes.len().checked_div(2).expect("Length should be even"); + let (high_bytes, low_bytes) = bytes.split_at(mid_point); + FeltPair { + high: Felt::from_bytes_be_slice(high_bytes), + low: Felt::from_bytes_be_slice(low_bytes), + } + } +} + +impl From for StarknetProposalId { + fn from(value: ProposalId) -> Self { + StarknetProposalId(value.into()) + } +} + +impl From> for StarknetProposalId { + fn from(value: Repr) -> Self { + StarknetProposalId((*value).into()) + } +} + +impl From> for StarknetIdentity { + fn from(value: Repr) -> Self { + StarknetIdentity((*value).into()) + } +} + +// Conversions for U256 +impl From for StarknetU256 { + fn from(value: U256) -> Self { + StarknetU256(FeltPair { + high: Felt::from(value.high()), + low: Felt::from(value.low()), + }) + } +} + +impl From for StarknetU256 { + fn from(value: u128) -> Self { + StarknetU256(FeltPair { + high: Felt::ZERO, + low: Felt::from(value), + }) + } +} + +// Conversions for ProxyMutateRequest +impl From<(SignerId, ProxyMutateRequest)> for StarknetProxyMutateRequest { + fn from((signer_id, request): (SignerId, ProxyMutateRequest)) -> Self { + StarknetProxyMutateRequest { + signer_id: signer_id.into(), + kind: request.into(), + } + } +} + +impl From for StarknetProxyMutateRequestKind { + fn from(request: ProxyMutateRequest) -> Self { + match request { + ProxyMutateRequest::Propose { proposal } => { + StarknetProxyMutateRequestKind::Propose(proposal.into()) + } + ProxyMutateRequest::Approve { approval } => { + StarknetProxyMutateRequestKind::Approve(approval.into()) + } + } + } +} + +// Conversions for Proposal +impl From for StarknetProposal { + fn from(proposal: Proposal) -> Self { + StarknetProposal { + proposal_id: proposal.id.into(), + author_id: proposal.author_id.into(), + actions: proposal.actions.into(), + } + } +} + +impl From for Proposal { + fn from(value: StarknetProposal) -> Self { + Proposal { + id: Repr::new(value.proposal_id.into()), + author_id: Repr::new(value.author_id.into()), + actions: vec![value.actions.into()], + } + } +} + +// Conversions for ProposalApproval +impl From for StarknetConfirmationRequest { + fn from(approval: ProposalApprovalWithSigner) -> Self { + StarknetConfirmationRequest { + proposal_id: approval.proposal_id.into(), + signer_id: approval.signer_id.into(), + added_timestamp: Felt::from(approval.added_timestamp), + } + } +} + +// Conversions for Actions +impl From> for StarknetProposalActionWithArgs { + fn from(actions: Vec) -> Self { + let action = actions + .into_iter() + .next() + .expect("At least one action required"); + match action { + ProposalAction::ExternalFunctionCall { + receiver_id, + method_name, + args, + .. + } => { + let args_vec: Vec = serde_json::from_str(&args).unwrap_or_default(); + let felt_args = args_vec + .iter() + .map(|arg| { + if arg.starts_with("0x") { + Felt::from_hex_unchecked(arg) + } else { + Felt::from_bytes_be_slice(arg.as_bytes()) + } + }) + .collect(); + + StarknetProposalActionWithArgs::ExternalFunctionCall( + Felt::from_bytes_be_slice(receiver_id.as_bytes()), + Felt::from_bytes_be_slice(method_name.as_bytes()), + felt_args, + ) + } + ProposalAction::Transfer { + receiver_id, + amount, + } => StarknetProposalActionWithArgs::Transfer( + Felt::from_bytes_be_slice(receiver_id.as_bytes()), + amount.into(), + ), + ProposalAction::SetNumApprovals { num_approvals } => { + StarknetProposalActionWithArgs::SetNumApprovals(Felt::from(num_approvals)) + } + ProposalAction::SetActiveProposalsLimit { + active_proposals_limit, + } => StarknetProposalActionWithArgs::SetActiveProposalsLimit(Felt::from( + active_proposals_limit, + )), + ProposalAction::SetContextValue { key, value } => { + StarknetProposalActionWithArgs::SetContextValue( + key.chunks(16).map(Felt::from_bytes_be_slice).collect(), + value.chunks(16).map(Felt::from_bytes_be_slice).collect(), + ) + } + } + } +} + +impl From for ProposalAction { + fn from(action: StarknetProposalActionWithArgs) -> Self { + match action { + StarknetProposalActionWithArgs::ExternalFunctionCall(contract, selector, calldata) => { + ProposalAction::ExternalFunctionCall { + receiver_id: format!("0x{}", hex::encode(contract.to_bytes_be())), + method_name: format!("0x{}", hex::encode(selector.to_bytes_be())), + args: calldata + .iter() + .map(|felt| format!("0x{}", hex::encode(felt.to_bytes_be()))) + .collect::>() + .join(","), + deposit: 0, + gas: 0, + } + } + StarknetProposalActionWithArgs::Transfer(receiver, amount) => { + let FeltPair { high, low } = amount.0; + ProposalAction::Transfer { + receiver_id: format!("0x{}", hex::encode(receiver.to_bytes_be())), + amount: u128::from_be_bytes(low.to_bytes_be()[16..32].try_into().unwrap()) + + (u128::from_be_bytes(high.to_bytes_be()[16..32].try_into().unwrap()) + << 64), + } + } + StarknetProposalActionWithArgs::SetNumApprovals(num) => { + ProposalAction::SetNumApprovals { + num_approvals: u32::from_be_bytes( + num.to_bytes_be()[28..32].try_into().unwrap(), + ), + } + } + StarknetProposalActionWithArgs::SetActiveProposalsLimit(limit) => { + ProposalAction::SetActiveProposalsLimit { + active_proposals_limit: u32::from_be_bytes( + limit.to_bytes_be()[28..32].try_into().unwrap(), + ), + } + } + StarknetProposalActionWithArgs::SetContextValue(key, value) => { + ProposalAction::SetContextValue { + key: key.iter().flat_map(|felt| felt.to_bytes_be()).collect(), + value: value.iter().flat_map(|felt| felt.to_bytes_be()).collect(), + } + } + } + } +} + +impl From for ProposalWithApprovals { + fn from(value: StarknetProposalWithApprovals) -> Self { + ProposalWithApprovals { + proposal_id: Repr::new(value.proposal_id.into()), + num_approvals: u32::from_be_bytes( + value.num_approvals.to_bytes_be()[28..32] + .try_into() + .unwrap(), + ) as usize, + } + } +} + +impl From for Vec { + fn from(value: StarknetApprovers) -> Self { + value + .approvers + .into_iter() + .map(|identity| { + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&identity.0.high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&identity.0.low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + }) + .collect() + } +} + +#[derive(Default, Debug)] +pub struct CallData(pub Vec); + +impl FeltWriter for CallData { + fn write(&mut self, felt: Felt) { + self.0.extend(felt.to_bytes_be()) + } +} + +#[derive(Debug, Encode)] +pub struct StarknetProposalsRequest { + pub offset: Felt, + pub length: Felt, +} + +impl From for ProposalId { + fn from(value: FeltPair) -> Self { + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(&value.high.to_bytes_be()[16..]); + bytes[16..].copy_from_slice(&value.low.to_bytes_be()[16..]); + bytes.rt().expect("Infallible conversion") + } +} + +impl From for ProposalId { + fn from(value: StarknetProposalId) -> Self { + value.0.into() + } +} diff --git a/crates/context/config/src/client/protocol/starknet.rs b/crates/context/config/src/client/protocol/starknet.rs index c3a771088..9a940faea 100644 --- a/crates/context/config/src/client/protocol/starknet.rs +++ b/crates/context/config/src/client/protocol/starknet.rs @@ -2,10 +2,14 @@ use core::str::FromStr; use std::borrow::Cow; use std::collections::BTreeMap; use std::sync::Arc; +use std::time::{Duration, Instant}; use serde::{Deserialize, Serialize}; -use starknet::accounts::{Account, ExecutionEncoding, SingleOwnerAccount}; -use starknet::core::types::{BlockId, BlockTag, Call, Felt, FunctionCall}; +use starknet::accounts::{Account, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount}; +use starknet::core::codec::Decode; +use starknet::core::types::{ + BlockId, BlockTag, Call, ExecutionResult, Felt, FunctionCall, TransactionFinalityStatus, +}; use starknet::core::utils::get_selector_from_name; use starknet::providers::jsonrpc::HttpTransport; use starknet::providers::{JsonRpcClient, Provider, Url}; @@ -13,6 +17,7 @@ use starknet::signers::{LocalWallet, SigningKey}; use thiserror::Error; use super::Protocol; +use crate::client::env::proxy::starknet::StarknetProposalWithApprovals; use crate::client::transport::{AssociatedTransport, Operation, Transport, TransportRequest}; #[derive(Copy, Clone, Debug)] @@ -219,13 +224,10 @@ impl Network { let calldata: Vec = if args.is_empty() { vec![] } else { - args.chunks(32) + args.chunks_exact(32) .map(|chunk| { - let mut padded_chunk = [0_u8; 32]; - for (i, byte) in chunk.iter().enumerate() { - padded_chunk[i] = *byte; - } - Felt::from_bytes_be(&padded_chunk) + let chunk_array: [u8; 32] = chunk.try_into().expect("chunk should be 32 bytes"); + Felt::from_bytes_be(&chunk_array) }) .collect() }; @@ -238,20 +240,19 @@ impl Network { let response = self .client - .call(&function_call, BlockId::Tag(BlockTag::Latest)) - .await; - - response.map_or( - Err(StarknetError::InvalidResponse { + .call(&function_call, BlockId::Tag(BlockTag::Pending)) + .await + .map_err(|e| StarknetError::Custom { operation: ErrorOperation::Query, - }), - |result| { - Ok(result - .into_iter() - .flat_map(|felt| felt.to_bytes_be().to_vec()) - .collect::>()) - }, - ) + reason: format!("Failed to query state: {}", e), + })?; + // Convert response to bytes + let response_bytes = response + .into_iter() + .flat_map(|felt| felt.to_bytes_be()) + .collect::>(); + + Ok(response_bytes) } async fn mutate( @@ -271,13 +272,10 @@ impl Network { let calldata: Vec = if args.is_empty() { vec![] } else { - args.chunks(32) + args.chunks_exact(32) .map(|chunk| { - let mut padded_chunk = [0_u8; 32]; - for (i, byte) in chunk.iter().enumerate() { - padded_chunk[i] = *byte; - } - Felt::from_bytes_be(&padded_chunk) + let chunk_array: [u8; 32] = chunk.try_into().expect("chunk should be 32 bytes"); + Felt::from_bytes_be(&chunk_array) }) .collect() }; @@ -312,9 +310,107 @@ impl Network { }]) .send() .await - .unwrap(); + .map_err(|e| StarknetError::Custom { + operation: ErrorOperation::Mutate, + reason: format!("Failed to send transaction: {}", e), + })?; + + let sent_at = Instant::now(); + let timeout = Duration::from_secs(60); + + let receipt = loop { + let result = account + .provider() + .get_transaction_receipt(response.transaction_hash) + .await; + + match result { + Ok(receipt) => { + if let starknet::core::types::TransactionReceipt::Invoke(invoke_receipt) = + &receipt.receipt + { + match ( + invoke_receipt.finality_status, + &invoke_receipt.execution_result, + ) { + ( + TransactionFinalityStatus::AcceptedOnL2, + ExecutionResult::Succeeded, + ) + | ( + TransactionFinalityStatus::AcceptedOnL1, + ExecutionResult::Succeeded, + ) => { + break receipt; + } + (_, ExecutionResult::Reverted { reason }) => { + return Err(StarknetError::Custom { + operation: ErrorOperation::Mutate, + reason: format!("Transaction reverted: {}", reason), + }); + } + } + } + } + Err(err) => { + if !err.to_string().contains("TransactionHashNotFound") { + return Err(StarknetError::Custom { + operation: ErrorOperation::Mutate, + reason: err.to_string(), + }); + } + } + } - let transaction_hash: Vec = vec![response.transaction_hash.to_bytes_be()[0]]; - Ok(transaction_hash) + if sent_at.elapsed() > timeout { + return Err(StarknetError::TransactionTimeout); + } + + std::thread::sleep(Duration::from_millis(1000)); + }; + // Process the receipt + match receipt.receipt { + starknet::core::types::TransactionReceipt::Invoke(invoke_receipt) => { + match invoke_receipt.execution_result { + ExecutionResult::Succeeded => { + // Process events and look for proposal creation event + for event in invoke_receipt.events.iter() { + // Check if this is a proposal creation event by its key + const PROPOSAL_CREATED_KEY: &str = "ProposalCreated"; + if !event.keys.is_empty() + && event.keys[0] + == get_selector_from_name(PROPOSAL_CREATED_KEY) + .expect("Failed to get selector for ProposalCreated") + { + if event.data.is_empty() { + return Ok(vec![]); + } + + let result = StarknetProposalWithApprovals::decode(&event.data) + .map_err(|e| StarknetError::Custom { + operation: ErrorOperation::Query, + reason: format!("Failed to decode event: {:?}", e), + })?; + let mut encoded = vec![0u8; 32]; + encoded.extend_from_slice(&result.proposal_id.0.high.to_bytes_be()); + encoded.extend_from_slice(&result.proposal_id.0.low.to_bytes_be()); + encoded.extend_from_slice(&result.num_approvals.to_bytes_be()); + return Ok(encoded); + } + } + // If we didn't find a proposal creation event, return empty vec + Ok(vec![]) + } + ExecutionResult::Reverted { reason } => Err(StarknetError::Custom { + operation: ErrorOperation::Mutate, + reason: format!("Transaction reverted: {}", reason), + }), + } + } + starknet::core::types::TransactionReceipt::L1Handler(_) + | starknet::core::types::TransactionReceipt::Declare(_) + | starknet::core::types::TransactionReceipt::Deploy(_) + | starknet::core::types::TransactionReceipt::DeployAccount(_) => Ok(vec![0]), + } } } diff --git a/crates/context/config/src/types.rs b/crates/context/config/src/types.rs index 7f2e957a2..a193e3547 100644 --- a/crates/context/config/src/types.rs +++ b/crates/context/config/src/types.rs @@ -307,6 +307,7 @@ impl ReprBytes for VerifyingKey { pub enum Capability { ManageApplication, ManageMembers, + Proxy, } #[derive(Eq, Debug, Clone, PartialEq, Serialize, Deserialize)] diff --git a/crates/merod/src/cli/init.rs b/crates/merod/src/cli/init.rs index 9ef4c6e96..751a32532 100644 --- a/crates/merod/src/cli/init.rs +++ b/crates/merod/src/cli/init.rs @@ -266,7 +266,7 @@ impl InitCommand { contract_id: match self.protocol { ConfigProtocol::Near => "calimero-context-config.testnet".parse()?, ConfigProtocol::Starknet => { - "0x1ee8182d5dd595be9797ccae1488bdf84b19a0f05a93ce6148b0efae04f4568" + "0x1b991ee006e2d1e372ab96d0a957401fa200358f317b681df2948f30e17c29c" .parse()? } }, diff --git a/crates/server/src/admin/handlers/proposals.rs b/crates/server/src/admin/handlers/proposals.rs index 70afddcdd..2b81080a1 100644 --- a/crates/server/src/admin/handlers/proposals.rs +++ b/crates/server/src/admin/handlers/proposals.rs @@ -1,10 +1,9 @@ use std::sync::Arc; -use std::vec; use axum::extract::Path; use axum::response::IntoResponse; use axum::{Extension, Json}; -use calimero_context_config::repr::{Repr, ReprBytes, ReprTransmute}; +use calimero_context_config::repr::{Repr, ReprTransmute}; use calimero_context_config::types::{ContextIdentity, ProposalId}; use calimero_context_config::{Proposal as ProposalConfig, ProposalWithApprovals}; use calimero_primitives::context::ContextId; @@ -16,7 +15,7 @@ use crate::AdminState; //todo split it up into separate files -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, Copy, Clone)] #[serde(rename_all = "camelCase")] pub enum ActionType { ExternalFunctionCall, From 3d5a67ad202bc47603bf1681569005dcaff54843 Mon Sep 17 00:00:00 2001 From: Xabi Losada Date: Wed, 27 Nov 2024 10:01:07 +0700 Subject: [PATCH 06/11] feat: add install script (#984) --- .../workflows/cross-platform-install-test.yml | 44 +++++++++++++++ scripts/install.sh | 56 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/cross-platform-install-test.yml create mode 100755 scripts/install.sh diff --git a/.github/workflows/cross-platform-install-test.yml b/.github/workflows/cross-platform-install-test.yml new file mode 100644 index 000000000..2c630eaf9 --- /dev/null +++ b/.github/workflows/cross-platform-install-test.yml @@ -0,0 +1,44 @@ +name: Test Install Script + +on: + push: + branches: + - master + + pull_request: + branches: + - master + +jobs: + test-install: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + arch: [x86_64, aarch64] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + # Determine Branch Name + - name: Set Branch Name + id: branch-name + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV + else + echo "BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV + fi + + # Run the installation script + - name: Test installation script + run: | + curl -s https://raw.githubusercontent.com/calimero-network/core/${{ env.BRANCH_NAME }}/scripts/install.sh | bash + + # Validate the binary installation + - name: Validate installation + run: | + which meroctl + meroctl --version diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 000000000..dfdce2834 --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +BINARY_NAME="meroctl" +VERSION="v0.1.1" +REPO="calimero-network/core" +INSTALL_DIR="$HOME/.local/bin" + +# Detect OS and Architecture +OS=$(uname | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) + +case "$ARCH" in + "x86_64") ARCH="x86_64" ;; + "arm64" | "aarch64") ARCH="aarch64" ;; + *) + echo "Unsupported architecture: $ARCH." + exit 1 + ;; +esac + +if [ "$OS" == "darwin" ]; then + PLATFORM="apple-darwin" +elif [ "$OS" == "linux" ]; then + PLATFORM="unknown-linux-gnu" +else + echo "Unsupported operating system: $OS." + exit 1 +fi + +# Construct download URL +TARBALL_NAME="${BINARY_NAME}_${ARCH}-${PLATFORM}.tar.gz" +DOWNLOAD_URL="https://github.com/$REPO/releases/download/$VERSION/$TARBALL_NAME" + +# Ensure installation directory exists +mkdir -p "$INSTALL_DIR" + +# Download binary tarball +echo "Downloading $TARBALL_NAME from $DOWNLOAD_URL..." +curl -L -o "$TARBALL_NAME" "$DOWNLOAD_URL" + +# Extract binary +echo "Extracting $TARBALL_NAME..." +tar -xzf "$TARBALL_NAME" +chmod +x "$BINARY_NAME" + +# Move binary to user-local bin directory +mv "$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME" +rm "$TARBALL_NAME" + +# Add $HOME/.local/bin to PATH if not already present +if ! echo "$PATH" | grep -q "$HOME/.local/bin"; then + echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" + echo "Added $HOME/.local/bin to PATH. Reload your shell or run: source ~/.bashrc" +fi + +echo "$BINARY_NAME installed successfully in $INSTALL_DIR. Run '$BINARY_NAME --version' to verify." From 00752873022ab0c6b1bda38159d9f738848cda04 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Wed, 27 Nov 2024 17:20:58 +0100 Subject: [PATCH 07/11] fix(near): handle request timeout (#989) --- .../config/src/client/protocol/near.rs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/context/config/src/client/protocol/near.rs b/crates/context/config/src/client/protocol/near.rs index 8fa40fca1..6454853c3 100644 --- a/crates/context/config/src/client/protocol/near.rs +++ b/crates/context/config/src/client/protocol/near.rs @@ -4,6 +4,9 @@ use std::{time, vec}; pub use near_crypto::SecretKey; use near_crypto::{InMemorySigner, PublicKey, Signer}; +use near_jsonrpc_client::errors::{ + JsonRpcError, JsonRpcServerError, JsonRpcServerResponseStatusError, +}; use near_jsonrpc_client::methods::query::{RpcQueryRequest, RpcQueryResponse}; use near_jsonrpc_client::methods::send_tx::RpcSendTransactionRequest; use near_jsonrpc_client::methods::tx::RpcTransactionStatusRequest; @@ -298,12 +301,24 @@ impl Network { match response { Ok(response) => break response, Err(err) => { - let Some(RpcTransactionError::TimeoutError) = err.handler_error() else { - return Err(NearError::Custom { - operation: ErrorOperation::Mutate, - reason: err.to_string(), - }); - }; + #[expect( + clippy::wildcard_enum_match_arm, + reason = "quite terse, these variants" + )] + match err { + JsonRpcError::ServerError( + JsonRpcServerError::ResponseStatusError( + JsonRpcServerResponseStatusError::TimeoutError, + ) + | JsonRpcServerError::HandlerError(RpcTransactionError::TimeoutError), + ) => {} + _ => { + return Err(NearError::Custom { + operation: ErrorOperation::Mutate, + reason: err.to_string(), + }); + } + } if sent_at.elapsed().as_secs() > 60 { return Err(NearError::TransactionTimeout); From 31d56aaeef5a7093ea0fb01366b22cf2a8082a38 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Wed, 27 Nov 2024 17:23:02 +0100 Subject: [PATCH 08/11] feat: nuke proxy when erasing context config (#990) --- contracts/context-config/src/sys.rs | 12 ++++++++++-- contracts/proxy-lib/src/lib.rs | 1 - contracts/proxy-lib/src/mutate.rs | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/contracts/context-config/src/sys.rs b/contracts/context-config/src/sys.rs index 00fb9121f..e59b2e1da 100644 --- a/contracts/context-config/src/sys.rs +++ b/contracts/context-config/src/sys.rs @@ -5,7 +5,7 @@ use calimero_context_config::repr::Repr; use calimero_context_config::types::{Application, ContextId, ContextIdentity, SignerId}; use calimero_context_config::{SystemRequest, Timestamp}; use near_sdk::store::{IterableMap, IterableSet}; -use near_sdk::{env, near}; +use near_sdk::{env, near, Gas, NearToken, Promise}; use crate::{parse_input, Config, ContextConfigs, ContextConfigsExt}; @@ -36,9 +36,17 @@ impl ContextConfigs { for (_, context) in self.contexts.drain() { let _ignored = context.application.into_inner(); context.members.into_inner().clear(); - let _ignored = context.proxy.into_inner(); + let proxy = context.proxy.into_inner(); + + let _is_sent_on_drop = Promise::new(proxy).function_call( + "nuke".to_owned(), + vec![], + NearToken::default(), + Gas::from_tgas(1), + ); } + self.next_proxy_id = 0; self.proxy_code.set(None); env::log_str(&format!( diff --git a/contracts/proxy-lib/src/lib.rs b/contracts/proxy-lib/src/lib.rs index 1207cfc28..7425d2783 100644 --- a/contracts/proxy-lib/src/lib.rs +++ b/contracts/proxy-lib/src/lib.rs @@ -102,7 +102,6 @@ impl ProxyContract { &self, proposal_id: Repr, ) -> Option>> { - let approvals_for_proposal = self.approvals.get(&proposal_id); let approvals = self.approvals.get(&proposal_id)?; Some(approvals.iter().flat_map(|a| a.rt()).collect()) } diff --git a/contracts/proxy-lib/src/mutate.rs b/contracts/proxy-lib/src/mutate.rs index ae415a974..8aec79d60 100644 --- a/contracts/proxy-lib/src/mutate.rs +++ b/contracts/proxy-lib/src/mutate.rs @@ -40,6 +40,7 @@ impl ProxyContract { } } } + #[near] impl ProxyContract { #[private] @@ -226,6 +227,26 @@ impl ProxyContract { ) } + fn erase(&mut self) { + // if this is going to be exposed, it should be a proposal + self.proposals.clear(); + self.approvals.clear(); + self.num_proposals_pk.clear(); + self.context_storage.clear(); + } + + pub fn nuke(&mut self) -> Promise { + require!( + env::predecessor_account_id() == self.context_config_account_id, + "Only the context config contract can nuke the proxy" + ); + + self.erase(); + + Promise::new(env::current_account_id()) + .delete_account(self.context_config_account_id.clone()) + } + #[private] pub fn update_contract_callback( &mut self, From 79c9c007550618c11e17507520c593cacbbb37bc Mon Sep 17 00:00:00 2001 From: petarjuki7 <36903459+petarjuki7@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:42:28 +0100 Subject: [PATCH 09/11] feat: transport generalization for recursive declaration (#987) --- .../config/src/client/protocol/near.rs | 20 ++++++- .../config/src/client/protocol/starknet.rs | 20 ++++++- crates/context/config/src/client/transport.rs | 60 +++++++++++++------ 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/crates/context/config/src/client/protocol/near.rs b/crates/context/config/src/client/protocol/near.rs index 6454853c3..f90211c01 100644 --- a/crates/context/config/src/client/protocol/near.rs +++ b/crates/context/config/src/client/protocol/near.rs @@ -28,7 +28,9 @@ use thiserror::Error; use url::Url; use super::Protocol; -use crate::client::transport::{AssociatedTransport, Operation, Transport, TransportRequest}; +use crate::client::transport::{ + AssociatedTransport, Operation, Transport, TransportLike, TransportRequest, +}; #[derive(Copy, Clone, Debug)] pub enum Near {} @@ -41,6 +43,22 @@ impl AssociatedTransport for NearTransport<'_> { type Protocol = Near; } +impl TransportLike for NearTransport<'_> { + type Error = NearError; + + async fn try_send( + &self, + request: TransportRequest<'_>, + payload: &Vec, + ) -> Option, Self::Error>> { + if request.protocol == "near" { + Some(self.send(request, payload.to_vec()).await) + } else { + None + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(try_from = "serde_creds::Credentials")] pub struct Credentials { diff --git a/crates/context/config/src/client/protocol/starknet.rs b/crates/context/config/src/client/protocol/starknet.rs index 9a940faea..5dacde3c9 100644 --- a/crates/context/config/src/client/protocol/starknet.rs +++ b/crates/context/config/src/client/protocol/starknet.rs @@ -18,7 +18,9 @@ use thiserror::Error; use super::Protocol; use crate::client::env::proxy::starknet::StarknetProposalWithApprovals; -use crate::client::transport::{AssociatedTransport, Operation, Transport, TransportRequest}; +use crate::client::transport::{ + AssociatedTransport, Operation, Transport, TransportLike, TransportRequest, +}; #[derive(Copy, Clone, Debug)] pub enum Starknet {} @@ -31,6 +33,22 @@ impl AssociatedTransport for StarknetTransport<'_> { type Protocol = Starknet; } +impl TransportLike for StarknetTransport<'_> { + type Error = StarknetError; + + async fn try_send( + &self, + request: TransportRequest<'_>, + payload: &Vec, + ) -> Option, Self::Error>> { + if request.protocol == "near" { + Some(self.send(request, payload.to_vec()).await) + } else { + None + } + } +} + #[derive(Copy, Clone, Debug, Deserialize, Serialize)] #[serde(try_from = "serde_creds::Credentials")] pub struct Credentials { diff --git a/crates/context/config/src/client/transport.rs b/crates/context/config/src/client/transport.rs index 3a5156137..8b90ca3bf 100644 --- a/crates/context/config/src/client/transport.rs +++ b/crates/context/config/src/client/transport.rs @@ -2,6 +2,7 @@ use core::error::Error; use std::borrow::Cow; use either::Either; +use serde::ser::StdError; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -18,7 +19,7 @@ pub trait Transport { ) -> Result, Self::Error>; } -#[derive(Debug)] +#[derive(Debug, Clone)] #[non_exhaustive] pub struct TransportRequest<'a> { pub protocol: Cow<'a, str>, @@ -72,13 +73,45 @@ impl Transport for Either { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")] pub enum Operation<'a> { Read { method: Cow<'a, str> }, Write { method: Cow<'a, str> }, } +pub trait TransportLike { + type Error; + + async fn try_send( + &self, + request: TransportRequest<'_>, + payload: &Vec, + ) -> Option, Self::Error>>; +} + +impl TransportLike for Both +where + L: TransportLike, + R: TransportLike, +{ + type Error = EitherError; + + async fn try_send( + &self, + request: TransportRequest<'_>, + payload: &Vec, + ) -> Option, Self::Error>> { + if let Some(result) = self.left.try_send(request.clone(), payload).await { + return Some(result.map_err(EitherError::Left)); + } + if let Some(result) = self.right.try_send(request, payload).await { + return Some(result.map_err(EitherError::Right)); + } + None + } +} + pub trait AssociatedTransport: Transport { type Protocol: Protocol; @@ -98,8 +131,10 @@ pub struct Both { impl Transport for Both where - L: AssociatedTransport, - R: AssociatedTransport, + L: TransportLike, + ::Error: StdError, + R: TransportLike, + ::Error: StdError, //no idea why { type Error = EitherError; @@ -108,20 +143,11 @@ where request: TransportRequest<'_>, payload: Vec, ) -> Result, Self::Error> { - if request.protocol == L::protocol() { - self.left - .send(request, payload) - .await - .map_err(EitherError::Left) - } else if request.protocol == R::protocol() { - self.right - .send(request, payload) - .await - .map_err(EitherError::Right) - } else { - return Err(EitherError::UnsupportedProtocol( + match self.try_send(request.clone(), &payload).await { + Some(result) => result, + None => Err(EitherError::UnsupportedProtocol( request.protocol.into_owned(), - )); + )), } } } From 93d84254f724010d161faf5f48b41a4b391fc6d1 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Wed, 27 Nov 2024 23:25:45 +0100 Subject: [PATCH 10/11] feat: iterate on transport generalization for recursive declaration (#991) --- crates/context/config/src/client.rs | 44 ++++- crates/context/config/src/client/env.rs | 9 +- .../config/src/client/env/config/mutate.rs | 2 +- .../config/src/client/env/config/query.rs | 14 +- .../config/src/client/env/proxy/mutate.rs | 2 +- .../config/src/client/env/proxy/query.rs | 10 +- .../config/src/client/protocol/near.rs | 28 +-- .../config/src/client/protocol/starknet.rs | 28 +-- crates/context/config/src/client/relayer.rs | 97 +++++++--- crates/context/config/src/client/transport.rs | 182 +++++++++--------- crates/context/config/src/client/utils.rs | 39 ++++ crates/merod/src/cli/relay.rs | 52 +++-- 12 files changed, 299 insertions(+), 208 deletions(-) create mode 100644 crates/context/config/src/client/utils.rs diff --git a/crates/context/config/src/client.rs b/crates/context/config/src/client.rs index 7b5d80753..859276e6f 100644 --- a/crates/context/config/src/client.rs +++ b/crates/context/config/src/client.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; use std::fmt::Debug; +use std::ops::Deref; use either::Either; use env::Method; @@ -10,10 +11,11 @@ pub mod env; pub mod protocol; pub mod relayer; pub mod transport; +pub mod utils; use config::{ClientConfig, ClientSelectedSigner, Credentials}; use protocol::{near, starknet, Protocol}; -use transport::{Both, Transport, TransportRequest}; +use transport::{Both, Transport, TransportArguments, TransportRequest, UnsupportedProtocol}; pub type AnyTransport = Either< relayer::RelayerTransport, @@ -110,17 +112,43 @@ pub enum ClientError { Transport(T::Error), #[error("codec error: {0}")] Codec(#[from] eyre::Report), - #[error("unsupported protocol: {0}")] - UnsupportedProtocol(String), + #[error( + "unsupported protocol: `{found}`, expected {}", + utils::humanize_iter(expected.deref()) + )] + UnsupportedProtocol { + found: String, + expected: Cow<'static, [Cow<'static, str>]>, + }, +} + +impl<'a, T: Transport> From> for ClientError { + fn from(err: UnsupportedProtocol<'a>) -> Self { + Self::UnsupportedProtocol { + found: err.args.protocol.into_owned(), + expected: err.expected, + } + } } impl Client { async fn send( &self, + protocol: Cow<'_, str>, request: TransportRequest<'_>, payload: Vec, - ) -> Result, T::Error> { - self.transport.send(request, payload).await + ) -> Result, ClientError> { + let res: Result<_, _> = self + .transport + .try_send(TransportArguments { + protocol, + request, + payload, + }) + .await + .into(); + + res?.map_err(ClientError::Transport) } pub fn query<'a, E: Environment<'a, T>>( @@ -182,7 +210,6 @@ impl<'a, T: Transport> CallClient<'a, T> { }; let request = TransportRequest { - protocol: Cow::Borrowed(&self.protocol), network_id: Cow::Borrowed(&self.network_id), contract_id: Cow::Borrowed(&self.contract_id), operation, @@ -190,9 +217,8 @@ impl<'a, T: Transport> CallClient<'a, T> { let response = self .client - .send(request, payload) - .await - .map_err(ClientError::Transport)?; + .send(self.protocol.as_ref().into(), request, payload) + .await?; M::decode(response).map_err(ClientError::Codec) } diff --git a/crates/context/config/src/client/env.rs b/crates/context/config/src/client/env.rs index 2b1734197..f3657ea15 100644 --- a/crates/context/config/src/client/env.rs +++ b/crates/context/config/src/client/env.rs @@ -23,7 +23,7 @@ mod utils { use crate::client::{CallClient, ClientError, Operation}; // todo! when crates are broken up, appropriately locate this - pub(super) async fn send_near_or_starknet( + pub(super) async fn send( client: &CallClient<'_, T>, params: Operation, ) -> Result> @@ -34,9 +34,10 @@ mod utils { match &*client.protocol { Near::PROTOCOL => client.send::(params).await, Starknet::PROTOCOL => client.send::(params).await, - unsupported_protocol => Err(ClientError::UnsupportedProtocol( - unsupported_protocol.to_owned(), - )), + unsupported_protocol => Err(ClientError::UnsupportedProtocol { + found: unsupported_protocol.to_owned(), + expected: vec![Near::PROTOCOL.into(), Starknet::PROTOCOL.into()].into(), + }), } } } diff --git a/crates/context/config/src/client/env/config/mutate.rs b/crates/context/config/src/client/env/config/mutate.rs index d5c851bf3..1c9ad5880 100644 --- a/crates/context/config/src/client/env/config/mutate.rs +++ b/crates/context/config/src/client/env/config/mutate.rs @@ -136,6 +136,6 @@ impl<'a, T: Transport + Debug> ContextConfigMutateRequest<'a, T> { kind: self.kind, }; - utils::send_near_or_starknet(&self.client, Operation::Write(request)).await + utils::send(&self.client, Operation::Write(request)).await } } diff --git a/crates/context/config/src/client/env/config/query.rs b/crates/context/config/src/client/env/config/query.rs index 990109b94..dc9d39f28 100644 --- a/crates/context/config/src/client/env/config/query.rs +++ b/crates/context/config/src/client/env/config/query.rs @@ -28,7 +28,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { context_id: Repr::new(context_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn application_revision( @@ -39,7 +39,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { context_id: Repr::new(context_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn members( @@ -54,7 +54,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { length, }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn has_member( @@ -67,7 +67,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { identity: Repr::new(identity), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn members_revision( @@ -78,7 +78,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { context_id: Repr::new(context_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn privileges( @@ -88,7 +88,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { ) -> Result>, ClientError> { let params = privileges::PrivilegesRequest::new(context_id, identities); - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn get_proxy_contract( @@ -99,6 +99,6 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { context_id: Repr::new(context_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } } diff --git a/crates/context/config/src/client/env/proxy/mutate.rs b/crates/context/config/src/client/env/proxy/mutate.rs index 14afae533..70620d404 100644 --- a/crates/context/config/src/client/env/proxy/mutate.rs +++ b/crates/context/config/src/client/env/proxy/mutate.rs @@ -128,6 +128,6 @@ impl<'a, T: Transport> ContextProxyMutateRequest<'a, T> { raw_request: self.raw_request, }; - utils::send_near_or_starknet(&self.client, Operation::Write(request)).await + utils::send(&self.client, Operation::Write(request)).await } } diff --git a/crates/context/config/src/client/env/proxy/query.rs b/crates/context/config/src/client/env/proxy/query.rs index da84c2cf9..0a48d43e9 100644 --- a/crates/context/config/src/client/env/proxy/query.rs +++ b/crates/context/config/src/client/env/proxy/query.rs @@ -32,7 +32,7 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> { offset, length: limit, }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn proposal( @@ -43,13 +43,13 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> { proposal_id: Repr::new(proposal_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn get_number_of_active_proposals(&self) -> Result> { let params = ActiveProposalRequest; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn get_number_of_proposal_approvals( @@ -60,7 +60,7 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> { proposal_id: Repr::new(proposal_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } pub async fn get_proposal_approvers( @@ -71,6 +71,6 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> { proposal_id: Repr::new(proposal_id), }; - utils::send_near_or_starknet(&self.client, Operation::Read(params)).await + utils::send(&self.client, Operation::Read(params)).await } } diff --git a/crates/context/config/src/client/protocol/near.rs b/crates/context/config/src/client/protocol/near.rs index f90211c01..56822c875 100644 --- a/crates/context/config/src/client/protocol/near.rs +++ b/crates/context/config/src/client/protocol/near.rs @@ -29,7 +29,7 @@ use url::Url; use super::Protocol; use crate::client::transport::{ - AssociatedTransport, Operation, Transport, TransportLike, TransportRequest, + AssociatedTransport, Operation, ProtocolTransport, TransportRequest, }; #[derive(Copy, Clone, Debug)] @@ -43,22 +43,6 @@ impl AssociatedTransport for NearTransport<'_> { type Protocol = Near; } -impl TransportLike for NearTransport<'_> { - type Error = NearError; - - async fn try_send( - &self, - request: TransportRequest<'_>, - payload: &Vec, - ) -> Option, Self::Error>> { - if request.protocol == "near" { - Some(self.send(request, payload.to_vec()).await) - } else { - None - } - } -} - #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(try_from = "serde_creds::Credentials")] pub struct Credentials { @@ -172,8 +156,6 @@ impl<'a> NearTransport<'a> { #[derive(Debug, Error)] #[non_exhaustive] pub enum NearError { - #[error("unsupported protocol `{0}`")] - UnsupportedProtocol(String), #[error("unknown network `{0}`")] UnknownNetwork(String), #[error("invalid response from RPC while {operation}")] @@ -206,7 +188,7 @@ pub enum ErrorOperation { FetchAccount, } -impl Transport for NearTransport<'_> { +impl ProtocolTransport for NearTransport<'_> { type Error = NearError; async fn send( @@ -214,12 +196,6 @@ impl Transport for NearTransport<'_> { request: TransportRequest<'_>, payload: Vec, ) -> Result, Self::Error> { - if request.protocol != Near::PROTOCOL { - return Err(NearError::UnsupportedProtocol( - request.protocol.into_owned(), - )); - } - let Some(network) = self.networks.get(&request.network_id) else { return Err(NearError::UnknownNetwork(request.network_id.into_owned())); }; diff --git a/crates/context/config/src/client/protocol/starknet.rs b/crates/context/config/src/client/protocol/starknet.rs index 5dacde3c9..8dafeaf2b 100644 --- a/crates/context/config/src/client/protocol/starknet.rs +++ b/crates/context/config/src/client/protocol/starknet.rs @@ -19,7 +19,7 @@ use thiserror::Error; use super::Protocol; use crate::client::env::proxy::starknet::StarknetProposalWithApprovals; use crate::client::transport::{ - AssociatedTransport, Operation, Transport, TransportLike, TransportRequest, + AssociatedTransport, Operation, ProtocolTransport, TransportRequest, }; #[derive(Copy, Clone, Debug)] @@ -33,22 +33,6 @@ impl AssociatedTransport for StarknetTransport<'_> { type Protocol = Starknet; } -impl TransportLike for StarknetTransport<'_> { - type Error = StarknetError; - - async fn try_send( - &self, - request: TransportRequest<'_>, - payload: &Vec, - ) -> Option, Self::Error>> { - if request.protocol == "near" { - Some(self.send(request, payload.to_vec()).await) - } else { - None - } - } -} - #[derive(Copy, Clone, Debug, Deserialize, Serialize)] #[serde(try_from = "serde_creds::Credentials")] pub struct Credentials { @@ -153,8 +137,6 @@ impl<'a> StarknetTransport<'a> { #[derive(Debug, Error)] #[non_exhaustive] pub enum StarknetError { - #[error("unsupported protocol: {0}")] - UnsupportedProtocol(String), #[error("unknown network `{0}`")] UnknownNetwork(String), #[error("invalid response from RPC while {operation}")] @@ -191,7 +173,7 @@ pub enum ErrorOperation { FetchNonce, } -impl Transport for StarknetTransport<'_> { +impl ProtocolTransport for StarknetTransport<'_> { type Error = StarknetError; async fn send( @@ -199,12 +181,6 @@ impl Transport for StarknetTransport<'_> { request: TransportRequest<'_>, payload: Vec, ) -> Result, Self::Error> { - if request.protocol != Starknet::PROTOCOL { - return Err(StarknetError::UnsupportedProtocol( - request.protocol.into_owned(), - )); - } - let Some(network) = self.networks.get(&request.network_id) else { return Err(StarknetError::UnknownNetwork( request.network_id.into_owned(), diff --git a/crates/context/config/src/client/relayer.rs b/crates/context/config/src/client/relayer.rs index 605c05a37..7e01411ab 100644 --- a/crates/context/config/src/client/relayer.rs +++ b/crates/context/config/src/client/relayer.rs @@ -1,10 +1,15 @@ +#![allow(clippy::multiple_inherent_impl, reason = "it's fine")] + use std::borrow::Cow; +use reqwest::StatusCode; use serde::{Deserialize, Serialize}; use thiserror::Error; use url::Url; -use super::transport::{Operation, Transport, TransportRequest}; +use super::transport::{ + Operation, Transport, TransportArguments, TransportRequest, UnsupportedProtocol, +}; #[derive(Debug)] #[non_exhaustive] @@ -48,40 +53,84 @@ pub enum RelayerError { "relayer response ({status}): {}", body.is_empty().then_some("").unwrap_or(body) )] - Response { - status: reqwest::StatusCode, - body: String, - }, + Response { status: StatusCode, body: String }, } -impl Transport for RelayerTransport { - type Error = RelayerError; +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "kind", content = "data")] +pub enum ServerError { + UnsupportedProtocol { + found: Cow<'static, str>, + expected: Cow<'static, [Cow<'static, str>]>, + }, +} - async fn send( +impl RelayerTransport { + async fn send<'a>( &self, - request: TransportRequest<'_>, - payload: Vec, - ) -> Result, Self::Error> { + args: TransportArguments<'a>, + ) -> Result, UnsupportedProtocol<'a>>, RelayerError> { + let request = RelayRequest { + protocol: args.protocol, + network_id: args.request.network_id, + contract_id: args.request.contract_id, + operation: args.request.operation, + payload: args.payload, + }; + let response = self .client .post(self.url.clone()) - .json(&RelayRequest { - protocol: request.protocol, - network_id: request.network_id, - contract_id: request.contract_id, - operation: request.operation, - payload, - }) + .json(&request) .send() .await?; - if !response.status().is_success() { - return Err(RelayerError::Response { - status: response.status(), - body: response.text().await?, - }); + match response.status() { + status if status.is_success() => { + return response + .bytes() + .await + .map(|v| Ok(v.into())) + .map_err(|e| e.into()); + } + status if status == StatusCode::BAD_REQUEST => {} + status => { + return Err(RelayerError::Response { + status, + body: response.text().await?, + }) + } } - response.bytes().await.map(Into::into).map_err(Into::into) + let error = response.json::().await?; + + match error { + ServerError::UnsupportedProtocol { found: _, expected } => { + let args = TransportArguments { + protocol: request.protocol, + request: TransportRequest { + network_id: request.network_id, + contract_id: request.contract_id, + operation: request.operation, + }, + payload: request.payload, + }; + + Ok(Err(UnsupportedProtocol { args, expected })) + } + } + } +} + +impl Transport for RelayerTransport { + type Error = RelayerError; + + async fn try_send<'a>( + &self, + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>> { + self.send(args) + .await + .map_or_else(|e| Ok(Err(e)), |v| v.map(Ok)) } } diff --git a/crates/context/config/src/client/transport.rs b/crates/context/config/src/client/transport.rs index 8b90ca3bf..df9e12ac9 100644 --- a/crates/context/config/src/client/transport.rs +++ b/crates/context/config/src/client/transport.rs @@ -2,16 +2,15 @@ use core::error::Error; use std::borrow::Cow; use either::Either; -use serde::ser::StdError; use serde::{Deserialize, Serialize}; use thiserror::Error; use super::protocol::Protocol; -pub trait Transport { +pub trait ProtocolTransport { type Error: Error; - #[expect(async_fn_in_trait, reason = "Should be fine")] + #[expect(async_fn_in_trait, reason = "constraints are upheld for now")] async fn send( &self, request: TransportRequest<'_>, @@ -19,30 +18,41 @@ pub trait Transport { ) -> Result, Self::Error>; } -#[derive(Debug, Clone)] -#[non_exhaustive] +#[derive(Debug)] pub struct TransportRequest<'a> { - pub protocol: Cow<'a, str>, pub network_id: Cow<'a, str>, pub contract_id: Cow<'a, str>, pub operation: Operation<'a>, } -impl<'a> TransportRequest<'a> { - #[must_use] - pub const fn new( - protocol: Cow<'a, str>, - network_id: Cow<'a, str>, - contract_id: Cow<'a, str>, - operation: Operation<'a>, - ) -> Self { - Self { - protocol, - network_id, - contract_id, - operation, - } - } +#[derive(Debug, Serialize, Deserialize)] +#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")] +pub enum Operation<'a> { + Read { method: Cow<'a, str> }, + Write { method: Cow<'a, str> }, +} + +pub trait Transport { + type Error: Error; + + #[expect(async_fn_in_trait, reason = "constraints are upheld for now")] + async fn try_send<'a>( + &self, + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>>; +} + +#[derive(Debug)] +pub struct TransportArguments<'a> { + pub protocol: Cow<'a, str>, + pub request: TransportRequest<'a>, + pub payload: Vec, +} + +#[derive(Debug)] +pub struct UnsupportedProtocol<'a> { + pub args: TransportArguments<'a>, + pub expected: Cow<'static, [Cow<'static, str>]>, } #[derive(Debug, Error)] @@ -51,68 +61,76 @@ pub enum EitherError { Left(L), #[error(transparent)] Right(R), - #[error("unsupported protocol: {0}")] - UnsupportedProtocol(String), } -impl Transport for Either { +impl Transport for Either +where + L: Transport, + R: Transport, +{ type Error = EitherError; - async fn send( + async fn try_send<'a>( &self, - request: TransportRequest<'_>, - payload: Vec, - ) -> Result, Self::Error> { + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>> { match self { - Self::Left(left) => left.send(request, payload).await.map_err(EitherError::Left), - Self::Right(right) => right - .send(request, payload) - .await - .map_err(EitherError::Right), + Self::Left(left) => Ok(left.try_send(args).await?.map_err(EitherError::Left)), + Self::Right(right) => Ok(right.try_send(args).await?.map_err(EitherError::Right)), } } } -#[derive(Debug, Serialize, Deserialize, Clone)] -#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")] -pub enum Operation<'a> { - Read { method: Cow<'a, str> }, - Write { method: Cow<'a, str> }, -} - -pub trait TransportLike { - type Error; - - async fn try_send( - &self, - request: TransportRequest<'_>, - payload: &Vec, - ) -> Option, Self::Error>>; +#[expect(clippy::exhaustive_structs, reason = "this is exhaustive")] +#[derive(Debug, Clone)] +pub struct Both { + pub left: L, + pub right: R, } -impl TransportLike for Both +impl Transport for Both where - L: TransportLike, - R: TransportLike, + L: Transport, + R: Transport, { type Error = EitherError; - async fn try_send( + async fn try_send<'a>( &self, - request: TransportRequest<'_>, - payload: &Vec, - ) -> Option, Self::Error>> { - if let Some(result) = self.left.try_send(request.clone(), payload).await { - return Some(result.map_err(EitherError::Left)); - } - if let Some(result) = self.right.try_send(request, payload).await { - return Some(result.map_err(EitherError::Right)); - } - None + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>> { + let left = self.left.try_send(args).await; + + let UnsupportedProtocol { + args, + expected: left_expected, + } = match left { + Ok(res) => return Ok(res.map_err(EitherError::Left)), + Err(err) => err, + }; + + let right = self.right.try_send(args).await; + + let UnsupportedProtocol { + args, + expected: right_expected, + } = match right { + Ok(res) => return Ok(res.map_err(EitherError::Right)), + Err(err) => err, + }; + + let mut expected = left_expected.into_owned(); + + expected.extend(right_expected.into_owned()); + + Err(UnsupportedProtocol { + args, + expected: expected.into(), + }) } } -pub trait AssociatedTransport: Transport { +pub trait AssociatedTransport: ProtocolTransport { type Protocol: Protocol; #[inline] @@ -122,32 +140,22 @@ pub trait AssociatedTransport: Transport { } } -#[expect(clippy::exhaustive_structs, reason = "this is exhaustive")] -#[derive(Debug, Clone)] -pub struct Both { - pub left: L, - pub right: R, -} - -impl Transport for Both -where - L: TransportLike, - ::Error: StdError, - R: TransportLike, - ::Error: StdError, //no idea why -{ - type Error = EitherError; +impl Transport for T { + type Error = T::Error; - async fn send( + async fn try_send<'a>( &self, - request: TransportRequest<'_>, - payload: Vec, - ) -> Result, Self::Error> { - match self.try_send(request.clone(), &payload).await { - Some(result) => result, - None => Err(EitherError::UnsupportedProtocol( - request.protocol.into_owned(), - )), + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>> { + let protocol = Self::protocol(); + + if args.protocol != protocol { + return Err(UnsupportedProtocol { + args, + expected: vec![protocol.into()].into(), + }); } + + Ok(self.send(args.request, args.payload).await) } } diff --git a/crates/context/config/src/client/utils.rs b/crates/context/config/src/client/utils.rs new file mode 100644 index 000000000..86608fbf4 --- /dev/null +++ b/crates/context/config/src/client/utils.rs @@ -0,0 +1,39 @@ +use core::fmt::{self, Write}; + +pub fn humanize_iter(iter: I) -> String +where + I: IntoIterator, +{ + let mut res = String::new(); + + let mut iter = iter.into_iter().peekable(); + + while let Some(item) = iter.next() { + if !res.is_empty() { + if iter.peek().is_some() { + res.push_str(", "); + } else { + res.push_str(" and "); + } + } + + write!(res, "`{}`", item).expect("infallible"); + } + + res +} + +#[test] +fn test_humanize_iter() { + assert_eq!( + humanize_iter(&["near", "starknet", "ethereum", "solana"]), + "`near`, `starknet`, `ethereum` and `solana`" + ); + assert_eq!( + humanize_iter(&["polkadot", "near", "icp"]), + "`polkadot`, `near` and `icp`" + ); + assert_eq!(humanize_iter(&["this", "that"]), "`this` and `that`"); + assert_eq!(humanize_iter(&["me"]), "`me`"); + assert_eq!(humanize_iter::<[u8; 0]>([]), ""); +} diff --git a/crates/merod/src/cli/relay.rs b/crates/merod/src/cli/relay.rs index f52fa8805..764417871 100644 --- a/crates/merod/src/cli/relay.rs +++ b/crates/merod/src/cli/relay.rs @@ -10,8 +10,10 @@ use axum::{Json, Router}; use calimero_config::ConfigFile; use calimero_context_config::client::config::Credentials; use calimero_context_config::client::protocol::{near, starknet}; -use calimero_context_config::client::relayer::RelayRequest; -use calimero_context_config::client::transport::{Both, Transport, TransportRequest}; +use calimero_context_config::client::relayer::{RelayRequest, ServerError}; +use calimero_context_config::client::transport::{ + Both, Transport, TransportArguments, TransportRequest, +}; use clap::{Parser, ValueEnum}; use eyre::{bail, Result as EyreResult}; use futures_util::FutureExt; @@ -119,21 +121,26 @@ impl RelayCommand { let handle = async move { while let Some((request, res_tx)) = rx.recv().await { - let payload = request.payload; - - let request = TransportRequest::new( - request.protocol, - request.network_id, - request.contract_id, - request.operation, - ); - - let _ignored = res_tx.send( - both_transport - .send(request, payload) - .await - .map_err(Into::into), - ); + let args = TransportArguments { + protocol: request.protocol, + request: TransportRequest { + network_id: request.network_id, + contract_id: request.contract_id, + operation: request.operation, + }, + payload: request.payload, + }; + + let res = both_transport + .try_send(args) + .await + .map(|res| res.map_err(Into::into)) + .map_err(|err| ServerError::UnsupportedProtocol { + found: err.args.protocol, + expected: err.expected, + }); + + let _ignored = res_tx.send(res); } }; @@ -153,7 +160,7 @@ impl RelayCommand { type AppState = mpsc::Sender; type RequestPayload = (RelayRequest<'static>, HandlerSender); -type HandlerSender = oneshot::Sender>>; +type HandlerSender = oneshot::Sender>, ServerError>>; async fn handler( State(req_tx): State, @@ -170,6 +177,15 @@ async fn handler( .await .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let res = match res { + Ok(res) => res, + Err(err) => { + debug!("failed to send request to handler: {:?}", err); + + return Ok((StatusCode::BAD_REQUEST, Json(err)).into_response()); + } + }; + match res { Ok(res) => Ok(res.into_response()), Err(err) => { From 1f1cfac647f5676785314968d8e999dc0ea79e30 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Fri, 29 Nov 2024 13:25:53 +0100 Subject: [PATCH 11/11] fix: correctly determine proxy contract when joining (#995) --- crates/context/src/lib.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index 3aed44210..e706b1ee7 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -145,19 +145,6 @@ impl ContextManager { PrivateKey::random(&mut rand::thread_rng()) } - async fn get_proxy_contract(&self, context_id: ContextId) -> EyreResult { - let proxy_contract = self - .config_client - .query::( - self.client_config.new.protocol.as_str().into(), - self.client_config.new.network.as_str().into(), - self.client_config.new.contract_id.as_str().into(), - ) - .get_proxy_contract(context_id.rt().expect("infallible conversion")) - .await?; - Ok(proxy_contract) - } - pub fn create_context( &self, seed: Option<[u8; 32]>, @@ -253,7 +240,15 @@ impl ContextManager { .send(*context_secret) .await?; - let proxy_contract = this.get_proxy_contract(context.id).await?; + let proxy_contract = this + .config_client + .query::( + this.client_config.new.protocol.as_str().into(), + this.client_config.new.network.as_str().into(), + this.client_config.new.contract_id.as_str().into(), + ) + .get_proxy_contract(context.id.rt().expect("infallible conversion")) + .await?; let key = ContextConfigKey::new(context.id); let mut config = handle.get(&key)?.ok_or_eyre("expected config to exist")?; @@ -355,7 +350,16 @@ impl ContextManager { let context_exists = handle.has(&ContextMetaKey::new(context_id))?; let mut config = if !context_exists { - let proxy_contract = self.get_proxy_contract(context_id).await?; + let proxy_contract = self + .config_client + .query::( + protocol.as_str().into(), + network_id.as_str().into(), + contract_id.as_str().into(), + ) + .get_proxy_contract(context_id.rt().expect("infallible conversion")) + .await?; + Some(ContextConfigParams { protocol: protocol.into(), network_id: network_id.into(),