minotari_node 5.4.0-pre.0

The tari full base node implementation
// Copyright 2025. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, Instant},
};

use log::debug;
use tari_node_components::blocks::Block;
use tokio::sync::RwLock;

const LOG_TARGET: &str = "minotari::base_node::xmrig_proxy::storage";
const MAX_TEMPLATE_AGE: Duration = Duration::from_secs(20 * 60); // 20 minutes

struct TemplateEntry {
    block: Block,
    inserted_at: Instant,
}

/// Thread-safe in-memory store for block templates, keyed by the 32-byte mining hash.
///
/// Templates are automatically expired after [`MAX_TEMPLATE_AGE`].
#[derive(Clone)]
pub struct BlockTemplateStorage {
    inner: Arc<RwLock<HashMap<[u8; 32], TemplateEntry>>>,
}

impl BlockTemplateStorage {
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Store a block template. If a template with the same key already exists it is replaced.
    pub async fn store(&self, key: [u8; 32], block: Block) {
        let mut map = self.inner.write().await;
        map.insert(key, TemplateEntry {
            block,
            inserted_at: Instant::now(),
        });
        debug!(target: LOG_TARGET, "Stored template, total templates={}", map.len());
    }

    /// Retrieve and remove a block template by its mining hash key.
    pub async fn take(&self, key: &[u8; 32]) -> Option<Block> {
        let mut map = self.inner.write().await;
        map.remove(key).map(|e| e.block)
    }

    /// Remove all templates older than [`MAX_TEMPLATE_AGE`].
    pub async fn remove_outdated(&self) {
        let now = Instant::now();
        let mut map = self.inner.write().await;
        let before = map.len();
        map.retain(|_, e| now.duration_since(e.inserted_at) < MAX_TEMPLATE_AGE);
        let removed = before.saturating_sub(map.len());
        if removed > 0 {
            debug!(target: LOG_TARGET, "Removed {removed} outdated templates");
        }
    }
}

impl Default for BlockTemplateStorage {
    fn default() -> Self {
        Self::new()
    }
}