From 24dfeb16d121034dda9a25056a0810c774714f64 Mon Sep 17 00:00:00 2001 From: man0s <95379755+losman0s@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:39:06 +0800 Subject: [PATCH] feat: make alignment bytes explicit in Balance (#240) * feat: make alignment bytes explicit in Balance * test: extend reg tests for Account --- .../marginfi/src/state/marginfi_account.rs | 4 ++ programs/marginfi/tests/misc/regression.rs | 68 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/programs/marginfi/src/state/marginfi_account.rs b/programs/marginfi/src/state/marginfi_account.rs index 978612c7..ea3944d7 100644 --- a/programs/marginfi/src/state/marginfi_account.rs +++ b/programs/marginfi/src/state/marginfi_account.rs @@ -750,6 +750,7 @@ assert_struct_align!(Balance, 8); pub struct Balance { pub active: bool, pub bank_pk: Pubkey, + pub _pad0: [u8; 7], pub asset_shares: WrappedI80F48, pub liability_shares: WrappedI80F48, pub emissions_outstanding: WrappedI80F48, @@ -821,6 +822,7 @@ impl Balance { Balance { active: false, bank_pk: Pubkey::default(), + _pad0: [0; 7], asset_shares: WrappedI80F48::from(I80F48::ZERO), liability_shares: WrappedI80F48::from(I80F48::ZERO), emissions_outstanding: WrappedI80F48::from(I80F48::ZERO), @@ -880,6 +882,7 @@ impl<'a> BankAccountWrapper<'a> { lending_account.balances[empty_index] = Balance { active: true, bank_pk: *bank_pk, + _pad0: [0; 7], asset_shares: I80F48::ZERO.into(), liability_shares: I80F48::ZERO.into(), emissions_outstanding: I80F48::ZERO.into(), @@ -1409,6 +1412,7 @@ mod test { balances: [Balance { active: true, bank_pk: bank_pk.into(), + _pad0: [0; 7], asset_shares: WrappedI80F48::default(), liability_shares: WrappedI80F48::default(), emissions_outstanding: WrappedI80F48::default(), diff --git a/programs/marginfi/tests/misc/regression.rs b/programs/marginfi/tests/misc/regression.rs index 8d27d39e..a8b855f2 100644 --- a/programs/marginfi/tests/misc/regression.rs +++ b/programs/marginfi/tests/misc/regression.rs @@ -33,13 +33,24 @@ async fn account_field_values_reg() -> anyhow::Result<()> { }; let account = MarginfiAccount::try_deserialize(&mut BASE64_STANDARD.decode(data)?.as_slice())?; + assert_eq!( + account.group, + pubkey!("4qp6Fx6tnZkY5Wropq9wUYgtFxXKwE6viZxFHg3rdAG8") + ); assert_eq!( account.authority, pubkey!("Dq7wypbedtaqQK9QqEFvfrxc4ppfRGXCeTVd7ee7n2jw") ); + assert_eq!(account.account_flags, 0); + assert_eq!(account._padding, [0; 63]); let balance_1 = account.lending_account.balances[0]; assert!(balance_1.active); + assert_eq!( + balance_1.bank_pk, + pubkey!("2s37akK2eyBbp8DZgCm7RtsaEz8eJP3Nxd4urLHQv7yB") + ); + assert_eq!(balance_1._pad0, [0; 7]); assert_eq!( I80F48::from(balance_1.asset_shares), I80F48::from_str("1650216221.466876226897366").unwrap() @@ -52,9 +63,19 @@ async fn account_field_values_reg() -> anyhow::Result<()> { I80F48::from(balance_1.emissions_outstanding), I80F48::from_str("0").unwrap() ); + assert_eq!( + I80F48::from(balance_1.last_update), + I80F48::from_str("1711158766").unwrap() + ); + assert_eq!(balance_1._padding, [0; 1]); let balance_2 = account.lending_account.balances[1]; assert!(balance_2.active); + assert_eq!( + balance_2.bank_pk, + pubkey!("CCKtUs6Cgwo4aaQUmBPmyoApH2gUDErxNZCAntD6LYGh") + ); + assert_eq!(balance_2._pad0, [0; 7]); assert_eq!( I80F48::from(balance_2.asset_shares), I80F48::from_str("0").unwrap() @@ -67,6 +88,11 @@ async fn account_field_values_reg() -> anyhow::Result<()> { I80F48::from(balance_2.emissions_outstanding), I80F48::from_str("0").unwrap() ); + assert_eq!( + I80F48::from(balance_2.last_update), + I80F48::from_str("1711158793").unwrap() + ); + assert_eq!(balance_2._padding, [0; 1]); // Sample 2 @@ -82,13 +108,24 @@ async fn account_field_values_reg() -> anyhow::Result<()> { }; let account = MarginfiAccount::try_deserialize(&mut BASE64_STANDARD.decode(data)?.as_slice())?; + assert_eq!( + account.group, + pubkey!("4qp6Fx6tnZkY5Wropq9wUYgtFxXKwE6viZxFHg3rdAG8") + ); assert_eq!( account.authority, pubkey!("3T1kGHp7CrdeW9Qj1t8NMc2Ks233RyvzVhoaUPWoBEFK") ); + assert_eq!(account.account_flags, 0); + assert_eq!(account._padding, [0; 63]); let balance_1 = account.lending_account.balances[0]; assert!(balance_1.active); + assert_eq!( + balance_1.bank_pk, + pubkey!("6hS9i46WyTq1KXcoa2Chas2Txh9TJAVr6n1t3tnrE23K") + ); + assert_eq!(balance_1._pad0, [0; 7]); assert_eq!( I80F48::from(balance_1.asset_shares), I80F48::from_str("470.952530958931234").unwrap() @@ -101,9 +138,19 @@ async fn account_field_values_reg() -> anyhow::Result<()> { I80F48::from(balance_1.emissions_outstanding), I80F48::from_str("26891413.388324654086347").unwrap() ); + assert_eq!( + I80F48::from(balance_1.last_update), + I80F48::from_str("1705760628").unwrap() + ); + assert_eq!(balance_1._padding, [0; 1]); let balance_2 = account.lending_account.balances[1]; assert!(!balance_2.active); + assert_eq!( + balance_2.bank_pk, + pubkey!("11111111111111111111111111111111") + ); + assert_eq!(balance_2._pad0, [0; 7]); assert_eq!( I80F48::from(balance_2.asset_shares), I80F48::from_str("0").unwrap() @@ -116,6 +163,11 @@ async fn account_field_values_reg() -> anyhow::Result<()> { I80F48::from(balance_2.emissions_outstanding), I80F48::from_str("0").unwrap() ); + assert_eq!( + I80F48::from(balance_2.last_update), + I80F48::from_str("0").unwrap() + ); + assert_eq!(balance_2._padding, [0; 1]); // Sample 3 @@ -131,13 +183,24 @@ async fn account_field_values_reg() -> anyhow::Result<()> { }; let account = MarginfiAccount::try_deserialize(&mut BASE64_STANDARD.decode(data)?.as_slice())?; + assert_eq!( + account.group, + pubkey!("4qp6Fx6tnZkY5Wropq9wUYgtFxXKwE6viZxFHg3rdAG8") + ); assert_eq!( account.authority, pubkey!("7hmfVTuXc7HeX3YQjpiCXGVQuTeXonzjp795jorZukVR") ); + assert_eq!(account.account_flags, 0); + assert_eq!(account._padding, [0; 63]); let balance_1 = account.lending_account.balances[0]; assert!(!balance_1.active); + assert_eq!( + balance_1.bank_pk, + pubkey!("11111111111111111111111111111111") + ); + assert_eq!(balance_1._pad0, [0; 7]); assert_eq!( I80F48::from(balance_1.asset_shares), I80F48::from_str("0").unwrap() @@ -150,6 +213,11 @@ async fn account_field_values_reg() -> anyhow::Result<()> { I80F48::from(balance_1.emissions_outstanding), I80F48::from_str("0").unwrap() ); + assert_eq!( + I80F48::from(balance_1.last_update), + I80F48::from_str("0").unwrap() + ); + assert_eq!(balance_1._padding, [0; 1]); Ok(()) }