Skip to content

Commit

Permalink
feat: make alignment bytes explicit in Balance (#240)
Browse files Browse the repository at this point in the history
* feat: make alignment bytes explicit in Balance

* test: extend reg tests for Account
  • Loading branch information
losman0s authored Aug 23, 2024
1 parent 9bd878f commit 24dfeb1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions programs/marginfi/src/state/marginfi_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
68 changes: 68 additions & 0 deletions programs/marginfi/tests/misc/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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(())
}
Expand Down

0 comments on commit 24dfeb1

Please sign in to comment.