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 6 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
89 changes: 87 additions & 2 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 @@ -236,6 +238,10 @@ Code snippets appearing in `this style` are to be interpreted as Python code.
| `MIN_VALIDATOR_WITHDRAWABILITY_DELAY` | `2**8` (= 256) | epochs | ~27 hours |
| `PERSISTENT_COMMITTEE_PERIOD` | `2**11` (= 2,048) | epochs | 9 days |
| `MAX_CROSSLINK_EPOCHS` | `2**6` (= 64) |
| `RANDAO_SLASHING_EPOCHS` | `2` | epochs | 12.8 minutes |
| `EPOCHS_PER_CUSTODY_PERIOD` | `2**11` (= 2,048) | epochs | ~9 days |
| `CUSTODY_PERIOD_TO_RANDAO_PADDING` | `2**6` (= 64) | epochs | ~6.8 hours |


* `MAX_CROSSLINK_EPOCHS` should be a small constant times `SHARD_COUNT // SLOTS_PER_EPOCH`

Expand All @@ -257,6 +263,7 @@ Code snippets appearing in `this style` are to be interpreted as Python code.
| `PROPOSER_REWARD_QUOTIENT` | `2**3` (= 8) |
| `INACTIVITY_PENALTY_QUOTIENT` | `2**24` (= 16,777,216) |
| `MIN_PENALTY_QUOTIENT` | `2**5` (= 32) |
| `RANDAO_REVEAL_PENALTY_QUOTIENT` | `2**7` (= 32) |

* The `BASE_REWARD_QUOTIENT` parameter dictates the per-epoch reward. It corresponds to ~2.54% annual interest assuming 10 million participating ETH in every epoch.
* The `INACTIVITY_PENALTY_QUOTIENT` equals `INVERSE_SQRT_E_DROP_TIME**2` where `INVERSE_SQRT_E_DROP_TIME := 2**12 epochs` (~18 days) is the time it takes the inactivity penalty to reduce the balance of non-participating [validators](#dfn-validator) to about `1/sqrt(e) ~= 60.6%`. Indeed, the balance retained by offline [validators](#dfn-validator) after `n` epochs is about `(1 - 1/INACTIVITY_PENALTY_QUOTIENT)**(n**2/2)` so after `INVERSE_SQRT_E_DROP_TIME` epochs it is roughly `(1 - 1/INACTIVITY_PENALTY_QUOTIENT)**(INACTIVITY_PENALTY_QUOTIENT/2) ~= 1/sqrt(e)`.
Expand All @@ -267,6 +274,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 @@ -429,7 +437,9 @@ The types are defined topologically to aid in facilitating an executable version
# Was the validator slashed
'slashed': 'bool',
# Rounded balance
'high_balance': 'uint64'
'high_balance': 'uint64',
# Future RANDAO reveals already exposed
'exposed_randao_reveals': ['uint64'],
}
```

Expand Down Expand Up @@ -485,6 +495,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 +579,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 @@ -1343,7 +1366,8 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
withdrawable_epoch=FAR_FUTURE_EPOCH,
initiated_exit=False,
slashed=False,
high_balance=0
high_balance=0,
exposed_randao_reveals=[],
dankrad marked this conversation as resolved.
Show resolved Hide resolved
)

# Note: In phase 2 registry indices that have been withdrawn for a long time will be recycled.
Expand Down Expand Up @@ -1512,6 +1536,7 @@ def get_empty_block() -> BeaconBlock:
),
proposer_slashings=[],
attester_slashings=[],
randao_reveal_slashings=[],
attestations=[],
deposits=[],
voluntary_exits=[],
Expand Down Expand Up @@ -2170,6 +2195,12 @@ def finish_epoch_update(state: BeaconState) -> None:
# Rotate current/previous epoch attestations
state.previous_epoch_attestations = state.current_epoch_attestations
state.current_epoch_attestations = []

# Clean up exposed RANDAO reveals
for index, validator in enumerate(state.validator_registry):
for reveal_index, reveal_epoch in enumerate(validator.exposed_randao_reveals):
if reveal_epoch <= current_epoch:
del validator.exposed_randao_reveals[reveal_index]
```

### Per-slot processing
Expand Down Expand Up @@ -2310,6 +2341,60 @@ 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`.

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 randao_reveal_slashing.epoch >= get_current_epoch(state) + RANDAO_SLASHING_EPOCHS
assert revealer.slashed is False
assert randao_reveal_slashing.epoch not in state.validator_registry[randao_reveal_slashing.revealer_index].exposed_randao_reveals

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,
),
)

if randao_reveal_slashing.epoch >= get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING:
dankrad marked this conversation as resolved.
Show resolved Hide resolved
# Replacement for custody reveal slashing
slash_validator(state, randao_reveal_slashing.revealer_index, randao_reveal_slashing.masker_index)
else:
# Only a small penalty for RANDAO reveal that does not interfere with the custody period
penalty = get_effective_balance(state, randao_reveal_slashing.revealer_index) // RANDAO_REVEAL_PENALTY_QUOTIENT
dankrad marked this conversation as resolved.
Show resolved Hide resolved
proposer_index = get_beacon_proposer_index(state, state.slot)
dankrad marked this conversation as resolved.
Show resolved Hide resolved
whistleblower_index = randao_reveal_slashing.masker_index
whistleblowing_reward = penalty // WHISTLEBLOWING_REWARD_QUOTIENT
proposer_reward = whistleblowing_reward // PROPOSER_REWARD_QUOTIENT
increase_balance(state, proposer_index, proposer_reward)
increase_balance(state, whistleblower_index, whistleblowing_reward - proposer_reward)
decrease_balance(state, randao_reveal_slashing.revealer_index, penalty)
state.validator_registry[randao_reveal_slashing.revealer_index].exposed_randao_reveals.append(randao_reveal_slashing.epoch)


dankrad marked this conversation as resolved.
Show resolved Hide resolved
```

##### Attestations

Verify that `len(block.body.attestations) <= MAX_ATTESTATIONS`.
Expand Down
51 changes: 19 additions & 32 deletions specs/core/1_custody-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ This document details the beacon chain additions and changes in Phase 1 of Ether
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
| `MAX_CHUNK_CHALLENGE_DELAY` | `2**11` (= 2,048) | epochs | ~9 days |
| `EPOCHS_PER_CUSTODY_PERIOD` | `2**11` (= 2,048) | epochs | ~9 days |
| `CUSTODY_RESPONSE_DEADLINE` | `2**14` (= 16,384) | epochs | ~73 days |

### Max operations per block
Expand All @@ -92,8 +91,7 @@ This document details the beacon chain additions and changes in Phase 1 of Ether

| Name | Value |
| - | - |
| `DOMAIN_CUSTODY_KEY_REVEAL` | `6` |
| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `7` |
| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `6` |

## Data structures

Expand Down Expand Up @@ -168,8 +166,6 @@ This document details the beacon chain additions and changes in Phase 1 of Ether
'revealer_index': ValidatorIndex,
'period': 'uint64',
'key': BLSSignature,
'masker_index': ValidatorIndex,
dankrad marked this conversation as resolved.
Show resolved Hide resolved
'mask': Hash,
}
```

Expand Down Expand Up @@ -229,30 +225,29 @@ def epoch_to_custody_period(epoch: Epoch) -> int:
return epoch // EPOCHS_PER_CUSTODY_PERIOD
```

### `get_randao_epoch_for_custody_period`

```python
def get_randao_epoch_for_custody_period(period: int) -> Epoch:
return period * EPOCHS_PER_CUSTODY_PERIOD + CUSTODY_PERIOD_TO_RANDAO_PADDING
```

### `verify_custody_key`

```python
def verify_custody_key(state: BeaconState, reveal: CustodyKeyReveal) -> bool:
# Case 1: non-masked non-punitive non-early reveal
epoch_to_sign = get_randao_epoch_for_custody_period(reveal.period)
pubkeys = [state.validator_registry[reveal.revealer_index].pubkey]
message_hashes = [hash_tree_root(reveal.period)]

# Case 2: masked punitive early reveal
# Masking prevents proposer stealing the whistleblower reward
# Secure under the aggregate extraction infeasibility assumption
# See pages 11-12 of https://crypto.stanford.edu/~dabo/pubs/papers/aggreg.pdf
if reveal.mask != ZERO_HASH:
pubkeys.append(state.validator_registry[reveal.masker_index].pubkey)
message_hashes.append(reveal.mask)
message_hashes = [hash_tree_root(epoch_to_sign)]

return bls_verify_multiple(
pubkeys=pubkeys,
message_hashes=message_hashes,
signature=reveal.key,
domain=get_domain(
fork=state.fork,
epoch=reveal.period * EPOCHS_PER_CUSTODY_PERIOD,
domain_type=DOMAIN_CUSTODY_KEY_REVEAL,
epoch=epoch_to_sign * EPOCHS_PER_CUSTODY_PERIOD,
domain_type=DOMAIN_RANDAO,
),
)
```
Expand All @@ -276,21 +271,13 @@ def process_custody_reveal(state: BeaconState,
revealer = state.validator_registry[reveal.revealer_index]
current_custody_period = epoch_to_custody_period(get_current_epoch(state))

# Case 1: non-masked non-punitive non-early reveal
if reveal.mask == ZERO_HASH:
assert reveal.period == epoch_to_custody_period(revealer.activation_epoch) + revealer.custody_reveal_index
# Revealer is active or exited
assert is_active_validator(revealer, get_current_epoch(state)) or revealer.exit_epoch > get_current_epoch(state)
revealer.custody_reveal_index += 1
revealer.max_reveal_lateness = max(revealer.max_reveal_lateness, current_custody_period - reveal.period)
proposer_index = get_beacon_proposer_index(state, state.slot)
increase_balance(state, proposer_index, base_reward(state, index) // MINOR_REWARD_QUOTIENT)

# Case 2: masked punitive early reveal
else:
assert reveal.period > current_custody_period
assert revealer.slashed is False
slash_validator(state, reveal.revealer_index, reveal.masker_index)
assert reveal.period == epoch_to_custody_period(revealer.activation_epoch) + revealer.custody_reveal_index
# Revealer is active or exited
assert is_active_validator(revealer, get_current_epoch(state)) or revealer.exit_epoch > get_current_epoch(state)
revealer.custody_reveal_index += 1
revealer.max_reveal_lateness = max(revealer.max_reveal_lateness, current_custody_period - reveal.period)
proposer_index = get_beacon_proposer_index(state, state.slot)
increase_balance(state, proposer_index, base_reward(state, index) // MINOR_REWARD_QUOTIENT)
```

#### Chunk challenges
Expand Down
101 changes: 101 additions & 0 deletions tests/phase0/block_processing/test_process_randao_reveal_slashing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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,
RANDAO_SLASHING_EPOCHS,
CUSTODY_PERIOD_TO_RANDAO_PADDING
)
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
if randao_reveal_slashing.epoch >= get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING:
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_reveal_with_custody_padding(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING)
pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing, True)

return pre_state, randao_reveal_slashing, post_state

def test_reveal_with_custody_padding_minus_one(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING - 1)
pre_state, post_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing, True)

return pre_state, randao_reveal_slashing, post_state

def test_double_reveal(state):

randao_reveal_slashing1 = get_valid_randao_reveal_slashing(state, get_current_epoch(state) + RANDAO_SLASHING_EPOCHS + 1)
pre_state, intermediate_state = run_randao_reveal_slashing_processing(state, randao_reveal_slashing1)

randao_reveal_slashing2 = get_valid_randao_reveal_slashing(intermediate_state, get_current_epoch(pre_state) + RANDAO_SLASHING_EPOCHS + 1)
intermediate_state_, post_state = run_randao_reveal_slashing_processing(intermediate_state, randao_reveal_slashing2, False)


return pre_state, [randao_reveal_slashing1, randao_reveal_slashing2], post_state

def test_revealer_is_slashed(state):
randao_reveal_slashing = get_valid_randao_reveal_slashing(state, get_current_epoch(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
Loading