1
1
import rlp
2
- from ethereum import blocks
3
- from ethereum .blocks import Account , BlockHeader , Block , CachedBlock
2
+ from ethereum .block import BlockHeader , Block
4
3
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
5
5
from ethereum .securetrie import SecureTrie
6
6
from ethereum .trie import BLANK_NODE , BLANK_ROOT
7
7
from ethereum .pruning_trie import Trie
@@ -43,34 +43,24 @@ def get_ancestor_list(self, n):
43
43
44
44
45
45
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
48
47
49
48
head_block = chain .head
50
49
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 ):
61
50
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 )
63
55
}
64
56
65
57
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 )
71
61
alloc = dict ()
72
62
count = 0
73
- for addr , account_rlp in state_trie .iter_branch ():
63
+ for addr , account_rlp in state . trie .iter_branch ():
74
64
alloc [encode_hex (addr )] = create_account_snapshot (env , account_rlp )
75
65
count += 1
76
66
print "[%d] created account snapshot %s" % (count , encode_hex (addr ))
@@ -91,13 +81,13 @@ def create_account_snapshot(env, rlpdata):
91
81
}
92
82
93
83
94
- def create_blocks_snapshot (base , head ):
84
+ def create_blocks_snapshot (chain , base , head ):
95
85
recent_blocks = list ()
96
86
block = head
97
87
while True :
98
88
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 )
101
91
else :
102
92
break
103
93
recent_blocks .reverse ()
@@ -117,19 +107,19 @@ def load_snapshot(chain, snapshot):
117
107
state = load_state (chain .env , snapshot ['alloc' ])
118
108
assert state .root_hash == base_header .state_root
119
109
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
133
123
134
124
def validate_uncles ():
135
125
return True
0 commit comments