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

RANDAO reveal slashing, custody period staggering and integration of custody and RANDAO reveals #880

Merged
merged 46 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b7c912c
Add RandaoRevealSlashing for early Randao reveals
dankrad Apr 5, 2019
6d047ab
Merge branch 'dev' into dankrad-patch-1
djrtwo Apr 5, 2019
7255ce6
add some randao reveal slashing tests
djrtwo Apr 5, 2019
dd4dc21
Unifying RANDAO reveal slashing and Custody reveal slashing; implemen…
dankrad Apr 10, 2019
a4ac66b
Fix custody reveal domain
dankrad Apr 10, 2019
414a706
Also test exposed_randao_reveals
dankrad Apr 10, 2019
9c3b412
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad Apr 15, 2019
cc3c900
Re-add exposed_randao_reveals to validator initialization
dankrad Apr 15, 2019
b42f9aa
Fix tests
dankrad Apr 15, 2019
38c09d7
Unified Randao Reveal slashing mechanism and Custody Reveal,
dankrad Apr 15, 2019
405e8c0
Move exposed_randao_reveals into separate data structure to keep vali…
dankrad Apr 16, 2019
4565b45
new shiny staggered custody periods
CarlBeek Apr 16, 2019
8890926
Fixes style and type hinting
CarlBeek Apr 16, 2019
13e6adf
removes whitespace
CarlBeek Apr 16, 2019
402f8fa
Cleans up multi-line conditionals
CarlBeek Apr 16, 2019
c9d8be0
Make RANDAO key reveal penalties proportional to block rewards
dankrad Apr 16, 2019
0bdd969
Minor typos
CarlBeek Apr 16, 2019
9c5de4d
Minor typos
CarlBeek Apr 16, 2019
1bf310c
Fixes off-by one error
CarlBeek Apr 16, 2019
93611de
Removes unnecicary whitepsace
CarlBeek Apr 16, 2019
92864ef
Merge branch 'dev' into carl-patch-0
CarlBeek Apr 16, 2019
6fc2a7c
Clean up comments; add test for key reveal too far in the future
dankrad Apr 16, 2019
f0edc47
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad Apr 16, 2019
35a106d
Reduce the CUSTODY_PERIOD_TO_RANDAO_PADDING again
dankrad Apr 16, 2019
d5b1a00
Fix max_proposer_slot_reward
dankrad Apr 16, 2019
1a42719
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad Apr 17, 2019
79b8add
Merge branch 'dev' into dankrad-patch-1
djrtwo Apr 17, 2019
e57bd46
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad Apr 27, 2019
2503f7c
Fix types
dankrad Apr 27, 2019
437fea3
Move test
dankrad Apr 27, 2019
e5d0b79
Fix RandaoKeyReveal tests
dankrad Apr 27, 2019
088edc2
Move all RANDAO key reveal to phase 1
dankrad Apr 27, 2019
a410e96
Factor out signature checking
dankrad Apr 27, 2019
4bcf763
Merge remote-tracking branch 'origin/dev' into carl-patch-0
dankrad Apr 29, 2019
d793b07
Merge custody key reveal staggering
dankrad Apr 29, 2019
a7bc5c0
Some fixes
dankrad Apr 29, 2019
a24304a
Update specs/core/1_custody-game.md
vbuterin Apr 30, 2019
8148ce6
Addressing Vitalik's suggestions: Separate RANDAO and Custody key rev…
dankrad Apr 30, 2019
2572171
Remove remnants of verify_custody_key
dankrad Apr 30, 2019
bb0d840
RandaoKeyReveal -> EarlyDerivedSecretReveal
dankrad Apr 30, 2019
ef52ad7
Make penalty proportional to number of secrets already exposed
dankrad Apr 30, 2019
79956a8
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad May 3, 2019
33eb63c
Update specs/core/1_custody-game.md
CarlBeek May 3, 2019
a57c476
Update specs/core/1_custody-game.md
CarlBeek May 3, 2019
054128e
Update specs/core/1_custody-game.md
CarlBeek May 3, 2019
ffded82
Merge remote-tracking branch 'origin/dev' into dankrad-patch-1
dankrad May 3, 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
55 changes: 55 additions & 0 deletions specs/core/0_beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [Beacon operations](#beacon-operations)
- [`ProposerSlashing`](#proposerslashing)
- [`AttesterSlashing`](#attesterslashing)
- [`RandaoRevealSlashing`](#randaorevealslashing)
- [`Attestation`](#attestation)
- [`Deposit`](#deposit)
- [`VoluntaryExit`](#voluntaryexit)
Expand Down Expand Up @@ -135,6 +136,7 @@
- [Operations](#operations)
- [Proposer slashings](#proposer-slashings)
- [Attester slashings](#attester-slashings)
- [Randao reveal slashings](#randao-reveal-slashings)
- [Attestations](#attestations)
- [Deposits](#deposits)
- [Voluntary exits](#voluntary-exits)
Expand Down Expand Up @@ -267,6 +269,7 @@ Code snippets appearing in `this style` are to be interpreted as Python code.
| - | - |
| `MAX_PROPOSER_SLASHINGS` | `2**4` (= 16) |
| `MAX_ATTESTER_SLASHINGS` | `2**0` (= 1) |
| `MAX_RANDAO_REVEAL_SLASHINGS` | `2**0` (= 1) |
| `MAX_ATTESTATIONS` | `2**7` (= 128) |
| `MAX_DEPOSITS` | `2**4` (= 16) |
| `MAX_VOLUNTARY_EXITS` | `2**4` (= 16) |
Expand Down Expand Up @@ -485,6 +488,18 @@ The types are defined topologically to aid in facilitating an executable version
}
```

#### `RandaoRevealSlashing`

```python
{
'revealer_index': ValidatorIndex,
'epoch': 'uint64',
'reveal': 'bytes96',
'masker_index': ValidatorIndex,
'mask': 'bytes32',
}
```

#### `Attestation`

```python
Expand Down Expand Up @@ -557,6 +572,7 @@ The types are defined topologically to aid in facilitating an executable version
'eth1_data': Eth1Data,
'proposer_slashings': [ProposerSlashing],
'attester_slashings': [AttesterSlashing],
'randao_reveal_slashings': [RandaoRevealSlashing],
'attestations': [Attestation],
'deposits': [Deposit],
'voluntary_exits': [VoluntaryExit],
Expand Down Expand Up @@ -1512,6 +1528,7 @@ def get_empty_block() -> BeaconBlock:
),
proposer_slashings=[],
attester_slashings=[],
randao_reveal_slashings=[],
attestations=[],
deposits=[],
voluntary_exits=[],
Expand Down Expand Up @@ -2310,6 +2327,44 @@ def process_attester_slashing(state: BeaconState,
slash_validator(state, index)
```

##### Randao reveal slashings

Verify that `len(block.body.randao_reveal_slashings) <= MAX_RANDAO_REVEAL_SLASHINGS`.

dankrad marked this conversation as resolved.
Show resolved Hide resolved
For each `randao_reveal_slashing` in `block.body.randao_reveal_slashings`, run the following function:

```python
def process_randao_reveal_slashing(state: BeaconState,
randao_reveal_slashing: RandaoRevealSlashing) -> None:
"""
Process ``RandaoRevealSlashing`` operation.
Note that this function mutates ``state``.
"""
revealer = state.validator_registry[randao_reveal_slashing.revealer_index]
masker = state.validator_registry[randao_reveal_slashing.masker_index]
pubkeys = [revealer.pubkey, masker.pubkey]
message_hashes = [
hash_tree_root(randao_reveal_slashing.epoch),
randao_reveal_slashing.mask,
]

assert bls_verify_multiple(
dankrad marked this conversation as resolved.
Show resolved Hide resolved
pubkeys=pubkeys,
message_hashes=message_hashes,
signature=randao_reveal_slashing.reveal,
domain=get_domain(
fork=state.fork,
epoch=randao_reveal_slashing.epoch,
domain_type=DOMAIN_RANDAO,
),
)

assert randao_reveal_slashing.epoch > get_current_epoch(state)
dankrad marked this conversation as resolved.
Show resolved Hide resolved
dankrad marked this conversation as resolved.
Show resolved Hide resolved
assert revealer.slashed is False

dankrad marked this conversation as resolved.
Show resolved Hide resolved
slash_validator(state, randao_reveal_slashing.revealer_index, randao_reveal_slashing.masker_index)
```

##### Attestations

Verify that `len(block.body.attestations) <= MAX_ATTESTATIONS`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from copy import deepcopy
import pytest

import build.phase0.spec as spec
from build.phase0.spec import (
get_balance,
get_current_epoch,
process_randao_reveal_slashing,
)
from tests.phase0.helpers import (
get_valid_randao_reveal_slashing,
)

# mark entire file as 'randao_reveal_slashings'
pytestmark = pytest.mark.randao_reveal_slashings


def run_randao_reveal_slashing_processing(state, randao_reveal_slashing, valid=True):
"""
Run ``process_randao_reveal_slashing`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
"""
post_state = deepcopy(state)

if not valid:
with pytest.raises(AssertionError):
process_randao_reveal_slashing(post_state, randao_reveal_slashing)
return state, None

process_randao_reveal_slashing(post_state, randao_reveal_slashing)

slashed_validator = post_state.validator_registry[randao_reveal_slashing.revealer_index]
assert not slashed_validator.initiated_exit
assert slashed_validator.slashed
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
# lost whistleblower reward
assert (
get_balance(post_state, randao_reveal_slashing.revealer_index) <
get_balance(state, randao_reveal_slashing.revealer_index)
)

return state, post_state


def test_success(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state)

pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing)

return pre_state, randao_reveal_slashing, post_state


def test_reveal_from_current_epoch(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state, get_current_epoch(state))

pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing, False)

return pre_state, randao_reveal_slashing, post_state


def test_reveal_from_past_epoch(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state, get_current_epoch(state) - 1)

pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing, False)

return pre_state, randao_reveal_slashing, post_state


def test_revealer_is_slashed(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state)
state.validator_registry[randao_reveal_slashing.revealer_index].slashed = True

pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing, False)

return pre_state, randao_reveal_slashing, post_state
38 changes: 38 additions & 0 deletions tests/phase0/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DepositData,
Eth1Data,
ProposerSlashing,
RandaoRevealSlashing,
VoluntaryExit,
# functions
convert_to_indexed,
Expand All @@ -31,6 +32,7 @@
get_empty_block,
get_epoch_start_slot,
get_genesis_beacon_state,
hash_tree_root,
slot_to_epoch,
verify_merkle_branch,
hash,
Expand Down Expand Up @@ -257,6 +259,42 @@ def get_valid_attester_slashing(state):
)


def get_valid_randao_reveal_slashing(state, epoch=None):
current_epoch = get_current_epoch(state)
revealer_index = get_active_validator_indices(state.validator_registry, current_epoch)[-1]
masker_index = get_active_validator_indices(state.validator_registry, current_epoch)[0]

if epoch is None:
epoch = current_epoch + 2

reveal = bls.sign(
message_hash=hash_tree_root(epoch),
privkey=pubkey_to_privkey[state.validator_registry[revealer_index].pubkey],
domain=get_domain(
fork=state.fork,
epoch=epoch,
domain_type=spec.DOMAIN_RANDAO,
),
)
mask = bls.sign(
message_hash=hash_tree_root(epoch),
privkey=pubkey_to_privkey[state.validator_registry[masker_index].pubkey],
domain=get_domain(
fork=state.fork,
epoch=epoch,
domain_type=spec.DOMAIN_RANDAO,
),
)

return RandaoRevealSlashing(
revealer_index=revealer_index,
epoch=epoch,
reveal=reveal,
masker_index=masker_index,
mask=mask,
)


def get_valid_attestation(state, slot=None):
if slot is None:
slot = state.slot
Expand Down
7 changes: 7 additions & 0 deletions utils/phase0/state_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def process_operations(state: BeaconState, block: BeaconBlock) -> None:
spec.process_attester_slashing,
)

process_operation_type(
state,
block.body.randao_reveal_slashings,
spec.MAX_RANDAO_REVEAL_SLASHINGS,
spec.process_randao_reveal_slashing,
)

process_operation_type(
state,
block.body.attestations,
Expand Down