Optimized blockchain storage

This commit is contained in:
Antonio Juarez 2014-06-20 16:56:33 +01:00
parent 8f2d0879ab
commit 198323aaf2
28 changed files with 1817 additions and 1158 deletions

View file

@ -1,3 +1,8 @@
Release notes 0.8.10
- Optimized blockchain storage memory usage
- Various code improvements
Release notes 0.8.9
- JSON RPC v2.0 compatibility

View file

@ -304,7 +304,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
if(m_send_que.size() > ABSTRACT_SERVER_SEND_QUE_MAX_COUNT)
{
send_guard.unlock();
LOG_ERROR("send que size is more than ABSTRACT_SERVER_SEND_QUE_MAX_COUNT(" << ABSTRACT_SERVER_SEND_QUE_MAX_COUNT << "), shutting down connection");
// LOG_ERROR("send que size is more than ABSTRACT_SERVER_SEND_QUE_MAX_COUNT(" << ABSTRACT_SERVER_SEND_QUE_MAX_COUNT << "), shutting down connection");
close();
return false;
}

View file

@ -512,7 +512,7 @@ public:
CRITICAL_REGION_LOCAL1(m_invoke_response_handlers_lock);
if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
{
LOG_ERROR_CC(m_connection_context, "Failed to do_send");
// LOG_ERROR_CC(m_connection_context, "Failed to do_send");
err_code = LEVIN_ERROR_CONNECTION;
break;
}
@ -635,7 +635,7 @@ public:
CRITICAL_REGION_BEGIN(m_send_lock);
if(!m_pservice_endpoint->do_send(&head, sizeof(head)))
{
LOG_ERROR_CC(m_connection_context, "Failed to do_send()");
// LOG_ERROR_CC(m_connection_context, "Failed to do_send()");
return -1;
}

View file

@ -150,7 +150,6 @@ void cn_slow_hash(const void *data, size_t length, char *hash)
uint8_t a[AES_BLOCK_SIZE];
uint8_t b[AES_BLOCK_SIZE];
uint8_t d[AES_BLOCK_SIZE];
uint8_t aes_key[AES_KEY_SIZE];
RDATA_ALIGN16 uint8_t expandedKey[256];
union cn_slow_hash_state state;
@ -201,8 +200,8 @@ void cn_slow_hash(const void *data, size_t length, char *hash)
for(i = 0; i < ITER / 2; i++)
{
#define TOTALBLOCKS (MEMORY / AES_BLOCK_SIZE)
#define state_index(x) (((*((uint64_t *)x) >> 4) & (TOTALBLOCKS - 1)) << 4)
#define TOTALBLOCKS (MEMORY / AES_BLOCK_SIZE)
#define state_index(x) (((*((uint64_t *)x) >> 4) & (TOTALBLOCKS - 1)) << 4)
// Iteration 1
p = &long_state[state_index(a)];

View file

@ -19,6 +19,7 @@
// MONEY_SUPPLY - total number coins to be generated
#define MONEY_SUPPLY ((uint64_t)(-1))
#define EMISSION_SPEED_FACTOR (18)
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW 100
#define CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE 10000 //size of block (bytes) after which reward for block calculated using block size
@ -29,9 +30,6 @@
#define DEFAULT_FEE ((uint64_t)1000000) // pow(10, 6)
#define ORPHANED_BLOCKS_MAX_COUNT 100
#define DIFFICULTY_TARGET 120 // seconds
#define DIFFICULTY_WINDOW 720 // blocks
#define DIFFICULTY_LAG 15 // !!!

View file

@ -0,0 +1 @@
#include "SwappedMap.h"

350
src/cryptonote_core/SwappedMap.h Executable file
View file

@ -0,0 +1,350 @@
#pragma once
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <list>
#include <unordered_map>
#include <string>
#include <vector>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
template<class Key, class T> class SwappedMap {
private:
struct Descriptor {
uint64_t offset;
uint64_t index;
};
public:
typedef typename std::pair<Key, T> value_type;
class const_iterator {
public:
//typedef ptrdiff_t difference_type;
//typedef std::bidirectional_iterator_tag iterator_category;
//typedef std::pair<const Key, T>* pointer;
//typedef std::pair<const Key, T>& reference;
//typedef std::pair<const Key, T> value_type;
const_iterator(SwappedMap* swappedMap, typename std::unordered_map<Key, Descriptor>::const_iterator descriptorsIterator) : m_swappedMap(swappedMap), m_descriptorsIterator(descriptorsIterator) {
}
const_iterator& operator++() {
++m_descriptorsIterator;
return *this;
}
bool operator !=(const_iterator other) const {
return m_descriptorsIterator != other.m_descriptorsIterator;
}
bool operator ==(const_iterator other) const {
return m_descriptorsIterator == other.m_descriptorsIterator;
}
const std::pair<const Key, T>& operator*() const {
return *m_swappedMap->load(m_descriptorsIterator->first, m_descriptorsIterator->second.offset);
}
const std::pair<const Key, T>* operator->() const {
return m_swappedMap->load(m_descriptorsIterator->first, m_descriptorsIterator->second.offset);
}
typename std::unordered_map<Key, Descriptor>::const_iterator innerIterator() const {
return m_descriptorsIterator;
}
private:
SwappedMap* m_swappedMap;
typename std::unordered_map<Key, Descriptor>::const_iterator m_descriptorsIterator;
};
typedef const_iterator iterator;
SwappedMap();
//SwappedMap(const SwappedMap&) = delete;
~SwappedMap();
//SwappedMap& operator=(const SwappedMap&) = delete;
bool open(const std::string& itemFileName, const std::string& indexFileName, size_t poolSize);
void close();
uint64_t size() const;
const_iterator begin();
const_iterator end();
size_t count(const Key& key) const;
const_iterator find(const Key& key);
void clear();
void erase(const_iterator iterator);
std::pair<const_iterator, bool> insert(const std::pair<const Key, T>& value);
private:
std::fstream m_itemsFile;
std::fstream m_indexesFile;
size_t m_poolSize;
std::unordered_map<Key, Descriptor> m_descriptors;
uint64_t m_itemsFileSize;
std::unordered_map<Key, T> m_items;
std::list<Key> m_cache;
std::unordered_map<Key, typename std::list<Key>::iterator> m_cacheIterators;
uint64_t m_cacheHits;
uint64_t m_cacheMisses;
std::pair<const Key, T>* prepare(const Key& key);
const std::pair<const Key, T>* load(const Key& key, uint64_t offset);
};
template<class Key, class T> SwappedMap<Key, T>::SwappedMap() {
}
template<class Key, class T> SwappedMap<Key, T>::~SwappedMap() {
close();
}
template<class Key, class T> bool SwappedMap<Key, T>::open(const std::string& itemFileName, const std::string& indexFileName, size_t poolSize) {
if (poolSize == 0) {
return false;
}
m_itemsFile.open(itemFileName, std::ios::in | std::ios::out | std::ios::binary);
m_indexesFile.open(indexFileName, std::ios::in | std::ios::out | std::ios::binary);
if (m_itemsFile && m_indexesFile) {
uint64_t count;
m_indexesFile.read(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
return false;
}
std::unordered_map<Key, Descriptor> descriptors;
uint64_t itemsFileSize = 0;
for (uint64_t i = 0; i < count; ++i) {
bool valid;
m_indexesFile.read(reinterpret_cast<char*>(&valid), sizeof valid);
if (!m_indexesFile) {
return false;
}
Key key;
m_indexesFile.read(reinterpret_cast<char*>(&key), sizeof key);
if (!m_indexesFile) {
return false;
}
uint32_t itemSize;
m_indexesFile.read(reinterpret_cast<char*>(&itemSize), sizeof itemSize);
if (!m_indexesFile) {
return false;
}
if (valid) {
Descriptor descriptor = { itemsFileSize, i };
descriptors.insert(std::make_pair(key, descriptor));
}
itemsFileSize += itemSize;
}
m_descriptors.swap(descriptors);
m_itemsFileSize = itemsFileSize;
} else {
m_itemsFile.open(itemFileName, std::ios::out | std::ios::binary);
m_itemsFile.close();
m_itemsFile.open(itemFileName, std::ios::in | std::ios::out | std::ios::binary);
m_indexesFile.open(indexFileName, std::ios::out | std::ios::binary);
uint64_t count = 0;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
return false;
}
m_indexesFile.close();
m_indexesFile.open(indexFileName, std::ios::in | std::ios::out | std::ios::binary);
m_descriptors.clear();
m_itemsFileSize = 0;
}
m_poolSize = poolSize;
m_items.clear();
m_cache.clear();
m_cacheIterators.clear();
m_cacheHits = 0;
m_cacheMisses = 0;
return true;
}
template<class Key, class T> void SwappedMap<Key, T>::close() {
std::cout << "SwappedMap cache hits: " << m_cacheHits << ", misses: " << m_cacheMisses << " (" << std::fixed << std::setprecision(2) << static_cast<double>(m_cacheMisses) / (m_cacheHits + m_cacheMisses) * 100 << "%)" << std::endl;
}
template<class Key, class T> uint64_t SwappedMap<Key, T>::size() const {
return m_descriptors.size();
}
template<class Key, class T> typename SwappedMap<Key, T>::const_iterator SwappedMap<Key, T>::begin() {
return const_iterator(this, m_descriptors.cbegin());
}
template<class Key, class T> typename SwappedMap<Key, T>::const_iterator SwappedMap<Key, T>::end() {
return const_iterator(this, m_descriptors.cend());
}
template<class Key, class T> size_t SwappedMap<Key, T>::count(const Key& key) const {
return m_descriptors.count(key);
}
template<class Key, class T> typename SwappedMap<Key, T>::const_iterator SwappedMap<Key, T>::find(const Key& key) {
return const_iterator(this, m_descriptors.find(key));
}
template<class Key, class T> void SwappedMap<Key, T>::clear() {
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::clear");
}
m_indexesFile.seekp(0);
uint64_t count = 0;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::clear");
}
m_descriptors.clear();
m_itemsFileSize = 0;
m_items.clear();
m_cache.clear();
m_cacheIterators.clear();
}
template<class Key, class T> void SwappedMap<Key, T>::erase(const_iterator iterator) {
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::erase");
}
typename std::unordered_map<Key, Descriptor>::const_iterator descriptorsIterator = iterator.innerIterator();
m_indexesFile.seekp(sizeof(uint64_t) + (sizeof(bool) + sizeof(Key) + sizeof(uint32_t)) * descriptorsIterator->second.index);
bool valid = false;
m_indexesFile.write(reinterpret_cast<char*>(&valid), sizeof valid);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::erase");
}
m_descriptors.erase(descriptorsIterator);
auto cacheIteratorsIterator = m_cacheIterators.find(descriptorsIterator->first);
if (cacheIteratorsIterator != m_cacheIterators.end()) {
m_items.erase(descriptorsIterator->first);
m_cache.erase(cacheIteratorsIterator->second);
m_cacheIterators.erase(cacheIteratorsIterator);
}
}
template<class Key, class T> std::pair<typename SwappedMap<Key, T>::const_iterator, bool> SwappedMap<Key, T>::insert(const std::pair<const Key, T>& value) {
uint64_t itemsFileSize;
{
if (!m_itemsFile) {
throw std::runtime_error("SwappedMap::insert");
}
m_itemsFile.seekp(m_itemsFileSize);
try {
boost::archive::binary_oarchive archive(m_itemsFile);
archive & value.second;
} catch (std::exception&) {
throw std::runtime_error("SwappedMap::insert");
}
itemsFileSize = m_itemsFile.tellp();
}
{
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::insert");
}
m_indexesFile.seekp(sizeof(uint64_t) + (sizeof(bool) + sizeof(Key) + sizeof(uint32_t)) * m_descriptors.size());
bool valid = true;
m_indexesFile.write(reinterpret_cast<char*>(&valid), sizeof valid);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::insert");
}
m_indexesFile.write(reinterpret_cast<const char*>(&value.first), sizeof value.first);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::insert");
}
uint32_t itemSize = static_cast<uint32_t>(itemsFileSize - m_itemsFileSize);
m_indexesFile.write(reinterpret_cast<char*>(&itemSize), sizeof itemSize);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::insert");
}
m_indexesFile.seekp(0);
uint64_t count = m_descriptors.size() + 1;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
throw std::runtime_error("SwappedMap::insert");
}
}
Descriptor descriptor = { m_itemsFileSize, m_descriptors.size() };
auto descriptorsInsert = m_descriptors.insert(std::make_pair(value.first, descriptor));
m_itemsFileSize = itemsFileSize;
T* newItem = &prepare(value.first)->second;
*newItem = value.second;
return std::make_pair(const_iterator(this, descriptorsInsert.first), true);
}
template<class Key, class T> std::pair<const Key, T>* SwappedMap<Key, T>::prepare(const Key& key) {
if (m_items.size() == m_poolSize) {
typename std::list<Key>::iterator cacheIter = m_cache.begin();
m_items.erase(*cacheIter);
m_cacheIterators.erase(*cacheIter);
m_cache.erase(cacheIter);
}
std::pair<typename std::unordered_map<Key, T>::iterator, bool> itemInsert = m_items.insert(std::make_pair(key, T()));
typename std::list<Key>::iterator cacheIter = m_cache.insert(m_cache.end(), key);
m_cacheIterators.insert(std::make_pair(key, cacheIter));
return &*itemInsert.first;
}
template<class Key, class T> const std::pair<const Key, T>* SwappedMap<Key, T>::load(const Key& key, uint64_t offset) {
auto itemIterator = m_items.find(key);
if (itemIterator != m_items.end()) {
auto cacheIteratorsIterator = m_cacheIterators.find(key);
if (cacheIteratorsIterator->second != --m_cache.end()) {
m_cache.splice(m_cache.end(), m_cache, cacheIteratorsIterator->second);
}
++m_cacheHits;
return &*itemIterator;
}
typename std::unordered_map<Key, Descriptor>::iterator descriptorsIterator = m_descriptors.find(key);
if (descriptorsIterator == m_descriptors.end()) {
throw std::runtime_error("SwappedMap::load");
}
if (!m_itemsFile) {
throw std::runtime_error("SwappedMap::load");
}
m_itemsFile.seekg(descriptorsIterator->second.offset);
T tempItem;
try {
boost::archive::binary_iarchive archive(m_itemsFile);
archive & tempItem;
} catch (std::exception&) {
throw std::runtime_error("SwappedMap::load");
}
std::pair<const Key, T>* item = prepare(key);
std::swap(tempItem, item->second);
++m_cacheMisses;
return item;
}

View file

@ -0,0 +1 @@
#include "SwappedVector.h"

View file

@ -0,0 +1,284 @@
#pragma once
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
//#include <boost/archive/binary_iarchive.hpp>
//#include <boost/archive/binary_oarchive.hpp>
#include "serialization/binary_archive.h"
template<class T> class SwappedVector {
public:
SwappedVector();
//SwappedVector(const SwappedVector&) = delete;
~SwappedVector();
//SwappedVector& operator=(const SwappedVector&) = delete;
bool open(const std::string& itemFileName, const std::string& indexFileName, size_t poolSize);
void close();
bool empty() const;
uint64_t size() const;
const T& operator[](uint64_t index);
const T& front();
const T& back();
void clear();
void pop_back();
void push_back(const T& item);
private:
struct ItemEntry;
struct CacheEntry;
struct ItemEntry {
public:
T item;
typename std::list<CacheEntry>::iterator cacheIter;
};
struct CacheEntry {
public:
typename std::map<uint64_t, ItemEntry>::iterator itemIter;
};
std::fstream m_itemsFile;
std::fstream m_indexesFile;
size_t m_poolSize;
std::vector<uint64_t> m_offsets;
uint64_t m_itemsFileSize;
std::map<uint64_t, ItemEntry> m_items;
std::list<CacheEntry> m_cache;
uint64_t m_cacheHits;
uint64_t m_cacheMisses;
T* prepare(uint64_t index);
};
template<class T> SwappedVector<T>::SwappedVector() {
}
template<class T> SwappedVector<T>::~SwappedVector() {
close();
}
template<class T> bool SwappedVector<T>::open(const std::string& itemFileName, const std::string& indexFileName, size_t poolSize) {
if (poolSize == 0) {
return false;
}
m_itemsFile.open(itemFileName, std::ios::in | std::ios::out | std::ios::binary);
m_indexesFile.open(indexFileName, std::ios::in | std::ios::out | std::ios::binary);
if (m_itemsFile && m_indexesFile) {
uint64_t count;
m_indexesFile.read(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
return false;
}
std::vector<uint64_t> offsets;
uint64_t itemsFileSize = 0;
for (uint64_t i = 0; i < count; ++i) {
uint32_t itemSize;
m_indexesFile.read(reinterpret_cast<char*>(&itemSize), sizeof itemSize);
if (!m_indexesFile) {
return false;
}
offsets.emplace_back(itemsFileSize);
itemsFileSize += itemSize;
}
m_offsets.swap(offsets);
m_itemsFileSize = itemsFileSize;
} else {
m_itemsFile.open(itemFileName, std::ios::out | std::ios::binary);
m_itemsFile.close();
m_itemsFile.open(itemFileName, std::ios::in | std::ios::out | std::ios::binary);
m_indexesFile.open(indexFileName, std::ios::out | std::ios::binary);
uint64_t count = 0;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
return false;
}
m_indexesFile.close();
m_indexesFile.open(indexFileName, std::ios::in | std::ios::out | std::ios::binary);
m_offsets.clear();
m_itemsFileSize = 0;
}
m_poolSize = poolSize;
m_items.clear();
m_cache.clear();
m_cacheHits = 0;
m_cacheMisses = 0;
return true;
}
template<class T> void SwappedVector<T>::close() {
std::cout << "SwappedVector cache hits: " << m_cacheHits << ", misses: " << m_cacheMisses << " (" << std::fixed << std::setprecision(2) << static_cast<double>(m_cacheMisses) / (m_cacheHits + m_cacheMisses) * 100 << "%)" << std::endl;
}
template<class T> bool SwappedVector<T>::empty() const {
return m_offsets.empty();
}
template<class T> uint64_t SwappedVector<T>::size() const {
return m_offsets.size();
}
template<class T> const T& SwappedVector<T>::operator[](uint64_t index) {
auto itemIter = m_items.find(index);
if (itemIter != m_items.end()) {
if (itemIter->second.cacheIter != --m_cache.end()) {
m_cache.splice(m_cache.end(), m_cache, itemIter->second.cacheIter);
}
++m_cacheHits;
return itemIter->second.item;
}
if (index >= m_offsets.size()) {
throw std::runtime_error("SwappedVector::operator[]");
}
if (!m_itemsFile) {
throw std::runtime_error("SwappedVector::operator[]");
}
m_itemsFile.seekg(m_offsets[index]);
T tempItem;
//try {
//boost::archive::binary_iarchive archive(m_itemsFile);
//archive & tempItem;
//} catch (std::exception&) {
// throw std::runtime_error("SwappedVector::operator[]");
//}
binary_archive<false> archive(m_itemsFile);
if (!do_serialize(archive, tempItem)) {
throw std::runtime_error("SwappedVector::operator[]");
}
T* item = prepare(index);
std::swap(tempItem, *item);
++m_cacheMisses;
return *item;
}
template<class T> const T& SwappedVector<T>::front() {
return operator[](0);
}
template<class T> const T& SwappedVector<T>::back() {
return operator[](m_offsets.size() - 1);
}
template<class T> void SwappedVector<T>::clear() {
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::clear");
}
m_indexesFile.seekp(0);
uint64_t count = 0;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::clear");
}
m_offsets.clear();
m_itemsFileSize = 0;
m_items.clear();
m_cache.clear();
}
template<class T> void SwappedVector<T>::pop_back() {
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::pop_back");
}
m_indexesFile.seekp(0);
uint64_t count = m_offsets.size() - 1;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::pop_back");
}
m_itemsFileSize = m_offsets.back();
m_offsets.pop_back();
auto itemIter = m_items.find(m_offsets.size());
if (itemIter != m_items.end()) {
m_cache.erase(itemIter->second.cacheIter);
m_items.erase(itemIter);
}
}
template<class T> void SwappedVector<T>::push_back(const T& item) {
uint64_t itemsFileSize;
{
if (!m_itemsFile) {
throw std::runtime_error("SwappedVector::push_back");
}
m_itemsFile.seekp(m_itemsFileSize);
//try {
// boost::archive::binary_oarchive archive(m_itemsFile);
// archive & item;
//} catch (std::exception&) {
// throw std::runtime_error("SwappedVector::push_back");
//}
binary_archive<true> archive(m_itemsFile);
if (!do_serialize(archive, *const_cast<T*>(&item))) {
throw std::runtime_error("SwappedVector::push_back");
}
itemsFileSize = m_itemsFile.tellp();
}
{
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::push_back");
}
m_indexesFile.seekp(sizeof(uint64_t) + sizeof(uint32_t) * m_offsets.size());
uint32_t itemSize = static_cast<uint32_t>(itemsFileSize - m_itemsFileSize);
m_indexesFile.write(reinterpret_cast<char*>(&itemSize), sizeof itemSize);
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::push_back");
}
m_indexesFile.seekp(0);
uint64_t count = m_offsets.size() + 1;
m_indexesFile.write(reinterpret_cast<char*>(&count), sizeof count);
if (!m_indexesFile) {
throw std::runtime_error("SwappedVector::push_back");
}
}
m_offsets.push_back(m_itemsFileSize);
m_itemsFileSize = itemsFileSize;
T* newItem = prepare(m_offsets.size() - 1);
*newItem = item;
}
template<class T> T* SwappedVector<T>::prepare(uint64_t index) {
if (m_items.size() == m_poolSize) {
auto cacheIter = m_cache.begin();
m_items.erase(cacheIter->itemIter);
m_cache.erase(cacheIter);
}
auto itemIter = m_items.insert(std::make_pair(index, ItemEntry()));
CacheEntry cacheEntry = { itemIter.first };
auto cacheIter = m_cache.insert(m_cache.end(), cacheEntry);
itemIter.first->second.cacheIter = cacheIter;
return &itemIter.first->second.item;
}

File diff suppressed because it is too large Load diff

View file

@ -3,55 +3,17 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/list.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/foreach.hpp>
#include <atomic>
#include "syncobj.h"
#include "string_tools.h"
#include "SwappedVector.h"
#include "cryptonote_format_utils.h"
#include "tx_pool.h"
#include "cryptonote_basic.h"
#include "common/util.h"
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "difficulty.h"
#include "cryptonote_core/cryptonote_format_utils.h"
#include "verification_context.h"
#include "crypto/hash.h"
#include "checkpoints.h"
namespace cryptonote
{
/************************************************************************/
/* */
/************************************************************************/
class blockchain_storage
{
namespace cryptonote {
class blockchain_storage {
public:
struct transaction_chain_entry
{
transaction tx;
uint64_t m_keeper_block_height;
size_t m_blob_size;
std::vector<uint64_t> m_global_output_indexes;
};
struct block_extended_info
{
block bl;
uint64_t height;
size_t block_cumulative_size;
difficulty_type cumulative_difficulty;
uint64_t already_generated_coins;
};
blockchain_storage(tx_memory_pool& tx_pool):m_tx_pool(tx_pool), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false), m_is_blockchain_storing(false)
{};
@ -60,26 +22,17 @@ namespace cryptonote
bool deinit();
void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
//bool push_new_block();
bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks, std::list<transaction>& txs);
bool get_blocks(uint64_t start_offset, size_t count, std::list<block>& blocks);
bool get_alternative_blocks(std::list<block>& blocks);
size_t get_alternative_blocks_count();
crypto::hash get_block_id_by_height(uint64_t height);
bool get_block_by_hash(const crypto::hash &h, block &blk);
void get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid);
template<class archive_t>
void serialize(archive_t & ar, const unsigned int version);
template<class archive_t> void serialize(archive_t & ar, const unsigned int version);
bool have_tx(const crypto::hash &id);
bool have_tx_keyimges_as_spent(const transaction &tx);
bool have_tx_keyimg_as_spent(const crypto::key_image &key_im);
transaction *get_tx(const crypto::hash &id);
template<class visitor_t>
bool scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t& vis, uint64_t* pmax_related_block_height = NULL);
uint64_t get_current_blockchain_height();
crypto::hash get_tail_id();
@ -90,231 +43,190 @@ namespace cryptonote
bool create_block_template(block& b, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, const blobdata& ex_nonce);
bool have_block(const crypto::hash& id);
size_t get_total_transactions();
bool get_outs(uint64_t amount, std::list<crypto::public_key>& pkeys);
bool get_short_chain_history(std::list<crypto::hash>& ids);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::list<std::pair<block, std::list<transaction> > >& blocks, uint64_t& total_height, uint64_t& start_height, size_t max_count);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::list<std::pair<block, std::list<transaction>>>& blocks, uint64_t& total_height, uint64_t& start_height, size_t max_count);
bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NOTIFY_RESPONSE_GET_OBJECTS::request& rsp);
bool handle_get_objects(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res);
bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res);
bool get_backward_blocks_sizes(size_t from_height, std::vector<size_t>& sz, size_t count);
bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs);
bool store_blockchain();
bool check_tx_input(const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig, uint64_t* pmax_related_block_height = NULL);
bool check_tx_inputs(const transaction& tx, const crypto::hash& tx_prefix_hash, uint64_t* pmax_used_block_height = NULL);
bool check_tx_inputs(const transaction& tx, uint64_t* pmax_used_block_height = NULL);
bool check_tx_inputs(const transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id);
uint64_t get_current_comulative_blocksize_limit();
bool is_storing_blockchain(){return m_is_blockchain_storing;}
uint64_t block_difficulty(size_t i);
template<class t_ids_container, class t_blocks_container, class t_missed_container>
bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs)
{
bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) {
CRITICAL_REGION_LOCAL(m_blockchain_lock);
BOOST_FOREACH(const auto& bl_id, block_ids)
{
auto it = m_blocks_index.find(bl_id);
if(it == m_blocks_index.end())
for (const auto& bl_id : block_ids) {
auto it = m_blockMap.find(bl_id);
if (it == m_blockMap.end()) {
missed_bs.push_back(bl_id);
else
{
} else {
CHECK_AND_ASSERT_MES(it->second < m_blocks.size(), false, "Internal error: bl_id=" << epee::string_tools::pod_to_hex(bl_id)
<< " have index record with offset="<<it->second<< ", bigger then m_blocks.size()=" << m_blocks.size());
blocks.push_back(m_blocks[it->second].bl);
}
}
return true;
}
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs)
{
bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) {
CRITICAL_REGION_LOCAL(m_blockchain_lock);
BOOST_FOREACH(const auto& tx_id, txs_ids)
{
auto it = m_transactions.find(tx_id);
if(it == m_transactions.end())
{
for (const auto& tx_id : txs_ids) {
auto it = m_transactionMap.find(tx_id);
if (it == m_transactionMap.end()) {
transaction tx;
if(!m_tx_pool.get_transaction(tx_id, tx))
if (!m_tx_pool.get_transaction(tx_id, tx)) {
missed_txs.push_back(tx_id);
else
} else {
txs.push_back(tx);
}
} else {
txs.push_back(transactionByIndex(it->second).tx);
}
else
txs.push_back(it->second.tx);
}
return true;
}
//debug functions
void print_blockchain(uint64_t start_index, uint64_t end_index);
void print_blockchain_index();
void print_blockchain_outs(const std::string& file);
private:
typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;
struct Transaction {
transaction tx;
std::vector<uint32_t> m_global_output_indexes;
template<class archive_t> void serialize(archive_t & ar, unsigned int version);
BEGIN_SERIALIZE_OBJECT()
FIELD(tx)
FIELD(m_global_output_indexes)
END_SERIALIZE()
};
struct Block {
block bl;
uint32_t height;
uint64_t block_cumulative_size;
difficulty_type cumulative_difficulty;
uint64_t already_generated_coins;
std::vector<Transaction> transactions;
template<class Archive> void serialize(Archive& archive, unsigned int version);
BEGIN_SERIALIZE_OBJECT()
FIELD(bl)
VARINT_FIELD(height)
VARINT_FIELD(block_cumulative_size)
VARINT_FIELD(cumulative_difficulty)
VARINT_FIELD(already_generated_coins)
FIELD(transactions)
END_SERIALIZE()
};
struct TransactionIndex {
uint32_t block;
uint16_t transaction;
template<class Archive> void serialize(Archive& archive, unsigned int version);
};
typedef std::unordered_set<crypto::key_image> key_images_container;
typedef std::vector<block_extended_info> blocks_container;
typedef std::unordered_map<crypto::hash, block_extended_info> blocks_ext_by_hash;
typedef std::unordered_map<crypto::hash, block> blocks_by_hash;
typedef std::map<uint64_t, std::vector<std::pair<crypto::hash, size_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
typedef std::unordered_map<crypto::hash, Block> blocks_ext_by_hash;
typedef std::map<uint64_t, std::vector<std::pair<TransactionIndex, uint16_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
tx_memory_pool& m_tx_pool;
epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
// main chain
blocks_container m_blocks; // height -> block_extended_info
blocks_by_id_index m_blocks_index; // crypto::hash -> height
transactions_container m_transactions;
key_images_container m_spent_keys;
size_t m_current_block_cumul_sz_limit;
// all alternative chains
blocks_ext_by_hash m_alternative_chains; // crypto::hash -> block_extended_info
// some invalid blocks
blocks_ext_by_hash m_invalid_blocks; // crypto::hash -> block_extended_info
outputs_container m_outputs;
std::string m_config_folder;
checkpoints m_checkpoints;
std::atomic<bool> m_is_in_checkpoint_zone;
std::atomic<bool> m_is_blockchain_storing;
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
bool pop_block_from_blockchain();
bool purge_block_data_from_blockchain(const block& b, size_t processed_tx_count);
bool purge_transaction_from_blockchain(const crypto::hash& tx_id);
bool purge_transaction_keyimages_from_blockchain(const transaction& tx, bool strict_check);
typedef SwappedVector<Block> Blocks;
typedef std::unordered_map<crypto::hash, uint32_t> BlockMap;
typedef std::unordered_map<crypto::hash, TransactionIndex> TransactionMap;
bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc);
bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc);
Blocks m_blocks;
BlockMap m_blockMap;
TransactionMap m_transactionMap;
template<class visitor_t> bool scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t& vis, uint64_t* pmax_related_block_height = NULL);
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
bool handle_alternative_block(const block& b, const crypto::hash& id, block_verification_context& bvc);
difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::iterator>& alt_chain, block_extended_info& bei);
difficulty_type get_next_difficulty_for_alternative_chain(const std::list<blocks_ext_by_hash::iterator>& alt_chain, Block& bei);
bool prevalidate_miner_transaction(const block& b, uint64_t height);
bool validate_miner_transaction(const block& b, size_t cumulative_block_size, uint64_t fee, uint64_t& base_reward, uint64_t already_generated_coins);
bool validate_transaction(const block& b, uint64_t height, const transaction& tx);
bool rollback_blockchain_switching(std::list<block>& original_chain, size_t rollback_height);
bool add_transaction_from_block(const transaction& tx, const crypto::hash& tx_id, const crypto::hash& bl_id, uint64_t bl_height);
bool push_transaction_to_global_outs_index(const transaction& tx, const crypto::hash& tx_id, std::vector<uint64_t>& global_indexes);
bool pop_transaction_from_global_index(const transaction& tx, const crypto::hash& tx_id);
bool get_last_n_blocks_sizes(std::vector<size_t>& sz, size_t count);
bool add_out_to_get_random_outs(std::vector<std::pair<crypto::hash, size_t> >& amount_outs, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs, uint64_t amount, size_t i);
bool add_out_to_get_random_outs(std::vector<std::pair<TransactionIndex, uint16_t>>& amount_outs, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs, uint64_t amount, size_t i);
bool is_tx_spendtime_unlocked(uint64_t unlock_time);
bool add_block_as_invalid(const block& bl, const crypto::hash& h);
bool add_block_as_invalid(const block_extended_info& bei, const crypto::hash& h);
size_t find_end_of_allowed_index(const std::vector<std::pair<crypto::hash, size_t> >& amount_outs);
size_t find_end_of_allowed_index(const std::vector<std::pair<TransactionIndex, uint16_t>>& amount_outs);
bool check_block_timestamp_main(const block& b);
bool check_block_timestamp(std::vector<uint64_t> timestamps, const block& b);
uint64_t get_adjusted_time();
bool complete_timestamps_vector(uint64_t start_height, std::vector<uint64_t>& timestamps);
bool update_next_comulative_size_limit();
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, uint64_t& starter_offset);
bool check_tx_input(const txin_to_key& txin, const crypto::hash& tx_prefix_hash, const std::vector<crypto::signature>& sig, uint64_t* pmax_related_block_height = NULL);
bool check_tx_inputs(const transaction& tx, const crypto::hash& tx_prefix_hash, uint64_t* pmax_used_block_height = NULL);
bool check_tx_inputs(const transaction& tx, uint64_t* pmax_used_block_height = NULL);
bool have_tx_keyimg_as_spent(const crypto::key_image &key_im);
const Transaction& transactionByIndex(TransactionIndex index);
bool pushBlock(const block& blockData, block_verification_context& bvc);
bool pushBlock(Block& block);
void popBlock(const crypto::hash& blockHash);
bool pushTransaction(Block& block, const crypto::hash& transactionHash, TransactionIndex transactionIndex);
void popTransaction(const transaction& transaction, const crypto::hash& transactionHash);
void popTransactions(const Block& block, const crypto::hash& minerTransactionHash);
};
/************************************************************************/
/* */
/************************************************************************/
#define CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER 12
template<class archive_t>
void blockchain_storage::serialize(archive_t & ar, const unsigned int version)
{
if(version < 11)
return;
CRITICAL_REGION_LOCAL(m_blockchain_lock);
ar & m_blocks;
ar & m_blocks_index;
ar & m_transactions;
ar & m_spent_keys;
ar & m_alternative_chains;
ar & m_outputs;
ar & m_invalid_blocks;
ar & m_current_block_cumul_sz_limit;
/*serialization bug workaround*/
if(version > 11)
{
uint64_t total_check_count = m_blocks.size() + m_blocks_index.size() + m_transactions.size() + m_spent_keys.size() + m_alternative_chains.size() + m_outputs.size() + m_invalid_blocks.size() + m_current_block_cumul_sz_limit;
if(archive_t::is_saving::value)
{
ar & total_check_count;
}else
{
uint64_t total_check_count_loaded = 0;
ar & total_check_count_loaded;
if(total_check_count != total_check_count_loaded)
{
LOG_ERROR("Blockchain storage data corruption detected. total_count loaded from file = " << total_check_count_loaded << ", expected = " << total_check_count);
LOG_PRINT_L0("Blockchain storage:" << ENDL <<
"m_blocks: " << m_blocks.size() << ENDL <<
"m_blocks_index: " << m_blocks_index.size() << ENDL <<
"m_transactions: " << m_transactions.size() << ENDL <<
"m_spent_keys: " << m_spent_keys.size() << ENDL <<
"m_alternative_chains: " << m_alternative_chains.size() << ENDL <<
"m_outputs: " << m_outputs.size() << ENDL <<
"m_invalid_blocks: " << m_invalid_blocks.size() << ENDL <<
"m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit);
throw std::runtime_error("Blockchain data corruption");
}
}
}
LOG_PRINT_L2("Blockchain storage:" << ENDL <<
"m_blocks: " << m_blocks.size() << ENDL <<
"m_blocks_index: " << m_blocks_index.size() << ENDL <<
"m_transactions: " << m_transactions.size() << ENDL <<
"m_spent_keys: " << m_spent_keys.size() << ENDL <<
"m_alternative_chains: " << m_alternative_chains.size() << ENDL <<
"m_outputs: " << m_outputs.size() << ENDL <<
"m_invalid_blocks: " << m_invalid_blocks.size() << ENDL <<
"m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit);
}
//------------------------------------------------------------------
template<class visitor_t>
bool blockchain_storage::scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t& vis, uint64_t* pmax_related_block_height)
{
template<class visitor_t> bool blockchain_storage::scan_outputkeys_for_indexes(const txin_to_key& tx_in_to_key, visitor_t& vis, uint64_t* pmax_related_block_height) {
CRITICAL_REGION_LOCAL(m_blockchain_lock);
auto it = m_outputs.find(tx_in_to_key.amount);
if(it == m_outputs.end() || !tx_in_to_key.key_offsets.size())
if (it == m_outputs.end() || !tx_in_to_key.key_offsets.size())
return false;
std::vector<uint64_t> absolute_offsets = relative_output_offsets_to_absolute(tx_in_to_key.key_offsets);
std::vector<std::pair<crypto::hash, size_t> >& amount_outs_vec = it->second;
std::vector<std::pair<TransactionIndex, uint16_t>>& amount_outs_vec = it->second;
size_t count = 0;
BOOST_FOREACH(uint64_t i, absolute_offsets)
{
if(i >= amount_outs_vec.size() )
{
for (uint64_t i : absolute_offsets) {
if(i >= amount_outs_vec.size() ) {
LOG_PRINT_L0("Wrong index in transaction inputs: " << i << ", expected maximum " << amount_outs_vec.size() - 1);
return false;
}
transactions_container::iterator tx_it = m_transactions.find(amount_outs_vec[i].first);
CHECK_AND_ASSERT_MES(tx_it != m_transactions.end(), false, "Wrong transaction id in output indexes: " << epee::string_tools::pod_to_hex(amount_outs_vec[i].first));
CHECK_AND_ASSERT_MES(amount_outs_vec[i].second < tx_it->second.tx.vout.size(), false,
"Wrong index in transaction outputs: " << amount_outs_vec[i].second << ", expected less then " << tx_it->second.tx.vout.size());
if(!vis.handle_output(tx_it->second.tx, tx_it->second.tx.vout[amount_outs_vec[i].second]))
{
//auto tx_it = m_transactionMap.find(amount_outs_vec[i].first);
//CHECK_AND_ASSERT_MES(tx_it != m_transactionMap.end(), false, "Wrong transaction id in output indexes: " << epee::string_tools::pod_to_hex(amount_outs_vec[i].first));
const Transaction& tx = transactionByIndex(amount_outs_vec[i].first);
CHECK_AND_ASSERT_MES(amount_outs_vec[i].second < tx.tx.vout.size(), false,
"Wrong index in transaction outputs: " << amount_outs_vec[i].second << ", expected less then " << tx.tx.vout.size());
if (!vis.handle_output(tx.tx, tx.tx.vout[amount_outs_vec[i].second])) {
LOG_PRINT_L0("Failed to handle_output for output no = " << count << ", with absolute offset " << i);
return false;
}
if(count++ == absolute_offsets.size()-1 && pmax_related_block_height)
{
if(*pmax_related_block_height < tx_it->second.m_keeper_block_height)
*pmax_related_block_height = tx_it->second.m_keeper_block_height;
if(count++ == absolute_offsets.size()-1 && pmax_related_block_height) {
if (*pmax_related_block_height < amount_outs_vec[i].first.block) {
*pmax_related_block_height = amount_outs_vec[i].first.block;
}
}
}
@ -322,6 +234,5 @@ namespace cryptonote
}
}
BOOST_CLASS_VERSION(cryptonote::blockchain_storage, CURRENT_BLOCKCHAIN_STORAGE_ARCHIVE_VER)
#include "cryptonote_boost_serialization.h"
#include "blockchain_storage_boost_serialization.h"

View file

@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
/*
namespace boost
{
namespace serialization
@ -31,3 +31,4 @@ namespace boost
}
}
*/

View file

@ -26,6 +26,7 @@ namespace cryptonote {
ADD_CHECKPOINT(468000, "251bcbd398b1f593193a7210934a3d87f692b2cb0c45206150f59683dd7e9ba1");
ADD_CHECKPOINT(480200, "363544ac9920c778b815c2fdbcbca70a0d79b21f662913a42da9b49e859f0e5b");
ADD_CHECKPOINT(484500, "5cdf2101a0a62a0ab2a1ca0c15a6212b21f6dbdc42a0b7c0bcf65ca40b7a14fb");
ADD_CHECKPOINT(506000, "3d54c1132f503d98d3f0d78bb46a4503c1a19447cb348361a2232e241cb45a3c");
return true;
}

View file

@ -34,7 +34,7 @@ namespace cryptonote {
}
//-----------------------------------------------------------------------------------------------
bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward) {
uint64_t base_reward = (MONEY_SUPPLY - already_generated_coins) >> 18;
uint64_t base_reward = (MONEY_SUPPLY - already_generated_coins) >> EMISSION_SPEED_FACTOR;
//make it soft
if (median_size < CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) {
@ -94,24 +94,17 @@ namespace cryptonote {
return true;
}
//-----------------------------------------------------------------------
bool get_account_address_from_str(account_public_address& adr, const std::string& str)
bool get_account_address_from_str(uint64_t& prefix, account_public_address& adr, const std::string& str)
{
if (2 * sizeof(public_address_outer_blob) != str.size())
{
blobdata data;
uint64_t prefix;
if (!tools::base58::decode_addr(str, prefix, data))
{
LOG_PRINT_L1("Invalid address format");
return false;
}
if (CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX != prefix)
{
LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX);
return false;
}
if (!::serialization::parse_binary(data, adr))
{
LOG_PRINT_L1("Account public address keys can't be parsed");
@ -127,6 +120,8 @@ namespace cryptonote {
else
{
// Old address format
prefix = CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX;
std::string buff;
if(!string_tools::parse_hexstr_to_binbuff(str, buff))
return false;
@ -158,6 +153,22 @@ namespace cryptonote {
return true;
}
//-----------------------------------------------------------------------
bool get_account_address_from_str(account_public_address& adr, const std::string& str)
{
uint64_t prefix;
if(!get_account_address_from_str(prefix, adr, str))
return false;
if(CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX != prefix)
{
LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX);
return false;
}
return true;
}
//-----------------------------------------------------------------------
bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) {
return cryptonote::get_transaction_hash(a) == cryptonote::get_transaction_hash(b);

View file

@ -41,6 +41,7 @@ namespace cryptonote {
bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward);
uint8_t get_account_address_checksum(const public_address_outer_blob& bl);
std::string get_account_address_as_str(const account_public_address& adr);
bool get_account_address_from_str(uint64_t& prefix, account_public_address& adr, const std::string& str);
bool get_account_address_from_str(account_public_address& adr, const std::string& str);
bool is_coinbase(const transaction& tx);

View file

@ -283,10 +283,10 @@ namespace cryptonote
return m_blockchain_storage.get_total_transactions();
}
//-----------------------------------------------------------------------------------------------
bool core::get_outs(uint64_t amount, std::list<crypto::public_key>& pkeys)
{
return m_blockchain_storage.get_outs(amount, pkeys);
}
//bool core::get_outs(uint64_t amount, std::list<crypto::public_key>& pkeys)
//{
// return m_blockchain_storage.get_outs(amount, pkeys);
//}
//-----------------------------------------------------------------------------------------------
bool core::add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block)
{
@ -397,15 +397,10 @@ namespace cryptonote
{
m_miner.on_synchronized();
}
bool core::get_backward_blocks_sizes(uint64_t from_height, std::vector<size_t>& sizes, size_t count)
{
return m_blockchain_storage.get_backward_blocks_sizes(from_height, sizes, count);
}
//-----------------------------------------------------------------------------------------------
bool core::add_new_block(const block& b, block_verification_context& bvc)
{
return m_blockchain_storage.add_new_block(b, bvc);
}
//bool core::get_backward_blocks_sizes(uint64_t from_height, std::vector<size_t>& sizes, size_t count)
//{
// return m_blockchain_storage.get_backward_blocks_sizes(from_height, sizes, count);
//}
//-----------------------------------------------------------------------------------------------
bool core::handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate)
{
@ -417,7 +412,6 @@ namespace cryptonote
return false;
}
block b = AUTO_VAL_INIT(b);
if(!parse_and_validate_block_from_blob(block_blob, b))
{
@ -425,9 +419,30 @@ namespace cryptonote
bvc.m_verifivation_failed = true;
return false;
}
add_new_block(b, bvc);
if(update_miner_blocktemplate && bvc.m_added_to_main_chain)
update_miner_block_template();
m_blockchain_storage.add_new_block(b, bvc);
if (bvc.m_added_to_main_chain) {
cryptonote_connection_context exclude_context = boost::value_initialized<cryptonote_connection_context>();
NOTIFY_NEW_BLOCK::request arg = AUTO_VAL_INIT(arg);
arg.hop = 0;
arg.current_blockchain_height = m_blockchain_storage.get_current_blockchain_height();
std::list<crypto::hash> missed_txs;
std::list<transaction> txs;
m_blockchain_storage.get_transactions(b.tx_hashes, txs, missed_txs);
if (missed_txs.size() > 0 && m_blockchain_storage.get_block_id_by_height(get_block_height(b)) != get_block_hash(b)) {
LOG_PRINT_L0("Block found but reorganize happened after that, block will not be relayed");
} else {
CHECK_AND_ASSERT_MES(txs.size() == b.tx_hashes.size() && !missed_txs.size(), false, "cant find some transactions in found block:" << get_block_hash(b) << " txs.size()=" << txs.size() << ", b.tx_hashes.size()=" << b.tx_hashes.size() << ", missed_txs.size()" << missed_txs.size());
block_to_blob(b, arg.b.block);
BOOST_FOREACH(auto& tx, txs) arg.b.txs.push_back(t_serializable_object_to_blob(tx));
m_pprotocol->relay_block(arg, exclude_context);
}
if (update_miner_blocktemplate) {
update_miner_block_template();
}
}
return true;
}
//-----------------------------------------------------------------------------------------------
@ -480,9 +495,9 @@ namespace cryptonote
return m_blockchain_storage.get_block_by_hash(h, blk);
}
//-----------------------------------------------------------------------------------------------
void core::get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid) {
m_blockchain_storage.get_all_known_block_ids(main, alt, invalid);
}
//void core::get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid) {
// m_blockchain_storage.get_all_known_block_ids(main, alt, invalid);
//}
//-----------------------------------------------------------------------------------------------
std::string core::print_pool(bool short_format)
{

View file

@ -59,7 +59,7 @@ namespace cryptonote
bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<transaction>& txs, std::list<crypto::hash>& missed_txs);
bool get_transaction(const crypto::hash &h, transaction &tx);
bool get_block_by_hash(const crypto::hash &h, block &blk);
void get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid);
//void get_all_known_block_ids(std::list<crypto::hash> &main, std::list<crypto::hash> &alt, std::list<crypto::hash> &invalid);
bool get_alternative_blocks(std::list<block>& blocks);
size_t get_alternative_blocks_count();
@ -70,13 +70,13 @@ namespace cryptonote
bool get_pool_transactions(std::list<transaction>& txs);
size_t get_pool_transactions_count();
size_t get_blockchain_total_transactions();
bool get_outs(uint64_t amount, std::list<crypto::public_key>& pkeys);
//bool get_outs(uint64_t amount, std::list<crypto::public_key>& pkeys);
bool have_block(const crypto::hash& id);
bool get_short_chain_history(std::list<crypto::hash>& ids);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp);
bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::list<std::pair<block, std::list<transaction> > >& blocks, uint64_t& total_height, uint64_t& start_height, size_t max_count);
bool get_stat_info(core_stat_info& st_inf);
bool get_backward_blocks_sizes(uint64_t from_height, std::vector<size_t>& sizes, size_t count);
//bool get_backward_blocks_sizes(uint64_t from_height, std::vector<size_t>& sizes, size_t count);
bool get_tx_outputs_gindexs(const crypto::hash& tx_id, std::vector<uint64_t>& indexs);
crypto::hash get_tail_id();
bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::response& res);
@ -93,7 +93,6 @@ namespace cryptonote
private:
bool add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_tx(const transaction& tx, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_block(const block& b, block_verification_context& bvc);
bool load_state_data();
bool parse_tx_from_blob(transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash, const blobdata& blob);

View file

@ -591,18 +591,23 @@ namespace cryptonote
return get_object_hash(t, res, blob_size);
}
//---------------------------------------------------------------
blobdata get_block_hashing_blob(const block& b)
bool get_block_hashing_blob(const block& b, blobdata& blob)
{
blobdata blob = t_serializable_object_to_blob(static_cast<block_header>(b));
if(!t_serializable_object_to_blob(static_cast<const block_header&>(b), blob))
return false;
crypto::hash tree_root_hash = get_tx_tree_hash(b);
blob.append((const char*)&tree_root_hash, sizeof(tree_root_hash ));
blob.append(tools::get_varint_data(b.tx_hashes.size()+1));
return blob;
blob.append(reinterpret_cast<const char*>(&tree_root_hash), sizeof(tree_root_hash));
blob.append(tools::get_varint_data(b.tx_hashes.size() + 1));
return true;
}
//---------------------------------------------------------------
bool get_block_hash(const block& b, crypto::hash& res)
{
return get_object_hash(get_block_hashing_blob(b), res);
blobdata blob;
if (!get_block_hashing_blob(b, blob))
return false;
return get_object_hash(blob, res);
}
//---------------------------------------------------------------
crypto::hash get_block_hash(const block& b)
@ -641,8 +646,9 @@ namespace cryptonote
//---------------------------------------------------------------
bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height)
{
block b_local = b; //workaround to avoid const errors with do_serialize
blobdata bd = get_block_hashing_blob(b);
blobdata bd;
if(!get_block_hashing_blob(b, bd))
return false;
crypto::cn_slow_hash(bd.data(), bd.size(), res);
return true;
}

View file

@ -74,7 +74,7 @@ namespace cryptonote
crypto::hash get_transaction_hash(const transaction& t);
bool get_transaction_hash(const transaction& t, crypto::hash& res);
bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size);
blobdata get_block_hashing_blob(const block& b);
bool get_block_hashing_blob(const block& b, blobdata& blob);
bool get_block_hash(const block& b, crypto::hash& res);
crypto::hash get_block_hash(const block& b);
bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
@ -85,7 +85,6 @@ namespace cryptonote
uint64_t get_outs_money_amount(const transaction& tx);
bool check_inputs_types_supported(const transaction& tx);
bool check_outs_valid(const transaction& tx);
blobdata get_block_hashing_blob(const block& b);
bool parse_amount(uint64_t& amount, const std::string& str_amount);
bool check_money_overflow(const transaction& tx);

View file

@ -31,7 +31,7 @@ namespace cryptonote
{
const command_line::arg_descriptor<std::string> arg_extra_messages = {"extra-messages-file", "Specify file for extra messages to include into coinbase transactions", "", true};
const command_line::arg_descriptor<std::string> arg_start_mining = {"start-mining", "Specify wallet address to mining for", "", true};
const command_line::arg_descriptor<uint32_t> arg_mining_threads = {"mining-threads", "Specify mining threads count", 0, true};
const command_line::arg_descriptor<uint32_t> arg_mining_threads = {"mining-threads", "Specify mining threads count", 0, true};
}
@ -340,7 +340,7 @@ namespace cryptonote
crypto::hash h;
get_block_longhash(b, h, height);
if(check_hash(h, local_diff))
if(!m_stop && check_hash(h, local_diff))
{
//we lucky!
++m_config.current_extra_message_index;
@ -354,6 +354,7 @@ namespace cryptonote
epee::serialization::store_t_to_json_file(m_config, m_config_folder_path + "/" + MINER_CONFIG_FILE_NAME);
}
}
nonce+=m_threads_total;
++m_hashes;
}

View file

@ -130,7 +130,7 @@ namespace nodetool
if (!parse_peers_and_add_to_container(vm, arg_p2p_add_exclusive_node, m_exclusive_peers))
return false;
}
else if (command_line::has_arg(vm, arg_p2p_add_priority_node))
if (command_line::has_arg(vm, arg_p2p_add_priority_node))
{
if (!parse_peers_and_add_to_container(vm, arg_p2p_add_priority_node, m_priority_peers))
return false;

View file

@ -331,7 +331,7 @@ namespace cryptonote
return false;
}
if(req.reserve_size > 255)
if(req.reserve_size > TX_EXTRA_NONCE_MAX_COUNT)
{
error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE;
error_resp.message = "To big reserved size, maximum 255";
@ -357,31 +357,41 @@ namespace cryptonote
LOG_ERROR("Failed to create block template");
return false;
}
blobdata block_blob = t_serializable_object_to_blob(b);
crypto::public_key tx_pub_key = cryptonote::get_tx_pub_key_from_extra(b.miner_tx);
if(tx_pub_key == null_pkey)
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: failed to create block template";
LOG_ERROR("Failed to tx pub key in coinbase extra");
LOG_ERROR("Failed to find tx pub key in coinbase extra");
return false;
}
res.reserved_offset = slow_memmem((void*)block_blob.data(), block_blob.size(), &tx_pub_key, sizeof(tx_pub_key));
if(!res.reserved_offset)
if(0 < req.reserve_size)
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: failed to create block template";
LOG_ERROR("Failed to find tx pub key in blockblob");
return false;
res.reserved_offset = slow_memmem((void*)block_blob.data(), block_blob.size(), &tx_pub_key, sizeof(tx_pub_key));
if(!res.reserved_offset)
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: failed to create block template";
LOG_ERROR("Failed to find tx pub key in blockblob");
return false;
}
res.reserved_offset += sizeof(tx_pub_key) + 3; //3 bytes: tag for TX_EXTRA_TAG_PUBKEY(1 byte), tag for TX_EXTRA_NONCE(1 byte), counter in TX_EXTRA_NONCE(1 byte)
if(res.reserved_offset + req.reserve_size > block_blob.size())
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: failed to create block template";
LOG_ERROR("Failed to calculate offset for reserved bytes");
return false;
}
}
res.reserved_offset += sizeof(tx_pub_key) + 3; //3 bytes: tag for TX_EXTRA_TAG_PUBKEY(1 byte), tag for TX_EXTRA_NONCE(1 byte), counter in TX_EXTRA_NONCE(1 byte)
if(res.reserved_offset + req.reserve_size > block_blob.size())
else
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Internal error: failed to create block template";
LOG_ERROR("Failed to calculate offset for ");
return false;
res.reserved_offset = 0;
}
res.blocktemplate_blob = string_tools::buff_to_hex_nodelimer(block_blob);
res.status = CORE_RPC_STATUS_OK;
return true;

View file

@ -2,6 +2,8 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <sstream>
#include "binary_archive.h"

View file

@ -2,6 +2,8 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <vector>
#include "serialization.h"

View file

@ -2,6 +2,8 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <sstream>
#include "json_archive.h"

View file

@ -16,6 +16,13 @@ namespace serialization
return ::do_serialize(ar, e);
}
template <typename Archive>
bool serialize_vector_element(Archive& ar, uint32_t& e)
{
ar.serialize_varint(e);
return true;
}
template <typename Archive>
bool serialize_vector_element(Archive& ar, uint64_t& e)
{

View file

@ -1,4 +1,4 @@
#define BUILD_COMMIT_ID "@VERSION@"
#define PROJECT_VERSION "0.8.9"
#define PROJECT_VERSION "0.8.10"
#define PROJECT_VERSION_BUILD_NO "65"
#define PROJECT_VERSION_LONG PROJECT_VERSION "." PROJECT_VERSION_BUILD_NO "(" BUILD_COMMIT_ID ")"

View file

@ -55,6 +55,9 @@ namespace tools
template<typename Base>
struct wallet_error_base : public Base
{
// This is necessary to compile with g++ 4.7.3, because of ~std::string() (m_loc) can throw an exception
~wallet_error_base() throw() { }
const std::string& location() const { return m_loc; }
std::string to_string() const
@ -96,6 +99,8 @@ namespace tools
{
}
~failed_rpc_request() throw() { }
const std::string& status() const { return m_status; }
std::string to_string() const
@ -128,6 +133,8 @@ namespace tools
{
}
~unexpected_txin_type() throw() { }
const cryptonote::transaction& tx() const { return m_tx; }
std::string to_string() const
@ -165,6 +172,8 @@ namespace tools
{
}
~file_error_base() throw() { }
const std::string& file() const { return m_file; }
std::string to_string() const { return wallet_logic_error::to_string(); }
@ -192,7 +201,7 @@ namespace tools
struct refresh_error : public wallet_logic_error
{
protected:
refresh_error(std::string&& loc, const std::string& message)
explicit refresh_error(std::string&& loc, const std::string& message)
: wallet_logic_error(std::move(loc), message)
{
}
@ -209,6 +218,8 @@ namespace tools
{
}
~acc_outs_lookup_error() throw() { }
const cryptonote::transaction& tx() const { return m_tx; }
const crypto::public_key& tx_pub_key() const { return m_tx_pub_key; }
const cryptonote::account_keys& acc_keys() const { return m_acc_keys; }
@ -235,6 +246,8 @@ namespace tools
{
}
~block_parse_error() throw() { }
const cryptonote::blobdata& block_blob() const { return m_block_blob; }
std::string to_string() const { return refresh_error::to_string(); }
@ -255,6 +268,8 @@ namespace tools
{
}
~tx_parse_error() throw() { }
const cryptonote::blobdata& tx_blob() const { return m_tx_blob; }
std::string to_string() const { return refresh_error::to_string(); }
@ -266,7 +281,7 @@ namespace tools
struct transfer_error : public wallet_logic_error
{
protected:
transfer_error(std::string&& loc, const std::string& message)
explicit transfer_error(std::string&& loc, const std::string& message)
: wallet_logic_error(std::move(loc), message)
{
}
@ -276,7 +291,7 @@ namespace tools
//----------------------------------------------------------------------------------------------------
struct not_enough_money : public transfer_error
{
not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee)
explicit not_enough_money(std::string&& loc, uint64_t availbable, uint64_t tx_amount, uint64_t fee)
: transfer_error(std::move(loc), "not enough money")
, m_available(availbable)
, m_tx_amount(tx_amount)
@ -315,6 +330,8 @@ namespace tools
{
}
~not_enough_outs_to_mix() throw() { }
const scanty_outs_t& scanty_outs() const { return m_scanty_outs; }
size_t mixin_count() const { return m_mixin_count; }
@ -347,6 +364,8 @@ namespace tools
{
}
~tx_not_constructed() throw() { }
const sources_t& sources() const { return m_sources; }
const destinations_t& destinations() const { return m_destinations; }
uint64_t unlock_time() const { return m_unlock_time; }
@ -401,6 +420,8 @@ namespace tools
{
}
~tx_rejected() throw() { }
const cryptonote::transaction& tx() const { return m_tx; }
const std::string& status() const { return m_status; }
@ -420,13 +441,15 @@ namespace tools
//----------------------------------------------------------------------------------------------------
struct tx_sum_overflow : public transfer_error
{
tx_sum_overflow(std::string&& loc, const std::vector<cryptonote::tx_destination_entry>& destinations, uint64_t fee)
explicit tx_sum_overflow(std::string&& loc, const std::vector<cryptonote::tx_destination_entry>& destinations, uint64_t fee)
: transfer_error(std::move(loc), "transaction sum + fee exceeds " + cryptonote::print_money(std::numeric_limits<uint64_t>::max()))
, m_destinations(destinations)
, m_fee(fee)
{
}
~tx_sum_overflow() throw() { }
const std::vector<cryptonote::tx_destination_entry>& destinations() const { return m_destinations; }
uint64_t fee() const { return m_fee; }
@ -457,6 +480,8 @@ namespace tools
{
}
~tx_too_big() throw() { }
const cryptonote::transaction& tx() const { return m_tx; }
uint64_t tx_size_limit() const { return m_tx_size_limit; }
@ -486,6 +511,8 @@ namespace tools
//----------------------------------------------------------------------------------------------------
struct wallet_rpc_error : public wallet_logic_error
{
~wallet_rpc_error() throw() { }
const std::string& request() const { return m_request; }
std::string to_string() const
@ -496,7 +523,7 @@ namespace tools
}
protected:
wallet_rpc_error(std::string&& loc, const std::string& message, const std::string& request)
explicit wallet_rpc_error(std::string&& loc, const std::string& message, const std::string& request)
: wallet_logic_error(std::move(loc), message)
, m_request(request)
{
@ -529,6 +556,8 @@ namespace tools
{
}
~wallet_files_doesnt_correspond() throw() { }
const std::string& keys_file() const { return m_keys_file; }
const std::string& wallet_file() const { return m_wallet_file; }