mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-09 04:21:22 +00:00
23a35c3767
* Store dht enrs on shutdown * Load enrs on startup and add tests * Remove enr_entries from behavior * Move all dht persisting logic to `NetworkService` * Move `PersistedDht` from eth2-libp2p to network crate * Add test to confirm dht persistence * Add logging * Remove extra call to beacon_chain persist * Expose only mutable `add_enr` method from behaviour * Fix tests * Fix merge errors
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
use eth2_libp2p::Enr;
|
|
use rlp;
|
|
use store::{DBColumn, Error as StoreError, SimpleStoreItem};
|
|
|
|
/// 32-byte key for accessing the `DhtEnrs`.
|
|
pub const DHT_DB_KEY: &str = "PERSISTEDDHTPERSISTEDDHTPERSISTE";
|
|
|
|
/// Wrapper around dht for persistence to disk.
|
|
pub struct PersistedDht {
|
|
pub enrs: Vec<Enr>,
|
|
}
|
|
|
|
impl SimpleStoreItem for PersistedDht {
|
|
fn db_column() -> DBColumn {
|
|
DBColumn::DhtEnrs
|
|
}
|
|
|
|
fn as_store_bytes(&self) -> Vec<u8> {
|
|
rlp::encode_list(&self.enrs)
|
|
}
|
|
|
|
fn from_store_bytes(bytes: &[u8]) -> Result<Self, StoreError> {
|
|
let rlp = rlp::Rlp::new(bytes);
|
|
let enrs: Vec<Enr> = rlp
|
|
.as_list()
|
|
.map_err(|e| StoreError::RlpError(format!("{}", e)))?;
|
|
Ok(PersistedDht { enrs })
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use eth2_libp2p::Enr;
|
|
use std::str::FromStr;
|
|
use std::sync::Arc;
|
|
use store::{MemoryStore, Store};
|
|
use types::Hash256;
|
|
use types::MinimalEthSpec;
|
|
#[test]
|
|
fn test_persisted_dht() {
|
|
let store = Arc::new(MemoryStore::<MinimalEthSpec>::open());
|
|
let enrs = vec![Enr::from_str("enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8").unwrap()];
|
|
let key = Hash256::from_slice(&DHT_DB_KEY.as_bytes());
|
|
store
|
|
.put(&key, &PersistedDht { enrs: enrs.clone() })
|
|
.unwrap();
|
|
let dht: PersistedDht = store.get(&key).unwrap().unwrap();
|
|
assert_eq!(dht.enrs, enrs);
|
|
}
|
|
}
|