Skip to content

Commit

Permalink
program: sequence check as u8 (#909)
Browse files Browse the repository at this point in the history
* program: sequence check as u8 (reuse existing padding)

(cherry picked from commit 0728bb5)
  • Loading branch information
farnyser authored and ckamm committed Mar 11, 2024
1 parent 8f9c47e commit a8af12a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 99 deletions.
32 changes: 7 additions & 25 deletions mango_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@
"args": [
{
"name": "expectedSequenceNumber",
"type": "u64"
"type": "u8"
}
]
},
Expand Down Expand Up @@ -7901,13 +7901,8 @@
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -7972,16 +7967,12 @@
],
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
},
Expand Down Expand Up @@ -9703,13 +9694,8 @@
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -9755,16 +9741,12 @@
"name": "lastCollateralFeeCharge",
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion programs/mango-v4/src/instructions/sequence_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::accounts_ix::*;
use crate::error::MangoError;
use crate::state::*;

pub fn sequence_check(ctx: Context<SequenceCheck>, expected_sequence_number: u64) -> Result<()> {
pub fn sequence_check(ctx: Context<SequenceCheck>, expected_sequence_number: u8) -> Result<()> {
let mut account = ctx.accounts.account.load_full_mut()?;

require_eq!(
Expand Down
5 changes: 1 addition & 4 deletions programs/mango-v4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,7 @@ pub mod mango_v4 {
Ok(())
}

pub fn sequence_check(
ctx: Context<SequenceCheck>,
expected_sequence_number: u64,
) -> Result<()> {
pub fn sequence_check(ctx: Context<SequenceCheck>, expected_sequence_number: u8) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::sequence_check(ctx, expected_sequence_number)?;
Ok(())
Expand Down
24 changes: 9 additions & 15 deletions programs/mango-v4/src/state/mango_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ pub struct MangoAccount {

pub bump: u8,

#[derivative(Debug = "ignore")]
pub padding: [u8; 1],
pub sequence_number: u8,

// (Display only)
// Cumulative (deposits - withdraws)
Expand Down Expand Up @@ -157,10 +156,8 @@ pub struct MangoAccount {
/// Time at which the last collateral fee was charged
pub last_collateral_fee_charge: u64,

pub sequence_number: u64,

#[derivative(Debug = "ignore")]
pub reserved: [u8; 144],
pub reserved: [u8; 152],

// dynamic
pub header_version: u8,
Expand Down Expand Up @@ -202,7 +199,7 @@ impl MangoAccount {
in_health_region: 0,
account_num: 0,
bump: 0,
padding: Default::default(),
sequence_number: 0,
net_deposits: 0,
perp_spot_transfers: 0,
health_region_begin_init_health: 0,
Expand All @@ -214,8 +211,7 @@ impl MangoAccount {
temporary_delegate: Pubkey::default(),
temporary_delegate_expiry: 0,
last_collateral_fee_charge: 0,
sequence_number: 0,
reserved: [0; 144],
reserved: [0; 152],
header_version: DEFAULT_MANGO_ACCOUNT_VERSION,
padding3: Default::default(),
padding4: Default::default(),
Expand Down Expand Up @@ -328,7 +324,7 @@ pub struct MangoAccountFixed {
being_liquidated: u8,
in_health_region: u8,
pub bump: u8,
pub padding: [u8; 1],
pub sequence_number: u8,
pub net_deposits: i64,
pub perp_spot_transfers: i64,
pub health_region_begin_init_health: i64,
Expand All @@ -340,12 +336,11 @@ pub struct MangoAccountFixed {
pub temporary_delegate: Pubkey,
pub temporary_delegate_expiry: u64,
pub last_collateral_fee_charge: u64,
pub sequence_number: u64,
pub reserved: [u8; 144],
pub reserved: [u8; 152],
}
const_assert_eq!(
size_of::<MangoAccountFixed>(),
32 * 4 + 8 + 8 * 8 + 32 + 8 + 8 + 8 + 144
32 * 4 + 8 + 8 * 8 + 32 + 8 + 8 + 152
);
const_assert_eq!(size_of::<MangoAccountFixed>(), 400);
const_assert_eq!(size_of::<MangoAccountFixed>() % 8, 0);
Expand Down Expand Up @@ -2901,7 +2896,7 @@ mod tests {
being_liquidated: fixed.being_liquidated,
in_health_region: fixed.in_health_region,
bump: fixed.bump,
padding: Default::default(),
sequence_number: 0,
net_deposits: fixed.net_deposits,
perp_spot_transfers: fixed.perp_spot_transfers,
health_region_begin_init_health: fixed.health_region_begin_init_health,
Expand All @@ -2913,8 +2908,7 @@ mod tests {
temporary_delegate: fixed.temporary_delegate,
temporary_delegate_expiry: fixed.temporary_delegate_expiry,
last_collateral_fee_charge: fixed.last_collateral_fee_charge,
sequence_number: 0,
reserved: [0u8; 144],
reserved: [0u8; 152],

header_version: *zerocopy_reader.header_version(),
padding3: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion programs/mango-v4/tests/program_test/mango_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5144,7 +5144,7 @@ impl ClientInstruction for TokenChargeCollateralFeesInstruction {
pub struct SequenceCheckInstruction {
pub account: Pubkey,
pub owner: TestKeypair,
pub expected_sequence_number: u64,
pub expected_sequence_number: u8,
}
#[async_trait::async_trait(?Send)]
impl ClientInstruction for SequenceCheckInstruction {
Expand Down
2 changes: 1 addition & 1 deletion ts/client/src/accounts/mangoAccount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Mango Account', () => {
new BN(0),
new BN(0),
new BN(0),
new BN(0),
0,
0,
[],
[],
Expand Down
4 changes: 2 additions & 2 deletions ts/client/src/accounts/mangoAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class MangoAccount {
buybackFeesAccruedCurrent: BN;
buybackFeesAccruedPrevious: BN;
buybackFeesExpiryTimestamp: BN;
sequenceNumber: BN;
sequenceNumber: number;
headerVersion: number;
tokens: unknown;
serum3: unknown;
Expand Down Expand Up @@ -96,7 +96,7 @@ export class MangoAccount {
public buybackFeesAccruedCurrent: BN,
public buybackFeesAccruedPrevious: BN,
public buybackFeesExpiryTimestamp: BN,
public sequenceNumber: BN,
public sequenceNumber: number,
public headerVersion: number,
tokens: TokenPositionDto[],
serum3: Serum3PositionDto[],
Expand Down
64 changes: 14 additions & 50 deletions ts/client/src/mango_v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ export type MangoV4 = {
"args": [
{
"name": "expectedSequenceNumber",
"type": "u64"
"type": "u8"
}
]
},
Expand Down Expand Up @@ -7901,13 +7901,8 @@ export type MangoV4 = {
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -7972,16 +7967,12 @@ export type MangoV4 = {
],
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
},
Expand Down Expand Up @@ -9703,13 +9694,8 @@ export type MangoV4 = {
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -9755,16 +9741,12 @@ export type MangoV4 = {
"name": "lastCollateralFeeCharge",
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
}
Expand Down Expand Up @@ -16190,7 +16172,7 @@ export const IDL: MangoV4 = {
"args": [
{
"name": "expectedSequenceNumber",
"type": "u64"
"type": "u8"
}
]
},
Expand Down Expand Up @@ -22305,13 +22287,8 @@ export const IDL: MangoV4 = {
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -22376,16 +22353,12 @@ export const IDL: MangoV4 = {
],
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
},
Expand Down Expand Up @@ -24107,13 +24080,8 @@ export const IDL: MangoV4 = {
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
1
]
}
"name": "sequenceNumber",
"type": "u8"
},
{
"name": "netDeposits",
Expand Down Expand Up @@ -24159,16 +24127,12 @@ export const IDL: MangoV4 = {
"name": "lastCollateralFeeCharge",
"type": "u64"
},
{
"name": "sequenceNumber",
"type": "u64"
},
{
"name": "reserved",
"type": {
"array": [
"u8",
144
152
]
}
}
Expand Down

0 comments on commit a8af12a

Please sign in to comment.