Skip to content
Merged

test #15

Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion examples/basic_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn main() -> Result<(), InterLiquidSdkError> {
let savedata = SaveData {
chain_id: "test-chain".to_string(),
block_height: 1,
block_time_unix_secs: 0,
block_time: 0,
state_sparse_tree_root: [0; 32],
keys_patricia_trie_root: [0; 32],
tx_snapshots: vec![],
Expand Down
7 changes: 5 additions & 2 deletions src/core/block/block.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::sha2::{Digest, Sha256};
use crate::{
sha2::{Digest, Sha256},
types::Timestamp,
};
use borsh_derive::{BorshDeserialize, BorshSerialize};

/// `Header` is the struct for block headers.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub struct Header {
pub chain_id: u64,
pub height: u64,
pub time: u64,
pub time: Timestamp,

pub header_hash_prev: [u8; 32],

Expand Down
30 changes: 15 additions & 15 deletions src/runner/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use borsh_derive::{BorshDeserialize, BorshSerialize};

use crate::zkp::WitnessTx;
use crate::{types::Timestamp, zkp::WitnessTx};

/// Represents all possible message types that can be sent between components in the runner system.
/// These messages coordinate the transaction processing and proof generation pipeline.
Expand All @@ -27,7 +27,7 @@ pub struct MessageTxReceived {

impl MessageTxReceived {
/// Creates a new MessageTxReceived instance.
///
///
/// # Arguments
/// * `tx` - The serialized transaction data
pub fn new(tx: Vec<u8>) -> Self {
Expand All @@ -41,31 +41,31 @@ impl MessageTxReceived {
pub struct MessageTxProofReady {
pub chain_id: String,
pub block_height: u64,
pub block_time_unix_secs: u64,
pub block_time: Timestamp,
pub tx_index: usize,
pub witness: WitnessTx,
}

impl MessageTxProofReady {
/// Creates a new MessageTxProofReady instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block containing the transaction
/// * `block_time_unix_secs` - The Unix timestamp of the block
/// * `block_time` - The Unix timestamp of the block
/// * `tx_index` - The index of the transaction within the block
/// * `witness` - The witness data needed for proving
pub fn new(
chain_id: String,
block_height: u64,
block_time_unix_secs: u64,
block_time: Timestamp,
tx_index: usize,
witness: WitnessTx,
) -> Self {
Self {
chain_id,
block_height,
block_time_unix_secs,
block_time,
tx_index,
witness,
}
Expand All @@ -92,7 +92,7 @@ pub struct MessageCommitStateProofReady {

impl MessageCommitStateProofReady {
/// Creates a new MessageCommitStateProofReady instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand All @@ -117,7 +117,7 @@ pub struct MessageCommitKeysProofReady {

impl MessageCommitKeysProofReady {
/// Creates a new MessageCommitKeysProofReady instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand All @@ -140,7 +140,7 @@ pub struct MessageBlockCommitted {

impl MessageBlockCommitted {
/// Creates a new MessageBlockCommitted instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the committed block
Expand All @@ -164,7 +164,7 @@ pub struct MessageTxProved {

impl MessageTxProved {
/// Creates a new MessageTxProved instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block containing the transaction
Expand Down Expand Up @@ -192,7 +192,7 @@ pub struct MessageTxProofAggregated {

impl MessageTxProofAggregated {
/// Creates a new MessageTxProofAggregated instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand Down Expand Up @@ -223,7 +223,7 @@ pub struct MessageCommitStateProved {

impl MessageCommitStateProved {
/// Creates a new MessageCommitStateProved instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand All @@ -247,7 +247,7 @@ pub struct MessageCommitKeysProved {

impl MessageCommitKeysProved {
/// Creates a new MessageCommitKeysProved instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand All @@ -272,7 +272,7 @@ pub struct MessageBlockProved {

impl MessageBlockProved {
/// Creates a new MessageBlockProved instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand Down
15 changes: 9 additions & 6 deletions src/runner/savedata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use borsh_derive::{BorshDeserialize, BorshSerialize};

use crate::state::{AccumulatedLogs, StateLog};
use crate::{
state::{AccumulatedLogs, StateLog},
types::Timestamp,
};

/// Represents a snapshot of state changes after executing a transaction.
/// Contains both the individual state logs and accumulated logs for verification.
Expand All @@ -12,7 +15,7 @@ pub struct TxExecutionSnapshot {

impl TxExecutionSnapshot {
/// Creates a new TxExecutionSnapshot instance.
///
///
/// # Arguments
/// * `logs` - List of individual state changes made by the transaction
/// * `accum_logs` - Accumulated state logs for merkle proof generation
Expand All @@ -28,15 +31,15 @@ impl TxExecutionSnapshot {
pub struct SaveData {
pub chain_id: String,
pub block_height: u64,
pub block_time_unix_secs: u64,
pub block_time: Timestamp,
pub state_sparse_tree_root: [u8; 32],
pub keys_patricia_trie_root: [u8; 32],
pub tx_snapshots: Vec<TxExecutionSnapshot>,
}

impl SaveData {
/// Creates a new SaveData instance.
///
///
/// # Arguments
/// * `chain_id` - The identifier of the blockchain
/// * `block_height` - The height of the block
Expand All @@ -47,15 +50,15 @@ impl SaveData {
pub fn new(
chain_id: String,
block_height: u64,
block_time_unix_secs: u64,
block_time: Timestamp,
state_sparse_tree_root: [u8; 32],
keys_patricia_trie_root: [u8; 32],
tx_snapshots: Vec<TxExecutionSnapshot>,
) -> Self {
Self {
chain_id,
block_height,
block_time_unix_secs,
block_time,
state_sparse_tree_root,
keys_patricia_trie_root,
tx_snapshots,
Expand Down
20 changes: 10 additions & 10 deletions src/runner/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct SequencerState<TX: Tx, S: StateManager> {

impl<TX: Tx, S: StateManager> SequencerState<TX, S> {
/// Creates a new SequencerState instance.
///
///
/// # Arguments
/// * `app` - The application instance containing business logic
/// * `savedata` - Persistent storage for blockchain data
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<TX: Tx, S: StateManager> Clone for SequencerState<TX, S> {

/// The Sequencer component responsible for processing transactions.
/// It executes transactions, updates state, and prepares witness data for proof generation.
///
///
/// # Type Parameters
/// * `TX` - Transaction type that implements the Tx trait
/// * `S` - State manager type that implements the StateManager trait
Expand All @@ -72,7 +72,7 @@ pub struct Sequencer<TX: Tx, S: StateManager> {

impl<TX: Tx, S: StateManager> Sequencer<TX, S> {
/// Creates a new Sequencer instance.
///
///
/// # Arguments
/// * `state` - The sequencer state containing app, storage, and state manager
/// * `sender` - Channel sender for broadcasting messages to other components
Expand All @@ -90,9 +90,9 @@ impl<TX: Tx, S: StateManager> Sequencer<TX, S> {
}

/// Runs the sequencer's main event loop.
///
///
/// Listens for incoming messages and processes transactions when received.
///
///
/// # Returns
/// * `Ok(())` - If the sequencer runs successfully
/// * `Err(InterLiquidSdkError)` - If an error occurs during processing
Expand All @@ -112,17 +112,17 @@ impl<TX: Tx, S: StateManager> Sequencer<TX, S> {
}

/// Handles a received transaction by executing it and generating witness data.
///
///
/// This method:
/// 1. Executes the transaction against the current state
/// 2. Collects state changes and logs
/// 3. Generates witness data for proof generation
/// 4. Updates the savedata with the execution snapshot
/// 5. Sends a message that the transaction is ready for proving
///
///
/// # Arguments
/// * `tx` - The serialized transaction data to process
///
///
/// # Returns
/// * `Ok(())` - If the transaction is processed successfully
/// * `Err(InterLiquidSdkError)` - If an error occurs during execution
Expand All @@ -149,7 +149,7 @@ impl<TX: Tx, S: StateManager> Sequencer<TX, S> {
let env = Environment::new(
savedata.chain_id.clone(),
savedata.block_height,
savedata.block_time_unix_secs,
savedata.block_time,
);

let mut ctx = SdkContext::new(env, &mut transactional);
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<TX: Tx, S: StateManager> Sequencer<TX, S> {
.send(RunnerMessage::TxProofReady(MessageTxProofReady::new(
savedata.chain_id.clone(),
savedata.block_height,
savedata.block_time_unix_secs,
savedata.block_time,
savedata.tx_snapshots.len() - 1,
witness,
)))
Expand Down
33 changes: 33 additions & 0 deletions src/types/address.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
/// Represents a 32-byte address used throughout the InterLiquid SDK.
/// This is typically used to identify accounts, contracts, or other entities.
pub type Address = [u8; 32];

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_address_creation() {
let addr: Address = [0u8; 32];
assert_eq!(addr.len(), 32);
assert!(addr.iter().all(|&b| b == 0));
}

#[test]
fn test_address_with_values() {
let mut addr: Address = [0u8; 32];
addr[0] = 1;
addr[31] = 255;

assert_eq!(addr[0], 1);
assert_eq!(addr[31], 255);
assert_eq!(addr[1], 0);
}

#[test]
fn test_address_comparison() {
let addr1: Address = [1u8; 32];
let addr2: Address = [1u8; 32];
let addr3: Address = [2u8; 32];

assert_eq!(addr1, addr2);
assert_ne!(addr1, addr3);
}
}
Loading
Loading