Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Normalize ETH units to Gwei (#249)
Browse files Browse the repository at this point in the history
* Rename

1. MIN_DEPOSIT -> MIN_DEPOSIT_AMOUNT
2. MAX_DEPOSIT -> MAX_DEPOSIT_AMOUNT

* Normalize ETH units to Gwei

* PR feedback
  • Loading branch information
hwwhww committed Jan 31, 2019
1 parent c431b19 commit e7366fa
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 83 deletions.
8 changes: 2 additions & 6 deletions eth2/beacon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@
shuffle,
split,
)
from eth2.beacon.constants import (
GWEI_PER_ETH,
)
from eth2.beacon.enums import (
SignatureDomain,
)
from eth2.beacon.typing import (
Bitfield,
BLSPubkey,
Ether,
Gwei,
ShardNumber,
SlotNumber,
Expand Down Expand Up @@ -423,12 +419,12 @@ def get_attestation_participants(state: 'BeaconState',
def get_effective_balance(
validator_balances: Sequence[Gwei],
index: ValidatorIndex,
max_deposit: Ether) -> Gwei:
max_deposit_amount: Gwei) -> Gwei:
"""
Return the effective balance (also known as "balance at stake") for a
``validator`` with the given ``index``.
"""
return min(validator_balances[index], Gwei(max_deposit * GWEI_PER_ETH))
return min(validator_balances[index], max_deposit_amount)


def get_fork_version(fork: 'Fork',
Expand Down
8 changes: 3 additions & 5 deletions eth2/beacon/on_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from eth2.beacon.constants import (
EMPTY_SIGNATURE,
GWEI_PER_ETH,
)
from eth2.beacon.deposit_helpers import (
process_deposit,
Expand All @@ -31,7 +30,6 @@
from eth2.beacon.types.forks import Fork
from eth2.beacon.types.states import BeaconState
from eth2.beacon.typing import (
Ether,
Gwei,
ShardNumber,
SlotNumber,
Expand Down Expand Up @@ -68,7 +66,7 @@ def get_initial_beacon_state(*,
latest_block_roots_length: int,
latest_index_roots_length: int,
epoch_length: int,
max_deposit: Ether,
max_deposit_amount: Gwei,
latest_penalized_exit_length: int,
latest_randao_mixes_length: int,
entry_exit_delay: int) -> BeaconState:
Expand Down Expand Up @@ -148,8 +146,8 @@ def get_initial_beacon_state(*,
is_enough_effective_balance = get_effective_balance(
state.validator_balances,
validator_index,
max_deposit,
) >= max_deposit * GWEI_PER_ETH
max_deposit_amount,
) >= max_deposit_amount
if is_enough_effective_balance:
state = activate_validator(
state,
Expand Down
12 changes: 6 additions & 6 deletions eth2/beacon/state_machines/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Address,
)
from eth2.beacon.typing import (
SlotNumber,
ShardNumber,
Ether,
Gwei,
Second,
ShardNumber,
SlotNumber,
)


Expand All @@ -19,7 +19,7 @@
# Misc
('SHARD_COUNT', int),
('TARGET_COMMITTEE_SIZE', int),
('EJECTION_BALANCE', Ether),
('EJECTION_BALANCE', Gwei),
('MAX_BALANCE_CHURN_QUOTIENT', int),
('BEACON_CHAIN_SHARD_NUMBER', ShardNumber),
('MAX_INDICES_PER_SLASHABLE_VOTE', int),
Expand All @@ -31,8 +31,8 @@
# Deposit contract
('DEPOSIT_CONTRACT_ADDRESS', Address),
('DEPOSIT_CONTRACT_TREE_DEPTH', int),
('MIN_DEPOSIT', Ether),
('MAX_DEPOSIT', Ether),
('MIN_DEPOSIT_AMOUNT', Gwei),
('MAX_DEPOSIT_AMOUNT', Gwei),
# ZERO_HASH (ZERO_HASH32) is defined in constants.py
# Initial values
('GENESIS_FORK_VERSION', int),
Expand Down
15 changes: 9 additions & 6 deletions eth2/beacon/state_machines/forks/serenity/configs.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from eth.constants import (
ZERO_ADDRESS,
)
from eth2.beacon.constants import (
GWEI_PER_ETH,
)
from eth2.beacon.state_machines.configs import BeaconConfig
from eth2.beacon.typing import (
SlotNumber,
ShardNumber,
Ether,
Gwei,
Second,
ShardNumber,
SlotNumber,
)


SERENITY_CONFIG = BeaconConfig(
# Misc
SHARD_COUNT=2**10, # (= 1,024) shards
TARGET_COMMITTEE_SIZE=2**7, # (= 128) validators
EJECTION_BALANCE=Ether(2**4), # (= 16) ETH
EJECTION_BALANCE=Gwei(2**4 * GWEI_PER_ETH), # (= 16,000,000,000) Gwei
MAX_BALANCE_CHURN_QUOTIENT=2**5, # (= 32)
BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
MAX_INDICES_PER_SLASHABLE_VOTE=2**12, # (= 4,096) votes
Expand All @@ -25,8 +28,8 @@
# Deposit contract
DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS, # TBD
DEPOSIT_CONTRACT_TREE_DEPTH=2**5, # (= 32)
MIN_DEPOSIT=Ether(2**0), # (= 1) ETH
MAX_DEPOSIT=Ether(2**5), # (= 32) ETH
MIN_DEPOSIT_AMOUNT=Gwei(2**0 * GWEI_PER_ETH), # (= 1,000,000,000) Gwei
MAX_DEPOSIT_AMOUNT=Gwei(2**5 * GWEI_PER_ETH), # (= 32,000,000,00) Gwei
# Initial values
GENESIS_FORK_VERSION=0,
GENESIS_SLOT=SlotNumber(0),
Expand Down
7 changes: 2 additions & 5 deletions eth2/beacon/tools/builder/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
Type,
)

from eth2.beacon.constants import (
GWEI_PER_ETH,
)
from eth2.beacon.on_startup import (
get_genesis_block,
get_initial_beacon_state,
Expand Down Expand Up @@ -75,7 +72,7 @@ def create_mock_initial_validator_deposits(
slot=config.GENESIS_SLOT,
),
),
amount=config.MAX_DEPOSIT * GWEI_PER_ETH,
amount=config.MAX_DEPOSIT_AMOUNT,
timestamp=deposit_timestamp,
),
)
Expand Down Expand Up @@ -114,7 +111,7 @@ def create_mock_genesis(
latest_block_roots_length=config.LATEST_BLOCK_ROOTS_LENGTH,
latest_index_roots_length=config.LATEST_INDEX_ROOTS_LENGTH,
epoch_length=config.EPOCH_LENGTH,
max_deposit=config.MAX_DEPOSIT,
max_deposit_amount=config.MAX_DEPOSIT_AMOUNT,
latest_penalized_exit_length=config.LATEST_PENALIZED_EXIT_LENGTH,
latest_randao_mixes_length=config.LATEST_RANDAO_MIXES_LENGTH,
entry_exit_delay=config.ENTRY_EXIT_DELAY,
Expand Down
1 change: 0 additions & 1 deletion eth2/beacon/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
ValidatorIndex = NewType('ValidatorIndex', int) # uint24
CommitteeIndex = NewType('CommitteeIndex', int)

Ether = NewType('Ether', int) # uint64
Gwei = NewType('Gwei', int) # uint64

Timestamp = NewType('Timestamp', int)
Expand Down
10 changes: 5 additions & 5 deletions eth2/beacon/validator_status_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from eth2.beacon.types.states import BeaconState
from eth2.beacon.typing import (
Ether,
Gwei,
SlotNumber,
ValidatorIndex,
)
Expand Down Expand Up @@ -100,7 +100,7 @@ def _settle_penality_to_validator_and_whistleblower(
latest_penalized_exit_length: int,
whistleblower_reward_quotient: int,
epoch_length: int,
max_deposit: Ether,
max_deposit_amount: Gwei,
target_committee_size: int,
shard_count: int) -> BeaconState:
"""
Expand All @@ -122,7 +122,7 @@ def _settle_penality_to_validator_and_whistleblower(
effective_balance = get_effective_balance(
state.validator_balances,
validator_index,
max_deposit,
max_deposit_amount,
)
penalized_exit_balance = (
state.latest_penalized_balances[current_epoch_penalization_index] +
Expand Down Expand Up @@ -174,7 +174,7 @@ def penalize_validator(state: BeaconState,
latest_penalized_exit_length: int,
whistleblower_reward_quotient: int,
entry_exit_delay: int,
max_deposit: Ether,
max_deposit_amount: Gwei,
target_committee_size: int,
shard_count: int) -> BeaconState:
"""
Expand All @@ -189,7 +189,7 @@ def penalize_validator(state: BeaconState,
latest_penalized_exit_length=latest_penalized_exit_length,
whistleblower_reward_quotient=whistleblower_reward_quotient,
epoch_length=epoch_length,
max_deposit=max_deposit,
max_deposit_amount=max_deposit_amount,
target_committee_size=target_committee_size,
shard_count=shard_count,
)
Expand Down
27 changes: 13 additions & 14 deletions tests/eth2/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from eth2.beacon.constants import (
EMPTY_SIGNATURE,
FAR_FUTURE_SLOT,
GWEI_PER_ETH,
)

from eth2.beacon.types.attestation_data import AttestationData
Expand Down Expand Up @@ -345,7 +344,7 @@ def filled_beacon_state(genesis_slot,


@pytest.fixture()
def ten_validators_state(filled_beacon_state, max_deposit):
def ten_validators_state(filled_beacon_state, max_deposit_amount):
validator_count = 10
return filled_beacon_state.copy(
validator_registry=tuple(
Expand All @@ -355,7 +354,7 @@ def ten_validators_state(filled_beacon_state, max_deposit):
)
for index in range(validator_count)
),
validator_balances=(max_deposit * GWEI_PER_ETH,) * validator_count,
validator_balances=(max_deposit_amount,) * validator_count,
)


Expand Down Expand Up @@ -451,13 +450,13 @@ def deposit_contract_tree_depth():


@pytest.fixture
def min_deposit():
return SERENITY_CONFIG.MIN_DEPOSIT
def min_deposit_amount():
return SERENITY_CONFIG.MIN_DEPOSIT_AMOUNT


@pytest.fixture
def max_deposit():
return SERENITY_CONFIG.MAX_DEPOSIT
def max_deposit_amount():
return SERENITY_CONFIG.MAX_DEPOSIT_AMOUNT


@pytest.fixture
Expand Down Expand Up @@ -605,7 +604,7 @@ def genesis_block(genesis_state, genesis_slot):
@pytest.fixture
def initial_validators(init_validator_pubkeys,
init_randao,
max_deposit):
max_deposit_amount):
"""
Inactive
"""
Expand All @@ -632,9 +631,9 @@ def activated_genesis_validators(initial_validators, genesis_slot):


@pytest.fixture
def genesis_balances(init_validator_pubkeys, max_deposit):
def genesis_balances(init_validator_pubkeys, max_deposit_amount):
return tuple(
max_deposit * GWEI_PER_ETH
max_deposit_amount
for _ in init_validator_pubkeys
)

Expand All @@ -657,8 +656,8 @@ def config(
latest_penalized_exit_length,
deposit_contract_address,
deposit_contract_tree_depth,
min_deposit,
max_deposit,
min_deposit_amount,
max_deposit_amount,
genesis_fork_version,
genesis_slot,
genesis_start_shard,
Expand Down Expand Up @@ -693,8 +692,8 @@ def config(
LATEST_PENALIZED_EXIT_LENGTH=latest_penalized_exit_length,
DEPOSIT_CONTRACT_ADDRESS=deposit_contract_address,
DEPOSIT_CONTRACT_TREE_DEPTH=deposit_contract_tree_depth,
MIN_DEPOSIT=min_deposit,
MAX_DEPOSIT=max_deposit,
MIN_DEPOSIT_AMOUNT=min_deposit_amount,
MAX_DEPOSIT_AMOUNT=max_deposit_amount,
GENESIS_FORK_VERSION=genesis_fork_version,
GENESIS_SLOT=genesis_slot,
GENESIS_START_SHARD=genesis_start_shard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

from eth2._utils import bls

from eth2.beacon.constants import (
GWEI_PER_ETH,
)
from eth2.beacon.enums import (
SignatureDomain,
)
Expand Down Expand Up @@ -74,7 +71,7 @@ def test_validate_proposer_signature(
sample_beacon_state_params,
beacon_chain_shard_number,
epoch_length,
max_deposit,
max_deposit_amount,
target_committee_size,
shard_count):

Expand All @@ -83,7 +80,7 @@ def test_validate_proposer_signature(
mock_validator_record(proposer_pubkey)
for _ in range(10)
),
validator_balances=(max_deposit * GWEI_PER_ETH,) * 10,
validator_balances=(max_deposit_amount,) * 10,
)

default_block = BeaconBlock(**sample_beacon_block_params)
Expand Down
7 changes: 2 additions & 5 deletions tests/eth2/beacon/test_deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
ValidationError,
)

from eth2.beacon.constants import (
GWEI_PER_ETH,
)
from eth2.beacon.deposit_helpers import (
add_pending_validator,
process_deposit,
Expand Down Expand Up @@ -97,15 +94,15 @@ def test_validate_proof_of_possession(sample_beacon_state_params, pubkeys, privk
def test_process_deposit(sample_beacon_state_params,
privkeys,
pubkeys,
max_deposit):
max_deposit_amount):
state = BeaconState(**sample_beacon_state_params).copy(
slot=1,
validator_registry=(),
)

privkey_1 = privkeys[0]
pubkey_1 = pubkeys[0]
amount = max_deposit * GWEI_PER_ETH
amount = max_deposit_amount
withdrawal_credentials = b'\x34' * 32
custody_commitment = b'\x11' * 32
randao_commitment = b'\x56' * 32
Expand Down
Loading

0 comments on commit e7366fa

Please sign in to comment.