danicoin/tests/NodeRpcProxyTests/NodeRpcProxyTests.cpp

140 lines
4.1 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2015 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-08-13 10:38:35 +00:00
2014-06-25 17:21:42 +00:00
#include <chrono>
#include <thread>
2015-07-15 12:23:00 +00:00
#include <Logging/LoggerRef.h>
#include <Logging/ConsoleLogger.h>
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
#include "NodeRpcProxy/NodeRpcProxy.h"
2014-06-25 17:21:42 +00:00
2015-05-27 12:08:46 +00:00
using namespace CryptoNote;
2015-07-15 12:23:00 +00:00
using namespace Logging;
2014-06-25 17:21:42 +00:00
2015-07-15 12:23:00 +00:00
#undef ERROR
2014-06-25 17:21:42 +00:00
class NodeObserver : public INodeObserver {
public:
2015-07-15 12:23:00 +00:00
NodeObserver(const std::string& name, NodeRpcProxy& nodeProxy, ILogger& log)
2014-06-25 17:21:42 +00:00
: m_name(name)
2015-07-15 12:23:00 +00:00
, m_nodeProxy(nodeProxy)
, logger(log, "NodeObserver:" + name) {
2014-06-25 17:21:42 +00:00
}
virtual ~NodeObserver() {
}
virtual void peerCountUpdated(size_t count) {
2015-07-15 12:23:00 +00:00
logger(INFO) << '[' << m_name << "] peerCountUpdated " << count << " = " << m_nodeProxy.getPeerCount();
2014-06-25 17:21:42 +00:00
}
2015-07-30 15:22:07 +00:00
virtual void localBlockchainUpdated(uint32_t height) override {
2015-07-15 12:23:00 +00:00
logger(INFO) << '[' << m_name << "] localBlockchainUpdated " << height << " = " << m_nodeProxy.getLastLocalBlockHeight();
2014-06-25 17:21:42 +00:00
std::vector<uint64_t> amounts;
amounts.push_back(100000000);
auto outs = std::make_shared<std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>>();
2015-07-15 12:23:00 +00:00
m_nodeProxy.getRandomOutsByAmounts(std::move(amounts), 10, *outs.get(), [outs, this](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (!ec) {
if (1 == outs->size() && 10 == (*outs)[0].outs.size()) {
2015-07-15 12:23:00 +00:00
logger(INFO) << "getRandomOutsByAmounts called successfully";
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "getRandomOutsByAmounts returned invalid result";
2014-06-25 17:21:42 +00:00
}
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "failed to call getRandomOutsByAmounts: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
}
});
}
2015-07-30 15:22:07 +00:00
virtual void lastKnownBlockHeightUpdated(uint32_t height) override {
2015-07-15 12:23:00 +00:00
logger(INFO) << '[' << m_name << "] lastKnownBlockHeightUpdated " << height << " = " << m_nodeProxy.getLastKnownBlockHeight();
2014-06-25 17:21:42 +00:00
}
private:
2015-07-15 12:23:00 +00:00
LoggerRef logger;
2014-06-25 17:21:42 +00:00
std::string m_name;
NodeRpcProxy& m_nodeProxy;
};
int main(int argc, const char** argv) {
2015-07-15 12:23:00 +00:00
Logging::ConsoleLogger log;
Logging::LoggerRef logger(log, "main");
2014-06-25 17:21:42 +00:00
NodeRpcProxy nodeProxy("127.0.0.1", 18081);
2015-07-15 12:23:00 +00:00
NodeObserver observer1("obs1", nodeProxy, log);
NodeObserver observer2("obs2", nodeProxy, log);
2014-06-25 17:21:42 +00:00
nodeProxy.addObserver(&observer1);
nodeProxy.addObserver(&observer2);
2015-07-15 12:23:00 +00:00
nodeProxy.init([&](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (ec) {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "init error: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(INFO, BRIGHT_GREEN) << "initialized";
2014-06-25 17:21:42 +00:00
}
});
//nodeProxy.init([](std::error_code ec) {
// if (ec) {
2015-07-15 12:23:00 +00:00
// logger(ERROR) << "init error: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
// } else {
// LOG_PRINT_GREEN("initialized", LOG_LEVEL_0);
// }
//});
std::this_thread::sleep_for(std::chrono::seconds(5));
if (nodeProxy.shutdown()) {
2015-07-15 12:23:00 +00:00
logger(INFO, BRIGHT_GREEN) << "shutdown";
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "shutdown error";
2014-06-25 17:21:42 +00:00
}
2015-07-15 12:23:00 +00:00
nodeProxy.init([&](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (ec) {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "init error: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(INFO, BRIGHT_GREEN) << "initialized";
2014-06-25 17:21:42 +00:00
}
});
std::this_thread::sleep_for(std::chrono::seconds(5));
if (nodeProxy.shutdown()) {
2015-07-15 12:23:00 +00:00
logger(INFO, BRIGHT_GREEN) << "shutdown";
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "shutdown error";
2014-06-25 17:21:42 +00:00
}
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction tx;
2015-07-15 12:23:00 +00:00
nodeProxy.relayTransaction(tx, [&](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (!ec) {
2015-07-15 12:23:00 +00:00
logger(INFO) << "relayTransaction called successfully";
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "failed to call relayTransaction: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
}
});
2015-07-15 12:23:00 +00:00
nodeProxy.init([&](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (ec) {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "init error: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(INFO, BRIGHT_GREEN) << "initialized";
2014-06-25 17:21:42 +00:00
}
});
std::this_thread::sleep_for(std::chrono::seconds(5));
2015-07-15 12:23:00 +00:00
nodeProxy.relayTransaction(tx, [&](std::error_code ec) {
2014-06-25 17:21:42 +00:00
if (!ec) {
2015-07-15 12:23:00 +00:00
logger(INFO) << "relayTransaction called successfully";
2014-06-25 17:21:42 +00:00
} else {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "failed to call relayTransaction: " << ec.message() << ':' << ec.value();
2014-06-25 17:21:42 +00:00
}
});
std::this_thread::sleep_for(std::chrono::seconds(60));
}