MCPcopy Create free account
hub / github.com/bcndev/bytecoin / WalletStateBasic

Method WalletStateBasic

src/Core/WalletStateBasic.cpp:92–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90}
91
92WalletStateBasic::WalletStateBasic(
93 logging::ILogger &log, const Config &config, const Currency &currency, DB &db, const std::string &cache_name)
94 : m_genesis_bid(currency.genesis_block_hash)
95 , m_config(config)
96 , m_currency(currency)
97 , m_log(log, "WalletState")
98 , m_db(db) {
99 std::string version;
100 std::string other_genesis_bid;
101 std::string other_cache_name;
102 m_db.get("$version", version);
103 m_db.get("$genesis_bid", other_genesis_bid);
104 m_db.get("$cache_name", other_cache_name);
105 if (version == "11") {
106 // version 12 has undo state for last max_undo_height blocks
107 // we do not delete excess states/headers for simplicity
108 DB::Cursor cur2 = m_db.rbegin(INDEX_HEIGHT_to_HEADER);
109 if (!cur2.end()) {
110 size_t height = common::integer_cast<Height>(common::read_varint_sqlite4(cur2.get_suffix()));
111 size_t from = height < max_undo_height ? 0 : height - max_undo_height;
112 size_t added = 0;
113 for (; from != height; from += 1) {
114 auto key = INDEX_HEIGHT_to_STATE + common::write_varint_sqlite4(from + 1);
115 BinaryArray ba;
116 if (!m_db.get(key, ba)) {
117 m_db.put(key, seria::to_binary(UndoMap{}), true);
118 added += 1;
119 }
120 }
121 m_log(logging::INFO) << logging::BrightGreen
122 << "Wallet Cache converted from version 11 to 12, added undo states " << added;
123 }
124 version = "12";
125 m_db.put("$version", version, false);
126 }
127 if (version != version_current || other_genesis_bid != common::pod_to_hex(m_genesis_bid) ||
128 other_cache_name != cache_name) {
129 if (!version.empty())
130 m_log(logging::INFO) << "Data format, wallet seed or genesis bid different, old version=" << version
131 << " current version=" << version_current << ", clearing wallet cache...";
132 clear_db(true);
133 version = version_current;
134 m_db.put("$version", version, true);
135 m_db.put("$cache_name", cache_name, true);
136 m_db.put("$genesis_bid", common::pod_to_hex(m_genesis_bid), true);
137 }
138 { // must close cursors before possible commit in wallet_addresses_updated
139 DB::Cursor cur2 = m_db.rbegin(INDEX_HEIGHT_to_HEADER);
140 if (!cur2.end()) {
141 m_chain_height = common::integer_cast<Height>(common::read_varint_sqlite4(cur2.get_suffix()));
142 m_tip = read_chain(m_chain_height);
143 } else
144 m_tip = fill_genesis(m_genesis_bid, m_currency.genesis_block_template);
145 }
146 // m_db.debug_print_index_size(INDEX_HEIGHT_to_STATE);
147 // m_db.debug_print_index_size(INDEX_HEIGHT_to_HEADER);
148 // m_db.debug_print_index_size(INDEX_TID_to_TRANSACTIONS);
149 // m_db.debug_print_index_size(INDEX_ADDRESS_HEIGHT_TID);

Callers

nothing calls this directly

Calls 9

write_varint_sqlite4Function · 0.85
to_binaryFunction · 0.85
pod_to_hexFunction · 0.85
read_varint_sqlite4Function · 0.50
getMethod · 0.45
rbeginMethod · 0.45
endMethod · 0.45
putMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected