Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for compute_fork_digest and compute_subnet_for_attestation #1896

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ def test_get_block_signature(spec, state):
)


@with_all_phases
@spec_state_test
def test_compute_fork_digest(spec, state):
actual_fork_digest = spec.compute_fork_digest(state.fork.current_version, state.genesis_validators_root)

expected_fork_data_root = spec.hash_tree_root(
spec.ForkData(current_version=state.fork.current_version,
genesis_validators_root=state.genesis_validators_root))
Comment on lines +322 to +324
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking tip: can just use

Suggested change
expected_fork_data_root = spec.hash_tree_root(
spec.ForkData(current_version=state.fork.current_version,
genesis_validators_root=state.genesis_validators_root))
expected_fork_data_root = spec.compute_fork_data_root(
current_version=state.fork.current_version,
genesis_validators_root=state.genesis_validators_root,
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah true! I wanted to cover testing compute_fork_data_root here too b/c I didn't see a test for that, but I suppose it's a better practice to just test 1-level down.

expected_fork_digest = spec.ForkDigest(expected_fork_data_root[:4])

assert actual_fork_digest == expected_fork_digest


# Attesting


Expand All @@ -336,6 +349,22 @@ def test_get_attestation_signature(spec, state):
)


@with_all_phases
@spec_state_test
def test_compute_subnet_for_attestation(spec, state):
for committee_idx in range(spec.MAX_COMMITTEES_PER_SLOT):
for slot in range(state.slot, state.slot + spec.SLOTS_PER_EPOCH):
actual_subnet_id = spec.compute_subnet_for_attestation(state, slot, committee_idx)

slots_since_epoch_start = slot % spec.SLOTS_PER_EPOCH
committees_since_epoch_start = spec.get_committee_count_at_slot(
state, slot) * slots_since_epoch_start
expected_subnet_id = (committees_since_epoch_start +
committee_idx) % spec.ATTESTATION_SUBNET_COUNT

assert actual_subnet_id == expected_subnet_id


# Attestation aggregation


Expand Down