3
3
from ethereum .blocks import Account , BlockHeader , Block , CachedBlock
4
4
from ethereum .utils import is_numeric , is_string , encode_hex , decode_hex , zpad , scan_bin , big_endian_to_int
5
5
from ethereum .securetrie import SecureTrie
6
- from ethereum .trie import Trie , BLANK_NODE , BLANK_ROOT
6
+ from ethereum .trie import BLANK_NODE , BLANK_ROOT
7
+ from ethereum .pruning_trie import Trie
7
8
8
9
9
10
class FakeHeader (object ):
@@ -68,18 +69,18 @@ def create_base_snapshot(base):
68
69
def create_state_snapshot (env , state_trie ):
69
70
alloc = dict ()
70
71
count = 0
71
- for addr , account_rlp in state_trie .to_dict (). items ():
72
+ for addr , account_rlp in state_trie .iter_branch ():
72
73
alloc [encode_hex (addr )] = create_account_snapshot (env , account_rlp )
73
74
count += 1
74
- print "[%d] created account snapshot %s" % encode_hex (addr )
75
+ print "[%d] created account snapshot %s" % ( count , encode_hex (addr ) )
75
76
return alloc
76
77
77
78
78
79
def create_account_snapshot (env , rlpdata ):
79
80
account = get_account (env , rlpdata )
80
81
storage_trie = SecureTrie (Trie (env .db , account .storage ))
81
82
storage = dict ()
82
- for k , v in storage_trie .to_dict (). items ():
83
+ for k , v in storage_trie .iter_branch ():
83
84
storage [encode_hex (k .lstrip ('\x00 ' ) or '\x00 ' )] = encode_hex (v )
84
85
return {
85
86
'nonce' : snapshot_form (account .nonce ),
@@ -132,6 +133,7 @@ def get_block(env, blockhash):
132
133
def validate_uncles ():
133
134
return True
134
135
136
+ print "Start loading recent blocks from snapshot"
135
137
first_block = rlp .decode (first_block_rlp , Block , env = chain .env )
136
138
chain .index .add_block (first_block )
137
139
chain ._store_block (first_block )
@@ -157,11 +159,15 @@ def validate_uncles():
157
159
def load_state (env , alloc ):
158
160
db = env .db
159
161
state = SecureTrie (Trie (db , BLANK_ROOT ))
160
- for addr , account in alloc .items ():
162
+ count = 0
163
+ print "Start loading state from snapshot"
164
+ for addr in alloc :
165
+ account = alloc [addr ]
161
166
acct = Account .blank_account (db , env .config ['ACCOUNT_INITIAL_NONCE' ])
162
167
if len (account ['storage' ]) > 0 :
163
168
t = SecureTrie (Trie (db , BLANK_ROOT ))
164
- for k , v in account ['storage' ].items ():
169
+ for k in account ['storage' ]:
170
+ v = account ['storage' ][k ]
165
171
enckey = zpad (decode_hex (k ), 32 )
166
172
t .update (enckey , decode_hex (v ))
167
173
acct .storage = t .root_hash
@@ -172,6 +178,10 @@ def load_state(env, alloc):
172
178
if account ['code' ]:
173
179
acct .code = decode_hex (account ['code' ])
174
180
state .update (decode_hex (addr ), rlp .encode (acct ))
181
+ count += 1
182
+ if count % 1000 == 0 :
183
+ db .commit ()
184
+ print "[%d] loaded account %s" % (count , addr )
175
185
db .commit ()
176
186
return state
177
187
0 commit comments