2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2015-04-06 16:13:07 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "TransfersSynchronizer.h"
|
|
|
|
#include "TransfersConsumer.h"
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "Common/StdInputStream.h"
|
|
|
|
#include "Common/StdOutputStream.h"
|
|
|
|
#include "Serialization/BinaryInputStreamSerializer.h"
|
|
|
|
#include "Serialization/BinaryOutputStreamSerializer.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
using namespace Common;
|
|
|
|
using namespace Crypto;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
namespace CryptoNote {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
const uint32_t TRANSFERS_STORAGE_ARCHIVE_VERSION = 0;
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
TransfersSyncronizer::TransfersSyncronizer(const CryptoNote::Currency& currency, IBlockchainSynchronizer& sync, INode& node) :
|
2015-04-06 16:13:07 +00:00
|
|
|
m_currency(currency), m_sync(sync), m_node(node) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TransfersSyncronizer::~TransfersSyncronizer() {
|
|
|
|
m_sync.stop();
|
|
|
|
for (const auto& kv : m_consumers) {
|
|
|
|
m_sync.removeConsumer(kv.second.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ITransfersSubscription& TransfersSyncronizer::addSubscription(const AccountSubscription& acc) {
|
|
|
|
auto it = m_consumers.find(acc.keys.address.viewPublicKey);
|
|
|
|
|
|
|
|
if (it == m_consumers.end()) {
|
|
|
|
std::unique_ptr<TransfersConsumer> consumer(
|
|
|
|
new TransfersConsumer(m_currency, m_node, acc.keys.viewSecretKey));
|
|
|
|
m_sync.addConsumer(consumer.get());
|
|
|
|
it = m_consumers.insert(std::make_pair(acc.keys.address.viewPublicKey, std::move(consumer))).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->second->addSubscription(acc);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
bool TransfersSyncronizer::removeSubscription(const AccountPublicAddress& acc) {
|
2015-04-06 16:13:07 +00:00
|
|
|
auto it = m_consumers.find(acc.viewPublicKey);
|
|
|
|
if (it == m_consumers.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (it->second->removeSubscription(acc)) {
|
|
|
|
m_sync.removeConsumer(it->second.get());
|
|
|
|
m_consumers.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
void TransfersSyncronizer::getSubscriptions(std::vector<AccountPublicAddress>& subscriptions) {
|
2015-04-06 16:13:07 +00:00
|
|
|
for (const auto& kv : m_consumers) {
|
|
|
|
kv.second->getSubscriptions(subscriptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
ITransfersSubscription* TransfersSyncronizer::getSubscription(const AccountPublicAddress& acc) {
|
2015-04-06 16:13:07 +00:00
|
|
|
auto it = m_consumers.find(acc.viewPublicKey);
|
|
|
|
return (it == m_consumers.end()) ? 0 : it->second->getSubscription(acc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransfersSyncronizer::save(std::ostream& os) {
|
|
|
|
m_sync.save(os);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
StdOutputStream stream(os);
|
|
|
|
CryptoNote::BinaryOutputStreamSerializer s(stream);
|
2015-04-06 16:13:07 +00:00
|
|
|
s(const_cast<uint32_t&>(TRANSFERS_STORAGE_ARCHIVE_VERSION), "version");
|
|
|
|
|
|
|
|
size_t subscriptionCount = m_consumers.size();
|
|
|
|
|
|
|
|
s.beginArray(subscriptionCount, "consumers");
|
|
|
|
|
|
|
|
for (const auto& consumer : m_consumers) {
|
|
|
|
s.beginObject("");
|
|
|
|
s(const_cast<PublicKey&>(consumer.first), "view_key");
|
|
|
|
|
|
|
|
std::stringstream consumerState;
|
|
|
|
// synchronization state
|
|
|
|
m_sync.getConsumerState(consumer.second.get())->save(consumerState);
|
|
|
|
|
|
|
|
std::string blob = consumerState.str();
|
|
|
|
s(blob, "state");
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<AccountPublicAddress> subscriptions;
|
2015-04-06 16:13:07 +00:00
|
|
|
consumer.second->getSubscriptions(subscriptions);
|
|
|
|
size_t subCount = subscriptions.size();
|
|
|
|
|
|
|
|
s.beginArray(subCount, "subscriptions");
|
|
|
|
|
|
|
|
for (auto& addr : subscriptions) {
|
|
|
|
auto sub = consumer.second->getSubscription(addr);
|
|
|
|
if (sub != nullptr) {
|
|
|
|
s.beginObject("");
|
|
|
|
|
|
|
|
std::stringstream subState;
|
|
|
|
assert(sub);
|
|
|
|
sub->getContainer().save(subState);
|
|
|
|
// store data block
|
|
|
|
std::string blob = subState.str();
|
|
|
|
s(addr, "address");
|
|
|
|
s(blob, "state");
|
|
|
|
|
|
|
|
s.endObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.endArray();
|
|
|
|
s.endObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
std::string getObjectState(IStreamSerializable& obj) {
|
|
|
|
std::stringstream stream;
|
|
|
|
obj.save(stream);
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setObjectState(IStreamSerializable& obj, const std::string& state) {
|
|
|
|
std::stringstream stream(state);
|
|
|
|
obj.load(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransfersSyncronizer::load(std::istream& is) {
|
|
|
|
m_sync.load(is);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
StdInputStream inputStream(is);
|
|
|
|
CryptoNote::BinaryInputStreamSerializer s(inputStream);
|
2015-04-06 16:13:07 +00:00
|
|
|
uint32_t version = 0;
|
|
|
|
|
|
|
|
s(version, "version");
|
|
|
|
|
|
|
|
if (version > TRANSFERS_STORAGE_ARCHIVE_VERSION) {
|
|
|
|
throw std::runtime_error("TransfersSyncronizer version mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ConsumerState {
|
|
|
|
PublicKey viewKey;
|
|
|
|
std::string state;
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<std::pair<AccountPublicAddress, std::string>> subscriptionStates;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<ConsumerState> updatedStates;
|
|
|
|
|
|
|
|
try {
|
|
|
|
size_t subscriptionCount = 0;
|
|
|
|
s.beginArray(subscriptionCount, "consumers");
|
|
|
|
|
|
|
|
while (subscriptionCount--) {
|
|
|
|
s.beginObject("");
|
|
|
|
PublicKey viewKey;
|
|
|
|
s(viewKey, "view_key");
|
|
|
|
|
|
|
|
std::string blob;
|
|
|
|
s(blob, "state");
|
|
|
|
|
|
|
|
auto subIter = m_consumers.find(viewKey);
|
|
|
|
if (subIter != m_consumers.end()) {
|
|
|
|
auto consumerState = m_sync.getConsumerState(subIter->second.get());
|
|
|
|
assert(consumerState);
|
|
|
|
|
|
|
|
{
|
|
|
|
// store previous state
|
|
|
|
auto prevConsumerState = getObjectState(*consumerState);
|
|
|
|
// load consumer state
|
|
|
|
setObjectState(*consumerState, blob);
|
|
|
|
updatedStates.push_back(ConsumerState{ viewKey, std::move(prevConsumerState) });
|
|
|
|
}
|
|
|
|
|
|
|
|
// load subscriptions
|
|
|
|
size_t subCount = 0;
|
|
|
|
s.beginArray(subCount, "subscriptions");
|
|
|
|
|
|
|
|
while (subCount--) {
|
|
|
|
s.beginObject("");
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
AccountPublicAddress acc;
|
2015-04-06 16:13:07 +00:00
|
|
|
std::string state;
|
|
|
|
|
|
|
|
s(acc, "address");
|
|
|
|
s(state, "state");
|
|
|
|
|
|
|
|
auto sub = subIter->second->getSubscription(acc);
|
|
|
|
|
|
|
|
if (sub != nullptr) {
|
|
|
|
auto prevState = getObjectState(sub->getContainer());
|
|
|
|
setObjectState(sub->getContainer(), state);
|
|
|
|
updatedStates.back().subscriptionStates.push_back(std::make_pair(acc, prevState));
|
|
|
|
}
|
|
|
|
|
|
|
|
s.endObject();
|
|
|
|
}
|
|
|
|
s.endArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.endObject();
|
|
|
|
s.endArray();
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
// rollback state
|
|
|
|
for (const auto& consumerState : updatedStates) {
|
|
|
|
auto consumer = m_consumers.find(consumerState.viewKey)->second.get();
|
|
|
|
setObjectState(*m_sync.getConsumerState(consumer), consumerState.state);
|
|
|
|
for (const auto& sub : consumerState.subscriptionStates) {
|
|
|
|
setObjectState(consumer->getSubscription(sub.first)->getContainer(), sub.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|