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

refactor(target_chains/starknet): use felt252 instead of bytes31 in ByteArray #1663

Merged
merged 1 commit into from
Jun 6, 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
45 changes: 20 additions & 25 deletions target_chains/starknet/contracts/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct ByteArray {
num_last_bytes: u8,
// Bytes in big endian. Each item except the last one stores 31 bytes.
// If `num_last_bytes < 31`, unused most significant bytes of the last item will be unused.
data: Array<bytes31>,
data: Array<felt252>,
}

impl DebugByteArray of Debug<ByteArray> {
Expand All @@ -19,10 +19,7 @@ impl DebugByteArray of Debug<ByteArray> {
let mut data = self.data.clone();
loop {
match data.pop_front() {
Option::Some(v) => {
let v: u256 = v.into();
write!(f, "{:?}, ", v).unwrap();
},
Option::Some(v) => { write!(f, "{:?}, ", v).unwrap(); },
Option::None => { break; },
}
};
Expand All @@ -33,7 +30,7 @@ impl DebugByteArray of Debug<ByteArray> {
#[generate_trait]
pub impl ByteArrayImpl of ByteArrayTrait {
/// Creates a byte array with the data.
fn new(data: Array<bytes31>, num_last_bytes: u8) -> ByteArray {
fn new(data: Array<felt252>, num_last_bytes: u8) -> ByteArray {
if data.len() == 0 {
assert!(num_last_bytes == 0);
} else {
Expand All @@ -50,7 +47,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {

/// Removes 31 or less bytes from the start of the array.
/// Returns the value and the number of bytes.
fn pop_front(ref self: ByteArray) -> Option<(bytes31, u8)> {
fn pop_front(ref self: ByteArray) -> Option<(felt252, u8)> {
let item = self.data.pop_front()?;
if self.data.is_empty() {
let num_bytes = self.num_last_bytes;
Expand Down Expand Up @@ -84,28 +81,28 @@ mod tests {

#[test]
fn byte_array_3_zeros() {
let mut array = ByteArrayImpl::new(array_try_into(array![0]), 3);
let mut array = ByteArrayImpl::new(array![0], 3);
assert!(array.len() == 3);
assert!(array.pop_front() == Option::Some((0.try_into().unwrap(), 3)));
assert!(array.pop_front() == Option::Some((0, 3)));
assert!(array.len() == 0);
assert!(array.pop_front() == Option::None);
}

#[test]
fn byte_array_3_bytes() {
let mut array = ByteArrayImpl::new(array_try_into(array![0x010203]), 3);
let mut array = ByteArrayImpl::new(array![0x010203], 3);
assert!(array.len() == 3);
assert!(array.pop_front() == Option::Some((0x010203.try_into().unwrap(), 3)));
assert!(array.pop_front() == Option::Some((0x010203, 3)));
assert!(array.len() == 0);
assert!(array.pop_front() == Option::None);
}

#[test]
fn byte_array_single_full() {
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
let mut array = ByteArrayImpl::new(array_try_into(array![value_31_bytes]), 31);
let mut array = ByteArrayImpl::new(array![value_31_bytes], 31);
assert!(array.len() == 31);
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
assert!(array.len() == 0);
assert!(array.pop_front() == Option::None);
}
Expand All @@ -114,13 +111,11 @@ mod tests {
fn byte_array_two_full() {
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
let mut array = ByteArrayImpl::new(
array_try_into(array![value_31_bytes, value2_31_bytes]), 31
);
let mut array = ByteArrayImpl::new(array![value_31_bytes, value2_31_bytes], 31);
assert!(array.len() == 62);
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
assert!(array.len() == 31);
assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31)));
assert!(array.pop_front() == Option::Some((value2_31_bytes, 31)));
assert!(array.len() == 0);
assert!(array.pop_front() == Option::None);
}
Expand All @@ -131,14 +126,14 @@ mod tests {
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
let value3_5_bytes = 0x4142434445;
let mut array = ByteArrayImpl::new(
array_try_into(array![value_31_bytes, value2_31_bytes, value3_5_bytes]), 5
array![value_31_bytes, value2_31_bytes, value3_5_bytes], 5
);
assert!(array.len() == 67);
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
assert!(array.len() == 36);
assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31)));
assert!(array.pop_front() == Option::Some((value2_31_bytes, 31)));
assert!(array.len() == 5);
assert!(array.pop_front() == Option::Some((value3_5_bytes.try_into().unwrap(), 5)));
assert!(array.pop_front() == Option::Some((value3_5_bytes, 5)));
assert!(array.pop_front() == Option::None);
}

Expand All @@ -151,18 +146,18 @@ mod tests {
#[test]
#[should_panic]
fn byte_array_last_too_large() {
ByteArrayImpl::new(array_try_into(array![1, 2, 3]), 35);
ByteArrayImpl::new(array![1, 2, 3], 35);
}

#[test]
#[should_panic]
fn byte_array_last_zero_invalid() {
ByteArrayImpl::new(array_try_into(array![1, 2, 0]), 0);
ByteArrayImpl::new(array![1, 2, 0], 0);
}

#[test]
#[should_panic]
fn byte_array_last_too_many_bytes() {
ByteArrayImpl::new(array_try_into(array![1, 2, 0x010203]), 2);
ByteArrayImpl::new(array![1, 2, 0x010203], 2);
}
}
10 changes: 5 additions & 5 deletions target_chains/starknet/contracts/src/reader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub impl ReaderImpl of ReaderTrait {

/// Reads the specified number of bytes as a new byte array.
fn read_byte_array(ref self: Reader, num_bytes: usize) -> ByteArray {
let mut array: Array<bytes31> = array![];
let mut array: Array<felt252> = array![];
let mut num_last_bytes = 0;
let mut num_remaining_bytes = num_bytes;
loop {
Expand Down Expand Up @@ -173,26 +173,26 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {

// Moved out from `read_bytes` because we cannot use `return` or `?` within a loop.
fn read_bytes_iteration(
ref self: Reader, num_bytes: usize, ref array: Array<bytes31>
ref self: Reader, num_bytes: usize, ref array: Array<felt252>
) -> (usize, bool) {
if num_bytes >= 31 {
let high = self.read_num_bytes(15);
let low = self.read_num_bytes(16);
let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW);
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
array.append(value);
(31, false)
} else if num_bytes > 16 {
// num_bytes < 31
let high = self.read_num_bytes((num_bytes - 16).try_into().expect(UNEXPECTED_OVERFLOW));
let low = self.read_num_bytes(16);
let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW);
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
array.append(value);
(num_bytes, true)
} else {
// bytes < 16
let low = self.read_num_bytes(num_bytes.try_into().expect(UNEXPECTED_OVERFLOW));
let value: felt252 = low.try_into().expect(UNEXPECTED_OVERFLOW);
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
array.append(value);
(num_bytes, true)
}
}
Expand Down
50 changes: 25 additions & 25 deletions target_chains/starknet/contracts/tests/data.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn good_update1() -> ByteArray {
226866843267230707879834616967256711063296411939069440476882347301771901839,
95752383404870925303422787,
];
ByteArrayImpl::new(array_try_into(bytes), 11)
ByteArrayImpl::new(bytes, 11)
}

// A wormhole VAA from a random update pulled from Hermes.
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn good_vm1() -> ByteArray {
52685537088250779930155363779405986390839624071318818148325576008719597568,
14615204155786886573933667335033405822686404253588533,
];
ByteArrayImpl::new(array_try_into(bytes), 22)
ByteArrayImpl::new(bytes, 22)
}

// A first update for a certain timestamp pulled from Hermes.
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn unique_update1() -> ByteArray {
28583007876111384456149499846085318299326698960792831530075402396150538907,
126290914008245563820443505,
];
ByteArrayImpl::new(array_try_into(bytes), 11)
ByteArrayImpl::new(bytes, 11)
}

// An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37
Expand All @@ -159,7 +159,7 @@ pub fn mainnet_guardian_set_upgrade1() -> ByteArray {
55852237138651071644815135002358067220635692701051811455610533875912981641,
190413173566657072516608762222993749133,
];
ByteArrayImpl::new(array_try_into(bytes), 16)
ByteArrayImpl::new(bytes, 16)
}

// An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn mainnet_guardian_set_upgrade2() -> ByteArray {
75218391584551901010047495874303520775865073092730040058902770251005073864,
13453,
];
ByteArrayImpl::new(array_try_into(bytes), 2)
ByteArrayImpl::new(bytes, 2)
}

// An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn mainnet_guardian_set_upgrade3() -> ByteArray {
75218391584551901010047495874303520775865073092730040058902770251005073864,
13453,
];
ByteArrayImpl::new(array_try_into(bytes), 2)
ByteArrayImpl::new(bytes, 2)
}

// An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37
Expand Down Expand Up @@ -312,7 +312,7 @@ pub fn mainnet_guardian_set_upgrade4() -> ByteArray {
75218391584551901010047495874303520775865073092730040058902770251005073864,
13453,
];
ByteArrayImpl::new(array_try_into(bytes), 2)
ByteArrayImpl::new(bytes, 2)
}

pub const TEST_GUARDIAN_ADDRESS1: felt252 = 0x686b9ea8e3237110eaaba1f1b7467559a3273819;
Expand All @@ -328,7 +328,7 @@ pub fn empty_set_upgrade() -> ByteArray {
1131377253,
210141960835432704,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// A wormhole guardian set upgrade instruction with emitter not expected by the test.
Expand All @@ -341,7 +341,7 @@ pub fn wrong_emitter_upgrade() -> ByteArray {
1131377253,
307122819832911374634462256129025725147663742791077927773782095897,
];
ByteArrayImpl::new(array_try_into(bytes), 28)
ByteArrayImpl::new(bytes, 28)
}

// A wormhole guardian set upgrade instruction with set index = 3 not expected by the test.
Expand All @@ -354,7 +354,7 @@ pub fn wrong_index_upgrade() -> ByteArray {
1131377253,
210624583337115497886730203944140689990237281548333499058561169433,
];
ByteArrayImpl::new(array_try_into(bytes), 28)
ByteArrayImpl::new(bytes, 28)
}

// A wormhole governance guardian set upgrade instruction signed by test guardian #1 containing test guardian #2 as the new guardian set.
Expand All @@ -367,7 +367,7 @@ pub fn upgrade_to_test2() -> ByteArray {
1131377253,
210624583337114749311237613435643962969294824395451022190048752713,
];
ByteArrayImpl::new(array_try_into(bytes), 28)
ByteArrayImpl::new(bytes, 28)
}

// A Pyth governance instruction to set fee signed by the test guardian #1.
Expand All @@ -379,7 +379,7 @@ pub fn pyth_set_fee() -> ByteArray {
49565958604199796163020368,
8072278384728444780182694421117884443886221966887092226,
];
ByteArrayImpl::new(array_try_into(bytes), 23)
ByteArrayImpl::new(bytes, 23)
}

// A Pyth governance instruction to set data sources signed by the test guardian #1.
Expand All @@ -393,7 +393,7 @@ pub fn pyth_set_data_sources() -> ByteArray {
223938022913800988696085410923418445187967252047785407181969631814277398528,
301,
];
ByteArrayImpl::new(array_try_into(bytes), 14)
ByteArrayImpl::new(bytes, 14)
}

// A Pyth governance instruction to set wormhole address signed by the test guardian #1.
Expand All @@ -406,7 +406,7 @@ pub fn pyth_set_wormhole() -> ByteArray {
148907253456057279176930315687485033494639386197985334929728922792833758561,
3789456330195130818,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// A Pyth governance instruction to request governance data source transfer signed by the test guardian #1.
Expand All @@ -418,7 +418,7 @@ pub fn pyth_request_transfer() -> ByteArray {
51983810243429054512432720,
101886477340929157123538945,
];
ByteArrayImpl::new(array_try_into(bytes), 11)
ByteArrayImpl::new(bytes, 11)
}

// A Pyth governance instruction to authorize governance data source transfer signed by the test guardian #1.
Expand All @@ -434,7 +434,7 @@ pub fn pyth_auth_transfer() -> ByteArray {
721420288,
20782639266000304984163621011457,
];
ByteArrayImpl::new(array_try_into(bytes), 18)
ByteArrayImpl::new(bytes, 18)
}

// A Pyth governance instruction to set fee with alternative emitter signed by the test guardian #1.
Expand All @@ -446,7 +446,7 @@ pub fn pyth_set_fee_alt_emitter() -> ByteArray {
51983810243429054512498256,
8072278384728444780182694421117884443886221966887092226,
];
ByteArrayImpl::new(array_try_into(bytes), 23)
ByteArrayImpl::new(bytes, 23)
}

// A Pyth governance instruction to upgrade the contract signed by the test guardian #1.
Expand All @@ -459,7 +459,7 @@ pub fn pyth_upgrade_fake1() -> ByteArray {
148907253453589022320407306335457538262203456299261498528172020674942501293,
9624434269354675143,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// A Pyth governance instruction to upgrade the contract signed by the test guardian #1.
Expand All @@ -472,7 +472,7 @@ pub fn pyth_upgrade_not_pyth() -> ByteArray {
148907253453589022305803196061110108233921773465491227564264876752079119569,
6736708290019375278,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// A Pyth governance instruction to upgrade the contract signed by the test guardian #1.
Expand All @@ -485,7 +485,7 @@ pub fn pyth_upgrade_wrong_magic() -> ByteArray {
148907253453589022340563264373887392414227070562033595690783947835630084766,
5698494087895763928,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// A Pyth governance instruction to upgrade the contract signed by the test guardian #1.
Expand All @@ -498,7 +498,7 @@ pub fn pyth_upgrade_invalid_hash() -> ByteArray {
148907253453589022218037939353255655322518022029545083499057126097303896064,
505,
];
ByteArrayImpl::new(array_try_into(bytes), 8)
ByteArrayImpl::new(bytes, 8)
}

// An update pulled from Hermes and re-signed by the test guardian #1.
Expand All @@ -520,7 +520,7 @@ pub fn test_price_update1() -> ByteArray {
87135893730137265929093180553063146337041045646221968026289709394440932141,
245333243912241114598596888050489286502591033459250287888834,
];
ByteArrayImpl::new(array_try_into(bytes), 25)
ByteArrayImpl::new(bytes, 25)
}

// An update pulled from Hermes and re-signed by the test guardian #1.
Expand All @@ -542,7 +542,7 @@ pub fn test_price_update2() -> ByteArray {
370855179649505412564259994413632062925303311800103998016489412083011059699,
1182295126766215829784496273374889928477877265080355104888778,
];
ByteArrayImpl::new(array_try_into(bytes), 25)
ByteArrayImpl::new(bytes, 25)
}

// An update pulled from Hermes and re-signed by the test guardian #1 with another emitter address.
Expand All @@ -564,7 +564,7 @@ pub fn test_update2_alt_emitter() -> ByteArray {
370855179649505412564259994413632062925303311800103998016489412083011059699,
1182295126766215829784496273374889928477877265080355104888778,
];
ByteArrayImpl::new(array_try_into(bytes), 25)
ByteArrayImpl::new(bytes, 25)
}

// An update pulled from Hermes and re-signed by the test guardian #2.
Expand All @@ -586,5 +586,5 @@ pub fn test_update2_set2() -> ByteArray {
370855179649505412564259994413632062925303311800103998016489412083011059699,
1182295126766215829784496273374889928477877265080355104888778,
];
ByteArrayImpl::new(array_try_into(bytes), 25)
ByteArrayImpl::new(bytes, 25)
}
Loading
Loading