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
use crate::{AccountState, BlockNum, Maybe, TransactionReceipt};
use ethbloom::Bloom;
use primitive_types::{H160, H256};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// compatibility with old persist
/// without field block_version

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Incomming {
    pub block_number: BlockNum,
    pub timestamp: u64,
    pub used_gas: u64,
    state_root: H256,
    last_block_hash: H256,
    /// Maybe::Nothing indicates removed account
    state_updates: HashMap<H160, (Maybe<AccountState>, HashMap<H256, H256>)>,

    /// Transactions that was processed but wasn't committed.
    /// Transactions should be ordered by execution order on all validators.
    executed_transactions: Vec<(H256, TransactionReceipt)>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Committed {
    pub block: BlockHeader,
    /// Transactions should be ordered somehow, because we
    pub committed_transactions: Vec<(H256, TransactionReceipt)>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockHeader {
    pub parent_hash: H256,
    pub state_root: H256,
    pub native_chain_hash: H256,
    pub transactions: Vec<H256>,
    pub transactions_root: H256,
    pub receipts_root: H256,
    pub logs_bloom: Bloom,
    pub block_number: u64,
    pub gas_limit: u64,
    pub gas_used: u64,
    pub timestamp: u64,
    pub native_chain_slot: u64,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvmPersistState {
    Committed(Committed),
    Incomming(Incomming), // Usually bank will never try to freeze banks with persist state.
}

// Convert from legacy structure to new one

impl From<Incomming> for crate::state::Incomming {
    fn from(incomming: Incomming) -> Self {
        Self {
            block_number: incomming.block_number,
            timestamp: incomming.timestamp,
            used_gas: incomming.used_gas,
            state_root: incomming.state_root,
            last_block_hash: incomming.last_block_hash,
            state_updates: incomming.state_updates,

            executed_transactions: incomming.executed_transactions,
            block_version: crate::BlockVersion::InitVersion,
        }
    }
}

impl From<BlockHeader> for crate::types::BlockHeader {
    fn from(block_header: BlockHeader) -> Self {
        Self {
            parent_hash: block_header.parent_hash,
            state_root: block_header.state_root,
            native_chain_hash: block_header.native_chain_hash,
            transactions: block_header.transactions,
            transactions_root: block_header.transactions_root,
            receipts_root: block_header.receipts_root,
            logs_bloom: block_header.logs_bloom,
            block_number: block_header.block_number,
            gas_limit: block_header.gas_limit,

            gas_used: block_header.gas_used,
            timestamp: block_header.timestamp,
            native_chain_slot: block_header.native_chain_slot,
            version: crate::BlockVersion::InitVersion,
        }
    }
}

impl From<Committed> for crate::state::Committed {
    fn from(committed: Committed) -> Self {
        Self {
            block: committed.block.into(),
            committed_transactions: committed.committed_transactions,
        }
    }
}

impl From<EvmPersistState> for crate::state::EvmPersistState {
    fn from(persist: EvmPersistState) -> Self {
        match persist {
            EvmPersistState::Committed(c) => Self::Committed(c.into()),
            EvmPersistState::Incomming(c) => Self::Incomming(c.into()),
        }
    }
}

// Convert from real structure to legacy one

impl From<crate::state::Incomming> for Incomming {
    fn from(incomming: crate::state::Incomming) -> Self {
        Self {
            block_number: incomming.block_number,
            timestamp: incomming.timestamp,
            used_gas: incomming.used_gas,
            state_root: incomming.state_root,
            last_block_hash: incomming.last_block_hash,
            state_updates: incomming.state_updates,

            executed_transactions: incomming.executed_transactions,
        }
    }
}

impl From<crate::types::BlockHeader> for BlockHeader {
    fn from(block_header: crate::types::BlockHeader) -> Self {
        Self {
            parent_hash: block_header.parent_hash,
            state_root: block_header.state_root,
            native_chain_hash: block_header.native_chain_hash,
            transactions: block_header.transactions,
            transactions_root: block_header.transactions_root,
            receipts_root: block_header.receipts_root,
            logs_bloom: block_header.logs_bloom,
            block_number: block_header.block_number,
            gas_limit: block_header.gas_limit,

            gas_used: block_header.gas_used,
            timestamp: block_header.timestamp,
            native_chain_slot: block_header.native_chain_slot,
        }
    }
}

impl From<crate::state::Committed> for Committed {
    fn from(committed: crate::state::Committed) -> Self {
        Self {
            block: committed.block.into(),
            committed_transactions: committed.committed_transactions,
        }
    }
}

impl From<crate::state::EvmPersistState> for EvmPersistState {
    fn from(persist: crate::state::EvmPersistState) -> Self {
        match persist {
            crate::state::EvmPersistState::Committed(c) => Self::Committed(c.into()),
            crate::state::EvmPersistState::Incomming(c) => Self::Incomming(c.into()),
        }
    }
}