#[cfg(test)]
mod tests {
use anya_core::layer2::{
create_protocol_state, create_validation_result, create_verification_result, AssetParams,
AssetTransfer, Layer2ProtocolTrait, Proof, ProtocolState, TransactionStatus,
TransferResult, ValidationResult, VerificationResult,
};
use std::sync::Arc;
struct TestProtocol;
impl Layer2ProtocolTrait for TestProtocol {
fn initialize(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(())
}
fn get_state(&self) -> Result<ProtocolState, Box<dyn std::error::Error + Send + Sync>> {
Ok(create_protocol_state("1.0", 0, None, true))
}
fn submit_transaction(
&self,
_tx_data: &[u8],
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
Ok("tx_id".to_string())
}
fn check_transaction_status(
&self,
_tx_id: &str,
) -> Result<TransactionStatus, Box<dyn std::error::Error + Send + Sync>> {
Ok(TransactionStatus::Confirmed)
}
fn sync_state(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(())
}
fn issue_asset(
&self,
_params: AssetParams,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
Ok("asset_id".to_string())
}
fn transfer_asset(
&self,
_transfer: AssetTransfer,
) -> Result<TransferResult, Box<dyn std::error::Error + Send + Sync>> {
Ok(TransferResult {
tx_id: "transfer_id".to_string(),
status: TransactionStatus::Confirmed,
fee: Some(1000),
timestamp: 0,
})
}
fn verify_proof(
&self,
_proof: Proof,
) -> Result<VerificationResult, Box<dyn std::error::Error + Send + Sync>> {
Ok(create_verification_result(true, None))
}
fn validate_state(
&self,
_state_data: &[u8],
) -> Result<ValidationResult, Box<dyn std::error::Error + Send + Sync>> {
Ok(create_validation_result(true, vec![]))
}
}
#[test]
fn test_custom_protocol() {
let protocol = TestProtocol;
let result = protocol.initialize();
assert!(result.is_ok());
let state_result = protocol.get_state();
assert!(state_result.is_ok());
}
#[test]
fn test_arc_protocol() {
let protocol = Arc::new(TestProtocol);
let result = protocol.initialize();
assert!(result.is_ok());
let state_result = protocol.get_state();
assert!(state_result.is_ok());
}
}