mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-05-18 23:19:30 +00:00
fix(oracle): preallocate price account serialization
Use borsh::object_length to reserve the exact serialized size for OraclePriceAccount data without relying on size_of_val. Add coverage for string-backed source identifiers.
This commit is contained in:
parent
56dbf251d3
commit
bf7c6593ae
@ -173,7 +173,9 @@ impl TryFrom<&Data> for OraclePriceAccount {
|
|||||||
|
|
||||||
impl From<&OraclePriceAccount> for Data {
|
impl From<&OraclePriceAccount> for Data {
|
||||||
fn from(price_account: &OraclePriceAccount) -> Self {
|
fn from(price_account: &OraclePriceAccount) -> Self {
|
||||||
let mut data = Vec::new();
|
let serialized_len =
|
||||||
|
borsh::object_length(price_account).expect("Oracle price account length must be known");
|
||||||
|
let mut data = Vec::with_capacity(serialized_len);
|
||||||
BorshSerialize::serialize(price_account, &mut data)
|
BorshSerialize::serialize(price_account, &mut data)
|
||||||
.expect("Serialization to Vec should not fail");
|
.expect("Serialization to Vec should not fail");
|
||||||
Self::try_from(data).expect("Oracle price account encoded data should fit into Data")
|
Self::try_from(data).expect("Oracle price account encoded data should fit into Data")
|
||||||
@ -292,6 +294,19 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_price_account_data_matches_borsh_object_length() {
|
||||||
|
let mut oracle_account = price_account(1_050_000, 1_000);
|
||||||
|
oracle_account.source_identifier = "mock-oracle-with-long-source-name".to_owned();
|
||||||
|
|
||||||
|
let data = Data::from(&oracle_account);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
data.as_ref().len(),
|
||||||
|
borsh::object_length(&oracle_account).expect("object length should be known")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn account_price_feed_rejects_wrong_feed_account() {
|
fn account_price_feed_rejects_wrong_feed_account() {
|
||||||
let oracle_account = price_account(1_050_000, 1_000);
|
let oracle_account = price_account(1_050_000, 1_000);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user