Исправить чтение state в shine_payments и описать e2e тест

This commit is contained in:
AidarKC
2026-06-06 17:12:26 +04:00
parent 89d06d317b
commit d25c19cdaa
2 changed files with 213 additions and 3 deletions
@@ -250,11 +250,16 @@ impl<'a> Reader<'a> {
}
trait StateCodec: Sized {
fn encoded_len() -> usize;
fn encode(&self) -> Vec<u8>;
fn decode(data: &[u8]) -> Result<Self, ProgramError>;
}
impl StateCodec for ConfigState {
fn encoded_len() -> usize {
1 + 32 + 32
}
fn encode(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(65);
out.push(self.version);
@@ -274,6 +279,10 @@ impl StateCodec for ConfigState {
}
impl StateCodec for CoefLimitState {
fn encoded_len() -> usize {
1 + 8 + 8 + 8
}
fn encode(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(25);
out.push(self.version);
@@ -295,6 +304,10 @@ impl StateCodec for CoefLimitState {
}
impl StateCodec for QueuesState {
fn encoded_len() -> usize {
1 + 12 * 8
}
fn encode(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(97);
out.push(self.version);
@@ -339,6 +352,10 @@ impl StateCodec for QueuesState {
}
impl StateCodec for TicketState {
fn encoded_len() -> usize {
1 + 1 + 8 + 1 + 32 + 8 + 8
}
fn encode(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(59);
out.push(self.version);
@@ -370,6 +387,10 @@ impl StateCodec for TicketState {
}
impl StateCodec for ManagerAllowanceState {
fn encoded_len() -> usize {
1 + 32 + 8 + 8 + 8
}
fn encode(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(57);
out.push(self.version);
@@ -393,6 +414,10 @@ impl StateCodec for ManagerAllowanceState {
}
impl StateCodec for VaultState {
fn encoded_len() -> usize {
1
}
fn encode(&self) -> Vec<u8> {
vec![self.version]
}
@@ -1162,9 +1187,9 @@ fn read_state<T: StateCodec>(pda: &AccountInfo) -> Result<T, ProgramError> {
require!(!is_uninitialized_account(pda), PaymentsError::EmptyState);
require_keys_eq!(*pda.owner, id(), PaymentsError::InvalidPdaAddress);
let data = pda.try_borrow_data()?;
let used_len = data.iter().rposition(|b| *b != 0).map(|idx| idx + 1).unwrap_or(0);
require!(used_len > 0, PaymentsError::EmptyState);
T::decode(&data[..used_len])
let encoded_len = T::encoded_len();
require!(data.len() >= encoded_len, PaymentsError::InvalidAccountData);
T::decode(&data[..encoded_len])
}
fn is_uninitialized_account(account: &AccountInfo) -> bool {