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
use std::io;

use beserial::{Deserialize, Serialize, SerializingError, WriteBytesExt};
use database::{FromDatabaseValue, IntoDatabaseValue};
use hash::Blake2bHash;
use block::{Block, BlockBody, Difficulty, Target};
use crate::super_block_counts::SuperBlockCounts;

#[derive(Default, Clone, PartialEq, Eq, Debug, Deserialize)]
pub struct ChainInfo {
    pub head: Block,
    pub total_difficulty: Difficulty,
    pub total_work: Difficulty,
    pub super_block_counts: SuperBlockCounts,
    pub on_main_chain: bool,
    pub main_chain_successor: Option<Blake2bHash>,
}

impl ChainInfo {
    pub fn initial(block: Block) -> Self {
        let target = Target::from(&block.header.pow());
        let mut super_block_counts = SuperBlockCounts::default();
        super_block_counts.add(target.get_depth());
        let total_difficulty = Difficulty::from(block.header.n_bits);
        let total_work = Difficulty::from(target);
        ChainInfo {
            head: block,
            total_difficulty,
            total_work,
            super_block_counts,
            on_main_chain: true,
            main_chain_successor: None,
        }
    }

    pub fn next(&self, block: Block) -> Self {
        let target = Target::from(&block.header.pow());
        let super_block_counts = self.super_block_counts.copy_and_add(target.get_depth());
        let total_difficulty = &self.total_difficulty + &Difficulty::from(block.header.n_bits);
        let total_work = &self.total_work + &Difficulty::from(target);
        ChainInfo {
            head: block,
            total_difficulty,
            total_work,
            super_block_counts,
            on_main_chain: false,
            main_chain_successor: None
        }
    }

    pub fn prev(&self, block: Block) -> Self {
        let target = Target::from(&block.header.pow());
        let super_block_counts = self.super_block_counts.copy_and_subtract(target.get_depth());
        let total_difficulty = &self.total_difficulty - &Difficulty::from(block.header.n_bits);
        let total_work = &self.total_work - &Difficulty::from(target);
        ChainInfo {
            head: block,
            total_difficulty,
            total_work,
            super_block_counts,
            on_main_chain: false,
            main_chain_successor: None
        }
    }
}

// Do not serialize the block body.
// XXX Move this into Block.serialize_xxx()?
impl Serialize for ChainInfo {
    fn serialize<W: WriteBytesExt>(&self, writer: &mut W) -> Result<usize, SerializingError> {
        let mut size = 0;
        size += Serialize::serialize(&self.head.header, writer)?;
        size += Serialize::serialize(&self.head.interlink, writer)?;
        size += Serialize::serialize(&None::<BlockBody>, writer)?;
        size += Serialize::serialize(&self.total_difficulty, writer)?;
        size += Serialize::serialize(&self.total_work, writer)?;
        size += Serialize::serialize(&self.super_block_counts, writer)?;
        size += Serialize::serialize(&self.on_main_chain, writer)?;
        size += Serialize::serialize(&self.main_chain_successor, writer)?;
        Ok(size)
    }

    fn serialized_size(&self) -> usize {
        let mut size = 0;
        size += Serialize::serialized_size(&self.head.header);
        size += Serialize::serialized_size(&self.head.interlink);
        size += Serialize::serialized_size(&None::<BlockBody>);
        size += Serialize::serialized_size(&self.total_difficulty);
        size += Serialize::serialized_size(&self.total_work);
        size += Serialize::serialized_size(&self.super_block_counts);
        size += Serialize::serialized_size(&self.on_main_chain);
        size += Serialize::serialized_size(&self.main_chain_successor);
        size
    }
}

impl IntoDatabaseValue for ChainInfo {
    fn database_byte_size(&self) -> usize {
        self.serialized_size()
    }

    fn copy_into_database(&self, mut bytes: &mut [u8]) {
        Serialize::serialize(&self, &mut bytes).unwrap();
    }
}

impl FromDatabaseValue for ChainInfo {
    fn copy_from_database(bytes: &[u8]) -> io::Result<Self> where Self: Sized {
        let mut cursor = io::Cursor::new(bytes);
        Ok(Deserialize::deserialize(&mut cursor)?)
    }
}