Skip to content

Commit

Permalink
Added get_indices_for_slot and get_block_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Aug 22, 2018
1 parent 67d22e7 commit 859a0db
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
23 changes: 23 additions & 0 deletions beacon_chain/state/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,26 @@ def get_new_shuffling(seed,
committee=indices
) for j, indices in enumerate(shard_indices)])
return o


def get_indices_for_slot(
crystallized_state,
slot,
config=DEFAULT_CONFIG):
cycle_length = config['cycle_length']

ifh_start = crystallized_state.last_state_recalc - cycle_length
assert ifh_start <= slot < ifh_start + cycle_length * 2
return crystallized_state.indices_for_slots[slot - ifh_start]


def get_block_hash(
active_state,
current_block,
slot,
config=DEFAULT_CONFIG):
cycle_length = config['cycle_length']

sback = current_block.slot_number - cycle_length * 2
assert sback <= slot < sback + cycle_length * 2
return active_state.recent_block_hashes[slot - sback]
101 changes: 99 additions & 2 deletions tests/state/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import pytest

from beacon_chain.utils.blake import blake

from beacon_chain.state.active_state import ActiveState
from beacon_chain.state.block import Block
from beacon_chain.state.helpers import (
get_new_shuffling
get_new_shuffling,
get_indices_for_slot,
get_block_hash,
)


Expand All @@ -28,7 +34,12 @@ def test_get_new_shuffling_is_complete(genesis_validators, config):
)

assert len(shuffling) == config['cycle_length']

print([
[j.committee
for j in item]
for item in shuffling]
)
print('shuffling: {}'.format(shuffling))
validators = set()
shards = set()
for height_indices in shuffling:
Expand All @@ -39,3 +50,89 @@ def test_get_new_shuffling_is_complete(genesis_validators, config):

# assert len(shards) == config['shard_count']
assert len(validators) == len(genesis_validators)


@pytest.mark.parametrize(
(
'num_validators,slot,success'
),
[
(100, 0, True),
(100, 63, True),
(100, 64, False),
],
)
def test_get_indices_for_slot(
genesis_crystallized_state,
num_validators,
slot,
success,
config):
crystallized_state = genesis_crystallized_state

if success:
indices_for_slot = get_indices_for_slot(
crystallized_state,
slot,
config=config,
)
assert len(indices_for_slot) > 0
else:
with pytest.raises(AssertionError):
get_indices_for_slot(
crystallized_state,
slot,
config=config,
)


@pytest.mark.parametrize(
(
'slot,success'
),
[
(0, True),
(127, True),
(128, False),
],
)
def test_get_block_hash(
genesis_block,
slot,
success,
config):
cycle_length = config['cycle_length']

blocks = get_empty_chain(cycle_length * 3)
active_state = ActiveState(
recent_block_hashes=[block.hash for block in blocks[:cycle_length*2]]
)
if success:
block_hash = get_block_hash(
active_state,
blocks[cycle_length*2],
slot,
config=config,
)
assert block_hash == blocks[slot].hash
else:
with pytest.raises(AssertionError):
get_block_hash(
active_state,
blocks[cycle_length*2],
slot,
config=config,
)


def get_empty_chain(length):
blocks = []
for slot in range(length * 3):
blocks.append(
Block(
slot_number=slot,
parent_hash=blocks[slot-1].hash if slot > 0 else b'00'*32
)
)

return blocks

0 comments on commit 859a0db

Please sign in to comment.