From f15c68ae55c91af5f0da65a8870dc7b22271cca7 Mon Sep 17 00:00:00 2001 From: Posey Date: Sun, 23 Sep 2018 13:15:11 -0500 Subject: [PATCH 1/5] swap ints for uints --- beacon_chain/state/attestation_record.py | 8 ++++---- beacon_chain/state/block.py | 2 +- beacon_chain/state/crosslink_record.py | 4 ++-- beacon_chain/state/crystallized_state.py | 14 +++++++------- beacon_chain/state/partial_crosslink_record.py | 2 +- beacon_chain/state/recent_proposer_record.py | 4 ++-- beacon_chain/state/shard_and_committee.py | 4 ++-- beacon_chain/state/validator_record.py | 10 +++++----- ssz/ssz.py | 10 ++++++++++ tests/ssz/test_serialize.py | 2 +- 10 files changed, 35 insertions(+), 25 deletions(-) diff --git a/beacon_chain/state/attestation_record.py b/beacon_chain/state/attestation_record.py index b9fc282..594b464 100644 --- a/beacon_chain/state/attestation_record.py +++ b/beacon_chain/state/attestation_record.py @@ -10,14 +10,14 @@ class AttestationRecord(): fields = { - 'slot': 'int64', - 'shard_id': 'int16', + 'slot': 'uint64', + 'shard_id': 'uint16', 'oblique_parent_hashes': ['hash32'], 'shard_block_hash': 'hash32', 'attester_bitfield': 'bytes', - 'justified_slot': 'int64', + 'justified_slot': 'uint64', 'justified_block_hash': 'hash32', - 'aggregate_sig': ['int256'], + 'aggregate_sig': ['uint256'], } defaults = { 'slot': 0, diff --git a/beacon_chain/state/block.py b/beacon_chain/state/block.py index c0ef145..4064d80 100644 --- a/beacon_chain/state/block.py +++ b/beacon_chain/state/block.py @@ -17,7 +17,7 @@ class Block(): # Hash of the parent block 'parent_hash': 'hash32', # Slot number (for the PoS mechanism) - 'slot_number': 'int64', + 'slot_number': 'uint64', # Randao commitment reveal 'randao_reveal': 'hash32', # Attestations diff --git a/beacon_chain/state/crosslink_record.py b/beacon_chain/state/crosslink_record.py index 2063769..da9d783 100644 --- a/beacon_chain/state/crosslink_record.py +++ b/beacon_chain/state/crosslink_record.py @@ -7,9 +7,9 @@ class CrosslinkRecord(): fields = { # What dynasty the crosslink was submitted in - 'dynasty': 'int64', + 'dynasty': 'uint64', # slot during which crosslink was added - 'slot': 'int64', + 'slot': 'uint64', # The block hash 'hash': 'hash32', } diff --git a/beacon_chain/state/crystallized_state.py b/beacon_chain/state/crystallized_state.py index 7ca2dee..482bef8 100644 --- a/beacon_chain/state/crystallized_state.py +++ b/beacon_chain/state/crystallized_state.py @@ -13,27 +13,27 @@ class CrystallizedState(): # List of validators 'validators': [ValidatorRecord], # Last CrystallizedState recalculation - 'last_state_recalc': 'int64', + 'last_state_recalc': 'uint64', # What active validators are part of the attester set # at what height, and in what shard. Starts at slot # last_state_recalc - CYCLE_LENGTH 'shard_and_committee_for_slots': [[ShardAndCommittee]], # The last justified slot - 'last_justified_slot': 'int64', + 'last_justified_slot': 'uint64', # Number of consecutive justified slots ending at this one - 'justified_streak': 'int64', + 'justified_streak': 'uint64', # The last finalized slot - 'last_finalized_slot': 'int64', + 'last_finalized_slot': 'uint64', # The current dynasty - 'current_dynasty': 'int64', + 'current_dynasty': 'uint64', # Records about the most recent crosslink for each shard 'crosslink_records': [CrosslinkRecord], # Total balance of deposits - 'total_deposits': 'int256', + 'total_deposits': 'uint256', # Used to select the committees for each shard 'dynasty_seed': 'hash32', # start of the current dynasty - 'dynasty_start': 'int64', + 'dynasty_start': 'uint64', } defaults = { 'validators': [], diff --git a/beacon_chain/state/partial_crosslink_record.py b/beacon_chain/state/partial_crosslink_record.py index 5780cca..b999df7 100644 --- a/beacon_chain/state/partial_crosslink_record.py +++ b/beacon_chain/state/partial_crosslink_record.py @@ -8,7 +8,7 @@ class PartialCrosslinkRecord(): fields = { # What shard is the crosslink being made for - 'shard_id': 'int16', + 'shard_id': 'uint16', # Hash of the block 'shard_block_hash': 'hash32', # Which of the eligible voters are voting for it (as a bitfield) diff --git a/beacon_chain/state/recent_proposer_record.py b/beacon_chain/state/recent_proposer_record.py index a568c1b..47f2306 100644 --- a/beacon_chain/state/recent_proposer_record.py +++ b/beacon_chain/state/recent_proposer_record.py @@ -7,11 +7,11 @@ class RecentProposerRecord(): fields = { # Proposer index - 'index': 'int24', + 'index': 'uint24', # New RANDAO commitment 'randao_commitment': 'hash32', # Balance delta - 'balance_delta': 'int24' + 'balance_delta': 'uint24' } defaults = { 'randao_commitment': b'\x00'*32, diff --git a/beacon_chain/state/shard_and_committee.py b/beacon_chain/state/shard_and_committee.py index dbf4d25..2afd905 100644 --- a/beacon_chain/state/shard_and_committee.py +++ b/beacon_chain/state/shard_and_committee.py @@ -7,9 +7,9 @@ class ShardAndCommittee(): fields = { # The shard ID - 'shard_id': 'int16', + 'shard_id': 'uint16', # Validator indices - 'committee': ['int24'], + 'committee': ['uint24'], } defaults = { diff --git a/beacon_chain/state/validator_record.py b/beacon_chain/state/validator_record.py index 07fbb3a..360d929 100644 --- a/beacon_chain/state/validator_record.py +++ b/beacon_chain/state/validator_record.py @@ -7,19 +7,19 @@ class ValidatorRecord(): fields = { # The validator's public key - 'pubkey': 'int256', + 'pubkey': 'uint256', # What shard the validator's balance will be sent to after withdrawal - 'withdrawal_shard': 'int16', + 'withdrawal_shard': 'uint16', # And what address 'withdrawal_address': 'address', # The validator's current RANDAO beacon commitment 'randao_commitment': 'hash32', # Current balance - 'balance': 'int128', + 'balance': 'uint128', # Dynasty where the validator is inducted - 'start_dynasty': 'int64', + 'start_dynasty': 'uint64', # Dynasty where the validator leaves - 'end_dynasty': 'int64' + 'end_dynasty': 'uint64' } defaults = { diff --git a/ssz/ssz.py b/ssz/ssz.py index fad3e2f..e8a7894 100644 --- a/ssz/ssz.py +++ b/ssz/ssz.py @@ -9,7 +9,12 @@ def serialize(val, typ=None): return val elif isinstance(typ, str) and typ[:3] == 'int': length = int(typ[3:]) + assert length % 8 == 0 + return val.to_bytes(length // 8, 'big', signed=True) + elif isinstance(typ, str) and typ[:4] == 'uint': + length = int(typ[4:]) assert length % 8 == 0 + assert val >= 0 return val.to_bytes(length // 8, 'big') elif typ == 'bytes': return len(val).to_bytes(4, 'big') + val @@ -34,6 +39,11 @@ def _deserialize(data, start, typ): length = int(typ[3:]) assert length % 8 == 0 assert len(data) + start >= length // 8 + return int.from_bytes(data[start: start+length//8], 'big', signed=True), start+length//8 + elif isinstance(typ, str) and typ[:4] == 'uint': + length = int(typ[4:]) + assert length % 8 == 0 + assert len(data) + start >= length // 8 return int.from_bytes(data[start: start+length//8], 'big'), start+length//8 elif typ == 'bytes': length = int.from_bytes(data[start:start+4], 'big') diff --git a/tests/ssz/test_serialize.py b/tests/ssz/test_serialize.py index 72b9c08..c226a91 100644 --- a/tests/ssz/test_serialize.py +++ b/tests/ssz/test_serialize.py @@ -30,7 +30,7 @@ def test_basic_serialization(value, typ, data): (b'', 'byte'), (b'', 'hash16'), (0, 0), - (-5, 'int32'), + (-5, 'uint32'), ] ) def test_failed_serialization(value, typ): From eb2f4c6f1d9570ec43ab933f1ea32cf424f177e2 Mon Sep 17 00:00:00 2001 From: Poseyy <33185528+Poseyy@users.noreply.github.com> Date: Mon, 24 Sep 2018 09:37:07 -0500 Subject: [PATCH 2/5] Update crystallized_state.py --- beacon_chain/state/crystallized_state.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/beacon_chain/state/crystallized_state.py b/beacon_chain/state/crystallized_state.py index 16efcd1..0dc4167 100644 --- a/beacon_chain/state/crystallized_state.py +++ b/beacon_chain/state/crystallized_state.py @@ -31,8 +31,6 @@ class CrystallizedState(): 'current_dynasty': 'uint64', # Records about the most recent crosslink for each shard 'crosslink_records': [CrosslinkRecord], - # Total balance of deposits - 'total_deposits': 'uint256', # Used to select the committees for each shard 'dynasty_seed': 'hash32', # start of the current dynasty From 92be81d1088459c43cf63b4f82a01cfed0a3826c Mon Sep 17 00:00:00 2001 From: Poseyy <33185528+Poseyy@users.noreply.github.com> Date: Tue, 2 Oct 2018 14:21:11 -0500 Subject: [PATCH 3/5] Delete partial_crosslink_record.py --- .../state/partial_crosslink_record.py | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 beacon_chain/state/partial_crosslink_record.py diff --git a/beacon_chain/state/partial_crosslink_record.py b/beacon_chain/state/partial_crosslink_record.py deleted file mode 100644 index b999df7..0000000 --- a/beacon_chain/state/partial_crosslink_record.py +++ /dev/null @@ -1,32 +0,0 @@ -from typing import ( # noqa: F401 - Any, - Dict, -) - - -class PartialCrosslinkRecord(): - - fields = { - # What shard is the crosslink being made for - 'shard_id': 'uint16', - # Hash of the block - 'shard_block_hash': 'hash32', - # Which of the eligible voters are voting for it (as a bitfield) - 'voter_bitfield': 'bytes' - } - defaults = { - 'shard_id': 0, - 'shard_block_hash': b'\x00'*32, - 'voter_bitfield': b'', - } # type: Dict[str, Any] - - def __init__(self, **kwargs): - for k in self.fields.keys(): - assert k in kwargs or k in self.defaults, k - setattr(self, k, kwargs.get(k, self.defaults.get(k))) - - def __setattr__(self, name: str, value: Any) -> None: - super().__setattr__(name, value) - - def __getattribute__(self, name: str) -> Any: - return super().__getattribute__(name) From a7d02d083ca2233b4835a671ae057fa8e48ae60f Mon Sep 17 00:00:00 2001 From: Poseyy <33185528+Poseyy@users.noreply.github.com> Date: Tue, 2 Oct 2018 14:21:40 -0500 Subject: [PATCH 4/5] Delete recent_proposer_record.py --- beacon_chain/state/recent_proposer_record.py | 30 -------------------- 1 file changed, 30 deletions(-) delete mode 100644 beacon_chain/state/recent_proposer_record.py diff --git a/beacon_chain/state/recent_proposer_record.py b/beacon_chain/state/recent_proposer_record.py deleted file mode 100644 index 47f2306..0000000 --- a/beacon_chain/state/recent_proposer_record.py +++ /dev/null @@ -1,30 +0,0 @@ -from typing import ( # noqa: F401 - Any, - Dict, -) - - -class RecentProposerRecord(): - fields = { - # Proposer index - 'index': 'uint24', - # New RANDAO commitment - 'randao_commitment': 'hash32', - # Balance delta - 'balance_delta': 'uint24' - } - defaults = { - 'randao_commitment': b'\x00'*32, - 'balance_delta': 0 - } # type: Dict[str, Any] - - 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))) - - def __setattr__(self, name: str, value: Any) -> None: - super().__setattr__(name, value) - - def __getattribute__(self, name: str) -> Any: - return super().__getattribute__(name) From 04a6818e4fe218bf7c32c8f679e0cf85fb490733 Mon Sep 17 00:00:00 2001 From: Poseyy <33185528+Poseyy@users.noreply.github.com> Date: Tue, 2 Oct 2018 14:31:38 -0500 Subject: [PATCH 5/5] Delete test_recent_proposer_record.py --- tests/state/test_recent_proposer_record.py | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 tests/state/test_recent_proposer_record.py diff --git a/tests/state/test_recent_proposer_record.py b/tests/state/test_recent_proposer_record.py deleted file mode 100644 index bc20689..0000000 --- a/tests/state/test_recent_proposer_record.py +++ /dev/null @@ -1,19 +0,0 @@ -import pytest - -from beacon_chain.state.recent_proposer_record import ( - RecentProposerRecord -) - - -@pytest.mark.parametrize( - 'param,default_value', - [ - ('randao_commitment', b'\x00'*32), - ('balance_delta', 0), - ] -) -def test_defaults(param, default_value, sample_recent_proposer_record_params): - del sample_recent_proposer_record_params[param] - proposer = RecentProposerRecord(**sample_recent_proposer_record_params) - - assert getattr(proposer, param) == default_value