From 186dca550d98d758e9f8dab878e6382153efeaf3 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Mon, 8 Jun 2020 21:53:51 +0000 Subject: [PATCH] Refactored by Sourcery --- factom_core/block_elements/admin_messages.py | 2 +- factom_core/block_elements/factoid_transaction.py | 8 ++++---- factom_core/blockchains/base.py | 4 +--- factom_core/blocks/admin_block.py | 13 ++++++++----- factom_core/blocks/directory_block.py | 2 +- factom_core/blocks/entry_block.py | 6 ++---- factom_core/blocks/entry_credit_block.py | 2 +- factom_core/db/leveldb.py | 12 ++++-------- factom_core/messages/block_syncing.py | 4 ++-- factom_core/primitives/signatures.py | 2 +- factom_core/utils/merkle.py | 2 +- factom_core/utils/varint.py | 4 ++-- p2p/parcel.py | 2 +- 13 files changed, 29 insertions(+), 34 deletions(-) diff --git a/factom_core/block_elements/admin_messages.py b/factom_core/block_elements/admin_messages.py index 5d55eb8..5a6b54d 100644 --- a/factom_core/block_elements/admin_messages.py +++ b/factom_core/block_elements/admin_messages.py @@ -512,7 +512,7 @@ class AddFederatedServerBitcoinAnchorKey(AdminMessage): def __post_init__(self): assert len(self.chain_id) == 32, "chain_id must be a bytes object of length 32" - assert self.hash_type == 0 or self.hash_type == 1, "hash_type must be 0 (p2pkh) or 1 (p2sh)" + assert self.hash_type in [0, 1], "hash_type must be 0 (p2pkh) or 1 (p2sh)" assert 0 <= self.priority <= 255, "priority must be in range(0, 256)" assert len(self.public_key_hash) == 20, "public_key_hash must be a bytes object of length 20" diff --git a/factom_core/block_elements/factoid_transaction.py b/factom_core/block_elements/factoid_transaction.py index 647a1d0..6e88440 100644 --- a/factom_core/block_elements/factoid_transaction.py +++ b/factom_core/block_elements/factoid_transaction.py @@ -88,25 +88,25 @@ def unmarshal_with_remainder(cls, raw: bytes): ec_purchase_count, data = ord(data[:1]), data[1:] inputs = [] - for i in range(input_count): + for _ in range(input_count): value, data = varint.decode(data) fct_address, data = data[:32], data[32:] inputs.append({"value": value, "fct_address": fct_address}) outputs = [] - for i in range(output_count): + for _ in range(output_count): value, data = varint.decode(data) fct_address, data = data[:32], data[32:] outputs.append({"value": value, "fct_address": fct_address}) ec_purchases = [] - for i in range(ec_purchase_count): + for _ in range(ec_purchase_count): value, data = varint.decode(data) ec_public_key, data = data[:32], data[32:] ec_purchases.append({"value": value, "ec_public_key": ec_public_key}) rcds = primitives.FullSignatureList() - for i in range(input_count): + for _ in range(input_count): data = data[1:] # skip 1 byte version number, always 0x01 for now signature, data = primitives.FullSignature.unmarshal(data[:96]), data[96:] rcds.append(signature) diff --git a/factom_core/blockchains/base.py b/factom_core/blockchains/base.py index 76dbbb7..7e1d676 100644 --- a/factom_core/blockchains/base.py +++ b/factom_core/blockchains/base.py @@ -64,9 +64,7 @@ def vm_for_hash(self, h: bytes) -> int: """ if len(self.vms) == 0: return 0 - v = 0 - for b in h: - v += b + v = sum(h) return v % len(self.vms) def seal_minute(self) -> None: diff --git a/factom_core/blocks/admin_block.py b/factom_core/blocks/admin_block.py index e61012d..192baad 100644 --- a/factom_core/blocks/admin_block.py +++ b/factom_core/blocks/admin_block.py @@ -93,7 +93,7 @@ def unmarshal(cls, raw: bytes, message_count: int): def unmarshal_with_remainder(cls, raw: bytes, message_count: int): data = raw messages = [] - for i in range(message_count): + for _ in range(message_count): admin_id, data = data[0], data[1:] msg = None if admin_id == MinuteNumber.ADMIN_ID: # Deprecated in M2 @@ -167,8 +167,7 @@ def unmarshal_with_remainder(cls, raw: bytes, message_count: int): elif admin_id <= 0x0E: msg = admin_id - print(f"Unsupported admin message type {admin_id} found") - + print(f'Unsupported admin message type {msg} found') if msg is not None: messages.append(msg) @@ -240,7 +239,7 @@ def unmarshal_with_remainder(cls, raw: bytes): header, data = AdminBlockHeader.unmarshal_with_remainder(raw) messages = [] - for i in range(header.message_count): + for _ in range(header.message_count): admin_id, data = data[0], data[1:] msg = None if admin_id == MinuteNumber.ADMIN_ID: # Deprecated in M2 @@ -314,7 +313,11 @@ def unmarshal_with_remainder(cls, raw: bytes): elif admin_id <= 0x0E: msg = admin_id - print("Unsupported admin message type {} found at Admin Block {}".format(admin_id, header.height)) + print( + "Unsupported admin message type {} found at Admin Block {}".format( + msg, header.height + ) + ) if msg is not None: messages.append(msg) diff --git a/factom_core/blocks/directory_block.py b/factom_core/blocks/directory_block.py index d85ae36..a076c4d 100644 --- a/factom_core/blocks/directory_block.py +++ b/factom_core/blocks/directory_block.py @@ -125,7 +125,7 @@ def unmarshal_with_remainder(cls, raw: bytes, block_count: int): assert factoid_block_chain_id == factom_core.blocks.FactoidBlockHeader.CHAIN_ID factoid_block_keymr, data = data[:32], data[32:] entry_blocks = [] - for i in range(block_count - 3): + for _ in range(block_count - 3): entry_block_chain_id, data = data[:32], data[32:] entry_block_keymr, data = data[:32], data[32:] entry_blocks.append({"chain_id": entry_block_chain_id, "keymr": entry_block_keymr}) diff --git a/factom_core/blocks/entry_block.py b/factom_core/blocks/entry_block.py index be66345..04aa118 100644 --- a/factom_core/blocks/entry_block.py +++ b/factom_core/blocks/entry_block.py @@ -101,7 +101,7 @@ def unmarshal_with_remainder(cls, raw: bytes, entry_count: int): # Entry hashes are listed in order, with a minute marker following what minute those entries were in entry_hashes = {} current_minute_entries = [] - for i in range(entry_count): + for _ in range(entry_count): entry_hash, data = data[:32], data[32:] if entry_hash[:-1] == bytes(31) and entry_hash[-1] <= 10: entry_hashes[entry_hash[-1]] = current_minute_entries @@ -118,9 +118,7 @@ def construct_header( Creates an returns an EntryBlockHeader for this body object, given the specified contextual parameters. """ - entry_count = 0 - for hashes in self.entry_hashes.values(): - entry_count += len(hashes) + entry_count = sum(len(hashes) for hashes in self.entry_hashes.values()) return EntryBlockHeader( chain_id=chain_id, body_mr=self.merkle_root, diff --git a/factom_core/blocks/entry_credit_block.py b/factom_core/blocks/entry_credit_block.py index 4c2d5ac..26aabfb 100644 --- a/factom_core/blocks/entry_credit_block.py +++ b/factom_core/blocks/entry_credit_block.py @@ -123,7 +123,7 @@ def unmarshal_with_remainder(cls, raw: bytes, object_count: int): data = raw objects = {} # map of minute --> objects array current_minute_objects = [] - for i in range(object_count): + for _ in range(object_count): ecid, data = data[0], data[1:] if ecid == 0x00: server_index, data = data[0], data[1:] diff --git a/factom_core/db/leveldb.py b/factom_core/db/leveldb.py index 87ae1ef..043c5f0 100644 --- a/factom_core/db/leveldb.py +++ b/factom_core/db/leveldb.py @@ -121,8 +121,7 @@ def get_admin_block(self, **kwargs) -> Union[blocks.AdminBlock, None]: raw = sub_db.get(lookup_hash) if raw is None: return None - block = blocks.AdminBlock.unmarshal(raw) - return block + return blocks.AdminBlock.unmarshal(raw) def get_admin_block_head(self) -> Union[blocks.AdminBlock, None]: prev_hash = self.get_chain_head(blocks.AdminBlockHeader.CHAIN_ID) @@ -158,8 +157,7 @@ def get_factoid_block(self, **kwargs) -> Union[blocks.FactoidBlock, None]: raw = sub_db.get(keymr) if raw is None: return None - block = blocks.FactoidBlock.unmarshal(raw) - return block + return blocks.FactoidBlock.unmarshal(raw) def get_factoid_block_head(self) -> Union[blocks.FactoidBlock, None]: prev_keymr = self.get_chain_head(blocks.FactoidBlockHeader.CHAIN_ID) @@ -195,8 +193,7 @@ def get_entry_credit_block(self, **kwargs) -> Union[blocks.EntryCreditBlock, Non raw = sub_db.get(header_hash) if raw is None: return None - block = blocks.EntryCreditBlock.unmarshal(raw) - return block + return blocks.EntryCreditBlock.unmarshal(raw) def get_entry_credit_block_head(self) -> Union[blocks.EntryCreditBlock, None]: prev_hash = self.get_chain_head(blocks.EntryCreditBlockHeader.CHAIN_ID) @@ -223,8 +220,7 @@ def get_entry_block(self, keymr: bytes) -> Union[blocks.EntryBlock, None]: raw = sub_db.get(keymr) if raw is None: return None - block = blocks.EntryBlock.unmarshal(raw) - return block + return blocks.EntryBlock.unmarshal(raw) def get_entry_block_head(self, chain_id: bytes) -> Union[blocks.EntryBlock, None]: prev_keymr = self.get_chain_head(chain_id) diff --git a/factom_core/messages/block_syncing.py b/factom_core/messages/block_syncing.py index 66afb31..ac8ec17 100644 --- a/factom_core/messages/block_syncing.py +++ b/factom_core/messages/block_syncing.py @@ -94,13 +94,13 @@ def unmarshal(cls, raw: bytes): entry_block_count, data = struct.unpack(">I", data[:4])[0], data[4:] entry_blocks = [] - for i in range(entry_block_count): + for _ in range(entry_block_count): entry_block, data = EntryBlock.unmarshal_with_remainder(data) entry_blocks.append(entry_block) entry_count, data = struct.unpack(">I", data[:4])[0], data[4:] entries = [] - for i in range(entry_count): + for _ in range(entry_count): entry_size, data = struct.unpack(">I", data[:4])[0], data[4:] entry, data = Entry.unmarshal(data[:entry_size]), data[entry_size:] entries.append(entry) diff --git a/factom_core/primitives/signatures.py b/factom_core/primitives/signatures.py index 85aed16..08edbd9 100644 --- a/factom_core/primitives/signatures.py +++ b/factom_core/primitives/signatures.py @@ -41,7 +41,7 @@ def marshal(self) -> bytes: def unmarshal(cls, raw: bytes): length, data = struct.unpack(">I", raw[:4])[0], raw[4:] signatures = [] - for i in range(length): + for _ in range(length): signature, data = FullSignature.unmarshal(data[:96]), data[96:] signatures.append(signature) assert len(data) == 0, "Extra bytes remaining!" diff --git a/factom_core/utils/merkle.py b/factom_core/utils/merkle.py index f8d8e86..5aee782 100644 --- a/factom_core/utils/merkle.py +++ b/factom_core/utils/merkle.py @@ -7,7 +7,7 @@ def get_merkle_root(hashes: list) -> bytes: def build_merkle_tree(hashes: list): - if len(hashes) == 0 or len(hashes) == 1: + if len(hashes) in [0, 1]: return hashes next_level = [] diff --git a/factom_core/utils/varint.py b/factom_core/utils/varint.py index 04e4f2a..2eaf5fa 100644 --- a/factom_core/utils/varint.py +++ b/factom_core/utils/varint.py @@ -21,7 +21,7 @@ def encode(number: int): b = b if b < 256 else struct.pack(">Q", b)[-1] buf.append(b) - h = h << 7 + h <<= 7 return bytes(buf) @@ -34,7 +34,7 @@ def decode(raw: bytes): data = raw while True: i, data = ord(data[:1]), data[1:] - result = result << 7 + result <<= 7 result += i & 0x7F if i < 0x80: break diff --git a/p2p/parcel.py b/p2p/parcel.py index 9bebe03..f29321f 100644 --- a/p2p/parcel.py +++ b/p2p/parcel.py @@ -36,7 +36,7 @@ def __init__(self, parcel_type: ParcelType, address: str, payload: bytes): """ if not ParcelType.is_valid(parcel_type): raise ValueError("Invalid parcel_type provided") - elif not type(address) is not str and address is not None: + elif type(address) is str and address is not None: raise ValueError("address must be a string or None") elif type(payload) is not bytes or len(payload) == 0: raise ValueError("payload must be a bytes object of non-zero length")