Ethereum  PoC-8
The C++ Implementation of Ethereum
ValidationSchemes.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 */
17 #include "ValidationSchemes.h"
18 #include <libdevcore/JsonUtils.h>
19 #include <string>
20 
21 using namespace std;
22 namespace js = json_spirit;
23 
24 namespace dev
25 {
26 namespace eth
27 {
28 namespace validation
29 {
30 string const c_sealEngine = "sealEngine";
31 string const c_params = "params";
32 string const c_genesis = "genesis";
33 string const c_accounts = "accounts";
34 string const c_balance = "balance";
35 string const c_wei = "wei";
36 string const c_finney = "finney";
37 string const c_author = "author";
38 string const c_coinbase = "coinbase";
39 string const c_nonce = "nonce";
40 string const c_gasLimit = "gasLimit";
41 string const c_timestamp = "timestamp";
42 string const c_difficulty = "difficulty";
43 string const c_extraData = "extraData";
44 string const c_mixHash = "mixHash";
45 string const c_parentHash = "parentHash";
46 string const c_precompiled = "precompiled";
47 string const c_code = "code";
48 string const c_storage = "storage";
49 string const c_gasUsed = "gasUsed";
50 string const c_codeFromFile = "codeFromFile";
51 string const c_shouldnotexist = "shouldnotexist";
52 
53 string const c_minGasLimit = "minGasLimit";
54 string const c_maxGasLimit = "maxGasLimit";
55 string const c_gasLimitBoundDivisor = "gasLimitBoundDivisor";
56 string const c_homesteadForkBlock = "homesteadForkBlock";
57 string const c_daoHardforkBlock = "daoHardforkBlock";
58 string const c_EIP150ForkBlock = "EIP150ForkBlock";
59 string const c_EIP158ForkBlock = "EIP158ForkBlock";
60 string const c_byzantiumForkBlock = "byzantiumForkBlock";
61 string const c_eWASMForkBlock = "eWASMForkBlock";
62 string const c_constantinopleForkBlock = "constantinopleForkBlock";
63 string const c_experimentalForkBlock = "experimentalForkBlock";
64 string const c_accountStartNonce = "accountStartNonce";
65 string const c_maximumExtraDataSize = "maximumExtraDataSize";
66 string const c_tieBreakingGas = "tieBreakingGas";
67 string const c_blockReward = "blockReward";
68 string const c_difficultyBoundDivisor = "difficultyBoundDivisor";
69 string const c_minimumDifficulty = "minimumDifficulty";
70 string const c_durationLimit = "durationLimit";
71 string const c_chainID = "chainID";
72 string const c_networkID = "networkID";
73 string const c_allowFutureBlocks = "allowFutureBlocks";
74 
75 void validateConfigJson(js::mObject const& _obj)
76 {
77  requireJsonFields(_obj, "ChainParams::loadConfig",
78  {{c_sealEngine, {{js::str_type}, JsonFieldPresence::Required}},
79  {c_params, {{js::obj_type}, JsonFieldPresence::Required}},
80  {c_genesis, {{js::obj_type}, JsonFieldPresence::Required}},
81  {c_accounts, {{js::obj_type}, JsonFieldPresence::Required}}});
82 
83  requireJsonFields(_obj.at(c_genesis).get_obj(), "ChainParams::loadConfig::genesis",
84  {{c_author, {{js::str_type}, JsonFieldPresence::Required}},
85  {c_nonce, {{js::str_type}, JsonFieldPresence::Required}},
86  {c_gasLimit, {{js::str_type}, JsonFieldPresence::Required}},
87  {c_timestamp, {{js::str_type}, JsonFieldPresence::Required}},
88  {c_difficulty, {{js::str_type}, JsonFieldPresence::Required}},
89  {c_extraData, {{js::str_type}, JsonFieldPresence::Required}},
90  {c_mixHash, {{js::str_type}, JsonFieldPresence::Required}},
91  {c_parentHash, {{js::str_type}, JsonFieldPresence::Optional}}});
92 
93  js::mObject const& accounts = _obj.at(c_accounts).get_obj();
94  for (auto const& acc : accounts)
95  validateAccountObj(acc.second.get_obj());
96 }
97 
98 void validateAccountMaskObj(js::mObject const& _obj)
99 {
100  // A map object with possibly defined fields
101  requireJsonFields(_obj, "validateAccountMaskObj",
102  {{c_storage, {{js::obj_type}, JsonFieldPresence::Optional}},
103  {c_balance, {{js::str_type}, JsonFieldPresence::Optional}},
104  {c_nonce, {{js::str_type}, JsonFieldPresence::Optional}},
105  {c_code, {{js::str_type}, JsonFieldPresence::Optional}},
106  {c_precompiled, {{js::obj_type}, JsonFieldPresence::Optional}},
107  {c_shouldnotexist, {{js::str_type}, JsonFieldPresence::Optional}},
108  {c_wei, {{js::str_type}, JsonFieldPresence::Optional}}});
109 }
110 
111 void validateAccountObj(js::mObject const& _obj)
112 {
113  if (_obj.count(c_precompiled))
114  {
115  // A precompiled contract
116  requireJsonFields(_obj, "validateAccountObj",
117  {{c_precompiled, {{js::obj_type}, JsonFieldPresence::Required}},
118  {c_wei, {{js::str_type}, JsonFieldPresence::Optional}},
119  {c_balance, {{js::str_type}, JsonFieldPresence::Optional}}});
120  }
121  else
122  {
123  // Extendable account description. Check typo errors and fields type.
124  requireJsonFields(_obj, "validateAccountObj",
125  {{c_code, {{js::str_type}, JsonFieldPresence::Optional}},
126  {c_nonce, {{js::str_type}, JsonFieldPresence::Optional}},
127  {c_storage, {{js::obj_type}, JsonFieldPresence::Optional}},
128  {c_balance, {{js::str_type}, JsonFieldPresence::Optional}},
129  {c_wei, {{js::str_type}, JsonFieldPresence::Optional}},
130  {c_codeFromFile, {{js::str_type}, JsonFieldPresence::Optional}}});
131 
132  // At least one field must be set
133  if (_obj.empty())
134  {
135  string comment =
136  "Error in validateAccountObj: At least one field must be set (code, nonce, "
137  "storage, balance, wei, codeFromFile)!";
138  BOOST_THROW_EXCEPTION(MissingField() << errinfo_comment(comment));
139  }
140 
141  // c_code, c_codeFromFile could not coexist
142  if (_obj.count(c_code) && _obj.count(c_codeFromFile))
143  {
144  string comment =
145  "Error in validateAccountObj: field 'code' contradicts field 'codeFromFile'!";
146  BOOST_THROW_EXCEPTION(UnknownField() << errinfo_comment(comment));
147  }
148  }
149 
150  // c_wei, c_balance could not coexist
151  if (_obj.count(c_wei) && _obj.count(c_balance))
152  {
153  string comment = "Error in validateAccountObj: field 'balance' contradicts field 'wei'!";
154  BOOST_THROW_EXCEPTION(UnknownField() << errinfo_comment(comment));
155  }
156 }
157 }
158 }
159 }
Definition: Address.cpp:20
string const c_accountStartNonce
string const c_byzantiumForkBlock
string const c_codeFromFile
A file containg a code as bytes.
string const c_daoHardforkBlock
string const c_gasLimitBoundDivisor
string const c_difficultyBoundDivisor
Definition: FixedHash.h:390
string const c_maximumExtraDataSize
string const c_EIP150ForkBlock
string const c_experimentalForkBlock
string const c_minimumDifficulty
string const c_EIP158ForkBlock
string const c_homesteadForkBlock
void validateConfigJson(js::mObject const &_obj)
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69
void validateAccountObj(js::mObject const &_obj)
void requireJsonFields(json_spirit::mObject const &_o, std::string const &_configName, std::map< std::string, JsonFieldOptions > const &_validationMap)
Definition: JsonUtils.cpp:59
string const c_allowFutureBlocks
string const c_constantinopleForkBlock
void validateAccountMaskObj(js::mObject const &_obj)