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

Tests with signatures, fixes #1074 #1102

Merged
merged 38 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4f3f68b
signatures in tests, most block-ops updated, still many to go
protolambda May 11, 2019
904e2e9
BLS on/off deco
protolambda May 13, 2019
ce4daea
Merge branch 'bls-test-deco' into tests-with-sigs
protolambda May 15, 2019
51c82c5
clean up helpers, make helpers pkg
protolambda May 15, 2019
a4e2263
fix/update bls funcs for testing
protolambda May 15, 2019
e07f1bc
update imports to new helpers + fix up imports
protolambda May 15, 2019
5c3e760
update remaining imports
protolambda May 15, 2019
8a43ec0
add signing methods
protolambda May 15, 2019
4dad74e
attestation signing
protolambda May 15, 2019
a9069cb
attester slashing signing
protolambda May 15, 2019
b92a9d8
sign block headers
protolambda May 15, 2019
242bb8c
sign deposits
protolambda May 15, 2019
32efe49
proposer slashing signing
protolambda May 15, 2019
9f00e4f
sign transfers
protolambda May 15, 2019
eea6c8f
voluntary exit testing sigs
protolambda May 15, 2019
62999d8
import fixes + fix import loop + fix minor signing things
protolambda May 17, 2019
f937dec
more keys, more validators, fix import
protolambda May 17, 2019
183e3a5
minor refactor import fix
protolambda May 17, 2019
08d0ff9
fix attestation aggregate bitfields
protolambda May 17, 2019
0f00b43
reduce validator key count again, fix valid attestation creation - sn…
protolambda May 17, 2019
a10aba4
make valid attest creation fill aggregate bitfield
protolambda May 17, 2019
90bcbd6
fix attester slashing test
protolambda May 17, 2019
b0aea2a
bugfix block proc transfer test
protolambda May 20, 2019
ab251d4
make tests work fast + bls signed optionally
protolambda May 20, 2019
4d08e9d
signed block header
protolambda May 21, 2019
ee9c1d9
test tagging pattern
protolambda May 21, 2019
cfc037f
tags applied after generation of vector (and only if), make BLS decor…
protolambda May 21, 2019
8303c58
bugfix, make BLS wrapper propagate test output properly
protolambda May 21, 2019
f1ba5aa
make test generator use BLS
protolambda May 21, 2019
0e136b1
update yaml and eth-utils dependencies
protolambda May 21, 2019
6b3b512
fix header format of operations tests
protolambda May 21, 2019
12af078
make decorators return wrapped fn results properly
protolambda May 21, 2019
b919d08
comment on the deposit signature being soft-rejected
protolambda May 21, 2019
a4363ba
tests for invalid signatures
protolambda May 21, 2019
7d3147d
add inconsistent bitfields test
protolambda May 22, 2019
dad89ae
minor lint
djrtwo May 22, 2019
58fd712
fix style issue with deposit processing helper
protolambda May 22, 2019
958f71b
minor underflow fix for proposer slashing test
protolambda May 22, 2019
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
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
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
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,35 @@ 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)


@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 +96,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 +156,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,35 +166,44 @@ 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

Expand All @@ -196,19 +212,23 @@ def test_bad_previous_crosslink(state):

@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)
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
get_beacon_proposer_index,
process_attester_slashing,
)
from eth2spec.test.helpers import (
from eth2spec.test.context import spec_state_test, expect_assertion_error
from eth2spec.test.helpers.attestations import sign_indexed_attestation
from eth2spec.test.helpers.attester_slashings import get_valid_attester_slashing
from eth2spec.test.helpers.block import apply_empty_block
from eth2spec.test.helpers.state import (
get_balance,
get_valid_attester_slashing,
next_epoch,
)

from eth2spec.test.context import spec_state_test, expect_assertion_error


def run_attester_slashing_processing(state, attester_slashing, valid=True):
"""
Expand Down Expand Up @@ -45,62 +46,73 @@ def run_attester_slashing_processing(state, attester_slashing, valid=True):
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH

# lost whistleblower reward
assert (
get_balance(state, slashed_index) <
pre_slashed_balance
)

# gained whistleblower reward
assert (
get_balance(state, proposer_index) >
pre_proposer_balance
)
if slashed_index != proposer_index:
# lost whistleblower reward
assert (
get_balance(state, slashed_index) <
pre_slashed_balance
)

# gained whistleblower reward
assert (
get_balance(state, proposer_index) >
pre_proposer_balance
)
else:
# gained rewards for all slashings, which may include others. And only lost that of themselves.
# Netto at least 0, if more people where slashed, a balance increase.
assert get_balance(state, slashed_index) >= pre_slashed_balance

yield 'post', state


@spec_state_test
def test_success_double(state):
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=True, signed_2=True)

yield from run_attester_slashing_processing(state, attester_slashing)


@spec_state_test
def test_success_surround(state):
next_epoch(state)
apply_empty_block(state)

state.current_justified_epoch += 1
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=False, signed_2=True)

# set attestion1 to surround attestation 2
attester_slashing.attestation_1.data.source_epoch = attester_slashing.attestation_2.data.source_epoch - 1
attester_slashing.attestation_1.data.target_epoch = attester_slashing.attestation_2.data.target_epoch + 1

sign_indexed_attestation(state, attester_slashing.attestation_1)

yield from run_attester_slashing_processing(state, attester_slashing)


@spec_state_test
def test_same_data(state):
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=False, signed_2=True)

attester_slashing.attestation_1.data = attester_slashing.attestation_2.data
sign_indexed_attestation(state, attester_slashing.attestation_1)

yield from run_attester_slashing_processing(state, attester_slashing, False)


@spec_state_test
def test_no_double_or_surround(state):
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=False, signed_2=True)

attester_slashing.attestation_1.data.target_epoch += 1
sign_indexed_attestation(state, attester_slashing.attestation_1)

yield from run_attester_slashing_processing(state, attester_slashing, False)


@spec_state_test
def test_participants_already_slashed(state):
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=True, signed_2=True)

# set all indices to slashed
attestation_1 = attester_slashing.attestation_1
Expand All @@ -113,10 +125,11 @@ def test_participants_already_slashed(state):

@spec_state_test
def test_custody_bit_0_and_1(state):
attester_slashing = get_valid_attester_slashing(state)
attester_slashing = get_valid_attester_slashing(state, signed_1=False, signed_2=True)

attester_slashing.attestation_1.custody_bit_1_indices = (
attester_slashing.attestation_1.custody_bit_0_indices
)
sign_indexed_attestation(state, attester_slashing.attestation_1)

yield from run_attester_slashing_processing(state, attester_slashing, False)
Loading