From dde86e68796841b98931948431fa300db0a9ef91 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Mon, 4 Mar 2024 23:35:27 +0800 Subject: [PATCH 1/4] Format code using `cargo fmt`. --- registry/src/contract.rs | 11 +- registry/src/lib.rs | 4 +- registry/src/storage/mod.rs | 2 +- registry/src/tests/mod.rs | 2 +- registry/src/tests/test_admin.rs | 56 +++++---- registry/src/tests/test_records.rs | 171 +++++++++++++++------------ registry/src/tests/test_transfers.rs | 65 ++++++---- 7 files changed, 184 insertions(+), 127 deletions(-) diff --git a/registry/src/contract.rs b/registry/src/contract.rs index d01d4c0..00042ad 100644 --- a/registry/src/contract.rs +++ b/registry/src/contract.rs @@ -178,7 +178,8 @@ impl RegistryContractTrait for RegistryContract { panic_with_error!(&e, &ContractErrors::ExpiredDomain); } - let node_hash: BytesN<32> = generate_node(&e, &sub, &(Bytes::from(domain.node.clone()))); + let node_hash: BytesN<32> = + generate_node(&e, &sub, &(Bytes::from(domain.node.clone()))); let record_key: RecordKeys = RecordKeys::SubRecord(node_hash.clone()); e.set_record(&Record::SubDomain(SubDomain { @@ -212,7 +213,9 @@ impl RegistryContractTrait for RegistryContract { Some(Record::Domain(domain)) } Record::SubDomain(sub) => { - if let Record::Domain(domain) = e.record(&RecordKeys::Record(sub.parent.clone())).unwrap() { + if let Record::Domain(domain) = + e.record(&RecordKeys::Record(sub.parent.clone())).unwrap() + { if domain.exp_date < e.ledger().timestamp() { panic_with_error!(&e, &ContractErrors::ExpiredDomain); } @@ -271,7 +274,9 @@ impl RegistryContractTrait for RegistryContract { ); } Record::SubDomain(sub) => { - if let Record::Domain(domain) = e.record(&RecordKeys::Record(sub.parent.clone())).unwrap() { + if let Record::Domain(domain) = + e.record(&RecordKeys::Record(sub.parent.clone())).unwrap() + { domain.owner.require_auth(); } else { panic_with_error!(&e, &ContractErrors::InvalidParent); diff --git a/registry/src/lib.rs b/registry/src/lib.rs index 6fae0f2..06d929c 100644 --- a/registry/src/lib.rs +++ b/registry/src/lib.rs @@ -1,7 +1,7 @@ #![no_std] mod contract; -mod storage; mod errors; +mod storage; +mod tests; mod utils; -mod tests; \ No newline at end of file diff --git a/registry/src/storage/mod.rs b/registry/src/storage/mod.rs index 8adea90..249b7b6 100644 --- a/registry/src/storage/mod.rs +++ b/registry/src/storage/mod.rs @@ -1,2 +1,2 @@ pub mod core; -pub mod record; \ No newline at end of file +pub mod record; diff --git a/registry/src/tests/mod.rs b/registry/src/tests/mod.rs index 8d79e45..47d6f2f 100644 --- a/registry/src/tests/mod.rs +++ b/registry/src/tests/mod.rs @@ -1,4 +1,4 @@ pub mod test_admin; pub mod test_records; +pub mod test_transfers; pub mod test_utils; -pub mod test_transfers; \ No newline at end of file diff --git a/registry/src/tests/test_admin.rs b/registry/src/tests/test_admin.rs index 8ec5a60..4edfb8a 100644 --- a/registry/src/tests/test_admin.rs +++ b/registry/src/tests/test_admin.rs @@ -1,9 +1,9 @@ #![cfg(test)] -use soroban_sdk::{Bytes, Env, IntoVal, Vec}; -use soroban_sdk::testutils::{MockAuth, MockAuthInvoke}; use crate::storage::core::{CoreData, CoreDataEntity}; use crate::tests::test_utils::{create_test_data, init_contract, TestData}; +use soroban_sdk::testutils::{MockAuth, MockAuthInvoke}; +use soroban_sdk::{Bytes, Env, IntoVal, Vec}; #[test] pub fn test_updating_tlds() { @@ -18,28 +18,40 @@ pub fn test_updating_tlds() { assert_eq!(core.allowed_tlds, test_data.allowed_tlds); // It should fail because the admin didn't sign the transaction - assert!( - test_data.contract_client.try_update_tlds(&Vec::from_array(&e, [Bytes::from_slice(&e, "eth".as_bytes())])) - .is_err() - ); - - test_data.contract_client - .mock_auths(&[ - MockAuth { - address: &test_data.adm, - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "update_tlds", - args: (Vec::from_array(&e, [Bytes::from_slice(&e, "eth".as_bytes())]), ).into_val(&e), - sub_invokes: &[], - }, - } - ]) - .update_tlds(&Vec::from_array(&e, [Bytes::from_slice(&e, "eth".as_bytes())])); + assert!(test_data + .contract_client + .try_update_tlds(&Vec::from_array( + &e, + [Bytes::from_slice(&e, "eth".as_bytes())] + )) + .is_err()); + + test_data + .contract_client + .mock_auths(&[MockAuth { + address: &test_data.adm, + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "update_tlds", + args: (Vec::from_array( + &e, + [Bytes::from_slice(&e, "eth".as_bytes())], + ),) + .into_val(&e), + sub_invokes: &[], + }, + }]) + .update_tlds(&Vec::from_array( + &e, + [Bytes::from_slice(&e, "eth".as_bytes())], + )); let updated_core: CoreData = e.as_contract(&test_data.contract_client.address, || { e.core_data().unwrap() }); - assert_eq!(updated_core.allowed_tlds, Vec::from_array(&e, [Bytes::from_slice(&e, "eth".as_bytes())])); -} \ No newline at end of file + assert_eq!( + updated_core.allowed_tlds, + Vec::from_array(&e, [Bytes::from_slice(&e, "eth".as_bytes())]) + ); +} diff --git a/registry/src/tests/test_records.rs b/registry/src/tests/test_records.rs index cc3330e..05fd3cb 100644 --- a/registry/src/tests/test_records.rs +++ b/registry/src/tests/test_records.rs @@ -3,9 +3,9 @@ use crate::errors::ContractErrors; use crate::storage::record::{Domain, Record, RecordKeys, SubDomain}; use crate::tests::test_utils::{create_test_data, init_contract, TestData}; +use crate::utils::records::generate_node; use soroban_sdk::testutils::{Address as _, MockAuth, MockAuthInvoke}; use soroban_sdk::{Address, Bytes, BytesN, Env, IntoVal}; -use crate::utils::records::generate_node; #[test] fn test_setting_record() { @@ -195,85 +195,99 @@ fn test_subdomains() { let tld: Bytes = Bytes::from_slice(&e, "xlm".as_bytes()); let duration: u64 = test_data.min_duration; - test_data.col_asset_stellar.mock_all_auths().mint( + test_data + .col_asset_stellar + .mock_all_auths() + .mint(&owner, &(duration as i128 * test_data.node_rate as i128)); + + test_data.contract_client.mock_all_auths().set_record( + &domain, + &tld, &owner, - &(duration as i128 * test_data.node_rate as i128), + &domain_address, + &duration, ); - test_data.contract_client - .mock_all_auths() - .set_record( - &domain, - &tld, - &owner, - &domain_address, - &duration, - ); - let sub_domain: Bytes = Bytes::from_slice(&e, "payments".as_bytes()); let domain_node: BytesN<32> = generate_node(&e, &domain, &tld); let new_address: Address = Address::generate(&e); - test_data.contract_client - .mock_auths(&[ - MockAuth { - address: &owner, - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "set_sub", - args: ( - sub_domain.clone(), - RecordKeys::Record(domain_node.clone()), - new_address.clone() - ).into_val(&e), - sub_invokes: &[], - }, - } - ]) - .set_sub(&sub_domain, &RecordKeys::Record(domain_node.clone()), &new_address); - - let sub_domain_node: BytesN<32> = generate_node(&e, &sub_domain, &Bytes::from(domain_node.clone())); - - let sub_domain_record: SubDomain = match test_data.contract_client.record(&RecordKeys::SubRecord(sub_domain_node.clone())).unwrap() { + test_data + .contract_client + .mock_auths(&[MockAuth { + address: &owner, + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "set_sub", + args: ( + sub_domain.clone(), + RecordKeys::Record(domain_node.clone()), + new_address.clone(), + ) + .into_val(&e), + sub_invokes: &[], + }, + }]) + .set_sub( + &sub_domain, + &RecordKeys::Record(domain_node.clone()), + &new_address, + ); + + let sub_domain_node: BytesN<32> = + generate_node(&e, &sub_domain, &Bytes::from(domain_node.clone())); + + let sub_domain_record: SubDomain = match test_data + .contract_client + .record(&RecordKeys::SubRecord(sub_domain_node.clone())) + .unwrap() + { Record::Domain(_) => panic!(), Record::SubDomain(sub) => sub, }; - assert_eq!(sub_domain_record.address, new_address); assert_eq!(sub_domain_record.parent, domain_node); // If there is no signature from the owner of the root domain, it can not update the subdomain - assert!( - test_data.contract_client - .mock_auths(&[ - MockAuth { - address: &Address::generate(&e), - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "set_sub", - args: ( - sub_domain.clone(), - RecordKeys::Record(domain_node.clone()), - new_address.clone() - ).into_val(&e), - sub_invokes: &[], - }, - } - ]) - .try_set_sub(&sub_domain, &RecordKeys::Record(domain_node.clone()), &new_address) - .is_err() - ); + assert!(test_data + .contract_client + .mock_auths(&[MockAuth { + address: &Address::generate(&e), + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "set_sub", + args: ( + sub_domain.clone(), + RecordKeys::Record(domain_node.clone()), + new_address.clone() + ) + .into_val(&e), + sub_invokes: &[], + }, + }]) + .try_set_sub( + &sub_domain, + &RecordKeys::Record(domain_node.clone()), + &new_address + ) + .is_err()); let sub_domain: Bytes = Bytes::from_slice(&e, "payments".as_bytes()); let domain_node: BytesN<32> = generate_node(&e, &domain, &tld); let updated_address: Address = Address::generate(&e); - test_data.contract_client - .mock_all_auths() - .set_sub(&sub_domain, &RecordKeys::Record(domain_node.clone()), &updated_address); + test_data.contract_client.mock_all_auths().set_sub( + &sub_domain, + &RecordKeys::Record(domain_node.clone()), + &updated_address, + ); - let updated_sub_domain_record: SubDomain = match test_data.contract_client.record(&RecordKeys::SubRecord(sub_domain_node.clone())).unwrap() { + let updated_sub_domain_record: SubDomain = match test_data + .contract_client + .record(&RecordKeys::SubRecord(sub_domain_node.clone())) + .unwrap() + { Record::Domain(_) => panic!(), Record::SubDomain(sub) => sub, }; @@ -309,24 +323,35 @@ fn test_updating_address() { let node: BytesN<32> = test_data.contract_client.parse_domain(&new_domain, &tld); - let first_record: Domain = match test_data.contract_client.record(&RecordKeys::Record(node.clone())).unwrap() { + let first_record: Domain = match test_data + .contract_client + .record(&RecordKeys::Record(node.clone())) + .unwrap() + { Record::Domain(domain) => domain, - Record::SubDomain(_) => panic!() + Record::SubDomain(_) => panic!(), }; - test_data.contract_client.mock_auths(&[MockAuth { - address: &owner, - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "update_address", - args: (RecordKeys::Record(node.clone()), second_address.clone()).into_val(&e), - sub_invokes: &[], - }, - }]).update_address(&RecordKeys::Record(node.clone()), &second_address); - - let second_record: Domain = match test_data.contract_client.record(&RecordKeys::Record(node.clone())).unwrap() { + test_data + .contract_client + .mock_auths(&[MockAuth { + address: &owner, + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "update_address", + args: (RecordKeys::Record(node.clone()), second_address.clone()).into_val(&e), + sub_invokes: &[], + }, + }]) + .update_address(&RecordKeys::Record(node.clone()), &second_address); + + let second_record: Domain = match test_data + .contract_client + .record(&RecordKeys::Record(node.clone())) + .unwrap() + { Record::Domain(domain) => domain, - Record::SubDomain(_) => panic!() + Record::SubDomain(_) => panic!(), }; assert_eq!(address, first_record.address); diff --git a/registry/src/tests/test_transfers.rs b/registry/src/tests/test_transfers.rs index fa05bcc..563bb07 100644 --- a/registry/src/tests/test_transfers.rs +++ b/registry/src/tests/test_transfers.rs @@ -1,9 +1,9 @@ #![cfg(test)] -use soroban_sdk::{Address, Bytes, Env, BytesN, IntoVal}; -use soroban_sdk::testutils::{Address as _, Ledger, LedgerInfo, MockAuth, MockAuthInvoke}; use crate::storage::record::{Domain, Record, RecordKeys}; use crate::tests::test_utils::{create_test_data, init_contract, TestData}; +use soroban_sdk::testutils::{Address as _, Ledger, LedgerInfo, MockAuth, MockAuthInvoke}; +use soroban_sdk::{Address, Bytes, BytesN, Env, IntoVal}; #[test] fn test_simple_transfer() { @@ -32,9 +32,13 @@ fn test_simple_transfer() { let node: BytesN<32> = test_data.contract_client.parse_domain(&new_domain, &tld); - let first_record: Domain = match test_data.contract_client.record(&RecordKeys::Record(node.clone())).unwrap() { + let first_record: Domain = match test_data + .contract_client + .record(&RecordKeys::Record(node.clone())) + .unwrap() + { Record::Domain(domain) => domain, - Record::SubDomain(_) => panic!() + Record::SubDomain(_) => panic!(), }; e.ledger().set(LedgerInfo { @@ -48,19 +52,26 @@ fn test_simple_transfer() { max_entry_ttl: u32::MAX, }); - test_data.contract_client.mock_auths(&[MockAuth { - address: &first_owner, - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "transfer", - args: (RecordKeys::Record(node.clone()), second_owner.clone()).into_val(&e), - sub_invokes: &[], - }, - }]).transfer(&RecordKeys::Record(node.clone()), &second_owner); + test_data + .contract_client + .mock_auths(&[MockAuth { + address: &first_owner, + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "transfer", + args: (RecordKeys::Record(node.clone()), second_owner.clone()).into_val(&e), + sub_invokes: &[], + }, + }]) + .transfer(&RecordKeys::Record(node.clone()), &second_owner); - let second_record: Domain = match test_data.contract_client.record(&RecordKeys::Record(node.clone())).unwrap() { + let second_record: Domain = match test_data + .contract_client + .record(&RecordKeys::Record(node.clone())) + .unwrap() + { Record::Domain(domain) => domain, - Record::SubDomain(_) => panic!() + Record::SubDomain(_) => panic!(), }; assert_eq!(first_owner, first_record.owner); @@ -69,13 +80,17 @@ fn test_simple_transfer() { assert_eq!(10, second_record.snapshot); // It should fail because first owner is no the owner anymore - assert!(test_data.contract_client.mock_auths(&[MockAuth { - address: &first_owner, - invoke: &MockAuthInvoke { - contract: &test_data.contract_client.address, - fn_name: "transfer", - args: (RecordKeys::Record(node.clone()), second_owner.clone()).into_val(&e), - sub_invokes: &[], - }, - }]).try_transfer(&RecordKeys::Record(node.clone()), &second_owner).is_err()); -} \ No newline at end of file + assert!(test_data + .contract_client + .mock_auths(&[MockAuth { + address: &first_owner, + invoke: &MockAuthInvoke { + contract: &test_data.contract_client.address, + fn_name: "transfer", + args: (RecordKeys::Record(node.clone()), second_owner.clone()).into_val(&e), + sub_invokes: &[], + }, + }]) + .try_transfer(&RecordKeys::Record(node.clone()), &second_owner) + .is_err()); +} From 60c6ea143295a129f9bc935aef9a24365968bee9 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Mon, 4 Mar 2024 23:39:11 +0800 Subject: [PATCH 2/4] Bump soroban-sdk to 20.4.0. --- Cargo.lock | 20 ++++++++++---------- Cargo.toml | 6 ++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75657d9..2e4b12c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1027,9 +1027,9 @@ dependencies = [ [[package]] name = "soroban-ledger-snapshot" -version = "20.3.2" +version = "20.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95a7b822725a73a90ef650bc1f325d13c8bae7a808156c101953092327e2edee" +checksum = "aa5dbe9eb4aa059560bef9c16d3a643f52b8edd38da3b412f1954b38863430d1" dependencies = [ "serde", "serde_json", @@ -1041,9 +1041,9 @@ dependencies = [ [[package]] name = "soroban-sdk" -version = "20.3.2" +version = "20.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdff4b5fc50f554499b81aa6ecbb4045beb84742ecda9777ebbdc90c0d93ec62" +checksum = "c9854eead7ba67903c3a03797308f03ebb2c967986406d414dbb8df1c838a17e" dependencies = [ "arbitrary", "bytes-lit", @@ -1061,9 +1061,9 @@ dependencies = [ [[package]] name = "soroban-sdk-macros" -version = "20.3.2" +version = "20.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12d147c3ce37842919893946a4467632aa012f567a7ab2286abe19e5ecc25e05" +checksum = "f9c95027081c8d45310c3d7e4998e99cd98332f1708f3c3d2ec567efaa966c95" dependencies = [ "crate-git-revision", "darling", @@ -1081,9 +1081,9 @@ dependencies = [ [[package]] name = "soroban-spec" -version = "20.3.2" +version = "20.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b7a132b7c234edf6ef3add4ffb17807f3b25a4ce5ab944ebbaf4d2326470eb1" +checksum = "04b6e554c6118ab0604bf983d0ecb51cb781eb60dee85e8e248a3fe3f3b10b8c" dependencies = [ "base64 0.13.1", "stellar-xdr", @@ -1093,9 +1093,9 @@ dependencies = [ [[package]] name = "soroban-spec-rust" -version = "20.3.2" +version = "20.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8d396f3b29800138e8abf2562aba0b579d09d8c2d2b956379fc9e68914a6e62" +checksum = "c70a942f37850c65349d4336e805eb655ad2e19dec458dcaf8d55dda955cfd6d" dependencies = [ "prettyplease", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 689be65..2429e7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,7 @@ [workspace] resolver = "2" -members = [ - "registry", -] +members = ["registry"] [profile.release-with-logs] inherits = "release" @@ -20,4 +18,4 @@ codegen-units = 1 lto = true [workspace.dependencies.soroban-sdk] -version = "20.3.2" +version = "20.4.0" From 6e3df9bbc2f7bcc6f44a57027591d28e0c0db7b3 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Mon, 4 Mar 2024 23:39:43 +0800 Subject: [PATCH 3/4] Add GitHub Action config for automated testing. --- .github/workflows/rust.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..10e7779 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,20 @@ +name: Rust + +on: + push: + pull_request: + +jobs: + fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: rustup update + - run: cargo fmt --check + + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: rustup update + - run: cargo test From deb1b853b25410f774ea128444d704b1d0da4083 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Mon, 4 Mar 2024 23:40:40 +0800 Subject: [PATCH 4/4] Add .git-blame-ignore-revs to avoid polluting git blame. --- .git-blame-ignore-revs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..cd1b129 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,3 @@ +# .git-blame-ignore-revs +# Format code using `cargo fmt`. +dde86e68796841b98931948431fa300db0a9ef91