Refactored into multiple modules
This commit is contained in:
45
crates/oracle-demo/src/oracle/node.rs
Normal file
45
crates/oracle-demo/src/oracle/node.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{utils, AssetPair, OracleId, PriceAttestation};
|
||||
|
||||
pub(crate) struct OracleNode {
|
||||
pub(crate) id: OracleId,
|
||||
pub(crate) crdt: Arc<Mutex<super::OracleNetworkCRDT>>,
|
||||
pub(crate) is_byzantine: bool,
|
||||
pub(crate) base_price: f64,
|
||||
}
|
||||
|
||||
impl OracleNode {
|
||||
pub(crate) fn new(id: String, is_byzantine: bool) -> Self {
|
||||
Self {
|
||||
id: OracleId(id),
|
||||
crdt: Arc::new(Mutex::new(super::OracleNetworkCRDT::new())),
|
||||
is_byzantine,
|
||||
base_price: 2500.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn submit_price(&self) {
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let price = if self.is_byzantine {
|
||||
self.base_price * 1.2 // Try to manipulate 20% higher
|
||||
} else {
|
||||
self.base_price * (1.0 + rng.random_range(-0.01..0.01))
|
||||
};
|
||||
|
||||
let attestation = PriceAttestation {
|
||||
id: format!("{}_{}", self.id.0, utils::timestamp()),
|
||||
oracle_id: self.id.clone(),
|
||||
asset_pair: AssetPair("ETH/USD".to_string()),
|
||||
price,
|
||||
confidence: if self.is_byzantine { 50 } else { 95 },
|
||||
timestamp: utils::timestamp(),
|
||||
};
|
||||
|
||||
let mut crdt = self.crdt.lock().unwrap();
|
||||
crdt.submit_attestation(attestation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user