Skip to content

Commit

Permalink
Merge pull request #120 from Poseyy/master
Browse files Browse the repository at this point in the history
Explicit uint vs int support *everywhere*
  • Loading branch information
djrtwo committed Oct 3, 2018
2 parents eaf1482 + 04a6818 commit ccf75cc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
8 changes: 4 additions & 4 deletions beacon_chain/state/attestation_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/state/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/state/crosslink_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
12 changes: 6 additions & 6 deletions beacon_chain/state/crystallized_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [],
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/state/shard_and_committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class ShardAndCommittee():
fields = {
# The shard ID
'shard_id': 'int16',
'shard_id': 'uint16',
# Validator indices
'committee': ['int24'],
'committee': ['uint24'],
}

defaults = {
Expand Down
10 changes: 5 additions & 5 deletions beacon_chain/state/validator_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
10 changes: 10 additions & 0 deletions ssz/ssz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/ssz/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ccf75cc

Please sign in to comment.