1
- use alloc:: { boxed:: Box , format, vec:: Vec } ;
2
- use pythnet_sdk:: wire:: to_vec;
1
+ use alloc:: { vec:: Vec } ;
3
2
use serde:: Serialize ;
4
3
use stylus_sdk:: alloy_primitives:: { keccak256, FixedBytes , B256 , I32 , I64 , U16 , U256 , U64 } ;
5
4
use stylus_sdk:: {
6
5
prelude:: * ,
7
6
storage:: { StorageFixedBytes , StorageI32 , StorageI64 , StorageKey , StorageU16 , StorageU64 } ,
8
7
} ;
9
8
10
- #[ derive( Serialize ) ]
11
- struct SerializableDataSource {
12
- chain_id : u16 ,
13
- #[ serde( with = "pythnet_sdk::wire::array" ) ]
14
- emitter_address : [ u8 ; 32 ] ,
15
- }
16
-
17
9
fn serialize_data_source_to_bytes (
18
10
chain_id : u16 ,
19
11
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 ] {
31
13
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
34
17
}
35
18
19
+
36
20
#[ derive( Debug ) ]
37
21
#[ storage]
38
22
pub struct DataSourceStorage {
@@ -51,8 +35,7 @@ impl StorageKey for DataSource {
51
35
let chain_id: u16 = self . chain_id . to :: < u16 > ( ) ;
52
36
let emitter_address: [ u8 ; 32 ] = self . emitter_address . as_slice ( ) . try_into ( ) . unwrap ( ) ;
53
37
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) ;
56
39
57
40
keccak256 ( bytes) . to_slot ( root)
58
41
}
@@ -100,8 +83,7 @@ mod tests {
100
83
expected_bytes[ 0 ..2 ] . copy_from_slice ( & chain_id. to_be_bytes ( ) ) ;
101
84
expected_bytes[ 2 ..] . copy_from_slice ( & emitter_address) ;
102
85
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) ;
105
87
106
88
assert_eq ! (
107
89
actual_bytes, expected_bytes,
0 commit comments