Skip to content

Commit 84e5ae6

Browse files
authored
refactor(target_chains/starknet): use felt252 instead of bytes31 in ByteArray (#1663)
1 parent 35c23b4 commit 84e5ae6

File tree

5 files changed

+58
-64
lines changed

5 files changed

+58
-64
lines changed

target_chains/starknet/contracts/src/byte_array.cairo

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct ByteArray {
1010
num_last_bytes: u8,
1111
// Bytes in big endian. Each item except the last one stores 31 bytes.
1212
// If `num_last_bytes < 31`, unused most significant bytes of the last item will be unused.
13-
data: Array<bytes31>,
13+
data: Array<felt252>,
1414
}
1515

1616
impl DebugByteArray of Debug<ByteArray> {
@@ -19,10 +19,7 @@ impl DebugByteArray of Debug<ByteArray> {
1919
let mut data = self.data.clone();
2020
loop {
2121
match data.pop_front() {
22-
Option::Some(v) => {
23-
let v: u256 = v.into();
24-
write!(f, "{:?}, ", v).unwrap();
25-
},
22+
Option::Some(v) => { write!(f, "{:?}, ", v).unwrap(); },
2623
Option::None => { break; },
2724
}
2825
};
@@ -33,7 +30,7 @@ impl DebugByteArray of Debug<ByteArray> {
3330
#[generate_trait]
3431
pub impl ByteArrayImpl of ByteArrayTrait {
3532
/// Creates a byte array with the data.
36-
fn new(data: Array<bytes31>, num_last_bytes: u8) -> ByteArray {
33+
fn new(data: Array<felt252>, num_last_bytes: u8) -> ByteArray {
3734
if data.len() == 0 {
3835
assert!(num_last_bytes == 0);
3936
} else {
@@ -50,7 +47,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
5047

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

8582
#[test]
8683
fn byte_array_3_zeros() {
87-
let mut array = ByteArrayImpl::new(array_try_into(array![0]), 3);
84+
let mut array = ByteArrayImpl::new(array![0], 3);
8885
assert!(array.len() == 3);
89-
assert!(array.pop_front() == Option::Some((0.try_into().unwrap(), 3)));
86+
assert!(array.pop_front() == Option::Some((0, 3)));
9087
assert!(array.len() == 0);
9188
assert!(array.pop_front() == Option::None);
9289
}
9390

9491
#[test]
9592
fn byte_array_3_bytes() {
96-
let mut array = ByteArrayImpl::new(array_try_into(array![0x010203]), 3);
93+
let mut array = ByteArrayImpl::new(array![0x010203], 3);
9794
assert!(array.len() == 3);
98-
assert!(array.pop_front() == Option::Some((0x010203.try_into().unwrap(), 3)));
95+
assert!(array.pop_front() == Option::Some((0x010203, 3)));
9996
assert!(array.len() == 0);
10097
assert!(array.pop_front() == Option::None);
10198
}
10299

103100
#[test]
104101
fn byte_array_single_full() {
105102
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
106-
let mut array = ByteArrayImpl::new(array_try_into(array![value_31_bytes]), 31);
103+
let mut array = ByteArrayImpl::new(array![value_31_bytes], 31);
107104
assert!(array.len() == 31);
108-
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
105+
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
109106
assert!(array.len() == 0);
110107
assert!(array.pop_front() == Option::None);
111108
}
@@ -114,13 +111,11 @@ mod tests {
114111
fn byte_array_two_full() {
115112
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
116113
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
117-
let mut array = ByteArrayImpl::new(
118-
array_try_into(array![value_31_bytes, value2_31_bytes]), 31
119-
);
114+
let mut array = ByteArrayImpl::new(array![value_31_bytes, value2_31_bytes], 31);
120115
assert!(array.len() == 62);
121-
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
116+
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
122117
assert!(array.len() == 31);
123-
assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31)));
118+
assert!(array.pop_front() == Option::Some((value2_31_bytes, 31)));
124119
assert!(array.len() == 0);
125120
assert!(array.pop_front() == Option::None);
126121
}
@@ -131,14 +126,14 @@ mod tests {
131126
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
132127
let value3_5_bytes = 0x4142434445;
133128
let mut array = ByteArrayImpl::new(
134-
array_try_into(array![value_31_bytes, value2_31_bytes, value3_5_bytes]), 5
129+
array![value_31_bytes, value2_31_bytes, value3_5_bytes], 5
135130
);
136131
assert!(array.len() == 67);
137-
assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31)));
132+
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
138133
assert!(array.len() == 36);
139-
assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31)));
134+
assert!(array.pop_front() == Option::Some((value2_31_bytes, 31)));
140135
assert!(array.len() == 5);
141-
assert!(array.pop_front() == Option::Some((value3_5_bytes.try_into().unwrap(), 5)));
136+
assert!(array.pop_front() == Option::Some((value3_5_bytes, 5)));
142137
assert!(array.pop_front() == Option::None);
143138
}
144139

@@ -151,18 +146,18 @@ mod tests {
151146
#[test]
152147
#[should_panic]
153148
fn byte_array_last_too_large() {
154-
ByteArrayImpl::new(array_try_into(array![1, 2, 3]), 35);
149+
ByteArrayImpl::new(array![1, 2, 3], 35);
155150
}
156151

157152
#[test]
158153
#[should_panic]
159154
fn byte_array_last_zero_invalid() {
160-
ByteArrayImpl::new(array_try_into(array![1, 2, 0]), 0);
155+
ByteArrayImpl::new(array![1, 2, 0], 0);
161156
}
162157

163158
#[test]
164159
#[should_panic]
165160
fn byte_array_last_too_many_bytes() {
166-
ByteArrayImpl::new(array_try_into(array![1, 2, 0x010203]), 2);
161+
ByteArrayImpl::new(array![1, 2, 0x010203], 2);
167162
}
168163
}

target_chains/starknet/contracts/src/reader.cairo

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub impl ReaderImpl of ReaderTrait {
101101

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

174174
// Moved out from `read_bytes` because we cannot use `return` or `?` within a loop.
175175
fn read_bytes_iteration(
176-
ref self: Reader, num_bytes: usize, ref array: Array<bytes31>
176+
ref self: Reader, num_bytes: usize, ref array: Array<felt252>
177177
) -> (usize, bool) {
178178
if num_bytes >= 31 {
179179
let high = self.read_num_bytes(15);
180180
let low = self.read_num_bytes(16);
181181
let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW);
182-
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
182+
array.append(value);
183183
(31, false)
184184
} else if num_bytes > 16 {
185185
// num_bytes < 31
186186
let high = self.read_num_bytes((num_bytes - 16).try_into().expect(UNEXPECTED_OVERFLOW));
187187
let low = self.read_num_bytes(16);
188188
let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW);
189-
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
189+
array.append(value);
190190
(num_bytes, true)
191191
} else {
192192
// bytes < 16
193193
let low = self.read_num_bytes(num_bytes.try_into().expect(UNEXPECTED_OVERFLOW));
194194
let value: felt252 = low.try_into().expect(UNEXPECTED_OVERFLOW);
195-
array.append(value.try_into().expect(UNEXPECTED_OVERFLOW));
195+
array.append(value);
196196
(num_bytes, true)
197197
}
198198
}

target_chains/starknet/contracts/tests/data.cairo

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn good_update1() -> ByteArray {
4848
226866843267230707879834616967256711063296411939069440476882347301771901839,
4949
95752383404870925303422787,
5050
];
51-
ByteArrayImpl::new(array_try_into(bytes), 11)
51+
ByteArrayImpl::new(bytes, 11)
5252
}
5353

5454
// A wormhole VAA from a random update pulled from Hermes.
@@ -86,7 +86,7 @@ pub fn good_vm1() -> ByteArray {
8686
52685537088250779930155363779405986390839624071318818148325576008719597568,
8787
14615204155786886573933667335033405822686404253588533,
8888
];
89-
ByteArrayImpl::new(array_try_into(bytes), 22)
89+
ByteArrayImpl::new(bytes, 22)
9090
}
9191

9292
// A first update for a certain timestamp pulled from Hermes.
@@ -134,7 +134,7 @@ pub fn unique_update1() -> ByteArray {
134134
28583007876111384456149499846085318299326698960792831530075402396150538907,
135135
126290914008245563820443505,
136136
];
137-
ByteArrayImpl::new(array_try_into(bytes), 11)
137+
ByteArrayImpl::new(bytes, 11)
138138
}
139139

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

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

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

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

318318
pub const TEST_GUARDIAN_ADDRESS1: felt252 = 0x686b9ea8e3237110eaaba1f1b7467559a3273819;
@@ -328,7 +328,7 @@ pub fn empty_set_upgrade() -> ByteArray {
328328
1131377253,
329329
210141960835432704,
330330
];
331-
ByteArrayImpl::new(array_try_into(bytes), 8)
331+
ByteArrayImpl::new(bytes, 8)
332332
}
333333

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)