| 114 | } |
| 115 | |
| 116 | BlockChain::BlockChain(logging::ILogger &log, const Config &config, const Currency ¤cy, bool read_only) |
| 117 | : m_genesis_bid(currency.genesis_block_hash) |
| 118 | , m_db(read_only ? platform::O_READ_EXISTING : platform::O_OPEN_ALWAYS, config.get_data_folder() + "/blockchain") |
| 119 | , m_archive(read_only || !config.is_archive, config.get_data_folder() + "/archive") |
| 120 | , m_log(log, "BlockChainState") |
| 121 | , m_config(config) |
| 122 | , m_currency(currency) { |
| 123 | invariant(CheckpointDifficulty{}.size() == currency.get_checkpoint_keys_count(), ""); |
| 124 | std::string version; |
| 125 | if (!m_db.get("$version", version)) { |
| 126 | DB::Cursor cur = m_db.begin(std::string{}); |
| 127 | if (!cur.end()) |
| 128 | throw std::runtime_error("Blockchain database format unknown version, please delete " + m_db.get_path()); |
| 129 | version = version_current; |
| 130 | m_db.put("$version", version, false); |
| 131 | } |
| 132 | if (version != version_current) |
| 133 | return; // BlockChainState will upgrade DB, we must not continue or risk crashing |
| 134 | Hash stored_genesis_bid; |
| 135 | if (get_chain(0, &stored_genesis_bid)) { |
| 136 | if (stored_genesis_bid != m_genesis_bid) |
| 137 | throw std::runtime_error("Database starts with different genesis_block"); |
| 138 | DB::Cursor cur2 = m_db.rbegin(TIP_CHAIN_PREFIX); |
| 139 | m_tip_height = cur2.end() ? -1 : common::integer_cast<Height>(common::read_varint_sqlite4(cur2.get_suffix())); |
| 140 | seria::from_binary(m_tip_bid, cur2.get_value_array()); |
| 141 | api::BlockHeader tip_header = read_header(m_tip_bid); |
| 142 | m_tip_cumulative_difficulty = tip_header.cumulative_difficulty; |
| 143 | m_header_tip_window.push_back(tip_header); |
| 144 | } |
| 145 | BinaryArray cha; |
| 146 | if (m_db.get("internal_import_chain", cha)) { |
| 147 | seria::from_binary(m_internal_import_chain, cha); |
| 148 | m_log(logging::INFO) << "BlockChain continue internal import of blocks, count=" |
| 149 | << m_internal_import_chain.size(); |
| 150 | } |
| 151 | // m_db.debug_print_index_size(BLOCK_PREFIX); |
| 152 | // m_db.debug_print_index_size(TRANSACTION_PREFIX); |
| 153 | // m_db.debug_print_index_size(TIP_CHAIN_PREFIX); |
| 154 | // m_db.debug_print_index_size(TIMESTAMP_BLOCK_PREFIX); |
| 155 | |
| 156 | // m_db.debug_print_index_size(CHECKPOINT_PREFIX_STABLE); |
| 157 | // m_db.debug_print_index_size(CHECKPOINT_PREFIX_LATEST); |
| 158 | // m_db.debug_print_index_size(CHILDREN_PREFIX); |
| 159 | // m_db.debug_print_index_size(CD_TIPS_PREFIX); |
| 160 | } |
| 161 | |
| 162 | void BlockChain::db_commit() { |
| 163 | m_log(logging::INFO) << "BlockChain::db_commit started... tip_height=" << m_tip_height |
nothing calls this directly
no test coverage detected