send transaction over p2p protocol as groundwork
This commit is contained in:
parent
a4ec874844
commit
6cadc8a79e
3 changed files with 172 additions and 170 deletions
|
|
@ -46,11 +46,12 @@ namespace levin
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* */
|
/* */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
class levin_client_impl
|
template<typename transport_t>
|
||||||
|
class levin_client_impl_t
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
levin_client_impl();
|
levin_client_impl_t();
|
||||||
virtual ~levin_client_impl();
|
virtual ~levin_client_impl_t();
|
||||||
|
|
||||||
bool connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0");
|
bool connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0");
|
||||||
bool connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0");
|
bool connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip = "0.0.0.0");
|
||||||
|
|
@ -61,14 +62,16 @@ namespace levin
|
||||||
virtual int notify(int command, const std::string& in_buff);
|
virtual int notify(int command, const std::string& in_buff);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
net_utils::blocked_mode_client m_transport;
|
transport_t m_transport;
|
||||||
|
//net_utils::blocked_mode_client m_transport;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* */
|
/* */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
class levin_client_impl2: public levin_client_impl
|
template<typename transport_t>
|
||||||
|
class levin_client_impl2: public levin_client_impl_t<transport_t>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
@ -79,8 +82,8 @@ namespace levin
|
||||||
}
|
}
|
||||||
namespace net_utils
|
namespace net_utils
|
||||||
{
|
{
|
||||||
typedef levin::levin_client_impl levin_client;
|
typedef levin::levin_client_impl_t<net_utils::blocked_mode_client> levin_client;
|
||||||
typedef levin::levin_client_impl2 levin_client2;
|
typedef levin::levin_client_impl2<net_utils::blocked_mode_client> levin_client2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,175 +32,174 @@
|
||||||
#include "string_tools.h"
|
#include "string_tools.h"
|
||||||
namespace epee
|
namespace epee
|
||||||
{
|
{
|
||||||
namespace levin
|
namespace levin
|
||||||
{
|
|
||||||
inline
|
|
||||||
bool levin_client_impl::connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip)
|
|
||||||
{
|
|
||||||
return m_transport.connect(string_tools::get_ip_string_from_int32(ip), port, timeout, timeout, bind_ip);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
bool levin_client_impl::connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip)
|
|
||||||
{
|
|
||||||
return m_transport.connect(addr, port, timeout, timeout, bind_ip);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
bool levin_client_impl::is_connected()
|
|
||||||
{
|
|
||||||
return m_transport.is_connected();
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
bool levin_client_impl::disconnect()
|
|
||||||
{
|
|
||||||
return m_transport.disconnect();
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
levin_client_impl::levin_client_impl()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
levin_client_impl::~levin_client_impl()
|
|
||||||
{
|
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
int levin_client_impl::invoke(int command, const std::string& in_buff, std::string& buff_out)
|
|
||||||
{
|
|
||||||
if(!is_connected())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
bucket_head head = {0};
|
|
||||||
head.m_signature = LEVIN_SIGNATURE;
|
|
||||||
head.m_cb = in_buff.size();
|
|
||||||
head.m_have_to_return_data = true;
|
|
||||||
head.m_command = command;
|
|
||||||
if(!m_transport.send(&head, sizeof(head)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(!m_transport.send(in_buff))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
std::string local_buff;
|
|
||||||
if(!m_transport.recv_n(local_buff, sizeof(bucket_head)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
head = *(bucket_head*)local_buff.data();
|
|
||||||
|
|
||||||
|
|
||||||
if(head.m_signature!=LEVIN_SIGNATURE)
|
|
||||||
{
|
|
||||||
LOG_PRINT_L0("Signature missmatch in response");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!m_transport.recv_n(buff_out, head.m_cb))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return head.m_return_code;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
int levin_client_impl::notify(int command, const std::string& in_buff)
|
|
||||||
{
|
|
||||||
if(!is_connected())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
bucket_head head = {0};
|
|
||||||
head.m_signature = LEVIN_SIGNATURE;
|
|
||||||
head.m_cb = in_buff.size();
|
|
||||||
head.m_have_to_return_data = false;
|
|
||||||
head.m_command = command;
|
|
||||||
|
|
||||||
if(!m_transport.send((const char*)&head, sizeof(head)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(!m_transport.send(in_buff))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
int levin_client_impl2::invoke(int command, const std::string& in_buff, std::string& buff_out)
|
|
||||||
{
|
|
||||||
if(!is_connected())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
bucket_head2 head = {0};
|
|
||||||
head.m_signature = LEVIN_SIGNATURE;
|
|
||||||
head.m_cb = in_buff.size();
|
|
||||||
head.m_have_to_return_data = true;
|
|
||||||
head.m_command = static_cast<uint32_t>(command);
|
|
||||||
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
|
|
||||||
head.m_flags = LEVIN_PACKET_REQUEST;
|
|
||||||
if(!m_transport.send(&head, sizeof(head)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(!m_transport.send(in_buff))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
//Since other side of connection could be running by async server,
|
|
||||||
//we can receive some unexpected notify(forwarded broadcast notifications for example).
|
|
||||||
//let's ignore every notify in the channel until we get invoke response
|
|
||||||
std::string local_buff;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (!m_transport.recv_n(local_buff, sizeof(bucket_head2)))
|
|
||||||
return LEVIN_ERROR_NET_ERROR;
|
|
||||||
|
|
||||||
head = *(bucket_head2*)local_buff.data();
|
template<typename transport_t>
|
||||||
if (head.m_signature != LEVIN_SIGNATURE)
|
bool levin_client_impl_t<transport_t>::connect(u_long ip, int port, unsigned int timeout, const std::string& bind_ip)
|
||||||
{
|
{
|
||||||
LOG_PRINT_L0("Signature missmatch in response");
|
return m_transport.connect(string_tools::get_ip_string_from_int32(ip), port, timeout, timeout, bind_ip);
|
||||||
return LEVIN_ERROR_SIGNATURE_MISMATCH;
|
|
||||||
}
|
}
|
||||||
if (!m_transport.recv_n(buff_out, head.m_cb))
|
//------------------------------------------------------------------------------
|
||||||
return LEVIN_ERROR_NET_ERROR;
|
template<typename transport_t>
|
||||||
|
bool levin_client_impl_t<transport_t>::connect(const std::string& addr, int port, unsigned int timeout, const std::string& bind_ip)
|
||||||
//now check if this is response to invoke (and extra validate if it's response to this(!) invoke)
|
|
||||||
if (head.m_flags&LEVIN_PACKET_RESPONSE)
|
|
||||||
{
|
{
|
||||||
//we got response, extra validate if its response to our request
|
return m_transport.connect(addr, port, timeout, timeout, bind_ip);
|
||||||
CHECK_AND_ASSERT_MES(head.m_command == static_cast<uint32_t>(command), LEVIN_ERROR_PROTOCOL_INCONSISTENT, "command id missmatch in response: " << head.m_command << ", expected: " << command);
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
bool levin_client_impl_t<transport_t>::is_connected()
|
||||||
|
{
|
||||||
|
return m_transport.is_connected();
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
bool levin_client_impl_t<transport_t>::disconnect()
|
||||||
|
{
|
||||||
|
return m_transport.disconnect();
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
levin_client_impl_t<transport_t>::levin_client_impl_t()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
levin_client_impl_t<transport_t>::~levin_client_impl_t()
|
||||||
|
{
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
int levin_client_impl_t<transport_t>::invoke(int command, const std::string& in_buff, std::string& buff_out)
|
||||||
|
{
|
||||||
|
if (!is_connected())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
bucket_head head = { 0 };
|
||||||
|
head.m_signature = LEVIN_SIGNATURE;
|
||||||
|
head.m_cb = in_buff.size();
|
||||||
|
head.m_have_to_return_data = true;
|
||||||
|
head.m_command = command;
|
||||||
|
if (!m_transport.send(&head, sizeof(head)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!m_transport.send(in_buff))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
std::string local_buff;
|
||||||
|
if (!m_transport.recv_n(local_buff, sizeof(bucket_head)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
head = *(bucket_head*)local_buff.data();
|
||||||
|
|
||||||
|
|
||||||
|
if (head.m_signature != LEVIN_SIGNATURE)
|
||||||
|
{
|
||||||
|
LOG_PRINT_L0("Signature missmatch in response");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_transport.recv_n(buff_out, head.m_cb))
|
||||||
|
return -1;
|
||||||
|
|
||||||
return head.m_return_code;
|
return head.m_return_code;
|
||||||
}
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
int levin_client_impl_t<transport_t>::notify(int command, const std::string& in_buff)
|
||||||
|
{
|
||||||
|
if (!is_connected())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
bucket_head head = { 0 };
|
||||||
|
head.m_signature = LEVIN_SIGNATURE;
|
||||||
|
head.m_cb = in_buff.size();
|
||||||
|
head.m_have_to_return_data = false;
|
||||||
|
head.m_command = command;
|
||||||
|
|
||||||
|
if (!m_transport.send((const char*)&head, sizeof(head)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!m_transport.send(in_buff))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
int levin_client_impl2<transport_t>::invoke(int command, const std::string& in_buff, std::string& buff_out)
|
||||||
|
{
|
||||||
|
if (!is_connected())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
bucket_head2 head = { 0 };
|
||||||
|
head.m_signature = LEVIN_SIGNATURE;
|
||||||
|
head.m_cb = in_buff.size();
|
||||||
|
head.m_have_to_return_data = true;
|
||||||
|
head.m_command = static_cast<uint32_t>(command);
|
||||||
|
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
|
||||||
|
head.m_flags = LEVIN_PACKET_REQUEST;
|
||||||
|
if (!m_transport.send(&head, sizeof(head)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!m_transport.send(in_buff))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
//Since other side of connection could be running by async server,
|
||||||
|
//we can receive some unexpected notify(forwarded broadcast notifications for example).
|
||||||
|
//let's ignore every notify in the channel until we get invoke response
|
||||||
|
std::string local_buff;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!m_transport.recv_n(local_buff, sizeof(bucket_head2)))
|
||||||
|
return LEVIN_ERROR_NET_ERROR;
|
||||||
|
|
||||||
|
head = *(bucket_head2*)local_buff.data();
|
||||||
|
if (head.m_signature != LEVIN_SIGNATURE)
|
||||||
|
{
|
||||||
|
LOG_PRINT_L0("Signature missmatch in response");
|
||||||
|
return LEVIN_ERROR_SIGNATURE_MISMATCH;
|
||||||
|
}
|
||||||
|
if (!m_transport.recv_n(buff_out, head.m_cb))
|
||||||
|
return LEVIN_ERROR_NET_ERROR;
|
||||||
|
|
||||||
|
//now check if this is response to invoke (and extra validate if it's response to this(!) invoke)
|
||||||
|
if (head.m_flags&LEVIN_PACKET_RESPONSE)
|
||||||
|
{
|
||||||
|
//we got response, extra validate if its response to our request
|
||||||
|
CHECK_AND_ASSERT_MES(head.m_command == static_cast<uint32_t>(command), LEVIN_ERROR_PROTOCOL_INCONSISTENT, "command id missmatch in response: " << head.m_command << ", expected: " << command);
|
||||||
|
return head.m_return_code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//never comes here
|
||||||
|
return LEVIN_ERROR_INTERNAL;
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
template<typename transport_t>
|
||||||
|
int levin_client_impl2<transport_t>::notify(int command, const std::string& in_buff)
|
||||||
|
{
|
||||||
|
if (!is_connected())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
bucket_head2 head = { 0 };
|
||||||
|
head.m_signature = LEVIN_SIGNATURE;
|
||||||
|
head.m_cb = in_buff.size();
|
||||||
|
head.m_have_to_return_data = false;
|
||||||
|
head.m_command = command;
|
||||||
|
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
|
||||||
|
head.m_flags = LEVIN_PACKET_REQUEST;
|
||||||
|
|
||||||
|
if (!m_transport.send((const char*)&head, sizeof(head)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!m_transport.send(in_buff))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//never comes here
|
|
||||||
return LEVIN_ERROR_INTERNAL;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
inline
|
|
||||||
int levin_client_impl2::notify(int command, const std::string& in_buff)
|
|
||||||
{
|
|
||||||
if(!is_connected())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
bucket_head2 head = {0};
|
|
||||||
head.m_signature = LEVIN_SIGNATURE;
|
|
||||||
head.m_cb = in_buff.size();
|
|
||||||
head.m_have_to_return_data = false;
|
|
||||||
head.m_command = command;
|
|
||||||
head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
|
|
||||||
head.m_flags = LEVIN_PACKET_REQUEST;
|
|
||||||
|
|
||||||
if(!m_transport.send((const char*)&head, sizeof(head)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(!m_transport.send(in_buff))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
@ -4591,7 +4591,7 @@ void wallet2::send_transaction_to_network(const transaction& tx)
|
||||||
#define ENABLE_TOR_RELAY
|
#define ENABLE_TOR_RELAY
|
||||||
#ifdef ENABLE_TOR_RELAY
|
#ifdef ENABLE_TOR_RELAY
|
||||||
//TODO check that core synchronized
|
//TODO check that core synchronized
|
||||||
epee::levin::levin_client_impl p2p_client;
|
epee::net_utils::levin_client2 p2p_client;
|
||||||
if (!p2p_client.connect("127.0.0.1", P2P_DEFAULT_PORT, 100000))
|
if (!p2p_client.connect("127.0.0.1", P2P_DEFAULT_PORT, 100000))
|
||||||
{
|
{
|
||||||
THROW_IF_FALSE_WALLET_EX(false, error::no_connection_to_daemon, "Failed to connect to TOR node");
|
THROW_IF_FALSE_WALLET_EX(false, error::no_connection_to_daemon, "Failed to connect to TOR node");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue