Skip to content

Commit

Permalink
Use 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 859a0db commit 34f44f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
43 changes: 23 additions & 20 deletions beacon_chain/state/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ def get_signed_parent_hashes(active_state,
attestation,
config=DEFAULT_CONFIG):
cycle_length = config['cycle_length']
oblique_parent_hashes = attestation.oblique_parent_hashes

parent_hashes = (
active_state.recent_block_hashes[
cycle_length + attestation.slot - block.slot_number:
cycle_length * 2 + attestation.slot - block.slot_number - len(oblique_parent_hashes)
] +
oblique_parent_hashes
parent_hashes = [
get_block_hash(
active_state,
block,
attestation.slot - cycle_length + i,
config,
)
for i
in range(cycle_length - len(attestation.oblique_parent_hashes))
] + attestation.oblique_parent_hashes

)
return parent_hashes


Expand All @@ -37,19 +39,20 @@ def get_attestation_indices(crystallized_state,
last_state_recalc = crystallized_state.last_state_recalc
cycle_length = config['cycle_length']
indices_for_slots = crystallized_state.indices_for_slots

shard_position = list(filter(
lambda x: (
indices_for_slots[attestation.slot - last_state_recalc + cycle_length][x].shard_id ==
attestation.shard_id
),
range(len(indices_for_slots[attestation.slot - last_state_recalc + cycle_length]))
))[0]
attestation_indices = (
indices_for_slots[
attestation.slot - last_state_recalc + cycle_length
][shard_position].committee
shard_id = attestation.shard_id

filtered_indices_for_slot = list(
filter(
lambda x: x.shard_id == shard_id,
get_indices_for_slot(
crystallized_state,
attestation.slot,
config=config,
)
)
)
if filtered_indices_for_slot:
attestation_indices = filtered_indices_for_slot[0].committee

return attestation_indices

Expand Down
16 changes: 16 additions & 0 deletions tests/state/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from beacon_chain.state.config import (
DEFAULT_CONFIG,
)
from beacon_chain.state.block import Block
from beacon_chain.state.validator_record import (
ValidatorRecord,
)
Expand All @@ -16,3 +17,18 @@ def mock_validator_record(pubkey, start_dynasty=0, config=DEFAULT_CONFIG):
start_dynasty=start_dynasty,
end_dynasty=config['default_end_dynasty']
)


def get_pseudo_chain(length):
"""Get a pseudo chain, only slot_number and parent_hash are valid.
"""
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
25 changes: 5 additions & 20 deletions tests/state/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
get_block_hash,
)

from tests.state.helpers import(
get_pseudo_chain,
)


@pytest.mark.parametrize(
(
Expand All @@ -34,12 +38,6 @@ 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 Down Expand Up @@ -103,7 +101,7 @@ def test_get_block_hash(
config):
cycle_length = config['cycle_length']

blocks = get_empty_chain(cycle_length * 3)
blocks = get_pseudo_chain(cycle_length * 3)
active_state = ActiveState(
recent_block_hashes=[block.hash for block in blocks[:cycle_length*2]]
)
Expand All @@ -123,16 +121,3 @@ def test_get_block_hash(
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 34f44f3

Please sign in to comment.