-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount.py
71 lines (58 loc) · 2.05 KB
/
account.py
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
import rlp
from rlp.sedes import big_endian_int, binary
from securetrie import SecureTrie
from trie import Trie
from db import RefcountDB, BaseDB
from balance import Balance
from netaddr import IPSet
from utils import object_to_bin
import utils
BLANK_HASH = utils.sha3(b'')
BLANK_ROOT = utils.sha3rlp(b'')
class Account(rlp.Serializable):
fields = [
('nonce', big_endian_int),
('balance', binary)
]
def __init__(self, nonce, balance, env, address):
assert isinstance(env.db, BaseDB)
self.env = env
self.address = address
super(Account, self).__init__(nonce, balance)
self.storage_cache = {}
self.storage_trie = SecureTrie(Trie(RefcountDB(self.env.db)))
self.touched = False
self.existent_at_start = True
self._mutable = True
self.deleted = False
def commit(self):
for k, v in self.storage_cache.items():
if v:
self.storage_trie.update(utils.encode_int32(k), rlp.encode(v))
else:
self.storage_trie.delete(utils.encode_int32(k))
self.storage_cache = {}
self.storage = self.storage_trie.root_hash
@property
def code(self):
return self.env.db.get(self.code_hash)
@classmethod
def blank_account(cls, env, address, initial_nonce=0):
env.db.put(BLANK_HASH, b'')
balance = Balance(IPSet())
o = cls(initial_nonce, object_to_bin(balance), env, address)
o.existent_at_start = False
return o
def is_blank(self):
return self.nonce == 0 and self.balance == 0 and self.code_hash == BLANK_HASH
@property
def exists(self):
if self.is_blank():
return self.touched or (
self.existent_at_start and not self.deleted)
return True
def to_dict(self):
odict = self.storage_trie.to_dict()
for k, v in self.storage_cache.items():
odict[utils.encode_int(k)] = rlp.encode(utils.encode_int(v))
return {'balance': str(self.balance), 'nonce': str(self.nonce)}