Skip to main content

rialo_types/
lib.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This crate contains basic types used by Rialo network.
5//! When a type is referenced by multiple crates, it is a
6//! good idea to have it in crate for simpler dependency management.
7//!
8//! This crate now includes REX-specific types with TEE attestation support.
9
10/// Transaction validity window in milliseconds.
11///
12/// A transaction is valid from its `valid_from` timestamp until `valid_from + TRANSACTION_VALIDITY_WINDOW_SECS`.
13/// This value is used by both the consensus layer (to filter invalid transactions before proposal)
14/// and the execution layer (to reject transactions during processing).
15///
16/// The value of 120 seconds (2 minutes) provides a reasonable window for:
17/// - Client clock drift tolerance
18/// - Network propagation delays
19/// - Transaction retry attempts
20pub const TRANSACTION_VALIDITY_WINDOW_MS: u64 = 120 * 1000;
21
22/// A random seed value derived from consensus.
23///
24/// This is a newtype wrapper around `u64` that provides type safety for random seed values
25/// used in block execution and transaction processing.
26#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, serde::Serialize, serde::Deserialize)]
27pub struct RandomSeed(pub [u8; 32]);
28
29impl From<[u8; 32]> for RandomSeed {
30    fn from(value: [u8; 32]) -> Self {
31        Self(value)
32    }
33}
34
35impl From<u64> for RandomSeed {
36    fn from(value: u64) -> Self {
37        Self((0..4).fold([0u8; 32], |mut acc, i| {
38            acc[i * 8..(i + 1) * 8].copy_from_slice(&value.to_le_bytes());
39            acc
40        }))
41    }
42}
43
44impl From<RandomSeed> for u64 {
45    fn from(seed: RandomSeed) -> Self {
46        u64::from_be_bytes(seed.0[0..8].try_into().expect("to succeed"))
47    }
48}
49
50// Admin transaction types (only available in non-pdk builds)
51#[cfg(feature = "non-pdk")]
52pub mod admin;
53
54// Transaction validity utilities (only available in non-pdk builds)
55#[cfg(feature = "non-pdk")]
56pub mod validity;
57
58// Consensus cryptographic types (only available in non-pdk builds)
59#[cfg(feature = "non-pdk")]
60pub mod consensus_crypto;
61
62// Core cryptographic types
63pub mod crypto;
64
65pub mod headers;
66pub mod http_filter;
67#[cfg(test)]
68mod http_filter_tests;
69#[cfg(feature = "non-pdk")]
70pub mod multiaddr;
71pub mod nonce;
72#[cfg(test)]
73mod nonce_tests;
74mod prelude;
75pub mod shared_rpc_types;
76pub mod transaction_execution_results;
77
78// REX-specific modules
79pub mod attestation;
80pub mod duties;
81pub mod duty_config;
82pub mod error;
83pub mod modified_entries;
84pub mod output;
85pub mod rex;
86pub mod rex_info;
87pub mod tee_types;
88#[cfg(test)]
89mod tests;
90pub mod websocket;
91pub mod websocket_op;
92
93// Re-export REX types
94// Re-export admin types (only available in non-pdk builds)
95#[cfg(feature = "non-pdk")]
96pub use admin::{
97    attestation_threshold, validate_attestation_consistency, AdminTransaction, AttestableRecord,
98    Attestation, AttestationError, Blake3Hash, Certificate, CertificateAddAttestationError,
99    CertificateAttestationConsistencyError, CommitteeAttestedRecord, EpochChangeConfig,
100    EpochConsensusConfig, EpochIdentifier, HandoverAttestation, HandoverCertificate,
101    HandoverCertificateAddAttestationError, HandoverChain, HandoverChainError, HandoverRecord,
102    RecordDigest, SnapshotAttestation, SnapshotAttestationSubmission,
103    SnapshotAttestationSubmissionError, SnapshotCertificate,
104    SnapshotCertificateAddAttestationError, SnapshotRecord, ValidatorInfo, ValidatorReadyMessage,
105    ATTESTATION_THRESHOLD_DENOMINATOR, ATTESTATION_THRESHOLD_NUMERATOR, BLAKE3_HASH_SIZE,
106    BLS12381_PUBLIC_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, READY_MESSAGE_SIGNING_SIZE,
107};
108// Re-export consensus crypto types (only available in non-pdk builds)
109#[cfg(feature = "non-pdk")]
110pub use attestation::{ed25519_keypair_to_pubkey_slice, ed25519_signature_to_slice};
111pub use attestation::{
112    AttestationReport, AttestationReportInner, VerificationError, VerifiedAttestation,
113};
114#[cfg(feature = "non-pdk")]
115pub use consensus_crypto::{
116    AuthorityPublicKey, NetworkKeyPair, NetworkPrivateKey, NetworkPublicKey, ProtocolKeyPair,
117    ProtocolKeySignature, ProtocolPublicKey,
118};
119// Re-export crypto types
120pub use crypto::{PublicKey, TeeUserData, TEE_USER_DATA_SIZE, X25519_KEY_SIZE};
121pub use duties::{AuthorityKeyBytes, RexDutiesMap, RexDutiesMapKey, RexDutiesMapValue};
122pub use duty_config::*;
123pub use error::*;
124pub use modified_entries::*;
125// Re-export multiaddr types
126#[cfg(feature = "non-pdk")]
127pub use multiaddr::{Multiaddr, Protocol};
128pub use output::*;
129/// Re-export for convenience
130pub use prelude::*;
131pub use rex_info::*;
132pub use tee_types::{
133    parse_evidence, AttestationEvidence, AwsSevSnpEvidenceData, AzureSevSnpEvidenceData,
134    SevSnpEvidenceData, TeeEvidenceError, TeeType,
135};
136pub use websocket_op::*;
137
138/// A wrapper enum to distinguish different types of messages processed in consensus.
139#[cfg(feature = "non-pdk")]
140#[derive(serde::Deserialize, serde::Serialize)]
141pub enum ConsensusMessage {
142    RexUpdateResult(Box<RexUpdateResult>),
143    VersionedTransaction(rialo_s_sdk::transaction::VersionedTransaction),
144    /// Admin transactions for privileged operations like epoch changes.
145    AdminTransaction(Box<AdminTransaction>),
146}
147
148// Re-export validity functions for convenience
149// Re-export test helpers when testing feature is enabled
150#[cfg(feature = "testing")]
151pub use validity::{
152    create_admin_transaction, create_rex_update_with_target_timestamp,
153    create_versioned_transaction_with_timestamp,
154};
155#[cfg(feature = "non-pdk")]
156pub use validity::{
157    extract_valid_from, extract_valid_until, is_expired, is_not_yet_valid, is_too_far_in_future,
158    MAX_FUTURE_VALID_FROM_MS,
159};