Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
p-mc2 committed Sep 24, 2018
2 parents f15c68a + 16ca0ef commit b3c20d3
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ venv
test-reports/
notes.txt
.mypy_cache
.eggs/
.eggs/
*.egg-info/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> Implements a proof of concept beacon chain for a sharded pos ethereum 2.0. Spec in progress can be found [here](https://notes.ethereum.org/s/Syj3QZSxm).
## Installation
Using a python3 environment, run the following to install required libraries:
Using a python3.6.* environment, run the following to install required libraries:
```
pip install -e .[dev]
```
Expand All @@ -27,5 +27,5 @@ make deploy

---

# Simple Serialization
[here](https://github.com/ethereum/beacon_chain/tree/master/ssz)
# Simple Serialize
[here](https://github.com/ethereum/beacon_chain/tree/master/ssz)
8 changes: 8 additions & 0 deletions beacon_chain/state/active_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)

from .attestation_record import AttestationRecord
from .chain import Chain


class ActiveState():
Expand Down Expand Up @@ -31,6 +32,13 @@ def __init__(self, **kwargs):
else:
self.block_vote_cache = {}

# chain is not part of protocol state
# is used as helper class to aid in doing state transition
if 'chain' in kwargs:
self.chain = kwargs['chain']
else:
self.chain = Chain()

def __setattr__(self, name: str, value: Any) -> None:
super().__setattr__(name, value)

Expand Down
13 changes: 8 additions & 5 deletions beacon_chain/state/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
)

from beacon_chain.utils.blake import blake
from beacon_chain.state.constants import (
ZERO_HASH32,
)

from .attestation_record import AttestationRecord

Expand All @@ -31,13 +34,13 @@ class Block():
} # type: Dict[str, Any]

defaults = {
'parent_hash': b'\x00'*32,
'parent_hash': ZERO_HASH32,
'slot_number': 0,
'randao_reveal': b'\x00'*32,
'randao_reveal': ZERO_HASH32,
'attestations': [],
'pow_chain_ref': b'\x00'*32,
'active_state_root': b'\x00'*32,
'crystallized_state_root': b'\x00'*32,
'pow_chain_ref': ZERO_HASH32,
'active_state_root': ZERO_HASH32,
'crystallized_state_root': ZERO_HASH32,
}

def __init__(self, **kwargs):
Expand Down
49 changes: 49 additions & 0 deletions beacon_chain/state/chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import (
List,
TYPE_CHECKING,
)

if TYPE_CHECKING:
from .block import Block # noqa: F401


class Chain():
# Note, this is not an object defined in the v2.1 spec
# this is a helper object to mask complexity in tracking
# blocks

def __init__(self, head: 'Block'=None, blocks: List['Block']=[]) -> None:
self.head = head
self.blocks = blocks
self.chain = [] # type: List['Block']

# temp helper
all_blocks_by_hash = {
block.hash: block
for block in self.blocks
}

if self.head:
tmp = self.head
self.chain.append(tmp)
while all_blocks_by_hash.get(tmp.parent_hash, None):
tmp = all_blocks_by_hash[tmp.parent_hash]
self.chain.append(tmp)

self.block_by_hash = {
block.hash: block
for block in self.chain
}
self.block_by_slot_number = {
block.slot_number: block
for block in self.chain
}

def __contains__(self, block: 'Block') -> bool:
return bool(self.get_block_by_hash(block.hash))

def get_block_by_slot_number(self, slot_number: int) -> 'Block':
return self.block_by_slot_number.get(slot_number, None)

def get_block_by_hash(self, block_hash: bytes) -> 'Block':
return self.block_by_hash.get(block_hash, None)
17 changes: 14 additions & 3 deletions beacon_chain/state/config.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
from beacon_chain.state.constants import (
WEI_PER_ETH,
)


BASE_REWARD_QUOTIENT = 2**15
DEFAULT_END_DYNASTY = 9999999999999999999
DEPOSIT_SIZE = 32 # ETH
DEPOSIT_SIZE = 32 * WEI_PER_ETH # WEI
CYCLE_LENGTH = 64 # slots
MAX_VALIDATOR_COUNT = 2**22 # validators
MIN_COMMITTEE_SIZE = 128 # validators
MIN_DYNASTY_LENGTH = 256 # slots
SHARD_COUNT = 1024 # shards
SLOT_DURATION = 8 # seconds
SQRT_E_DROP_TIME = 2**20 # seconds


def generate_config(*,
base_reward_quotient=BASE_REWARD_QUOTIENT,
default_end_dynasty=DEFAULT_END_DYNASTY,
deposit_size=DEPOSIT_SIZE,
cycle_length=CYCLE_LENGTH,
max_validator_count=MAX_VALIDATOR_COUNT,
min_committee_size=MIN_COMMITTEE_SIZE,
min_dynasty_length=MIN_DYNASTY_LENGTH,
shard_count=SHARD_COUNT,
slot_duration=SLOT_DURATION):
slot_duration=SLOT_DURATION,
sqrt_e_drop_time=SQRT_E_DROP_TIME):
return {
'base_reward_quotient': base_reward_quotient,
'default_end_dynasty': default_end_dynasty,
'deposit_size': deposit_size,
'cycle_length': cycle_length,
'max_validator_count': max_validator_count,
'min_committee_size': min_committee_size,
'min_dynasty_length': min_dynasty_length,
'shard_count': shard_count,
'slot_duration': slot_duration
'slot_duration': slot_duration,
'sqrt_e_drop_time': sqrt_e_drop_time
}


Expand Down
3 changes: 3 additions & 0 deletions beacon_chain/state/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from eth_utils import denoms

from beacon_chain.beacon_typing.custom import (
Hash32,
)


WEI_PER_ETH = denoms.ether
ZERO_HASH32 = Hash32(32 * b'\x00')
20 changes: 19 additions & 1 deletion beacon_chain/state/crystallized_state.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import ( # noqa: F401
Any,
Dict,
List,
)

from .crosslink_record import CrosslinkRecord
from .shard_and_committee import ShardAndCommittee
from .validator_record import ValidatorRecord

from .helpers import get_active_validator_indices


class CrystallizedState():
fields = {
Expand Down Expand Up @@ -44,7 +47,6 @@ class CrystallizedState():
'last_finalized_slot': 0,
'current_dynasty': 0,
'crosslink_records': [],
'total_deposits': 0,
'dynasty_seed': b'\x00'*32,
'dynasty_start': 0,
} # type: Dict[str, Any]
Expand All @@ -60,6 +62,22 @@ def __setattr__(self, name: str, value: Any) -> None:
def __getattribute__(self, name: str) -> Any:
return super().__getattribute__(name)

@property
def active_validator_indices(self) -> List[int]:
return get_active_validator_indices(
self.current_dynasty,
self.validators
)

@property
def total_deposits(self) -> int:
return sum(
map(
lambda index: self.validators[index].balance,
self.active_validator_indices
)
)

@property
def num_validators(self) -> int:
return len(self.validators)
Expand Down
7 changes: 3 additions & 4 deletions beacon_chain/state/genesis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .active_state import ActiveState
from .block import Block
from .chain import Chain
from .constants import (
ZERO_HASH32,
)
Expand All @@ -30,7 +31,8 @@ def get_genesis_active_state(config: Dict[str, Any]) -> ActiveState:

return ActiveState(
pending_attestations=[],
recent_block_hashes=recent_block_hashes
recent_block_hashes=recent_block_hashes,
chain=Chain()
)


Expand All @@ -52,8 +54,6 @@ def get_genesis_crystallized_state(
# concatenate with itself to span 2*CYCLE_LENGTH
shard_and_committee_for_slots = shard_and_committee_for_slots + shard_and_committee_for_slots

total_deposits = config['deposit_size'] * len(validators)

return CrystallizedState(
validators=validators,
last_state_recalc=0,
Expand All @@ -67,7 +67,6 @@ def get_genesis_crystallized_state(
for i
in range(config['shard_count'])
],
total_deposits=total_deposits,
dynasty_seed=init_shuffling_seed,
dynasty_start=0,
)
Expand Down
Loading

0 comments on commit b3c20d3

Please sign in to comment.