Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Merge pull request #30 from hwwhww/len_of_list
Browse files Browse the repository at this point in the history
Use `num` properties instead of `len()` functions
  • Loading branch information
hwwhww authored Jul 2, 2018
2 parents e9ec7b4 + e233c99 commit 8192419
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 66 deletions.
8 changes: 8 additions & 0 deletions beacon_chain/state/active_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ def __init__(self, **kwargs):
for k in self.fields.keys():
assert k in kwargs or k in self.defaults
setattr(self, k, kwargs.get(k, self.defaults.get(k)))

@property
def num_recent_attesters(self):
return len(self.recent_attesters)

@property
def num_recent_proposers(self):
return len(self.recent_proposers)
4 changes: 4 additions & 0 deletions beacon_chain/state/aggregate_vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ def __init__(self, **kwargs):
for k in self.fields.keys():
assert k in kwargs or k in self.defaults
setattr(self, k, kwargs.get(k, self.defaults.get(k)))

@property
def num_aggregate_sig(self):
return len(self.aggregate_sig)
12 changes: 12 additions & 0 deletions beacon_chain/state/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ def verify(self, pub):
@property
def hash(self):
return blake(serialize(self))

@property
def num_attestation_aggregate_sig(self):
return len(self.attestation_aggregate_sig)

@property
def num_shard_aggregate_votes(self):
return len(self.shard_aggregate_votes)

@property
def num_sig(self):
return len(self.sig)
21 changes: 20 additions & 1 deletion beacon_chain/state/crystallized_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CrystallizedState():
'active_validators': [],
'queued_validators': [],
'exited_validators': [],
'current_shuffling': ['int24'],
'current_shuffling': [],
'current_epoch': 0,
'last_justified_epoch': 0,
'last_finalized_epoch': 0,
Expand All @@ -49,3 +49,22 @@ def __init__(self, **kwargs):
for k in self.fields.keys():
assert k in kwargs or k in self.defaults
setattr(self, k, kwargs.get(k, self.defaults.get(k)))

# Check if num_active_validators == num_current_shuffling
assert self.num_active_validators == len(self.current_shuffling)

@property
def num_active_validators(self):
return len(self.active_validators)

@property
def num_queued_validators(self):
return len(self.queued_validators)

@property
def num_exited_validators(self):
return len(self.exited_validators)

@property
def num_crosslink_records(self):
return len(self.crosslink_records)
9 changes: 5 additions & 4 deletions beacon_chain/state/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_crosslink_shards(crystallized_state, config=DEFAULT_CONFIG):
)

crosslink_shards_count = get_crosslink_shards_count(
len(crystallized_state.active_validators),
crystallized_state.num_active_validators,
config=config,
)

Expand All @@ -54,18 +54,19 @@ def get_crosslink_notaries(
crystallized_state,
config=config,
)
num_crosslink_shards = len(crosslink_shards)

if shard_id not in crosslink_shards:
return None

num_active_validators = len(crystallized_state.active_validators)
num_active_validators = crystallized_state.num_active_validators

start = (
num_active_validators * crosslink_shards.index(shard_id) //
len(crosslink_shards)
num_crosslink_shards
)
end = (
num_active_validators * (crosslink_shards.index(shard_id) + 1) //
len(crosslink_shards)
num_crosslink_shards
)
return crystallized_state.current_shuffling[start:end]
18 changes: 9 additions & 9 deletions beacon_chain/state/state_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def get_attesters_and_proposer(crystallized_state,
skip_count,
config=DEFAULT_CONFIG):
attester_count = config['attester_count']
attestation_count = min(len(crystallized_state.active_validators), attester_count)
attestation_count = min(crystallized_state.num_active_validators, attester_count)

indices = get_shuffling(
active_state.randao,
len(crystallized_state.active_validators),
crystallized_state.num_active_validators,
attestation_count + skip_count + 1,
config
)
Expand All @@ -89,7 +89,7 @@ def get_attesters_and_proposer(crystallized_state,

# Get rewards and vote data
def process_ffg_deposits(crystallized_state, ffg_voter_bitfield):
total_validators = len(crystallized_state.active_validators)
total_validators = crystallized_state.num_active_validators
finality_distance = crystallized_state.current_epoch - crystallized_state.last_finalized_epoch
online_reward = 6 if finality_distance <= 2 else 0
offline_penalty = 3 * finality_distance
Expand Down Expand Up @@ -137,7 +137,7 @@ def process_crosslinks(crystallized_state, crosslinks, config=DEFAULT_CONFIG):
main_crosslink[c.shard_id] = (c.shard_block_hash, vote_count, c.voter_bitfield)
# Adjust crosslinks
new_crosslink_records = [x for x in crystallized_state.crosslink_records]
deltas = [0] * len(crystallized_state.active_validators)
deltas = [0] * crystallized_state.num_active_validators

# Process the shards that are selected to be crosslinking...
crosslink_shards = get_crosslink_shards(crystallized_state, config=config)
Expand Down Expand Up @@ -185,14 +185,14 @@ def process_crosslinks(crystallized_state, crosslinks, config=DEFAULT_CONFIG):


def process_recent_attesters(crystallized_state, recent_attesters, config=DEFAULT_CONFIG):
deltas = [0] * len(crystallized_state.active_validators)
deltas = [0] * crystallized_state.num_active_validators
for index in recent_attesters:
deltas[index] += config['attester_reward']
return deltas


def process_recent_proposers(crystallized_state, recent_proposers):
deltas = [0] * len(crystallized_state.active_validators)
deltas = [0] * crystallized_state.num_active_validators
for proposer in recent_proposers:
deltas[proposer.index] += proposer.balance_delta
return deltas
Expand All @@ -212,8 +212,8 @@ def get_incremented_validator_sets(crystallized_state,
else:
i += 1
induct = min(
len(crystallized_state.queued_validators),
len(crystallized_state.active_validators) // 30 + 1
crystallized_state.num_queued_validators,
crystallized_state.num_active_validators // 30 + 1
)
for i in range(induct):
if crystallized_state.queued_validators[i].switch_dynasty > crystallized_state.dynasty + 1:
Expand Down Expand Up @@ -382,7 +382,7 @@ def _initialize_new_epoch(crystallized_state, active_state, config=DEFAULT_CONFI
active_state = ActiveState(
height=active_state.height,
randao=active_state.randao,
ffg_voter_bitfield=get_empty_bitfield(len(crystallized_state.active_validators)),
ffg_voter_bitfield=get_empty_bitfield(crystallized_state.num_active_validators),
balance_deltas=[],
partial_crosslinks=[],
total_skip_count=active_state.total_skip_count,
Expand Down
3 changes: 1 addition & 2 deletions tests/state/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def sample_active_state_params():
'height': 30,
'randao': b'\x35'*32,
'ffg_voter_bitfield': b'\x42\x60',
'balance_deltas': [1, 2, 3],
'recent_attesters': [0, 2, 10],
'partial_crosslinks': [],
'total_skip_count': 33,
Expand Down Expand Up @@ -192,7 +191,7 @@ def genesis_active_state(genesis_crystallized_state,
return ActiveState(
height=1,
randao=init_randao,
ffg_voter_bitfield=bytearray((len(genesis_crystallized_state.active_validators) + 7) // 8),
ffg_voter_bitfield=get_empty_bitfield(genesis_crystallized_state.num_active_validators),
balance_deltas=[],
partial_crosslinks=[],
total_skip_count=0
Expand Down
43 changes: 43 additions & 0 deletions tests/state/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from beacon_chain.state.config import (
DEFAULT_CONFIG,
)
from beacon_chain.state.crystallized_state import (
CrystallizedState,
)
from beacon_chain.state.validator_record import (
ValidatorRecord,
)
from beacon_chain.state.state_transition import (
get_shuffling,
)


def mock_crystallized_state(
genesis_crystallized_state,
init_shuffling_seed,
next_shard,
active_validators=None,
config=DEFAULT_CONFIG):

crystallized_state = CrystallizedState()
crystallized_state.next_shard = next_shard
if active_validators is not None:
crystallized_state.active_validators = active_validators
crystallized_state.current_shuffling = get_shuffling(
init_shuffling_seed,
len(active_validators),
config=config,
)
return crystallized_state


def mock_validator_record(pubkey, config):
return ValidatorRecord(
pubkey=pubkey,
withdrawal_shard=0,
withdrawal_address=pubkey.to_bytes(32, 'big')[-20:],
randao_commitment=b'\x55'*32,
balance=config['default_balance'],
switch_dynasty=config['default_switch_dynasty']
)

15 changes: 14 additions & 1 deletion tests/state/test_active_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,18 @@ def test_recent_proposers(sample_active_state_params,
sample_active_state_params['recent_proposers'] = [recent_proposer]

active_state = ActiveState(**sample_active_state_params)
assert len(active_state.recent_proposers) == 1
assert active_state.num_recent_proposers == 1
assert eq(active_state.recent_proposers[0], recent_proposer)


def test_num_properties():
activate_state = ActiveState(
recent_attesters=[1, 2],
recent_proposers=[
RecentProposerRecord(index=index)
for index in range(3)
]
)

assert activate_state.num_recent_attesters == 2
assert activate_state.num_recent_proposers == 3
11 changes: 11 additions & 0 deletions tests/state/test_aggregate_vote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from beacon_chain.state.aggregate_vote import (
AggregateVote,
)


def test_num_properties():
aggregate_vote = AggregateVote(
aggregate_sig=list(range(3))
)

assert aggregate_vote.num_aggregate_sig == 3
15 changes: 15 additions & 0 deletions tests/state/test_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from beacon_chain.state.block import (
Block,
)
from beacon_chain.state.aggregate_vote import (
AggregateVote,
)
from beacon_chain.utils.bls import (
privtopub,
)
Expand Down Expand Up @@ -45,3 +48,15 @@ def test_block_hash():
assert block.hash != signed_block_hash_1
assert block.hash != signed_block_hash_2


def test_num_properties():
aggregate_vote = AggregateVote()
block = Block(
attestation_aggregate_sig=list(range(2)),
shard_aggregate_votes=[aggregate_vote],
sig=list(range(3)),
)

assert block.num_attestation_aggregate_sig == 2
assert block.num_shard_aggregate_votes == 1
assert block.num_sig == 3
41 changes: 41 additions & 0 deletions tests/state/test_crystallized_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from beacon_chain.state.crystallized_state import (
CrystallizedState,
)
from beacon_chain.state.crosslink_record import (
CrosslinkRecord,
)

from tests.state.helpers import (
mock_validator_record,
)


def test_num_properties(config):
active_validators = [
mock_validator_record(pubkey, config)
for pubkey in range(2)
]
queued_validators = [
mock_validator_record(pubkey, config)
for pubkey in range(3)
]
exited_validators = [
mock_validator_record(pubkey, config)
for pubkey in range(4)
]
crosslink_records = [
CrosslinkRecord(hash=b'\x00'*32, epoch=0) for i in range(5)
]

crystallized_state = CrystallizedState(
active_validators=active_validators,
queued_validators=queued_validators,
exited_validators=exited_validators,
current_shuffling=active_validators,
crosslink_records=crosslink_records,
)

assert crystallized_state.num_active_validators == 2
assert crystallized_state.num_queued_validators == 3
assert crystallized_state.num_exited_validators == 4
assert crystallized_state.num_crosslink_records == 5
Loading

0 comments on commit 8192419

Please sign in to comment.