Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 764372f

Browse files
author
Jan Xie
committed
fix create snapshot
1 parent 223fe19 commit 764372f

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

ethereum/chain.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ def get_score(self, block):
203203
if key not in self.db:
204204
try:
205205
parent_score = self.get_score(self.get_parent(block))
206-
self.db.put(key, str(parent_score + block.difficulty +
207-
random.randrange(block.difficulty // 10**6 + 1)))
206+
self.db.put(key, str(parent_score + block.difficulty))
208207
except:
209208
return int(self.db.get('score:' + block.prevhash))
210209
return int(self.db.get(key))

ethereum/snapshot.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import rlp
2-
from ethereum import blocks
3-
from ethereum.blocks import Account, BlockHeader, Block, CachedBlock
2+
from ethereum.block import BlockHeader, Block
43
from ethereum.utils import is_numeric, is_string, encode_hex, decode_hex, zpad, scan_bin, big_endian_to_int
4+
from ethereum.state import State, Account
55
from ethereum.securetrie import SecureTrie
66
from ethereum.trie import BLANK_NODE, BLANK_ROOT
77
from ethereum.pruning_trie import Trie
@@ -43,34 +43,24 @@ def get_ancestor_list(self, n):
4343

4444

4545
def create_snapshot(chain, recent=1024):
46-
env = chain.env
47-
assert recent > env.config['MAX_UNCLE_DEPTH']+2
46+
assert recent > chain.env.config['MAX_UNCLE_DEPTH']+2
4847

4948
head_block = chain.head
5049
base_block = chain.get_block_by_number(max(head_block.number-recent, 0))
51-
52-
snapshot = create_env_snapshot(base_block)
53-
snapshot['base'] = create_base_snapshot(base_block)
54-
snapshot['blocks'] = create_blocks_snapshot(base_block, head_block)
55-
snapshot['alloc'] = create_state_snapshot(env, base_block.state)
56-
57-
return snapshot
58-
59-
60-
def create_env_snapshot(base):
6150
return {
62-
'chainDifficulty': snapshot_form(base.get_score())
51+
'base': snapshot_form(rlp.encode(base_block.header)),
52+
'chainDifficulty': snapshot_form(chain.get_score(base_block)),
53+
'blocks': create_blocks_snapshot(chain, base_block, head_block),
54+
'alloc': create_state_snapshot(chain, base_block)
6355
}
6456

6557

66-
def create_base_snapshot(base):
67-
return snapshot_form(rlp.encode(base.header))
68-
69-
70-
def create_state_snapshot(env, state_trie):
58+
def create_state_snapshot(chain, block):
59+
env = chain.env
60+
state = State(block.state_root, env)
7161
alloc = dict()
7262
count = 0
73-
for addr, account_rlp in state_trie.iter_branch():
63+
for addr, account_rlp in state.trie.iter_branch():
7464
alloc[encode_hex(addr)] = create_account_snapshot(env, account_rlp)
7565
count += 1
7666
print "[%d] created account snapshot %s" % (count, encode_hex(addr))
@@ -91,13 +81,13 @@ def create_account_snapshot(env, rlpdata):
9181
}
9282

9383

94-
def create_blocks_snapshot(base, head):
84+
def create_blocks_snapshot(chain, base, head):
9585
recent_blocks = list()
9686
block = head
9787
while True:
9888
recent_blocks.append(snapshot_form(rlp.encode(block)))
99-
if block.prevhash != base.hash:
100-
block = block.get_parent()
89+
if block and block.prevhash != base.hash:
90+
block = chain.get_parent(block)
10191
else:
10292
break
10393
recent_blocks.reverse()
@@ -117,19 +107,19 @@ def load_snapshot(chain, snapshot):
117107
state = load_state(chain.env, snapshot['alloc'])
118108
assert state.root_hash == base_header.state_root
119109

120-
_get_block_header = blocks.get_block_header
121-
def get_block_header(db, blockhash):
122-
if blockhash == first_header_data[0]: # first block's prevhash
123-
return base_header
124-
return _get_block_header(db, blockhash)
125-
blocks.get_block_header = get_block_header
126-
127-
_get_block = blocks.get_block
128-
def get_block(env, blockhash):
129-
if blockhash == first_header_data[0]:
130-
return FakeBlock(env, get_block_header(env.db, blockhash), int(snapshot['chainDifficulty']))
131-
return _get_block(env, blockhash)
132-
blocks.get_block = get_block
110+
#_get_block_header = blocks.get_block_header
111+
#def get_block_header(db, blockhash):
112+
#if blockhash == first_header_data[0]: # first block's prevhash
113+
#return base_header
114+
#return _get_block_header(db, blockhash)
115+
#blocks.get_block_header = get_block_header
116+
117+
#_get_block = blocks.get_block
118+
#def get_block(env, blockhash):
119+
#if blockhash == first_header_data[0]:
120+
#return FakeBlock(env, get_block_header(env.db, blockhash), int(snapshot['chainDifficulty']))
121+
#return _get_block(env, blockhash)
122+
#blocks.get_block = get_block
133123

134124
def validate_uncles():
135125
return True

0 commit comments

Comments
 (0)