Skip to content

Commit

Permalink
Fix remaining linting issues (#10962)
Browse files Browse the repository at this point in the history
* FIx remaining linting issues

* Revert type:ignore

* Revert token_bytes change
  • Loading branch information
mariano54 authored Apr 2, 2022
1 parent 8b5c701 commit 890c7d3
Show file tree
Hide file tree
Showing 25 changed files with 170 additions and 287 deletions.
10 changes: 3 additions & 7 deletions benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
from chia.types.blockchain_format.classgroup import ClassgroupElement
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.sized_bytes import bytes32, bytes100
from chia.types.blockchain_format.vdf import VDFInfo, VDFProof
from chia.types.blockchain_format.foliage import Foliage, FoliageBlockData, FoliageTransactionBlock, TransactionsInfo
from chia.types.blockchain_format.pool_target import PoolTarget
Expand Down Expand Up @@ -56,9 +56,7 @@ def rand_bytes(num) -> bytes:


def rand_hash() -> bytes32:
# TODO: address hint errors and remove ignores
# error: Incompatible return value type (got "bytes", expected "bytes32") [return-value]
return rand_bytes(32) # type: ignore[return-value]
return bytes32(rand_bytes(32))


def rand_g1() -> G1Element:
Expand All @@ -72,9 +70,7 @@ def rand_g2() -> G2Element:


def rand_class_group_element() -> ClassgroupElement:
# TODO: address hint errors and remove ignores
# error: Argument 1 to "ClassgroupElement" has incompatible type "bytes"; expected "bytes100" [arg-type]
return ClassgroupElement(rand_bytes(100)) # type: ignore[arg-type]
return ClassgroupElement(bytes100(rand_bytes(100)))


def rand_vdf() -> VDFInfo:
Expand Down
12 changes: 3 additions & 9 deletions chia/consensus/block_body_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,12 @@ async def validate_block_body(
return root_error, None

# 12. The additions and removals must result in the correct filter
byte_array_tx: List[bytes32] = []
byte_array_tx: List[bytearray] = []

for coin in additions + coinbase_additions:
# TODO: address hint error and remove ignore
# error: Argument 1 to "append" of "list" has incompatible type "bytearray"; expected "bytes32"
# [arg-type]
byte_array_tx.append(bytearray(coin.puzzle_hash)) # type: ignore[arg-type]
byte_array_tx.append(bytearray(coin.puzzle_hash))
for coin_name in removals:
# TODO: address hint error and remove ignore
# error: Argument 1 to "append" of "list" has incompatible type "bytearray"; expected "bytes32"
# [arg-type]
byte_array_tx.append(bytearray(coin_name)) # type: ignore[arg-type]
byte_array_tx.append(bytearray(coin_name))

bip158: PyBIP158 = PyBIP158(byte_array_tx)
encoded_filter = bytes(bip158.GetEncoded())
Expand Down
22 changes: 5 additions & 17 deletions chia/consensus/block_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
log = logging.getLogger(__name__)


# TODO: address hint error and remove ignore
# error: Incompatible default for argument "seed" (default has type "bytes", argument has type "bytes32")
# [assignment]
def create_foliage(
constants: ConsensusConstants,
reward_block_unfinished: RewardChainBlockUnfinished,
Expand All @@ -53,7 +50,7 @@ def create_foliage(
pool_target: PoolTarget,
get_plot_signature: Callable[[bytes32, G1Element], G2Element],
get_pool_signature: Callable[[PoolTarget, Optional[G1Element]], Optional[G2Element]],
seed: bytes32 = b"", # type: ignore[assignment]
seed: bytes = b"",
) -> Tuple[Foliage, Optional[FoliageTransactionBlock], Optional[TransactionsInfo]]:
"""
Creates a foliage for a given reward chain block. This may or may not be a tx block. In the case of a tx block,
Expand Down Expand Up @@ -95,7 +92,7 @@ def create_foliage(
height = uint32(prev_block.height + 1)

# Create filter
byte_array_tx: List[bytes32] = []
byte_array_tx: List[bytearray] = []
tx_additions: List[Coin] = []
tx_removals: List[bytes32] = []

Expand Down Expand Up @@ -192,16 +189,10 @@ def create_foliage(
additions.extend(reward_claims_incorporated.copy())
for coin in additions:
tx_additions.append(coin)
# TODO: address hint error and remove ignore
# error: Argument 1 to "append" of "list" has incompatible type "bytearray"; expected "bytes32"
# [arg-type]
byte_array_tx.append(bytearray(coin.puzzle_hash)) # type: ignore[arg-type]
byte_array_tx.append(bytearray(coin.puzzle_hash))
for coin in removals:
tx_removals.append(coin.name())
# TODO: address hint error and remove ignore
# error: Argument 1 to "append" of "list" has incompatible type "bytearray"; expected "bytes32"
# [arg-type]
byte_array_tx.append(bytearray(coin.name())) # type: ignore[arg-type]
byte_array_tx.append(bytearray(coin.name()))

bip158: PyBIP158 = PyBIP158(byte_array_tx)
encoded = bytes(bip158.GetEncoded())
Expand Down Expand Up @@ -289,9 +280,6 @@ def create_foliage(
return foliage, foliage_transaction_block, transactions_info


# TODO: address hint error and remove ignore
# error: Incompatible default for argument "seed" (default has type "bytes", argument has type "bytes32")
# [assignment]
def create_unfinished_block(
constants: ConsensusConstants,
sub_slot_start_total_iters: uint128,
Expand All @@ -308,7 +296,7 @@ def create_unfinished_block(
signage_point: SignagePoint,
timestamp: uint64,
blocks: BlockchainInterface,
seed: bytes32 = b"", # type: ignore[assignment]
seed: bytes = b"",
block_generator: Optional[BlockGenerator] = None,
aggregate_sig: G2Element = G2Element(),
additions: Optional[List[Coin]] = None,
Expand Down
49 changes: 20 additions & 29 deletions chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ async def get_full_peak(self) -> Optional[FullBlock]:
if self._peak_height is None:
return None
""" Return list of FullBlocks that are peaks"""
# TODO: address hint error and remove ignore
# error: Argument 1 to "get_full_block" of "BlockStore" has incompatible type "Optional[bytes32]";
# expected "bytes32" [arg-type]
block = await self.block_store.get_full_block(self.height_to_hash(self._peak_height)) # type: ignore[arg-type]
peak_hash: Optional[bytes32] = self.height_to_hash(self._peak_height)
assert peak_hash is not None # Since we must have the peak block
block = await self.block_store.get_full_block(peak_hash)
assert block is not None
return block

Expand Down Expand Up @@ -308,12 +307,9 @@ def get_hint_list(self, npc_result: NPCResult) -> List[Tuple[bytes32, bytes]]:
if opcode == ConditionOpcode.CREATE_COIN:
for condition in conditions:
if len(condition.vars) > 2 and condition.vars[2] != b"":
puzzle_hash, amount_bin = condition.vars[0], condition.vars[1]
amount = int_from_bytes(amount_bin)
# TODO: address hint error and remove ignore
# error: Argument 2 to "Coin" has incompatible type "bytes"; expected "bytes32"
# [arg-type]
coin_id = Coin(npc.coin_name, puzzle_hash, amount).name() # type: ignore[arg-type]
puzzle_hash, amount_bin = bytes32(condition.vars[0]), condition.vars[1]
amount: uint64 = uint64(int_from_bytes(amount_bin))
coin_id: bytes32 = Coin(npc.coin_name, puzzle_hash, amount).name()
h_list.append((coin_id, condition.vars[2]))
return h_list

Expand Down Expand Up @@ -679,11 +675,11 @@ def block_record(self, header_hash: bytes32) -> BlockRecord:
return self.__block_records[header_hash]

def height_to_block_record(self, height: uint32) -> BlockRecord:
header_hash = self.height_to_hash(height)
# TODO: address hint error and remove ignore
# error: Argument 1 to "block_record" of "Blockchain" has incompatible type "Optional[bytes32]"; expected
# "bytes32" [arg-type]
return self.block_record(header_hash) # type: ignore[arg-type]
# Precondition: height is in the blockchain
header_hash: Optional[bytes32] = self.height_to_hash(height)
if header_hash is None:
raise ValueError(f"Height is not in blockchain: {height}")
return self.block_record(header_hash)

def get_ses_heights(self) -> List[uint32]:
return self.__height_map.get_ses_heights()
Expand Down Expand Up @@ -762,10 +758,8 @@ async def get_header_blocks_in_range(
hashes = []
for height in range(start, stop + 1):
if self.contains_height(uint32(height)):
# TODO: address hint error and remove ignore
# error: Incompatible types in assignment (expression has type "Optional[bytes32]", variable has
# type "bytes32") [assignment]
header_hash: bytes32 = self.height_to_hash(uint32(height)) # type: ignore[assignment]
header_hash: Optional[bytes32] = self.height_to_hash(uint32(height))
assert header_hash is not None
hashes.append(header_hash)

blocks: List[FullBlock] = []
Expand Down Expand Up @@ -810,23 +804,20 @@ async def get_block_records_at(self, heights: List[uint32], batch_size=900) -> L
gets block records by height (only blocks that are part of the chain)
"""
records: List[BlockRecord] = []
hashes = []
hashes: List[bytes32] = []
assert batch_size < 999 # sqlite in python 3.7 has a limit on 999 variables in queries
for height in heights:
hashes.append(self.height_to_hash(height))
header_hash: Optional[bytes32] = self.height_to_hash(height)
if header_hash is None:
raise ValueError(f"Do not have block at height {height}")
hashes.append(header_hash)
if len(hashes) > batch_size:
# TODO: address hint error and remove ignore
# error: Argument 1 to "get_block_records_by_hash" of "BlockStore" has incompatible type
# "List[Optional[bytes32]]"; expected "List[bytes32]" [arg-type]
res = await self.block_store.get_block_records_by_hash(hashes) # type: ignore[arg-type]
res = await self.block_store.get_block_records_by_hash(hashes)
records.extend(res)
hashes = []

if len(hashes) > 0:
# TODO: address hint error and remove ignore
# error: Argument 1 to "get_block_records_by_hash" of "BlockStore" has incompatible type
# "List[Optional[bytes32]]"; expected "List[bytes32]" [arg-type]
res = await self.block_store.get_block_records_by_hash(hashes) # type: ignore[arg-type]
res = await self.block_store.get_block_records_by_hash(hashes)
records.extend(res)
return records

Expand Down
14 changes: 4 additions & 10 deletions chia/consensus/multiprocess_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def batch_pre_validate_blocks(
expected_sub_slot_iters: List[uint64],
validate_signatures: bool,
) -> List[bytes]:
blocks: Dict[bytes, BlockRecord] = {}
blocks: Dict[bytes32, BlockRecord] = {}
for k, v in blocks_pickled.items():
blocks[k] = BlockRecord.from_bytes(v)
blocks[bytes32(k)] = BlockRecord.from_bytes(v)
results: List[PreValidationResult] = []
constants: ConsensusConstants = dataclass_from_dict(ConsensusConstants, constants_dict)
if full_blocks_pickled is not None and header_blocks_pickled is not None:
Expand Down Expand Up @@ -99,12 +99,9 @@ def batch_pre_validate_blocks(
continue

header_block = get_block_header(block, tx_additions, removals)
# TODO: address hint error and remove ignore
# error: Argument 1 to "BlockCache" has incompatible type "Dict[bytes, BlockRecord]"; expected
# "Dict[bytes32, BlockRecord]" [arg-type]
required_iters, error = validate_finished_header_block(
constants,
BlockCache(blocks), # type: ignore[arg-type]
BlockCache(blocks),
header_block,
check_filter,
expected_difficulty[i],
Expand Down Expand Up @@ -144,12 +141,9 @@ def batch_pre_validate_blocks(
for i in range(len(header_blocks_pickled)):
try:
header_block = HeaderBlock.from_bytes(header_blocks_pickled[i])
# TODO: address hint error and remove ignore
# error: Argument 1 to "BlockCache" has incompatible type "Dict[bytes, BlockRecord]"; expected
# "Dict[bytes32, BlockRecord]" [arg-type]
required_iters, error = validate_finished_header_block(
constants,
BlockCache(blocks), # type: ignore[arg-type]
BlockCache(blocks),
header_block,
check_filter,
expected_difficulty[i],
Expand Down
7 changes: 2 additions & 5 deletions chia/full_node/block_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,9 @@ async def get_blocks_by_hash(self, header_hashes: List[bytes32]) -> List[FullBlo
async with self.db_wrapper.read_db() as conn:
async with conn.execute(formatted_str, header_hashes_db) as cursor:
for row in await cursor.fetchall():
header_hash = self.maybe_from_hex(row[0])
header_hash = bytes32(self.maybe_from_hex(row[0]))
full_block: FullBlock = self.maybe_decompress(row[1])
# TODO: address hint error and remove ignore
# error: Invalid index type "bytes" for "Dict[bytes32, FullBlock]";
# expected type "bytes32" [index]
all_blocks[header_hash] = full_block # type: ignore[index]
all_blocks[header_hash] = full_block
self.block_cache.put(header_hash, full_block)
ret: List[FullBlock] = []
for hh in header_hashes:
Expand Down
7 changes: 3 additions & 4 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,10 +1297,9 @@ async def peak_post_processing(
fork_block: Optional[BlockRecord] = None
if fork_height != block.height - 1 and block.height != 0:
# This is a reorg
# TODO: address hint error and remove ignore
# error: Argument 1 to "block_record" of "Blockchain" has incompatible type "Optional[bytes32]";
# expected "bytes32" [arg-type]
fork_block = self.blockchain.block_record(self.blockchain.height_to_hash(fork_height)) # type: ignore[arg-type] # noqa: E501
fork_hash: Optional[bytes32] = self.blockchain.height_to_hash(fork_height)
assert fork_hash is not None
fork_block = self.blockchain.block_record(fork_hash)

fns_peak_result: FullNodeStorePeakResult = self.full_node_store.new_peak(
record,
Expand Down
Loading

0 comments on commit 890c7d3

Please sign in to comment.