aboutsummaryrefslogtreecommitdiffstats
path: root/test/IPCSocket.cpp
blob: eb4c34505ea36ae732625e120e2e3a5295c31faa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    cpp-ethereum 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file IPCSocket.cpp
 * @author Dimtiry Khokhlov <dimitry@ethdev.com>
 * @date 2016
 */

#include <string>
#include <stdio.h>
#include <thread>
#include "IPCSocket.h"
using namespace std;

IPCSocket::IPCSocket(string const& _path): m_address(_path)
{
    if (_path.length() > 108)
        BOOST_FAIL("Error opening IPC: socket path is too long!");

    struct sockaddr_un saun;
    saun.sun_family = AF_UNIX;
    strcpy(saun.sun_path, _path.c_str());

    if ((m_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
        BOOST_FAIL("Error creating IPC socket object");

    int len = sizeof(saun.sun_family) + strlen(saun.sun_path);

    if (connect(m_socket, reinterpret_cast<struct sockaddr const*>(&saun), len) < 0)
        BOOST_FAIL("Error connecting to IPC socket: " << _path);

    m_fp = fdopen(m_socket, "r");
}

string IPCSocket::sendRequest(string const& _req)
{
    send(m_socket, _req.c_str(), _req.length(), 0);

    char c;
    string response;
    while ((c = fgetc(m_fp)) != EOF)
    {
        if (c != '\n')
            response += c;
        else
            break;
    }
    return response;
}

string RPCRequest::eth_getCode(string const& _address, string const& _blockNumber)
{
    return getReply("result\":", rpcCall("eth_getCode", { makeString(_address), makeString(_blockNumber) }));
}

RPCRequest::transactionReceipt RPCRequest::eth_getTransactionReceipt(string const& _transactionHash)
{
    transactionReceipt receipt;
    string srpcCall = rpcCall("eth_getTransactionReceipt", { makeString(_transactionHash) });
    receipt.gasUsed = getReply("gasUsed\":" , srpcCall);
    receipt.contractAddress = getReply("contractAddress\":" , srpcCall);
    return receipt;
}

string RPCRequest::eth_sendTransaction(transactionData const& _td)
{
    string transaction = c_transaction;
    std::map<string, string> replaceMap;
    replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from;
    replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to :  _td.to;
    replaceMap["[GAS]"] = _td.gas;
    replaceMap["[GASPRICE]"] = _td.gasPrice;
    replaceMap["[VALUE]"] = _td.value;
    replaceMap["[DATA]"] = _td.data;
    parseString(transaction, replaceMap);
    return getReply("result\":", rpcCall("eth_sendTransaction", { transaction }));
}

string RPCRequest::eth_call(transactionData const& _td, string const& _blockNumber)
{
    string transaction = c_transaction;
    std::map<string, string> replaceMap;
    replaceMap["[FROM]"] = (_td.from.length() == 20) ? "0x" + _td.from : _td.from;
    replaceMap["[TO]"] = (_td.to.length() == 20 || _td.to == "") ? "0x" + _td.to : _td.to;
    replaceMap["[GAS]"] = _td.gas;
    replaceMap["[GASPRICE]"] = _td.gasPrice;
    replaceMap["[VALUE]"] = _td.value;
    replaceMap["[DATA]"] = _td.data;
    parseString(transaction, replaceMap);
    return getReply("result\":", rpcCall("eth_call", { transaction, makeString(_blockNumber) }));
}

string RPCRequest::eth_sendTransaction(string const& _transaction)
{
    return getReply("result\":", rpcCall("eth_sendTransaction", { _transaction }));
}

string RPCRequest::eth_getBalance(string const& _address, string const& _blockNumber)
{
    string address = (_address.length() == 20) ? "0x" + _address : _address;
    return getReply("result\":", rpcCall("eth_getBalance", { makeString(address), makeString(_blockNumber) }));
}

void RPCRequest::personal_unlockAccount(string const& _address, string const& _password, int _duration)
{
    rpcCall("personal_unlockAccount", { makeString(_address), makeString(_password), to_string(_duration) });
}

string RPCRequest::personal_newAccount(string const& _password)
{
    return getReply("result\":", rpcCall("personal_newAccount", { makeString(_password) }));
}

void RPCRequest::test_setChainParams(string const& _author, string const& _account, string const& _balance)
{
    if (_account.size() < 40)
        return;
    string config = c_genesisConfiguration;
    std::map<string, string> replaceMap;
    replaceMap["[AUTHOR]"] = _author;
    replaceMap["[ACCOUNT]"] = (_account[0] == '0' && _account[1] == 'x') ? _account.substr(2, 40) : _account;
    replaceMap["[BALANCE]"] = _balance;
    parseString(config, replaceMap);
    test_setChainParams(config);
}

void RPCRequest::test_setChainParams(string const& _config)
{
    rpcCall("test_setChainParams", { _config });
}

void RPCRequest::test_mineBlocks(int _number)
{
    rpcCall("test_mineBlocks", { to_string(_number) });
    std::this_thread::sleep_for(chrono::seconds(1));
}

string RPCRequest::rpcCall(string const& _methodName, vector<string> const& _args)
{
    string request = "{\"jsonrpc\":\"2.0\",\"method\":\"" + _methodName + "\",\"params\":[";
    for (size_t i = 0; i < _args.size(); ++i)
    {
        request += _args[i];
        if (i + 1 != _args.size())
            request += ", ";
    }

    request += "],\"id\":" + to_string(m_rpcSequence) + "}";
    ++m_rpcSequence;

    string reply = m_ipcSocket.sendRequest(request);
    //cout << "Request: " << request << endl;
    //cout << "Reply: " << reply << endl;
    return reply;
}

void RPCRequest::parseString(string& _string, map<string, string> const& _varMap)
{
    std::vector<string> types;
    for (std::map<std::string, std::string>::const_iterator it = _varMap.begin(); it != _varMap.end(); it++)
        types.push_back(it->first);

    for (unsigned i = 0; i < types.size(); i++)
    {
        std::size_t pos = _string.find(types.at(i));
        while (pos != std::string::npos)
        {
            _string.replace(pos, types.at(i).size(), _varMap.at(types.at(i)));
            pos = _string.find(types.at(i));
        }
    }
}

string RPCRequest::getReply(string const& _what, string const& _arg)
{
    string reply = "";
    size_t posStart = _arg.find(_what);
    size_t posEnd = _arg.find(",", posStart);
    if (posEnd == string::npos)
        posEnd = _arg.find("}", posStart);
    if (posStart != string::npos)
        reply = _arg.substr(posStart + _what.length(), posEnd - posStart - _what.length());
    reply.erase(std::remove(reply.begin(), reply.end(), '"'), reply.end());
    return reply;
}