dae5d75dd6
* cargo upgrade "ethereum-types" --all --allow-prerelease * [ethash] fix compilation errors * [ethkey] fix compilation errors * [journaldb] fix compilation errors * [dir] fix compilation errors * [ethabi] update to 0.7 * wip * [eip-712] fix compilation errors * [ethjson] fix compilation errors * [Cargo.toml] add TODO to remove patches * [ethstore] fix compilation errors * use patched keccak-hash with new primitive-types * wip * [ethcore-network-devp2p] fix compilation errors * [vm] fix compilation errors * [common-types, evm, wasm] fix compilation errors * [ethcore-db] Require AsRef instead of Deref for keys * [ethcore-blockchain] fix some compilation errors * [blooms-db] fix compilation errors Thanks a lot @dvdplm :) * we don't need no rlp ethereum feature * [ethcore] fix some compilation errors * [parity-ipfs-api] fix compilation error * [ethcore-light] fix compilation errors * [Cargo.lock] update parity-common * [ethcore-private-tx] fix some compilation errors * wip * [ethcore-private-tx] fix compilation errors * [parity-updater] fix compilation errors * [parity-rpc] fix compilation errors * [parity-bin] fix other compilation errors * update to new ethereum-types * update keccak-hash * [fastmap] fix compilation in tests * [blooms-db] fix compilation in tests * [common-types] fix compilation in tests * [triehash-ethereum] fix compilation in tests * [ethkey] fix compilation in tests * [pwasm-run-test] fix compilation errors * [wasm] fix compilation errors * [ethjson] fix compilation in tests * [eip-712] fix compilation in tests * [ethcore-blockchain] fix compilation in tests * [ethstore] fix compilation in tests * [ethstore-accounts] fix compilation in tests * [parity-hash-fetch] fix compilation in tests * [parity-whisper] fix compilation in tests * [ethcore-miner] fix compilation in tests * [ethcore-network-devp2p] fix compilation in tests * [*] upgrade rand to 0.6 * [evm] get rid of num-bigint conversions * [ethcore] downgrade trie-standardmap and criterion * [ethcore] fix some warnings * [ethcore] fix compilation in tests * [evmbin] fix compilation in tests * [updater] fix compilation in tests * [ethash] fix compilation in tests * [ethcore-secretstore] fix compilation in tests * [ethcore-sync] fix compilation in tests * [parity-rpc] fix compilation in tests * [ethcore] finally fix compilation in tests FUCK YEAH!!! * [ethstore] lazy_static is unused * [ethcore] fix test * fix up bad merge * [Cargo.toml] remove unused patches * [*] replace some git dependencies with crates.io * [Cargo.toml] remove unused lazy_static * [*] clean up * [ethcore] fix transaction_filter_deprecated test * [private-tx] fix serialization tests * fix more serialization tests * [ethkey] fix smoky test * [rpc] fix tests, please? * [ethcore] remove commented out code * Apply suggestions from code review Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * [ethstore] remove unused dev-dependency * [ethcore] remove resolved TODO * [*] resolve keccak-hash TODO * [*] s/Address::default()/Address::zero() * [rpc] remove Subscribers::new_test * [rpc] remove EthPubSubClient::new_test * [ethcore] use trie-standardmap from crates.io * [dir] fix db_root_path * [ethcore] simplify snapshot::tests::helpers::fill_storage * Apply suggestions from code review Co-Authored-By: David <dvdplm@gmail.com> * [ethcore-secretstore] resolve TODO in serialization * [ethcore-network-devp2p] resolve TODO in save_key * [Cargo.lock] update triehash * [*] use ethabi from crates.io * [ethkey] use secp256k1 from master branch * [Cargo.lock] update eth-secp256k1
155 lines
4.4 KiB
Rust
155 lines
4.4 KiB
Rust
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Parity Ethereum.
|
|
|
|
// Parity Ethereum 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.
|
|
|
|
// Parity Ethereum 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 Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use std::io::{Seek, SeekFrom, Write, Read};
|
|
use std::path::Path;
|
|
use std::{io, fs};
|
|
|
|
use ethbloom;
|
|
|
|
/// Autoresizable file containing blooms.
|
|
pub struct File {
|
|
/// Backing file.
|
|
file: fs::File,
|
|
/// Current file len.
|
|
len: u64,
|
|
}
|
|
|
|
impl File {
|
|
/// Opens database file. Creates new file if database file does not exist.
|
|
pub fn open<P>(path: P) -> io::Result<File> where P: AsRef<Path> {
|
|
let file = fs::OpenOptions::new()
|
|
.read(true)
|
|
.write(true)
|
|
.create(true)
|
|
// appending is done manually by calling `ensure_space_for_write`
|
|
.append(false)
|
|
.open(path)?;
|
|
let len = file.metadata()?.len();
|
|
|
|
let file = File {
|
|
file,
|
|
len,
|
|
};
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
/// Resizes the file if there is not enough space to write bloom at given position.
|
|
fn ensure_space_for_write(&mut self, pos: u64) -> io::Result<()> {
|
|
// position to write + 256 bytes
|
|
let required_space = (pos + 1) * 256;
|
|
if required_space > self.len {
|
|
self.file.set_len(required_space)?;
|
|
self.len = required_space;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Read bloom at given position.
|
|
pub fn read_bloom(&self, pos: u64) -> io::Result<ethbloom::Bloom> {
|
|
let mut file_ref = &self.file;
|
|
file_ref.seek(SeekFrom::Start(pos * 256))?;
|
|
let mut bloom = ethbloom::Bloom::default();
|
|
file_ref.read_exact(bloom.as_bytes_mut())?;
|
|
Ok(bloom)
|
|
}
|
|
|
|
/// Accrue bloom into bloom at given position.
|
|
pub fn accrue_bloom<'a, B>(&mut self, pos: u64, bloom: B) -> io::Result<()> where ethbloom::BloomRef<'a>: From<B> {
|
|
self.ensure_space_for_write(pos)?;
|
|
let mut old_bloom: ethbloom::Bloom = self.read_bloom(pos)?;
|
|
old_bloom.accrue_bloom(bloom);
|
|
let mut file_ref = &self.file;
|
|
file_ref.seek(SeekFrom::Start(pos * 256))?;
|
|
file_ref.write_all(old_bloom.as_bytes())
|
|
}
|
|
|
|
/// Replace bloom at given position with a new one.
|
|
pub fn replace_bloom<'a, B>(&mut self, pos: u64, bloom: B) -> io::Result<()> where ethbloom::BloomRef<'a>: From<B> {
|
|
self.ensure_space_for_write(pos)?;
|
|
let mut file_ref = &self.file;
|
|
file_ref.seek(SeekFrom::Start(pos * 256))?;
|
|
file_ref.write_all(ethbloom::BloomRef::from(bloom).data())
|
|
}
|
|
|
|
/// Returns an iterator over file.
|
|
///
|
|
/// This function needs to be mutable `fs::File` is just a shared reference a system file handle.
|
|
/// https://users.rust-lang.org/t/how-to-handle-match-with-irrelevant-ok--/6291/15
|
|
pub fn iterator_from(&mut self, pos: u64) -> io::Result<FileIterator> {
|
|
let start = std::cmp::min(self.len, pos * 256);
|
|
let mut buf_reader = io::BufReader::new(&self.file);
|
|
buf_reader.seek(SeekFrom::Start(start))?;
|
|
|
|
let iter = FileIterator {
|
|
file: buf_reader,
|
|
};
|
|
|
|
Ok(iter)
|
|
}
|
|
|
|
/// Flush outstanding modifications to the disk
|
|
pub fn flush(&mut self) -> io::Result<()> {
|
|
self.file.flush()
|
|
}
|
|
}
|
|
|
|
/// Iterator over blooms of a single file.
|
|
pub struct FileIterator<'a> {
|
|
/// Backing file.
|
|
file: io::BufReader<&'a fs::File>,
|
|
}
|
|
|
|
impl<'a> FileIterator<'a> {
|
|
/// Advance file by n blooms
|
|
pub fn advance(&mut self, n: u64) -> io::Result<()> {
|
|
self.file.seek(SeekFrom::Current(n as i64 * 256))?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'a> Iterator for FileIterator<'a> {
|
|
type Item = io::Result<ethbloom::Bloom>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let mut bloom = ethbloom::Bloom::default();
|
|
match self.file.read_exact(bloom.as_bytes_mut()) {
|
|
Ok(_) => Some(Ok(bloom)),
|
|
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => None,
|
|
Err(err) => Some(Err(err)),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use ethbloom::Bloom;
|
|
use tempdir::TempDir;
|
|
use super::File;
|
|
|
|
#[test]
|
|
fn test_file() {
|
|
let tempdir = TempDir::new("").unwrap();
|
|
let mut file = File::open(tempdir.path().join("file")).unwrap();
|
|
file.accrue_bloom(0, &Bloom::from_low_u64_be(1)).unwrap();
|
|
file.flush().unwrap();
|
|
assert_eq!(file.read_bloom(0).unwrap(), Bloom::from_low_u64_be(1));
|
|
|
|
}
|
|
}
|