Skip to content

Commit 0f9de33

Browse files
committed
cleaned serialization
1 parent ac3d505 commit 0f9de33

File tree

1 file changed

+8
-26
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+8
-26
lines changed

target_chains/stylus/contracts/pyth-receiver/src/structs.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
1-
use alloc::{boxed::Box, format, vec::Vec};
2-
use pythnet_sdk::wire::to_vec;
1+
use alloc::{vec::Vec};
32
use serde::Serialize;
43
use stylus_sdk::alloy_primitives::{keccak256, FixedBytes, B256, I32, I64, U16, U256, U64};
54
use stylus_sdk::{
65
prelude::*,
76
storage::{StorageFixedBytes, StorageI32, StorageI64, StorageKey, StorageU16, StorageU64},
87
};
98

10-
#[derive(Serialize)]
11-
struct SerializableDataSource {
12-
chain_id: u16,
13-
#[serde(with = "pythnet_sdk::wire::array")]
14-
emitter_address: [u8; 32],
15-
}
16-
179
fn serialize_data_source_to_bytes(
1810
chain_id: u16,
1911
emitter_address: &[u8; 32],
20-
) -> Result<[u8; 34], Box<dyn core::error::Error>> {
21-
let data_source = SerializableDataSource {
22-
chain_id,
23-
emitter_address: *emitter_address,
24-
};
25-
26-
let bytes = to_vec::<_, byteorder::BE>(&data_source)?;
27-
if bytes.len() != 34 {
28-
return Err(format!("Expected 34 bytes, got {}", bytes.len()).into());
29-
}
30-
12+
) -> [u8; 34] {
3113
let mut result = [0u8; 34];
32-
result.copy_from_slice(&bytes);
33-
Ok(result)
14+
result[0..2].copy_from_slice(&chain_id.to_be_bytes());
15+
result[2..].copy_from_slice(emitter_address);
16+
result
3417
}
3518

19+
3620
#[derive(Debug)]
3721
#[storage]
3822
pub struct DataSourceStorage {
@@ -51,8 +35,7 @@ impl StorageKey for DataSource {
5135
let chain_id: u16 = self.chain_id.to::<u16>();
5236
let emitter_address: [u8; 32] = self.emitter_address.as_slice().try_into().unwrap();
5337

54-
let bytes = serialize_data_source_to_bytes(chain_id, &emitter_address)
55-
.expect("Failed to serialize DataSource");
38+
let bytes = serialize_data_source_to_bytes(chain_id, &emitter_address);
5639

5740
keccak256(bytes).to_slot(root)
5841
}
@@ -100,8 +83,7 @@ mod tests {
10083
expected_bytes[0..2].copy_from_slice(&chain_id.to_be_bytes());
10184
expected_bytes[2..].copy_from_slice(&emitter_address);
10285

103-
let actual_bytes = serialize_data_source_to_bytes(chain_id, &emitter_address)
104-
.expect("Serialization should succeed");
86+
let actual_bytes = serialize_data_source_to_bytes(chain_id, &emitter_address);
10587

10688
assert_eq!(
10789
actual_bytes, expected_bytes,

0 commit comments

Comments
 (0)