Skip to content

Commit 178ad4c

Browse files
authored
[pythnet-sdk] Bump borsh add borsh to MerklePriceUpdate (#1186)
* Add borsh * Bump borsh * Cleanup * Try * Cleanup * Do it * Add a test
1 parent 7bf41c4 commit 178ad4c

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

pythnet/pythnet_sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ name = "pythnet_sdk"
1212

1313
[dependencies]
1414
bincode = "1.3.1"
15-
borsh = "0.9.1"
15+
borsh = "0.10.3"
1616
bytemuck = { version = "1.11.0", features = ["derive"] }
1717
byteorder = "1.4.3"
1818
fast-math = "0.1"

pythnet/pythnet_sdk/src/wire.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub mod v1 {
4040
hashers::keccak256_160::Keccak160,
4141
require,
4242
},
43+
borsh::{
44+
BorshDeserialize,
45+
BorshSerialize,
46+
},
4347
serde::{
4448
Deserialize,
4549
Serialize,
@@ -99,7 +103,9 @@ pub mod v1 {
99103
},
100104
}
101105

102-
#[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)]
106+
#[derive(
107+
Clone, Debug, Hash, PartialEq, Serialize, Deserialize, BorshDeserialize, BorshSerialize,
108+
)]
103109
pub struct MerklePriceUpdate {
104110
pub message: PrefixedVec<u16, u8>,
105111
pub proof: MerklePath<Keccak160>,

pythnet/pythnet_sdk/src/wire/prefixed_vec.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
use serde::{
2-
de::DeserializeSeed,
3-
ser::{
4-
SerializeSeq,
5-
SerializeStruct,
1+
use {
2+
borsh::{
3+
BorshDeserialize,
4+
BorshSerialize,
5+
},
6+
serde::{
7+
de::DeserializeSeed,
8+
ser::{
9+
SerializeSeq,
10+
SerializeStruct,
11+
},
12+
Deserialize,
13+
Serialize,
614
},
7-
Deserialize,
8-
Serialize,
915
};
1016

1117
/// PrefixlessVec overrides the serialization to _not_ write a length prefix.
12-
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
18+
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, BorshDeserialize, BorshSerialize)]
1319
struct PrefixlessVec<T> {
1420
inner: Vec<T>,
1521
}
@@ -99,7 +105,7 @@ where
99105
///
100106
/// For non-Pyth formats this results in a struct which is the correct way to interpret our
101107
/// data on chain anyway.
102-
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
108+
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, BorshDeserialize, BorshSerialize)]
103109
pub struct PrefixedVec<L, T> {
104110
__phantom: std::marker::PhantomData<L>,
105111
data: PrefixlessVec<T>,
@@ -227,3 +233,13 @@ where
227233
)
228234
}
229235
}
236+
237+
#[test]
238+
fn test_borsh_roundtrip() {
239+
let prefixed_vec = PrefixedVec::<u16, u8>::from(vec![1, 2, 3, 4, 5]);
240+
let encoded = borsh::to_vec(&prefixed_vec).unwrap();
241+
assert_eq!(encoded, vec![5, 0, 0, 0, 1, 2, 3, 4, 5]);
242+
243+
let decoded_prefixed_vec = PrefixedVec::<u16, u8>::try_from_slice(encoded.as_slice()).unwrap();
244+
assert_eq!(decoded_prefixed_vec, prefixed_vec);
245+
}

pythnet/pythnet_sdk/src/wormhole.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ use {
2727
};
2828

2929
#[repr(transparent)]
30-
#[derive(Default)]
30+
#[derive(Default, PartialEq, Debug)]
3131
pub struct PostedMessageUnreliableData {
3232
pub message: MessageData,
3333
}
3434

35-
#[derive(Debug, Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)]
35+
#[derive(
36+
Debug, Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize, PartialEq,
37+
)]
3638
pub struct MessageData {
3739
pub vaa_version: u8,
3840
pub consistency_level: u8,
@@ -54,22 +56,19 @@ impl BorshSerialize for PostedMessageUnreliableData {
5456
}
5557

5658
impl BorshDeserialize for PostedMessageUnreliableData {
57-
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
58-
if buf.len() < 3 {
59-
return Err(Error::new(InvalidData, "Not enough bytes"));
60-
}
59+
fn deserialize_reader<R: std::io::prelude::Read>(reader: &mut R) -> std::io::Result<Self> {
60+
let mut magic = [0u8; 3];
61+
reader.read_exact(&mut magic)?;
6162

6263
let expected = b"msu";
63-
let magic: &[u8] = &buf[0..3];
64-
if magic != expected {
64+
if &magic != expected {
6565
return Err(Error::new(
6666
InvalidData,
6767
format!("Magic mismatch. Expected {expected:?} but got {magic:?}"),
6868
));
6969
};
70-
*buf = &buf[3..];
7170
Ok(PostedMessageUnreliableData {
72-
message: <MessageData as BorshDeserialize>::deserialize(buf)?,
71+
message: <MessageData as BorshDeserialize>::deserialize_reader(reader)?,
7372
})
7473
}
7574
}
@@ -99,3 +98,27 @@ impl Clone for PostedMessageUnreliableData {
9998
pub struct AccumulatorSequenceTracker {
10099
pub sequence: u64,
101100
}
101+
102+
#[test]
103+
fn test_borsh_roundtrip() {
104+
let post_message_unreliable_data = PostedMessageUnreliableData {
105+
message: MessageData {
106+
vaa_version: 1,
107+
consistency_level: 2,
108+
vaa_time: 3,
109+
vaa_signature_account: [4u8; 32],
110+
submission_time: 5,
111+
nonce: 6,
112+
sequence: 7,
113+
emitter_chain: 8,
114+
emitter_address: [9u8; 32],
115+
payload: vec![10u8; 32],
116+
},
117+
};
118+
119+
120+
let encoded = borsh::to_vec(&post_message_unreliable_data).unwrap();
121+
122+
let decoded = PostedMessageUnreliableData::try_from_slice(encoded.as_slice()).unwrap();
123+
assert_eq!(decoded, post_message_unreliable_data);
124+
}

0 commit comments

Comments
 (0)