Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add temporary flag to disable storing headers #1668

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/trin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ pub struct TrinConfig {
value_parser = max_radius_parser,
)]
pub max_radius: Distance,

#[arg(
long = "disable-history-storage",
help = "Disable storing all history data locally. This is a temporary flag used for upgrading the network, and should be removed once the upgrade is complete.",
default_value = "false"
)]
pub disable_history_storage: bool,
}

impl Default for TrinConfig {
Expand Down Expand Up @@ -249,6 +256,7 @@ impl Default for TrinConfig {
network: MAINNET.clone(),
max_radius: max_radius_parser(DEFAULT_MAX_RADIUS)
.expect("Parsing static DEFAULT_MAX_RADIUS to work"),
disable_history_storage: false,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions bin/trin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub async fn run_trin(
portalnet_config.clone(),
storage_config_factory.create(&Subnetwork::History, trin_config.max_radius)?,
header_oracle.clone(),
trin_config.disable_history_storage,
)
.await?
} else {
Expand Down
2 changes: 2 additions & 0 deletions crates/subnetworks/history/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub async fn initialize_history_network(
portalnet_config: PortalnetConfig,
storage_config: PortalStorageConfig,
header_oracle: Arc<RwLock<HeaderOracle>>,
disable_history_storage: bool,
) -> anyhow::Result<(
HistoryHandler,
HistoryNetworkTask,
Expand All @@ -58,6 +59,7 @@ pub async fn initialize_history_network(
storage_config,
portalnet_config.clone(),
header_oracle,
disable_history_storage,
)
.await?;
let event_stream = history_network.overlay.event_stream().await?;
Expand Down
6 changes: 5 additions & 1 deletion crates/subnetworks/history/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl HistoryNetwork {
storage_config: PortalStorageConfig,
portal_config: PortalnetConfig,
header_oracle: Arc<RwLock<HeaderOracle>>,
disable_history_storage: bool,
) -> anyhow::Result<Self> {
let config = OverlayConfig {
bootnode_enrs: portal_config.bootnodes,
Expand All @@ -54,7 +55,10 @@ impl HistoryNetwork {
utp_transfer_limit: portal_config.utp_transfer_limit,
..Default::default()
};
let storage = Arc::new(PLRwLock::new(HistoryStorage::new(storage_config)?));
let storage = Arc::new(PLRwLock::new(HistoryStorage::new(
storage_config,
disable_history_storage,
)?));
let validator = Arc::new(ChainHistoryValidator { header_oracle });
let ping_extensions = Arc::new(HistoryPingExtensions {});
let overlay = OverlayProtocol::new(
Expand Down
15 changes: 12 additions & 3 deletions crates/subnetworks/history/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use trin_storage::{
#[derive(Debug)]
pub struct HistoryStorage {
store: IdIndexedV1Store<HistoryContentKey>,
disable_history_storage: bool,
}

impl ContentStore for HistoryStorage {
Expand All @@ -35,6 +36,10 @@ impl ContentStore for HistoryStorage {
key: &HistoryContentKey,
) -> Result<ShouldWeStoreContent, ContentStoreError> {
let content_id = ContentId::from(key.content_id());
// temporarily disable storing all history network
if self.disable_history_storage {
return Ok(ShouldWeStoreContent::NotWithinRadius);
}
if self.store.distance_to_content_id(&content_id) > self.store.radius() {
Ok(ShouldWeStoreContent::NotWithinRadius)
} else if self.store.has_content(&content_id)? {
Expand All @@ -50,11 +55,15 @@ impl ContentStore for HistoryStorage {
}

impl HistoryStorage {
pub fn new(config: PortalStorageConfig) -> Result<Self, ContentStoreError> {
pub fn new(
config: PortalStorageConfig,
disable_history_storage: bool,
) -> Result<Self, ContentStoreError> {
let sql_connection_pool = config.sql_connection_pool.clone();
let config = IdIndexedV1StoreConfig::new(ContentType::History, Subnetwork::History, config);
Ok(Self {
store: create_store(ContentType::History, config, sql_connection_pool)?,
disable_history_storage,
})
}

Expand Down Expand Up @@ -97,7 +106,7 @@ pub mod test {
fn test_store_random_bytes() -> TestResult {
let (temp_dir, storage_config) =
create_test_portal_storage_config_with_capacity(CAPACITY_MB).unwrap();
let mut storage = HistoryStorage::new(storage_config).unwrap();
let mut storage = HistoryStorage::new(storage_config, false).unwrap();
let content_key = HistoryContentKey::random().unwrap();
let mut value = [0u8; 32];
rand::thread_rng().fill_bytes(&mut value);
Expand All @@ -118,7 +127,7 @@ pub mod test {
async fn test_get_data() -> Result<(), ContentStoreError> {
let (temp_dir, storage_config) =
create_test_portal_storage_config_with_capacity(CAPACITY_MB).unwrap();
let mut storage = HistoryStorage::new(storage_config)?;
let mut storage = HistoryStorage::new(storage_config, false)?;
let content_key = HistoryContentKey::BlockHeaderByHash(BlockHeaderByHashKey::default());
let value: Vec<u8> = "OGFWs179fWnqmjvHQFGHszXloc3Wzdb4".into();
storage.put(content_key.clone(), &value)?;
Expand Down