2016-04-06 06:53:37 +00:00
|
|
|
//
|
|
|
|
// Created by mwo on 5/11/15.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MicroCore.h"
|
|
|
|
|
2016-09-06 10:34:07 +00:00
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
namespace xmreg
|
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* The constructor is interesting, as
|
|
|
|
* m_mempool and m_blockchain_storage depend
|
|
|
|
* on each other.
|
|
|
|
*
|
|
|
|
* So basically m_mempool is initialized with
|
|
|
|
* reference to Blockchain (i.e., Blockchain&)
|
|
|
|
* and m_blockchain_storage is initialized with
|
|
|
|
* reference to m_mempool (i.e., tx_memory_pool&)
|
|
|
|
*
|
|
|
|
* The same is done in cryptonode::core.
|
|
|
|
*/
|
|
|
|
MicroCore::MicroCore():
|
|
|
|
m_mempool(m_blockchain_storage),
|
|
|
|
m_blockchain_storage(m_mempool)
|
2018-03-09 00:36:21 +00:00
|
|
|
{
|
|
|
|
m_device = &hw::get_device("default");
|
|
|
|
}
|
2017-04-28 05:45:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialized the MicroCore object.
|
|
|
|
*
|
|
|
|
* Create BlockchainLMDB on the heap.
|
|
|
|
* Open database files located in blockchain_path.
|
|
|
|
* Initialize m_blockchain_storage with the BlockchainLMDB object.
|
|
|
|
*/
|
|
|
|
bool
|
2018-06-01 00:39:49 +00:00
|
|
|
MicroCore::init(const string& _blockchain_path, network_type nt)
|
2017-04-28 05:45:30 +00:00
|
|
|
{
|
|
|
|
int db_flags = 0;
|
2016-05-01 02:02:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
blockchain_path = _blockchain_path;
|
2016-04-12 10:07:34 +00:00
|
|
|
|
2018-06-01 00:39:49 +00:00
|
|
|
nettype = nt;
|
2016-09-06 10:34:07 +00:00
|
|
|
|
2018-06-01 00:39:49 +00:00
|
|
|
db_flags |= MDB_RDONLY;
|
|
|
|
db_flags |= MDB_NOLOCK;
|
2016-09-06 10:34:07 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
BlockchainDB* db = nullptr;
|
|
|
|
db = new BlockchainLMDB();
|
2016-09-06 10:34:07 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// try opening lmdb database files
|
|
|
|
db->open(blockchain_path, db_flags);
|
2016-04-06 06:53:37 +00:00
|
|
|
}
|
2017-04-28 05:45:30 +00:00
|
|
|
catch (const std::exception& e)
|
2016-04-06 06:53:37 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
cerr << "Error opening database: " << e.what();
|
|
|
|
return false;
|
2016-04-06 06:53:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// check if the blockchain database
|
|
|
|
// is successful opened
|
|
|
|
if(!db->is_open())
|
|
|
|
return false;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// initialize Blockchain object to manage
|
|
|
|
// the database.
|
2018-06-01 00:39:49 +00:00
|
|
|
return m_blockchain_storage.init(db, nettype);
|
2017-04-28 05:45:30 +00:00
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* Get m_blockchain_storage.
|
|
|
|
* Initialize m_blockchain_storage with the BlockchainLMDB object.
|
|
|
|
*/
|
|
|
|
Blockchain&
|
|
|
|
MicroCore::get_core()
|
|
|
|
{
|
|
|
|
return m_blockchain_storage;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2018-08-07 03:53:25 +00:00
|
|
|
tx_memory_pool&
|
|
|
|
MicroCore::get_mempool()
|
|
|
|
{
|
|
|
|
return m_mempool;
|
|
|
|
}
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* Get block by its height
|
|
|
|
*
|
|
|
|
* returns true if success
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
MicroCore::get_block_by_height(const uint64_t& height, block& blk)
|
|
|
|
{
|
|
|
|
try
|
2016-04-06 06:53:37 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
blk = m_blockchain_storage.get_db().get_block_from_height(height);
|
2016-04-06 06:53:37 +00:00
|
|
|
}
|
2017-04-28 05:45:30 +00:00
|
|
|
catch (const BLOCK_DNE& e)
|
|
|
|
{
|
|
|
|
cerr << "Block of height " << height
|
|
|
|
<< " not found in the blockchain!"
|
|
|
|
<< e.what()
|
|
|
|
<< endl;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (const DB_ERROR& e)
|
2016-04-30 01:35:48 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
cerr << "Blockchain access error when getting block " << height
|
|
|
|
<< e.what()
|
|
|
|
<< endl;
|
2016-04-30 01:35:48 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
cerr << "Something went terribly wrong when getting block " << height
|
|
|
|
<< endl;
|
2016-04-30 01:35:48 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-30 01:35:48 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-30 01:35:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* Get transaction tx from the blockchain using it hash
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
MicroCore::get_tx(const crypto::hash& tx_hash, transaction& tx)
|
|
|
|
{
|
|
|
|
if (m_blockchain_storage.have_tx(tx_hash))
|
|
|
|
{
|
|
|
|
// get transaction with given hash
|
2019-10-16 07:51:00 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
tx = m_blockchain_storage.get_db().get_tx(tx_hash);
|
|
|
|
}
|
|
|
|
catch (TX_DNE const& e)
|
|
|
|
{
|
|
|
|
cerr << "MicroCore::get_tx: " << e.what() << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-28 05:45:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "MicroCore::get_tx tx does not exist in blockchain: " << tx_hash << endl;
|
|
|
|
return false;
|
2019-10-16 07:51:00 +00:00
|
|
|
}
|
2016-04-30 01:35:48 +00:00
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MicroCore::get_tx(const string& tx_hash_str, transaction& tx)
|
|
|
|
{
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// parse tx hash string to hash object
|
|
|
|
crypto::hash tx_hash;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
if (!xmreg::parse_str_secret_key(tx_hash_str, tx_hash))
|
2016-04-06 06:53:37 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
cerr << "Cant parse tx hash: " << tx_hash_str << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
if (!get_tx(tx_hash, tx))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* Find output with given public key in a given transaction
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
MicroCore::find_output_in_tx(const transaction& tx,
|
|
|
|
const public_key& output_pubkey,
|
|
|
|
tx_out& out,
|
|
|
|
size_t& output_index)
|
|
|
|
{
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
size_t idx {0};
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// search in the ouputs for an output which
|
|
|
|
// public key matches to what we want
|
|
|
|
auto it = std::find_if(tx.vout.begin(), tx.vout.end(),
|
|
|
|
[&](const tx_out& o)
|
|
|
|
{
|
|
|
|
const txout_to_key& tx_in_to_key
|
|
|
|
= boost::get<txout_to_key>(o.target);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
++idx;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return tx_in_to_key.key == output_pubkey;
|
|
|
|
});
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
if (it != tx.vout.end())
|
|
|
|
{
|
|
|
|
// we found the desired public key
|
|
|
|
out = *it;
|
|
|
|
output_index = idx > 0 ? idx - 1 : idx;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
//cout << idx << " " << output_index << endl;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
uint64_t
|
|
|
|
MicroCore::get_blk_timestamp(uint64_t blk_height)
|
|
|
|
{
|
|
|
|
cryptonote::block blk;
|
|
|
|
|
|
|
|
if (!get_block_by_height(blk_height, blk))
|
2016-04-06 06:53:37 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
cerr << "Cant get block by height: " << blk_height << endl;
|
|
|
|
return 0;
|
2016-04-06 06:53:37 +00:00
|
|
|
}
|
2016-04-07 05:41:15 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
return blk.timestamp;
|
|
|
|
}
|
2016-04-07 05:41:15 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* De-initialized Blockchain.
|
|
|
|
*
|
|
|
|
* since blockchain is opened as MDB_RDONLY
|
|
|
|
* need to manually free memory taken on heap
|
|
|
|
* by BlockchainLMDB
|
|
|
|
*/
|
|
|
|
MicroCore::~MicroCore()
|
|
|
|
{
|
2017-05-26 01:51:41 +00:00
|
|
|
//m_blockchain_storage.get_db().close();
|
2017-04-28 05:45:30 +00:00
|
|
|
delete &m_blockchain_storage.get_db();
|
|
|
|
}
|
2016-04-07 05:41:15 +00:00
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
bool
|
|
|
|
init_blockchain(const string& path,
|
|
|
|
MicroCore& mcore,
|
2018-06-01 00:39:49 +00:00
|
|
|
Blockchain*& core_storage,
|
|
|
|
network_type nt)
|
2017-04-28 05:45:30 +00:00
|
|
|
{
|
2016-04-07 05:41:15 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// initialize the core using the blockchain path
|
2018-06-01 00:39:49 +00:00
|
|
|
if (!mcore.init(path, nt))
|
2016-05-01 02:02:37 +00:00
|
|
|
{
|
2017-04-28 05:45:30 +00:00
|
|
|
cerr << "Error accessing blockchain." << endl;
|
|
|
|
return false;
|
2016-05-01 02:02:37 +00:00
|
|
|
}
|
2016-04-07 05:41:15 +00:00
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
// get the high level Blockchain object to interact
|
|
|
|
// with the blockchain lmdb database
|
|
|
|
core_storage = &(mcore.get_core());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:53:25 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
MicroCore::get_block_complete_entry(block const& b, block_complete_entry& bce)
|
|
|
|
{
|
|
|
|
bce.block = cryptonote::block_to_blob(b);
|
|
|
|
|
|
|
|
for (const auto &tx_hash: b.tx_hashes)
|
|
|
|
{
|
|
|
|
transaction tx;
|
|
|
|
|
|
|
|
if (!get_tx(tx_hash, tx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
cryptonote::blobdata txblob = tx_to_blob(tx);
|
|
|
|
|
|
|
|
bce.txs.push_back(txblob);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 05:45:30 +00:00
|
|
|
string
|
|
|
|
MicroCore::get_blkchain_path()
|
|
|
|
{
|
|
|
|
return blockchain_path;
|
|
|
|
}
|
|
|
|
|
2018-03-09 00:36:21 +00:00
|
|
|
hw::device* const
|
|
|
|
MicroCore::get_device() const
|
|
|
|
{
|
|
|
|
return m_device;
|
|
|
|
}
|
|
|
|
|
2016-04-30 01:35:48 +00:00
|
|
|
}
|