From af6ed6dd0541912d5c04d0970191b5dbf092efad Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Sat, 13 Jul 2024 16:48:44 +0100 Subject: [PATCH] fix: crash on reverse resolving post subdomain transfer --- src/naming/main.cairo | 5 +++ src/tests/naming/test_abuses.cairo | 51 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/naming/main.cairo b/src/naming/main.cairo index 8e1e9ed..e1dc5c8 100644 --- a/src/naming/main.cairo +++ b/src/naming/main.cairo @@ -203,6 +203,9 @@ mod Naming { fn domain_to_address( self: @ContractState, domain: Span, hint: Span ) -> ContractAddress { + if domain.len() == 0 { + return ContractAddressZeroable::zero(); + } let (resolver, parent_length) = self.domain_to_resolver(domain); // if there is a resolver starting from the top if (resolver != ContractAddressZeroable::zero()) { @@ -305,9 +308,11 @@ mod Naming { // self.domain_to_address(domain, array![].span()) == address, // 'domain not pointing back' // ); + println!("echo: {:?}", domain); if self.domain_to_address(domain, array![].span()) != address { return array![].span(); } + println!("hey"); domain } } diff --git a/src/tests/naming/test_abuses.cairo b/src/tests/naming/test_abuses.cairo index ef0a32c..3fa0930 100644 --- a/src/tests/naming/test_abuses.cairo +++ b/src/tests/naming/test_abuses.cairo @@ -409,3 +409,54 @@ fn test_buy_empty_domain() { 0 ); } + + +#[test] +#[available_gas(2000000000)] +fn test_subdomain_reverse() { + // setup + let (eth, pricing, identity, naming) = deploy(); + let alpha = contract_address_const::<0x123>(); + let bravo = contract_address_const::<0x456>(); + let charlie = contract_address_const::<0x789>(); + + // we mint the ids + set_contract_address(alpha); + identity.mint(1); + set_contract_address(bravo); + identity.mint(2); + set_contract_address(charlie); + identity.mint(3); + + set_contract_address(alpha); + let aller: felt252 = 35683102; + + // we check how much a domain costs + let (_, price) = pricing.compute_buy_price(5, 365); + + // we allow the naming to take our money + eth.approve(naming.contract_address, price); + + // we buy with no resolver, no sponsor, no discount and empty metadata + naming + .buy(1, aller, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0); + + let subdomain = array![aller, aller].span(); + + // we transfer aller.aller.stark to id2 + naming.transfer_domain(subdomain, 2); + + // and make sure the owner has been updated + assert(naming.domain_to_id(subdomain) == 2, 'owner not updated correctly'); + set_contract_address(bravo); + let result = naming.address_to_domain(bravo, array![].span()); + assert(result == array![].span(), 'unexpected result'); + // we then set this subdomain as main domain and ensures reverse resolving works + identity.set_main_id(2); + let result = naming.address_to_domain(bravo, array![].span()); + assert(result == subdomain, 'unexpected result'); + // before transfering this subdomain + naming.transfer_domain(subdomain, 3); + let result = naming.address_to_domain(bravo, array![].span()); + assert(result == array![].span(), 'unexpected result'); +}