use anchor_lang::prelude::*;
#[account]
pub struct Config {
pub pubkey_g2: [u8; 128],
pub genesis_time: u64,
pub period: u64,
pub chain_hash: [u8; 32],
pub authority: Pubkey,
pub bump: u8,
}
impl Config {
pub const LEN: usize = 8 + 128 + 8 + 8 + 32 + 32 + 1;
}
#[cfg(test)]
mod tests {
use super::*;
use anchor_lang::{AnchorDeserialize, AnchorSerialize};
#[test]
fn config_len_is_217_bytes() {
assert_eq!(
Config::LEN,
217,
"Config::LEN must equal spec.md §Account Schema (217 bytes)"
);
}
#[test]
fn config_borsh_roundtrip_pins_v1_schema() {
let original = Config {
pubkey_g2: [0xAB; 128],
genesis_time: 0x1122_3344_5566_7788,
period: 3,
chain_hash: [0xCD; 32],
authority: Pubkey::new_unique(),
bump: 255,
};
let bytes = original.try_to_vec().expect("serialize must succeed");
assert_eq!(
bytes.len(),
Config::LEN - 8,
"Borsh-serialized Config must be exactly 209 bytes (Config::LEN - 8 discriminator)",
);
assert_eq!(
&bytes[0..128],
&[0xAB; 128],
"pubkey_g2 must be first field"
);
assert_eq!(
&bytes[128..136],
&0x1122_3344_5566_7788u64.to_le_bytes(),
"genesis_time must follow pubkey_g2 as LE u64"
);
assert_eq!(
&bytes[136..144],
&3u64.to_le_bytes(),
"period must follow genesis_time as LE u64"
);
assert_eq!(
&bytes[144..176],
&[0xCD; 32],
"chain_hash must follow period"
);
assert_eq!(bytes[208], 255, "bump must be the last byte");
let recovered = Config::try_from_slice(&bytes).expect("deserialize must succeed");
assert_eq!(recovered.pubkey_g2, original.pubkey_g2);
assert_eq!(recovered.genesis_time, original.genesis_time);
assert_eq!(recovered.period, original.period);
assert_eq!(recovered.chain_hash, original.chain_hash);
assert_eq!(recovered.authority, original.authority);
assert_eq!(recovered.bump, original.bump);
}
}