use std::collections::HashMap;
use std::sync::{Arc, RwLock};
#[derive(Debug, Clone)]
pub struct NodeConfig {
pub data_dir: String,
pub network: bitcoin::Network,
pub bind_address: String,
pub bind_port: u16,
}
impl Default for NodeConfig {
fn default() -> Self {
Self {
data_dir: "./rgb-data".to_string(),
network: bitcoin::Network::Testnet,
bind_address: "127.0.0.1".to_string(),
bind_port: 3000,
}
}
}
#[derive(Debug)]
pub struct RGBNode {
config: NodeConfig,
contracts: Arc<RwLock<HashMap<String, String>>>, }
impl Default for RGBNode {
fn default() -> Self {
Self::new()
}
}
impl RGBNode {
pub fn new() -> Self {
Self {
config: NodeConfig::default(),
contracts: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn with_config(config: NodeConfig) -> Self {
Self {
config,
contracts: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn start(&self) -> Result<(), &'static str> {
println!(
"Starting RGB node on {}:{}",
self.config.bind_address, self.config.bind_port
);
Ok(())
}
pub fn stop(&self) -> Result<(), &'static str> {
println!("Stopping RGB node");
Ok(())
}
pub fn register_contract(
&self,
contract_id: &str,
contract_data: &str,
) -> Result<(), &'static str> {
if let Ok(mut contracts) = self.contracts.write() {
contracts.insert(contract_id.to_string(), contract_data.to_string());
Ok(())
} else {
Err("Failed to acquire write lock on contracts")
}
}
pub fn get_contract(&self, contract_id: &str) -> Result<String, &'static str> {
if let Ok(contracts) = self.contracts.read() {
if let Some(contract_data) = contracts.get(contract_id) {
Ok(contract_data.clone())
} else {
Err("Contract not found")
}
} else {
Err("Failed to acquire read lock on contracts")
}
}
}