// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// 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 .
#include "NodeFactory.h"
#include "NodeRpcProxy/NodeRpcProxy.h"
#include
#include
namespace PaymentService {
class NodeRpcStub: public CryptoNote::INode {
public:
virtual ~NodeRpcStub() {}
virtual bool addObserver(CryptoNote::INodeObserver* observer) { return true; }
virtual bool removeObserver(CryptoNote::INodeObserver* observer) { return true; }
virtual void init(const Callback& callback) { }
virtual bool shutdown() { return true; }
virtual size_t getPeerCount() const { return 0; }
virtual uint32_t getLastLocalBlockHeight() const override { return 0; }
virtual uint32_t getLastKnownBlockHeight() const override { return 0; }
virtual uint32_t getLocalBlockCount() const override { return 0; }
virtual uint32_t getKnownBlockCount() const override { return 0; }
virtual uint64_t getLastLocalBlockTimestamp() const override { return 0; }
virtual void relayTransaction(const CryptoNote::Transaction& transaction, const Callback& callback) override { callback(std::error_code()); }
virtual void getRandomOutsByAmounts(std::vector&& amounts, uint64_t outsCount,
std::vector& result, const Callback& callback) override {
}
virtual void getNewBlocks(std::vector&& knownBlockIds, std::vector& newBlocks, uint32_t& startHeight, const Callback& callback) override {
startHeight = 0;
callback(std::error_code());
}
virtual void getTransactionOutsGlobalIndices(const Crypto::Hash& transactionHash, std::vector& outsGlobalIndices, const Callback& callback) override { }
virtual void queryBlocks(std::vector&& knownBlockIds, uint64_t timestamp, std::vector& newBlocks,
uint32_t& startHeight, const Callback& callback) override {
startHeight = 0;
callback(std::error_code());
};
virtual void getPoolSymmetricDifference(std::vector&& knownPoolTxIds, Crypto::Hash knownBlockId, bool& isBcActual,
std::vector>& newTxs, std::vector& deletedTxIds, const Callback& callback) {
isBcActual = true;
callback(std::error_code());
}
virtual void getBlocks(const std::vector& blockHeights, std::vector>& blocks,
const Callback& callback) override { }
virtual void getBlocks(const std::vector& blockHashes, std::vector& blocks,
const Callback& callback) override { }
virtual void getBlocks(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t blocksNumberLimit, std::vector& blocks, uint32_t& blocksNumberWithinTimestamps,
const Callback& callback) override { }
virtual void getTransactions(const std::vector& transactionHashes, std::vector& transactions,
const Callback& callback) override { }
virtual void getPoolTransactions(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t transactionsNumberLimit, std::vector& transactions, uint64_t& transactionsNumberWithinTimestamps,
const Callback& callback) override { }
virtual void getTransactionsByPaymentId(const Crypto::Hash& paymentId, std::vector& transactions,
const Callback& callback) override { }
virtual void getMultisignatureOutputByGlobalIndex(uint64_t amount, uint32_t gindex, CryptoNote::MultisignatureOutput& out,
const Callback& callback) override { }
virtual void isSynchronized(bool& syncStatus, const Callback& callback) override { }
};
class NodeInitObserver {
public:
NodeInitObserver() {
initFuture = initPromise.get_future();
}
void initCompleted(std::error_code result) {
initPromise.set_value(result);
}
void waitForInitEnd() {
std::error_code ec = initFuture.get();
if (ec) {
throw std::system_error(ec);
}
return;
}
private:
std::promise initPromise;
std::future initFuture;
};
NodeFactory::NodeFactory() {
}
NodeFactory::~NodeFactory() {
}
CryptoNote::INode* NodeFactory::createNode(const std::string& daemonAddress, uint16_t daemonPort) {
std::unique_ptr node(new CryptoNote::NodeRpcProxy(daemonAddress, daemonPort));
NodeInitObserver initObserver;
node->init(std::bind(&NodeInitObserver::initCompleted, &initObserver, std::placeholders::_1));
initObserver.waitForInitEnd();
return node.release();
}
CryptoNote::INode* NodeFactory::createNodeStub() {
return new NodeRpcStub();
}
}