mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-03 22:03:06 +00:00
data field fixes
This commit is contained in:
parent
d78d97c815
commit
c3cd110688
@ -6,8 +6,8 @@ edition = "2024"
|
||||
[dependencies]
|
||||
risc0-zkvm = { version = "3.0.3", features = ['std'] }
|
||||
serde = { version = "1.0", default-features = false }
|
||||
thiserror = { version = "2.0.12", optional = true }
|
||||
bytemuck = { version = "1.13", optional = true }
|
||||
thiserror = { version = "2.0.12" }
|
||||
bytemuck = "1.13"
|
||||
chacha20 = { version = "0.9", default-features = false }
|
||||
k256 = { version = "0.13.3", optional = true }
|
||||
base58 = { version = "0.2.0", optional = true }
|
||||
@ -19,4 +19,4 @@ serde_json = "1.0.81"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
host = ["dep:bytemuck", "dep:k256", "dep:base58", "dep:anyhow"]
|
||||
host = ["dep:k256", "dep:base58", "dep:anyhow"]
|
||||
|
||||
@ -32,6 +32,7 @@ pub struct AccountWithMetadata {
|
||||
pub is_authorized: bool,
|
||||
pub account_id: AccountId,
|
||||
}
|
||||
|
||||
#[cfg(feature = "host")]
|
||||
impl AccountWithMetadata {
|
||||
pub fn new(account: Account, is_authorized: bool, account_id: impl Into<AccountId>) -> Self {
|
||||
@ -43,7 +44,7 @@ impl AccountWithMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize, Default)]
|
||||
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize)]
|
||||
#[cfg_attr(
|
||||
any(feature = "host", test),
|
||||
derive(Debug, Copy, PartialOrd, Ord)
|
||||
|
||||
@ -22,8 +22,8 @@ pub struct ProgramInput<T> {
|
||||
/// Each program can derive up to `2^256` unique account IDs by choosing different
|
||||
/// seeds. PDAs allow programs to control namespaced account identifiers without
|
||||
/// collisions between programs.
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
#[cfg_attr(any(feature = "host", test), derive(Debug, Eq))]
|
||||
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
|
||||
#[cfg_attr(any(feature = "host", test), derive(Debug))]
|
||||
pub struct PdaSeed([u8; 32]);
|
||||
|
||||
impl PdaSeed {
|
||||
@ -54,8 +54,8 @@ impl From<(&ProgramId, &PdaSeed)> for AccountId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
||||
#[cfg_attr(any(feature = "host", test), derive(Debug, Eq))]
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(any(feature = "host", test), derive(Debug,))]
|
||||
pub struct ChainedCall {
|
||||
/// The program ID of the program to execute
|
||||
pub program_id: ProgramId,
|
||||
|
||||
@ -73,7 +73,7 @@ struct PoolDefinition{
|
||||
}
|
||||
|
||||
impl PoolDefinition {
|
||||
fn into_data(self) -> Vec<u8> {
|
||||
fn into_data(self) -> Data {
|
||||
let mut bytes = [0; POOL_DEFINITION_DATA_SIZE];
|
||||
bytes[0..32].copy_from_slice(&self.definition_token_a_id.to_bytes());
|
||||
bytes[32..64].copy_from_slice(&self.definition_token_b_id.to_bytes());
|
||||
@ -85,7 +85,11 @@ impl PoolDefinition {
|
||||
bytes[192..208].copy_from_slice(&self.reserve_b.to_le_bytes());
|
||||
bytes[208..224].copy_from_slice(&self.fees.to_le_bytes());
|
||||
bytes[224] = self.active as u8;
|
||||
bytes.into()
|
||||
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("225 bytes should fit into Data")
|
||||
}
|
||||
|
||||
fn parse(data: &[u8]) -> Option<Self> {
|
||||
@ -144,14 +148,18 @@ struct TokenHolding {
|
||||
}
|
||||
|
||||
impl TokenDefinition {
|
||||
fn into_data(self) -> Vec<u8> {
|
||||
fn into_data(self) -> Data {
|
||||
let mut bytes = [0; TOKEN_DEFINITION_DATA_SIZE];
|
||||
bytes[0] = self.account_type;
|
||||
bytes[1..7].copy_from_slice(&self.name);
|
||||
bytes[7..].copy_from_slice(&self.total_supply.to_le_bytes());
|
||||
bytes.into()
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("23 bytes should fit into Data")
|
||||
}
|
||||
|
||||
|
||||
fn parse(data: &[u8]) -> Option<Self> {
|
||||
if data.len() != TOKEN_DEFINITION_DATA_SIZE || data[0] != TOKEN_DEFINITION_TYPE {
|
||||
None
|
||||
@ -209,17 +217,21 @@ impl TokenHolding {
|
||||
bytes[0] = self.account_type;
|
||||
bytes[1..33].copy_from_slice(&self.definition_id.to_bytes());
|
||||
bytes[33..].copy_from_slice(&self.balance.to_le_bytes());
|
||||
bytes.into()
|
||||
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("49 bytes should fit into Data")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type Instruction = Vec<u8>;
|
||||
fn main() {
|
||||
let ProgramInput {
|
||||
let (ProgramInput {
|
||||
pre_states,
|
||||
instruction,
|
||||
} = read_nssa_inputs::<Instruction>();
|
||||
}, instruction_words) = read_nssa_inputs::<Instruction>();
|
||||
|
||||
let (post_states, chained_calls) = match instruction[0] {
|
||||
0 => {
|
||||
@ -266,7 +278,7 @@ fn main() {
|
||||
_ => panic!("Invalid instruction"),
|
||||
};
|
||||
|
||||
write_nssa_outputs_with_chained_call(pre_states, post_states, chained_calls);
|
||||
write_nssa_outputs_with_chained_call(instruction_words, pre_states, post_states, chained_calls);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -2242,14 +2242,16 @@ pub mod tests {
|
||||
definition_id: AccountId,
|
||||
balance: u128,
|
||||
}
|
||||
|
||||
impl TokenDefinition {
|
||||
fn into_data(self) -> Vec<u8> {
|
||||
fn into_data(self) -> Data {
|
||||
let mut bytes = [0; TOKEN_DEFINITION_DATA_SIZE];
|
||||
bytes[0] = self.account_type;
|
||||
bytes[1..7].copy_from_slice(&self.name);
|
||||
bytes[7..].copy_from_slice(&self.total_supply.to_le_bytes());
|
||||
bytes.into()
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("23 bytes should fit into Data")
|
||||
}
|
||||
}
|
||||
|
||||
@ -2282,7 +2284,10 @@ pub mod tests {
|
||||
bytes[0] = self.account_type;
|
||||
bytes[1..33].copy_from_slice(&self.definition_id.to_bytes());
|
||||
bytes[33..].copy_from_slice(&self.balance.to_le_bytes());
|
||||
bytes.into()
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("33 bytes should fit into Data")
|
||||
}
|
||||
}
|
||||
|
||||
@ -2379,84 +2384,77 @@ pub mod tests {
|
||||
.expect("Hash output must be exactly 32 bytes long"),
|
||||
)
|
||||
}
|
||||
const POOL_DEFINITION_DATA_SIZE: usize = 225;
|
||||
|
||||
const POOL_DEFINITION_DATA_SIZE: usize = 225;
|
||||
#[derive(Default)]
|
||||
struct PoolDefinition{
|
||||
definition_token_a_id: AccountId,
|
||||
definition_token_b_id: AccountId,
|
||||
vault_a_id: AccountId,
|
||||
vault_b_id: AccountId,
|
||||
liquidity_pool_id: AccountId,
|
||||
liquidity_pool_supply: u128,
|
||||
reserve_a: u128,
|
||||
reserve_b: u128,
|
||||
fees: u128,
|
||||
active: bool
|
||||
}
|
||||
|
||||
struct PoolDefinition {
|
||||
definition_token_a_id: AccountId,
|
||||
definition_token_b_id: AccountId,
|
||||
vault_a_addr: AccountId,
|
||||
vault_b_addr: AccountId,
|
||||
liquidity_pool_id: AccountId,
|
||||
liquidity_pool_supply: u128,
|
||||
reserve_a: u128,
|
||||
reserve_b: u128,
|
||||
fees: u128,
|
||||
active: bool,
|
||||
impl PoolDefinition {
|
||||
fn into_data(self) -> Data {
|
||||
let mut bytes = [0; POOL_DEFINITION_DATA_SIZE];
|
||||
bytes[0..32].copy_from_slice(&self.definition_token_a_id.to_bytes());
|
||||
bytes[32..64].copy_from_slice(&self.definition_token_b_id.to_bytes());
|
||||
bytes[64..96].copy_from_slice(&self.vault_a_id.to_bytes());
|
||||
bytes[96..128].copy_from_slice(&self.vault_b_id.to_bytes());
|
||||
bytes[128..160].copy_from_slice(&self.liquidity_pool_id.to_bytes());
|
||||
bytes[160..176].copy_from_slice(&self.liquidity_pool_supply.to_le_bytes());
|
||||
bytes[176..192].copy_from_slice(&self.reserve_a.to_le_bytes());
|
||||
bytes[192..208].copy_from_slice(&self.reserve_b.to_le_bytes());
|
||||
bytes[208..224].copy_from_slice(&self.fees.to_le_bytes());
|
||||
bytes[224] = self.active as u8;
|
||||
|
||||
bytes
|
||||
.to_vec()
|
||||
.try_into()
|
||||
.expect("225 bytes should fit into Data")
|
||||
}
|
||||
|
||||
impl PoolDefinition {
|
||||
fn into_data(self) -> Vec<u8> {
|
||||
let mut bytes = [0; POOL_DEFINITION_DATA_SIZE];
|
||||
bytes[0..32].copy_from_slice(&self.definition_token_a_id.to_bytes());
|
||||
bytes[32..64].copy_from_slice(&self.definition_token_b_id.to_bytes());
|
||||
bytes[64..96].copy_from_slice(&self.vault_a_addr.to_bytes());
|
||||
bytes[96..128].copy_from_slice(&self.vault_b_addr.to_bytes());
|
||||
bytes[128..160].copy_from_slice(&self.liquidity_pool_id.to_bytes());
|
||||
bytes[160..176].copy_from_slice(&self.liquidity_pool_supply.to_le_bytes());
|
||||
bytes[176..192].copy_from_slice(&self.reserve_a.to_le_bytes());
|
||||
bytes[192..208].copy_from_slice(&self.reserve_b.to_le_bytes());
|
||||
bytes[208..224].copy_from_slice(&self.fees.to_le_bytes());
|
||||
bytes[224] = self.active as u8;
|
||||
bytes.into()
|
||||
}
|
||||
fn parse(data: &[u8]) -> Option<Self> {
|
||||
if data.len() != POOL_DEFINITION_DATA_SIZE {
|
||||
None
|
||||
} else {
|
||||
let definition_token_a_id = AccountId::new(data[0..32].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Token A definition"));
|
||||
let definition_token_b_id = AccountId::new(data[32..64].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Vault B definition"));
|
||||
let vault_a_id = AccountId::new(data[64..96].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Vault A"));
|
||||
let vault_b_id = AccountId::new(data[96..128].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Vault B"));
|
||||
let liquidity_pool_id = AccountId::new(data[128..160].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Token liquidity pool definition"));
|
||||
let liquidity_pool_supply = u128::from_le_bytes(data[160..176].try_into().expect("Parse data: The AMM program must be provided a valid u128 for liquidity cap"));
|
||||
let reserve_a = u128::from_le_bytes(data[176..192].try_into().expect("Parse data: The AMM program must be provided a valid u128 for reserve A balance"));
|
||||
let reserve_b = u128::from_le_bytes(data[192..208].try_into().expect("Parse data: The AMM program must be provided a valid u128 for reserve B balance"));
|
||||
let fees = u128::from_le_bytes(data[208..224].try_into().expect("Parse data: The AMM program must be provided a valid u128 for fees"));
|
||||
|
||||
fn parse(data: &[u8]) -> Option<Self> {
|
||||
if data.len() != POOL_DEFINITION_DATA_SIZE {
|
||||
None
|
||||
} else {
|
||||
let definition_token_a_id = AccountId::new(data[0..32].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Token A definition"));
|
||||
let definition_token_b_id = AccountId::new(data[32..64].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Vault B definition"));
|
||||
let vault_a_addr = AccountId::new(data[64..96].try_into().expect(
|
||||
"Parse data: The AMM program must be provided a valid AccountId for Vault A",
|
||||
));
|
||||
let vault_b_addr = AccountId::new(data[96..128].try_into().expect(
|
||||
"Parse data: The AMM program must be provided a valid AccountId for Vault B",
|
||||
));
|
||||
let liquidity_pool_id = AccountId::new(data[128..160].try_into().expect("Parse data: The AMM program must be provided a valid AccountId for Token liquidity pool definition"));
|
||||
let liquidity_pool_supply = u128::from_le_bytes(data[160..176].try_into().expect(
|
||||
"Parse data: The AMM program must be provided a valid u128 for liquidity cap",
|
||||
));
|
||||
let reserve_a = u128::from_le_bytes(data[176..192].try_into().expect("Parse data: The AMM program must be provided a valid u128 for reserve A balance"));
|
||||
let reserve_b = u128::from_le_bytes(data[192..208].try_into().expect("Parse data: The AMM program must be provided a valid u128 for reserve B balance"));
|
||||
let fees =
|
||||
u128::from_le_bytes(data[208..224].try_into().expect(
|
||||
"Parse data: The AMM program must be provided a valid u128 for fees",
|
||||
));
|
||||
|
||||
let active = match data[224] {
|
||||
0 => false,
|
||||
1 => true,
|
||||
_ => panic!(
|
||||
"Parse data: The AMM program must be provided a valid bool for active"
|
||||
),
|
||||
};
|
||||
|
||||
Some(Self {
|
||||
definition_token_a_id,
|
||||
definition_token_b_id,
|
||||
vault_a_addr,
|
||||
vault_b_addr,
|
||||
liquidity_pool_id,
|
||||
liquidity_pool_supply,
|
||||
reserve_a,
|
||||
reserve_b,
|
||||
fees,
|
||||
active,
|
||||
})
|
||||
}
|
||||
let active = match data[224] {
|
||||
0 => false,
|
||||
1 => true,
|
||||
_ => panic!("Parse data: The AMM program must be provided a valid bool for active"),
|
||||
};
|
||||
|
||||
Some(Self {
|
||||
definition_token_a_id,
|
||||
definition_token_b_id,
|
||||
vault_a_id,
|
||||
vault_b_id,
|
||||
liquidity_pool_id,
|
||||
liquidity_pool_supply,
|
||||
reserve_a,
|
||||
reserve_b,
|
||||
fees,
|
||||
active,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum AccountsEnum {
|
||||
UserTokenAHolding,
|
||||
@ -2688,8 +2686,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::PoolLPSupplyInit,
|
||||
@ -2787,8 +2785,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::PoolLPSupplyInit,
|
||||
@ -2846,8 +2844,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::PoolLPSupplyInit,
|
||||
@ -2905,8 +2903,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::TokenLPSupplyAdd,
|
||||
@ -2984,8 +2982,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::TokenLPSupplyRemove,
|
||||
@ -3073,8 +3071,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: 0,
|
||||
reserve_a: 0,
|
||||
@ -3130,8 +3128,8 @@ pub mod tests {
|
||||
data: PoolDefinition::into_data(PoolDefinition {
|
||||
definition_token_a_id: helper_id_constructor(IdEnum::TokenADefinitionId),
|
||||
definition_token_b_id: helper_id_constructor(IdEnum::TokenBDefinitionId),
|
||||
vault_a_addr: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_addr: helper_id_constructor(IdEnum::VaultBId),
|
||||
vault_a_id: helper_id_constructor(IdEnum::VaultAId),
|
||||
vault_b_id: helper_id_constructor(IdEnum::VaultBId),
|
||||
liquidity_pool_id: helper_id_constructor(IdEnum::TokenLPDefinitionId),
|
||||
liquidity_pool_supply: helper_balances_constructor(
|
||||
BalancesEnum::UserTokenAHoldingNewDef,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user