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

fix: resolve infinite loop in _white_list_collection #244

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions apps/blockchain/starknet/src/bridge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ mod bridge {
self.white_listed_list.write(prev, (active, target));
break;
}
prev = next;
};
self.white_listed_list.write(collection, (false, no_value));
}
Expand Down
112 changes: 91 additions & 21 deletions apps/blockchain/starknet/src/tests/bridge_t.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -694,81 +694,151 @@ mod tests {
let collection1 = starknet::contract_address_const::<'collection1'>();
let collection2 = starknet::contract_address_const::<'collection2'>();
let collection3 = starknet::contract_address_const::<'collection3'>();

let collection4 = starknet::contract_address_const::<'collection4'>();
let collection5 = starknet::contract_address_const::<'collection5'>();

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection1, true);
bridge.white_list_collection(collection2, true);
bridge.white_list_collection(collection3, true);
bridge.white_list_collection(collection4, true);
bridge.white_list_collection(collection5, true);
stop_prank(CheatTarget::One(bridge_address));

let white_listed = bridge.get_white_listed_collections();
assert_eq!(white_listed.len(), 3, "White list shall contain 3 elements");
assert_eq!(white_listed.len(), 5, "White list shall contain 5 elements");
assert_eq!(*white_listed.at(0), collection1, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection2, "Wrong collection address in white list");
assert_eq!(*white_listed.at(2), collection3, "Wrong collection address in white list");
assert_eq!(*white_listed.at(3), collection4, "Wrong collection address in white list");
assert_eq!(*white_listed.at(4), collection5, "Wrong collection address in white list");
assert!(bridge.is_white_listed(collection1), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection2), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection3), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection4), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection5), "Collection1 should be whitelisted");

let mut spy = spy_events(SpyOn::One(bridge_address));

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection2, false);
bridge.white_list_collection(collection3, false);
stop_prank(CheatTarget::One(bridge_address));
let white_listed = bridge.get_white_listed_collections();
assert_eq!(white_listed.len(), 2, "White list shall contain 2 elements");
assert_eq!(white_listed.len(), 4, "White list shall contain 4 elements");
assert_eq!(*white_listed.at(0), collection1, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection3, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection2, "Wrong collection address in white list");
assert_eq!(*white_listed.at(2), collection4, "Wrong collection address in white list");
assert_eq!(*white_listed.at(3), collection5, "Wrong collection address in white list");
assert!(bridge.is_white_listed(collection1), "Collection1 should be whitelisted");
assert!(!bridge.is_white_listed(collection2), "Collection1 should not be whitelisted");
assert!(bridge.is_white_listed(collection3), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection2), "Collection1 should be whitelisted");
assert!(!bridge.is_white_listed(collection3), "Collection1 should not be whitelisted");
assert!(bridge.is_white_listed(collection4), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection5), "Collection1 should be whitelisted");

spy.assert_emitted(@array![
(
bridge_address,
bridge::Event::CollectionWhiteListUpdated(
bridge::CollectionWhiteListUpdated {
collection: collection3,
enabled: false,
}
)
)
]);

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection1, false);
bridge.white_list_collection(collection3, false);
bridge.white_list_collection(collection4, false);
stop_prank(CheatTarget::One(bridge_address));

let white_listed = bridge.get_white_listed_collections();
assert!(white_listed.is_empty(), "White list shall be empty");
assert_eq!(white_listed.len(), 2, "White list shall contain 2 elements");
JoE11-y marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(*white_listed.at(0), collection2, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection5, "Wrong collection address in white list");
assert!(!bridge.is_white_listed(collection1), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection2), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection3), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection4), "Collection1 should not be whitelisted");

spy.assert_emitted(@array![
(
bridge_address,
bridge::Event::CollectionWhiteListUpdated(
bridge::CollectionWhiteListUpdated {
collection: collection1,
enabled: false,
}
)
),
(
bridge_address,
bridge::Event::CollectionWhiteListUpdated(
bridge::CollectionWhiteListUpdated {
collection: collection4,
enabled: false,
}
)
)
]);

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection1, true);
bridge.white_list_collection(collection3, true);
bridge.white_list_collection(collection2, false);
stop_prank(CheatTarget::One(bridge_address));

let white_listed = bridge.get_white_listed_collections();
assert_eq!(white_listed.len(), 2, "White list shall contain 2 elements");
assert_eq!(*white_listed.at(0), collection1, "Wrong collection address in white list");
assert_eq!(*white_listed.at(0), collection5, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection3, "Wrong collection address in white list");
assert!(bridge.is_white_listed(collection1), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection5), "Collection1 should be whitelisted");
assert!(!bridge.is_white_listed(collection2), "Collection1 should not be whitelisted");
assert!(bridge.is_white_listed(collection3), "Collection1 should be whitelisted");

spy.assert_emitted(@array![
(
bridge_address,
bridge::Event::CollectionWhiteListUpdated(
bridge::CollectionWhiteListUpdated {
collection: collection3,
enabled: true,
}
)
),
(
bridge_address,
bridge::Event::CollectionWhiteListUpdated(
bridge::CollectionWhiteListUpdated {
collection: collection2,
enabled: false,
}
)
)
]);

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection1, false);
bridge.white_list_collection(collection3, false);
bridge.white_list_collection(collection2, true);
stop_prank(CheatTarget::One(bridge_address));

let white_listed = bridge.get_white_listed_collections();
assert_eq!(white_listed.len(), 2, "White list shall contain 2 elements");
assert_eq!(*white_listed.at(0), collection3, "Wrong collection address in white list");
assert_eq!(*white_listed.at(0), collection5, "Wrong collection address in white list");
assert_eq!(*white_listed.at(1), collection2, "Wrong collection address in white list");
assert!(!bridge.is_white_listed(collection1), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection3), "Collection1 should not be whitelisted");
assert!(bridge.is_white_listed(collection2), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection3), "Collection1 should be whitelisted");
assert!(bridge.is_white_listed(collection5), "Collection1 should be whitelisted");

start_prank(CheatTarget::One(bridge_address), BRIDGE_ADMIN);
bridge.white_list_collection(collection5, false);
bridge.white_list_collection(collection2, false);
bridge.white_list_collection(collection3, false);
bridge.white_list_collection(collection1, false);
bridge.white_list_collection(collection1, false);
stop_prank(CheatTarget::One(bridge_address));

let white_listed = bridge.get_white_listed_collections();
assert!(white_listed.is_empty(), "White list shall be empty");
assert!(!bridge.is_white_listed(collection1), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection2), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection3), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection4), "Collection1 should not be whitelisted");
assert!(!bridge.is_white_listed(collection5), "Collection1 should not be whitelisted");
}

#[test]
Expand Down
Loading