-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix SignersInfo packing issue (#1301)
* Address SignersInfo packing issue * Update changelog * Add test for unpacking zero value * Move packing fuzz tests to a separate module * Move changelog entry
- Loading branch information
Showing
7 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use core::integer::u128_safe_divmod; | ||
use core::num::traits::Bounded; | ||
use crate::multisig::storage_utils::{SignersInfo, SignersInfoStorePackingV2}; | ||
use starknet::storage_access::StorePacking; | ||
|
||
#[test] | ||
fn test_signers_info_pack_unpack_v2(quorum: u32, signers_count: u32) { | ||
let info = SignersInfo { quorum, signers_count }; | ||
let packed_value = SignersInfoStorePackingV2::pack(info); | ||
let unpacked_info = SignersInfoStorePackingV2::unpack(packed_value); | ||
|
||
assert_eq!(unpacked_info.quorum, quorum); | ||
assert_eq!(unpacked_info.signers_count, signers_count); | ||
} | ||
|
||
#[test] | ||
fn test_signers_info_pack_with_v1_unpack_with_v2(quorum: u32, signers_count: u32) { | ||
if signers_count == Bounded::MAX { | ||
// Cannot properly unpack if packed with V1 and `signers_count` is max u32 value | ||
return; | ||
}; | ||
let info = SignersInfo { quorum, signers_count }; | ||
let packed_value = LegacySignersInfoStorePackingV1::pack(info); | ||
let unpacked_info = SignersInfoStorePackingV2::unpack(packed_value); | ||
|
||
assert_eq!(unpacked_info.quorum, quorum); | ||
assert_eq!(unpacked_info.signers_count, signers_count); | ||
} | ||
|
||
// | ||
// Helpers | ||
// | ||
|
||
const MAX_U32: NonZero<u128> = 0xffffffff; | ||
|
||
impl LegacySignersInfoStorePackingV1 of StorePacking<SignersInfo, u128> { | ||
fn pack(value: SignersInfo) -> u128 { | ||
let SignersInfo { quorum, signers_count } = value; | ||
quorum.into() * MAX_U32.into() + signers_count.into() | ||
} | ||
|
||
fn unpack(value: u128) -> SignersInfo { | ||
let (quorum, signers_count) = u128_safe_divmod(value, MAX_U32); | ||
SignersInfo { | ||
quorum: quorum.try_into().unwrap(), signers_count: signers_count.try_into().unwrap(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters