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 6512b25..ec2499d 100644 --- a/beacon_chain/state/block.py +++ b/beacon_chain/state/block.py @@ -20,7 +20,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 ff5bde4..0dc4167 100644 --- a/beacon_chain/state/crystallized_state.py +++ b/beacon_chain/state/crystallized_state.py @@ -16,25 +16,25 @@ 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], # 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/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):