Fabcoin Core  0.16.2
P2P Digital Currency
AdminEth.cpp
Go to the documentation of this file.
1 #include <jsonrpccpp/common/exception.h>
2 #include <libdevcore/CommonJS.h>
4 #include <libethcore/ICAP.h>
5 #include <libethereum/Client.h>
8 #include "AdminEth.h"
9 #include "SessionManager.h"
10 #include "JsonHelper.h"
11 using namespace std;
12 using namespace dev;
13 using namespace dev::rpc;
14 using namespace dev::eth;
15 
16 AdminEth::AdminEth(eth::Client& _eth, eth::TrivialGasPricer& _gp, eth::KeyManager& _keyManager, SessionManager& _sm):
17  m_eth(_eth),
18  m_gp(_gp),
19  m_keyManager(_keyManager),
20  m_sm(_sm)
21 {}
22 
23 bool AdminEth::admin_eth_setMining(bool _on, string const& _session)
24 {
25  RPC_ADMIN;
26  if (_on)
28  else
30  return true;
31 }
32 
34 {
35  RPC_ADMIN;
36  Json::Value ret;
38  ret["importing"] = (int)bqs.importing;
39  ret["verified"] = (int)bqs.verified;
40  ret["verifying"] = (int)bqs.verifying;
41  ret["unverified"] = (int)bqs.unverified;
42  ret["future"] = (int)bqs.future;
43  ret["unknown"] = (int)bqs.unknown;
44  ret["bad"] = (int)bqs.bad;
45  return ret;
46 }
47 
48 bool AdminEth::admin_eth_setAskPrice(string const& _wei, string const& _session)
49 {
50  RPC_ADMIN;
51  m_gp.setAsk(jsToU256(_wei));
52  return true;
53 }
54 
55 bool AdminEth::admin_eth_setBidPrice(string const& _wei, string const& _session)
56 {
57  RPC_ADMIN;
58  m_gp.setBid(jsToU256(_wei));
59  return true;
60 }
61 
62 Json::Value AdminEth::admin_eth_findBlock(string const& _blockHash, string const& _session)
63 {
64  RPC_ADMIN;
65  h256 h(_blockHash);
66  if (m_eth.blockChain().isKnown(h))
67  return toJson(m_eth.blockChain().info(h));
68  switch(m_eth.blockQueue().blockStatus(h))
69  {
70  case QueueStatus::Ready:
71  return "ready";
72  case QueueStatus::Importing:
73  return "importing";
74  case QueueStatus::UnknownParent:
75  return "unknown parent";
76  case QueueStatus::Bad:
77  return "bad";
78  default:
79  return "unknown";
80  }
81 }
82 
83 string AdminEth::admin_eth_blockQueueFirstUnknown(string const& _session)
84 {
85  RPC_ADMIN;
86  return m_eth.blockQueue().firstUnknown().hex();
87 }
88 
89 bool AdminEth::admin_eth_blockQueueRetryUnknown(string const& _session)
90 {
91  RPC_ADMIN;
93  return true;
94 }
95 
97 {
98  RPC_ADMIN;
99  Json::Value ret;
100  u256 total = 0;
101  u256 pendingtotal = 0;
102  Address beneficiary;
103  for (auto const& address: m_keyManager.accounts())
104  {
105  auto pending = m_eth.balanceAt(address, PendingBlock);
106  auto latest = m_eth.balanceAt(address, LatestBlock);
107  Json::Value a;
108  if (address == beneficiary)
109  a["beneficiary"] = true;
110  a["address"] = toJS(address);
111  a["balance"] = toJS(latest);
112  a["nicebalance"] = formatBalance(latest);
113  a["pending"] = toJS(pending);
114  a["nicepending"] = formatBalance(pending);
115  ret["accounts"][m_keyManager.accountName(address)] = a;
116  total += latest;
117  pendingtotal += pending;
118  }
119  ret["total"] = toJS(total);
120  ret["nicetotal"] = formatBalance(total);
121  ret["pendingtotal"] = toJS(pendingtotal);
122  ret["nicependingtotal"] = formatBalance(pendingtotal);
123  return ret;
124 }
125 
126 Json::Value AdminEth::admin_eth_newAccount(Json::Value const& _info, string const& _session)
127 {
128  RPC_ADMIN;
129  if (!_info.isMember("name"))
130  throw jsonrpc::JsonRpcException("No member found: name");
131  string name = _info["name"].asString();
132  auto s = ICAP::createDirect();
133  h128 uuid;
134  if (_info.isMember("password"))
135  {
136  string password = _info["password"].asString();
137  string hint = _info["passwordHint"].asString();
138  uuid = m_keyManager.import(s, name, password, hint);
139  }
140  else
141  uuid = m_keyManager.import(s, name);
142  Json::Value ret;
143  ret["account"] = toJS(toAddress(s));
144  ret["uuid"] = toUUID(uuid);
145  return ret;
146 }
147 
148 bool AdminEth::admin_eth_setMiningBenefactor(string const& _uuidOrAddress, string const& _session)
149 {
150  RPC_ADMIN;
151  return miner_setEtherbase(_uuidOrAddress);
152 }
153 
154 Json::Value AdminEth::admin_eth_inspect(string const& _address, string const& _session)
155 {
156  RPC_ADMIN;
157  if (!isHash<Address>(_address))
158  throw jsonrpc::JsonRpcException("Invalid address given.");
159 
160  Json::Value ret;
161  auto h = Address(fromHex(_address));
162  ret["storage"] = toJson(m_eth.storageAt(h, PendingBlock));
163  ret["balance"] = toJS(m_eth.balanceAt(h, PendingBlock));
164  ret["nonce"] = toJS(m_eth.countAt(h, PendingBlock));
165  ret["code"] = toJS(m_eth.codeAt(h, PendingBlock));
166  return ret;
167 }
168 
169 h256 AdminEth::blockHash(string const& _blockNumberOrHash) const
170 {
171  if (isHash<h256>(_blockNumberOrHash))
172  return h256(_blockNumberOrHash.substr(_blockNumberOrHash.size() - 64, 64));
173  try
174  {
175  return m_eth.blockChain().numberHash(stoul(_blockNumberOrHash));
176  }
177  catch (...)
178  {
179  throw jsonrpc::JsonRpcException("Invalid argument");
180  }
181 }
182 
183 Json::Value AdminEth::admin_eth_reprocess(string const& _blockNumberOrHash, string const& _session)
184 {
185  RPC_ADMIN;
186  Json::Value ret;
188  m_eth.block(blockHash(_blockNumberOrHash), &ps);
189  ret["enact"] = ps.enact;
190  ret["verify"] = ps.verify;
191  ret["total"] = ps.verify + ps.enact;
192  return ret;
193 }
194 
195 Json::Value AdminEth::admin_eth_vmTrace(string const& _blockNumberOrHash, int _txIndex, string const& _session)
196 {
197  RPC_ADMIN;
198 
199  Json::Value ret;
200 
201  if (_txIndex < 0)
202  throw jsonrpc::JsonRpcException("Negative index");
203  Block block = m_eth.block(blockHash(_blockNumberOrHash));
204  if ((unsigned)_txIndex < block.pending().size())
205  {
206  Transaction t = block.pending()[_txIndex];
207  State s(State::Null);
208  Executive e(s, block, _txIndex, m_eth.blockChain());
209  try
210  {
211  StandardTrace st;
212  st.setShowMnemonics();
213  e.initialize(t);
214  if (!e.execute())
215  e.go(st.onOp());
216  e.finalize();
217  Json::Reader().parse(st.json(), ret);
218  }
219  catch(Exception const& _e)
220  {
221  cwarn << diagnostic_information(_e);
222  }
223  }
224 
225  return ret;
226 }
227 
228 Json::Value AdminEth::admin_eth_getReceiptByHashAndIndex(string const& _blockNumberOrHash, int _txIndex, string const& _session)
229 {
230  RPC_ADMIN;
231  if (_txIndex < 0)
232  throw jsonrpc::JsonRpcException("Negative index");
233  auto h = blockHash(_blockNumberOrHash);
234  if (!m_eth.blockChain().isKnown(h))
235  throw jsonrpc::JsonRpcException("Invalid/unknown block.");
236  auto rs = m_eth.blockChain().receipts(h);
237  if ((unsigned)_txIndex >= rs.receipts.size())
238  throw jsonrpc::JsonRpcException("Index too large.");
239  return toJson(rs.receipts[_txIndex]);
240 }
241 
243 {
245  return true;
246 }
247 
249 {
250  m_eth.stopSealing();
251  return true;
252 }
253 
254 bool AdminEth::miner_setEtherbase(string const& _uuidOrAddress)
255 {
256  Address a;
257  h128 uuid = fromUUID(_uuidOrAddress);
258  if (uuid)
259  a = m_keyManager.address(uuid);
260  else if (isHash<Address>(_uuidOrAddress))
261  a = Address(_uuidOrAddress);
262  else
263  throw jsonrpc::JsonRpcException("Invalid UUID or address");
264 
267  else
268  m_eth.setAuthor(a);
269  return true;
270 }
271 
272 bool AdminEth::miner_setExtra(string const& _extraData)
273 {
274  m_eth.setExtraData(asBytes(_extraData));
275  return true;
276 }
277 
278 bool AdminEth::miner_setGasPrice(string const& _gasPrice)
279 {
280  m_gp.setAsk(jsToU256(_gasPrice));
281  return true;
282 }
283 
285 {
286  EthashClient const* client = nullptr;
287  try
288  {
289  client = asEthashClient(&m_eth);
290  }
291  catch (...)
292  {
293  throw jsonrpc::JsonRpcException("Hashrate not available - blockchain does not support mining.");
294  }
295  return toJS(client->hashrate());
296 }
h128 fromUUID(std::string const &_uuid)
Definition: FixedHash.cpp:31
void stopSealing() override
Stop sealing.
Definition: Client.h:161
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
virtual bool admin_eth_setMiningBenefactor(std::string const &_uuidOrAddress, std::string const &_session) override
Definition: AdminEth.cpp:148
virtual bool miner_setEtherbase(std::string const &_uuidOrAddress) override
Definition: AdminEth.cpp:254
u256 hashrate() const
The hashrate...
virtual Json::Value admin_eth_findBlock(std::string const &_blockHash, std::string const &_session) override
Definition: AdminEth.cpp:62
virtual bool admin_eth_setMining(bool _on, std::string const &_session) override
Definition: AdminEth.cpp:23
std::string const & accountName(Address const &_address) const
Definition: KeyManager.cpp:351
BlockHeader info(h256 const &_hash) const
Get the partial-header of a block (or the most recent mined if none given). Thread-safe.
Definition: BlockChain.h:144
eth::TrivialGasPricer & m_gp
Definition: AdminEth.h:53
dev::eth::Block block(h256 const &_blockHash, PopulationStatistics *o_stats) const
Get the block.
Definition: Client.cpp:760
virtual bool miner_start(int _threads) override
Definition: AdminEth.cpp:242
virtual Json::Value admin_eth_vmTrace(std::string const &_blockNumberOrHash, int _txIndex, std::string const &_session) override
Definition: AdminEth.cpp:195
EthashClient & asEthashClient(Interface &_c)
h256 blockHash(std::string const &_blockNumberOrHash) const
Definition: AdminEth.cpp:169
#define h(i)
Definition: sha.cpp:736
virtual Json::Value admin_eth_inspect(std::string const &_address, std::string const &_session) override
Definition: AdminEth.cpp:154
virtual std::string miner_hashrate() override
Definition: AdminEth.cpp:284
BlockReceipts receipts(h256 const &_hash) const
Get the transactions&#39; receipts of a block (or the most recent mined if none given).
Definition: BlockChain.h:165
virtual u256 countAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:168
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
virtual bytes codeAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:183
virtual bool admin_eth_blockQueueRetryUnknown(std::string const &_session) override
Definition: AdminEth.cpp:89
void setExtraData(bytes const &_extraData)
Set the extra data that goes into sealed blocks.
Definition: Client.h:193
virtual bool miner_setExtra(std::string const &_extraData) override
Definition: AdminEth.cpp:272
std::hash for asio::adress
Definition: Common.h:323
virtual bool miner_setGasPrice(std::string const &_gasPrice) override
Definition: AdminEth.cpp:278
eth::KeyManager & m_keyManager
Definition: AdminEth.h:54
QueueStatus blockStatus(h256 const &_h) const
Get some infomration on the given block&#39;s status regarding us.
Definition: BlockQueue.cpp:386
virtual u256 balanceAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:163
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
virtual std::map< h256, std::pair< u256, u256 > > storageAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:193
h128 import(Secret const &_s, std::string const &_accountName, std::string const &_pass, std::string const &_passwordHint)
#define a(i)
Active model of a block within the block chain.
Definition: Block.h:73
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
std::string json(bool _styled=false) const
Definition: Executive.cpp:150
Base class for all exceptions.
Definition: Exceptions.h:39
Config::Value_type Value
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
#define RPC_ADMIN
Definition: SessionManager.h:5
BlockChain const & blockChain() const
Get the object representing the current canonical blockchain.
Definition: Client.h:125
h256 numberHash(unsigned _i) const
Get the hash for a given block&#39;s number.
Definition: BlockChain.h:183
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
const char * name
Definition: rest.cpp:36
virtual Json::Value admin_eth_allAccounts(std::string const &_session) override
Definition: AdminEth.cpp:96
virtual Json::Value admin_eth_getReceiptByHashAndIndex(std::string const &_blockNumberOrHash, int _txIndex, std::string const &_session) override
Definition: AdminEth.cpp:228
Main API hub for interfacing with Ethereum.
Definition: Client.h:75
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
FixedHash< 32 > h256
Definition: FixedHash.h:340
#define cwarn
Definition: Log.h:304
virtual Json::Value admin_eth_blockQueueStatus(std::string const &_session) override
Definition: AdminEth.cpp:33
h256 firstUnknown() const
Return first block with an unknown parent.
Definition: BlockQueue.h:263
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:106
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bool isKnown(h256 const &_hash, bool _isCurrent=true) const
Returns true if the given block is known (though not necessarily a part of the canon chain)...
std::string formatBalance(bigint const &_b)
User-friendly string representation of the amount _b in wei.
Definition: Common.cpp:102
virtual bool admin_eth_setBidPrice(std::string const &_wei, std::string const &_session) override
Definition: AdminEth.cpp:55
Transactions const & pending() const
Get the list of pending transactions.
Definition: Block.h:188
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:87
void setBid(u256 const &_bid)
Definition: GasPricer.h:64
BlockQueueStatus status() const
Get some infomration on the current status.
Definition: BlockQueue.cpp:378
virtual Json::Value admin_eth_newAccount(const Json::Value &_info, std::string const &_session) override
Definition: AdminEth.cpp:126
virtual bool admin_eth_setAskPrice(std::string const &_wei, std::string const &_session) override
Definition: AdminEth.cpp:48
virtual bool miner_stop() override
Definition: AdminEth.cpp:248
Address address(h128 const &_uuid) const
Definition: KeyManager.cpp:205
Json::Value toJson(unordered_map< u256, u256 > const &_storage)
Definition: JsonHelper.cpp:41
eth::Client & m_eth
Definition: AdminEth.h:52
void retryUnknown()
Retries all blocks with unknown parents.
Definition: Client.h:189
#define e(i)
Definition: sha.cpp:733
void setAsk(u256 const &_ask)
Definition: GasPricer.h:63
virtual Json::Value admin_eth_reprocess(std::string const &_blockNumberOrHash, std::string const &_session) override
Definition: AdminEth.cpp:183
std::function< void(Address const &)> m_setMiningBenefactor
Definition: AdminEth.h:56
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
void startSealing() override
Start sealing.
Definition: Client.cpp:583
Addresses accounts() const
Definition: KeyManager.cpp:328
virtual std::string admin_eth_blockQueueFirstUnknown(std::string const &_session) override
Definition: AdminEth.cpp:83
BlockQueue const & blockQueue() const
Get the block queue.
Definition: Client.h:131
u256 jsToU256(std::string const &_s)
Definition: CommonJS.h:110
virtual void setAuthor(Address const &_us) override
Set the block author address.
Definition: Client.h:145
std::string toUUID(h128 const &_uuid)
Definition: FixedHash.cpp:43
std::string hex() const
Definition: FixedHash.h:130