forked from FISCO-BCOS/FISCO-BCOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageState.cpp
442 lines (405 loc) · 12.5 KB
/
StorageState.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*7
* @CopyRight:
* FISCO-BCOS 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.
*
* FISCO-BCOS 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 FISCO-BCOS. If not, see <http://www.gnu.org/licenses/>
* (c) 2016-2018 fisco-dev contributors.
*/
/**
* @Mpt state interface for EVM
*
* @file StorageState.cpp
* @author xingqiangbai
* @date 2018-10-22
*/
#include "StorageState.h"
#include "libdevcrypto/Hash.h"
#include "libethcore/Exceptions.h"
#include "libstorage/Table.h"
using namespace dev;
using namespace dev::eth;
using namespace dev::storagestate;
using namespace dev::storage;
using namespace dev::executive;
bool StorageState::addressInUse(Address const& _address) const
{
auto table = getTable(_address);
// if (table && !table->empty())
if (table)
{
return true;
}
return false;
}
bool StorageState::accountNonemptyAndExisting(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
if (balance(_address) > u256(0) || codeHash(_address) != EmptySHA3 ||
getNonce(_address) != m_accountStartNonce)
return true;
}
return false;
}
bool StorageState::addressHasCode(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_CODE_HASH, table->newCondition());
if (entries->size() != 0u)
{
auto codeHash = h256(fromHex(entries->get(0)->getField(STORAGE_VALUE)));
return codeHash != EmptySHA3;
}
}
return false;
}
u256 StorageState::balance(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_BALANCE, table->newCondition());
if (entries->size() != 0u)
{
return u256(entries->get(0)->getField(STORAGE_VALUE));
}
}
return 0;
}
void StorageState::addBalance(Address const& _address, u256 const& _amount)
{
if (_amount == 0)
{
return;
}
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_BALANCE, table->newCondition());
if (entries->size() != 0u)
{
auto entry = entries->get(0);
auto balance = u256(entry->getField(STORAGE_VALUE));
balance += _amount;
Entry::Ptr updateEntry = table->newEntry();
updateEntry->setField(STORAGE_VALUE, balance.str());
table->update(ACCOUNT_BALANCE, updateEntry, table->newCondition());
}
}
else
{
createAccount(_address, requireAccountStartNonce(), _amount);
}
}
void StorageState::subBalance(Address const& _address, u256 const& _amount)
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_BALANCE, table->newCondition());
if (entries->size() != 0u)
{
auto entry = entries->get(0);
auto balance = u256(entry->getField(STORAGE_VALUE));
if (balance < _amount)
BOOST_THROW_EXCEPTION(NotEnoughCash());
balance -= _amount;
Entry::Ptr updateEntry = table->newEntry();
updateEntry->setField(STORAGE_VALUE, balance.str());
table->update(ACCOUNT_BALANCE, updateEntry, table->newCondition());
}
}
else
{
BOOST_THROW_EXCEPTION(NotEnoughCash());
}
}
void StorageState::setBalance(Address const& _address, u256 const& _amount)
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_BALANCE, table->newCondition());
if (entries->size() != 0u)
{
auto entry = entries->get(0);
auto balance = u256(entry->getField(STORAGE_VALUE));
balance = _amount;
Entry::Ptr updateEntry = table->newEntry();
updateEntry->setField(STORAGE_VALUE, balance.str());
table->update(ACCOUNT_BALANCE, updateEntry, table->newCondition());
}
}
else
{
createAccount(_address, requireAccountStartNonce(), _amount);
}
}
void StorageState::transferBalance(Address const& _from, Address const& _to, u256 const& _value)
{
subBalance(_from, _value);
addBalance(_to, _value);
}
h256 StorageState::storageRoot(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
return table->hash();
}
return h256();
}
u256 StorageState::storage(Address const& _address, u256 const& _key)
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(_key.str(), table->newCondition());
if (entries->size() != 0u)
{
return u256(entries->get(0)->getField(STORAGE_VALUE));
}
}
return u256(0);
}
void StorageState::setStorage(Address const& _address, u256 const& _location, u256 const& _value)
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(_location.str(), table->newCondition());
if (entries->size() == 0u)
{
auto entry = table->newEntry();
entry->setField(STORAGE_KEY, _location.str());
entry->setField(STORAGE_VALUE, _value.str());
table->insert(_location.str(), entry);
}
else
{
auto entry = table->newEntry();
entry->setField(STORAGE_KEY, _location.str());
entry->setField(STORAGE_VALUE, _value.str());
table->update(_location.str(), entry, table->newCondition());
}
}
}
void StorageState::clearStorage(Address const&) {}
void StorageState::setCode(Address const& _address, bytes&& _code)
{
auto table = getTable(_address);
if (table)
{
auto entry = table->newEntry();
entry->setField(STORAGE_VALUE, toHex(_code));
table->update(ACCOUNT_CODE, entry, table->newCondition());
entry = table->newEntry();
entry->setField(STORAGE_VALUE, toHex(sha3(_code)));
table->update(ACCOUNT_CODE_HASH, entry, table->newCondition());
}
}
void StorageState::kill(Address _address)
{
auto table = getTable(_address);
if (table)
{
auto entry = table->newEntry();
entry->setField(STORAGE_VALUE, m_accountStartNonce.str());
table->update(ACCOUNT_NONCE, entry, table->newCondition());
entry = table->newEntry();
entry->setField(STORAGE_VALUE, u256(0).str());
table->update(ACCOUNT_BALANCE, entry, table->newCondition());
entry = table->newEntry();
entry->setField(STORAGE_VALUE, "");
table->update(ACCOUNT_CODE, entry, table->newCondition());
entry = table->newEntry();
entry->setField(STORAGE_VALUE, toHex(EmptySHA3));
table->update(ACCOUNT_CODE_HASH, entry, table->newCondition());
entry = table->newEntry();
entry->setField(STORAGE_VALUE, "false");
table->update(ACCOUNT_ALIVE, entry, table->newCondition());
}
clear();
}
bytes const StorageState::code(Address const& _address) const
{
if (codeHash(_address) == EmptySHA3)
return NullBytes;
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_CODE, table->newCondition());
if (entries->size() != 0u)
{
return fromHex(entries->get(0)->getField(STORAGE_VALUE));
}
}
return NullBytes;
}
h256 StorageState::codeHash(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_CODE_HASH, table->newCondition());
if (entries->size() != 0u)
{
return h256(fromHex(entries->get(0)->getField(STORAGE_VALUE)));
}
}
return EmptySHA3;
}
size_t StorageState::codeSize(Address const& _address) const
{
return code(_address).size();
}
void StorageState::createContract(Address const& _address)
{
createAccount(_address, requireAccountStartNonce());
}
void StorageState::incNonce(Address const& _address)
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_NONCE, table->newCondition());
if (entries->size() != 0u)
{
auto entry = entries->get(0);
auto nonce = u256(entry->getField(STORAGE_VALUE));
++nonce;
Entry::Ptr updateEntry = table->newEntry();
updateEntry->setField(STORAGE_VALUE, nonce.str());
table->update(ACCOUNT_NONCE, updateEntry, table->newCondition());
}
}
else
createAccount(_address, requireAccountStartNonce() + 1);
}
void StorageState::setNonce(Address const& _address, u256 const& _newNonce)
{
auto table = getTable(_address);
if (table)
{
auto entry = table->newEntry();
entry->setField(STORAGE_VALUE, _newNonce.str());
table->update(ACCOUNT_NONCE, entry, table->newCondition());
}
else
createAccount(_address, _newNonce);
}
u256 StorageState::getNonce(Address const& _address) const
{
auto table = getTable(_address);
if (table)
{
auto entries = table->select(ACCOUNT_NONCE, table->newCondition());
if (entries->size() != 0u)
{
auto entry = entries->get(0);
return u256(entry->getField(STORAGE_VALUE));
}
}
return m_accountStartNonce;
}
h256 StorageState::rootHash(bool needCalculate) const
{
if (needCalculate)
{
return m_memoryTableFactory->hash();
}
return h256();
}
void StorageState::commit()
{
m_memoryTableFactory->commit();
}
void StorageState::dbCommit(h256 const&, int64_t)
{
// ExecutiveContext will commit
// m_memoryTableFactory->commitDB(_blockHash, _blockNumber);
}
void StorageState::setRoot(h256 const&) {}
u256 const& StorageState::accountStartNonce() const
{
return m_accountStartNonce;
}
u256 const& StorageState::requireAccountStartNonce() const
{
if (m_accountStartNonce == Invalid256)
BOOST_THROW_EXCEPTION(InvalidAccountStartNonceInState());
return m_accountStartNonce;
}
void StorageState::noteAccountStartNonce(u256 const& _actual)
{
if (m_accountStartNonce == Invalid256)
m_accountStartNonce = _actual;
else if (m_accountStartNonce != _actual)
BOOST_THROW_EXCEPTION(IncorrectAccountStartNonceInState());
}
size_t StorageState::savepoint() const
{
return m_memoryTableFactory->savepoint();
}
void StorageState::rollback(size_t _savepoint)
{
m_memoryTableFactory->rollback(_savepoint);
}
void StorageState::clear()
{
// m_cache.clear();
}
bool StorageState::checkAuthority(Address const& _origin, Address const& _contract) const
{
auto table = getTable(_contract);
if (table)
return table->checkAuthority(_origin);
else
return true;
}
void StorageState::createAccount(Address const& _address, u256 const& _nonce, u256 const& _amount)
{
std::string tableName("_contract_data_" + _address.hex() + "_");
auto table = m_memoryTableFactory->createTable(tableName, STORAGE_KEY, STORAGE_VALUE, false);
if (!table)
{
return;
}
auto entry = table->newEntry();
entry->setField(STORAGE_KEY, ACCOUNT_BALANCE);
entry->setField(STORAGE_VALUE, _amount.str());
table->insert(ACCOUNT_BALANCE, entry);
entry = table->newEntry();
entry->setField(STORAGE_KEY, ACCOUNT_CODE_HASH);
entry->setField(STORAGE_VALUE, toHex(EmptySHA3));
table->insert(ACCOUNT_CODE_HASH, entry);
entry = table->newEntry();
entry->setField(STORAGE_KEY, ACCOUNT_CODE);
entry->setField(STORAGE_VALUE, "");
table->insert(ACCOUNT_CODE, entry);
entry = table->newEntry();
entry->setField(STORAGE_KEY, ACCOUNT_NONCE);
entry->setField(STORAGE_VALUE, _nonce.str());
table->insert(ACCOUNT_NONCE, entry);
entry = table->newEntry();
entry->setField(STORAGE_KEY, ACCOUNT_ALIVE);
entry->setField(STORAGE_VALUE, "true");
table->insert(ACCOUNT_ALIVE, entry);
}
inline storage::Table::Ptr StorageState::getTable(Address const& _address) const
{
std::string tableName("_contract_data_" + _address.hex() + "_");
return m_memoryTableFactory->openTable(tableName);
}