|
| 1 | +//! BorNode: wires all components together. |
| 2 | +
|
| 3 | +use alloy_primitives::{Address, B256}; |
| 4 | +use bor_chainspec::BorChainSpec; |
| 5 | +use bor_consensus::BorSnapshot; |
| 6 | +use bor_storage::persistence::{InMemorySpanStore, InMemorySnapshotStore, SpanStore, SnapshotStore}; |
| 7 | +use crate::config::{BorNodeConfig, BorNetwork}; |
| 8 | +use std::sync::{Arc, RwLock}; |
| 9 | + |
| 10 | +/// The assembled Bor node with all components wired together. |
| 11 | +pub struct BorNode { |
| 12 | + /// Node configuration. |
| 13 | + pub config: BorNodeConfig, |
| 14 | + /// Chain specification. |
| 15 | + pub chain_spec: Arc<BorChainSpec>, |
| 16 | + /// Span store. |
| 17 | + pub span_store: Arc<RwLock<InMemorySpanStore>>, |
| 18 | + /// Snapshot store. |
| 19 | + pub snapshot_store: Arc<RwLock<InMemorySnapshotStore>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl BorNode { |
| 23 | + /// Create a new BorNode from configuration. |
| 24 | + pub fn new(config: BorNodeConfig) -> eyre::Result<Self> { |
| 25 | + let chain_spec = match config.network { |
| 26 | + BorNetwork::Mainnet => Arc::new(bor_chainspec::bor_mainnet_genesis()), |
| 27 | + BorNetwork::Amoy => Arc::new(bor_chainspec::bor_amoy_genesis()), |
| 28 | + }; |
| 29 | + |
| 30 | + let span_store = Arc::new(RwLock::new(InMemorySpanStore::new())); |
| 31 | + let snapshot_store = Arc::new(RwLock::new(InMemorySnapshotStore::new())); |
| 32 | + |
| 33 | + Ok(Self { |
| 34 | + config, |
| 35 | + chain_spec, |
| 36 | + span_store, |
| 37 | + snapshot_store, |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + /// Get the chain ID. |
| 42 | + pub fn chain_id(&self) -> u64 { |
| 43 | + self.config.chain_id() |
| 44 | + } |
| 45 | + |
| 46 | + /// Store a snapshot. |
| 47 | + pub fn put_snapshot(&self, block_hash: B256, snapshot: &BorSnapshot) -> eyre::Result<()> { |
| 48 | + let data = snapshot.encode(); |
| 49 | + let mut store = self.snapshot_store.write().map_err(|e| eyre::eyre!("{e}"))?; |
| 50 | + store.put_snapshot(block_hash.0, data); |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + /// Get a snapshot by block hash. |
| 55 | + pub fn get_snapshot(&self, block_hash: &B256) -> eyre::Result<Option<BorSnapshot>> { |
| 56 | + let store = self.snapshot_store.read().map_err(|e| eyre::eyre!("{e}"))?; |
| 57 | + match store.get_snapshot(&block_hash.0) { |
| 58 | + Some(data) => { |
| 59 | + let snap = BorSnapshot::decode(&data)?; |
| 60 | + Ok(Some(snap)) |
| 61 | + } |
| 62 | + None => Ok(None), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use super::*; |
| 70 | + use bor_primitives::{Validator, ValidatorSet}; |
| 71 | + |
| 72 | + fn test_validator_set() -> ValidatorSet { |
| 73 | + ValidatorSet { |
| 74 | + validators: vec![Validator { |
| 75 | + id: 1, |
| 76 | + address: Address::new([0xaa; 20]), |
| 77 | + voting_power: 100, |
| 78 | + signer: Address::new([0xaa; 20]), |
| 79 | + proposer_priority: 0, |
| 80 | + }], |
| 81 | + proposer: None, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_node_builder_compiles() { |
| 87 | + let config = BorNodeConfig::amoy(); |
| 88 | + let node = BorNode::new(config).unwrap(); |
| 89 | + assert_eq!(node.chain_id(), 80002); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_node_mainnet() { |
| 94 | + let config = BorNodeConfig::mainnet(); |
| 95 | + let node = BorNode::new(config).unwrap(); |
| 96 | + assert_eq!(node.chain_id(), 137); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_snapshot_roundtrip() { |
| 101 | + let config = BorNodeConfig::amoy(); |
| 102 | + let node = BorNode::new(config).unwrap(); |
| 103 | + |
| 104 | + let hash = B256::from([0xab; 32]); |
| 105 | + let snapshot = BorSnapshot::new(100, hash, test_validator_set()); |
| 106 | + |
| 107 | + node.put_snapshot(hash, &snapshot).unwrap(); |
| 108 | + |
| 109 | + let retrieved = node.get_snapshot(&hash).unwrap().unwrap(); |
| 110 | + assert_eq!(retrieved.number, 100); |
| 111 | + assert_eq!(retrieved.validator_set.validators.len(), 1); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_snapshot_not_found() { |
| 116 | + let config = BorNodeConfig::amoy(); |
| 117 | + let node = BorNode::new(config).unwrap(); |
| 118 | + let result = node.get_snapshot(&B256::ZERO).unwrap(); |
| 119 | + assert!(result.is_none()); |
| 120 | + } |
| 121 | +} |
0 commit comments