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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::parser::errors::{OpError, OpErrorKind, OpResult};
use crate::parser::reader::BlockchainRead;
use bitcoin::{Block, Transaction};
use std::collections::HashMap;
use std::convert::From;
use std::fs::{self, DirEntry, File};
use std::io::{self, BufReader, Cursor, Seek, SeekFrom};
use std::path::{Path, PathBuf};

///
/// An index of all blk files found.
///
#[derive(Debug, Clone)]
pub struct BlkFile {
    files: HashMap<i32, PathBuf>,
}

impl BlkFile {
    ///
    /// Construct an index of all blk files.
    ///
    pub(crate) fn new(path: &Path) -> OpResult<BlkFile> {
        Ok(BlkFile {
            files: BlkFile::scan_path(path)?,
        })
    }

    ///
    /// Read a Block from blk file.
    ///
    #[inline]
    pub(crate) fn read_raw_block(&self, n_file: i32, offset: u32) -> OpResult<Vec<u8>> {
        if let Some(blk_path) = self.files.get(&n_file) {
            let mut r = BufReader::new(File::open(blk_path)?);
            r.seek(SeekFrom::Start(offset as u64 - 4))?;
            let block_size = r.read_u32()?;
            let block = r.read_u8_vec(block_size)?;
            Ok(block)
        } else {
            Err(OpError::from("blk file not found, sync with bitcoin core"))
        }
    }

    ///
    /// Read a Block from blk file.
    ///
    pub(crate) fn read_block(&self, n_file: i32, offset: u32) -> OpResult<Block> {
        Cursor::new(self.read_raw_block(n_file, offset)?).read_block()
    }

    ///
    /// Read a transaction from blk file.
    ///
    pub(crate) fn read_transaction(
        &self,
        n_file: i32,
        n_pos: u32,
        n_tx_offset: u32,
    ) -> OpResult<Transaction> {
        if let Some(blk_path) = self.files.get(&n_file) {
            let mut r = BufReader::new(File::open(blk_path)?);
            // the size of a header is 80.
            r.seek(SeekFrom::Start(n_pos as u64 + n_tx_offset as u64 + 80))?;
            r.read_transaction()
        } else {
            Err(OpError::from("blk file not found, sync with bitcoin core"))
        }
    }

    ///
    /// Scan blk folder to build an index of all blk files.
    ///
    fn scan_path(path: &Path) -> OpResult<HashMap<i32, PathBuf>> {
        let mut collected = HashMap::with_capacity(4000);
        for entry in fs::read_dir(path)? {
            match entry {
                Ok(de) => {
                    let path = BlkFile::resolve_path(&de)?;
                    if !path.is_file() {
                        continue;
                    };
                    if let Some(file_name) = path.as_path().file_name() {
                        if let Some(file_name) = file_name.to_str() {
                            if let Some(index) = BlkFile::parse_blk_index(file_name) {
                                collected.insert(index, path);
                            }
                        }
                    }
                }
                Err(msg) => {
                    return Err(OpError::from(msg));
                }
            }
        }
        collected.shrink_to_fit();
        if collected.is_empty() {
            Err(OpError::new(OpErrorKind::RuntimeError).join_msg("No blk files found!"))
        } else {
            Ok(collected)
        }
    }

    ///
    /// Resolve symlink.
    ///
    fn resolve_path(entry: &DirEntry) -> io::Result<PathBuf> {
        if entry.file_type()?.is_symlink() {
            fs::read_link(entry.path())
        } else {
            Ok(entry.path())
        }
    }

    ///
    /// Extract index from block file name.
    ///
    fn parse_blk_index(file_name: &str) -> Option<i32> {
        let prefix = "blk";
        let ext = ".dat";
        if file_name.starts_with(prefix) && file_name.ends_with(ext) {
            file_name[prefix.len()..(file_name.len() - ext.len())]
                .parse::<i32>()
                .ok()
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_blk_index() {
        assert_eq!(0, BlkFile::parse_blk_index("blk00000.dat").unwrap());
        assert_eq!(6, BlkFile::parse_blk_index("blk6.dat").unwrap());
        assert_eq!(1202, BlkFile::parse_blk_index("blk1202.dat").unwrap());
        assert_eq!(
            13412451,
            BlkFile::parse_blk_index("blk13412451.dat").unwrap()
        );
        assert_eq!(true, BlkFile::parse_blk_index("blkindex.dat").is_none());
        assert_eq!(true, BlkFile::parse_blk_index("invalid.dat").is_none());
    }
}