1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Ethereum blooms database

extern crate byteorder;
extern crate ethbloom;
extern crate parking_lot;
extern crate tiny_keccak;

#[cfg(test)]
extern crate tempdir;

mod db;
mod file;

use parking_lot::Mutex;
use std::{io, path::Path};

/// Threadsafe API for blooms database.
///
/// # Warning
///
/// This database does not guarantee atomic writes.
pub struct Database {
    database: Mutex<db::Database>,
}

impl Database {
    /// Creates new database handle.
    ///
    /// # Arguments
    ///
    /// * `path` - database directory
    pub fn open<P>(path: P) -> io::Result<Database>
    where
        P: AsRef<Path>,
    {
        let result = Database {
            database: Mutex::new(db::Database::open(path)?),
        };

        Ok(result)
    }

    /// Closes the inner database
    pub fn close(&self) -> io::Result<()> {
        self.database.lock().close()
    }

    /// Reopens database at the same location.
    pub fn reopen(&self) -> io::Result<()> {
        self.database.lock().reopen()
    }

    /// Inserts one or more blooms into database.
    ///
    /// # Arguments
    ///
    /// * `from` - index of the first bloom that needs to be inserted
    /// * `blooms` - iterator over blooms
    pub fn insert_blooms<'a, I, B>(&self, from: u64, blooms: I) -> io::Result<()>
    where
        ethbloom::BloomRef<'a>: From<B>,
        I: Iterator<Item = B>,
    {
        self.database.lock().insert_blooms(from, blooms)
    }

    /// Returns indexes of all headers matching given bloom in a specified range.
    ///
    /// # Arguments
    ///
    /// * `from` - index of the first bloom that needs to be checked
    /// * `to` - index of the last bloom that needs to be checked (inclusive range)
    /// * `blooms` - searched pattern
    pub fn filter<'a, B, I, II>(&self, from: u64, to: u64, blooms: II) -> io::Result<Vec<u64>>
    where
        ethbloom::BloomRef<'a>: From<B>,
        II: IntoIterator<Item = B, IntoIter = I> + Copy,
        I: Iterator<Item = B>,
    {
        self.database
            .lock()
            .iterate_matching(from, to, blooms)?
            .collect::<Result<Vec<u64>, _>>()
    }
}