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

Commit

Permalink
fix casper genesis generation, set correct genesis block hash in state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Xie committed Sep 6, 2016
1 parent f409c37 commit 03cf231
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
17 changes: 11 additions & 6 deletions ethereum/casper_utils.py
Expand Up @@ -166,9 +166,8 @@ def make_withdrawal_signature(key):
v, r, s = ecsign(h, key)
return encode_int32(v) + encode_int32(r) + encode_int32(s)

def casper_contract_bootstrap(state, timestamp=0, epoch_length=100, number=0, gas_limit=4712388, nonce=0, ct=None):
if not ct:
ct = get_casper_ct()
def casper_contract_bootstrap(state, timestamp=0, epoch_length=100, number=0, gas_limit=4712388, nonce=0):
ct = get_casper_ct()
# Set genesis time, and initialize epoch number
t = Transaction(nonce, 0, 10**8, casper_config['CASPER_ADDR'], 0, ct.encode('initialize', [timestamp, epoch_length, number, gas_limit]))
success = apply_transaction(state, t)
Expand Down Expand Up @@ -203,14 +202,16 @@ def casper_state_initialize(state):
def make_casper_genesis(validators, alloc, timestamp=0, epoch_length=100):
state = mk_basic_state(alloc, None, env=Env(config=casper_config))
state.gas_limit = 10**8 * (len(validators) + 1)
state.prev_headers[0].timestamp = timestamp
state.prev_headers[0].difficulty = 1
state.timestamp = timestamp
state.block_difficulty = 1

header = state.prev_headers[0]
header.timestamp = timestamp
header.difficulty = 1

ct = get_casper_ct()
initialize(state)
casper_contract_bootstrap(state, ct=ct)
casper_contract_bootstrap(state, timestamp=header.timestamp, gas_limit=header.gas_limit)

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

assert call_casper(state, 'getEpoch', []) == 0
assert call_casper(state, 'getTotalDeposits', []) == sum([d for a,d,r,a in validators])
state.set_storage_data(utils.normalize_address(state.config['METROPOLIS_BLOCKHASH_STORE']),
state.block_number % state.config['METROPOLIS_WRAPAROUND'],
header.hash)
state.commit()

return state


Expand Down
4 changes: 1 addition & 3 deletions ethereum/chain.py
Expand Up @@ -22,7 +22,7 @@
class Chain(object):

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

initialize(self.state)
self.new_head_cb = new_head_cb
if post_state_initialize:
post_state_initialize(self.state)

self.head_hash = self.state.prev_headers[0].hash
self.genesis = Block(self.state.prev_headers[0], [], [])
Expand Down
19 changes: 10 additions & 9 deletions ethereum/parse_genesis_declaration.py
Expand Up @@ -43,7 +43,8 @@ def state_from_genesis_declaration(genesis_data, env, block=None):
state.set_storage_data(addr, parse_as_bin(k), parse_as_bin(v))
initialize(state, block)
state.commit()
block.header.state_root = state.trie.root_hash
# genesis block's state_root should be blank node hash
# Don't do this: block.header.state_root = state.trie.root_hash
state.prev_headers=[block.header]
return state

Expand Down Expand Up @@ -90,16 +91,16 @@ def mk_basic_state(alloc, header, env):
if not header:
header = {
"number": 0, "gas_limit": 4712388, "gas_used": 0,
"timestamp": 1467446877, "difficulty": 2**25, "hash": '00' * 32,
"timestamp": 1467446877, "difficulty": 2**25,
"uncles_hash": '0x'+encode_hex(BLANK_UNCLES_HASH)
}
state.prev_headers = [FakeHeader(hash=parse_as_bin(header['hash']),
number=parse_as_int(header['number']),
timestamp=parse_as_int(header['timestamp']),
difficulty=parse_as_int(header['difficulty']),
gas_limit=parse_as_int(header['gas_limit']),
uncles_hash=parse_as_bin(header['uncles_hash']))]
h = BlockHeader(number=parse_as_int(header['number']),
timestamp=parse_as_int(header['timestamp']),
difficulty=parse_as_int(header['difficulty']),
gas_limit=parse_as_int(header['gas_limit']),
uncles_hash=parse_as_bin(header['uncles_hash']))
state.prev_headers = [h]

for addr, data in alloc.items():
addr = normalize_address(addr)
assert len(addr) == 20
Expand Down

0 comments on commit 03cf231

Please sign in to comment.