Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup of escrow clearing after withdrawing, added tests to check #246

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/blockchain/starknet/src/bridge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ mod bridge {
if is_escrowed {
IERC721Dispatcher { contract_address: collection_l2 }
.transfer_from(from, to, token_id);

let null_value = starknet::contract_address_const::<0>();
self.escrow.write((collection_l2, token_id), null_value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use scarb fmt for indentation

} else {
if (req.uris.len() != 0) {
let token_uri = req.uris[i];
Expand Down Expand Up @@ -367,6 +370,10 @@ mod bridge {
self.l2_to_l1_addresses.write(collection_l2, collection_l1);
self.emit(L1L2CollectionMappingUpdated { collection_l1, collection_l2 });
}

fn is_token_escrowed(ref self: ContractState, collection: ContractAddress, token_id: u256) -> bool {
!self.escrow.read((collection, token_id)).is_zero()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a ref since there is no state modification

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way I'm not sure this view is really needed since it's only use for testing purpose.
Please see https://foundry-rs.github.io/starknet-foundry/testing/testing-contract-internals.html for a better solution.

}

#[abi(embed_v0)]
Expand Down
1 change: 1 addition & 0 deletions apps/blockchain/starknet/src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ trait IStarklane<T> {
fn is_enabled(self: @T) -> bool;

fn set_l1_l2_collection_mapping(ref self: T , collection_l1: EthAddress, collection_l2: ContractAddress);
fn is_token_escrowed(ref self: T, collection: ContractAddress, token_id: u256) -> bool;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for ref since there is no state modification

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way I'm not sure this view is really needed since it's only use for testing purpose.
Please see https://foundry-rs.github.io/starknet-foundry/testing/testing-contract-internals.html for a better solution.

Copy link
Contributor Author

@od-hunter od-hunter Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ptisserand , i had implemented the view cause the method mentioned in this docs , isnt really working as it should . Something about the mapping being a little more complex to read its value and no clear resource pointing how to go about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ptisserand please reply

}

/// Upgradeable contract.
Expand Down
83 changes: 80 additions & 3 deletions apps/blockchain/starknet/src/tests/bridge_t.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ mod tests {
req.ids = ids;
let mut buf = array![];
req.serialize(ref buf);

//get mapping value before withdrawal

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed

let mut l1_handler = L1Handler {
contract_address: bridge_address,
// selector: 0x03593216f3a8b22f4cf375e5486e3d13bfde9d0f26976d20ac6f653c73f7e507,
Expand All @@ -286,7 +287,6 @@ mod tests {
l1_handler.execute().unwrap();
let bridge = IStarklaneDispatcher { contract_address: bridge_address };

// Deserialize the request and check some expected values.
let mut sp = buf.span();
let req = Serde::<Request>::deserialize(ref sp).unwrap();

Expand All @@ -297,7 +297,8 @@ mod tests {

assert!(erc721.owner_of(0) == OWNER_L2, "Wrong owner after req");
assert!(erc721.owner_of(1) == OWNER_L2, "Wrong owner after req");

//get mapping value after withdrawal , then compare to see if it has been cleared

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed

spy.assert_emitted(@array![
(
bridge_address,
Expand Down Expand Up @@ -1214,4 +1215,80 @@ mod tests {
assert_eq!(bridge.get_l1_collection_address(erc721b_address), collection_l1);
assert_eq!(bridge.get_l2_collection_address(collection_l1), erc721b_address);
}

#[test]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use scarb fmt for indentation

fn test_escrow_clearing_after_withdrawal() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing on your branch

[FAIL] starklane::tests::bridge_t::tests::test_escrow_clearing_after_withdrawal

Failure data:
    0x526573756c743a3a756e77726170206661696c65642e ('Result::unwrap failed.')

// Setup
let erc721b_contract_class = declare("erc721_bridgeable");
let BRIDGE_ADMIN = starknet::contract_address_const::<'starklane'>();
let BRIDGE_L1 = EthAddress { address: 'starklane_l1' };
let COLLECTION_OWNER = starknet::contract_address_const::<'collection owner'>();
let OWNER_L1 = EthAddress { address: 'owner_l1' };
let OWNER_L2 = starknet::contract_address_const::<'owner_l2'>();

// Deploy bridge and ERC721 contract
let bridge_address = deploy_starklane(BRIDGE_ADMIN, BRIDGE_L1, erc721b_contract_class.class_hash);
let erc721b_address = deploy_erc721b(
erc721b_contract_class,
"TestToken",
"TT",
bridge_address,
COLLECTION_OWNER
);

let bridge = IStarklaneDispatcher { contract_address: bridge_address };
let erc721 = IERC721Dispatcher { contract_address: erc721b_address };

// Mint a token and deposit it to the bridge
mint_range(erc721b_address, COLLECTION_OWNER, COLLECTION_OWNER, 0, 1);
start_prank(CheatTarget::One(erc721b_address), COLLECTION_OWNER);
erc721.set_approval_for_all(bridge_address, true);
stop_prank(CheatTarget::One(erc721b_address));

start_prank(CheatTarget::One(bridge_address), COLLECTION_OWNER);
bridge.deposit_tokens(
0x123,
erc721b_address,
OWNER_L1,
array![0].span(),
false,
false
);
stop_prank(CheatTarget::One(bridge_address));

// Verify token is in escrow
assert_eq!(erc721.owner_of(0), bridge_address, "Token should be in escrow");

// Prepare withdrawal request
let collection_l1: EthAddress = 0xe0c.try_into().unwrap();
let mut req = setup_request(
collection_l1,
erc721b_address,
OWNER_L1,
OWNER_L2,
"TestToken",
"TT",
"base_uri"
);
req.ids = array![0].span();
let mut buf = array![];
req.serialize(ref buf);

// Execute withdrawal
let mut l1_handler = L1Handler {
contract_address: bridge_address,
function_selector: selector!("withdraw_auto_from_l1"),
from_address: BRIDGE_L1.into(),
payload: buf.span()
};
l1_handler.execute().unwrap();

// Verify token is transferred to OWNER_L2
assert_eq!(erc721.owner_of(0), OWNER_L2, "Token should be transferred to OWNER_L2");

// Verify escrow is cleared
let is_escrowed = bridge.is_token_escrowed(erc721b_address, 0);
assert!(!is_escrowed, "Escrow should be cleared after withdrawal");
}

}
Loading