Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions interface/src/data/intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct OrderIntent {
///
/// Layout: one character per byte, cell widths proportional to field size,
/// each divider belongs to the cell on its right. The byte range is
/// annotated below. Amounts and `valid_to` are big-endian encoded.
/// annotated below. Amounts and `valid_to` are little-endian encoded.
///
/// ```text
/// partially_fillable ─────┐
Expand Down Expand Up @@ -178,9 +178,9 @@ impl From<&OrderIntent> for EncodedOrderIntent {
*owner = intent.owner.to_bytes();
*buy_token = intent.buy_token_account.to_bytes();
*sell_token = intent.sell_token_account.to_bytes();
*sell_amount = intent.sell_amount.to_be_bytes();
*buy_amount = intent.buy_amount.to_be_bytes();
*valid_to = intent.valid_to.to_be_bytes();
*sell_amount = intent.sell_amount.to_le_bytes();
*buy_amount = intent.buy_amount.to_le_bytes();
*valid_to = intent.valid_to.to_le_bytes();
*kind = [intent.kind as u8];
*partially_fillable = [intent.partially_fillable as u8];
*app_data = intent.app_data;
Expand Down Expand Up @@ -226,9 +226,9 @@ impl TryFrom<&[u8; EncodedOrderIntent::SIZE]> for OrderIntent {
owner: Pubkey::new_from_array(*owner),
buy_token_account: Pubkey::new_from_array(*buy_token),
sell_token_account: Pubkey::new_from_array(*sell_token),
sell_amount: u64::from_be_bytes(*sell_amount),
buy_amount: u64::from_be_bytes(*buy_amount),
valid_to: u32::from_be_bytes(*valid_to),
sell_amount: u64::from_le_bytes(*sell_amount),
buy_amount: u64::from_le_bytes(*buy_amount),
valid_to: u32::from_le_bytes(*valid_to),
kind: match kind {
[0] => OrderKind::Sell,
[1] => OrderKind::Buy,
Expand Down Expand Up @@ -457,7 +457,7 @@ mod tests {
#[test]
fn uid_digest_regression() {
let intent = sample_intent(OrderKind::Buy, true);
let expected = hex!("091d7e1959ac6f7a400a91f1dcd9ce436f8f53e2b7a1d968acb08f79d3c1231d");
let expected = hex!("7ce7c6a74671090771fa33851387444064aca759ce55b80708723076722f5e00");
assert_eq!(intent.uid(), Hash::from(expected));
}

Expand All @@ -482,12 +482,12 @@ mod tests {
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
// sell_amount (0x0123_4567_89ab_cdef, BE u64)
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
// buy_amount (0xfedc_ba98_7654_3210, BE u64)
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
// valid_to (0xdead_beef, BE u32)
0xde, 0xad, 0xbe, 0xef,
// sell_amount (0x0123_4567_89ab_cdef, LE u64)
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01,
// buy_amount (0xfedc_ba98_7654_3210, LE u64)
0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe,
// valid_to (0xdead_beef, LE u32)
0xef, 0xbe, 0xad, 0xde,
// kind (Buy = 1)
0x01,
// partially_fillable (true = 1)
Expand Down
14 changes: 7 additions & 7 deletions interface/src/data/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub struct OrderAccount {
/// written to/read from the order PDA's data area.
///
/// Layout: one character per byte, cell widths proportional to field size,
/// each divider belongs to the cell on its right. Integers are big-endian.
/// The intent slot holds a verbatim [`EncodedOrderIntent`]; see that
/// type's docs for its inner layout.
/// each divider belongs to the cell on its right. Integers are little-endian
/// (Anchor/Borsh convention). The intent slot holds a verbatim
/// [`EncodedOrderIntent`]; see that type's docs for its inner layout.
///
/// ```text
/// ┌──── cancelled
Expand Down Expand Up @@ -120,8 +120,8 @@ pub fn write_account(
EncodedOrderAccount::W_INTENT
];
*cancelled_slot = [cancelled as u8];
*amount_withdrawn_slot = amount_withdrawn.to_be_bytes();
*amount_received_slot = amount_received.to_be_bytes();
*amount_withdrawn_slot = amount_withdrawn.to_le_bytes();
*amount_received_slot = amount_received.to_le_bytes();
*created_by_slot = created_by.to_bytes();
*intent_slot = *encoded_intent;
}
Expand Down Expand Up @@ -166,8 +166,8 @@ impl TryFrom<[u8; EncodedOrderAccount::SIZE]> for OrderAccount {
[1] => true,
_ => return Err(ProgramError::InvalidAccountData),
},
amount_withdrawn: u64::from_be_bytes(*amount_withdrawn),
amount_received: u64::from_be_bytes(*amount_received),
amount_withdrawn: u64::from_le_bytes(*amount_withdrawn),
amount_received: u64::from_le_bytes(*amount_received),
created_by: Pubkey::new_from_array(*created_by),
intent: OrderIntent::try_from(intent).map_err(|_| ProgramError::InvalidAccountData)?,
})
Expand Down
18 changes: 9 additions & 9 deletions interface/src/instruction/settle/begin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Pull {
/// the builder.
///
/// Wire format (grouped, with `n` orders and `T` total transfers):
/// `[discriminator=0][finalize_ix_index: u16 BE][n: u8][bump×n][transfer_count×n]
/// `[discriminator=0][finalize_ix_index: u16 LE][n: u8][bump×n][transfer_count×n]
/// [amount: u64 BE ×T]`.
/// Required accounts: `[instructions_sysvar (R), state_pda (R), token_program
/// (R)]` followed, per order, by `[order_pda (R), sell_token_account (W),
Expand Down Expand Up @@ -79,7 +79,7 @@ impl From<BeginSettle<'_>> for Instruction {
.collect();
let data = [
&[SettlementInstruction::BeginSettle.discriminator()][..],
&finalize_ix_index.to_be_bytes()[..],
&finalize_ix_index.to_le_bytes()[..],
&[order_pdas.len() as u8][..],
&order
.iter()
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {
data,
[
&[SettlementInstruction::BeginSettle.discriminator()][..],
&hex!("1337")[..], // counterpart index
&hex!("3713")[..], // counterpart index, little-endian
&[0][..], // order count
]
.concat(),
Expand Down Expand Up @@ -354,7 +354,7 @@ mod tests {
data,
[
&[SettlementInstruction::BeginSettle.discriminator()][..],
&hex!("1337")[..], // counterpart index
&hex!("3713")[..], // counterpart index, little-endian
&[2][..], // order count
&[low_bump, high_bump][..], // bumps
&[0, 0][..], // transfer counts (both zero)
Expand Down Expand Up @@ -430,7 +430,7 @@ mod tests {
data,
[
&[SettlementInstruction::BeginSettle.discriminator()][..],
&hex!("1337")[..], // counterpart index
&hex!("3713")[..], // counterpart index, little-endian
&[2][..], // order count
&[0xa1, 0xb1][..], // bumps
&[2, 1][..], // counts
Expand Down Expand Up @@ -480,7 +480,7 @@ mod tests {
];
let data = ix_data![
[SettlementInstruction::BeginSettle.discriminator()],
[0x13, 0x37], // finalize index
[0x37, 0x13], // finalize index, little-endian
[0x00], // order count
];
let BeginSettleInput {
Expand Down Expand Up @@ -539,7 +539,7 @@ mod tests {
];
let data = ix_data![
[SettlementInstruction::BeginSettle.discriminator()],
[0x13, 0x37], // finalize index
[0x37, 0x13], // finalize index, little-endian
[0x01], // order count
[0xab], // one order's bump
[0x00], // that order's transfer count
Expand Down Expand Up @@ -585,7 +585,7 @@ mod tests {
];
let data = ix_data![
[SettlementInstruction::BeginSettle.discriminator()],
[0x13, 0x37], // finalize index
[0x37, 0x13], // finalize index, little-endian
[0x01], // order count
[0xab], // bump
[0x02], // transfer count
Expand Down Expand Up @@ -640,7 +640,7 @@ mod tests {
// then all transfer counts (every order has zero transfers).
let data = ix_data![
[SettlementInstruction::BeginSettle.discriminator()],
[0x13, 0x37], // finalize index
[0x37, 0x13], // finalize index, little-endian
[ORDER_COUNT as u8], // order count
bumps,
[0u8; ORDER_COUNT],
Expand Down
10 changes: 5 additions & 5 deletions interface/src/instruction/settle/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::{recover_counterpart, INSTRUCTIONS_SYSVAR_ID};
/// `begin_ix_index` is the index of the paired `BeginSettle` instruction in the
/// same transaction.
///
/// Wire format: `[discriminator=1, begin_ix_index: u16 BE]`, 3 bytes.
/// Wire format: `[discriminator=1, begin_ix_index: u16 LE]`, 3 bytes.
/// Required accounts: `[instructions_sysvar (R)]`.
pub struct FinalizeSettle {
pub program_id: Pubkey,
Expand All @@ -31,7 +31,7 @@ impl From<FinalizeSettle> for Instruction {
accounts: vec![AccountMeta::new_readonly(INSTRUCTIONS_SYSVAR_ID, false)],
data: [
&[SettlementInstruction::FinalizeSettle.discriminator()],
&builder.begin_ix_index.to_be_bytes()[..],
&builder.begin_ix_index.to_le_bytes()[..],
]
.concat(),
}
Expand Down Expand Up @@ -86,7 +86,7 @@ mod tests {
data,
[
&[SettlementInstruction::FinalizeSettle.discriminator()][..],
&hex!("1337")[..], // counterpart index
&hex!("3713")[..], // counterpart index, little-endian
]
.concat(),
);
Expand All @@ -104,7 +104,7 @@ mod tests {
let mut accounts = [fake_account(address)];
let data = ix_data![
[SettlementInstruction::FinalizeSettle.discriminator()],
[0x13, 0x37], // begin index
[0x37, 0x13], // begin index, little-endian
];
let FinalizeSettleInput {
begin_ix_index,
Expand Down Expand Up @@ -147,7 +147,7 @@ mod tests {
let mut accounts = [fake_account(first_address), fake_account(second_address)];
let data = ix_data![
[SettlementInstruction::FinalizeSettle.discriminator()],
[0x13, 0x37], // begin index
[0x37, 0x13], // begin index, little-endian
[42], // extra
];
let FinalizeSettleInput {
Expand Down
10 changes: 5 additions & 5 deletions interface/src/instruction/settle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub use begin::{BeginSettle, BeginSettleInput, Pull, SettledOrder, SettledOrders
pub use finalize::{FinalizeSettle, FinalizeSettleInput};

/// Reads the first two bytes of a byte slice (instruction data) and
/// interprets them as a big-endian u16, returning it together with the
/// interprets them as a little-endian u16, returning it together with the
/// remaining bytes to parse.
/// It's meant to be used for BeginSettle and FinalizeSettle to extract the
/// counterpart index, that is, the index linking that instruction to the
/// opposite instruction which is encoded as the first
/// 2 bytes of the instruction data: `[0x13, 0x37]` → `0x1337`.
/// 2 bytes of the instruction data: `[0x37, 0x13]` → `0x1337`.
/// Returns `InvalidInstructionData` if fewer than two bytes are provided.
pub fn recover_counterpart(instruction_data: &[u8]) -> Result<(u16, &[u8]), ProgramError> {
match instruction_data {
[b1, b2, rest @ ..] => Ok((u16::from_be_bytes([*b1, *b2]), rest)),
[b1, b2, rest @ ..] => Ok((u16::from_le_bytes([*b1, *b2]), rest)),
_ => Err(ProgramError::InvalidInstructionData),
}
}
Expand All @@ -35,7 +35,7 @@ mod tests {
/// Builds an instruction-data byte vector from a list of field chunks, so a
/// test can spell out the wire layout one field per line without repeating
/// the `&[..][..]` slicing. Each chunk is anything sliceable to `[u8]` (a
/// byte array, a `Vec<u8>`, the result of `to_be_bytes()`, ...).
/// byte array, a `Vec<u8>`, the result of `to_le_bytes()`, ...).
macro_rules! ix_data {
($($chunk:expr),* $(,)?) => {
[$(&$chunk[..]),*].concat()
Expand Down Expand Up @@ -64,7 +64,7 @@ mod tests {
assert_eq!(
recover_counterpart(
&[
&hex!("1337")[..], // counterpart index
&hex!("3713")[..], // counterpart index, little-endian
&[42][..], // trailing
]
.concat()
Expand Down
4 changes: 2 additions & 2 deletions programs/settlement/tests/begin_settle_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fn rejects_orders_in_wrong_address_order() {
// Lay out the two distinct orders strictly decreasing by PDA address, which
// the program rejects. The interface builder would sort them, so build the
// instruction by hand in the current wire format: data is
// `[discriminator, finalize_ix_index (BE), order_count, bump×n, transfer_count×n]`
// `[discriminator, finalize_ix_index (LE), order_count, bump×n, transfer_count×n]`
// (no transfers here) and accounts are `[instructions_sysvar, state_pda,
// token_program, (order_pda, sell_token_account)...]`.
let mut orders = [
Expand All @@ -413,7 +413,7 @@ fn rejects_orders_in_wrong_address_order() {
orders.sort_by_key(|&(pda, _, _)| std::cmp::Reverse(pda));

let mut data = vec![SettlementInstruction::BeginSettle.discriminator()];
data.extend_from_slice(&1u16.to_be_bytes());
data.extend_from_slice(&1u16.to_le_bytes());
data.push(orders.len() as u8);
data.extend(orders.iter().map(|&(_, _, bump)| bump));
// No transfers: one zero transfer-count byte per order.
Expand Down