Skip to content

Commit 955c81b

Browse files
authored
Make AccumulatorUpdateData deserialization accept minor version bumps (#909)
* Make AccumulatorUpdateData deserialization accept minor version increases * Add test for additional bytes at the end of the message
1 parent fb6672f commit 955c81b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

pythnet/pythnet_sdk/src/messages.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,41 @@ impl Arbitrary for TwapMessage {
152152
}
153153
}
154154
}
155+
156+
#[cfg(test)]
157+
mod tests {
158+
159+
use crate::{
160+
messages::{
161+
Message,
162+
PriceFeedMessage,
163+
},
164+
wire::Serializer,
165+
};
166+
167+
// Test if additional payload to the end of a message is forward compatible
168+
#[test]
169+
fn test_forward_compatibility() {
170+
use {
171+
serde::Serialize,
172+
std::iter,
173+
};
174+
let msg = Message::PriceFeedMessage(PriceFeedMessage {
175+
feed_id: [1u8; 32],
176+
price: 1,
177+
conf: 1,
178+
exponent: 1,
179+
publish_time: 1,
180+
prev_publish_time: 1,
181+
ema_price: 1,
182+
ema_conf: 1,
183+
});
184+
let mut buffer = Vec::new();
185+
let mut cursor = std::io::Cursor::new(&mut buffer);
186+
let mut serializer: Serializer<_, byteorder::LE> = Serializer::new(&mut cursor);
187+
msg.serialize(&mut serializer).unwrap();
188+
buffer.extend(iter::repeat(0).take(10));
189+
let deserialized = crate::wire::from_slice::<byteorder::LE, Message>(&buffer).unwrap();
190+
assert_eq!(deserialized, msg);
191+
}
192+
}

pythnet/pythnet_sdk/src/wire.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub mod v1 {
4646
},
4747
};
4848
pub const PYTHNET_ACCUMULATOR_UPDATE_MAGIC: &[u8; 4] = b"PNAU";
49+
pub const CURRENT_MINOR_VERSION: u8 = 0;
4950

5051
// Transfer Format.
5152
// --------------------------------------------------------------------------------
@@ -79,7 +80,10 @@ pub mod v1 {
7980
Error::InvalidMagic
8081
);
8182
require!(message.major_version == 1, Error::InvalidVersion);
82-
require!(message.minor_version == 0, Error::InvalidVersion);
83+
require!(
84+
message.minor_version >= CURRENT_MINOR_VERSION,
85+
Error::InvalidVersion
86+
);
8387
Ok(message)
8488
}
8589
}
@@ -385,4 +389,28 @@ mod tests {
385389
// The deserialized value should be the same as the original.
386390
assert_eq!(deserialized_update, empty_update);
387391
}
392+
393+
// Test if the AccumulatorUpdateData major and minor version increases work as expected
394+
#[test]
395+
fn test_accumulator_forward_compatibility() {
396+
use serde::Serialize;
397+
// Serialize an empty update into a buffer.
398+
399+
let empty_update = AccumulatorUpdateData::new(Proof::WormholeMerkle {
400+
vaa: PrefixedVec::from(vec![]),
401+
updates: vec![],
402+
});
403+
let mut buffer = Vec::new();
404+
let mut cursor = std::io::Cursor::new(&mut buffer);
405+
let mut serializer: Serializer<_, byteorder::LE> = Serializer::new(&mut cursor);
406+
empty_update.serialize(&mut serializer).unwrap();
407+
408+
// Test if bumping minor version is still compatible
409+
buffer[5] = 0x03;
410+
AccumulatorUpdateData::try_from_slice(&buffer).unwrap();
411+
412+
// Test if bumping major version makes it incompatible
413+
buffer[4] = 0x03;
414+
AccumulatorUpdateData::try_from_slice(&buffer).unwrap_err();
415+
}
388416
}

0 commit comments

Comments
 (0)