| Crates.io | scrutiny_chain_blockchain_core |
| lib.rs | scrutiny_chain_blockchain_core |
| version | 0.1.1 |
| created_at | 2025-02-28 23:44:49.878258+00 |
| updated_at | 2025-02-28 23:44:49.878258+00 |
| description | AI-Enhanced Blockchain Security Analysis Platform Core Blockchain Functionality |
| homepage | |
| repository | https://github.com/robertfischer3/scrutiny-chain |
| max_upload_size | |
| id | 1573329 |
| size | 45,375 |
Core blockchain functionality and traits for the Scrutiny Chain blockchain security analysis platform.
The scrutiny_chain_blockchain_core crate provides the foundational abstractions and models for interacting with blockchain data in the Scrutiny Chain ecosystem. It defines key interfaces that other crates implement to ensure consistent interaction with various blockchain data sources.
The core of this crate is the BlockchainDataProvider trait, which defines the standard interface for accessing blockchain data:
use scrutiny_chain_blockchain_core::blockchain::BlockchainDataProvider;
use scrutiny_chain_common::types::{Address, Hash};
// Example usage with any provider implementing the trait
async fn check_contract(provider: &impl BlockchainDataProvider, address: &Address) {
let is_contract = provider.is_contract(address).await.unwrap();
if is_contract {
let contract = provider.get_contract(address).await.unwrap();
println!("Contract bytecode size: {}", contract.bytecode_size());
}
}
The crate defines standard models for blockchain data:
use scrutiny_chain_blockchain_core::models::Transaction;
use scrutiny_chain_common::types::{Address, Hash};
// Create a new transaction
let tx = Transaction::new(
Hash("0x123".to_string()),
Address("0xabc".to_string()),
Some(Address("0xdef".to_string())),
1000, // value
50, // gas price
21000, // gas limit
5, // nonce
vec![], // data
);
// Calculate total cost
let cost = tx.total_cost(); // value + (gas_price * gas_limit)
// Check if it's a contract creation
let is_creation = tx.is_contract_creation();
use scrutiny_chain_blockchain_core::models::SmartContract;
use scrutiny_chain_common::types::Address;
// Create a new smart contract
let contract = SmartContract::new(
Address("0x789".to_string()),
vec![1, 2, 3], // bytecode
Address("0xabc".to_string()), // creator
"0x123".to_string(), // creation tx
);
// Get contract details
let bytecode_size = contract.bytecode_size();
let age = contract.age_in_seconds();
use scrutiny_chain_blockchain_core::models::SecurityAnalysis;
use scrutiny_chain_common::types::RiskLevel;
use std::collections::HashMap;
// Create a security analysis result
let analysis = SecurityAnalysis {
risk_level: RiskLevel::High,
findings: vec!["Reentrancy vulnerability detected".to_string()],
metadata: HashMap::new(),
};
The crate defines specific error types for blockchain operations:
use scrutiny_chain_blockchain_core::blockchain::BlockchainError;
// Handle specific blockchain errors
match result {
Err(e) if e.downcast_ref::<BlockchainError>() == Some(&BlockchainError::ContractNotFound(_)) => {
println!("Not a contract address");
}
_ => { /* Handle other cases */ }
}
scrutiny_chain_common crate from the Scrutiny Chain workspaceAdd this crate as a dependency in your Cargo.toml:
[dependencies]
scrutiny_chain_blockchain-core = { path = "../scrutiny_chain_blockchain-core" }
cargo build -p scrutiny_chain_blockchain-core
cargo test -p scrutiny_chain_blockchain-core
cargo watch -x "test -p scrutiny_chain_blockchain-core"
Generate and view the documentation:
cargo doc -p scrutiny_chain_blockchain-core --no-deps --open
Here's a simplified example of implementing the BlockchainDataProvider trait:
use scrutiny_chain_blockchain_core::blockchain::{BlockchainDataProvider, BlockchainError};
use scrutiny_chain_blockchain_core::models::{Transaction, SmartContract, SecurityAnalysis};
use scrutiny_chain_common::types::{Address, Hash, TimeRange};
use scrutiny_chain_common::error::Result;
use async_trait::async_trait;
use std::collections::HashMap;
struct EthereumProvider {
// Configuration and client details
}
#[async_trait]
impl BlockchainDataProvider for EthereumProvider {
async fn get_transaction(&self, hash: &Hash) -> Result<Transaction> {
// Implementation to fetch transaction from Ethereum
}
async fn get_contract(&self, address: &Address) -> Result<SmartContract> {
// Implementation to fetch contract from Ethereum
}
// Implement other required methods...
}