Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion factom_core/block_elements/admin_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AddFederatedServerBitcoinAnchorKey.__post_init__ refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator

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"

Expand Down
8 changes: 4 additions & 4 deletions factom_core/block_elements/factoid_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Comment on lines -91 to +109

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FactoidTransaction.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore

data = data[1:] # skip 1 byte version number, always 0x01 for now
signature, data = primitives.FullSignature.unmarshal(data[:96]), data[96:]
rcds.append(signature)
Expand Down
4 changes: 1 addition & 3 deletions factom_core/blockchains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Blockchain.vm_for_hash refactored with the following changes:

  • Convert for loop into call to sum()
  • Simplify generator expression

return v % len(self.vms)

def seal_minute(self) -> None:
Expand Down
13 changes: 8 additions & 5 deletions factom_core/blocks/admin_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AdminBlockBody.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore
  • Use previously assigned local variable

admin_id, data = data[0], data[1:]
msg = None
if admin_id == MinuteNumber.ADMIN_ID: # Deprecated in M2
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Comment on lines -243 to +242

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AdminBlock.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore
  • Use previously assigned local variable

admin_id, data = data[0], data[1:]
msg = None
if admin_id == MinuteNumber.ADMIN_ID: # Deprecated in M2
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion factom_core/blocks/directory_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DirectoryBlockBody.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore

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})
Expand Down
6 changes: 2 additions & 4 deletions factom_core/blocks/entry_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EntryBlockBody.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore

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
Expand All @@ -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())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EntryBlockBody.construct_header refactored with the following changes:

  • Convert for loop into call to sum()

return EntryBlockHeader(
chain_id=chain_id,
body_mr=self.merkle_root,
Expand Down
2 changes: 1 addition & 1 deletion factom_core/blocks/entry_credit_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EntryCreditBlockBody.unmarshal_with_remainder refactored with the following changes:

  • Replace unused for index with underscore

ecid, data = data[0], data[1:]
if ecid == 0x00:
server_index, data = data[0], data[1:]
Expand Down
12 changes: 4 additions & 8 deletions factom_core/db/leveldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FactomdLevelDB.get_admin_block refactored with the following changes:

  • Inline variable that is only used once


def get_admin_block_head(self) -> Union[blocks.AdminBlock, None]:
prev_hash = self.get_chain_head(blocks.AdminBlockHeader.CHAIN_ID)
Expand Down Expand Up @@ -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)
Comment on lines -161 to +160

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FactomdLevelDB.get_factoid_block refactored with the following changes:

  • Inline variable that is only used once


def get_factoid_block_head(self) -> Union[blocks.FactoidBlock, None]:
prev_keymr = self.get_chain_head(blocks.FactoidBlockHeader.CHAIN_ID)
Expand Down Expand Up @@ -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)
Comment on lines -198 to +196

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FactomdLevelDB.get_entry_credit_block refactored with the following changes:

  • Inline variable that is only used once


def get_entry_credit_block_head(self) -> Union[blocks.EntryCreditBlock, None]:
prev_hash = self.get_chain_head(blocks.EntryCreditBlockHeader.CHAIN_ID)
Expand All @@ -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)
Comment on lines -226 to +223

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FactomdLevelDB.get_entry_block refactored with the following changes:

  • Inline variable that is only used once


def get_entry_block_head(self, chain_id: bytes) -> Union[blocks.EntryBlock, None]:
prev_keymr = self.get_chain_head(chain_id)
Expand Down
4 changes: 2 additions & 2 deletions factom_core/messages/block_syncing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Comment on lines -97 to +103

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DirectoryBlockState.unmarshal refactored with the following changes:

  • Replace unused for index with underscore

entry_size, data = struct.unpack(">I", data[:4])[0], data[4:]
entry, data = Entry.unmarshal(data[:entry_size]), data[entry_size:]
entries.append(entry)
Expand Down
2 changes: 1 addition & 1 deletion factom_core/primitives/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FullSignatureList.unmarshal refactored with the following changes:

  • Replace unused for index with underscore

signature, data = FullSignature.unmarshal(data[:96]), data[96:]
signatures.append(signature)
assert len(data) == 0, "Extra bytes remaining!"
Expand Down
2 changes: 1 addition & 1 deletion factom_core/utils/merkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function build_merkle_tree refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator

return hashes

next_level = []
Expand Down
4 changes: 2 additions & 2 deletions factom_core/utils/varint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function encode refactored with the following changes:

  • Replace assignment with augmented assignment


return bytes(buf)

Expand All @@ -34,7 +34,7 @@ def decode(raw: bytes):
data = raw
while True:
i, data = ord(data[:1]), data[1:]
result = result << 7
result <<= 7

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function decode refactored with the following changes:

  • Replace assignment with augmented assignment

result += i & 0x7F
if i < 0x80:
break
Expand Down
2 changes: 1 addition & 1 deletion p2p/parcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parcel.__init__ refactored with the following changes:

  • Simplify logical expression

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")
Expand Down