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

Commit 03cf231

Browse files
author
Jan Xie
committed
fix casper genesis generation, set correct genesis block hash in state
1 parent f409c37 commit 03cf231

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

ethereum/casper_utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ def make_withdrawal_signature(key):
166166
v, r, s = ecsign(h, key)
167167
return encode_int32(v) + encode_int32(r) + encode_int32(s)
168168

169-
def casper_contract_bootstrap(state, timestamp=0, epoch_length=100, number=0, gas_limit=4712388, nonce=0, ct=None):
170-
if not ct:
171-
ct = get_casper_ct()
169+
def casper_contract_bootstrap(state, timestamp=0, epoch_length=100, number=0, gas_limit=4712388, nonce=0):
170+
ct = get_casper_ct()
172171
# Set genesis time, and initialize epoch number
173172
t = Transaction(nonce, 0, 10**8, casper_config['CASPER_ADDR'], 0, ct.encode('initialize', [timestamp, epoch_length, number, gas_limit]))
174173
success = apply_transaction(state, t)
@@ -203,14 +202,16 @@ def casper_state_initialize(state):
203202
def make_casper_genesis(validators, alloc, timestamp=0, epoch_length=100):
204203
state = mk_basic_state(alloc, None, env=Env(config=casper_config))
205204
state.gas_limit = 10**8 * (len(validators) + 1)
206-
state.prev_headers[0].timestamp = timestamp
207-
state.prev_headers[0].difficulty = 1
208205
state.timestamp = timestamp
209206
state.block_difficulty = 1
210207

208+
header = state.prev_headers[0]
209+
header.timestamp = timestamp
210+
header.difficulty = 1
211+
211212
ct = get_casper_ct()
212213
initialize(state)
213-
casper_contract_bootstrap(state, ct=ct)
214+
casper_contract_bootstrap(state, timestamp=header.timestamp, gas_limit=header.gas_limit)
214215

215216
# Add validators
216217
for i, (vcode, deposit_size, randao_commitment, address) in enumerate(validators):
@@ -221,7 +222,11 @@ def make_casper_genesis(validators, alloc, timestamp=0, epoch_length=100):
221222

222223
assert call_casper(state, 'getEpoch', []) == 0
223224
assert call_casper(state, 'getTotalDeposits', []) == sum([d for a,d,r,a in validators])
225+
state.set_storage_data(utils.normalize_address(state.config['METROPOLIS_BLOCKHASH_STORE']),
226+
state.block_number % state.config['METROPOLIS_WRAPAROUND'],
227+
header.hash)
224228
state.commit()
229+
225230
return state
226231

227232

ethereum/chain.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class Chain(object):
2323

2424
def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
25-
new_head_cb=None, post_state_initialize=None, **kwargs):
25+
new_head_cb=None, **kwargs):
2626
self.env = env or Env()
2727
# Initialize the state
2828
if 'head_hash' in self.db:
@@ -56,8 +56,6 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
5656

5757
initialize(self.state)
5858
self.new_head_cb = new_head_cb
59-
if post_state_initialize:
60-
post_state_initialize(self.state)
6159

6260
self.head_hash = self.state.prev_headers[0].hash
6361
self.genesis = Block(self.state.prev_headers[0], [], [])

ethereum/parse_genesis_declaration.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def state_from_genesis_declaration(genesis_data, env, block=None):
4343
state.set_storage_data(addr, parse_as_bin(k), parse_as_bin(v))
4444
initialize(state, block)
4545
state.commit()
46-
block.header.state_root = state.trie.root_hash
46+
# genesis block's state_root should be blank node hash
47+
# Don't do this: block.header.state_root = state.trie.root_hash
4748
state.prev_headers=[block.header]
4849
return state
4950

@@ -90,16 +91,16 @@ def mk_basic_state(alloc, header, env):
9091
if not header:
9192
header = {
9293
"number": 0, "gas_limit": 4712388, "gas_used": 0,
93-
"timestamp": 1467446877, "difficulty": 2**25, "hash": '00' * 32,
94+
"timestamp": 1467446877, "difficulty": 2**25,
9495
"uncles_hash": '0x'+encode_hex(BLANK_UNCLES_HASH)
9596
}
96-
state.prev_headers = [FakeHeader(hash=parse_as_bin(header['hash']),
97-
number=parse_as_int(header['number']),
98-
timestamp=parse_as_int(header['timestamp']),
99-
difficulty=parse_as_int(header['difficulty']),
100-
gas_limit=parse_as_int(header['gas_limit']),
101-
uncles_hash=parse_as_bin(header['uncles_hash']))]
102-
97+
h = BlockHeader(number=parse_as_int(header['number']),
98+
timestamp=parse_as_int(header['timestamp']),
99+
difficulty=parse_as_int(header['difficulty']),
100+
gas_limit=parse_as_int(header['gas_limit']),
101+
uncles_hash=parse_as_bin(header['uncles_hash']))
102+
state.prev_headers = [h]
103+
103104
for addr, data in alloc.items():
104105
addr = normalize_address(addr)
105106
assert len(addr) == 20

0 commit comments

Comments
 (0)