1
- from ethereum import parse_genesis_declaration , db
1
+ from ethereum import genesis_helpers , db
2
2
from ethereum .block import Block , BlockHeader
3
3
from ethereum .config import Env
4
- import ethereum .state_transition as state_transition
5
- from ethereum import chain
4
+ import ethereum .messages as messages
5
+ import ethereum .common as common
6
+ from pyethapp .leveldb_service import LevelDB
7
+ from ethereum .pow import chain
6
8
import rlp
7
9
import json
8
10
import os
9
11
import sys
10
12
import time
13
+ import random
11
14
12
15
# from ethereum.slogging import LogRecorder, configure_logging, set_level
13
16
# config_string = ':info,eth.vm.log:trace,eth.vm.op:trace,eth.vm.stack:trace,eth.vm.exit:trace,eth.pb.msg:trace,eth.pb.tx:debug'
14
17
# configure_logging(config_string=config_string)
15
18
16
- state_transition .SKIP_MEDSTATES = True
17
- state_transition .SKIP_RECEIPT_ROOT_VALIDATION = True
19
+ messages .SKIP_MEDSTATES = True
20
+ common .SKIP_RECEIPT_ROOT_VALIDATION = True
18
21
# assert not state_transition.SKIP_MEDSTATES or state_transition.SKIP_RECEIPT_ROOT_VALIDATION
19
22
20
23
STATE_LOAD_FN = 'saved_state.json'
33
36
if '--benchmark' in sys .argv :
34
37
BENCHMARK = int (sys .argv [sys .argv .index ('--benchmark' ) + 1 ])
35
38
39
+ DB_DIR = '/tmp/%d' % random .randrange (int (time .time () * 1000000 ))
40
+ if '--db' in sys .argv :
41
+ DB_DIR = int (sys .argv [sys .argv .index ('--db' ) + 1 ])
42
+
36
43
_path , _file = os .path .split (STATE_LOAD_FN )
37
44
if _file in os .listdir (os .path .join (os .getcwd (), _path )):
38
- print 'loading state from %s ...' % STATE_LOAD_FN
45
+ print ( 'loading state from %s ...' % STATE_LOAD_FN )
39
46
c = chain .Chain (json .load (open (STATE_LOAD_FN )), Env ())
40
- print 'loaded.'
47
+ print ( 'loaded.' )
41
48
elif 'genesis_frontier.json' not in os .listdir (os .getcwd ()):
42
- print 'Please download genesis_frontier.json from ' + \
43
- 'http://vitalik.ca/files/genesis_frontier.json'
49
+ print ( 'Please download genesis_frontier.json from ' + \
50
+ 'http://vitalik.ca/files/genesis_frontier.json' )
44
51
sys .exit ()
45
52
else :
46
- c = chain .Chain (json .load (open ('genesis_frontier.json' )), Env ())
53
+ c = chain .Chain (json .load (open ('genesis_frontier.json' )), Env (LevelDB ( DB_DIR ) ))
47
54
assert c .state .trie .root_hash .encode ('hex' ) == \
48
55
'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544'
49
56
assert c .state .prev_headers [0 ].hash .encode ('hex' ) == \
50
57
'd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'
51
- print 'state generated from genesis'
52
- print 'Attempting to open %s' % RLP_BLOCKS_FILE
58
+ print ( 'state generated from genesis' )
59
+ print ( 'Attempting to open %s' % RLP_BLOCKS_FILE )
53
60
_path , _file = os .path .split (RLP_BLOCKS_FILE )
54
61
if not _path or _file not in os .listdir (_path ):
55
- print 'Please download 200kblocks.rlp from http://vitalik.ca/files/200kblocks.rlp ' + \
56
- 'and put it in this directory to continue the test'
62
+ print ( 'Please download 200kblocks.rlp from http://vitalik.ca/files/200kblocks.rlp ' + \
63
+ 'and put it in this directory to continue the test' )
57
64
sys .exit ()
58
65
59
- batch_size = 1024 * 10240 # approximately 10000 blocks
60
66
f = open (RLP_BLOCKS_FILE )
61
67
62
- # skip already processed blocks
63
- skip = c .state .block_number + 1
64
- print 'Skipping %d' % skip
65
- count = 0
66
- block_rlps = f .readlines (batch_size )
67
- while len (block_rlps ) > 0 :
68
- if len (block_rlps ) + count <= skip :
69
- count += len (block_rlps )
70
- block_rlps = f .readlines (batch_size )
71
- else :
72
- block_rlps = block_rlps [skip - count :]
73
- count = skip
74
- break
75
- print "skipped %d processed blocks" % skip
76
-
68
+ pos = 0
69
+ block_source_data = f .read ()
70
+ block_rlps = []
71
+ while pos < len (block_source_data ):
72
+ _ , l1 , l2 = rlp .codec .consume_length_prefix (block_source_data , pos )
73
+ block_rlps .append (block_source_data [pos : l1 + l2 ])
74
+ pos = l1 + l2
77
75
78
76
def report (st , num_blks , num_txs , gas_used ):
79
77
now = time .time ()
80
78
elapsed = now - st
81
79
tps = num_txs / elapsed
82
80
bps = num_blks / elapsed
83
81
gps = gas_used / elapsed
84
- print '%.2f >>> elapsed:%d blocks:%d txs:%d gas:%d bps:%d tps:%d gps:%d' % (now , elapsed , num_blks , num_txs , gas_used , bps , tps , gps )
82
+ print ( '%.2f >>> elapsed:%d blocks:%d txs:%d gas:%d bps:%d tps:%d gps:%d' % (now , elapsed , num_blks , num_txs , gas_used , bps , tps , gps ) )
85
83
86
84
87
85
def check_snapshot_consistency (snapshot , env = None ):
@@ -98,7 +96,7 @@ def check_snapshot_consistency(snapshot, env=None):
98
96
99
97
100
98
def snapshot (c , num_blocks ):
101
- print 'creating snapshot'
99
+ print ( 'creating snapshot' )
102
100
snapshot = c .state .to_snapshot ()
103
101
if (num_blocks / SAVE_INTERVAL ) % 2 == 1 :
104
102
check_snapshot_consistency (snapshot , env = None )
@@ -114,10 +112,11 @@ def snapshot(c, num_blocks):
114
112
open (fn , 'w' ).write (json .dumps (snapshot , indent = 4 ))
115
113
116
114
REPORT_INTERVAL = 1000
117
- SAVE_INTERVAL = 10 * 1000
118
- SNAPSHOT_INTERVAL = 100 * 1000
115
+ SAVE_INTERVAL = 999999
116
+ SNAPSHOT_INTERVAL = 999999
119
117
120
- MANUAL_SNAPSHOTS = [68000 , 68382 , 68666 , 69000 , 909330 ]
118
+ # MANUAL_SNAPSHOTS = [68000, 68382, 68666, 69000, 909330]
119
+ MANUAL_SNAPSHOTS = []
121
120
122
121
# don't check pow
123
122
BlockHeader .check_pow = lambda * args : True
@@ -127,33 +126,31 @@ def snapshot(c, num_blocks):
127
126
num_blks = 0
128
127
num_txs = 0
129
128
gas_used = 0
130
- while len (block_rlps ) > 0 :
131
- for block in block_rlps :
132
- # print 'prevh:', s.prev_headers
133
- block = rlp .decode (block .strip ().decode ('hex' ), Block )
134
- assert c .add_block (block )
135
- num_blks += 1
136
- num_txs += len (block .transactions )
137
- gas_used += block .gas_used
138
- if BENCHMARK > 0 :
139
- report (st , num_blks , num_txs , gas_used )
140
- if num_blks == BENCHMARK :
141
- print "Benchmark completed (%d blocks)." % num_blks
142
- sys .exit ()
143
- else :
144
- num_blocks = block .header .number + 1
145
- if num_blocks % REPORT_INTERVAL == 0 or num_blocks in MANUAL_SNAPSHOTS :
146
- report (st , REPORT_INTERVAL , num_txs , gas_used )
147
- st = time .time ()
148
- num_blks = 0
149
- num_txs = 0
150
- gas_used = 0
151
- if num_blocks % SAVE_INTERVAL == 0 or num_blocks in MANUAL_SNAPSHOTS :
152
- snapshot (c , num_blocks )
153
- st = time .time ()
154
- num_blks = 0
155
- num_txs = 0
156
- gas_used = 0
157
- block_rlps = f .readlines (batch_size )
158
-
159
- print 'Test successful'
129
+ for block in block_rlps [1 :50000 ]:
130
+ # print 'prevh:', s.prev_headers
131
+ block = rlp .decode (block , Block )
132
+ assert c .add_block (block )
133
+ num_blks += 1
134
+ num_txs += len (block .transactions )
135
+ gas_used += block .gas_used
136
+ if BENCHMARK > 0 :
137
+ report (st , num_blks , num_txs , gas_used )
138
+ if num_blks == BENCHMARK :
139
+ print ("Benchmark completed (%d blocks)." % num_blks )
140
+ sys .exit ()
141
+ else :
142
+ num_blocks = block .header .number + 1
143
+ if num_blocks % REPORT_INTERVAL == 0 or num_blocks in MANUAL_SNAPSHOTS :
144
+ report (st , REPORT_INTERVAL , num_txs , gas_used )
145
+ st = time .time ()
146
+ num_blks = 0
147
+ num_txs = 0
148
+ gas_used = 0
149
+ if num_blocks % SAVE_INTERVAL == 0 or num_blocks in MANUAL_SNAPSHOTS :
150
+ snapshot (c , num_blocks )
151
+ st = time .time ()
152
+ num_blks = 0
153
+ num_txs = 0
154
+ gas_used = 0
155
+
156
+ print ('Test successful' )
0 commit comments