Skip to content

Commit 83c1e43

Browse files
authored
refactor(target_chains/starknet): rename ByteArray to ByteBuffer (#1665)
1 parent 94191a5 commit 83c1e43

File tree

17 files changed

+135
-131
lines changed

17 files changed

+135
-131
lines changed

target_chains/starknet/contracts/src/byte_array.cairo renamed to target_chains/starknet/contracts/src/byte_buffer.cairo

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use super::util::one_shift_left_bytes_u256;
55
/// A byte array with storage format similar to `core::ByteArray`, but
66
/// suitable for reading data from it.
77
#[derive(Drop, Clone, Serde)]
8-
pub struct ByteArray {
8+
pub struct ByteBuffer {
99
// Number of bytes stored in the last item of `self.data` (or 0 if it's empty).
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.
1313
data: Array<felt252>,
1414
}
1515

16-
impl DebugByteArray of Debug<ByteArray> {
17-
fn fmt(self: @ByteArray, ref f: Formatter) -> Result<(), core::fmt::Error> {
18-
write!(f, "ByteArray {{ num_last_bytes: {}, data: [", self.num_last_bytes)?;
16+
impl DebugByteBuffer of Debug<ByteBuffer> {
17+
fn fmt(self: @ByteBuffer, ref f: Formatter) -> Result<(), core::fmt::Error> {
18+
write!(f, "ByteBuffer {{ num_last_bytes: {}, data: [", self.num_last_bytes)?;
1919
let mut data = self.data.clone();
2020
loop {
2121
match data.pop_front() {
@@ -28,9 +28,9 @@ impl DebugByteArray of Debug<ByteArray> {
2828
}
2929

3030
#[generate_trait]
31-
pub impl ByteArrayImpl of ByteArrayTrait {
31+
pub impl ByteBufferImpl of ByteBufferTrait {
3232
/// Creates a byte array with the data.
33-
fn new(data: Array<felt252>, num_last_bytes: u8) -> ByteArray {
33+
fn new(data: Array<felt252>, num_last_bytes: u8) -> ByteBuffer {
3434
if data.len() == 0 {
3535
assert!(num_last_bytes == 0);
3636
} else {
@@ -39,15 +39,15 @@ pub impl ByteArrayImpl of ByteArrayTrait {
3939
let last: u256 = (*data.at(data.len() - 1)).into();
4040
assert!(
4141
last / one_shift_left_bytes_u256(num_last_bytes) == 0,
42-
"ByteArrayImpl::new: last value is too large"
42+
"ByteBufferImpl::new: last value is too large"
4343
);
4444
}
45-
ByteArray { num_last_bytes, data }
45+
ByteBuffer { num_last_bytes, data }
4646
}
4747

4848
/// Removes 31 or less bytes from the start of the array.
4949
/// Returns the value and the number of bytes.
50-
fn pop_front(ref self: ByteArray) -> Option<(felt252, u8)> {
50+
fn pop_front(ref self: ByteBuffer) -> Option<(felt252, u8)> {
5151
let item = self.data.pop_front()?;
5252
if self.data.is_empty() {
5353
let num_bytes = self.num_last_bytes;
@@ -58,7 +58,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
5858
}
5959
}
6060

61-
fn len(self: @ByteArray) -> usize {
61+
fn len(self: @ByteBuffer) -> usize {
6262
if self.data.is_empty() {
6363
0
6464
} else {
@@ -69,19 +69,19 @@ pub impl ByteArrayImpl of ByteArrayTrait {
6969

7070
#[cfg(test)]
7171
mod tests {
72-
use super::{ByteArray, ByteArrayImpl};
72+
use super::{ByteBuffer, ByteBufferImpl};
7373
use pyth::util::array_try_into;
7474

7575
#[test]
7676
fn empty_byte_array() {
77-
let mut array = ByteArrayImpl::new(array![], 0);
77+
let mut array = ByteBufferImpl::new(array![], 0);
7878
assert!(array.len() == 0);
7979
assert!(array.pop_front() == Option::None);
8080
}
8181

8282
#[test]
8383
fn byte_array_3_zeros() {
84-
let mut array = ByteArrayImpl::new(array![0], 3);
84+
let mut array = ByteBufferImpl::new(array![0], 3);
8585
assert!(array.len() == 3);
8686
assert!(array.pop_front() == Option::Some((0, 3)));
8787
assert!(array.len() == 0);
@@ -90,7 +90,7 @@ mod tests {
9090

9191
#[test]
9292
fn byte_array_3_bytes() {
93-
let mut array = ByteArrayImpl::new(array![0x010203], 3);
93+
let mut array = ByteBufferImpl::new(array![0x010203], 3);
9494
assert!(array.len() == 3);
9595
assert!(array.pop_front() == Option::Some((0x010203, 3)));
9696
assert!(array.len() == 0);
@@ -100,7 +100,7 @@ mod tests {
100100
#[test]
101101
fn byte_array_single_full() {
102102
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
103-
let mut array = ByteArrayImpl::new(array![value_31_bytes], 31);
103+
let mut array = ByteBufferImpl::new(array![value_31_bytes], 31);
104104
assert!(array.len() == 31);
105105
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
106106
assert!(array.len() == 0);
@@ -111,7 +111,7 @@ mod tests {
111111
fn byte_array_two_full() {
112112
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
113113
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
114-
let mut array = ByteArrayImpl::new(array![value_31_bytes, value2_31_bytes], 31);
114+
let mut array = ByteBufferImpl::new(array![value_31_bytes, value2_31_bytes], 31);
115115
assert!(array.len() == 62);
116116
assert!(array.pop_front() == Option::Some((value_31_bytes, 31)));
117117
assert!(array.len() == 31);
@@ -125,7 +125,7 @@ mod tests {
125125
let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
126126
let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f;
127127
let value3_5_bytes = 0x4142434445;
128-
let mut array = ByteArrayImpl::new(
128+
let mut array = ByteBufferImpl::new(
129129
array![value_31_bytes, value2_31_bytes, value3_5_bytes], 5
130130
);
131131
assert!(array.len() == 67);
@@ -140,24 +140,24 @@ mod tests {
140140
#[test]
141141
#[should_panic]
142142
fn byte_array_empty_invalid() {
143-
ByteArrayImpl::new(array![], 5);
143+
ByteBufferImpl::new(array![], 5);
144144
}
145145

146146
#[test]
147147
#[should_panic]
148148
fn byte_array_last_too_large() {
149-
ByteArrayImpl::new(array![1, 2, 3], 35);
149+
ByteBufferImpl::new(array![1, 2, 3], 35);
150150
}
151151

152152
#[test]
153153
#[should_panic]
154154
fn byte_array_last_zero_invalid() {
155-
ByteArrayImpl::new(array![1, 2, 0], 0);
155+
ByteBufferImpl::new(array![1, 2, 0], 0);
156156
}
157157

158158
#[test]
159159
#[should_panic]
160160
fn byte_array_last_too_many_bytes() {
161-
ByteArrayImpl::new(array![1, 2, 0x010203], 2);
161+
ByteBufferImpl::new(array![1, 2, 0x010203], 2);
162162
}
163163
}

target_chains/starknet/contracts/src/lib.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
pub mod pyth;
22
pub mod wormhole;
3-
pub mod byte_array;
3+
pub mod byte_buffer;
44
pub mod reader;
55
pub mod hash;
66
pub mod util;
77
pub mod merkle_tree;
88

9-
pub use byte_array::{ByteArray, ByteArrayTrait};
9+
pub use byte_buffer::{ByteBuffer, ByteBufferTrait};
1010
pub use pyth::{
1111
Event, PriceFeedUpdated, WormholeAddressSet, GovernanceDataSourceSet, ContractUpgraded,
1212
DataSourcesSet, FeeSet, GetPriceUnsafeError, GovernanceActionError, UpdatePriceFeedsError,

target_chains/starknet/contracts/src/merkle_tree.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::hash::{Hasher, HasherImpl};
22
use super::reader::{Reader, ReaderImpl};
3-
use super::byte_array::ByteArray;
3+
use super::byte_buffer::ByteBuffer;
44
use super::util::ONE_SHIFT_96;
55
use core::cmp::{min, max};
66
use core::panic_with_felt252;
@@ -49,7 +49,7 @@ fn node_hash(a: u256, b: u256) -> u256 {
4949
hasher.finalize() / ONE_SHIFT_96
5050
}
5151

52-
pub fn read_and_verify_proof(root_digest: u256, message: @ByteArray, ref reader: Reader) {
52+
pub fn read_and_verify_proof(root_digest: u256, message: @ByteBuffer, ref reader: Reader) {
5353
let mut message_reader = ReaderImpl::new(message.clone());
5454
let mut current_hash = leaf_hash(message_reader.clone());
5555

target_chains/starknet/contracts/src/pyth.cairo

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod pyth {
2626
parse_wormhole_proof
2727
};
2828
use pyth::reader::{Reader, ReaderImpl};
29-
use pyth::byte_array::{ByteArray, ByteArrayImpl};
29+
use pyth::byte_buffer::{ByteBuffer, ByteBufferImpl};
3030
use core::panic_with_felt252;
3131
use core::starknet::{
3232
ContractAddress, get_caller_address, get_execution_info, ClassHash, SyscallResultTrait,
@@ -260,11 +260,11 @@ mod pyth {
260260
info.publish_time
261261
}
262262

263-
fn update_price_feeds(ref self: ContractState, data: ByteArray) {
263+
fn update_price_feeds(ref self: ContractState, data: ByteBuffer) {
264264
self.update_price_feeds_internal(data, array![], 0, 0, false);
265265
}
266266

267-
fn get_update_fee(self: @ContractState, data: ByteArray, token: ContractAddress) -> u256 {
267+
fn get_update_fee(self: @ContractState, data: ByteBuffer, token: ContractAddress) -> u256 {
268268
let single_update_fee = if token == self.fee_token_address1.read() {
269269
self.single_update_fee1.read()
270270
} else if token == self.fee_token_address2.read() {
@@ -283,7 +283,7 @@ mod pyth {
283283

284284
fn update_price_feeds_if_necessary(
285285
ref self: ContractState,
286-
update: ByteArray,
286+
update: ByteBuffer,
287287
required_publish_times: Array<PriceFeedPublishTime>
288288
) {
289289
let mut i = 0;
@@ -305,7 +305,7 @@ mod pyth {
305305

306306
fn parse_price_feed_updates(
307307
ref self: ContractState,
308-
data: ByteArray,
308+
data: ByteBuffer,
309309
price_ids: Array<u256>,
310310
min_publish_time: u64,
311311
max_publish_time: u64
@@ -318,7 +318,7 @@ mod pyth {
318318

319319
fn parse_unique_price_feed_updates(
320320
ref self: ContractState,
321-
data: ByteArray,
321+
data: ByteBuffer,
322322
price_ids: Array<u256>,
323323
publish_time: u64,
324324
max_staleness: u64,
@@ -383,7 +383,7 @@ mod pyth {
383383
wormhole.chain_id()
384384
}
385385

386-
fn execute_governance_instruction(ref self: ContractState, data: ByteArray) {
386+
fn execute_governance_instruction(ref self: ContractState, data: ByteBuffer) {
387387
let wormhole = IWormholeDispatcher { contract_address: self.wormhole_address.read() };
388388
let vm = wormhole.parse_and_verify_vm(data.clone());
389389
self.verify_governance_vm(@vm);
@@ -510,7 +510,7 @@ mod pyth {
510510
}
511511

512512
fn check_new_wormhole(
513-
ref self: ContractState, wormhole_address: ContractAddress, vm: ByteArray
513+
ref self: ContractState, wormhole_address: ContractAddress, vm: ByteBuffer
514514
) {
515515
let wormhole = IWormholeDispatcher { contract_address: wormhole_address };
516516
let vm = wormhole.parse_and_verify_vm(vm);
@@ -545,7 +545,7 @@ mod pyth {
545545
}
546546
}
547547

548-
fn authorize_governance_transfer(ref self: ContractState, claim_vaa: ByteArray) {
548+
fn authorize_governance_transfer(ref self: ContractState, claim_vaa: ByteBuffer) {
549549
let wormhole = IWormholeDispatcher { contract_address: self.wormhole_address.read() };
550550
let claim_vm = wormhole.parse_and_verify_vm(claim_vaa.clone());
551551
// Note: no verify_governance_vm() because claim_vaa is signed by the new data source
@@ -604,7 +604,7 @@ mod pyth {
604604
// for any of the specified feeds.
605605
fn update_price_feeds_internal(
606606
ref self: ContractState,
607-
data: ByteArray,
607+
data: ByteBuffer,
608608
price_ids: Array<u256>,
609609
min_publish_time: u64,
610610
max_publish_time: u64,

target_chains/starknet/contracts/src/pyth/fake_upgrades.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait IFakePyth<T> {
1111
#[starknet::contract]
1212
mod pyth_fake_upgrade1 {
1313
use pyth::pyth::{GetPriceUnsafeError, DataSource, Price};
14-
use pyth::byte_array::ByteArray;
14+
use pyth::byte_buffer::ByteBuffer;
1515

1616
#[storage]
1717
struct Storage {}
@@ -36,7 +36,7 @@ mod pyth_fake_upgrade1 {
3636
#[starknet::contract]
3737
mod pyth_fake_upgrade_wrong_magic {
3838
use pyth::pyth::{GetPriceUnsafeError, DataSource, Price};
39-
use pyth::byte_array::ByteArray;
39+
use pyth::byte_buffer::ByteBuffer;
4040

4141
#[storage]
4242
struct Storage {}

target_chains/starknet/contracts/src/pyth/governance.cairo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pyth::reader::ReaderTrait;
22
use core::array::ArrayTrait;
33
use pyth::reader::{Reader, ReaderImpl};
4-
use pyth::byte_array::ByteArray;
4+
use pyth::byte_buffer::ByteBuffer;
55
use pyth::pyth::errors::GovernanceActionError;
66
use core::panic_with_felt252;
77
use core::starknet::{ContractAddress, ClassHash};
@@ -82,7 +82,7 @@ pub struct AuthorizeGovernanceDataSourceTransfer {
8282
// Transfer governance control over this contract to another data source.
8383
// The claim_vaa field is a VAA created by the new data source; using a VAA prevents mistakes
8484
// in the handoff by ensuring that the new data source can send VAAs (i.e., is not an invalid address).
85-
pub claim_vaa: ByteArray,
85+
pub claim_vaa: ByteBuffer,
8686
}
8787

8888
#[derive(Drop, Debug)]
@@ -94,7 +94,7 @@ pub struct UpgradeContract {
9494
pub new_implementation: ClassHash,
9595
}
9696

97-
pub fn parse_instruction(payload: ByteArray) -> GovernanceInstruction {
97+
pub fn parse_instruction(payload: ByteBuffer) -> GovernanceInstruction {
9898
let mut reader = ReaderImpl::new(payload);
9999
let magic = reader.read_u32();
100100
if magic != MAGIC {

target_chains/starknet/contracts/src/pyth/interface.cairo

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{GetPriceUnsafeError, GetPriceNoOlderThanError};
2-
use pyth::byte_array::ByteArray;
2+
use pyth::byte_buffer::ByteBuffer;
33
use pyth::wormhole::VerifiedVM;
44
use core::starknet::ContractAddress;
55

@@ -20,21 +20,25 @@ pub trait IPyth<T> {
2020
fn price_feed_exists(self: @T, price_id: u256) -> bool;
2121
fn latest_price_info_publish_time(self: @T, price_id: u256) -> u64;
2222

23-
fn update_price_feeds(ref self: T, data: ByteArray);
23+
fn update_price_feeds(ref self: T, data: ByteBuffer);
2424
fn update_price_feeds_if_necessary(
25-
ref self: T, update: ByteArray, required_publish_times: Array<PriceFeedPublishTime>
25+
ref self: T, update: ByteBuffer, required_publish_times: Array<PriceFeedPublishTime>
2626
);
2727
fn parse_price_feed_updates(
2828
ref self: T,
29-
data: ByteArray,
29+
data: ByteBuffer,
3030
price_ids: Array<u256>,
3131
min_publish_time: u64,
3232
max_publish_time: u64
3333
) -> Array<PriceFeed>;
3434
fn parse_unique_price_feed_updates(
35-
ref self: T, data: ByteArray, price_ids: Array<u256>, publish_time: u64, max_staleness: u64,
35+
ref self: T,
36+
data: ByteBuffer,
37+
price_ids: Array<u256>,
38+
publish_time: u64,
39+
max_staleness: u64,
3640
) -> Array<PriceFeed>;
37-
fn get_update_fee(self: @T, data: ByteArray, token: ContractAddress) -> u256;
41+
fn get_update_fee(self: @T, data: ByteBuffer, token: ContractAddress) -> u256;
3842
fn wormhole_address(self: @T) -> ContractAddress;
3943
fn fee_token_addresses(self: @T) -> Array<ContractAddress>;
4044
fn get_single_update_fee(self: @T, token: ContractAddress) -> u256;
@@ -46,7 +50,7 @@ pub trait IPyth<T> {
4650
fn governance_data_source_index(self: @T) -> u32;
4751
fn chain_id(self: @T) -> u16;
4852

49-
fn execute_governance_instruction(ref self: T, data: ByteArray);
53+
fn execute_governance_instruction(ref self: T, data: ByteBuffer);
5054
fn version(self: @T) -> felt252;
5155
fn pyth_upgradable_magic(self: @T) -> u32;
5256
}

target_chains/starknet/contracts/src/pyth/price_update.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pyth::reader::{Reader, ReaderImpl};
22
use pyth::pyth::{UpdatePriceFeedsError, PriceFeed, Price};
33
use core::panic_with_felt252;
4-
use pyth::byte_array::ByteArray;
4+
use pyth::byte_buffer::ByteBuffer;
55
use pyth::merkle_tree::read_and_verify_proof;
66
use pyth::util::{u32_as_i32, u64_as_i64};
77

@@ -88,7 +88,7 @@ pub fn read_and_verify_header(ref reader: Reader) {
8888
}
8989
}
9090

91-
pub fn parse_wormhole_proof(payload: ByteArray) -> u256 {
91+
pub fn parse_wormhole_proof(payload: ByteBuffer) -> u256 {
9292
let mut reader = ReaderImpl::new(payload);
9393
if reader.read_u32() != ACCUMULATOR_WORMHOLE_MAGIC {
9494
panic_with_felt252(UpdatePriceFeedsError::InvalidUpdateData.into());

0 commit comments

Comments
 (0)