Skip to content

Commit

Permalink
[pythnet-sdk] Bump borsh add borsh to MerklePriceUpdate (#1186)
Browse files Browse the repository at this point in the history
* Add borsh

* Bump borsh

* Cleanup

* Try

* Cleanup

* Do it

* Add a test
  • Loading branch information
guibescos authored Dec 15, 2023
1 parent 7bf41c4 commit 178ad4c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pythnet/pythnet_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "pythnet_sdk"

[dependencies]
bincode = "1.3.1"
borsh = "0.9.1"
borsh = "0.10.3"
bytemuck = { version = "1.11.0", features = ["derive"] }
byteorder = "1.4.3"
fast-math = "0.1"
Expand Down
8 changes: 7 additions & 1 deletion pythnet/pythnet_sdk/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub mod v1 {
hashers::keccak256_160::Keccak160,
require,
},
borsh::{
BorshDeserialize,
BorshSerialize,
},
serde::{
Deserialize,
Serialize,
Expand Down Expand Up @@ -99,7 +103,9 @@ pub mod v1 {
},
}

#[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)]
#[derive(
Clone, Debug, Hash, PartialEq, Serialize, Deserialize, BorshDeserialize, BorshSerialize,
)]
pub struct MerklePriceUpdate {
pub message: PrefixedVec<u16, u8>,
pub proof: MerklePath<Keccak160>,
Expand Down
34 changes: 25 additions & 9 deletions pythnet/pythnet_sdk/src/wire/prefixed_vec.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use serde::{
de::DeserializeSeed,
ser::{
SerializeSeq,
SerializeStruct,
use {
borsh::{
BorshDeserialize,
BorshSerialize,
},
serde::{
de::DeserializeSeed,
ser::{
SerializeSeq,
SerializeStruct,
},
Deserialize,
Serialize,
},
Deserialize,
Serialize,
};

/// PrefixlessVec overrides the serialization to _not_ write a length prefix.
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, BorshDeserialize, BorshSerialize)]
struct PrefixlessVec<T> {
inner: Vec<T>,
}
Expand Down Expand Up @@ -99,7 +105,7 @@ where
///
/// For non-Pyth formats this results in a struct which is the correct way to interpret our
/// data on chain anyway.
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, BorshDeserialize, BorshSerialize)]
pub struct PrefixedVec<L, T> {
__phantom: std::marker::PhantomData<L>,
data: PrefixlessVec<T>,
Expand Down Expand Up @@ -227,3 +233,13 @@ where
)
}
}

#[test]
fn test_borsh_roundtrip() {
let prefixed_vec = PrefixedVec::<u16, u8>::from(vec![1, 2, 3, 4, 5]);
let encoded = borsh::to_vec(&prefixed_vec).unwrap();
assert_eq!(encoded, vec![5, 0, 0, 0, 1, 2, 3, 4, 5]);

let decoded_prefixed_vec = PrefixedVec::<u16, u8>::try_from_slice(encoded.as_slice()).unwrap();
assert_eq!(decoded_prefixed_vec, prefixed_vec);
}
43 changes: 33 additions & 10 deletions pythnet/pythnet_sdk/src/wormhole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ use {
};

#[repr(transparent)]
#[derive(Default)]
#[derive(Default, PartialEq, Debug)]
pub struct PostedMessageUnreliableData {
pub message: MessageData,
}

#[derive(Debug, Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize)]
#[derive(
Debug, Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize, PartialEq,
)]
pub struct MessageData {
pub vaa_version: u8,
pub consistency_level: u8,
Expand All @@ -54,22 +56,19 @@ impl BorshSerialize for PostedMessageUnreliableData {
}

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

let expected = b"msu";
let magic: &[u8] = &buf[0..3];
if magic != expected {
if &magic != expected {
return Err(Error::new(
InvalidData,
format!("Magic mismatch. Expected {expected:?} but got {magic:?}"),
));
};
*buf = &buf[3..];
Ok(PostedMessageUnreliableData {
message: <MessageData as BorshDeserialize>::deserialize(buf)?,
message: <MessageData as BorshDeserialize>::deserialize_reader(reader)?,
})
}
}
Expand Down Expand Up @@ -99,3 +98,27 @@ impl Clone for PostedMessageUnreliableData {
pub struct AccumulatorSequenceTracker {
pub sequence: u64,
}

#[test]
fn test_borsh_roundtrip() {
let post_message_unreliable_data = PostedMessageUnreliableData {
message: MessageData {
vaa_version: 1,
consistency_level: 2,
vaa_time: 3,
vaa_signature_account: [4u8; 32],
submission_time: 5,
nonce: 6,
sequence: 7,
emitter_chain: 8,
emitter_address: [9u8; 32],
payload: vec![10u8; 32],
},
};


let encoded = borsh::to_vec(&post_message_unreliable_data).unwrap();

let decoded = PostedMessageUnreliableData::try_from_slice(encoded.as_slice()).unwrap();
assert_eq!(decoded, post_message_unreliable_data);
}

0 comments on commit 178ad4c

Please sign in to comment.