Fabcoin Core  0.16.2
P2P Digital Currency
Account.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "Account.h"
25 #include <libethcore/Precompiled.h>
26 
27 using namespace std;
28 using namespace dev;
29 using namespace dev::eth;
30 
31 void Account::setNewCode(bytes&& _code)
32 {
33  m_codeCache = std::move(_code);
34  m_hasNewCode = true;
35  m_codeHash = sha3(m_codeCache);
36 }
37 
38 namespace js = json_spirit;
39 
40 uint64_t toUnsigned(js::mValue const& _v)
41 {
42  switch (_v.type())
43  {
44  case js::int_type: return _v.get_uint64();
45  case js::str_type: return fromBigEndian<uint64_t>(fromHex(_v.get_str()));
46  default: return 0;
47  }
48 }
49 
50 AccountMap dev::eth::jsonToAccountMap(std::string const& _json, u256 const& _defaultNonce, AccountMaskMap* o_mask, PrecompiledContractMap* o_precompiled)
51 {
52  auto u256Safe = [](std::string const& s) -> u256 {
53  bigint ret(s);
54  if (ret >= bigint(1) << 256)
55  BOOST_THROW_EXCEPTION(ValueTooLarge() << errinfo_comment("State value is equal or greater than 2**256") );
56  return (u256)ret;
57  };
58 
59  std::unordered_map<Address, Account> ret;
60 
61  js::mValue val;
62  json_spirit::read_string(_json, val);
63  js::mObject o = val.get_obj();
64  for (auto const& account: o.count("alloc") ? o["alloc"].get_obj() : o.count("accounts") ? o["accounts"].get_obj() : o)
65  {
66  Address a(fromHex(account.first));
67  auto o = account.second.get_obj();
68 
69  bool haveBalance = (o.count("wei") || o.count("finney") || o.count("balance"));
70  bool haveNonce = o.count("nonce");
71  bool haveCode = o.count("code");
72  bool haveStorage = o.count("storage");
73  bool shouldNotExists = o.count("shouldnotexist");
74 
75  if (haveStorage || haveCode || haveNonce || haveBalance)
76  {
77  u256 balance = 0;
78  if (o.count("wei"))
79  balance = u256Safe(o["wei"].get_str());
80  else if (o.count("finney"))
81  balance = u256Safe(o["finney"].get_str()) * finney;
82  else if (o.count("balance"))
83  balance = u256Safe(o["balance"].get_str());
84 
85  u256 nonce = haveNonce ? u256Safe(o["nonce"].get_str()) : _defaultNonce;
86 
87  if (haveCode)
88  {
89  ret[a] = Account(nonce, balance);
90  if (o["code"].type() == json_spirit::str_type)
91  {
92  if (o["code"].get_str().find("0x") != 0)
93  cerr << "Error importing code of account " << a << "! Code needs to be hex bytecode prefixed by \"0x\".";
94  else
95  ret[a].setNewCode(fromHex(o["code"].get_str().substr(2)));
96  }
97  else
98  cerr << "Error importing code of account " << a << "! Code field needs to be a string";
99  }
100  else
101  ret[a] = Account(nonce, balance);
102 
103  if (haveStorage)
104  for (pair<string, js::mValue> const& j: o["storage"].get_obj())
105  ret[a].setStorage(u256(j.first), u256(j.second.get_str()));
106  }
107 
108  if (o_mask)
109  {
110  (*o_mask)[a] = AccountMask(haveBalance, haveNonce, haveCode, haveStorage, shouldNotExists);
111  if (!haveStorage && !haveCode && !haveNonce && !haveBalance && shouldNotExists) //defined only shouldNotExists field
112  ret[a] = Account(0, 0);
113  }
114 
115  if (o_precompiled && o.count("precompiled"))
116  {
117  js::mObject p = o["precompiled"].get_obj();
118  auto n = p["name"].get_str();
119  if (!p.count("linear"))
120  {
121  cwarn << "No gas cost given for precompiled contract " << n;
122  throw;
123  }
124  try
125  {
126  auto l = p["linear"].get_obj();
127  u256 startingBlock = 0;
128  if (p.count("startingBlock"))
129  startingBlock = u256(p["startingBlock"].get_str());
130  unsigned base = toUnsigned(l["base"]);
131  unsigned word = toUnsigned(l["word"]);
132  o_precompiled->insert(make_pair(a, PrecompiledContract(base, word, PrecompiledRegistrar::executor(n), startingBlock)));
133  }
134  catch (ExecutorNotFound)
135  {
136  // Oh dear - missing a plugin?
137  cwarn << "Couldn't create a precompiled contract account. Missing an executor called:" << n;
138  throw;
139  }
140  }
141  }
142 
143  return ret;
144 }
const Object & get_obj() const
uint64_t toUnsigned(js::mValue const &_v)
Definition: Account.cpp:40
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::unordered_map< Address, PrecompiledContract > PrecompiledContractMap
Definition: Account.h:243
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:239
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
Models the state of a single Ethereum account.
Definition: Account.h:67
boost::uint64_t get_uint64() const
std::hash for asio::adress
Definition: Common.h:323
#define a(i)
Value_type type() const
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
bool read_string(const String_type &s, Value_type &value)
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
#define cwarn
Definition: Log.h:304
const String_type & get_str() const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::unordered_map< Address, AccountMask > AccountMaskMap
Definition: Account.h:240
mConfig::Object_type mObject
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
AccountMap jsonToAccountMap(std::string const &_json, u256 const &_defaultNonce=0, AccountMaskMap *o_mask=nullptr, PrecompiledContractMap *o_precompiled=nullptr)
Definition: Account.cpp:50
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:78
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
word32 word
Definition: config.h:308