-
Couldn't load subscription status.
- Fork 2
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| if config.p2p_port: | ||
| self.p2p_port = config.p2p_port | ||
| else: | ||
| self.p2p_port = None | ||
| self.p2p_port = config.p2p_port or None | ||
| peer_db = PeerDB(self.chain, self.data_dir) | ||
| self.p2p_manager = P2pManager(self, self.p2p_port, peer_db) | ||
|
|
||
| if config.rpc_port: | ||
| self.rpc_port = config.rpc_port | ||
| else: | ||
| self.rpc_port = None | ||
| self.rpc_port = config.rpc_port or None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Node.__init__ refactored with the following changes:
- Replace if statement with if expression [×2] (
assign-if-exp) - Simplify if expression by using or [×2] (
or-if-exp-identity)
| if prevout: | ||
| prevout = TxOut.parse(prevout, check_validity=False) | ||
| prev_outputs.append(prevout) | ||
| self.removed_utxos.add(prevout_bytes) | ||
| else: | ||
| if not prevout: | ||
| raise Exception | ||
|
|
||
| prevout = TxOut.parse(prevout, check_validity=False) | ||
| prev_outputs.append(prevout) | ||
| self.removed_utxos.add(prevout_bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Chainstate.add_block refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| elif self.db.get(out_point_bytes): | ||
| self.removed_utxos.add(out_point_bytes) | ||
| else: | ||
| if self.db.get(out_point_bytes): | ||
| self.removed_utxos.add(out_point_bytes) | ||
| else: | ||
| raise Exception | ||
| raise Exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Chainstate.apply_rev_block refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| data_dir = Path(data_dir) | ||
| else: | ||
| data_dir = Path.home() / ".btclib" | ||
| data_dir = Path(data_dir) if data_dir else Path.home() / ".btclib" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Config.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
btclib_node/download.py
Outdated
| if node.status >= NodeStatus.HeaderSynced: | ||
| if node.status < NodeStatus.HeaderSynced: | ||
| return | ||
| if not node.download_window: | ||
| node.download_window = node.index.get_download_candidates() | ||
| node.download_window = [ | ||
| x | ||
| for x in node.download_window | ||
| if not node.index.get_block_info(x).downloaded | ||
| ] | ||
| if not node.download_window: | ||
| return | ||
|
|
||
| if not node.download_window: | ||
| node.download_window = node.index.get_download_candidates() | ||
| node.download_window = [ | ||
| x | ||
| for x in node.download_window | ||
| if not node.index.get_block_info(x).downloaded | ||
| connections = list(node.p2p_manager.connections.values()) | ||
| pending = [] | ||
| exit = True | ||
| for conn in connections: | ||
| conn_queue = conn.block_download_queue | ||
| new_queue = [ | ||
| header | ||
| for header in conn_queue | ||
| if not node.index.get_block_info(header).downloaded | ||
| ] | ||
| if not node.download_window: | ||
| return | ||
| conn.block_download_queue = new_queue | ||
| pending.extend(new_queue) | ||
| if not new_queue: | ||
| exit = False | ||
| if exit: | ||
| return | ||
|
|
||
| connections = list(node.p2p_manager.connections.values()) | ||
| pending = [] | ||
| exit = True | ||
| for conn in connections: | ||
| conn_queue = conn.block_download_queue | ||
| new_queue = [] | ||
| for header in conn_queue: | ||
| if not node.index.get_block_info(header).downloaded: | ||
| new_queue.append(header) | ||
| conn.block_download_queue = new_queue | ||
| pending.extend(new_queue) | ||
| if not new_queue: | ||
| exit = False | ||
| if exit: | ||
| return | ||
| waiting = [header for header in node.download_window if header not in pending] | ||
| pending = [x[0] for x in Counter(pending).most_common()[::-1]] | ||
|
|
||
| waiting = [header for header in node.download_window if header not in pending] | ||
| pending = [x[0] for x in Counter(pending).most_common()[::-1]] | ||
|
|
||
| for conn in connections: | ||
| if conn.block_download_queue == []: | ||
| if waiting: | ||
| new = waiting[:16] | ||
| waiting = waiting[16:] | ||
| elif pending: | ||
| new = pending[:4] | ||
| pending = pending[4:] | ||
| else: | ||
| return | ||
| conn.block_download_queue = new | ||
| conn.send(Getdata([(0x40000002, hash) for hash in new])) | ||
| for conn in connections: | ||
| if conn.block_download_queue == []: | ||
| if waiting: | ||
| new = waiting[:16] | ||
| waiting = waiting[16:] | ||
| elif pending: | ||
| new = pending[:4] | ||
| pending = pending[4:] | ||
| else: | ||
| return | ||
| conn.block_download_queue = new | ||
| conn.send(Getdata([(0x40000002, hash) for hash in new])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function block_download refactored with the following changes:
- Add guard clause (
last-if-guard) - Convert for loop into list comprehension (
list-comprehension)
btclib_node/p2p/callbacks.py
Outdated
| transactions = [x[1] for x in inv.inventory if x[0] == 1 or x[0] == 0x40000001] | ||
| blocks = [x[1] for x in inv.inventory if x[0] == 2 or x[0] == 0x40000002] | ||
| if blocks: | ||
| transactions = [x[1] for x in inv.inventory if x[0] in [1, 0x40000001]] | ||
| if blocks := [x[1] for x in inv.inventory if x[0] in [2, 0x40000002]]: | ||
| block_locators = node.index.get_block_locator_hashes() | ||
| conn.send(Getheaders(ProtocolVersion, block_locators, blocks[-1])) | ||
|
|
||
| missing_tx = node.mempool.get_missing(transactions) | ||
| if missing_tx: | ||
| if missing_tx := node.mempool.get_missing(transactions): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function inv refactored with the following changes:
- Use named expression to simplify assignment and conditional [×2] (
use-named-expression) - Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons)
btclib_node/p2p/callbacks.py
Outdated
| transactions = [x[1] for x in getdata.inventory if x[0] == 1 or x[0] == 0x40000001] | ||
| blocks = [x[1] for x in getdata.inventory if x[0] == 2 or x[0] == 0x40000002] | ||
| transactions = [x[1] for x in getdata.inventory if x[0] in [1, 0x40000001]] | ||
| blocks = [x[1] for x in getdata.inventory if x[0] in [2, 0x40000002]] | ||
| for txid in transactions: | ||
| tx = node.mempool.get_tx(txid) | ||
| if tx: | ||
| if tx := node.mempool.get_tx(txid): | ||
| conn.send(TxMsg(tx)) | ||
| for block_hash in blocks: | ||
| block = node.block_db.get_block(block_hash) | ||
| if block: | ||
| if block := node.block_db.get_block(block_hash): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function getdata refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons) - Use named expression to simplify assignment and conditional [×2] (
use-named-expression)
| else: | ||
| if node.status == NodeStatus.SyncingHeaders: | ||
| node.status = NodeStatus.HeaderSynced | ||
| elif node.status == NodeStatus.SyncingHeaders: | ||
| node.status = NodeStatus.HeaderSynced |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function headers refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
btclib_node/p2p/callbacks.py
Outdated
| headers = node.index.get_headers_from_locators( | ||
| if headers := node.index.get_headers_from_locators( | ||
| getheaders.block_hashes, getheaders.hash_stop | ||
| ) | ||
| if headers: | ||
| ): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function getheaders refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| elif conn.status == P2pConnStatus.Closed: | ||
| pass | ||
| else: | ||
| elif conn.status != P2pConnStatus.Closed: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function handle_p2p_handshake refactored with the following changes:
- Remove empty elif clause (
remove-pass-elif)
| elif conn.status == P2pConnStatus.Closed: | ||
| pass | ||
| else: | ||
| elif conn.status != P2pConnStatus.Closed: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function handle_p2p refactored with the following changes:
- Remove empty elif clause (
remove-pass-elif)
| connection_num = 1 | ||
| else: | ||
| connection_num = 10 | ||
| connection_num = 1 if self.node.status < NodeStatus.HeaderSynced else 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function P2pManager.manage_connections refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| addresses = [] | ||
| for x in range(len_addresses): | ||
| addresses.append(NetworkAddress.deserialize(stream)) | ||
| addresses = [NetworkAddress.deserialize(stream) for _ in range(len_addresses)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Addr.deserialize refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace unused for index with underscore (
for-index-underscore)
| short_ids = [] | ||
| short_ids_length = var_int.parse(stream) | ||
| for x in range(short_ids_length): | ||
| short_ids.append(stream.read(6)[::-1]) | ||
| short_ids = [stream.read(6)[::-1] for _ in range(short_ids_length)] | ||
| prefilled_tx_list = [] | ||
| prefilled_tx_num = var_int.parse(stream) | ||
| for x in range(prefilled_tx_num): | ||
| for _ in range(prefilled_tx_num): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Cmpctblock.deserialize refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block) - Replace unused for index with underscore [×2] (
for-index-underscore) - Convert for loop into list comprehension (
list-comprehension)
| headers_num = var_int.parse(stream) | ||
| headers = [] | ||
| for x in range(headers_num): | ||
| for _ in range(headers_num): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Headers.deserialize refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| response = "HTTP/1.1 200 OK\n" | ||
| response += "Content-Type: application/json\n" | ||
| response = "HTTP/1.1 200 OK\n" + "Content-Type: application/json\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Connection.async_send refactored with the following changes:
- Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign)
| try: | ||
| conn = manager.connections[id] | ||
| return conn | ||
| return manager.connections[id] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_connection refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if "method" not in request: | ||
| return False | ||
| if "id" not in request: | ||
| return False | ||
| return True | ||
| return False if "method" not in request else "id" in request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_valid_rpc refactored with the following changes:
- Lift code into else after jump in control flow [×2] (
reintroduce-else) - Replace if statement with if expression [×2] (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
| if "params" in request: | ||
| params = request["params"] | ||
| else: | ||
| params = [] | ||
| params = request["params"] if "params" in request else [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function handle_rpc refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| previous_block_hash = chain[-1].hash | ||
| else: | ||
| previous_block_hash = start | ||
| previous_block_hash = chain[-1].hash if chain else start |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function generate_random_header_chain refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| for block in chain: | ||
| node.block_db.add_block(block) | ||
| for x in range(len(chain)): | ||
| for _ in range(len(chain)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.89%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!