Skip to content

Commit

Permalink
undo some unnecessary int type casts (#17521)
Browse files Browse the repository at this point in the history
* undo redundant type-casts from slots, SubEpochSummary and SubEpochData

* undo redundant type-casts from proof-of-space, reward chain and foliage types

* undo redundant type-casts from VDFInfo, VDFProof and ClassgroupElement

* remove unnecessary type annotation
  • Loading branch information
arvidn authored Feb 8, 2024
1 parent 76bb49c commit fe89b5b
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 92 deletions.
10 changes: 4 additions & 6 deletions chia/consensus/block_body_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ def include_spends(self, npc_result: Optional[NPCResult], block: FullBlock, head
self.removals_since_fork[bytes32(spend.coin_id)] = ForkRem(bytes32(spend.puzzle_hash), height)
for puzzle_hash, amount, hint in spend.create_coin:
coin = Coin(bytes32(spend.coin_id), bytes32(puzzle_hash), uint64(amount))
self.additions_since_fork[coin.name()] = ForkAdd(
coin, uint32(height), uint64(timestamp), hint, False
)
self.additions_since_fork[coin.name()] = ForkAdd(coin, height, timestamp, hint, False)
for coin in block.get_included_reward_coins():
assert block.foliage_transaction_block is not None
timestamp = block.foliage_transaction_block.timestamp
assert coin.name() not in self.additions_since_fork
self.additions_since_fork[coin.name()] = ForkAdd(coin, uint32(block.height), uint64(timestamp), None, True)
self.additions_since_fork[coin.name()] = ForkAdd(coin, block.height, timestamp, None, True)

def rollback(self, header_hash: bytes32, height: int) -> None:
assert height <= self.peak_height
Expand Down Expand Up @@ -400,7 +398,7 @@ async def validate_block_body(
height,
height,
False,
uint64(block.foliage_transaction_block.timestamp),
block.foliage_transaction_block.timestamp,
)
removal_coin_records[new_unspent.name] = new_unspent
else:
Expand Down Expand Up @@ -500,7 +498,7 @@ async def validate_block_body(
block_timestamp: uint64
if height < constants.SOFT_FORK2_HEIGHT:
# this does not happen on mainnet. testnet10 only
block_timestamp = uint64(block.foliage_transaction_block.timestamp) # pragma: no cover
block_timestamp = block.foliage_transaction_block.timestamp # pragma: no cover
else:
block_timestamp = prev_transaction_block_timestamp

Expand Down
10 changes: 5 additions & 5 deletions chia/consensus/block_header_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def validate_unfinished_header_block(
if genesis_block and header_block.prev_header_hash != constants.GENESIS_CHALLENGE:
return None, ValidationError(Err.INVALID_PREV_BLOCK_HASH)

overflow = is_overflow_block(constants, uint8(header_block.reward_chain_block.signage_point_index))
overflow = is_overflow_block(constants, header_block.reward_chain_block.signage_point_index)
if skip_overflow_last_ss_validation and overflow:
if final_eos_is_already_included(header_block, blocks, expected_sub_slot_iters):
skip_overflow_last_ss_validation = False
Expand Down Expand Up @@ -524,13 +524,13 @@ def validate_unfinished_header_block(
sp_iters: uint64 = calculate_sp_iters(
constants,
expected_sub_slot_iters,
uint8(header_block.reward_chain_block.signage_point_index),
header_block.reward_chain_block.signage_point_index,
)

ip_iters: uint64 = calculate_ip_iters(
constants,
expected_sub_slot_iters,
uint8(header_block.reward_chain_block.signage_point_index),
header_block.reward_chain_block.signage_point_index,
required_iters,
)
if header_block.reward_chain_block.challenge_chain_sp_vdf is None:
Expand Down Expand Up @@ -876,7 +876,7 @@ def validate_finished_header_block(
ip_iters: uint64 = calculate_ip_iters(
constants,
expected_sub_slot_iters,
uint8(header_block.reward_chain_block.signage_point_index),
header_block.reward_chain_block.signage_point_index,
required_iters,
)
if not genesis_block:
Expand Down Expand Up @@ -983,7 +983,7 @@ def validate_finished_header_block(

# 31. Check infused challenge chain infusion point VDF
if not genesis_block:
overflow = is_overflow_block(constants, uint8(header_block.reward_chain_block.signage_point_index))
overflow = is_overflow_block(constants, header_block.reward_chain_block.signage_point_index)
deficit = calculate_deficit(
constants,
header_block.height,
Expand Down
12 changes: 6 additions & 6 deletions chia/consensus/full_block_to_block_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def block_to_block_record(
sub_slot_iters, _ = get_next_sub_slot_iters_and_difficulty(
constants, len(block.finished_sub_slots) > 0, prev_b, blocks
)
overflow = is_overflow_block(constants, uint8(block.reward_chain_block.signage_point_index))
overflow = is_overflow_block(constants, block.reward_chain_block.signage_point_index)
deficit = calculate_deficit(
constants,
block.height,
Expand All @@ -62,8 +62,8 @@ def block_to_block_record(
blocks,
block.height,
blocks.block_record(prev_b.prev_hash),
uint64.construct_optional(block.finished_sub_slots[0].challenge_chain.new_difficulty),
uint64.construct_optional(block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters),
block.finished_sub_slots[0].challenge_chain.new_difficulty,
block.finished_sub_slots[0].challenge_chain.new_sub_slot_iters,
)
if ses.get_hash() != found_ses_hash:
raise ValueError(Err.INVALID_SUB_EPOCH_SUMMARY)
Expand Down Expand Up @@ -148,7 +148,7 @@ def header_block_to_sub_block_record(
block.height,
block.weight,
block.total_iters,
uint8(block.reward_chain_block.signage_point_index),
block.reward_chain_block.signage_point_index,
block.reward_chain_block.challenge_chain_ip_vdf.output,
icc_output,
block.reward_chain_block.get_hash(),
Expand All @@ -160,9 +160,9 @@ def header_block_to_sub_block_record(
deficit,
overflow,
prev_transaction_block_height,
uint64.construct_optional(timestamp),
timestamp,
prev_transaction_block_hash,
uint64.construct_optional(fees),
fees,
reward_claims_incorporated,
finished_challenge_slot_hashes,
finished_infused_challenge_slot_hashes,
Expand Down
2 changes: 1 addition & 1 deletion chia/consensus/make_sub_epoch_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def next_sub_epoch_summary(
Returns:
object: the new sub-epoch summary
"""
signage_point_index = uint8(block.reward_chain_block.signage_point_index)
signage_point_index = block.reward_chain_block.signage_point_index
prev_b: Optional[BlockRecord] = blocks.try_block_record(block.prev_header_hash)
if prev_b is None or prev_b.height == 0:
return None
Expand Down
4 changes: 2 additions & 2 deletions chia/consensus/multiprocess_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from chia.util.condition_tools import pkm_pairs
from chia.util.errors import Err, ValidationError
from chia.util.generator_tools import get_block_header, tx_removals_and_additions
from chia.util.ints import uint8, uint16, uint32, uint64
from chia.util.ints import uint16, uint32, uint64
from chia.util.streamable import Streamable, streamable

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -282,7 +282,7 @@ async def pre_validate_blocks_multiprocessing(
constants, len(block.finished_sub_slots) > 0, prev_b, block_records
)

overflow = is_overflow_block(constants, uint8(block.reward_chain_block.signage_point_index))
overflow = is_overflow_block(constants, block.reward_chain_block.signage_point_index)
challenge = get_block_challenge(constants, block, BlockCache(recent_blocks), prev_b is None, overflow, False)
if block.reward_chain_block.challenge_chain_sp_vdf is None:
cc_sp_hash: bytes32 = challenge
Expand Down
2 changes: 1 addition & 1 deletion chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ async def new_infusion_point_vdf(
+ calculate_sp_iters(
self.constants,
sub_slot_iters,
uint8(unfinished_block.reward_chain_block.signage_point_index),
unfinished_block.reward_chain_block.signage_point_index,
)
)

Expand Down
4 changes: 2 additions & 2 deletions chia/full_node/full_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,9 @@ def get_pool_sig(_1: PoolTarget, _2: Optional[G1Element]) -> Optional[G2Element]
sub_slot_iters = peak.sub_slot_iters
for sub_slot in finished_sub_slots:
if sub_slot.challenge_chain.new_difficulty is not None:
difficulty = uint64(sub_slot.challenge_chain.new_difficulty)
difficulty = sub_slot.challenge_chain.new_difficulty
if sub_slot.challenge_chain.new_sub_slot_iters is not None:
sub_slot_iters = uint64(sub_slot.challenge_chain.new_sub_slot_iters)
sub_slot_iters = sub_slot.challenge_chain.new_sub_slot_iters

required_iters: uint64 = calculate_iterations_quality(
self.full_node.constants.DIFFICULTY_CONSTANT_FACTOR,
Expand Down
48 changes: 24 additions & 24 deletions chia/full_node/weight_proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ async def __first_sub_slot_vdfs(
None,
None,
None,
uint8(curr.reward_chain_block.signage_point_index),
curr.reward_chain_block.signage_point_index,
None,
None,
None,
Expand Down Expand Up @@ -549,7 +549,7 @@ def handle_block_vdfs(self, curr: HeaderBlock, blocks: Dict[bytes32, BlockRecord
curr.challenge_chain_ip_proof,
icc_ip_proof,
cc_sp_info,
uint8(curr.reward_chain_block.signage_point_index),
curr.reward_chain_block.signage_point_index,
None,
None,
None,
Expand All @@ -565,7 +565,7 @@ def validate_weight_proof_single_proc(self, weight_proof: WeightProof) -> Tuple[
if len(weight_proof.sub_epochs) == 0:
return False, uint32(0)

peak_height = uint32(weight_proof.recent_chain_data[-1].reward_chain_block.height)
peak_height = weight_proof.recent_chain_data[-1].reward_chain_block.height
log.info(f"validate weight proof peak height {peak_height}")
summaries, sub_epoch_weight_list = _validate_sub_epoch_summaries(self.constants, weight_proof)
if summaries is None:
Expand Down Expand Up @@ -707,7 +707,7 @@ def _create_sub_epoch_data(
) -> SubEpochData:
reward_chain_hash: bytes32 = sub_epoch_summary.reward_chain_hash
# Number of subblocks overflow in previous slot
previous_sub_epoch_overflows = uint8(sub_epoch_summary.num_blocks_overflow) # total in sub epoch - expected
previous_sub_epoch_overflows = sub_epoch_summary.num_blocks_overflow # total in sub epoch - expected
# New work difficulty and iterations per sub-slot
sub_slot_iters = sub_epoch_summary.new_sub_slot_iters
new_difficulty = sub_epoch_summary.new_difficulty
Expand Down Expand Up @@ -746,7 +746,7 @@ async def _challenge_block_vdfs(
header_block.challenge_chain_ip_proof,
None,
cc_sp_info,
uint8(header_block.reward_chain_block.signage_point_index),
header_block.reward_chain_block.signage_point_index,
None,
None,
None,
Expand Down Expand Up @@ -886,7 +886,7 @@ def _map_sub_epoch_summaries(

# if new epoch update diff and iters
if data.new_difficulty is not None:
curr_difficulty = uint64(data.new_difficulty)
curr_difficulty = data.new_difficulty

# add to dict
summaries.append(ses)
Expand Down Expand Up @@ -998,7 +998,7 @@ def _validate_segment(
return False, uint64(0), uint64(0), uint64(0), []
assert sub_slot_data.signage_point_index is not None
ip_iters = ip_iters + calculate_ip_iters(
constants, curr_ssi, uint8(sub_slot_data.signage_point_index), required_iters
constants, curr_ssi, sub_slot_data.signage_point_index, required_iters
)
vdf_list = _get_challenge_block_vdfs(constants, idx, segment.sub_slots, curr_ssi)
to_validate.extend(vdf_list)
Expand All @@ -1025,7 +1025,7 @@ def _get_challenge_block_vdfs(
assert sub_slot_data.signage_point_index
sp_input = ClassgroupElement.get_default_element()
if not sub_slot_data.cc_signage_point.normalized_to_identity and sub_slot_idx >= 1:
is_overflow = is_overflow_block(constants, uint8(sub_slot_data.signage_point_index))
is_overflow = is_overflow_block(constants, sub_slot_data.signage_point_index)
prev_ssd = sub_slots[sub_slot_idx - 1]
sp_input = sub_slot_data_vdf_input(
constants, sub_slot_data, sub_slot_idx, sub_slots, is_overflow, prev_ssd.is_end_of_slot(), ssi
Expand Down Expand Up @@ -1103,7 +1103,7 @@ def _validate_sub_slot_data(
assert sub_slot_data.cc_sp_vdf_info
input = ClassgroupElement.get_default_element()
if not sub_slot_data.cc_signage_point.normalized_to_identity:
is_overflow = is_overflow_block(constants, uint8(sub_slot_data.signage_point_index))
is_overflow = is_overflow_block(constants, sub_slot_data.signage_point_index)
input = sub_slot_data_vdf_input(
constants, sub_slot_data, sub_slot_idx, sub_slots, is_overflow, prev_ssd.is_end_of_slot(), ssi
)
Expand Down Expand Up @@ -1208,9 +1208,9 @@ def validate_recent_blocks(
last_blocks_to_validate = 100 # todo remove cap after benchmarks
for summary in summaries[:ses_idx]:
if summary.new_sub_slot_iters is not None:
ssi = uint64(summary.new_sub_slot_iters)
ssi = summary.new_sub_slot_iters
if summary.new_difficulty is not None:
diff = uint64(summary.new_difficulty)
diff = summary.new_difficulty

ses_blocks, sub_slots, transaction_blocks = 0, 0, 0
challenge, prev_challenge = recent_chain.recent_chain_data[0].reward_chain_block.pos_ss_cc_challenge_hash, None
Expand All @@ -1226,18 +1226,18 @@ def validate_recent_blocks(
for sub_slot in block.finished_sub_slots:
prev_challenge = sub_slot.challenge_chain.challenge_chain_end_of_slot_vdf.challenge
challenge = sub_slot.challenge_chain.get_hash()
deficit = uint8(sub_slot.reward_chain.deficit)
deficit = sub_slot.reward_chain.deficit
if sub_slot.challenge_chain.subepoch_summary_hash is not None:
ses = True
assert summaries[ses_idx].get_hash() == sub_slot.challenge_chain.subepoch_summary_hash
ses_idx += 1
if sub_slot.challenge_chain.new_sub_slot_iters is not None:
ssi = uint64(sub_slot.challenge_chain.new_sub_slot_iters)
ssi = sub_slot.challenge_chain.new_sub_slot_iters
if sub_slot.challenge_chain.new_difficulty is not None:
diff = uint64(sub_slot.challenge_chain.new_difficulty)
diff = sub_slot.challenge_chain.new_difficulty

if (challenge is not None) and (prev_challenge is not None):
overflow = is_overflow_block(constants, uint8(block.reward_chain_block.signage_point_index))
overflow = is_overflow_block(constants, block.reward_chain_block.signage_point_index)
if not adjusted:
assert prev_block_record is not None
prev_block_record = dataclasses.replace(
Expand Down Expand Up @@ -1334,7 +1334,7 @@ def __validate_pospace(

sub_slot_data: SubSlotData = segment.sub_slots[idx]

if sub_slot_data.signage_point_index and is_overflow_block(constants, uint8(sub_slot_data.signage_point_index)):
if sub_slot_data.signage_point_index and is_overflow_block(constants, sub_slot_data.signage_point_index):
curr_slot = segment.sub_slots[idx - 1]
assert curr_slot.cc_slot_end_info
challenge = curr_slot.cc_slot_end_info.challenge
Expand Down Expand Up @@ -1391,14 +1391,14 @@ def __get_rc_sub_slot(
slots_n = 1
assert first
assert first.signage_point_index is not None
if is_overflow_block(constants, uint8(first.signage_point_index)):
if is_overflow_block(constants, first.signage_point_index):
if idx >= 2 and slots[idx - 2].cc_slot_end is None:
slots_n = 2

new_diff = None if ses is None else ses.new_difficulty
new_ssi = None if ses is None else ses.new_sub_slot_iters
ses_hash: Optional[bytes32] = None if ses is None else ses.get_hash()
overflow = is_overflow_block(constants, uint8(first.signage_point_index))
overflow = is_overflow_block(constants, first.signage_point_index)
if overflow:
if idx >= 2 and slots[idx - 2].cc_slot_end is not None and slots[idx - 1].cc_slot_end is not None:
ses_hash = None
Expand Down Expand Up @@ -1483,9 +1483,9 @@ def _get_curr_diff_ssi(
curr_ssi = constants.SUB_SLOT_ITERS_STARTING
for ses in reversed(summaries[0:idx]):
if ses.new_sub_slot_iters is not None:
curr_ssi = uint64(ses.new_sub_slot_iters)
curr_ssi = ses.new_sub_slot_iters
assert ses.new_difficulty is not None
curr_difficulty = uint64(ses.new_difficulty)
curr_difficulty = ses.new_difficulty
break

return curr_difficulty, curr_ssi
Expand Down Expand Up @@ -1521,7 +1521,7 @@ def _get_last_ses_hash(
if slot.challenge_chain.subepoch_summary_hash is not None:
return (
slot.challenge_chain.subepoch_summary_hash,
uint32(curr.reward_chain_block.height),
curr.reward_chain_block.height,
)
idx += 1
return None, uint32(0)
Expand Down Expand Up @@ -1558,8 +1558,8 @@ def get_sp_total_iters(
assert sub_slot_data.cc_ip_vdf_info is not None
assert sub_slot_data.total_iters is not None
assert sub_slot_data.signage_point_index is not None
sp_iters: uint64 = calculate_sp_iters(constants, ssi, uint8(sub_slot_data.signage_point_index))
ip_iters: uint64 = uint64(sub_slot_data.cc_ip_vdf_info.number_of_iterations)
sp_iters = calculate_sp_iters(constants, ssi, sub_slot_data.signage_point_index)
ip_iters = sub_slot_data.cc_ip_vdf_info.number_of_iterations
sp_sub_slot_total_iters = uint128(sub_slot_data.total_iters - ip_iters)
if is_overflow:
sp_sub_slot_total_iters = uint128(sp_sub_slot_total_iters - ssi)
Expand Down Expand Up @@ -1645,7 +1645,7 @@ async def validate_weight_proof_inner(
if len(weight_proof.sub_epochs) == 0:
return False, []

peak_height = uint32(weight_proof.recent_chain_data[-1].reward_chain_block.height)
peak_height = weight_proof.recent_chain_data[-1].reward_chain_block.height
log.info(f"validate weight proof peak height {peak_height}")
seed = summaries[-2].get_hash()
rng = random.Random(seed)
Expand Down
Loading

0 comments on commit fe89b5b

Please sign in to comment.