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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::path::PathBuf;

use crate::common::block_status::BlockStatus;
use crate::common::meta::Meta;
use crate::database::block_file::{BlockFile, BlockFileOps, PutResult as WriteLocation};
use crate::database::dbkeys::DBKeys;
use crate::database::{DBError, DBErrorType, DBResult, HashValue, IDB};
use crate::traits::{BlockHeader, Decode, Encode, EnumConverter, Proto};

use byteorder::{BigEndian, ByteOrder};
use rocksdb::DB as RocksDB;

pub struct BlockDB<BlockFileType = BlockFile, DatabaseType = RocksDB>
where
    BlockFileType: BlockFileOps,
    DatabaseType: IDB,
{
    database: DatabaseType,
    block_file: BlockFileType,
    file_number: u32,
    db_keys: DBKeys,
}

impl<BlockFileType, DatabaseType, OptionType> BlockDB<BlockFileType, DatabaseType>
where
    BlockFileType: BlockFileOps,
    DatabaseType: IDB<OptionType = OptionType>,
{
    pub fn new(
        db_path: PathBuf,
        file_path: PathBuf,
        db_keys: DBKeys,
        options: Option<OptionType>,
    ) -> DBResult<Self> {
        let mut database = DatabaseType::open(db_path, options)?;
        let file_number = match database._get(&db_keys.file_number) {
            Ok(val) => BigEndian::read_u32(&val),
            Err(err) => match err.error_type {
                DBErrorType::NotFoundError => {
                    database.set(&db_keys.file_number, &vec![0; 4])?;
                    0
                }
                _ => return Err(err),
            },
        };
        let file_position = match database._get(&db_keys.file_position) {
            Ok(val) => BigEndian::read_u64(&val),
            Err(err) => match err.error_type {
                DBErrorType::NotFoundError => {
                    database.set(&db_keys.file_number, &vec![0; 8])?;
                    0
                }
                _ => return Err(err),
            },
        };
        let block_file;
        match BlockFileType::new(&file_path, file_number, file_position) {
            Ok(b) => block_file = b,
            Err(_) => return Err(Box::new(DBError::new(DBErrorType::NotFoundError))),
        }
        Ok(BlockDB {
            database,
            block_file,
            file_number,
            db_keys,
        })
    }

    pub fn get_header_tip_hash(&self) -> DBResult<HashValue> {
        self.database._get(&self.db_keys.header_tip)
    }

    pub fn get_tip_meta<HeaderType>(&self) -> DBResult<(Meta<HeaderType>, Meta<HeaderType>)>
    where
        HeaderType: BlockHeader
            + Clone
            + Proto<ProtoType = crate::serialization::blockHeader::BlockHeader>
            + Encode,
    {
        let b_tip = self.get_meta(&self.get_block_tip_hash()?)?;
        let h_tip = self.get_meta(&self.get_header_tip_hash()?)?;
        Ok((h_tip, b_tip))
    }

    pub fn set_header_tip_hash(&mut self, hash: &HashValue) -> DBResult<()> {
        self.database.set(&self.db_keys.header_tip, hash)
    }

    pub fn get_block_tip_hash(&self) -> DBResult<HashValue> {
        self.database._get(&self.db_keys.block_tip)
    }
    pub fn set_block_tip_hash(&mut self, hash: &HashValue) -> DBResult<()> {
        self.database.set(&self.db_keys.block_tip, hash)
    }

    pub fn set_hash_using_height(&mut self, height: u32, hash: &HashValue) -> DBResult<()> {
        let mut height_buf = vec![0; 4];
        BigEndian::write_u32(&mut height_buf, height);
        self.database.set(&height_buf, &hash)
    }

    fn get_hash_by_height(&self, height: u32) -> DBResult<HashValue> {
        let mut height_buf = vec![0; 4];
        BigEndian::write_u32(&mut height_buf, height);
        self.database._get(&height_buf)
    }

    pub fn set_meta<HeaderType>(
        &mut self,
        hash: &HashValue,
        meta_info: &Meta<HeaderType>,
    ) -> DBResult<()>
    where
        HeaderType: BlockHeader + Clone + Proto + Encode,
    {
        let mut hash_copy = hash.clone();
        hash_copy.insert(0, b"b"[0]);
        let encoded;
        match meta_info.encode() {
            Ok(v) => encoded = v,
            Err(_) => {
                return Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                    "Failed to encode metadata".to_string(),
                ))));
            }
        }
        self.database.set(hash_copy.as_ref(), &encoded)
    }

    pub fn get_meta<HeaderType>(&self, hash: &HashValue) -> DBResult<Meta<HeaderType>>
    where
        HeaderType: BlockHeader
            + Clone
            + Proto<ProtoType = crate::serialization::blockHeader::BlockHeader>
            + Encode,
    {
        let mut hash_copy = hash.clone();
        hash_copy.insert(0, b"b"[0]);
        match self.database._get(&hash_copy) {
            Ok(value) => match Meta::decode(&value.to_vec()) {
                Ok(m) => return Ok(m),
                Err(_) => {
                    return Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                        "Failed to decode metadata".to_string(),
                    ))));
                }
            },
            Err(e) => return Err(e),
        }
    }

    pub fn set_block<T>(&mut self, block: &mut T) -> DBResult<WriteLocation>
    where
        T: Encode + Proto,
    {
        let write_location;
        match self.block_file.put::<T>(block) {
            Ok(w) => write_location = w,
            Err(_) => {
                return Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                    "Failed to put to block file".to_string(),
                ))));
            }
        }
        if self.file_number != write_location.file_number {
            self.file_number = write_location.file_number;
            let mut file_number_buf = vec![0; 4];
            BigEndian::write_u32(&mut file_number_buf, write_location.file_number);
            self.database
                .set(&self.db_keys.file_number, &file_number_buf)?;
        }
        let mut file_position_buf = vec![0; 8];
        BigEndian::write_u64(&mut file_position_buf, write_location.file_position);
        self.database
            .set(&self.db_keys.file_position, &file_position_buf)?;
        Ok(write_location)
    }

    pub fn get_blocks<T, HeaderType>(&mut self, from_height: u32, count: u32) -> DBResult<Vec<T>>
    where
        T: Decode + Clone,
        HeaderType: BlockHeader
            + Encode
            + Clone
            + Proto<ProtoType = crate::serialization::blockHeader::BlockHeader>,
    {
        let mut i = 0;
        let mut blocks = Vec::new();
        let _limit = count - 1;
        while i != _limit {
            blocks.push(self.get_block_by_height::<T, HeaderType>(from_height + i)?);
            i += 1;
        }
        Ok(blocks)
    }

    pub fn get_block<T, HeaderType>(&mut self, hash: &HashValue) -> DBResult<T>
    where
        T: Decode + Clone,
        HeaderType: BlockHeader
            + Encode
            + Clone
            + Proto<ProtoType = crate::serialization::blockHeader::BlockHeader>,
    {
        let meta_info = self.get_meta(hash)?;
        self.get_block_by_meta_info::<T, HeaderType>(meta_info)
    }

    pub fn get_block_by_height<T, HeaderType>(&mut self, height: u32) -> DBResult<T>
    where
        T: Decode + Clone,
        HeaderType: BlockHeader
            + Encode
            + Clone
            + Proto<ProtoType = crate::serialization::blockHeader::BlockHeader>,
    {
        let hash = self.get_hash_by_height(height)?;
        self.get_block::<T, HeaderType>(&hash)
    }

    fn get_block_by_meta_info<T, HeaderType>(&mut self, meta_info: Meta<HeaderType>) -> DBResult<T>
    where
        T: Decode + Clone,
        HeaderType: BlockHeader + Encode + Clone + Proto,
    {
        if meta_info.length == Some(0)
            || meta_info.file_number.is_none()
            || meta_info.offset.is_none()
            || meta_info.length.is_none()
        {
            return Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                "No meta information from block".to_string(),
            ))));
        }

        match self.block_file.get::<T>(
            meta_info.file_number.unwrap(),
            meta_info.offset.unwrap(),
            meta_info.length.unwrap() as usize,
        ) {
            Ok(b) => Ok(b),
            Err(_) => Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                "Failed to get block file".to_string(),
            )))),
        }
    }

    pub fn set_block_status(&mut self, hash: &Vec<u8>, status: BlockStatus) -> DBResult<()> {
        let mut hash_cpy = hash.clone();
        hash_cpy.insert(0, 's' as u8);

        let status_byte = status.to_output();

        self.database.set(&hash_cpy, &vec![status_byte])
    }

    pub fn get_block_status(&self, hash: &Vec<u8>) -> DBResult<BlockStatus> {
        let mut hash_cpy = hash.clone();
        hash_cpy.insert(0, 's' as u8);
        let status_u8: u8;
        if let Ok(num) = self.database._get(&hash_cpy) {
            status_u8 = num[0];
        } else {
            return Ok(BlockStatus::Nothing);
        }
        match BlockStatus::from_input(status_u8) {
            Ok(block_status) => Ok(block_status),
            Err(e) => Err(Box::new(DBError::new(DBErrorType::UnexpectedError(
                e.to_string(),
            )))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::block::tests::{create_test_block_without_meta, create_test_header};
    use crate::common::block::Block;
    use crate::common::header::Header;
    use crate::common::signed_tx::SignedTx;
    use crate::common::test_functions::common_tests::assert_block;
    use crate::database::block_file::BlockFileResult;
    use crate::database::mock::RocksDBMock;

    struct BlockFileMock {
        write_location: WriteLocation,
        encoded_block: Vec<u8>,
    }

    impl BlockFileMock {
        pub fn new(write_location: WriteLocation, encoded_block: Vec<u8>) -> BlockFileMock {
            BlockFileMock {
                write_location,
                encoded_block,
            }
        }
    }

    const MAX_MOCK_FILE_SIZE: u64 = 1000;

    impl BlockFileOps for BlockFileMock {
        fn new(_path: &PathBuf, _file_number: u32, _file_position: u64) -> BlockFileResult<Self> {
            let write_location = WriteLocation {
                file_number: 0,
                file_position: 0,
                offset: 0,
                length: 0,
            };
            Ok(BlockFileMock::new(write_location, vec![]))
        }
        fn get<T>(&mut self, _file_number: u32, _offset: u64, _length: usize) -> BlockFileResult<T>
        where
            T: Decode,
        {
            T::decode(&self.encoded_block)
        }
        fn put<T>(&mut self, any_block: &mut T) -> BlockFileResult<WriteLocation>
        where
            T: Encode + Proto,
        {
            let bytes = any_block.encode()?;
            let length = bytes.len() as u64;
            self.encoded_block = bytes.clone();

            let carry = if self.write_location.file_position + length > MAX_MOCK_FILE_SIZE {
                1
            } else {
                0
            };
            self.write_location.file_number = self.write_location.file_number + carry;
            self.write_location.file_position = if carry == 1 {
                length
            } else {
                self.write_location.file_position + length
            };
            self.write_location.offset = if carry == 1 {
                0
            } else {
                self.write_location.file_position - length
            };

            Ok(WriteLocation {
                file_number: self.write_location.file_number,
                file_position: self.write_location.file_position,
                offset: self.write_location.offset,
                length: length as u32,
            })
        }
    }

    #[test]
    fn it_set_block_status_and_get_from_db() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let mut hash = vec![167];
        let block_status = BlockStatus::Block;

        db.set_block_status(&hash, block_status).unwrap();
        let _status = db.get_block_status(&hash).unwrap();

        hash.push(123);
        match db.get_block_status(&hash) {
            Err(e) => assert_eq!(e.error_type, DBErrorType::NotFoundError),
            Ok(status) => assert_eq!(status, BlockStatus::Nothing),
        }
    }

    #[test]
    #[should_panic]
    fn it_set_hash_using_height_and_get_from_db() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let hash = vec![167, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let height = 0xFFFFFFFE;
        db.set_hash_using_height(height, &hash).unwrap();
        let db_hash = db.get_hash_by_height(height).unwrap();
        assert_eq!(db_hash, hash);

        db.get_hash_by_height(height + 1).unwrap();
    }

    #[test]
    fn it_set_header_tip_hash_and_get_from_db() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let hash = vec![13, 04, 05, 09];

        db.set_header_tip_hash(&hash).unwrap();

        let db_hash = db.get_header_tip_hash().unwrap();
        assert_eq!(db_hash, hash);
    }

    #[test]
    fn it_check_file_numbers_and_positions_when_it_set_and_get_many_blocks() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let mut hash = b"hash_for_test_meta".to_vec();
        let mut blocks = vec![];

        for i in 1..255 {
            if hash.len() > 50 {
                hash.pop();
            }
            hash.push(i as u8);

            db.set_hash_using_height(i, &hash).unwrap();
            assert_eq!(db.get_hash_by_height(i).unwrap(), hash);

            let mut block = create_test_block_without_meta();
            let write_location = db.set_block(&mut block).unwrap();
            let mut meta = create_meta_without_file_info();
            meta.file_number = Some(write_location.file_number);
            meta.offset = Some(write_location.offset);
            meta.length = Some((write_location.length) as u32);
            db.set_meta(&hash, &meta).unwrap();
            assert_meta(meta, db.get_meta(&hash).unwrap());
            let result_block = db.get_block::<Block<Header, SignedTx>, Header>(&hash);
            blocks.push(block.clone());
            assert_block(block, result_block.unwrap());
        }

        let database_blocks: Vec<Block<Header, SignedTx>> = db
            .get_blocks::<Block<Header, SignedTx>, Header>(1, 255)
            .unwrap();
        let blocks_cnt = database_blocks.len();
        for i in 0..blocks_cnt - 1 {
            assert_block(
                blocks.get(i).unwrap().clone(),
                database_blocks.get(i).unwrap().clone(),
            );
        }
    }

    #[test]
    fn it_set_block_tip_hash_and_get_from_db() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let mut hash = vec![04, 05, 09, 13];
        for i in 0..255 {
            if hash.len() > 50 {
                hash.pop();
            }
            hash.push(i % 255 as u8);
            db.set_block_tip_hash(&hash).unwrap();
            let db_hash = db.get_block_tip_hash().unwrap();
            assert_eq!(db_hash, hash);
        }
    }

    #[test]
    fn it_set_meta_info_to_db_and_get() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let meta_info = create_meta();

        let mut hash = vec![
            218, 175, 98, 56, 136, 59, 157, 43, 178, 250, 66, 194, 50, 129, 87, 37, 147, 54, 157,
            79, 238, 83, 118, 209, 92, 202, 25, 32, 246, 230, 153, 39,
        ];

        db.set_meta(&hash, &meta_info).unwrap();

        match db.get_meta::<Header>(&hash) {
            Ok(meta) => {
                assert_eq!(meta.height, meta_info.height);
                assert_eq!(meta.t_ema, meta_info.t_ema);
                assert_eq!(meta.p_ema, meta_info.p_ema);
                assert_eq!(meta.next_difficulty, meta_info.next_difficulty);
                assert_eq!(meta.total_work, meta_info.total_work);
                assert_eq!(meta.offset.unwrap(), meta_info.offset.unwrap());
                assert_eq!(meta.file_number.unwrap(), meta_info.file_number.unwrap());
                assert_eq!(meta.length.unwrap(), meta_info.length.unwrap());
            }
            Err(err) => panic!(format!(
                "meta data should be same as the original one {:?}",
                err
            )),
        }

        hash.insert(0, 't' as u8);
        match db.get_meta::<Header>(&hash) {
            Ok(meta) => panic!(format!(
                "meta data with wrong hash should not be found from db {:?}",
                meta
            )),
            Err(e) => match e.error_type {
                DBErrorType::NotFoundError => {}
                _ => panic!("{:?}", e),
            },
        }
    }

    #[test]
    fn it_set_meta_info_without_file_info_to_db_and_get() {
        let db_keys = DBKeys::new(b"a".to_vec(), b"b".to_vec(), b"c".to_vec(), b"d".to_vec());
        let mut db = create_database(db_keys);
        let meta_info = create_meta_without_file_info();

        let mut hash = vec![
            218, 1, 2, 3, 4, 5, 175, 98, 56, 136, 59, 157, 43, 178, 250, 66, 194, 50, 129,
        ];

        assert_eq!(meta_info.file_number, None);
        assert_eq!(meta_info.offset, None);
        assert_eq!(meta_info.length, None);
        db.set_meta(&hash, &meta_info).unwrap();

        match db.get_meta::<Header>(&hash) {
            Ok(meta) => {
                assert_eq!(meta.height, meta_info.height);
                assert_eq!(meta.t_ema, meta_info.t_ema);
                assert_eq!(meta.p_ema, meta_info.p_ema);
                assert_eq!(meta.next_difficulty, meta_info.next_difficulty);
                assert_eq!(meta.total_work, meta_info.total_work);
                // protobuf encoding for BlockDB gives 0 for ()
                assert_eq!(meta.file_number, Some(0));
                assert_eq!(meta.offset, Some(0));
                assert_eq!(meta.length, Some(0));
            }
            Err(err) => panic!(format!(
                "meta data should be same as the original one {:?}",
                err
            )),
        }

        hash.insert(0, 't' as u8);
        match db.get_meta::<Header>(&hash) {
            Ok(meta) => panic!(format!(
                "meta data with wrong hash should not be found from db {:?}",
                meta
            )),
            Err(e) => match e.error_type {
                DBErrorType::NotFoundError => {}
                _ => panic!("{:?}", e),
            },
        }
    }

    fn create_database(db_keys: DBKeys) -> BlockDB<BlockFileMock, RocksDBMock> {
        let mut path = PathBuf::new();
        let mut file_path = PathBuf::new();
        path.push("./test");
        file_path.push("./testFile");
        BlockDB::<BlockFileMock, RocksDBMock>::new(path, file_path, db_keys, None).unwrap()
    }

    fn create_meta_without_file_info() -> Meta<Header> {
        let height = 1234589;
        let t_ema = 134.0;
        let p_ema = 0.234;
        let next_difficulty = 0.01345;
        let total_work = 1e23;
        Meta::new(
            height,
            create_test_header(),
            t_ema,
            p_ema,
            next_difficulty,
            total_work,
            None,
            None,
            None,
            BlockStatus::Block,
        )
    }

    fn create_meta() -> Meta<Header> {
        let mut meta = create_meta_without_file_info();
        meta.offset = Some(123);
        meta.file_number = Some(234);
        meta.length = Some(345);
        meta
    }

    fn assert_meta(meta1: Meta<Header>, meta2: Meta<Header>) {
        assert_eq!(meta1.height, meta1.height);
        assert_eq!(meta1.t_ema, meta2.t_ema);
        assert_eq!(meta1.p_ema, meta2.p_ema);
        assert_eq!(meta1.next_difficulty, meta2.next_difficulty);
        assert_eq!(meta1.total_work, meta2.total_work);
        assert_eq!(meta1.file_number, meta2.file_number);
        assert_eq!(meta1.offset, meta2.offset);
        assert_eq!(meta1.length, meta2.length);
    }
}