Skip to content

Commit

Permalink
Merge pull request #1102 from ethereum/tests-with-sigs
Browse files Browse the repository at this point in the history
Tests with signatures, fixes #1074
  • Loading branch information
djrtwo authored May 22, 2019
2 parents ca22c19 + 958f71b commit 43bb64e
Show file tree
Hide file tree
Showing 42 changed files with 1,106 additions and 709 deletions.
2 changes: 1 addition & 1 deletion scripts/phase0/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def build_phase0_spec(sourcefile, outfile):
Tuple,
)
from eth2spec.utils.minimal_ssz import *
from eth2spec.utils.bls_stub import *
from eth2spec.utils.bls import *
""")
for i in (1, 2, 3, 4, 8, 32, 48, 96):
Expand Down
3 changes: 2 additions & 1 deletion specs/core/0_beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,8 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
amount = deposit.data.amount
validator_pubkeys = [v.pubkey for v in state.validator_registry]
if pubkey not in validator_pubkeys:
# Verify the deposit signature (proof of possession)
# Verify the deposit signature (proof of possession).
# Invalid signatures are allowed by the deposit contract, and hence included on-chain, but must not be processed.
if not bls_verify(pubkey, signing_root(deposit.data), deposit.data.signature, get_domain(state, DOMAIN_DEPOSIT)):
return

Expand Down
2 changes: 1 addition & 1 deletion test_generators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ It's recommended to extend the base-generator.

Create a `requirements.txt` in the root of your generator directory:
```
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
../../test_libs/config_helpers
../../test_libs/pyspec
Expand Down
2 changes: 1 addition & 1 deletion test_generators/bls/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
py-ecc==1.6.0
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
2 changes: 1 addition & 1 deletion test_generators/operations/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
../../test_libs/config_helpers
../../test_libs/pyspec
4 changes: 2 additions & 2 deletions test_generators/operations/suite_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def generate_from_tests(pkg):
for name in fn_names:
tfn = getattr(pkg, name)
try:
out.append(tfn(generator_mode=True))
out.append(tfn(generator_mode=True, bls_active=True))
except AssertionError:
print("ERROR: failed to generate vector from test: %s (pkg: %s)" % (name, pkg.__name__))
return out
Expand All @@ -34,6 +34,6 @@ def suite_definition(configs_path: str) -> gen_typing.TestSuiteOutput:
forks=["phase0"],
config=config_name,
runner="operations",
handler=config_name,
handler=operation_name,
test_cases=get_cases()))
return suite_definition
2 changes: 1 addition & 1 deletion test_generators/shuffling/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
../../test_libs/config_helpers
../../test_libs/pyspec
2 changes: 1 addition & 1 deletion test_generators/ssz_generic/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
../../test_libs/config_helpers
ssz==0.1.0a2
2 changes: 1 addition & 1 deletion test_generators/ssz_static/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eth-utils==1.4.1
eth-utils==1.6.0
../../test_libs/gen_helpers
../../test_libs/config_helpers
../../test_libs/pyspec
2 changes: 1 addition & 1 deletion test_libs/config_helpers/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruamel.yaml==0.15.87
ruamel.yaml==0.15.96
2 changes: 1 addition & 1 deletion test_libs/config_helpers/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
name='config_helpers',
packages=['preset_loader'],
install_requires=[
"ruamel.yaml==0.15.87"
"ruamel.yaml==0.15.96"
]
)
4 changes: 2 additions & 2 deletions test_libs/gen_helpers/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ruamel.yaml==0.15.87
eth-utils==1.4.1
ruamel.yaml==0.15.96
eth-utils==1.6.0
4 changes: 2 additions & 2 deletions test_libs/gen_helpers/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name='gen_helpers',
packages=['gen_base'],
install_requires=[
"ruamel.yaml==0.15.87",
"eth-utils==1.4.1"
"ruamel.yaml==0.15.96",
"eth-utils==1.6.0"
]
)
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from copy import deepcopy

import eth2spec.phase0.spec as spec

from eth2spec.phase0.state_transition import (
state_transition,
)
from eth2spec.phase0.spec import (
get_current_epoch,
process_attestation
)
from eth2spec.test.helpers import (
build_empty_block_for_next_slot,
from eth2spec.phase0.state_transition import (
state_transition_to,
)
from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls
from eth2spec.test.helpers.attestations import (
get_valid_attestation,
sign_attestation,
)
from eth2spec.test.helpers.state import (
next_epoch,
next_slot,
)

from eth2spec.test.context import spec_state_test, expect_assertion_error
from eth2spec.test.helpers.block import apply_empty_block


def run_attestation_processing(state, attestation, valid=True):
Expand Down Expand Up @@ -56,37 +57,44 @@ def run_attestation_processing(state, attestation, valid=True):

@spec_state_test
def test_success(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=True)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

yield from run_attestation_processing(state, attestation)


@spec_state_test
def test_success_previous_epoch(state):
attestation = get_valid_attestation(state)
block = build_empty_block_for_next_slot(state)
block.slot = state.slot + spec.SLOTS_PER_EPOCH
state_transition(state, block)
attestation = get_valid_attestation(state, signed=True)
next_epoch(state)
apply_empty_block(state)

yield from run_attestation_processing(state, attestation)


@always_bls
@spec_state_test
def test_invalid_attestation_signature(state):
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_before_inclusion_delay(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=True)
# do not increment slot to allow for inclusion delay

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_after_epoch_slots(state):
attestation = get_valid_attestation(state)
block = build_empty_block_for_next_slot(state)
attestation = get_valid_attestation(state, signed=True)
# increment past latest inclusion slot
block.slot = state.slot + spec.SLOTS_PER_EPOCH + 1
state_transition(state, block)
state_transition_to(state, state.slot + spec.SLOTS_PER_EPOCH + 1)
apply_empty_block(state)

yield from run_attestation_processing(state, attestation, False)

Expand All @@ -97,44 +105,52 @@ def test_old_source_epoch(state):
state.finalized_epoch = 2
state.previous_justified_epoch = 3
state.current_justified_epoch = 4
attestation = get_valid_attestation(state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1)
attestation = get_valid_attestation(state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1, signed=False)

# test logic sanity check: make sure the attestation is pointing to oldest known source epoch
assert attestation.data.source_epoch == state.previous_justified_epoch

# Now go beyond that, it will be invalid
attestation.data.source_epoch -= 1

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_wrong_shard(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.data.shard += 1

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_new_source_epoch(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.data.source_epoch += 1

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_source_root_is_target_root(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.data.source_root = attestation.data.target_root

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


Expand All @@ -149,7 +165,7 @@ def test_invalid_current_source_root(state):
state.current_justified_epoch = 4
state.current_justified_root = b'\xff' * 32

attestation = get_valid_attestation(state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1)
attestation = get_valid_attestation(state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

# Test logic sanity checks:
Expand All @@ -159,56 +175,81 @@ def test_invalid_current_source_root(state):
# Make attestation source root invalid: should be previous justified, not current one
attestation.data.source_root = state.current_justified_root

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_bad_source_root(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.data.source_root = b'\x42' * 32

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_non_zero_crosslink_data_root(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.data.crosslink_data_root = b'\x42' * 32

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_bad_previous_crosslink(state):
next_epoch(state)
attestation = get_valid_attestation(state)
apply_empty_block(state)

attestation = get_valid_attestation(state, signed=True)
for _ in range(spec.MIN_ATTESTATION_INCLUSION_DELAY):
next_slot(state)
apply_empty_block(state)

state.current_crosslinks[attestation.data.shard].epoch += 10

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_inconsistent_bitfields(state):
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.custody_bitfield = deepcopy(attestation.aggregation_bitfield) + b'\x00'

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_non_empty_custody_bitfield(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.custody_bitfield = deepcopy(attestation.aggregation_bitfield)

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)


@spec_state_test
def test_empty_aggregation_bitfield(state):
attestation = get_valid_attestation(state)
attestation = get_valid_attestation(state, signed=False)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY

attestation.aggregation_bitfield = b'\x00' * len(attestation.aggregation_bitfield)

sign_attestation(state, attestation)

yield from run_attestation_processing(state, attestation, False)
Loading

0 comments on commit 43bb64e

Please sign in to comment.